*** 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-decode-rfc1522)
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-tree-buffer "*Tree*"
1537   "Buffer where Gnus thread trees are displayed.")
1538
1539 ;; Dummy variable.
1540 (defvar gnus-use-generic-from nil)
1541
1542 (defvar gnus-thread-indent-array nil)
1543 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1544
1545 (defvar gnus-newsrc-file-version nil)
1546
1547 (defvar gnus-method-history nil)
1548 ;; Variable holding the user answers to all method prompts.
1549
1550 (defvar gnus-group-history nil)
1551 ;; Variable holding the user answers to all group prompts.
1552
1553 (defvar gnus-server-alist nil
1554   "List of available servers.")
1555
1556 (defvar gnus-group-indentation-function nil)
1557
1558 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1559
1560 (defvar gnus-goto-missing-group-function nil)
1561
1562 (defvar gnus-override-subscribe-method nil)
1563
1564 (defvar gnus-group-goto-next-group-function nil
1565   "Function to override finding the next group after listing groups.")
1566
1567 (defconst gnus-article-mark-lists
1568   '((marked . tick) (replied . reply)
1569     (expirable . expire) (killed . killed)
1570     (bookmarks . bookmark) (dormant . dormant)
1571     (scored . score) (saved . save)
1572     (cached . cache)
1573     ))
1574
1575 ;; Avoid highlighting in kill files.
1576 (defvar gnus-summary-inhibit-highlight nil)
1577 (defvar gnus-newsgroup-selected-overlay nil)
1578
1579 (defvar gnus-inhibit-hiding nil)
1580 (defvar gnus-group-indentation "")
1581 (defvar gnus-inhibit-limiting nil)
1582 (defvar gnus-created-frames nil)
1583
1584 (defvar gnus-article-mode-map nil)
1585 (defvar gnus-dribble-buffer nil)
1586 (defvar gnus-headers-retrieved-by nil)
1587 (defvar gnus-article-reply nil)
1588 (defvar gnus-override-method nil)
1589 (defvar gnus-article-check-size nil)
1590
1591 (defvar gnus-current-score-file nil)
1592 (defvar gnus-newsgroup-adaptive-score-file nil)
1593 (defvar gnus-scores-exclude-files nil)
1594
1595 (defvar gnus-opened-servers nil)
1596
1597 (defvar gnus-current-move-group nil)
1598 (defvar gnus-current-copy-group nil)
1599 (defvar gnus-current-crosspost-group nil)
1600
1601 (defvar gnus-newsgroup-dependencies nil)
1602 (defvar gnus-newsgroup-async nil)
1603 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1604
1605 (defvar gnus-newsgroup-adaptive nil)
1606
1607 (defvar gnus-summary-display-table nil)
1608 (defvar gnus-summary-display-article-function nil)
1609
1610 (defvar gnus-summary-highlight-line-function nil
1611   "Function called after highlighting a summary line.")
1612
1613 (defvar gnus-group-line-format-alist
1614   `((?M gnus-tmp-marked-mark ?c)
1615     (?S gnus-tmp-subscribed ?c)
1616     (?L gnus-tmp-level ?d)
1617     (?N (cond ((eq number t) "*" )
1618               ((numberp number) 
1619                (int-to-string
1620                 (+ number
1621                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1622                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1623               (t number)) ?s)
1624     (?R gnus-tmp-number-of-read ?s)
1625     (?t gnus-tmp-number-total ?d)
1626     (?y gnus-tmp-number-of-unread ?s)
1627     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1628     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1629     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1630            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1631     (?g gnus-tmp-group ?s)
1632     (?G gnus-tmp-qualified-group ?s)
1633     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1634     (?D gnus-tmp-newsgroup-description ?s)
1635     (?o gnus-tmp-moderated ?c)
1636     (?O gnus-tmp-moderated-string ?s)
1637     (?p gnus-tmp-process-marked ?c)
1638     (?s gnus-tmp-news-server ?s)
1639     (?n gnus-tmp-news-method ?s)
1640     (?P gnus-group-indentation ?s)
1641     (?l gnus-tmp-grouplens ?s)
1642     (?z gnus-tmp-news-method-string ?s)
1643     (?u gnus-tmp-user-defined ?s)))
1644
1645 (defvar gnus-summary-line-format-alist
1646   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1647     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1648     (?s gnus-tmp-subject-or-nil ?s)
1649     (?n gnus-tmp-name ?s)
1650     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1651         ?s)
1652     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1653             gnus-tmp-from) ?s)
1654     (?F gnus-tmp-from ?s)
1655     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1656     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1657     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1658     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1659     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1660     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1661     (?L gnus-tmp-lines ?d)
1662     (?I gnus-tmp-indentation ?s)
1663     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1664     (?R gnus-tmp-replied ?c)
1665     (?\[ gnus-tmp-opening-bracket ?c)
1666     (?\] gnus-tmp-closing-bracket ?c)
1667     (?\> (make-string gnus-tmp-level ? ) ?s)
1668     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1669     (?i gnus-tmp-score ?d)
1670     (?z gnus-tmp-score-char ?c)
1671     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1672     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1673     (?U gnus-tmp-unread ?c)
1674     (?t (gnus-summary-number-of-articles-in-thread
1675          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1676         ?d)
1677     (?e (gnus-summary-number-of-articles-in-thread
1678          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1679         ?c)
1680     (?u gnus-tmp-user-defined ?s))
1681   "An alist of format specifications that can appear in summary lines,
1682 and what variables they correspond with, along with the type of the
1683 variable (string, integer, character, etc).")
1684
1685 (defvar gnus-summary-dummy-line-format-alist
1686   `((?S gnus-tmp-subject ?s)
1687     (?N gnus-tmp-number ?d)
1688     (?u gnus-tmp-user-defined ?s)))
1689
1690 (defvar gnus-summary-mode-line-format-alist
1691   `((?G gnus-tmp-group-name ?s)
1692     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1693     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1694     (?A gnus-tmp-article-number ?d)
1695     (?Z gnus-tmp-unread-and-unselected ?s)
1696     (?V gnus-version ?s)
1697     (?U gnus-tmp-unread ?d)
1698     (?S gnus-tmp-subject ?s)
1699     (?e gnus-tmp-unselected ?d)
1700     (?u gnus-tmp-user-defined ?s)
1701     (?d (length gnus-newsgroup-dormant) ?d)
1702     (?t (length gnus-newsgroup-marked) ?d)
1703     (?r (length gnus-newsgroup-reads) ?d)
1704     (?E gnus-newsgroup-expunged-tally ?d)
1705     (?s (gnus-current-score-file-nondirectory) ?s)))
1706
1707 (defvar gnus-article-mode-line-format-alist
1708   gnus-summary-mode-line-format-alist)
1709
1710 (defvar gnus-group-mode-line-format-alist
1711   `((?S gnus-tmp-news-server ?s)
1712     (?M gnus-tmp-news-method ?s)
1713     (?u gnus-tmp-user-defined ?s)))
1714
1715 (defvar gnus-have-read-active-file nil)
1716
1717 (defconst gnus-maintainer
1718   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1719   "The mail address of the Gnus maintainers.")
1720
1721 (defconst gnus-version "September Gnus v0.88"
1722   "Version number for this version of Gnus.")
1723
1724 (defvar gnus-info-nodes
1725   '((gnus-group-mode "(gnus)The Group Buffer")
1726     (gnus-summary-mode "(gnus)The Summary Buffer")
1727     (gnus-article-mode "(gnus)The Article Buffer"))
1728   "Assoc list of major modes and related Info nodes.")
1729
1730 (defvar gnus-group-buffer "*Group*")
1731 (defvar gnus-summary-buffer "*Summary*")
1732 (defvar gnus-article-buffer "*Article*")
1733 (defvar gnus-server-buffer "*Server*")
1734
1735 (defvar gnus-work-buffer " *gnus work*")
1736
1737 (defvar gnus-original-article-buffer " *Original Article*")
1738 (defvar gnus-original-article nil)
1739
1740 (defvar gnus-buffer-list nil
1741   "Gnus buffers that should be killed on exit.")
1742
1743 (defvar gnus-slave nil
1744   "Whether this Gnus is a slave or not.")
1745
1746 (defvar gnus-variable-list
1747   '(gnus-newsrc-options gnus-newsrc-options-n
1748     gnus-newsrc-last-checked-date
1749     gnus-newsrc-alist gnus-server-alist
1750     gnus-killed-list gnus-zombie-list
1751     gnus-topic-topology gnus-topic-alist
1752     gnus-format-specs)
1753   "Gnus variables saved in the quick startup file.")
1754
1755 (defvar gnus-newsrc-options nil
1756   "Options line in the .newsrc file.")
1757
1758 (defvar gnus-newsrc-options-n nil
1759   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1760
1761 (defvar gnus-newsrc-last-checked-date nil
1762   "Date Gnus last asked server for new newsgroups.")
1763
1764 (defvar gnus-topic-topology nil
1765   "The complete topic hierarchy.")
1766
1767 (defvar gnus-topic-alist nil
1768   "The complete topic-group alist.")
1769
1770 (defvar gnus-newsrc-alist nil
1771   "Assoc list of read articles.
1772 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1773
1774 (defvar gnus-newsrc-hashtb nil
1775   "Hashtable of gnus-newsrc-alist.")
1776
1777 (defvar gnus-killed-list nil
1778   "List of killed newsgroups.")
1779
1780 (defvar gnus-killed-hashtb nil
1781   "Hash table equivalent of gnus-killed-list.")
1782
1783 (defvar gnus-zombie-list nil
1784   "List of almost dead newsgroups.")
1785
1786 (defvar gnus-description-hashtb nil
1787   "Descriptions of newsgroups.")
1788
1789 (defvar gnus-list-of-killed-groups nil
1790   "List of newsgroups that have recently been killed by the user.")
1791
1792 (defvar gnus-active-hashtb nil
1793   "Hashtable of active articles.")
1794
1795 (defvar gnus-moderated-list nil
1796   "List of moderated newsgroups.")
1797
1798 (defvar gnus-group-marked nil)
1799
1800 (defvar gnus-current-startup-file nil
1801   "Startup file for the current host.")
1802
1803 (defvar gnus-last-search-regexp nil
1804   "Default regexp for article search command.")
1805
1806 (defvar gnus-last-shell-command nil
1807   "Default shell command on article.")
1808
1809 (defvar gnus-current-select-method nil
1810   "The current method for selecting a newsgroup.")
1811
1812 (defvar gnus-group-list-mode nil)
1813
1814 (defvar gnus-article-internal-prepare-hook nil)
1815
1816 (defvar gnus-newsgroup-name nil)
1817 (defvar gnus-newsgroup-begin nil)
1818 (defvar gnus-newsgroup-end nil)
1819 (defvar gnus-newsgroup-last-rmail nil)
1820 (defvar gnus-newsgroup-last-mail nil)
1821 (defvar gnus-newsgroup-last-folder nil)
1822 (defvar gnus-newsgroup-last-file nil)
1823 (defvar gnus-newsgroup-auto-expire nil)
1824 (defvar gnus-newsgroup-active nil)
1825
1826 (defvar gnus-newsgroup-data nil)
1827 (defvar gnus-newsgroup-data-reverse nil)
1828 (defvar gnus-newsgroup-limit nil)
1829 (defvar gnus-newsgroup-limits nil)
1830
1831 (defvar gnus-newsgroup-unreads nil
1832   "List of unread articles in the current newsgroup.")
1833
1834 (defvar gnus-newsgroup-unselected nil
1835   "List of unselected unread articles in the current newsgroup.")
1836
1837 (defvar gnus-newsgroup-reads nil
1838   "Alist of read articles and article marks in the current newsgroup.")
1839
1840 (defvar gnus-newsgroup-expunged-tally nil)
1841
1842 (defvar gnus-newsgroup-marked nil
1843   "List of ticked articles in the current newsgroup (a subset of unread art).")
1844
1845 (defvar gnus-newsgroup-killed nil
1846   "List of ranges of articles that have been through the scoring process.")
1847
1848 (defvar gnus-newsgroup-cached nil
1849   "List of articles that come from the article cache.")
1850
1851 (defvar gnus-newsgroup-saved nil
1852   "List of articles that have been saved.")
1853
1854 (defvar gnus-newsgroup-kill-headers nil)
1855
1856 (defvar gnus-newsgroup-replied nil
1857   "List of articles that have been replied to in the current newsgroup.")
1858
1859 (defvar gnus-newsgroup-expirable nil
1860   "List of articles in the current newsgroup that can be expired.")
1861
1862 (defvar gnus-newsgroup-processable nil
1863   "List of articles in the current newsgroup that can be processed.")
1864
1865 (defvar gnus-newsgroup-bookmarks nil
1866   "List of articles in the current newsgroup that have bookmarks.")
1867
1868 (defvar gnus-newsgroup-dormant nil
1869   "List of dormant articles in the current newsgroup.")
1870
1871 (defvar gnus-newsgroup-scored nil
1872   "List of scored articles in the current newsgroup.")
1873
1874 (defvar gnus-newsgroup-headers nil
1875   "List of article headers in the current newsgroup.")
1876
1877 (defvar gnus-newsgroup-threads nil)
1878
1879 (defvar gnus-newsgroup-prepared nil
1880   "Whether the current group has been prepared properly.")
1881
1882 (defvar gnus-newsgroup-ancient nil
1883   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1884
1885 (defvar gnus-newsgroup-sparse nil)
1886
1887 (defvar gnus-current-article nil)
1888 (defvar gnus-article-current nil)
1889 (defvar gnus-current-headers nil)
1890 (defvar gnus-have-all-headers nil)
1891 (defvar gnus-last-article nil)
1892 (defvar gnus-newsgroup-history nil)
1893 (defvar gnus-current-kill-article nil)
1894
1895 ;; Save window configuration.
1896 (defvar gnus-prev-winconf nil)
1897
1898 (defvar gnus-summary-mark-positions nil)
1899 (defvar gnus-group-mark-positions nil)
1900
1901 (defvar gnus-reffed-article-number nil)
1902
1903 ;;; Let the byte-compiler know that we know about this variable.
1904 (defvar rmail-default-rmail-file)
1905
1906 (defvar gnus-cache-removable-articles nil)
1907
1908 (defvar gnus-dead-summary nil)
1909
1910 (defconst gnus-summary-local-variables
1911   '(gnus-newsgroup-name
1912     gnus-newsgroup-begin gnus-newsgroup-end
1913     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1914     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1915     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1916     gnus-newsgroup-unselected gnus-newsgroup-marked
1917     gnus-newsgroup-reads gnus-newsgroup-saved
1918     gnus-newsgroup-replied gnus-newsgroup-expirable
1919     gnus-newsgroup-processable gnus-newsgroup-killed
1920     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1921     gnus-newsgroup-headers gnus-newsgroup-threads
1922     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1923     gnus-current-article gnus-current-headers gnus-have-all-headers
1924     gnus-last-article gnus-article-internal-prepare-hook
1925     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1926     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1927     gnus-newsgroup-async gnus-thread-expunge-below
1928     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1929     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1930     gnus-newsgroup-history gnus-newsgroup-ancient
1931     gnus-newsgroup-sparse
1932     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1933     gnus-newsgroup-adaptive-score-file
1934     (gnus-newsgroup-expunged-tally . 0)
1935     gnus-cache-removable-articles gnus-newsgroup-cached
1936     gnus-newsgroup-data gnus-newsgroup-data-reverse
1937     gnus-newsgroup-limit gnus-newsgroup-limits)
1938   "Variables that are buffer-local to the summary buffers.")
1939
1940 (defconst gnus-bug-message
1941   "Sending a bug report to the Gnus Towers.
1942 ========================================
1943
1944 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1945 be sent to the Gnus Bug Exterminators.
1946
1947 At the bottom of the buffer you'll see lots of variable settings.
1948 Please do not delete those.  They will tell the Bug People what your
1949 environment is, so that it will be easier to locate the bugs.
1950
1951 If you have found a bug that makes Emacs go \"beep\", set
1952 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1953 and include the backtrace in your bug report.
1954
1955 Please describe the bug in annoying, painstaking detail.
1956
1957 Thank you for your help in stamping out bugs.
1958 ")
1959
1960 ;;; End of variables.
1961
1962 ;; Define some autoload functions Gnus might use.
1963 (eval-and-compile
1964
1965   ;; This little mapcar goes through the list below and marks the
1966   ;; symbols in question as autoloaded functions.
1967   (mapcar
1968    (lambda (package)
1969      (let ((interactive (nth 1 (memq ':interactive package))))
1970        (mapcar
1971         (lambda (function)
1972           (let (keymap)
1973             (when (consp function)
1974               (setq keymap (car (memq 'keymap function)))
1975               (setq function (car function)))
1976             (autoload function (car package) nil interactive keymap)))
1977         (if (eq (nth 1 package) ':interactive)
1978             (cdddr package)
1979           (cdr package)))))
1980    '(("metamail" metamail-buffer)
1981      ("info" Info-goto-node)
1982      ("hexl" hexl-hex-string-to-integer)
1983      ("pp" pp pp-to-string pp-eval-expression)
1984      ("mail-extr" mail-extract-address-components)
1985      ("nnmail" nnmail-split-fancy nnmail-article-group)
1986      ("nnvirtual" nnvirtual-catchup-group)
1987      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1988       timezone-make-sortable-date timezone-make-time-string)
1989      ("sendmail" mail-position-on-field mail-setup)
1990      ("rmailout" rmail-output)
1991      ("rnewspost" news-mail-other-window news-reply-yank-original
1992       news-caesar-buffer-body)
1993      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1994       rmail-show-message)
1995      ("gnus-soup" :interactive t
1996       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1997       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1998      ("nnsoup" nnsoup-pack-replies)
1999      ("gnus-scomo" :interactive t gnus-score-mode)
2000      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2001       gnus-Folder-save-name gnus-folder-save-name)
2002      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2003      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2004       gnus-server-make-menu-bar gnus-article-make-menu-bar
2005       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2006       gnus-summary-highlight-line gnus-carpal-setup-buffer
2007       gnus-group-highlight-line
2008       gnus-article-add-button gnus-insert-next-page-button
2009       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2010      ("gnus-vis" :interactive t
2011       gnus-article-push-button gnus-article-press-button
2012       gnus-article-highlight gnus-article-highlight-some
2013       gnus-article-highlight-headers gnus-article-highlight-signature
2014       gnus-article-add-buttons gnus-article-add-buttons-to-head
2015       gnus-article-next-button gnus-article-prev-button)
2016      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2017       gnus-demon-add-disconnection gnus-demon-add-handler
2018       gnus-demon-remove-handler)
2019      ("gnus-demon" :interactive t
2020       gnus-demon-init gnus-demon-cancel)
2021      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2022       gnus-tree-open gnus-tree-close)
2023      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2024       gnus-nocem-unwanted-article-p)
2025      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2026      ("gnus-srvr" gnus-browse-foreign-server)
2027      ("gnus-cite" :interactive t
2028       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2029       gnus-article-hide-citation gnus-article-fill-cited-article
2030       gnus-article-hide-citation-in-followups)
2031      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2032       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2033       gnus-execute gnus-expunge)
2034      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2035       gnus-cache-possibly-remove-articles gnus-cache-request-article
2036       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2037       gnus-cache-enter-remove-article gnus-cached-article-p
2038       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2039      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2040       gnus-cache-remove-article)
2041      ("gnus-score" :interactive t
2042       gnus-summary-increase-score gnus-summary-lower-score
2043       gnus-score-flush-cache gnus-score-close
2044       gnus-score-raise-same-subject-and-select
2045       gnus-score-raise-same-subject gnus-score-default
2046       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2047       gnus-score-lower-same-subject gnus-score-lower-thread
2048       gnus-possibly-score-headers)
2049      ("gnus-score"
2050       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2051       gnus-current-score-file-nondirectory gnus-score-adaptive
2052       gnus-score-find-trace gnus-score-file-name)
2053      ("gnus-edit" :interactive t gnus-score-customize)
2054      ("gnus-topic" :interactive t gnus-topic-mode)
2055      ("gnus-topic" gnus-topic-remove-group)
2056      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2057      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2058      ("gnus-uu" :interactive t
2059       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2060       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2061       gnus-uu-mark-by-regexp gnus-uu-mark-all
2062       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2063       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2064       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2065       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2066       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2067       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2068       gnus-uu-decode-binhex-view)
2069      ("gnus-msg" (gnus-summary-send-map keymap)
2070       gnus-mail-yank-original gnus-mail-send-and-exit
2071       gnus-sendmail-setup-mail gnus-article-mail
2072       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2073      ("gnus-msg" :interactive t
2074       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2075       gnus-summary-followup gnus-summary-followup-with-original
2076       gnus-summary-followup-and-reply
2077       gnus-summary-followup-and-reply-with-original
2078       gnus-summary-cancel-article gnus-summary-supersede-article
2079       gnus-post-news gnus-inews-news gnus-cancel-news
2080       gnus-summary-reply gnus-summary-reply-with-original
2081       gnus-summary-mail-forward gnus-summary-mail-other-window
2082       gnus-bug)
2083      ("gnus-picon" :interactive t gnus-article-display-picons
2084       gnus-group-display-picons gnus-picons-article-display-x-face)
2085      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2086       gnus-grouplens-mode)
2087      ("gnus-vm" gnus-vm-mail-setup)
2088      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2089       gnus-summary-save-article-vm gnus-yank-article))))
2090
2091 \f
2092
2093 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2094 ;; If you want the cursor to go somewhere else, set these two
2095 ;; functions in some startup hook to whatever you want.
2096 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2097 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2098
2099 ;;; Various macros and substs.
2100
2101 (defun gnus-header-from (header)
2102   (mail-header-from header))
2103
2104 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2105   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2106   (let ((tempvar (make-symbol "GnusStartBufferWindow")))
2107     `(let ((,tempvar (selected-window)))
2108        (unwind-protect
2109            (progn
2110              (pop-to-buffer ,buffer)
2111              ,@forms)
2112          (select-window ,tempvar)))))
2113
2114 (defmacro gnus-gethash (string hashtable)
2115   "Get hash value of STRING in HASHTABLE."
2116   `(symbol-value (intern-soft ,string ,hashtable)))
2117
2118 (defmacro gnus-sethash (string value hashtable)
2119   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2120   `(set (intern ,string ,hashtable) ,value))
2121
2122 (defmacro gnus-intern-safe (string hashtable)
2123   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2124   `(let ((symbol (intern ,string ,hashtable)))
2125      (or (boundp symbol)
2126          (set symbol nil))
2127      symbol))
2128
2129 (defmacro gnus-group-unread (group)
2130   "Get the currently computed number of unread articles in GROUP."
2131   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2132
2133 (defmacro gnus-group-entry (group)
2134   "Get the newsrc entry for GROUP."
2135   `(gnus-gethash ,group gnus-newsrc-hashtb))
2136
2137 (defmacro gnus-active (group)
2138   "Get active info on GROUP."
2139   `(gnus-gethash ,group gnus-active-hashtb))
2140
2141 (defmacro gnus-set-active (group active)
2142   "Set GROUP's active info."
2143   `(gnus-sethash ,group ,active gnus-active-hashtb))
2144
2145 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2146 ;;   function `substring' might cut on a middle of multi-octet
2147 ;;   character.
2148 (defun gnus-truncate-string (str width)
2149   (substring str 0 width))
2150
2151 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2152 ;; to limit the length of a string.  This function is necessary since
2153 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2154 (defsubst gnus-limit-string (str width)
2155   (if (> (length str) width)
2156       (substring str 0 width)
2157     str))
2158
2159 (defsubst gnus-simplify-subject-re (subject)
2160   "Remove \"Re:\" from subject lines."
2161   (if (string-match "^[Rr][Ee]: *" subject)
2162       (substring subject (match-end 0))
2163     subject))
2164
2165 (defsubst gnus-functionp (form)
2166   "Return non-nil if FORM is funcallable."
2167   (or (and (symbolp form) (fboundp form))
2168       (and (listp form) (eq (car form) 'lambda))))
2169
2170 (defsubst gnus-goto-char (point)
2171   (and point (goto-char point)))
2172
2173 (defmacro gnus-buffer-exists-p (buffer)
2174   `(let ((buffer ,buffer))
2175      (and buffer
2176           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2177                    buffer))))
2178
2179 (defmacro gnus-kill-buffer (buffer)
2180   `(let ((buf ,buffer))
2181      (if (gnus-buffer-exists-p buf)
2182          (kill-buffer buf))))
2183
2184 (defsubst gnus-point-at-bol ()
2185   "Return point at the beginning of the line."
2186   (let ((p (point)))
2187     (beginning-of-line)
2188     (prog1
2189         (point)
2190       (goto-char p))))
2191
2192 (defsubst gnus-point-at-eol ()
2193   "Return point at the end of the line."
2194   (let ((p (point)))
2195     (end-of-line)
2196     (prog1
2197         (point)
2198       (goto-char p))))
2199
2200 (defun gnus-alive-p ()
2201   "Say whether Gnus is running or not."
2202   (and gnus-group-buffer
2203        (get-buffer gnus-group-buffer)))
2204
2205 ;; Delete the current line (and the next N lines.);
2206 (defmacro gnus-delete-line (&optional n)
2207   `(delete-region (progn (beginning-of-line) (point))
2208                   (progn (forward-line ,(or n 1)) (point))))
2209
2210 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2211 (defvar gnus-init-inhibit nil)
2212 (defun gnus-read-init-file (&optional inhibit-next)
2213   (if gnus-init-inhibit
2214       (setq gnus-init-inhibit nil)
2215     (setq gnus-init-inhibit inhibit-next)
2216     (and gnus-init-file
2217          (or (and (file-exists-p gnus-init-file)
2218                   ;; Don't try to load a directory.
2219                   (not (file-directory-p gnus-init-file)))
2220              (file-exists-p (concat gnus-init-file ".el"))
2221              (file-exists-p (concat gnus-init-file ".elc")))
2222          (condition-case var
2223              (load gnus-init-file nil t)
2224            (error
2225             (error "Error in %s: %s" gnus-init-file var))))))
2226
2227 ;; Info access macros.
2228
2229 (defmacro gnus-info-group (info)
2230   `(nth 0 ,info))
2231 (defmacro gnus-info-rank (info)
2232   `(nth 1 ,info))
2233 (defmacro gnus-info-read (info)
2234   `(nth 2 ,info))
2235 (defmacro gnus-info-marks (info)
2236   `(nth 3 ,info))
2237 (defmacro gnus-info-method (info)
2238   `(nth 4 ,info))
2239 (defmacro gnus-info-params (info)
2240   `(nth 5 ,info))
2241
2242 (defmacro gnus-info-level (info)
2243   `(let ((rank (gnus-info-rank ,info)))
2244      (if (consp rank)
2245          (car rank)
2246        rank)))
2247 (defmacro gnus-info-score (info)
2248   `(let ((rank (gnus-info-rank ,info)))
2249      (or (and (consp rank) (cdr rank)) 0)))
2250
2251 (defmacro gnus-info-set-group (info group)
2252   `(setcar ,info ,group))
2253 (defmacro gnus-info-set-rank (info rank)
2254   `(setcar (nthcdr 1 ,info) ,rank))
2255 (defmacro gnus-info-set-read (info read)
2256   `(setcar (nthcdr 2 ,info) ,read))
2257 (defmacro gnus-info-set-marks (info marks)
2258   `(setcar (nthcdr 3 ,info) ,marks))
2259 (defmacro gnus-info-set-method (info method)
2260   `(setcar (nthcdr 4 ,info) ,method))
2261 (defmacro gnus-info-set-params (info params)
2262   `(setcar (nthcdr 5 ,info) ,params))
2263
2264 (defmacro gnus-info-set-level (info level)
2265   `(let ((rank (cdr ,info)))
2266      (if (consp (car rank))
2267          (setcar (car rank) ,level)
2268        (setcar rank ,level))))
2269 (defmacro gnus-info-set-score (info score)
2270   `(let ((rank (cdr ,info)))
2271      (if (consp (car rank))
2272          (setcdr (car rank) ,score)
2273        (setcar rank (cons (car rank) ,score)))))
2274
2275 (defmacro gnus-get-info (group)
2276   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2277
2278 (defun gnus-byte-code (func)
2279   "Return a form that can be `eval'ed based on FUNC."
2280   (let ((fval (symbol-function func)))
2281     (if (byte-code-function-p fval)
2282         (let ((flist (append fval nil)))
2283           (setcar flist 'byte-code)
2284           flist)
2285       (cons 'progn (cddr fval)))))
2286
2287 ;;; Load the compatability functions.
2288
2289 (require 'gnus-cus)
2290 (require 'gnus-ems)
2291
2292 \f
2293 ;;;
2294 ;;; Shutdown
2295 ;;;
2296
2297 (defvar gnus-shutdown-alist nil)
2298
2299 (defun gnus-add-shutdown (function &rest symbols)
2300   "Run FUNCTION whenever one of SYMBOLS is shut down."
2301   (push (cons function symbols) gnus-shutdown-alist))
2302
2303 (defun gnus-shutdown (symbol)
2304   "Shut down everything that waits for SYMBOL."
2305   (let ((alist gnus-shutdown-alist)
2306         entry)
2307     (while (setq entry (pop alist))
2308       (when (memq symbol (cdr entry))
2309         (funcall (car entry))))))
2310
2311 \f
2312
2313 ;; Format specs.  The chunks below are the machine-generated forms
2314 ;; that are to be evaled as the result of the default format strings.
2315 ;; We write them in here to get them byte-compiled.  That way the
2316 ;; default actions will be quite fast, while still retaining the full
2317 ;; flexibility of the user-defined format specs.
2318
2319 ;; First we have lots of dummy defvars to let the compiler know these
2320 ;; are really dynamic variables.
2321
2322 (defvar gnus-tmp-unread)
2323 (defvar gnus-tmp-replied)
2324 (defvar gnus-tmp-score-char)
2325 (defvar gnus-tmp-indentation)
2326 (defvar gnus-tmp-opening-bracket)
2327 (defvar gnus-tmp-lines)
2328 (defvar gnus-tmp-name)
2329 (defvar gnus-tmp-closing-bracket)
2330 (defvar gnus-tmp-subject-or-nil)
2331 (defvar gnus-tmp-subject)
2332 (defvar gnus-tmp-marked)
2333 (defvar gnus-tmp-marked-mark)
2334 (defvar gnus-tmp-subscribed)
2335 (defvar gnus-tmp-process-marked)
2336 (defvar gnus-tmp-number-of-unread)
2337 (defvar gnus-tmp-group-name)
2338 (defvar gnus-tmp-group)
2339 (defvar gnus-tmp-article-number)
2340 (defvar gnus-tmp-unread-and-unselected)
2341 (defvar gnus-tmp-news-method)
2342 (defvar gnus-tmp-news-server)
2343 (defvar gnus-tmp-article-number)
2344 (defvar gnus-mouse-face)
2345 (defvar gnus-mouse-face-prop)
2346
2347 (defun gnus-summary-line-format-spec ()
2348   (insert gnus-tmp-unread gnus-tmp-replied
2349           gnus-tmp-score-char gnus-tmp-indentation)
2350   (put-text-property
2351    (point)
2352    (progn
2353      (insert
2354       gnus-tmp-opening-bracket
2355       (format "%4d: %-20s"
2356               gnus-tmp-lines
2357               (if (> (length gnus-tmp-name) 20)
2358                   (substring gnus-tmp-name 0 20)
2359                 gnus-tmp-name))
2360       gnus-tmp-closing-bracket)
2361      (point))
2362    gnus-mouse-face-prop gnus-mouse-face)
2363   (insert " " gnus-tmp-subject-or-nil "\n"))
2364
2365 (defvar gnus-summary-line-format-spec
2366   (gnus-byte-code 'gnus-summary-line-format-spec))
2367
2368 (defun gnus-summary-dummy-line-format-spec ()
2369   (insert "*  ")
2370   (put-text-property
2371    (point)
2372    (progn
2373      (insert ":                          :")
2374      (point))
2375    gnus-mouse-face-prop gnus-mouse-face)
2376   (insert " " gnus-tmp-subject "\n"))
2377
2378 (defvar gnus-summary-dummy-line-format-spec
2379   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2380
2381 (defun gnus-group-line-format-spec ()
2382   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2383           gnus-tmp-process-marked
2384           gnus-group-indentation
2385           (format "%5s: " gnus-tmp-number-of-unread))
2386   (put-text-property
2387    (point)
2388    (progn
2389      (insert gnus-tmp-group "\n")
2390      (1- (point)))
2391    gnus-mouse-face-prop gnus-mouse-face))
2392 (defvar gnus-group-line-format-spec
2393   (gnus-byte-code 'gnus-group-line-format-spec))
2394
2395 (defvar gnus-format-specs
2396   `((version . ,emacs-version)
2397     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2398     (summary-dummy ,gnus-summary-dummy-line-format
2399                    ,gnus-summary-dummy-line-format-spec)
2400     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2401
2402 (defvar gnus-article-mode-line-format-spec nil)
2403 (defvar gnus-summary-mode-line-format-spec nil)
2404 (defvar gnus-group-mode-line-format-spec nil)
2405
2406 ;;; Phew.  All that gruft is over, fortunately.
2407
2408 \f
2409 ;;;
2410 ;;; Gnus Utility Functions
2411 ;;;
2412
2413 (defun gnus-extract-address-components (from)
2414   (let (name address)
2415     ;; First find the address - the thing with the @ in it.  This may
2416     ;; not be accurate in mail addresses, but does the trick most of
2417     ;; the time in news messages.
2418     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2419         (setq address (substring from (match-beginning 0) (match-end 0))))
2420     ;; Then we check whether the "name <address>" format is used.
2421     (and address
2422          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2423          ;; Linear white space is not required.
2424          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2425          (and (setq name (substring from 0 (match-beginning 0)))
2426               ;; Strip any quotes from the name.
2427               (string-match "\".*\"" name)
2428               (setq name (substring name 1 (1- (match-end 0))))))
2429     ;; If not, then "address (name)" is used.
2430     (or name
2431         (and (string-match "(.+)" from)
2432              (setq name (substring from (1+ (match-beginning 0))
2433                                    (1- (match-end 0)))))
2434         (and (string-match "()" from)
2435              (setq name address))
2436         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2437         ;; XOVER might not support folded From headers.
2438         (and (string-match "(.*" from)
2439              (setq name (substring from (1+ (match-beginning 0))
2440                                    (match-end 0)))))
2441     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2442     (list (or name from) (or address from))))
2443
2444 (defun gnus-fetch-field (field)
2445   "Return the value of the header FIELD of current article."
2446   (save-excursion
2447     (save-restriction
2448       (let ((case-fold-search t)
2449             (inhibit-point-motion-hooks t))
2450         (nnheader-narrow-to-headers)
2451         (mail-fetch-field field)))))
2452
2453 (defun gnus-goto-colon ()
2454   (beginning-of-line)
2455   (search-forward ":" (gnus-point-at-eol) t))
2456
2457 ;;;###autoload
2458 (defun gnus-update-format (var)
2459   "Update the format specification near point."
2460   (interactive
2461    (list
2462     (save-excursion
2463       (eval-defun nil)
2464       ;; Find the end of the current word.
2465       (re-search-forward "[ \t\n]" nil t)
2466       ;; Search backward.
2467       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2468         (match-string 1)))))
2469   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2470                               (match-string 1 var))))
2471          (entry (assq type gnus-format-specs))
2472          value spec)
2473     (when entry
2474       (setq gnus-format-specs (delq entry gnus-format-specs)))
2475     (set
2476      (intern (format "%s-spec" var))
2477      (gnus-parse-format (setq value (symbol-value (intern var)))
2478                         (symbol-value (intern (format "%s-alist" var)))
2479                         (not (string-match "mode" var))))
2480     (setq spec (symbol-value (intern (format "%s-spec" var))))
2481     (push (list type value spec) gnus-format-specs)
2482
2483     (pop-to-buffer "*Gnus Format*")
2484     (erase-buffer)
2485     (lisp-interaction-mode)
2486     (insert (pp-to-string spec))))
2487
2488 (defun gnus-update-format-specifications (&optional force)
2489   "Update all (necessary) format specifications."
2490   ;; Make the indentation array.
2491   (gnus-make-thread-indent-array)
2492
2493   ;; See whether all the stored info needs to be flushed.
2494   (when (or force
2495             (not (equal emacs-version
2496                         (cdr (assq 'version gnus-format-specs)))))
2497     (setq gnus-format-specs nil))
2498
2499   ;; Go through all the formats and see whether they need updating.
2500   (let ((types '(summary summary-dummy group
2501                          summary-mode group-mode article-mode))
2502         new-format entry type val)
2503     (while (setq type (pop types))
2504       ;; Jump to the proper buffer to find out the value of
2505       ;; the variable, if possible.  (It may be buffer-local.)
2506       (save-excursion
2507         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2508               val)
2509           (when (and (boundp buffer)
2510                      (setq val (symbol-value buffer))
2511                      (get-buffer val)
2512                      (buffer-name (get-buffer val)))
2513             (set-buffer (get-buffer val)))
2514           (setq new-format (symbol-value
2515                             (intern (format "gnus-%s-line-format" type))))))
2516       (setq entry (cdr (assq type gnus-format-specs)))
2517       (if (and entry
2518                (equal (car entry) new-format))
2519           ;; Use the old format.
2520           (set (intern (format "gnus-%s-line-format-spec" type))
2521                (cadr entry))
2522         ;; This is a new format.
2523         (setq val
2524               (if (not (stringp new-format))
2525                   ;; This is a function call or something.
2526                   new-format
2527                 ;; This is a "real" format.
2528                 (gnus-parse-format
2529                  new-format
2530                  (symbol-value
2531                   (intern (format "gnus-%s-line-format-alist"
2532                                   (if (eq type 'article-mode)
2533                                       'summary-mode type))))
2534                  (not (string-match "mode$" (symbol-name type))))))
2535         ;; Enter the new format spec into the list.
2536         (if entry
2537             (progn
2538               (setcar (cdr entry) val)
2539               (setcar entry new-format))
2540           (push (list type new-format val) gnus-format-specs))
2541         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2542
2543   (unless (assq 'version gnus-format-specs)
2544     (push (cons 'version emacs-version) gnus-format-specs))
2545
2546   (gnus-update-group-mark-positions)
2547   (gnus-update-summary-mark-positions))
2548
2549 (defun gnus-update-summary-mark-positions ()
2550   "Compute where the summary marks are to go."
2551   (save-excursion
2552     (when (and gnus-summary-buffer
2553                (get-buffer gnus-summary-buffer)
2554                (buffer-name (get-buffer gnus-summary-buffer)))
2555       (set-buffer gnus-summary-buffer))
2556     (let ((gnus-replied-mark 129)
2557           (gnus-score-below-mark 130)
2558           (gnus-score-over-mark 130)
2559           (thread nil)
2560           (gnus-visual nil)
2561           (spec gnus-summary-line-format-spec)
2562           pos)
2563       (save-excursion
2564         (gnus-set-work-buffer)
2565         (let ((gnus-summary-line-format-spec spec))
2566           (gnus-summary-insert-line
2567            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2568           (goto-char (point-min))
2569           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2570                                              (- (point) 2)))))
2571           (goto-char (point-min))
2572           (push (cons 'replied (and (search-forward "\201" nil t) 
2573                                     (- (point) 2)))
2574                 pos)
2575           (goto-char (point-min))
2576           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2577                 pos)))
2578       (setq gnus-summary-mark-positions pos))))
2579
2580 (defun gnus-update-group-mark-positions ()
2581   (save-excursion
2582     (let ((gnus-process-mark 128)
2583           (gnus-group-marked '("dummy.group"))
2584           (gnus-active-hashtb (make-vector 10 0)))
2585       (gnus-set-active "dummy.group" '(0 . 0))
2586       (gnus-set-work-buffer)
2587       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2588       (goto-char (point-min))
2589       (setq gnus-group-mark-positions
2590             (list (cons 'process (and (search-forward "\200" nil t)
2591                                       (- (point) 2))))))))
2592
2593 (defvar gnus-mouse-face-0 'highlight)
2594 (defvar gnus-mouse-face-1 'highlight)
2595 (defvar gnus-mouse-face-2 'highlight)
2596 (defvar gnus-mouse-face-3 'highlight)
2597 (defvar gnus-mouse-face-4 'highlight)
2598
2599 (defun gnus-mouse-face-function (form type)
2600   `(put-text-property
2601     (point) (progn ,@form (point))
2602     gnus-mouse-face-prop
2603     ,(if (equal type 0)
2604          'gnus-mouse-face
2605        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2606
2607 (defvar gnus-face-0 'bold)
2608 (defvar gnus-face-1 'italic)
2609 (defvar gnus-face-2 'bold-italic)
2610 (defvar gnus-face-3 'bold)
2611 (defvar gnus-face-4 'bold)
2612
2613 (defun gnus-face-face-function (form type)
2614   `(put-text-property
2615     (point) (progn ,@form (point))
2616     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2617
2618 (defun gnus-max-width-function (el max-width)
2619   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2620   (if (symbolp el)
2621       `(if (> (length ,el) ,max-width)
2622            (substring ,el 0 ,max-width)
2623          ,el)
2624     `(let ((val (eval ,el)))
2625        (if (numberp val)
2626            (setq val (int-to-string val)))
2627        (if (> (length val) ,max-width)
2628            (substring val 0 ,max-width)
2629          val))))
2630
2631 (defun gnus-parse-format (format spec-alist &optional insert)
2632   ;; This function parses the FORMAT string with the help of the
2633   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2634   ;; string.  If the FORMAT string contains the specifiers %( and %)
2635   ;; the text between them will have the mouse-face text property.
2636   (if (string-match
2637        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2638        format)
2639       (gnus-parse-complex-format format spec-alist)
2640     ;; This is a simple format.
2641     (gnus-parse-simple-format format spec-alist insert)))
2642
2643 (defun gnus-parse-complex-format (format spec-alist)
2644   (save-excursion
2645     (gnus-set-work-buffer)
2646     (insert format)
2647     (goto-char (point-min))
2648     (while (re-search-forward "\"" nil t)
2649       (replace-match "\\\"" nil t))
2650     (goto-char (point-min))
2651     (insert "(\"")
2652     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2653       (let ((number (if (match-beginning 1)
2654                         (match-string 1) "0"))
2655             (delim (aref (match-string 2) 0)))
2656         (if (or (= delim ?\() (= delim ?\{))
2657             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2658                                    " " number " \""))
2659           (replace-match "\")\""))))
2660     (goto-char (point-max))
2661     (insert "\")")
2662     (goto-char (point-min))
2663     (let ((form (read (current-buffer))))
2664       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2665
2666 (defun gnus-complex-form-to-spec (form spec-alist)
2667   (delq nil
2668         (mapcar
2669          (lambda (sform)
2670            (if (stringp sform)
2671                (gnus-parse-simple-format sform spec-alist t)
2672              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2673                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2674                       (nth 1 sform))))
2675          form)))
2676
2677 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2678   ;; This function parses the FORMAT string with the help of the
2679   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2680   ;; string.
2681   (let ((max-width 0)
2682         spec flist fstring newspec elem beg result dontinsert)
2683     (save-excursion
2684       (gnus-set-work-buffer)
2685       (insert format)
2686       (goto-char (point-min))
2687       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2688                                 nil t)
2689         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2690               (setq newspec "%"
2691                     beg (1+ (match-beginning 0)))
2692           ;; First check if there are any specs that look anything like
2693           ;; "%12,12A", ie. with a "max width specification".  These have
2694           ;; to be treated specially.
2695           (if (setq beg (match-beginning 1))
2696               (setq max-width
2697                     (string-to-int
2698                      (buffer-substring
2699                       (1+ (match-beginning 1)) (match-end 1))))
2700             (setq max-width 0)
2701             (setq beg (match-beginning 2)))
2702           ;; Find the specification from `spec-alist'.
2703           (unless (setq elem (cdr (assq spec spec-alist)))
2704             (setq elem '("*" ?s)))
2705           ;; Treat user defined format specifiers specially.
2706           (when (eq (car elem) 'gnus-tmp-user-defined)
2707             (setq elem
2708                   (list
2709                    (list (intern (concat "gnus-user-format-function-"
2710                                          (match-string 3)))
2711                          'gnus-tmp-header) ?s))
2712             (delete-region (match-beginning 3) (match-end 3)))
2713           (if (not (zerop max-width))
2714               (let ((el (car elem)))
2715                 (cond ((= (cadr elem) ?c)
2716                        (setq el (list 'char-to-string el)))
2717                       ((= (cadr elem) ?d)
2718                        (setq el (list 'int-to-string el))))
2719                 (setq flist (cons (gnus-max-width-function el max-width)
2720                                   flist))
2721                 (setq newspec ?s))
2722             (progn
2723               (setq flist (cons (car elem) flist))
2724               (setq newspec (cadr elem)))))
2725         ;; Remove the old specification (and possibly a ",12" string).
2726         (delete-region beg (match-end 2))
2727         ;; Insert the new specification.
2728         (goto-char beg)
2729         (insert newspec))
2730       (setq fstring (buffer-substring 1 (point-max))))
2731     ;; Do some postprocessing to increase efficiency.
2732     (setq
2733      result
2734      (cond
2735       ;; Emptyness.
2736       ((string= fstring "")
2737        nil)
2738       ;; Not a format string.
2739       ((not (string-match "%" fstring))
2740        (list fstring))
2741       ;; A format string with just a single string spec.
2742       ((string= fstring "%s")
2743        (list (car flist)))
2744       ;; A single character.
2745       ((string= fstring "%c")
2746        (list (car flist)))
2747       ;; A single number.
2748       ((string= fstring "%d")
2749        (setq dontinsert)
2750        (if insert
2751            (list `(princ ,(car flist)))
2752          (list `(int-to-string ,(car flist)))))
2753       ;; Just lots of chars and strings.
2754       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2755        (nreverse flist))
2756       ;; A single string spec at the beginning of the spec.
2757       ((string-match "\\`%[sc][^%]+\\'" fstring)
2758        (list (car flist) (substring fstring 2)))
2759       ;; A single string spec in the middle of the spec.
2760       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2761        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2762       ;; A single string spec in the end of the spec.
2763       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2764        (list (match-string 1 fstring) (car flist)))
2765       ;; A more complex spec.
2766       (t
2767        (list (cons 'format (cons fstring (nreverse flist)))))))
2768
2769     (if insert
2770         (when result
2771           (if dontinsert
2772               result
2773             (cons 'insert result)))
2774       (cond ((stringp result)
2775              result)
2776             ((consp result)
2777              (cons 'concat result))
2778             (t "")))))
2779
2780 (defun gnus-eval-format (format &optional alist props)
2781   "Eval the format variable FORMAT, using ALIST.
2782 If PROPS, insert the result."
2783   (let ((form (gnus-parse-format format alist props)))
2784     (if props
2785         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
2786       (eval form))))
2787
2788 (defun gnus-remove-text-with-property (prop)
2789   "Delete all text in the current buffer with text property PROP."
2790   (save-excursion
2791     (goto-char (point-min))
2792     (while (not (eobp))
2793       (while (get-text-property (point) prop)
2794         (delete-char 1))
2795       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2796
2797 (defun gnus-set-work-buffer ()
2798   (if (get-buffer gnus-work-buffer)
2799       (progn
2800         (set-buffer gnus-work-buffer)
2801         (erase-buffer))
2802     (set-buffer (get-buffer-create gnus-work-buffer))
2803     (kill-all-local-variables)
2804     (buffer-disable-undo (current-buffer))
2805     (gnus-add-current-to-buffer-list)))
2806
2807 ;; Article file names when saving.
2808
2809 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2810   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2811 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2812 Otherwise, it is like ~/News/news/group/num."
2813   (let ((default
2814           (expand-file-name
2815            (concat (if (gnus-use-long-file-name 'not-save)
2816                        (gnus-capitalize-newsgroup newsgroup)
2817                      (gnus-newsgroup-directory-form newsgroup))
2818                    "/" (int-to-string (mail-header-number headers)))
2819            (or gnus-article-save-directory "~/News"))))
2820     (if (and last-file
2821              (string-equal (file-name-directory default)
2822                            (file-name-directory last-file))
2823              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2824         default
2825       (or last-file default))))
2826
2827 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2828   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2829 If variable `gnus-use-long-file-name' is non-nil, it is
2830 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2831   (let ((default
2832           (expand-file-name
2833            (concat (if (gnus-use-long-file-name 'not-save)
2834                        newsgroup
2835                      (gnus-newsgroup-directory-form newsgroup))
2836                    "/" (int-to-string (mail-header-number headers)))
2837            (or gnus-article-save-directory "~/News"))))
2838     (if (and last-file
2839              (string-equal (file-name-directory default)
2840                            (file-name-directory last-file))
2841              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2842         default
2843       (or last-file default))))
2844
2845 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2846   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2847 If variable `gnus-use-long-file-name' is non-nil, it is
2848 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2849   (or last-file
2850       (expand-file-name
2851        (if (gnus-use-long-file-name 'not-save)
2852            (gnus-capitalize-newsgroup newsgroup)
2853          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2854        (or gnus-article-save-directory "~/News"))))
2855
2856 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2857   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2858 If variable `gnus-use-long-file-name' is non-nil, it is
2859 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2860   (or last-file
2861       (expand-file-name
2862        (if (gnus-use-long-file-name 'not-save)
2863            newsgroup
2864          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2865        (or gnus-article-save-directory "~/News"))))
2866
2867 ;; For subscribing new newsgroup
2868
2869 (defun gnus-subscribe-hierarchical-interactive (groups)
2870   (let ((groups (sort groups 'string<))
2871         prefixes prefix start ans group starts)
2872     (while groups
2873       (setq prefixes (list "^"))
2874       (while (and groups prefixes)
2875         (while (not (string-match (car prefixes) (car groups)))
2876           (setq prefixes (cdr prefixes)))
2877         (setq prefix (car prefixes))
2878         (setq start (1- (length prefix)))
2879         (if (and (string-match "[^\\.]\\." (car groups) start)
2880                  (cdr groups)
2881                  (setq prefix
2882                        (concat "^" (substring (car groups) 0 (match-end 0))))
2883                  (string-match prefix (cadr groups)))
2884             (progn
2885               (setq prefixes (cons prefix prefixes))
2886               (message "Descend hierarchy %s? ([y]nsq): "
2887                        (substring prefix 1 (1- (length prefix))))
2888               (setq ans (read-char))
2889               (cond ((= ans ?n)
2890                      (while (and groups
2891                                  (string-match prefix
2892                                                (setq group (car groups))))
2893                        (setq gnus-killed-list
2894                              (cons group gnus-killed-list))
2895                        (gnus-sethash group group gnus-killed-hashtb)
2896                        (setq groups (cdr groups)))
2897                      (setq starts (cdr starts)))
2898                     ((= ans ?s)
2899                      (while (and groups
2900                                  (string-match prefix
2901                                                (setq group (car groups))))
2902                        (gnus-sethash group group gnus-killed-hashtb)
2903                        (gnus-subscribe-alphabetically (car groups))
2904                        (setq groups (cdr groups)))
2905                      (setq starts (cdr starts)))
2906                     ((= ans ?q)
2907                      (while groups
2908                        (setq group (car groups))
2909                        (setq gnus-killed-list (cons group gnus-killed-list))
2910                        (gnus-sethash group group gnus-killed-hashtb)
2911                        (setq groups (cdr groups))))
2912                     (t nil)))
2913           (message "Subscribe %s? ([n]yq)" (car groups))
2914           (setq ans (read-char))
2915           (setq group (car groups))
2916           (cond ((= ans ?y)
2917                  (gnus-subscribe-alphabetically (car groups))
2918                  (gnus-sethash group group gnus-killed-hashtb))
2919                 ((= ans ?q)
2920                  (while groups
2921                    (setq group (car groups))
2922                    (setq gnus-killed-list (cons group gnus-killed-list))
2923                    (gnus-sethash group group gnus-killed-hashtb)
2924                    (setq groups (cdr groups))))
2925                 (t
2926                  (setq gnus-killed-list (cons group gnus-killed-list))
2927                  (gnus-sethash group group gnus-killed-hashtb)))
2928           (setq groups (cdr groups)))))))
2929
2930 (defun gnus-subscribe-randomly (newsgroup)
2931   "Subscribe new NEWSGROUP by making it the first newsgroup."
2932   (gnus-subscribe-newsgroup newsgroup))
2933
2934 (defun gnus-subscribe-alphabetically (newgroup)
2935   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2936   (let ((groups (cdr gnus-newsrc-alist))
2937         before)
2938     (while (and (not before) groups)
2939       (if (string< newgroup (caar groups))
2940           (setq before (caar groups))
2941         (setq groups (cdr groups))))
2942     (gnus-subscribe-newsgroup newgroup before)))
2943
2944 (defun gnus-subscribe-hierarchically (newgroup)
2945   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2946   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2947   (save-excursion
2948     (set-buffer (find-file-noselect gnus-current-startup-file))
2949     (let ((groupkey newgroup)
2950           before)
2951       (while (and (not before) groupkey)
2952         (goto-char (point-min))
2953         (let ((groupkey-re
2954                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2955           (while (and (re-search-forward groupkey-re nil t)
2956                       (progn
2957                         (setq before (match-string 1))
2958                         (string< before newgroup)))))
2959         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2960         (setq groupkey
2961               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2962                   (substring groupkey (match-beginning 1) (match-end 1)))))
2963       (gnus-subscribe-newsgroup newgroup before))))
2964
2965 (defun gnus-subscribe-interactively (group)
2966   "Subscribe the new GROUP interactively.
2967 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2968 it is killed."
2969   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2970       (gnus-subscribe-hierarchically group)
2971     (push group gnus-killed-list)))
2972
2973 (defun gnus-subscribe-zombies (group)
2974   "Make the new GROUP into a zombie group."
2975   (push group gnus-zombie-list))
2976
2977 (defun gnus-subscribe-killed (group)
2978   "Make the new GROUP a killed group."
2979   (push group gnus-killed-list))
2980
2981 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2982   "Subscribe new NEWSGROUP.
2983 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2984 the first newsgroup."
2985   ;; We subscribe the group by changing its level to `subscribed'.
2986   (gnus-group-change-level
2987    newsgroup gnus-level-default-subscribed
2988    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2989   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2990
2991 ;; For directories
2992
2993 (defun gnus-newsgroup-directory-form (newsgroup)
2994   "Make hierarchical directory name from NEWSGROUP name."
2995   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2996         (len (length newsgroup))
2997         idx)
2998     ;; If this is a foreign group, we don't want to translate the
2999     ;; entire name.
3000     (if (setq idx (string-match ":" newsgroup))
3001         (aset newsgroup idx ?/)
3002       (setq idx 0))
3003     ;; Replace all occurrences of `.' with `/'.
3004     (while (< idx len)
3005       (if (= (aref newsgroup idx) ?.)
3006           (aset newsgroup idx ?/))
3007       (setq idx (1+ idx)))
3008     newsgroup))
3009
3010 (defun gnus-newsgroup-savable-name (group)
3011   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3012   ;; with dots.
3013   (nnheader-replace-chars-in-string group ?/ ?.))
3014
3015 (defun gnus-make-directory (dir)
3016   "Make DIRECTORY recursively."
3017   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3018   ;; of the many mysteries of the universe.
3019   (let* ((dir (expand-file-name dir default-directory))
3020          dirs err)
3021     (if (string-match "/$" dir)
3022         (setq dir (substring dir 0 (match-beginning 0))))
3023     ;; First go down the path until we find a directory that exists.
3024     (while (not (file-exists-p dir))
3025       (setq dirs (cons dir dirs))
3026       (string-match "/[^/]+$" dir)
3027       (setq dir (substring dir 0 (match-beginning 0))))
3028     ;; Then create all the subdirs.
3029     (while (and dirs (not err))
3030       (condition-case ()
3031           (make-directory (car dirs))
3032         (error (setq err t)))
3033       (setq dirs (cdr dirs)))
3034     ;; We return whether we were successful or not.
3035     (not dirs)))
3036
3037 (defun gnus-capitalize-newsgroup (newsgroup)
3038   "Capitalize NEWSGROUP name."
3039   (and (not (zerop (length newsgroup)))
3040        (concat (char-to-string (upcase (aref newsgroup 0)))
3041                (substring newsgroup 1))))
3042
3043 ;; Various... things.
3044
3045 (defun gnus-simplify-subject (subject &optional re-only)
3046   "Remove `Re:' and words in parentheses.
3047 If RE-ONLY is non-nil, strip leading `Re:'s only."
3048   (let ((case-fold-search t))           ;Ignore case.
3049     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3050     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3051       (setq subject (substring subject (match-end 0))))
3052     ;; Remove uninteresting prefixes.
3053     (if (and (not re-only)
3054              gnus-simplify-ignored-prefixes
3055              (string-match gnus-simplify-ignored-prefixes subject))
3056         (setq subject (substring subject (match-end 0))))
3057     ;; Remove words in parentheses from end.
3058     (unless re-only
3059       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3060         (setq subject (substring subject 0 (match-beginning 0)))))
3061     ;; Return subject string.
3062     subject))
3063
3064 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3065 ;; all whitespace.
3066 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3067 (defun gnus-simplify-buffer-fuzzy ()
3068   (goto-char (point-min))
3069   (while (search-forward "\t" nil t)
3070     (replace-match " " t t))
3071   (goto-char (point-min))
3072   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3073   (goto-char (match-beginning 0))
3074   (while (or
3075           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3076           (looking-at "^[[].*: .*[]]$"))
3077     (goto-char (point-min))
3078     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3079                               nil t)
3080       (replace-match "" t t))
3081     (goto-char (point-min))
3082     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3083       (goto-char (match-end 0))
3084       (delete-char -1)
3085       (delete-region
3086        (progn (goto-char (match-beginning 0)))
3087        (re-search-forward ":"))))
3088   (goto-char (point-min))
3089   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3090     (replace-match "" t t))
3091   (goto-char (point-min))
3092   (while (re-search-forward "  +" nil t)
3093     (replace-match " " t t))
3094   (goto-char (point-min))
3095   (while (re-search-forward " $" nil t)
3096     (replace-match "" t t))
3097   (goto-char (point-min))
3098   (while (re-search-forward "^ +" nil t)
3099     (replace-match "" t t))
3100   (goto-char (point-min))
3101   (when gnus-simplify-subject-fuzzy-regexp
3102     (if (listp gnus-simplify-subject-fuzzy-regexp)
3103         (let ((list gnus-simplify-subject-fuzzy-regexp))
3104           (while list
3105             (goto-char (point-min))
3106             (while (re-search-forward (car list) nil t)
3107               (replace-match "" t t))
3108             (setq list (cdr list))))
3109       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3110         (replace-match "" t t)))))
3111
3112 (defun gnus-simplify-subject-fuzzy (subject)
3113   "Siplify a subject string fuzzily."
3114   (save-excursion
3115     (gnus-set-work-buffer)
3116     (let ((case-fold-search t))
3117       (insert subject)
3118       (inline (gnus-simplify-buffer-fuzzy))
3119       (buffer-string))))
3120
3121 ;; Add the current buffer to the list of buffers to be killed on exit.
3122 (defun gnus-add-current-to-buffer-list ()
3123   (or (memq (current-buffer) gnus-buffer-list)
3124       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3125
3126 (defun gnus-string> (s1 s2)
3127   (not (or (string< s1 s2)
3128            (string= s1 s2))))
3129
3130 (defun gnus-read-active-file-p ()
3131   "Say whether the active file has been read from `gnus-select-method'."
3132   (memq gnus-select-method gnus-have-read-active-file))
3133
3134 ;;; General various misc type functions.
3135
3136 (defun gnus-clear-system ()
3137   "Clear all variables and buffers."
3138   ;; Clear Gnus variables.
3139   (let ((variables gnus-variable-list))
3140     (while variables
3141       (set (car variables) nil)
3142       (setq variables (cdr variables))))
3143   ;; Clear other internal variables.
3144   (setq gnus-list-of-killed-groups nil
3145         gnus-have-read-active-file nil
3146         gnus-newsrc-alist nil
3147         gnus-newsrc-hashtb nil
3148         gnus-killed-list nil
3149         gnus-zombie-list nil
3150         gnus-killed-hashtb nil
3151         gnus-active-hashtb nil
3152         gnus-moderated-list nil
3153         gnus-description-hashtb nil
3154         gnus-current-headers nil
3155         gnus-thread-indent-array nil
3156         gnus-newsgroup-headers nil
3157         gnus-newsgroup-name nil
3158         gnus-server-alist nil
3159         gnus-group-list-mode nil
3160         gnus-opened-servers nil
3161         gnus-current-select-method nil)
3162   (gnus-shutdown 'gnus)
3163   ;; Kill the startup file.
3164   (and gnus-current-startup-file
3165        (get-file-buffer gnus-current-startup-file)
3166        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3167   ;; Clear the dribble buffer.
3168   (gnus-dribble-clear)
3169   ;; Kill global KILL file buffer.
3170   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3171     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3172   (gnus-kill-buffer nntp-server-buffer)
3173   ;; Kill Gnus buffers.
3174   (while gnus-buffer-list
3175     (gnus-kill-buffer (pop gnus-buffer-list)))
3176   ;; Remove Gnus frames.
3177   (while gnus-created-frames
3178     (when (frame-live-p (car gnus-created-frames))
3179       ;; We slap a condition-case around this `delete-frame' to ensure 
3180       ;; agains errors if we try do delete the single frame that's left.
3181       (condition-case ()
3182           (delete-frame (car gnus-created-frames))
3183         (error nil)))
3184     (pop gnus-created-frames)))
3185
3186 (defun gnus-windows-old-to-new (setting)
3187   ;; First we take care of the really, really old Gnus 3 actions.
3188   (when (symbolp setting)
3189     (setq setting
3190           ;; Take care of ooold GNUS 3.x values.
3191           (cond ((eq setting 'SelectArticle) 'article)
3192                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3193                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3194                 (t setting))))
3195   (if (or (listp setting)
3196           (not (and gnus-window-configuration
3197                     (memq setting '(group summary article)))))
3198       setting
3199     (let* ((setting (if (eq setting 'group)
3200                         (if (assq 'newsgroup gnus-window-configuration)
3201                             'newsgroup
3202                           'newsgroups) setting))
3203            (elem (cadr (assq setting gnus-window-configuration)))
3204            (total (apply '+ elem))
3205            (types '(group summary article))
3206            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3207            (i 0)
3208            perc
3209            out)
3210       (while (< i 3)
3211         (or (not (numberp (nth i elem)))
3212             (zerop (nth i elem))
3213             (progn
3214               (setq perc  (/ (float (nth 0 elem)) total))
3215               (setq out (cons (if (eq pbuf (nth i types))
3216                                   (vector (nth i types) perc 'point)
3217                                 (vector (nth i types) perc))
3218                               out))))
3219         (setq i (1+ i)))
3220       (list (nreverse out)))))
3221
3222 ;;;###autoload
3223 (defun gnus-add-configuration (conf)
3224   "Add the window configuration CONF to `gnus-buffer-configuration'."
3225   (setq gnus-buffer-configuration
3226         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3227                          gnus-buffer-configuration))))
3228
3229 (defvar gnus-frame-list nil)
3230
3231 (defun gnus-configure-frame (split &optional window)
3232   "Split WINDOW according to SPLIT."
3233   (unless window
3234     (setq window (get-buffer-window (current-buffer))))
3235   (select-window window)
3236   ;; This might be an old-stylee buffer config.
3237   (when (vectorp split)
3238     (setq split (append split nil)))
3239   (when (or (consp (car split))
3240             (vectorp (car split)))
3241     (push 1.0 split)
3242     (push 'vertical split))
3243   ;; The SPLIT might be something that is to be evaled to
3244   ;; return a new SPLIT.
3245   (while (and (not (assq (car split) gnus-window-to-buffer))
3246               (gnus-functionp (car split)))
3247     (setq split (eval split)))
3248   (let* ((type (car split))
3249          (subs (cddr split))
3250          (len (if (eq type 'horizontal) (window-width) (window-height)))
3251          (total 0)
3252          (window-min-width (or gnus-window-min-width window-min-width))
3253          (window-min-height (or gnus-window-min-height window-min-height))
3254          s result new-win rest comp-subs size sub)
3255     (cond
3256      ;; Nothing to do here.
3257      ((null split))
3258      ;; Don't switch buffers.
3259      ((null type)
3260       (and (memq 'point split) window))
3261      ;; This is a buffer to be selected.
3262      ((not (memq type '(frame horizontal vertical)))
3263       (let ((buffer (cond ((stringp type) type)
3264                           (t (cdr (assq type gnus-window-to-buffer)))))
3265             buf)
3266         (unless buffer
3267           (error "Illegal buffer type: %s" type))
3268         (unless (setq buf (get-buffer (if (symbolp buffer)
3269                                           (symbol-value buffer) buffer)))
3270           (setq buf (get-buffer-create (if (symbolp buffer)
3271                                            (symbol-value buffer) buffer))))
3272         (switch-to-buffer buf)
3273         ;; We return the window if it has the `point' spec.
3274         (and (memq 'point split) window)))
3275      ;; This is a frame split.
3276      ((eq type 'frame)
3277       (unless gnus-frame-list
3278         (setq gnus-frame-list (list (window-frame
3279                                      (get-buffer-window (current-buffer))))))
3280       (let ((i 0)
3281             params frame fresult)
3282         (while (< i (length subs))
3283           ;; Frame parameter is gotten from the sub-split.
3284           (setq params (cadr (elt subs i)))
3285           ;; It should be a list.
3286           (unless (listp params)
3287             (setq params nil))
3288           ;; Create a new frame?
3289           (unless (setq frame (elt gnus-frame-list i))
3290             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3291             (push frame gnus-created-frames))
3292           ;; Is the old frame still alive?
3293           (unless (frame-live-p frame)
3294             (setcar (nthcdr i gnus-frame-list)
3295                     (setq frame (make-frame params))))
3296           ;; Select the frame in question and do more splits there.
3297           (select-frame frame)
3298           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3299           (incf i))
3300         ;; Select the frame that has the selected buffer.
3301         (when fresult
3302           (select-frame (window-frame fresult)))))
3303      ;; This is a normal split.
3304      (t
3305       (when (> (length subs) 0)
3306         ;; First we have to compute the sizes of all new windows.
3307         (while subs
3308           (setq sub (append (pop subs) nil))
3309           (while (and (not (assq (car sub) gnus-window-to-buffer))
3310                       (gnus-functionp (car sub)))
3311             (setq sub (eval sub)))
3312           (when sub
3313             (push sub comp-subs)
3314             (setq size (cadar comp-subs))
3315             (cond ((equal size 1.0)
3316                    (setq rest (car comp-subs))
3317                    (setq s 0))
3318                   ((floatp size)
3319                    (setq s (floor (* size len))))
3320                   ((integerp size)
3321                    (setq s size))
3322                   (t
3323                    (error "Illegal size: %s" size)))
3324             ;; Try to make sure that we are inside the safe limits.
3325             (cond ((zerop s))
3326                   ((eq type 'horizontal)
3327                    (setq s (max s window-min-width)))
3328                   ((eq type 'vertical)
3329                    (setq s (max s window-min-height))))
3330             (setcar (cdar comp-subs) s)
3331             (incf total s)))
3332         ;; Take care of the "1.0" spec.
3333         (if rest
3334             (setcar (cdr rest) (- len total))
3335           (error "No 1.0 specs in %s" split))
3336         ;; The we do the actual splitting in a nice recursive
3337         ;; fashion.
3338         (setq comp-subs (nreverse comp-subs))
3339         (while comp-subs
3340           (if (null (cdr comp-subs))
3341               (setq new-win window)
3342             (setq new-win
3343                   (split-window window (cadar comp-subs)
3344                                 (eq type 'horizontal))))
3345           (setq result (or (gnus-configure-frame
3346                             (car comp-subs) window) result))
3347           (select-window new-win)
3348           (setq window new-win)
3349           (setq comp-subs (cdr comp-subs))))
3350       ;; Return the proper window, if any.
3351       (when result
3352         (select-window result))))))
3353
3354 (defvar gnus-frame-split-p nil)
3355
3356 (defun gnus-configure-windows (setting &optional force)
3357   (setq setting (gnus-windows-old-to-new setting))
3358   (let ((split (if (symbolp setting)
3359                    (cadr (assq setting gnus-buffer-configuration))
3360                  setting))
3361         all-visible)
3362
3363     (setq gnus-frame-split-p nil)
3364
3365     (unless split
3366       (error "No such setting: %s" setting))
3367
3368     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3369              (not force))
3370         ;; All the windows mentioned are already visible, so we just
3371         ;; put point in the assigned buffer, and do not touch the
3372         ;; winconf.
3373         (select-window all-visible)
3374
3375       ;; Either remove all windows or just remove all Gnus windows.
3376       (let ((frame (selected-frame)))
3377         (unwind-protect
3378             (if gnus-use-full-window
3379                 ;; We want to remove all other windows.
3380                 (if (not gnus-frame-split-p)
3381                     ;; This is not a `frame' split, so we ignore the
3382                     ;; other frames.  
3383                     (delete-other-windows)
3384                   ;; This is a `frame' split, so we delete all windows
3385                   ;; on all frames.
3386                   (mapcar 
3387                    (lambda (frame)
3388                      (unless (eq (cdr (assq 'minibuffer
3389                                             (frame-parameters frame)))
3390                                  'only)
3391                        (select-frame frame)
3392                        (delete-other-windows)))
3393                    (frame-list)))
3394               ;; Just remove some windows.
3395               (gnus-remove-some-windows)
3396               (switch-to-buffer nntp-server-buffer))
3397           (select-frame frame)))
3398
3399       (switch-to-buffer nntp-server-buffer)
3400       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3401
3402 (defun gnus-all-windows-visible-p (split)
3403   "Say whether all buffers in SPLIT are currently visible.
3404 In particular, the value returned will be the window that
3405 should have point."
3406   (let ((stack (list split))
3407         (all-visible t)
3408         type buffer win buf)
3409     (while (and (setq split (pop stack))
3410                 all-visible)
3411       ;; Be backwards compatible.
3412       (when (vectorp split)
3413         (setq split (append split nil)))
3414       (when (or (consp (car split))
3415                 (vectorp (car split)))
3416         (push 1.0 split)
3417         (push 'vertical split))
3418       ;; The SPLIT might be something that is to be evaled to
3419       ;; return a new SPLIT.
3420       (while (and (not (assq (car split) gnus-window-to-buffer))
3421                   (gnus-functionp (car split)))
3422         (setq split (eval split)))
3423
3424       (setq type (elt split 0))
3425       (cond
3426        ;; Nothing here.
3427        ((null split) t)
3428        ;; A buffer.
3429        ((not (memq type '(horizontal vertical frame)))
3430         (setq buffer (cond ((stringp type) type)
3431                            (t (cdr (assq type gnus-window-to-buffer)))))
3432         (unless buffer
3433           (error "Illegal buffer type: %s" type))
3434         (when (setq buf (get-buffer (if (symbolp buffer)
3435                                         (symbol-value buffer)
3436                                       buffer)))
3437           (setq win (get-buffer-window buf t)))
3438         (if win
3439             (when (memq 'point split)
3440                 (setq all-visible win))
3441           (setq all-visible nil)))
3442        (t
3443         (when (eq type 'frame)
3444           (setq gnus-frame-split-p t))
3445         (setq stack (append (cddr split) stack)))))
3446     (unless (eq all-visible t)
3447       all-visible)))
3448
3449 (defun gnus-window-top-edge (&optional window)
3450   (nth 1 (window-edges window)))
3451
3452 (defun gnus-remove-some-windows ()
3453   (let ((buffers gnus-window-to-buffer)
3454         buf bufs lowest-buf lowest)
3455     (save-excursion
3456       ;; Remove windows on all known Gnus buffers.
3457       (while buffers
3458         (setq buf (cdar buffers))
3459         (if (symbolp buf)
3460             (setq buf (and (boundp buf) (symbol-value buf))))
3461         (and buf
3462              (get-buffer-window buf)
3463              (progn
3464                (setq bufs (cons buf bufs))
3465                (pop-to-buffer buf)
3466                (if (or (not lowest)
3467                        (< (gnus-window-top-edge) lowest))
3468                    (progn
3469                      (setq lowest (gnus-window-top-edge))
3470                      (setq lowest-buf buf)))))
3471         (setq buffers (cdr buffers)))
3472       ;; Remove windows on *all* summary buffers.
3473       (walk-windows
3474        (lambda (win)
3475          (let ((buf (window-buffer win)))
3476            (if (string-match    "^\\*Summary" (buffer-name buf))
3477                (progn
3478                  (setq bufs (cons buf bufs))
3479                  (pop-to-buffer buf)
3480                  (if (or (not lowest)
3481                          (< (gnus-window-top-edge) lowest))
3482                      (progn
3483                        (setq lowest-buf buf)
3484                        (setq lowest (gnus-window-top-edge)))))))))
3485       (and lowest-buf
3486            (progn
3487              (pop-to-buffer lowest-buf)
3488              (switch-to-buffer nntp-server-buffer)))
3489       (while bufs
3490         (and (not (eq (car bufs) lowest-buf))
3491              (delete-windows-on (car bufs)))
3492         (setq bufs (cdr bufs))))))
3493
3494 (defun gnus-version ()
3495   "Version numbers of this version of Gnus."
3496   (interactive)
3497   (let ((methods gnus-valid-select-methods)
3498         (mess gnus-version)
3499         meth)
3500     ;; Go through all the legal select methods and add their version
3501     ;; numbers to the total version string.  Only the backends that are
3502     ;; currently in use will have their message numbers taken into
3503     ;; consideration.
3504     (while methods
3505       (setq meth (intern (concat (caar methods) "-version")))
3506       (and (boundp meth)
3507            (stringp (symbol-value meth))
3508            (setq mess (concat mess "; " (symbol-value meth))))
3509       (setq methods (cdr methods)))
3510     (gnus-message 2 mess)))
3511
3512 (defun gnus-info-find-node ()
3513   "Find Info documentation of Gnus."
3514   (interactive)
3515   ;; Enlarge info window if needed.
3516   (let ((mode major-mode)
3517         gnus-info-buffer)
3518     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3519     (setq gnus-info-buffer (current-buffer))
3520     (gnus-configure-windows 'info)))
3521
3522 (defun gnus-days-between (date1 date2)
3523   ;; Return the number of days between date1 and date2.
3524   (- (gnus-day-number date1) (gnus-day-number date2)))
3525
3526 (defun gnus-day-number (date)
3527   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3528                      (timezone-parse-date date))))
3529     (timezone-absolute-from-gregorian
3530      (nth 1 dat) (nth 2 dat) (car dat))))
3531
3532 (defun gnus-encode-date (date)
3533   "Convert DATE to internal time."
3534   (let* ((parse (timezone-parse-date date))
3535          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3536          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3537     (encode-time (caddr time) (cadr time) (car time)
3538                  (caddr date) (cadr date) (car date) (nth 4 date))))
3539
3540 (defun gnus-time-minus (t1 t2)
3541   "Subtract two internal times."
3542   (let ((borrow (< (cadr t1) (cadr t2))))
3543     (list (- (car t1) (car t2) (if borrow 1 0))
3544           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3545
3546 (defun gnus-file-newer-than (file date)
3547   (let ((fdate (nth 5 (file-attributes file))))
3548     (or (> (car fdate) (car date))
3549         (and (= (car fdate) (car date))
3550              (> (nth 1 fdate) (nth 1 date))))))
3551
3552 (defmacro gnus-local-set-keys (&rest plist)
3553   "Set the keys in PLIST in the current keymap."
3554   `(gnus-define-keys-1 (current-local-map) ',plist))
3555
3556 (defmacro gnus-define-keys (keymap &rest plist)
3557   "Define all keys in PLIST in KEYMAP."
3558   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3559
3560 (put 'gnus-define-keys 'lisp-indent-function 1)
3561 (put 'gnus-define-keys 'lisp-indent-hook 1)
3562 (put 'gnus-define-keymap 'lisp-indent-function 1)
3563 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3564
3565 (defmacro gnus-define-keymap (keymap &rest plist)
3566   "Define all keys in PLIST in KEYMAP."
3567   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3568
3569 (defun gnus-define-keys-1 (keymap plist)
3570   (when (null keymap)
3571     (error "Can't set keys in a null keymap"))
3572   (cond ((symbolp keymap)
3573          (setq keymap (symbol-value keymap)))
3574         ((keymapp keymap))
3575         ((listp keymap)
3576          (set (car keymap) nil)
3577          (define-prefix-command (car keymap))
3578          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3579          (setq keymap (symbol-value (car keymap)))))
3580   (let (key)
3581     (while plist
3582       (when (symbolp (setq key (pop plist)))
3583         (setq key (symbol-value key)))
3584       (define-key keymap key (pop plist)))))
3585
3586 (defun gnus-group-read-only-p (&optional group)
3587   "Check whether GROUP supports editing or not.
3588 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3589 that that variable is buffer-local to the summary buffers."
3590   (let ((group (or group gnus-newsgroup-name)))
3591     (not (gnus-check-backend-function 'request-replace-article group))))
3592
3593 (defun gnus-group-total-expirable-p (group)
3594   "Check whether GROUP is total-expirable or not."
3595   (let ((params (gnus-info-params (gnus-get-info group))))
3596     (or (memq 'total-expire params)
3597         (cdr (assq 'total-expire params)) ; (total-expire . t)
3598         (and gnus-total-expirable-newsgroups ; Check var.
3599              (string-match gnus-total-expirable-newsgroups group)))))
3600
3601 (defun gnus-group-auto-expirable-p (group)
3602   "Check whether GROUP is total-expirable or not."
3603   (let ((params (gnus-info-params (gnus-get-info group))))
3604     (or (memq 'auto-expire params)
3605         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3606         (and gnus-auto-expirable-newsgroups ; Check var.
3607              (string-match gnus-auto-expirable-newsgroups group)))))
3608
3609 (defun gnus-virtual-group-p (group)
3610   "Say whether GROUP is virtual or not."
3611   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3612                         gnus-valid-select-methods)))
3613
3614 (defun gnus-news-group-p (group &optional article)
3615   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3616   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3617       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3618            (eq (gnus-request-type group article) 'news))))
3619
3620 (defsubst gnus-simplify-subject-fully (subject)
3621   "Simplify a subject string according to the user's wishes."
3622   (cond
3623    ((null gnus-summary-gather-subject-limit)
3624     (gnus-simplify-subject-re subject))
3625    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3626     (gnus-simplify-subject-fuzzy subject))
3627    ((numberp gnus-summary-gather-subject-limit)
3628     (gnus-limit-string (gnus-simplify-subject-re subject)
3629                        gnus-summary-gather-subject-limit))
3630    (t
3631     subject)))
3632
3633 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3634   "Check whether two subjects are equal.  If optional argument
3635 simple-first is t, first argument is already simplified."
3636   (cond
3637    ((null simple-first)
3638     (equal (gnus-simplify-subject-fully s1)
3639            (gnus-simplify-subject-fully s2)))
3640    (t
3641     (equal s1
3642            (gnus-simplify-subject-fully s2)))))
3643
3644 ;; Returns a list of writable groups.
3645 (defun gnus-writable-groups ()
3646   (let ((alist gnus-newsrc-alist)
3647         groups group)
3648     (while (setq group (car (pop alist)))
3649       (unless (gnus-group-read-only-p group)
3650         (push group groups)))
3651     (nreverse groups)))
3652
3653 (defun gnus-completing-read (default prompt &rest args)
3654   ;; Like `completing-read', except that DEFAULT is the default argument.
3655   (let* ((prompt (if default 
3656                      (concat prompt " (default " default ") ")
3657                    (concat prompt " ")))
3658          (answer (apply 'completing-read prompt args)))
3659     (if (or (null answer) (zerop (length answer)))
3660         default
3661       answer)))
3662
3663 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3664 ;; the echo area.
3665 (defun gnus-y-or-n-p (prompt)
3666   (prog1
3667       (y-or-n-p prompt)
3668     (message "")))
3669
3670 (defun gnus-yes-or-no-p (prompt)
3671   (prog1
3672       (yes-or-no-p prompt)
3673     (message "")))
3674
3675 ;; Check whether to use long file names.
3676 (defun gnus-use-long-file-name (symbol)
3677   ;; The variable has to be set...
3678   (and gnus-use-long-file-name
3679        ;; If it isn't a list, then we return t.
3680        (or (not (listp gnus-use-long-file-name))
3681            ;; If it is a list, and the list contains `symbol', we
3682            ;; return nil.
3683            (not (memq symbol gnus-use-long-file-name)))))
3684
3685 ;; I suspect there's a better way, but I haven't taken the time to do
3686 ;; it yet. -erik selberg@cs.washington.edu
3687 (defun gnus-dd-mmm (messy-date)
3688   "Return a string like DD-MMM from a big messy string"
3689   (let ((datevec (timezone-parse-date messy-date)))
3690     (format "%2s-%s"
3691             (condition-case ()
3692                 ;; Make sure leading zeroes are stripped.
3693                 (number-to-string (string-to-number (aref datevec 2)))
3694               (error "??"))
3695             (capitalize
3696              (or (car
3697                   (nth (1- (string-to-number (aref datevec 1)))
3698                        timezone-months-assoc))
3699                  "???")))))
3700
3701 ;; Make a hash table (default and minimum size is 255).
3702 ;; Optional argument HASHSIZE specifies the table size.
3703 (defun gnus-make-hashtable (&optional hashsize)
3704   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3705
3706 ;; Make a number that is suitable for hashing; bigger than MIN and one
3707 ;; less than 2^x.
3708 (defun gnus-create-hash-size (min)
3709   (let ((i 1))
3710     (while (< i min)
3711       (setq i (* 2 i)))
3712     (1- i)))
3713
3714 ;; Show message if message has a lower level than `gnus-verbose'.
3715 ;; Guideline for numbers:
3716 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3717 ;; for things that take a long time, 7 - not very important messages
3718 ;; on stuff, 9 - messages inside loops.
3719 (defun gnus-message (level &rest args)
3720   (if (<= level gnus-verbose)
3721       (apply 'message args)
3722     ;; We have to do this format thingy here even if the result isn't
3723     ;; shown - the return value has to be the same as the return value
3724     ;; from `message'.
3725     (apply 'format args)))
3726
3727 (defun gnus-error (level &rest args)
3728   "Beep an error if `gnus-verbose' is on LEVEL or less."
3729   (when (<= (floor level) gnus-verbose)
3730     (apply 'message args)
3731     (ding)
3732     (let (duration)
3733       (when (and (floatp level)
3734                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3735         (sit-for duration))))
3736   nil)
3737
3738 ;; Generate a unique new group name.
3739 (defun gnus-generate-new-group-name (leaf)
3740   (let ((name leaf)
3741         (num 0))
3742     (while (gnus-gethash name gnus-newsrc-hashtb)
3743       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3744     name))
3745
3746 (defsubst gnus-hide-text (b e props)
3747   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
3748   (gnus-add-text-properties b e props)
3749   (when (memq 'intangible props)
3750     (put-text-property (max (1- b) (point-min))
3751                        b 'intangible (cddr (memq 'intangible props)))))
3752
3753 (defsubst gnus-unhide-text (b e)
3754   "Remove hidden text properties from region between B and E."
3755   (remove-text-properties b e gnus-hidden-properties)
3756   (when (memq 'intangible gnus-hidden-properties)
3757     (put-text-property (max (1- b) (point-min))
3758                        b 'intangible nil)))
3759
3760 (defun gnus-hide-text-type (b e type)
3761   "Hide text of TYPE between B and E."
3762   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3763
3764 ;; Find out whether the gnus-visual TYPE is wanted.
3765 (defun gnus-visual-p (&optional type class)
3766   (and gnus-visual                      ; Has to be non-nil, at least.
3767        (if (not type)                   ; We don't care about type.
3768            gnus-visual
3769          (if (listp gnus-visual)        ; It's a list, so we check it.
3770              (or (memq type gnus-visual)
3771                  (memq class gnus-visual))
3772            t))))
3773
3774 (defun gnus-parent-id (references)
3775   "Return the last Message-ID in REFERENCES."
3776   (when (and references
3777              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3778     (substring references (match-beginning 1) (match-end 1))))
3779
3780 (defun gnus-split-references (references)
3781   "Return a list of Message-IDs in REFERENCES."
3782   (let ((beg 0)
3783         ids)
3784     (while (string-match "<[^>]+>" references beg)
3785       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3786             ids))
3787     (nreverse ids)))
3788
3789 (defun gnus-buffer-live-p (buffer)
3790   "Say whether BUFFER is alive or not."
3791   (and buffer
3792        (get-buffer buffer)
3793        (buffer-name (get-buffer buffer))))
3794
3795 (defun gnus-ephemeral-group-p (group)
3796   "Say whether GROUP is ephemeral or not."
3797   (gnus-group-get-parameter group 'quit-config))
3798
3799 (defun gnus-group-quit-config (group)
3800   "Return the quit-config of GROUP."
3801   (gnus-group-get-parameter group 'quit-config))
3802
3803 (defun gnus-simplify-mode-line ()
3804   "Make mode lines a bit simpler."
3805   (setq mode-line-modified "-- ")
3806   (when (listp mode-line-format)
3807     (make-local-variable 'mode-line-format)
3808     (setq mode-line-format (copy-sequence mode-line-format))
3809     (when (equal (nth 3 mode-line-format) "   ")
3810       (setcar (nthcdr 3 mode-line-format) " "))))
3811
3812 ;;; List and range functions
3813
3814 (defun gnus-last-element (list)
3815   "Return last element of LIST."
3816   (while (cdr list)
3817     (setq list (cdr list)))
3818   (car list))
3819
3820 (defun gnus-copy-sequence (list)
3821   "Do a complete, total copy of a list."
3822   (if (and (consp list) (not (consp (cdr list))))
3823       (cons (car list) (cdr list))
3824     (mapcar (lambda (elem) (if (consp elem)
3825                                (if (consp (cdr elem))
3826                                    (gnus-copy-sequence elem)
3827                                  (cons (car elem) (cdr elem)))
3828                              elem))
3829             list)))
3830
3831 (defun gnus-set-difference (list1 list2)
3832   "Return a list of elements of LIST1 that do not appear in LIST2."
3833   (let ((list1 (copy-sequence list1)))
3834     (while list2
3835       (setq list1 (delq (car list2) list1))
3836       (setq list2 (cdr list2)))
3837     list1))
3838
3839 (defun gnus-sorted-complement (list1 list2)
3840   "Return a list of elements of LIST1 that do not appear in LIST2.
3841 Both lists have to be sorted over <."
3842   (let (out)
3843     (if (or (null list1) (null list2))
3844         (or list1 list2)
3845       (while (and list1 list2)
3846         (cond ((= (car list1) (car list2))
3847                (setq list1 (cdr list1)
3848                      list2 (cdr list2)))
3849               ((< (car list1) (car list2))
3850                (setq out (cons (car list1) out))
3851                (setq list1 (cdr list1)))
3852               (t
3853                (setq out (cons (car list2) out))
3854                (setq list2 (cdr list2)))))
3855       (nconc (nreverse out) (or list1 list2)))))
3856
3857 (defun gnus-intersection (list1 list2)
3858   (let ((result nil))
3859     (while list2
3860       (if (memq (car list2) list1)
3861           (setq result (cons (car list2) result)))
3862       (setq list2 (cdr list2)))
3863     result))
3864
3865 (defun gnus-sorted-intersection (list1 list2)
3866   ;; LIST1 and LIST2 have to be sorted over <.
3867   (let (out)
3868     (while (and list1 list2)
3869       (cond ((= (car list1) (car list2))
3870              (setq out (cons (car list1) out)
3871                    list1 (cdr list1)
3872                    list2 (cdr list2)))
3873             ((< (car list1) (car list2))
3874              (setq list1 (cdr list1)))
3875             (t
3876              (setq list2 (cdr list2)))))
3877     (nreverse out)))
3878
3879 (defun gnus-set-sorted-intersection (list1 list2)
3880   ;; LIST1 and LIST2 have to be sorted over <.
3881   ;; This function modifies LIST1.
3882   (let* ((top (cons nil list1))
3883          (prev top))
3884     (while (and list1 list2)
3885       (cond ((= (car list1) (car list2))
3886              (setq prev list1
3887                    list1 (cdr list1)
3888                    list2 (cdr list2)))
3889             ((< (car list1) (car list2))
3890              (setcdr prev (cdr list1))
3891              (setq list1 (cdr list1)))
3892             (t
3893              (setq list2 (cdr list2)))))
3894     (setcdr prev nil)
3895     (cdr top)))
3896
3897 (defun gnus-compress-sequence (numbers &optional always-list)
3898   "Convert list of numbers to a list of ranges or a single range.
3899 If ALWAYS-LIST is non-nil, this function will always release a list of
3900 ranges."
3901   (let* ((first (car numbers))
3902          (last (car numbers))
3903          result)
3904     (if (null numbers)
3905         nil
3906       (if (not (listp (cdr numbers)))
3907           numbers
3908         (while numbers
3909           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3910                 ((= (1+ last) (car numbers)) ;Still in sequence
3911                  (setq last (car numbers)))
3912                 (t                      ;End of one sequence
3913                  (setq result
3914                        (cons (if (= first last) first
3915                                (cons first last)) result))
3916                  (setq first (car numbers))
3917                  (setq last  (car numbers))))
3918           (setq numbers (cdr numbers)))
3919         (if (and (not always-list) (null result))
3920             (if (= first last) (list first) (cons first last))
3921           (nreverse (cons (if (= first last) first (cons first last))
3922                           result)))))))
3923
3924 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3925 (defun gnus-uncompress-range (ranges)
3926   "Expand a list of ranges into a list of numbers.
3927 RANGES is either a single range on the form `(num . num)' or a list of
3928 these ranges."
3929   (let (first last result)
3930     (cond
3931      ((null ranges)
3932       nil)
3933      ((not (listp (cdr ranges)))
3934       (setq first (car ranges))
3935       (setq last (cdr ranges))
3936       (while (<= first last)
3937         (setq result (cons first result))
3938         (setq first (1+ first)))
3939       (nreverse result))
3940      (t
3941       (while ranges
3942         (if (atom (car ranges))
3943             (if (numberp (car ranges))
3944                 (setq result (cons (car ranges) result)))
3945           (setq first (caar ranges))
3946           (setq last  (cdar ranges))
3947           (while (<= first last)
3948             (setq result (cons first result))
3949             (setq first (1+ first))))
3950         (setq ranges (cdr ranges)))
3951       (nreverse result)))))
3952
3953 (defun gnus-add-to-range (ranges list)
3954   "Return a list of ranges that has all articles from both RANGES and LIST.
3955 Note: LIST has to be sorted over `<'."
3956   (if (not ranges)
3957       (gnus-compress-sequence list t)
3958     (setq list (copy-sequence list))
3959     (or (listp (cdr ranges))
3960         (setq ranges (list ranges)))
3961     (let ((out ranges)
3962           ilist lowest highest temp)
3963       (while (and ranges list)
3964         (setq ilist list)
3965         (setq lowest (or (and (atom (car ranges)) (car ranges))
3966                          (caar ranges)))
3967         (while (and list (cdr list) (< (cadr list) lowest))
3968           (setq list (cdr list)))
3969         (if (< (car ilist) lowest)
3970             (progn
3971               (setq temp list)
3972               (setq list (cdr list))
3973               (setcdr temp nil)
3974               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3975         (setq highest (or (and (atom (car ranges)) (car ranges))
3976                           (cdar ranges)))
3977         (while (and list (<= (car list) highest))
3978           (setq list (cdr list)))
3979         (setq ranges (cdr ranges)))
3980       (if list
3981           (setq out (nconc (gnus-compress-sequence list t) out)))
3982       (setq out (sort out (lambda (r1 r2)
3983                             (< (or (and (atom r1) r1) (car r1))
3984                                (or (and (atom r2) r2) (car r2))))))
3985       (setq ranges out)
3986       (while ranges
3987         (if (atom (car ranges))
3988             (if (cdr ranges)
3989                 (if (atom (cadr ranges))
3990                     (if (= (1+ (car ranges)) (cadr ranges))
3991                         (progn
3992                           (setcar ranges (cons (car ranges)
3993                                                (cadr ranges)))
3994                           (setcdr ranges (cddr ranges))))
3995                   (if (= (1+ (car ranges)) (caadr ranges))
3996                       (progn
3997                         (setcar (cadr ranges) (car ranges))
3998                         (setcar ranges (cadr ranges))
3999                         (setcdr ranges (cddr ranges))))))
4000           (if (cdr ranges)
4001               (if (atom (cadr ranges))
4002                   (if (= (1+ (cdar ranges)) (cadr ranges))
4003                       (progn
4004                         (setcdr (car ranges) (cadr ranges))
4005                         (setcdr ranges (cddr ranges))))
4006                 (if (= (1+ (cdar ranges)) (caadr ranges))
4007                     (progn
4008                       (setcdr (car ranges) (cdadr ranges))
4009                       (setcdr ranges (cddr ranges)))))))
4010         (setq ranges (cdr ranges)))
4011       out)))
4012
4013 (defun gnus-remove-from-range (ranges list)
4014   "Return a list of ranges that has all articles from LIST removed from RANGES.
4015 Note: LIST has to be sorted over `<'."
4016   ;; !!! This function shouldn't look like this, but I've got a headache.
4017   (gnus-compress-sequence
4018    (gnus-sorted-complement
4019     (gnus-uncompress-range ranges) list)))
4020
4021 (defun gnus-member-of-range (number ranges)
4022   (if (not (listp (cdr ranges)))
4023       (and (>= number (car ranges))
4024            (<= number (cdr ranges)))
4025     (let ((not-stop t))
4026       (while (and ranges
4027                   (if (numberp (car ranges))
4028                       (>= number (car ranges))
4029                     (>= number (caar ranges)))
4030                   not-stop)
4031         (if (if (numberp (car ranges))
4032                 (= number (car ranges))
4033               (and (>= number (caar ranges))
4034                    (<= number (cdar ranges))))
4035             (setq not-stop nil))
4036         (setq ranges (cdr ranges)))
4037       (not not-stop))))
4038
4039 (defun gnus-range-length (range)
4040   "Return the length RANGE would have if uncompressed."
4041   (length (gnus-uncompress-range range)))
4042
4043 (defun gnus-sublist-p (list sublist)
4044   "Test whether all elements in SUBLIST are members of LIST."
4045   (let ((sublistp t))
4046     (while sublist
4047       (unless (memq (pop sublist) list)
4048         (setq sublistp nil
4049               sublist nil)))
4050     sublistp))
4051
4052 \f
4053 ;;;
4054 ;;; Gnus group mode
4055 ;;;
4056
4057 (defvar gnus-group-mode-map nil)
4058 (put 'gnus-group-mode 'mode-class 'special)
4059
4060 (unless gnus-group-mode-map
4061   (setq gnus-group-mode-map (make-keymap))
4062   (suppress-keymap gnus-group-mode-map)
4063
4064   (gnus-define-keys gnus-group-mode-map
4065     " " gnus-group-read-group
4066     "=" gnus-group-select-group
4067     "\r" gnus-group-select-group
4068     "\M-\r" gnus-group-quick-select-group
4069     "j" gnus-group-jump-to-group
4070     "n" gnus-group-next-unread-group
4071     "p" gnus-group-prev-unread-group
4072     "\177" gnus-group-prev-unread-group
4073     [delete] gnus-group-prev-unread-group
4074     "N" gnus-group-next-group
4075     "P" gnus-group-prev-group
4076     "\M-n" gnus-group-next-unread-group-same-level
4077     "\M-p" gnus-group-prev-unread-group-same-level
4078     "," gnus-group-best-unread-group
4079     "." gnus-group-first-unread-group
4080     "u" gnus-group-unsubscribe-current-group
4081     "U" gnus-group-unsubscribe-group
4082     "c" gnus-group-catchup-current
4083     "C" gnus-group-catchup-current-all
4084     "l" gnus-group-list-groups
4085     "L" gnus-group-list-all-groups
4086     "m" gnus-group-mail
4087     "g" gnus-group-get-new-news
4088     "\M-g" gnus-group-get-new-news-this-group
4089     "R" gnus-group-restart
4090     "r" gnus-group-read-init-file
4091     "B" gnus-group-browse-foreign-server
4092     "b" gnus-group-check-bogus-groups
4093     "F" gnus-find-new-newsgroups
4094     "\C-c\C-d" gnus-group-describe-group
4095     "\M-d" gnus-group-describe-all-groups
4096     "\C-c\C-a" gnus-group-apropos
4097     "\C-c\M-\C-a" gnus-group-description-apropos
4098     "a" gnus-group-post-news
4099     "\ek" gnus-group-edit-local-kill
4100     "\eK" gnus-group-edit-global-kill
4101     "\C-k" gnus-group-kill-group
4102     "\C-y" gnus-group-yank-group
4103     "\C-w" gnus-group-kill-region
4104     "\C-x\C-t" gnus-group-transpose-groups
4105     "\C-c\C-l" gnus-group-list-killed
4106     "\C-c\C-x" gnus-group-expire-articles
4107     "\C-c\M-\C-x" gnus-group-expire-all-groups
4108     "V" gnus-version
4109     "s" gnus-group-save-newsrc
4110     "z" gnus-group-suspend
4111     "Z" gnus-group-clear-dribble
4112     "q" gnus-group-exit
4113     "Q" gnus-group-quit
4114     "?" gnus-group-describe-briefly
4115     "\C-c\C-i" gnus-info-find-node
4116     "\M-e" gnus-group-edit-group-method
4117     "^" gnus-group-enter-server-mode
4118     gnus-mouse-2 gnus-mouse-pick-group
4119     "<" beginning-of-buffer
4120     ">" end-of-buffer
4121     "\C-c\C-b" gnus-bug
4122     "\C-c\C-s" gnus-group-sort-groups
4123     "t" gnus-topic-mode
4124     "\C-c\M-g" gnus-activate-all-groups
4125     "\M-&" gnus-group-universal-argument
4126     "#" gnus-group-mark-group
4127     "\M-#" gnus-group-unmark-group)
4128
4129   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4130     "m" gnus-group-mark-group
4131     "u" gnus-group-unmark-group
4132     "w" gnus-group-mark-region
4133     "m" gnus-group-mark-buffer
4134     "r" gnus-group-mark-regexp
4135     "U" gnus-group-unmark-all-groups)
4136
4137   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4138     "d" gnus-group-make-directory-group
4139     "h" gnus-group-make-help-group
4140     "a" gnus-group-make-archive-group
4141     "k" gnus-group-make-kiboze-group
4142     "m" gnus-group-make-group
4143     "E" gnus-group-edit-group
4144     "e" gnus-group-edit-group-method
4145     "p" gnus-group-edit-group-parameters
4146     "v" gnus-group-add-to-virtual
4147     "V" gnus-group-make-empty-virtual
4148     "D" gnus-group-enter-directory
4149     "f" gnus-group-make-doc-group
4150     "r" gnus-group-rename-group
4151     "\177" gnus-group-delete-group
4152     [delete] gnus-group-delete-group)
4153
4154    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4155      "b" gnus-group-brew-soup
4156      "w" gnus-soup-save-areas
4157      "s" gnus-soup-send-replies
4158      "p" gnus-soup-pack-packet
4159      "r" nnsoup-pack-replies)
4160
4161    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4162      "s" gnus-group-sort-groups
4163      "a" gnus-group-sort-groups-by-alphabet
4164      "u" gnus-group-sort-groups-by-unread
4165      "l" gnus-group-sort-groups-by-level
4166      "v" gnus-group-sort-groups-by-score
4167      "r" gnus-group-sort-groups-by-rank
4168      "m" gnus-group-sort-groups-by-method)
4169
4170    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4171      "k" gnus-group-list-killed
4172      "z" gnus-group-list-zombies
4173      "s" gnus-group-list-groups
4174      "u" gnus-group-list-all-groups
4175      "A" gnus-group-list-active
4176      "a" gnus-group-apropos
4177      "d" gnus-group-description-apropos
4178      "m" gnus-group-list-matching
4179      "M" gnus-group-list-all-matching
4180      "l" gnus-group-list-level)
4181
4182    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4183      "f" gnus-score-flush-cache)
4184
4185    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4186      "f" gnus-group-fetch-faq)
4187
4188    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4189      "l" gnus-group-set-current-level
4190      "t" gnus-group-unsubscribe-current-group
4191      "s" gnus-group-unsubscribe-group
4192      "k" gnus-group-kill-group
4193      "y" gnus-group-yank-group
4194      "w" gnus-group-kill-region
4195      "\C-k" gnus-group-kill-level
4196      "z" gnus-group-kill-all-zombies))
4197
4198 (defun gnus-group-mode ()
4199   "Major mode for reading news.
4200
4201 All normal editing commands are switched off.
4202 \\<gnus-group-mode-map>
4203 The group buffer lists (some of) the groups available.  For instance,
4204 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4205 lists all zombie groups.
4206
4207 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4208 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4209
4210 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4211
4212 The following commands are available:
4213
4214 \\{gnus-group-mode-map}"
4215   (interactive)
4216   (when (and menu-bar-mode
4217              (gnus-visual-p 'group-menu 'menu))
4218     (gnus-group-make-menu-bar))
4219   (kill-all-local-variables)
4220   (gnus-simplify-mode-line)
4221   (setq major-mode 'gnus-group-mode)
4222   (setq mode-name "Group")
4223   (gnus-group-set-mode-line)
4224   (setq mode-line-process nil)
4225   (use-local-map gnus-group-mode-map)
4226   (buffer-disable-undo (current-buffer))
4227   (setq truncate-lines t)
4228   (setq buffer-read-only t)
4229   (run-hooks 'gnus-group-mode-hook))
4230
4231 (defun gnus-mouse-pick-group (e)
4232   "Enter the group under the mouse pointer."
4233   (interactive "e")
4234   (mouse-set-point e)
4235   (gnus-group-read-group nil))
4236
4237 ;; Look at LEVEL and find out what the level is really supposed to be.
4238 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4239 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4240 (defun gnus-group-default-level (&optional level number-or-nil)
4241   (cond
4242    (gnus-group-use-permanent-levels
4243     (or (setq gnus-group-use-permanent-levels
4244               (or level (if (numberp gnus-group-use-permanent-levels)
4245                             gnus-group-use-permanent-levels
4246                           (or gnus-group-default-list-level
4247                               gnus-level-subscribed))))
4248         gnus-group-default-list-level gnus-level-subscribed))
4249    (number-or-nil
4250     level)
4251    (t
4252     (or level gnus-group-default-list-level gnus-level-subscribed))))
4253
4254 ;;;###autoload
4255 (defun gnus-slave-no-server (&optional arg)
4256   "Read network news as a slave, without connecting to local server"
4257   (interactive "P")
4258   (gnus-no-server arg t))
4259
4260 ;;;###autoload
4261 (defun gnus-no-server (&optional arg slave)
4262   "Read network news.
4263 If ARG is a positive number, Gnus will use that as the
4264 startup level.  If ARG is nil, Gnus will be started at level 2.
4265 If ARG is non-nil and not a positive number, Gnus will
4266 prompt the user for the name of an NNTP server to use.
4267 As opposed to `gnus', this command will not connect to the local server."
4268   (interactive "P")
4269   (make-local-variable 'gnus-group-use-permanent-levels)
4270   (setq gnus-group-use-permanent-levels
4271         (or arg (1- gnus-level-default-subscribed)))
4272   (gnus gnus-group-use-permanent-levels t slave))
4273
4274 ;;;###autoload
4275 (defun gnus-slave (&optional arg)
4276   "Read news as a slave."
4277   (interactive "P")
4278   (gnus arg nil 'slave))
4279
4280 ;;;###autoload
4281 (defun gnus-other-frame (&optional arg)
4282   "Pop up a frame to read news."
4283   (interactive "P")
4284   (if (get-buffer gnus-group-buffer)
4285       (let ((pop-up-frames t))
4286         (gnus arg))
4287     (select-frame (make-frame))
4288     (gnus arg)))
4289
4290 ;;;###autoload
4291 (defun gnus (&optional arg dont-connect slave)
4292   "Read network news.
4293 If ARG is non-nil and a positive number, Gnus will use that as the
4294 startup level.  If ARG is non-nil and not a positive number, Gnus will
4295 prompt the user for the name of an NNTP server to use."
4296   (interactive "P")
4297
4298   (if (get-buffer gnus-group-buffer)
4299       (progn
4300         (switch-to-buffer gnus-group-buffer)
4301         (gnus-group-get-new-news))
4302
4303     (gnus-clear-system)
4304     (nnheader-init-server-buffer)
4305     (gnus-read-init-file)
4306     (setq gnus-slave slave)
4307
4308     (gnus-group-setup-buffer)
4309     (let ((buffer-read-only nil))
4310       (erase-buffer)
4311       (if (not gnus-inhibit-startup-message)
4312           (progn
4313             (gnus-group-startup-message)
4314             (sit-for 0))))
4315
4316     (let ((level (and (numberp arg) (> arg 0) arg))
4317           did-connect)
4318       (unwind-protect
4319           (progn
4320             (or dont-connect
4321                 (setq did-connect
4322                       (gnus-start-news-server (and arg (not level))))))
4323         (if (and (not dont-connect)
4324                  (not did-connect))
4325             (gnus-group-quit)
4326           (run-hooks 'gnus-startup-hook)
4327           ;; NNTP server is successfully open.
4328
4329           ;; Find the current startup file name.
4330           (setq gnus-current-startup-file
4331                 (gnus-make-newsrc-file gnus-startup-file))
4332
4333           ;; Read the dribble file.
4334           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4335
4336           ;; Allow using GroupLens predictions.
4337           (when gnus-use-grouplens
4338             (bbb-login)
4339             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4340
4341           (gnus-summary-make-display-table)
4342           ;; Do the actual startup.
4343           (gnus-setup-news nil level dont-connect)
4344           ;; Generate the group buffer.
4345           (gnus-group-list-groups level)
4346           (gnus-group-first-unread-group)
4347           (gnus-configure-windows 'group)
4348           (gnus-group-set-mode-line))))))
4349
4350 (defun gnus-unload ()
4351   "Unload all Gnus features."
4352   (interactive)
4353   (or (boundp 'load-history)
4354       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4355   (let ((history load-history)
4356         feature)
4357     (while history
4358       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4359            (setq feature (cdr (assq 'provide (car history))))
4360            (unload-feature feature 'force))
4361       (setq history (cdr history)))))
4362
4363 (defun gnus-compile ()
4364   "Byte-compile the user-defined format specs."
4365   (interactive)
4366   (let ((entries gnus-format-specs)
4367         entry gnus-tmp-func)
4368     (save-excursion
4369       (gnus-message 7 "Compiling format specs...")
4370
4371       (while entries
4372         (setq entry (pop entries))
4373         (if (eq (car entry) 'version)
4374             (setq gnus-format-specs (delq entry gnus-format-specs))
4375           (when (and (listp (caddr entry))
4376                      (not (eq 'byte-code (caaddr entry))))
4377             (fset 'gnus-tmp-func
4378                   `(lambda () ,(caddr entry)))
4379             (byte-compile 'gnus-tmp-func)
4380             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4381
4382       (push (cons 'version emacs-version) gnus-format-specs)
4383
4384       (gnus-message 7 "Compiling user specs...done"))))
4385
4386 (defun gnus-indent-rigidly (start end arg)
4387   "Indent rigidly using only spaces and no tabs."
4388   (save-excursion
4389     (save-restriction
4390       (narrow-to-region start end)
4391       (indent-rigidly start end arg)
4392       (goto-char (point-min))
4393       (while (search-forward "\t" nil t)
4394         (replace-match "        " t t)))))
4395
4396 (defun gnus-group-startup-message (&optional x y)
4397   "Insert startup message in current buffer."
4398   ;; Insert the message.
4399   (erase-buffer)
4400   (insert
4401    (format "              %s
4402           _    ___ _             _
4403           _ ___ __ ___  __    _ ___
4404           __   _     ___    __  ___
4405               _           ___     _
4406              _  _ __             _
4407              ___   __            _
4408                    __           _
4409                     _      _   _
4410                    _      _    _
4411                       _  _    _
4412                   __  ___
4413                  _   _ _     _
4414                 _   _
4415               _    _
4416              _    _
4417             _
4418           __
4419
4420 "
4421            ""))
4422   ;; And then hack it.
4423   (gnus-indent-rigidly (point-min) (point-max)
4424                        (/ (max (- (window-width) (or x 46)) 0) 2))
4425   (goto-char (point-min))
4426   (forward-line 1)
4427   (let* ((pheight (count-lines (point-min) (point-max)))
4428          (wheight (window-height))
4429          (rest (- wheight pheight)))
4430     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4431   ;; Fontify some.
4432   (goto-char (point-min))
4433   (and (search-forward "Praxis" nil t)
4434        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4435   (goto-char (point-min))
4436   (let* ((mode-string (gnus-group-set-mode-line)))
4437     (setq mode-line-buffer-identification
4438           (list (concat gnus-version (substring (car mode-string) 4))))
4439     (set-buffer-modified-p t)))
4440
4441 (defun gnus-group-setup-buffer ()
4442   (or (get-buffer gnus-group-buffer)
4443       (progn
4444         (switch-to-buffer gnus-group-buffer)
4445         (gnus-add-current-to-buffer-list)
4446         (gnus-group-mode)
4447         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4448
4449 (defun gnus-group-list-groups (&optional level unread lowest)
4450   "List newsgroups with level LEVEL or lower that have unread articles.
4451 Default is all subscribed groups.
4452 If argument UNREAD is non-nil, groups with no unread articles are also
4453 listed."
4454   (interactive (list (if current-prefix-arg
4455                          (prefix-numeric-value current-prefix-arg)
4456                        (or
4457                         (gnus-group-default-level nil t)
4458                         gnus-group-default-list-level
4459                         gnus-level-subscribed))))
4460   (or level
4461       (setq level (car gnus-group-list-mode)
4462             unread (cdr gnus-group-list-mode)))
4463   (setq level (gnus-group-default-level level))
4464   (gnus-group-setup-buffer)             ;May call from out of group buffer
4465   (gnus-update-format-specifications)
4466   (let ((case-fold-search nil)
4467         (props (text-properties-at (gnus-point-at-bol)))
4468         (group (gnus-group-group-name)))
4469     (set-buffer gnus-group-buffer)
4470     (funcall gnus-group-prepare-function level unread lowest)
4471     (if (zerop (buffer-size))
4472         (gnus-message 5 gnus-no-groups-message)
4473       (goto-char (point-max))
4474       (when (or (not gnus-group-goto-next-group-function)
4475                 (not (funcall gnus-group-goto-next-group-function 
4476                               group props)))
4477         (if (not group)
4478             ;; Go to the first group with unread articles.
4479             (gnus-group-search-forward t)
4480           ;; Find the right group to put point on.  If the current group
4481           ;; has disappeared in the new listing, try to find the next
4482           ;; one.        If no next one can be found, just leave point at the
4483           ;; first newsgroup in the buffer.
4484           (if (not (gnus-goto-char
4485                     (text-property-any
4486                      (point-min) (point-max)
4487                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4488               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4489                 (while (and newsrc
4490                             (not (gnus-goto-char
4491                                   (text-property-any
4492                                    (point-min) (point-max) 'gnus-group
4493                                    (gnus-intern-safe
4494                                     (caar newsrc) gnus-active-hashtb)))))
4495                   (setq newsrc (cdr newsrc)))
4496                 (or newsrc (progn (goto-char (point-max))
4497                                   (forward-line -1)))))))
4498       ;; Adjust cursor point.
4499       (gnus-group-position-point))))
4500
4501 (defun gnus-group-list-level (level &optional all)
4502   "List groups on LEVEL.
4503 If ALL (the prefix), also list groups that have no unread articles."
4504   (interactive "nList groups on level: \nP")
4505   (gnus-group-list-groups level all level))
4506
4507 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4508   "List all newsgroups with unread articles of level LEVEL or lower.
4509 If ALL is non-nil, list groups that have no unread articles.
4510 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4511 If REGEXP, only list groups matching REGEXP."
4512   (set-buffer gnus-group-buffer)
4513   (let ((buffer-read-only nil)
4514         (newsrc (cdr gnus-newsrc-alist))
4515         (lowest (or lowest 1))
4516         info clevel unread group params)
4517     (erase-buffer)
4518     (if (< lowest gnus-level-zombie)
4519         ;; List living groups.
4520         (while newsrc
4521           (setq info (car newsrc)
4522                 group (gnus-info-group info)
4523                 params (gnus-info-params info)
4524                 newsrc (cdr newsrc)
4525                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4526           (and unread                   ; This group might be bogus
4527                (or (not regexp)
4528                    (string-match regexp group))
4529                (<= (setq clevel (gnus-info-level info)) level)
4530                (>= clevel lowest)
4531                (or all                  ; We list all groups?
4532                    (if (eq unread t)    ; Unactivated?
4533                        gnus-group-list-inactive-groups ; We list unactivated 
4534                      (> unread 0))      ; We list groups with unread articles
4535                    (and gnus-list-groups-with-ticked-articles
4536                         (cdr (assq 'tick (gnus-info-marks info))))
4537                                         ; And groups with tickeds
4538                    ;; Check for permanent visibility.
4539                    (and gnus-permanently-visible-groups
4540                         (string-match gnus-permanently-visible-groups
4541                                       group))
4542                    (memq 'visible params)
4543                    (cdr (assq 'visible params)))
4544                (gnus-group-insert-group-line
4545                 group (gnus-info-level info)
4546                 (gnus-info-marks info) unread (gnus-info-method info)))))
4547
4548     ;; List dead groups.
4549     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4550          (gnus-group-prepare-flat-list-dead
4551           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4552           gnus-level-zombie ?Z
4553           regexp))
4554     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4555          (gnus-group-prepare-flat-list-dead
4556           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4557           gnus-level-killed ?K regexp))
4558
4559     (gnus-group-set-mode-line)
4560     (setq gnus-group-list-mode (cons level all))
4561     (run-hooks 'gnus-group-prepare-hook)))
4562
4563 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4564   ;; List zombies and killed lists somewhat faster, which was
4565   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4566   ;; this by ignoring the group format specification altogether.
4567   (let (group)
4568     (if regexp
4569         ;; This loop is used when listing groups that match some
4570         ;; regexp.
4571         (while groups
4572           (setq group (pop groups))
4573           (when (string-match regexp group)
4574             (gnus-add-text-properties
4575              (point) (prog1 (1+ (point))
4576                        (insert " " mark "     *: " group "\n"))
4577              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4578                    'gnus-unread t
4579                    'gnus-level level))))
4580       ;; This loop is used when listing all groups.
4581       (while groups
4582         (gnus-add-text-properties
4583          (point) (prog1 (1+ (point))
4584                    (insert " " mark "     *: "
4585                            (setq group (pop groups)) "\n"))
4586          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4587                'gnus-unread t
4588                'gnus-level level))))))
4589
4590 (defmacro gnus-group-real-name (group)
4591   "Find the real name of a foreign newsgroup."
4592   `(let ((gname ,group))
4593      (if (string-match ":[^:]+$" gname)
4594          (substring gname (1+ (match-beginning 0)))
4595        gname)))
4596
4597 (defsubst gnus-server-add-address (method)
4598   (let ((method-name (symbol-name (car method))))
4599     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4600              (not (assq (intern (concat method-name "-address")) method)))
4601         (append method (list (list (intern (concat method-name "-address"))
4602                                    (nth 1 method))))
4603       method)))
4604
4605 (defsubst gnus-server-get-method (group method)
4606   ;; Input either a server name, and extended server name, or a
4607   ;; select method, and return a select method.
4608   (cond ((stringp method)
4609          (gnus-server-to-method method))
4610         ((equal method gnus-select-method)
4611          gnus-select-method)
4612         ((and (stringp (car method)) group)
4613          (gnus-server-extend-method group method))
4614         ((and method (not group)
4615               (equal (cadr method) ""))
4616          method)
4617         (t
4618          (gnus-server-add-address method))))
4619
4620 (defun gnus-server-to-method (server)
4621   "Map virtual server names to select methods."
4622   (or 
4623    ;; Perhaps this is the native server?
4624    (and (equal server "native") gnus-select-method)
4625    ;; It should be in the server alist.
4626    (cdr (assoc server gnus-server-alist))
4627    ;; If not, we look through all the opened server
4628    ;; to see whether we can find it there.
4629    (let ((opened gnus-opened-servers))
4630      (while (and opened
4631                  (not (equal server (format "%s:%s" (caaar opened)
4632                                             (cadaar opened)))))
4633        (pop opened))
4634      (caar opened))))
4635
4636 (defmacro gnus-method-equal (ss1 ss2)
4637   "Say whether two servers are equal."
4638   `(let ((s1 ,ss1)
4639          (s2 ,ss2))
4640      (or (equal s1 s2)
4641          (and (= (length s1) (length s2))
4642               (progn
4643                 (while (and s1 (member (car s1) s2))
4644                   (setq s1 (cdr s1)))
4645                 (null s1))))))
4646
4647 (defun gnus-server-equal (m1 m2)
4648   "Say whether two methods are equal."
4649   (let ((m1 (cond ((null m1) gnus-select-method)
4650                   ((stringp m1) (gnus-server-to-method m1))
4651                   (t m1)))
4652         (m2 (cond ((null m2) gnus-select-method)
4653                   ((stringp m2) (gnus-server-to-method m2))
4654                   (t m2))))
4655     (gnus-method-equal m1 m2)))
4656
4657 (defun gnus-servers-using-backend (backend)
4658   "Return a list of known servers using BACKEND."
4659   (let ((opened gnus-opened-servers)
4660         out)
4661     (while opened
4662       (when (eq backend (caaar opened))
4663         (push (caar opened) out))
4664       (pop opened))
4665     out))
4666
4667 (defun gnus-group-prefixed-name (group method)
4668   "Return the whole name from GROUP and METHOD."
4669   (and (stringp method) (setq method (gnus-server-to-method method)))
4670   (concat (format "%s" (car method))
4671           (if (and
4672                (or (assoc (format "%s" (car method)) 
4673                           (gnus-methods-using 'address))
4674                    (gnus-server-equal method gnus-message-archive-method))
4675                (nth 1 method)
4676                (not (string= (nth 1 method) "")))
4677               (concat "+" (nth 1 method)))
4678           ":" group))
4679
4680 (defun gnus-group-real-prefix (group)
4681   "Return the prefix of the current group name."
4682   (if (string-match "^[^:]+:" group)
4683       (substring group 0 (match-end 0))
4684     ""))
4685
4686 (defun gnus-group-method (group)
4687   "Return the server or method used for selecting GROUP."
4688   (let ((prefix (gnus-group-real-prefix group)))
4689     (if (equal prefix "")
4690         gnus-select-method
4691       (let ((servers gnus-opened-servers)
4692             (server "")
4693             backend possible found)
4694         (if (string-match "^[^\\+]+\\+" prefix)
4695             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4696                   server (substring prefix (match-end 0) (1- (length prefix))))
4697           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4698         (while servers
4699           (when (eq (caaar servers) backend)
4700             (setq possible (caar servers))
4701             (when (equal (cadaar servers) server)
4702               (setq found (caar servers))))
4703           (pop servers))
4704         (or (car (rassoc found gnus-server-alist))
4705             found
4706             (car (rassoc possible gnus-server-alist))
4707             possible
4708             (list backend server))))))
4709
4710 (defsubst gnus-secondary-method-p (method)
4711   "Return whether METHOD is a secondary select method."
4712   (let ((methods gnus-secondary-select-methods)
4713         (gmethod (gnus-server-get-method nil method)))
4714     (while (and methods
4715                 (not (equal (gnus-server-get-method nil (car methods))
4716                             gmethod)))
4717       (setq methods (cdr methods)))
4718     methods))
4719
4720 (defun gnus-group-foreign-p (group)
4721   "Say whether a group is foreign or not."
4722   (and (not (gnus-group-native-p group))
4723        (not (gnus-group-secondary-p group))))
4724
4725 (defun gnus-group-native-p (group)
4726   "Say whether the group is native or not."
4727   (not (string-match ":" group)))
4728
4729 (defun gnus-group-secondary-p (group)
4730   "Say whether the group is secondary or not."
4731   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4732
4733 (defun gnus-group-get-parameter (group &optional symbol)
4734   "Returns the group parameters for GROUP.
4735 If SYMBOL, return the value of that symbol in the group parameters."
4736   (let ((params (gnus-info-params (gnus-get-info group))))
4737     (if symbol
4738         (gnus-group-parameter-value params symbol)
4739       params)))
4740
4741 (defun gnus-group-parameter-value (params symbol)
4742   "Return the value of SYMBOL in group PARAMS."
4743   (or (car (memq symbol params))        ; It's either a simple symbol
4744       (cdr (assq symbol params))))      ; or a cons.
4745
4746 (defun gnus-group-add-parameter (group param)
4747   "Add parameter PARAM to GROUP."
4748   (let ((info (gnus-get-info group)))
4749     (if (not info)
4750         () ; This is a dead group.  We just ignore it.
4751       ;; Cons the new param to the old one and update.
4752       (gnus-group-set-info (cons param (gnus-info-params info))
4753                            group 'params))))
4754
4755 (defun gnus-group-set-parameter (group name value)
4756   "Set parameter NAME to VALUE in GROUP."
4757   (let ((info (gnus-get-info group)))
4758     (if (not info)
4759         () ; This is a dead group.  We just ignore it.
4760       (let ((old-params (gnus-info-params info))
4761             (new-params (list (cons name value))))
4762         (while old-params
4763           (if (or (not (listp (car old-params)))
4764                   (not (eq (caar old-params) name)))
4765               (setq new-params (append new-params (list (car old-params)))))
4766           (setq old-params (cdr old-params)))
4767         (gnus-group-set-info new-params group 'params)))))
4768
4769 (defun gnus-group-add-score (group &optional score)
4770   "Add SCORE to the GROUP score.
4771 If SCORE is nil, add 1 to the score of GROUP."
4772   (let ((info (gnus-get-info group)))
4773     (when info
4774       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4775
4776 (defun gnus-summary-bubble-group ()
4777   "Increase the score of the current group.
4778 This is a handy function to add to `gnus-summary-exit-hook' to
4779 increase the score of each group you read."
4780   (gnus-group-add-score gnus-newsgroup-name))
4781
4782 (defun gnus-group-set-info (info &optional method-only-group part)
4783   (let* ((entry (gnus-gethash
4784                  (or method-only-group (gnus-info-group info))
4785                  gnus-newsrc-hashtb))
4786          (part-info info)
4787          (info (if method-only-group (nth 2 entry) info))
4788          method)
4789     (when method-only-group
4790       (unless entry
4791         (error "Trying to change non-existent group %s" method-only-group))
4792       ;; We have received parts of the actual group info - either the
4793       ;; select method or the group parameters.  We first check
4794       ;; whether we have to extend the info, and if so, do that.
4795       (let ((len (length info))
4796             (total (if (eq part 'method) 5 6)))
4797         (when (< len total)
4798           (setcdr (nthcdr (1- len) info)
4799                   (make-list (- total len) nil)))
4800         ;; Then we enter the new info.
4801         (setcar (nthcdr (1- total) info) part-info)))
4802     (unless entry
4803       ;; This is a new group, so we just create it.
4804       (save-excursion
4805         (set-buffer gnus-group-buffer)
4806         (setq method (gnus-info-method info))
4807         (when (gnus-server-equal method "native")
4808           (setq method nil))
4809         (save-excursion
4810           (set-buffer gnus-group-buffer)
4811           (if method
4812               ;; It's a foreign group...
4813               (gnus-group-make-group
4814                (gnus-group-real-name (gnus-info-group info))
4815                (if (stringp method) method
4816                  (prin1-to-string (car method)))
4817                (and (consp method)
4818                     (nth 1 (gnus-info-method info))))
4819             ;; It's a native group.
4820             (gnus-group-make-group (gnus-info-group info))))
4821         (gnus-message 6 "Note: New group created")
4822         (setq entry
4823               (gnus-gethash (gnus-group-prefixed-name
4824                              (gnus-group-real-name (gnus-info-group info))
4825                              (or (gnus-info-method info) gnus-select-method))
4826                             gnus-newsrc-hashtb))))
4827     ;; Whether it was a new group or not, we now have the entry, so we
4828     ;; can do the update.
4829     (if entry
4830         (progn
4831           (setcar (nthcdr 2 entry) info)
4832           (when (and (not (eq (car entry) t))
4833                      (gnus-active (gnus-info-group info)))
4834             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4835       (error "No such group: %s" (gnus-info-group info)))))
4836
4837 (defun gnus-group-set-method-info (group select-method)
4838   (gnus-group-set-info select-method group 'method))
4839
4840 (defun gnus-group-set-params-info (group params)
4841   (gnus-group-set-info params group 'params))
4842
4843 (defun gnus-group-update-group-line ()
4844   "Update the current line in the group buffer."
4845   (let* ((buffer-read-only nil)
4846          (group (gnus-group-group-name))
4847          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4848     (and entry
4849          (not (gnus-ephemeral-group-p group))
4850          (gnus-dribble-enter
4851           (concat "(gnus-group-set-info '"
4852                   (prin1-to-string (nth 2 entry)) ")")))
4853     (gnus-delete-line)
4854     (gnus-group-insert-group-line-info group)
4855     (forward-line -1)
4856     (gnus-group-position-point)))
4857
4858 (defun gnus-group-insert-group-line-info (group)
4859   "Insert GROUP on the current line."
4860   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4861         active info)
4862     (if entry
4863         (progn
4864           ;; (Un)subscribed group.
4865           (setq info (nth 2 entry))
4866           (gnus-group-insert-group-line
4867            group (gnus-info-level info) (gnus-info-marks info)
4868            (or (car entry) t) (gnus-info-method info)))
4869       ;; This group is dead.
4870       (gnus-group-insert-group-line
4871        group
4872        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4873        nil
4874        (if (setq active (gnus-active group))
4875            (- (1+ (cdr active)) (car active)) 0)
4876        nil))))
4877
4878 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4879                                                     gnus-tmp-marked number
4880                                                     gnus-tmp-method)
4881   "Insert a group line in the group buffer."
4882   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4883          (gnus-tmp-number-total
4884           (if gnus-tmp-active
4885               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4886             0))
4887          (gnus-tmp-number-of-unread
4888           (if (numberp number) (int-to-string (max 0 number))
4889             "*"))
4890          (gnus-tmp-number-of-read
4891           (if (numberp number)
4892               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4893             "*"))
4894          (gnus-tmp-subscribed
4895           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4896                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4897                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4898                 (t ?K)))
4899          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4900          (gnus-tmp-newsgroup-description
4901           (if gnus-description-hashtb
4902               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4903             ""))
4904          (gnus-tmp-moderated
4905           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4906          (gnus-tmp-moderated-string
4907           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4908          (gnus-tmp-method
4909           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4910          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4911          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4912          (gnus-tmp-news-method-string
4913           (if gnus-tmp-method
4914               (format "(%s:%s)" (car gnus-tmp-method)
4915                       (cadr gnus-tmp-method)) ""))
4916          (gnus-tmp-marked-mark
4917           (if (and (numberp number)
4918                    (zerop number)
4919                    (cdr (assq 'tick gnus-tmp-marked)))
4920               ?* ? ))
4921          (gnus-tmp-process-marked
4922           (if (member gnus-tmp-group gnus-group-marked)
4923               gnus-process-mark ? ))
4924          (gnus-tmp-grouplens
4925           (or (and gnus-use-grouplens
4926                    (bbb-grouplens-group-p gnus-tmp-group))
4927               ""))
4928          (buffer-read-only nil)
4929          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4930     (beginning-of-line)
4931     (gnus-add-text-properties
4932      (point)
4933      (prog1 (1+ (point))
4934        ;; Insert the text.
4935        (eval gnus-group-line-format-spec))
4936      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4937        gnus-unread ,(if (numberp number)
4938                         (string-to-int gnus-tmp-number-of-unread)
4939                       t)
4940        gnus-marked ,gnus-tmp-marked-mark
4941        gnus-indentation ,gnus-group-indentation
4942        gnus-level ,gnus-tmp-level))
4943     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4944       (forward-line -1)
4945       (run-hooks 'gnus-group-update-hook)
4946       (forward-line))))
4947
4948 (defun gnus-group-update-group (group &optional visible-only)
4949   "Update all lines where GROUP appear.
4950 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4951 already."
4952   (save-excursion
4953     (set-buffer gnus-group-buffer)
4954     ;; The buffer may be narrowed.
4955     (save-restriction
4956       (widen)
4957       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4958             (loc (point-min))
4959             found buffer-read-only)
4960         ;; Enter the current status into the dribble buffer.
4961         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4962           (if (and entry (not (gnus-ephemeral-group-p group)))
4963               (gnus-dribble-enter
4964                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4965                        ")"))))
4966         ;; Find all group instances.  If topics are in use, each group
4967         ;; may be listed in more than once.
4968         (while (setq loc (text-property-any
4969                           loc (point-max) 'gnus-group ident))
4970           (setq found t)
4971           (goto-char loc)
4972           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4973             (gnus-delete-line)
4974             (gnus-group-insert-group-line-info group))
4975           (setq loc (1+ loc)))
4976         (unless (or found visible-only)
4977           ;; No such line in the buffer, find out where it's supposed to
4978           ;; go, and insert it there (or at the end of the buffer).
4979           (if gnus-goto-missing-group-function
4980               (funcall gnus-goto-missing-group-function group)
4981             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4982               (while (and entry (car entry)
4983                           (not
4984                            (gnus-goto-char
4985                             (text-property-any
4986                              (point-min) (point-max)
4987                              'gnus-group (gnus-intern-safe
4988                                           (caar entry) gnus-active-hashtb)))))
4989                 (setq entry (cdr entry)))
4990               (or entry (goto-char (point-max)))))
4991           ;; Finally insert the line.
4992           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4993             (gnus-group-insert-group-line-info group)))
4994         (gnus-group-set-mode-line)))))
4995
4996 (defun gnus-group-set-mode-line ()
4997   (when (memq 'group gnus-updated-mode-lines)
4998     (let* ((gformat (or gnus-group-mode-line-format-spec
4999                         (setq gnus-group-mode-line-format-spec
5000                               (gnus-parse-format
5001                                gnus-group-mode-line-format
5002                                gnus-group-mode-line-format-alist))))
5003            (gnus-tmp-news-server (cadr gnus-select-method))
5004            (gnus-tmp-news-method (car gnus-select-method))
5005            (max-len 60)
5006            gnus-tmp-header                      ;Dummy binding for user-defined formats
5007            ;; Get the resulting string.
5008            (mode-string (eval gformat)))
5009       ;; Say whether the dribble buffer has been modified.
5010       (setq mode-line-modified
5011             (if (and gnus-dribble-buffer
5012                      (buffer-name gnus-dribble-buffer)
5013                      (buffer-modified-p gnus-dribble-buffer))
5014                 "-* " "-- "))
5015       ;; If the line is too long, we chop it off.
5016       (when (> (length mode-string) max-len)
5017         (setq mode-string (substring mode-string 0 (- max-len 4))))
5018       (prog1
5019           (setq mode-line-buffer-identification (list mode-string))
5020         (set-buffer-modified-p t)))))
5021
5022 (defun gnus-group-group-name ()
5023   "Get the name of the newsgroup on the current line."
5024   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5025     (and group (symbol-name group))))
5026
5027 (defun gnus-group-group-level ()
5028   "Get the level of the newsgroup on the current line."
5029   (get-text-property (gnus-point-at-bol) 'gnus-level))
5030
5031 (defun gnus-group-group-indentation ()
5032   "Get the indentation of the newsgroup on the current line."
5033   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5034       (and gnus-group-indentation-function
5035            (funcall gnus-group-indentation-function))
5036       ""))
5037
5038 (defun gnus-group-group-unread ()
5039   "Get the number of unread articles of the newsgroup on the current line."
5040   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5041
5042 (defun gnus-group-search-forward (&optional backward all level first-too)
5043   "Find the next newsgroup with unread articles.
5044 If BACKWARD is non-nil, find the previous newsgroup instead.
5045 If ALL is non-nil, just find any newsgroup.
5046 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5047 group exists.
5048 If FIRST-TOO, the current line is also eligible as a target."
5049   (let ((way (if backward -1 1))
5050         (low gnus-level-killed)
5051         (beg (point))
5052         pos found lev)
5053     (if (and backward (progn (beginning-of-line)) (bobp))
5054         nil
5055       (or first-too (forward-line way))
5056       (while (and
5057               (not (eobp))
5058               (not (setq
5059                     found
5060                     (and (or all
5061                              (and
5062                               (let ((unread
5063                                      (get-text-property (point) 'gnus-unread)))
5064                                 (and (numberp unread) (> unread 0)))
5065                               (setq lev (get-text-property (point)
5066                                                            'gnus-level))
5067                               (<= lev gnus-level-subscribed)))
5068                          (or (not level)
5069                              (and (setq lev (get-text-property (point)
5070                                                                'gnus-level))
5071                                   (or (= lev level)
5072                                       (and (< lev low)
5073                                            (< level lev)
5074                                            (progn
5075                                              (setq low lev)
5076                                              (setq pos (point))
5077                                              nil))))))))
5078               (zerop (forward-line way)))))
5079     (if found
5080         (progn (gnus-group-position-point) t)
5081       (goto-char (or pos beg))
5082       (and pos t))))
5083
5084 ;;; Gnus group mode commands
5085
5086 ;; Group marking.
5087
5088 (defun gnus-group-mark-group (n &optional unmark no-advance)
5089   "Mark the current group."
5090   (interactive "p")
5091   (let ((buffer-read-only nil)
5092         group)
5093     (while (and (> n 0)
5094                 (not (eobp)))
5095       (when (setq group (gnus-group-group-name))
5096         ;; Update the mark.
5097         (beginning-of-line)
5098         (forward-char
5099          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5100         (delete-char 1)
5101         (if unmark
5102             (progn
5103               (insert " ")
5104               (setq gnus-group-marked (delete group gnus-group-marked)))
5105           (insert "#")
5106           (setq gnus-group-marked
5107                 (cons group (delete group gnus-group-marked))))
5108         (or no-advance (zerop (gnus-group-next-group 1))))
5109       (decf n))
5110     (gnus-summary-position-point)
5111     n))
5112
5113 (defun gnus-group-unmark-group (n)
5114   "Remove the mark from the current group."
5115   (interactive "p")
5116   (gnus-group-mark-group n 'unmark)
5117   (gnus-group-position-point))
5118
5119 (defun gnus-group-unmark-all-groups ()
5120   "Unmark all groups."
5121   (interactive)
5122   (let ((groups gnus-group-marked))
5123     (save-excursion
5124       (while groups
5125         (gnus-group-remove-mark (pop groups)))))
5126   (gnus-group-position-point))
5127
5128 (defun gnus-group-mark-region (unmark beg end)
5129   "Mark all groups between point and mark.
5130 If UNMARK, remove the mark instead."
5131   (interactive "P\nr")
5132   (let ((num (count-lines beg end)))
5133     (save-excursion
5134       (goto-char beg)
5135       (- num (gnus-group-mark-group num unmark)))))
5136
5137 (defun gnus-group-mark-buffer (&optional unmark)
5138   "Mark all groups in the buffer.
5139 If UNMARK, remove the mark instead."
5140   (interactive "P")
5141   (gnus-group-mark-region unmark (point-min) (point-max)))
5142
5143 (defun gnus-group-mark-regexp (regexp)
5144   "Mark all groups that match some regexp."
5145   (interactive "sMark (regexp): ")
5146   (let ((alist (cdr gnus-newsrc-alist))
5147         group)
5148     (while alist
5149       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5150         (gnus-group-set-mark group))))
5151   (gnus-group-position-point))
5152
5153 (defun gnus-group-remove-mark (group)
5154   "Remove the process mark from GROUP and move point there.
5155 Return nil if the group isn't displayed."
5156   (if (gnus-group-goto-group group)
5157       (save-excursion
5158         (gnus-group-mark-group 1 'unmark t)
5159         t)
5160     (setq gnus-group-marked
5161           (delete group gnus-group-marked))
5162     nil))
5163
5164 (defun gnus-group-set-mark (group)
5165   "Set the process mark on GROUP."
5166   (if (gnus-group-goto-group group) 
5167       (save-excursion
5168         (gnus-group-mark-group 1 nil t))
5169     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5170
5171 (defun gnus-group-universal-argument (arg &optional groups func)
5172   "Perform any command on all groups accoring to the process/prefix convention."
5173   (interactive "P")
5174   (let ((groups (or groups (gnus-group-process-prefix arg)))
5175         group func)
5176     (if (eq (setq func (or func
5177                            (key-binding
5178                             (read-key-sequence
5179                              (substitute-command-keys
5180                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5181             'undefined)
5182         (gnus-error 1 "Undefined key")
5183       (while groups
5184         (gnus-group-remove-mark (setq group (pop groups)))
5185         (command-execute func))))
5186   (gnus-group-position-point))
5187
5188 (defun gnus-group-process-prefix (n)
5189   "Return a list of groups to work on.
5190 Take into consideration N (the prefix) and the list of marked groups."
5191   (cond
5192    (n
5193     (setq n (prefix-numeric-value n))
5194     ;; There is a prefix, so we return a list of the N next
5195     ;; groups.
5196     (let ((way (if (< n 0) -1 1))
5197           (n (abs n))
5198           group groups)
5199       (save-excursion
5200         (while (and (> n 0)
5201                     (setq group (gnus-group-group-name)))
5202           (setq groups (cons group groups))
5203           (setq n (1- n))
5204           (gnus-group-next-group way)))
5205       (nreverse groups)))
5206    ((and (boundp 'transient-mark-mode)
5207          transient-mark-mode
5208          (boundp 'mark-active)
5209          mark-active)
5210     ;; Work on the region between point and mark.
5211     (let ((max (max (point) (mark)))
5212           groups)
5213       (save-excursion
5214         (goto-char (min (point) (mark)))
5215         (while
5216             (and
5217              (push (gnus-group-group-name) groups)
5218              (zerop (gnus-group-next-group 1))
5219              (< (point) max)))
5220         (nreverse groups))))
5221    (gnus-group-marked
5222     ;; No prefix, but a list of marked articles.
5223     (reverse gnus-group-marked))
5224    (t
5225     ;; Neither marked articles or a prefix, so we return the
5226     ;; current group.
5227     (let ((group (gnus-group-group-name)))
5228       (and group (list group))))))
5229
5230 ;; Selecting groups.
5231
5232 (defun gnus-group-read-group (&optional all no-article group)
5233   "Read news in this newsgroup.
5234 If the prefix argument ALL is non-nil, already read articles become
5235 readable.  IF ALL is a number, fetch this number of articles.  If the
5236 optional argument NO-ARTICLE is non-nil, no article will be
5237 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5238 group."
5239   (interactive "P")
5240   (let ((group (or group (gnus-group-group-name)))
5241         number active marked entry)
5242     (or group (error "No group on current line"))
5243     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5244                                             group gnus-newsrc-hashtb)))))
5245     ;; This group might be a dead group.  In that case we have to get
5246     ;; the number of unread articles from `gnus-active-hashtb'.
5247     (setq number
5248           (cond ((numberp all) all)
5249                 (entry (car entry))
5250                 ((setq active (gnus-active group))
5251                  (- (1+ (cdr active)) (car active)))))
5252     (gnus-summary-read-group
5253      group (or all (and (numberp number)
5254                         (zerop (+ number (length (cdr (assq 'tick marked)))
5255                                   (length (cdr (assq 'dormant marked)))))))
5256      no-article)))
5257
5258 (defun gnus-group-select-group (&optional all)
5259   "Select this newsgroup.
5260 No article is selected automatically.
5261 If ALL is non-nil, already read articles become readable.
5262 If ALL is a number, fetch this number of articles."
5263   (interactive "P")
5264   (gnus-group-read-group all t))
5265
5266 (defun gnus-group-quick-select-group (&optional all)
5267   "Select the current group \"quickly\".
5268 This means that no highlighting or scoring will be performed."
5269   (interactive "P")
5270   (let (gnus-visual
5271         gnus-score-find-score-files-function
5272         gnus-apply-kill-hook
5273         gnus-summary-expunge-below)
5274     (gnus-group-read-group all t)))
5275
5276 (defun gnus-group-visible-select-group (&optional all)
5277   "Select the current group without hiding any articles."
5278   (interactive "P")
5279   (let ((gnus-inhibit-limiting t))
5280     (gnus-group-read-group all t)))
5281
5282 ;;;###autoload
5283 (defun gnus-fetch-group (group)
5284   "Start Gnus if necessary and enter GROUP.
5285 Returns whether the fetching was successful or not."
5286   (interactive "sGroup name: ")
5287   (or (get-buffer gnus-group-buffer)
5288       (gnus))
5289   (gnus-group-read-group nil nil group))
5290
5291 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5292 ;; if selection was successful.
5293 (defun gnus-group-read-ephemeral-group
5294   (group method &optional activate quit-config)
5295   (let ((group (if (gnus-group-foreign-p group) group
5296                  (gnus-group-prefixed-name group method))))
5297     (gnus-sethash
5298      group
5299      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5300                      ((quit-config . ,(if quit-config quit-config
5301                                         (cons (current-buffer) 'summary))))))
5302      gnus-newsrc-hashtb)
5303     (set-buffer gnus-group-buffer)
5304     (or (gnus-check-server method)
5305         (error "Unable to contact server: %s" (gnus-status-message method)))
5306     (if activate (or (gnus-request-group group)
5307                      (error "Couldn't request group")))
5308     (condition-case ()
5309         (gnus-group-read-group t t group)
5310       (error nil)
5311       (quit nil))))
5312
5313 (defun gnus-group-jump-to-group (group)
5314   "Jump to newsgroup GROUP."
5315   (interactive
5316    (list (completing-read
5317           "Group: " gnus-active-hashtb nil
5318           (gnus-read-active-file-p)
5319           nil
5320           'gnus-group-history)))
5321
5322   (when (equal group "")
5323     (error "Empty group name"))
5324
5325   (when (string-match "[\000-\032]" group)
5326     (error "Control characters in group: %s" group))
5327
5328   (let ((b (text-property-any
5329             (point-min) (point-max)
5330             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5331     (unless (gnus-ephemeral-group-p group)
5332       (if b
5333           ;; Either go to the line in the group buffer...
5334           (goto-char b)
5335         ;; ... or insert the line.
5336         (or
5337          (gnus-active group)
5338          (gnus-activate-group group)
5339          (error "%s error: %s" group (gnus-status-message group)))
5340
5341         (gnus-group-update-group group)
5342         (goto-char (text-property-any
5343                     (point-min) (point-max)
5344                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5345     ;; Adjust cursor point.
5346     (gnus-group-position-point)))
5347
5348 (defun gnus-group-goto-group (group)
5349   "Goto to newsgroup GROUP."
5350   (when group
5351     (let ((b (text-property-any (point-min) (point-max)
5352                                 'gnus-group (gnus-intern-safe
5353                                              group gnus-active-hashtb))))
5354       (and b (goto-char b)))))
5355
5356 (defun gnus-group-next-group (n)
5357   "Go to next N'th newsgroup.
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))
5363
5364 (defun gnus-group-next-unread-group (n &optional all level)
5365   "Go to next N'th unread newsgroup.
5366 If N is negative, search backward instead.
5367 If ALL is non-nil, choose any newsgroup, unread or not.
5368 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5369 such group can be found, the next group with a level higher than
5370 LEVEL.
5371 Returns the difference between N and the number of skips actually
5372 made."
5373   (interactive "p")
5374   (let ((backward (< n 0))
5375         (n (abs n)))
5376     (while (and (> n 0)
5377                 (gnus-group-search-forward
5378                  backward (or (not gnus-group-goto-unread) all) level))
5379       (setq n (1- n)))
5380     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5381                                (if level " on this level or higher" "")))
5382     n))
5383
5384 (defun gnus-group-prev-group (n)
5385   "Go to previous N'th newsgroup.
5386 Returns the difference between N and the number of skips actually
5387 done."
5388   (interactive "p")
5389   (gnus-group-next-unread-group (- n) t))
5390
5391 (defun gnus-group-prev-unread-group (n)
5392   "Go to previous N'th unread newsgroup.
5393 Returns the difference between N and the number of skips actually
5394 done."
5395   (interactive "p")
5396   (gnus-group-next-unread-group (- n)))
5397
5398 (defun gnus-group-next-unread-group-same-level (n)
5399   "Go to next N'th unread newsgroup on the same level.
5400 If N is negative, search backward instead.
5401 Returns the difference between N and the number of skips actually
5402 done."
5403   (interactive "p")
5404   (gnus-group-next-unread-group n t (gnus-group-group-level))
5405   (gnus-group-position-point))
5406
5407 (defun gnus-group-prev-unread-group-same-level (n)
5408   "Go to next N'th unread newsgroup on the same level.
5409 Returns the difference between N and the number of skips actually
5410 done."
5411   (interactive "p")
5412   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5413   (gnus-group-position-point))
5414
5415 (defun gnus-group-best-unread-group (&optional exclude-group)
5416   "Go to the group with the highest level.
5417 If EXCLUDE-GROUP, do not go to that group."
5418   (interactive)
5419   (goto-char (point-min))
5420   (let ((best 100000)
5421         unread best-point)
5422     (while (not (eobp))
5423       (setq unread (get-text-property (point) 'gnus-unread))
5424       (if (and (numberp unread) (> unread 0))
5425           (progn
5426             (if (and (get-text-property (point) 'gnus-level)
5427                      (< (get-text-property (point) 'gnus-level) best)
5428                      (or (not exclude-group)
5429                          (not (equal exclude-group (gnus-group-group-name)))))
5430                 (progn
5431                   (setq best (get-text-property (point) 'gnus-level))
5432                   (setq best-point (point))))))
5433       (forward-line 1))
5434     (if best-point (goto-char best-point))
5435     (gnus-summary-position-point)
5436     (and best-point (gnus-group-group-name))))
5437
5438 (defun gnus-group-first-unread-group ()
5439   "Go to the first group with unread articles."
5440   (interactive)
5441   (prog1
5442       (let ((opoint (point))
5443             unread)
5444         (goto-char (point-min))
5445         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5446                 (and (numberp unread)   ; Not a topic.
5447                      (not (zerop unread))) ; Has unread articles.
5448                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5449             (point)                     ; Success.
5450           (goto-char opoint)
5451           nil))                         ; Not success.
5452     (gnus-group-position-point)))
5453
5454 (defun gnus-group-enter-server-mode ()
5455   "Jump to the server buffer."
5456   (interactive)
5457   (gnus-enter-server-buffer))
5458
5459 (defun gnus-group-make-group (name &optional method address)
5460   "Add a new newsgroup.
5461 The user will be prompted for a NAME, for a select METHOD, and an
5462 ADDRESS."
5463   (interactive
5464    (cons
5465     (read-string "Group name: ")
5466     (let ((method
5467            (completing-read
5468             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5469             nil t nil 'gnus-method-history)))
5470       (cond ((assoc method gnus-valid-select-methods)
5471              (list method
5472                    (if (memq 'prompt-address
5473                              (assoc method gnus-valid-select-methods))
5474                        (read-string "Address: ")
5475                      "")))
5476             ((assoc method gnus-server-alist)
5477              (list method))
5478             (t
5479              (list method ""))))))
5480
5481   (let* ((meth (and method (if address (list (intern method) address)
5482                              method)))
5483          (nname (if method (gnus-group-prefixed-name name meth) name))
5484          backend info)
5485     (when (gnus-gethash nname gnus-newsrc-hashtb)
5486       (error "Group %s already exists" nname))
5487     ;; Subscribe to the new group.
5488     (gnus-group-change-level
5489      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5490      gnus-level-default-subscribed gnus-level-killed
5491      (and (gnus-group-group-name)
5492           (gnus-gethash (gnus-group-group-name)
5493                         gnus-newsrc-hashtb))
5494      t)
5495     ;; Make it active.
5496     (gnus-set-active nname (cons 1 0))
5497     (or (gnus-ephemeral-group-p name)
5498         (gnus-dribble-enter
5499          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5500     ;; Insert the line.
5501     (gnus-group-insert-group-line-info nname)
5502     (forward-line -1)
5503     (gnus-group-position-point)
5504
5505     ;; Load the backend and try to make the backend create
5506     ;; the group as well.
5507     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5508                                                   nil meth))))
5509                  gnus-valid-select-methods)
5510       (require backend))
5511     (gnus-check-server meth)
5512     (and (gnus-check-backend-function 'request-create-group nname)
5513          (gnus-request-create-group nname))
5514     t))
5515
5516 (defun gnus-group-delete-group (group &optional force)
5517   "Delete the current group.
5518 If FORCE (the prefix) is non-nil, all the articles in the group will
5519 be deleted.  This is \"deleted\" as in \"removed forever from the face
5520 of the Earth\".  There is no undo."
5521   (interactive
5522    (list (gnus-group-group-name)
5523          current-prefix-arg))
5524   (or group (error "No group to rename"))
5525   (or (gnus-check-backend-function 'request-delete-group group)
5526       (error "This backend does not support group deletion"))
5527   (prog1
5528       (if (not (gnus-yes-or-no-p
5529                 (format
5530                  "Do you really want to delete %s%s? "
5531                  group (if force " and all its contents" ""))))
5532           () ; Whew!
5533         (gnus-message 6 "Deleting group %s..." group)
5534         (if (not (gnus-request-delete-group group force))
5535             (gnus-error 3 "Couldn't delete group %s" group)
5536           (gnus-message 6 "Deleting group %s...done" group)
5537           (gnus-group-goto-group group)
5538           (gnus-group-kill-group 1 t)
5539           (gnus-sethash group nil gnus-active-hashtb)
5540           t))
5541     (gnus-group-position-point)))
5542
5543 (defun gnus-group-rename-group (group new-name)
5544   (interactive
5545    (list
5546     (gnus-group-group-name)
5547     (progn
5548       (or (gnus-check-backend-function
5549            'request-rename-group (gnus-group-group-name))
5550           (error "This backend does not support renaming groups"))
5551       (read-string "New group name: "))))
5552
5553   (or (gnus-check-backend-function 'request-rename-group group)
5554       (error "This backend does not support renaming groups"))
5555
5556   (or group (error "No group to rename"))
5557   (and (string-match "^[ \t]*$" new-name)
5558        (error "Not a valid group name"))
5559
5560   ;; We find the proper prefixed name.
5561   (setq new-name
5562         (gnus-group-prefixed-name
5563          (gnus-group-real-name new-name)
5564          (gnus-info-method (gnus-get-info group))))
5565
5566   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5567   (prog1
5568       (if (not (gnus-request-rename-group group new-name))
5569           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5570         ;; We rename the group internally by killing it...
5571         (gnus-group-goto-group group)
5572         (gnus-group-kill-group)
5573         ;; ... changing its name ...
5574         (setcar (cdar gnus-list-of-killed-groups) new-name)
5575         ;; ... and then yanking it.  Magic!
5576         (gnus-group-yank-group)
5577         (gnus-set-active new-name (gnus-active group))
5578         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5579         new-name)
5580     (gnus-group-position-point)))
5581
5582 (defun gnus-group-edit-group (group &optional part)
5583   "Edit the group on the current line."
5584   (interactive (list (gnus-group-group-name)))
5585   (let* ((part (or part 'info))
5586          (done-func `(lambda ()
5587                        "Exit editing mode and update the information."
5588                        (interactive)
5589                        (gnus-group-edit-group-done ',part ,group)))
5590          (winconf (current-window-configuration))
5591          info)
5592     (or group (error "No group on current line"))
5593     (or (setq info (gnus-get-info group))
5594         (error "Killed group; can't be edited"))
5595     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5596     (gnus-configure-windows 'edit-group)
5597     (gnus-add-current-to-buffer-list)
5598     (emacs-lisp-mode)
5599     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5600     (use-local-map (copy-keymap emacs-lisp-mode-map))
5601     (local-set-key "\C-c\C-c" done-func)
5602     (make-local-variable 'gnus-prev-winconf)
5603     (setq gnus-prev-winconf winconf)
5604     (erase-buffer)
5605     (insert
5606      (cond
5607       ((eq part 'method)
5608        ";; Type `C-c C-c' after editing the select method.\n\n")
5609       ((eq part 'params)
5610        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5611       ((eq part 'info)
5612        ";; Type `C-c C-c' after editing the group info.\n\n")))
5613     (insert
5614      (pp-to-string
5615       (cond ((eq part 'method)
5616              (or (gnus-info-method info) "native"))
5617             ((eq part 'params)
5618              (gnus-info-params info))
5619             (t info)))
5620      "\n")))
5621
5622 (defun gnus-group-edit-group-method (group)
5623   "Edit the select method of GROUP."
5624   (interactive (list (gnus-group-group-name)))
5625   (gnus-group-edit-group group 'method))
5626
5627 (defun gnus-group-edit-group-parameters (group)
5628   "Edit the group parameters of GROUP."
5629   (interactive (list (gnus-group-group-name)))
5630   (gnus-group-edit-group group 'params))
5631
5632 (defun gnus-group-edit-group-done (part group)
5633   "Get info from buffer, update variables and jump to the group buffer."
5634   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5635   (goto-char (point-min))
5636   (let* ((form (read (current-buffer)))
5637          (winconf gnus-prev-winconf)
5638          (method (cond ((eq part 'info) (nth 4 form))
5639                        ((eq part 'method) form)
5640                        (t nil)))
5641          (info (cond ((eq part 'info) form)
5642                      ((eq part 'method) (gnus-get-info group))
5643                      (t nil)))
5644          (new-group (if info
5645                       (if (or (not method)
5646                               (gnus-server-equal
5647                                gnus-select-method method))
5648                           (gnus-group-real-name (car info))
5649                         (gnus-group-prefixed-name
5650                          (gnus-group-real-name (car info)) method))
5651                       nil)))
5652     (when (and new-group
5653                (not (equal new-group group)))
5654       (when (gnus-group-goto-group group)
5655         (gnus-group-kill-group 1))
5656       (gnus-activate-group new-group))
5657     ;; Set the info.
5658     (if (and info new-group)
5659         (progn
5660           (setq info (gnus-copy-sequence info))
5661           (setcar info new-group)
5662           (unless (gnus-server-equal method "native")
5663             (unless (nthcdr 3 info)
5664               (nconc info (list nil nil)))
5665             (unless (nthcdr 4 info)
5666               (nconc info (list nil)))
5667             (gnus-info-set-method info method))
5668           (gnus-group-set-info info))
5669       (gnus-group-set-info form (or new-group group) part))
5670     (kill-buffer (current-buffer))
5671     (and winconf (set-window-configuration winconf))
5672     (set-buffer gnus-group-buffer)
5673     (gnus-group-update-group (or new-group group))
5674     (gnus-group-position-point)))
5675
5676 (defun gnus-group-make-help-group ()
5677   "Create the Gnus documentation group."
5678   (interactive)
5679   (let ((path load-path)
5680         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5681         file dir)
5682     (and (gnus-gethash name gnus-newsrc-hashtb)
5683          (error "Documentation group already exists"))
5684     (while path
5685       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5686             file nil)
5687       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5688                 (file-exists-p
5689                  (setq file (concat (file-name-directory
5690                                      (directory-file-name dir))
5691                                     "etc/gnus-tut.txt"))))
5692         (setq path nil)))
5693     (if (not file)
5694         (gnus-message 1 "Couldn't find doc group")
5695       (gnus-group-make-group
5696        (gnus-group-real-name name)
5697        (list 'nndoc "gnus-help"
5698              (list 'nndoc-address file)
5699              (list 'nndoc-article-type 'mbox)))))
5700   (gnus-group-position-point))
5701
5702 (defun gnus-group-make-doc-group (file type)
5703   "Create a group that uses a single file as the source."
5704   (interactive
5705    (list (read-file-name "File name: ")
5706          (and current-prefix-arg 'ask)))
5707   (when (eq type 'ask)
5708     (let ((err "")
5709           char found)
5710       (while (not found)
5711         (message
5712          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5713          err)
5714         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5715                           ((= char ?b) 'babyl)
5716                           ((= char ?d) 'digest)
5717                           ((= char ?f) 'forward)
5718                           ((= char ?a) 'mmfd)
5719                           (t (setq err (format "%c unknown. " char))
5720                              nil))))
5721       (setq type found)))
5722   (let* ((file (expand-file-name file))
5723          (name (gnus-generate-new-group-name
5724                 (gnus-group-prefixed-name
5725                  (file-name-nondirectory file) '(nndoc "")))))
5726     (gnus-group-make-group
5727      (gnus-group-real-name name)
5728      (list 'nndoc (file-name-nondirectory file)
5729            (list 'nndoc-address file)
5730            (list 'nndoc-article-type (or type 'guess))))))
5731
5732 (defun gnus-group-make-archive-group (&optional all)
5733   "Create the (ding) Gnus archive group of the most recent articles.
5734 Given a prefix, create a full group."
5735   (interactive "P")
5736   (let ((group (gnus-group-prefixed-name
5737                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5738     (and (gnus-gethash group gnus-newsrc-hashtb)
5739          (error "Archive group already exists"))
5740     (gnus-group-make-group
5741      (gnus-group-real-name group)
5742      (list 'nndir (if all "hpc" "edu")
5743            (list 'nndir-directory
5744                  (if all gnus-group-archive-directory
5745                    gnus-group-recent-archive-directory))))))
5746
5747 (defun gnus-group-make-directory-group (dir)
5748   "Create an nndir group.
5749 The user will be prompted for a directory.  The contents of this
5750 directory will be used as a newsgroup.  The directory should contain
5751 mail messages or news articles in files that have numeric names."
5752   (interactive
5753    (list (read-file-name "Create group from directory: ")))
5754   (or (file-exists-p dir) (error "No such directory"))
5755   (or (file-directory-p dir) (error "Not a directory"))
5756   (let ((ext "")
5757         (i 0)
5758         group)
5759     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5760       (setq group
5761             (gnus-group-prefixed-name
5762              (concat (file-name-as-directory (directory-file-name dir))
5763                      ext)
5764              '(nndir "")))
5765       (setq ext (format "<%d>" (setq i (1+ i)))))
5766     (gnus-group-make-group
5767      (gnus-group-real-name group)
5768      (list 'nndir group (list 'nndir-directory dir)))))
5769
5770 (defun gnus-group-make-kiboze-group (group address scores)
5771   "Create an nnkiboze group.
5772 The user will be prompted for a name, a regexp to match groups, and
5773 score file entries for articles to include in the group."
5774   (interactive
5775    (list
5776     (read-string "nnkiboze group name: ")
5777     (read-string "Source groups (regexp): ")
5778     (let ((headers (mapcar (lambda (group) (list group))
5779                            '("subject" "from" "number" "date" "message-id"
5780                              "references" "chars" "lines" "xref"
5781                              "followup" "all" "body" "head")))
5782           scores header regexp regexps)
5783       (while (not (equal "" (setq header (completing-read
5784                                           "Match on header: " headers nil t))))
5785         (setq regexps nil)
5786         (while (not (equal "" (setq regexp (read-string
5787                                             (format "Match on %s (string): "
5788                                                     header)))))
5789           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5790         (setq scores (cons (cons header regexps) scores)))
5791       scores)))
5792   (gnus-group-make-group group "nnkiboze" address)
5793   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5794     (let (emacs-lisp-mode-hook)
5795       (pp scores (current-buffer)))))
5796
5797 (defun gnus-group-add-to-virtual (n vgroup)
5798   "Add the current group to a virtual group."
5799   (interactive
5800    (list current-prefix-arg
5801          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5802                           "nnvirtual:")))
5803   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5804       (error "%s is not an nnvirtual group" vgroup))
5805   (let* ((groups (gnus-group-process-prefix n))
5806          (method (gnus-info-method (gnus-get-info vgroup))))
5807     (setcar (cdr method)
5808             (concat
5809              (nth 1 method) "\\|"
5810              (mapconcat
5811               (lambda (s)
5812                 (gnus-group-remove-mark s)
5813                 (concat "\\(^" (regexp-quote s) "$\\)"))
5814               groups "\\|"))))
5815   (gnus-group-position-point))
5816
5817 (defun gnus-group-make-empty-virtual (group)
5818   "Create a new, fresh, empty virtual group."
5819   (interactive "sCreate new, empty virtual group: ")
5820   (let* ((method (list 'nnvirtual "^$"))
5821          (pgroup (gnus-group-prefixed-name group method)))
5822     ;; Check whether it exists already.
5823     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5824          (error "Group %s already exists." pgroup))
5825     ;; Subscribe the new group after the group on the current line.
5826     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5827     (gnus-group-update-group pgroup)
5828     (forward-line -1)
5829     (gnus-group-position-point)))
5830
5831 (defun gnus-group-enter-directory (dir)
5832   "Enter an ephemeral nneething group."
5833   (interactive "DDirectory to read: ")
5834   (let* ((method (list 'nneething dir))
5835          (leaf (gnus-group-prefixed-name
5836                 (file-name-nondirectory (directory-file-name dir))
5837                 method))
5838          (name (gnus-generate-new-group-name leaf)))
5839     (let ((nneething-read-only t))
5840       (or (gnus-group-read-ephemeral-group
5841            name method t
5842            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5843                                       'summary 'group)))
5844           (error "Couldn't enter %s" dir)))))
5845
5846 ;; Group sorting commands
5847 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5848
5849 (defun gnus-group-sort-groups (func &optional reverse)
5850   "Sort the group buffer according to FUNC.
5851 If REVERSE, reverse the sorting order."
5852   (interactive (list gnus-group-sort-function
5853                      current-prefix-arg))
5854   (let ((func (cond 
5855                ((not (listp func)) func)
5856                ((null func) func)
5857                ((= 1 (length func)) (car func))
5858                (t `(lambda (t1 t2)
5859                      ,(gnus-make-sort-function 
5860                        (reverse func)))))))
5861     ;; We peel off the dummy group from the alist.
5862     (when func
5863       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5864         (pop gnus-newsrc-alist))
5865       ;; Do the sorting.
5866       (setq gnus-newsrc-alist
5867             (sort gnus-newsrc-alist func))
5868       (when reverse
5869         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5870       ;; Regenerate the hash table.
5871       (gnus-make-hashtable-from-newsrc-alist)
5872       (gnus-group-list-groups))))
5873
5874 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5875   "Sort the group buffer alphabetically by group name.
5876 If REVERSE, sort in reverse order."
5877   (interactive "P")
5878   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5879
5880 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5881   "Sort the group buffer by number of unread articles.
5882 If REVERSE, sort in reverse order."
5883   (interactive "P")
5884   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5885
5886 (defun gnus-group-sort-groups-by-level (&optional reverse)
5887   "Sort the group buffer by group level.
5888 If REVERSE, sort in reverse order."
5889   (interactive "P")
5890   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5891
5892 (defun gnus-group-sort-groups-by-score (&optional reverse)
5893   "Sort the group buffer by group score.
5894 If REVERSE, sort in reverse order."
5895   (interactive "P")
5896   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5897
5898 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5899   "Sort the group buffer by group rank.
5900 If REVERSE, sort in reverse order."
5901   (interactive "P")
5902   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5903
5904 (defun gnus-group-sort-groups-by-method (&optional reverse)
5905   "Sort the group buffer alphabetically by backend name.
5906 If REVERSE, sort in reverse order."
5907   (interactive "P")
5908   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5909
5910 (defun gnus-group-sort-by-alphabet (info1 info2)
5911   "Sort alphabetically."
5912   (string< (gnus-info-group info1) (gnus-info-group info2)))
5913
5914 (defun gnus-group-sort-by-unread (info1 info2)
5915   "Sort by number of unread articles."
5916   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5917         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5918     (< (or (and (numberp n1) n1) 0)
5919        (or (and (numberp n2) n2) 0))))
5920
5921 (defun gnus-group-sort-by-level (info1 info2)
5922   "Sort by level."
5923   (< (gnus-info-level info1) (gnus-info-level info2)))
5924
5925 (defun gnus-group-sort-by-method (info1 info2)
5926   "Sort alphabetically by backend name."
5927   (string< (symbol-name (car (gnus-find-method-for-group
5928                               (gnus-info-group info1) info1)))
5929            (symbol-name (car (gnus-find-method-for-group
5930                               (gnus-info-group info2) info2)))))
5931
5932 (defun gnus-group-sort-by-score (info1 info2)
5933   "Sort by group score."
5934   (< (gnus-info-score info1) (gnus-info-score info2)))
5935
5936 (defun gnus-group-sort-by-rank (info1 info2)
5937   "Sort by level and score."
5938   (let ((level1 (gnus-info-level info1))
5939         (level2 (gnus-info-level info2)))
5940     (or (< level1 level2)
5941         (and (= level1 level2)
5942              (> (gnus-info-score info1) (gnus-info-score info2))))))
5943
5944 ;; Group catching up.
5945
5946 (defun gnus-group-clear-data (n)
5947   "Clear all marks and read ranges from the current group."
5948   (interactive "P")
5949   (let ((groups (gnus-group-process-prefix n))
5950         group info)
5951     (while (setq group (pop groups))
5952       (setq info (gnus-get-info group))
5953       (gnus-info-set-read info nil)
5954       (when (gnus-info-marks info)
5955         (gnus-info-set-marks info nil))
5956       (gnus-get-unread-articles-in-group info (gnus-active group) t)
5957       (when (gnus-group-goto-group group)
5958         (gnus-group-remove-mark group)
5959         (gnus-group-update-group-line)))))
5960
5961 (defun gnus-group-catchup-current (&optional n all)
5962   "Mark all articles not marked as unread in current newsgroup as read.
5963 If prefix argument N is numeric, the ARG next newsgroups will be
5964 caught up.  If ALL is non-nil, marked articles will also be marked as
5965 read.  Cross references (Xref: header) of articles are ignored.
5966 The difference between N and actual number of newsgroups that were
5967 caught up is returned."
5968   (interactive "P")
5969   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5970                gnus-expert-user
5971                (gnus-y-or-n-p
5972                 (if all
5973                     "Do you really want to mark all articles as read? "
5974                   "Mark all unread articles as read? "))))
5975       n
5976     (let ((groups (gnus-group-process-prefix n))
5977           (ret 0))
5978       (while groups
5979         ;; Virtual groups have to be given special treatment.
5980         (let ((method (gnus-find-method-for-group (car groups))))
5981           (if (eq 'nnvirtual (car method))
5982               (nnvirtual-catchup-group
5983                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5984         (gnus-group-remove-mark (car groups))
5985         (if (>= (gnus-group-group-level) gnus-level-zombie)
5986             (gnus-message 2 "Dead groups can't be caught up")
5987           (if (prog1
5988                   (gnus-group-goto-group (car groups))
5989                 (gnus-group-catchup (car groups) all))
5990               (gnus-group-update-group-line)
5991             (setq ret (1+ ret))))
5992         (setq groups (cdr groups)))
5993       (gnus-group-next-unread-group 1)
5994       ret)))
5995
5996 (defun gnus-group-catchup-current-all (&optional n)
5997   "Mark all articles in current newsgroup as read.
5998 Cross references (Xref: header) of articles are ignored."
5999   (interactive "P")
6000   (gnus-group-catchup-current n 'all))
6001
6002 (defun gnus-group-catchup (group &optional all)
6003   "Mark all articles in GROUP as read.
6004 If ALL is non-nil, all articles are marked as read.
6005 The return value is the number of articles that were marked as read,
6006 or nil if no action could be taken."
6007   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6008          (num (car entry)))
6009     ;; Do the updating only if the newsgroup isn't killed.
6010     (if (not (numberp (car entry)))
6011         (gnus-message 1 "Can't catch up; non-active group")
6012       ;; Do auto-expirable marks if that's required.
6013       (when (gnus-group-auto-expirable-p group)
6014         (gnus-add-marked-articles
6015          group 'expire (gnus-list-of-unread-articles group))
6016         (when all
6017           (let ((marks (nth 3 (nth 2 entry))))
6018             (gnus-add-marked-articles
6019              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6020             (gnus-add-marked-articles
6021              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6022       (when entry
6023         (gnus-update-read-articles group nil)
6024         ;; Also nix out the lists of marks and dormants.
6025         (when all
6026           (gnus-add-marked-articles group 'tick nil nil 'force)
6027           (gnus-add-marked-articles group 'dormant nil nil 'force))
6028         (run-hooks 'gnus-group-catchup-group-hook)
6029         num))))
6030
6031 (defun gnus-group-expire-articles (&optional n)
6032   "Expire all expirable articles in the current newsgroup."
6033   (interactive "P")
6034   (let ((groups (gnus-group-process-prefix n))
6035         group)
6036     (unless groups
6037       (error "No groups to expire"))
6038     (while (setq group (pop groups))
6039       (gnus-group-remove-mark group)
6040       (when (gnus-check-backend-function 'request-expire-articles group)
6041         (gnus-message 6 "Expiring articles in %s..." group)
6042         (let* ((info (gnus-get-info group))
6043                (expirable (if (gnus-group-total-expirable-p group)
6044                               (cons nil (gnus-list-of-read-articles group))
6045                             (assq 'expire (gnus-info-marks info))))
6046                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6047           (when expirable
6048             (setcdr
6049              expirable
6050              (gnus-compress-sequence
6051               (if expiry-wait
6052                   ;; We set the expiry variables to the groupp
6053                   ;; parameter. 
6054                   (let ((nnmail-expiry-wait-function nil)
6055                         (nnmail-expiry-wait expiry-wait))
6056                     (gnus-request-expire-articles
6057                      (gnus-uncompress-sequence (cdr expirable)) group))
6058                 ;; Just expire using the normal expiry values.
6059                 (gnus-request-expire-articles
6060                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6061           (gnus-message 6 "Expiring articles in %s...done" group)))
6062       (gnus-group-position-point))))
6063
6064 (defun gnus-group-expire-all-groups ()
6065   "Expire all expirable articles in all newsgroups."
6066   (interactive)
6067   (save-excursion
6068     (gnus-message 5 "Expiring...")
6069     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6070                                      (cdr gnus-newsrc-alist))))
6071       (gnus-group-expire-articles nil)))
6072   (gnus-group-position-point)
6073   (gnus-message 5 "Expiring...done"))
6074
6075 (defun gnus-group-set-current-level (n level)
6076   "Set the level of the next N groups to LEVEL."
6077   (interactive
6078    (list
6079     current-prefix-arg
6080     (string-to-int
6081      (let ((s (read-string
6082                (format "Level (default %s): "
6083                        (or (gnus-group-group-level) 
6084                            gnus-level-default-subscribed)))))
6085        (if (string-match "^\\s-*$" s)
6086            (int-to-string (or (gnus-group-group-level) 
6087                               gnus-level-default-subscribed))
6088          s)))))
6089   (or (and (>= level 1) (<= level gnus-level-killed))
6090       (error "Illegal level: %d" level))
6091   (let ((groups (gnus-group-process-prefix n))
6092         group)
6093     (while (setq group (pop groups))
6094       (gnus-group-remove-mark group)
6095       (gnus-message 6 "Changed level of %s from %d to %d"
6096                     group (or (gnus-group-group-level) gnus-level-killed)
6097                     level)
6098       (gnus-group-change-level
6099        group level (or (gnus-group-group-level) gnus-level-killed))
6100       (gnus-group-update-group-line)))
6101   (gnus-group-position-point))
6102
6103 (defun gnus-group-unsubscribe-current-group (&optional n)
6104   "Toggle subscription of the current group.
6105 If given numerical prefix, toggle the N next groups."
6106   (interactive "P")
6107   (let ((groups (gnus-group-process-prefix n))
6108         group)
6109     (while groups
6110       (setq group (car groups)
6111             groups (cdr groups))
6112       (gnus-group-remove-mark group)
6113       (gnus-group-unsubscribe-group
6114        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6115                  gnus-level-default-unsubscribed
6116                gnus-level-default-subscribed) t)
6117       (gnus-group-update-group-line))
6118     (gnus-group-next-group 1)))
6119
6120 (defun gnus-group-unsubscribe-group (group &optional level silent)
6121   "Toggle subscription to GROUP.
6122 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6123 group line."
6124   (interactive
6125    (list (completing-read
6126           "Group: " gnus-active-hashtb nil
6127           (gnus-read-active-file-p)
6128           nil 
6129           'gnus-group-history)))
6130   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6131     (cond
6132      ((string-match "^[ \t]$" group)
6133       (error "Empty group name"))
6134      (newsrc
6135       ;; Toggle subscription flag.
6136       (gnus-group-change-level
6137        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6138                                       gnus-level-subscribed)
6139                                   (1+ gnus-level-subscribed)
6140                                 gnus-level-default-subscribed)))
6141       (unless silent
6142         (gnus-group-update-group group)))
6143      ((and (stringp group)
6144            (or (not (gnus-read-active-file-p))
6145                (gnus-active group)))
6146       ;; Add new newsgroup.
6147       (gnus-group-change-level
6148        group
6149        (if level level gnus-level-default-subscribed)
6150        (or (and (member group gnus-zombie-list)
6151                 gnus-level-zombie)
6152            gnus-level-killed)
6153        (and (gnus-group-group-name)
6154             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6155       (unless silent
6156         (gnus-group-update-group group)))
6157      (t (error "No such newsgroup: %s" group)))
6158     (gnus-group-position-point)))
6159
6160 (defun gnus-group-transpose-groups (n)
6161   "Move the current newsgroup up N places.
6162 If given a negative prefix, move down instead.  The difference between
6163 N and the number of steps taken is returned."
6164   (interactive "p")
6165   (or (gnus-group-group-name)
6166       (error "No group on current line"))
6167   (gnus-group-kill-group 1)
6168   (prog1
6169       (forward-line (- n))
6170     (gnus-group-yank-group)
6171     (gnus-group-position-point)))
6172
6173 (defun gnus-group-kill-all-zombies ()
6174   "Kill all zombie newsgroups."
6175   (interactive)
6176   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6177   (setq gnus-zombie-list nil)
6178   (gnus-group-list-groups))
6179
6180 (defun gnus-group-kill-region (begin end)
6181   "Kill newsgroups in current region (excluding current point).
6182 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6183   (interactive "r")
6184   (let ((lines
6185          ;; Count lines.
6186          (save-excursion
6187            (count-lines
6188             (progn
6189               (goto-char begin)
6190               (beginning-of-line)
6191               (point))
6192             (progn
6193               (goto-char end)
6194               (beginning-of-line)
6195               (point))))))
6196     (goto-char begin)
6197     (beginning-of-line)                 ;Important when LINES < 1
6198     (gnus-group-kill-group lines)))
6199
6200 (defun gnus-group-kill-group (&optional n discard)
6201   "Kill the next N groups.
6202 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6203 However, only groups that were alive can be yanked; already killed
6204 groups or zombie groups can't be yanked.
6205 The return value is the name of the group that was killed, or a list
6206 of groups killed."
6207   (interactive "P")
6208   (let ((buffer-read-only nil)
6209         (groups (gnus-group-process-prefix n))
6210         group entry level out)
6211     (if (< (length groups) 10)
6212         ;; This is faster when there are few groups.
6213         (while groups
6214           (push (setq group (pop groups)) out)
6215           (gnus-group-remove-mark group)
6216           (setq level (gnus-group-group-level))
6217           (gnus-delete-line)
6218           (when (and (not discard)
6219                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6220             (push (cons (car entry) (nth 2 entry))
6221                   gnus-list-of-killed-groups))
6222           (gnus-group-change-level
6223            (if entry entry group) gnus-level-killed (if entry nil level)))
6224       ;; If there are lots and lots of groups to be killed, we use
6225       ;; this thing instead.
6226       (let (entry)
6227         (setq groups (nreverse groups))
6228         (while groups
6229           (gnus-group-remove-mark (setq group (pop groups)))
6230           (gnus-delete-line)
6231           (cond
6232            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6233             (push (cons (car entry) (nth 2 entry))
6234                   gnus-list-of-killed-groups)
6235             (setcdr (cdr entry) (cdddr entry)))
6236            ((member group gnus-zombie-list)
6237             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6238         (gnus-make-hashtable-from-newsrc-alist)))
6239
6240     (gnus-group-position-point)
6241     (if (< (length out) 2) (car out) (nreverse out))))
6242
6243 (defun gnus-group-yank-group (&optional arg)
6244   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6245 inserting it before the current newsgroup.  The numeric ARG specifies
6246 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6247 is returned, or (if several groups are yanked) a list of yanked groups
6248 is returned."
6249   (interactive "p")
6250   (setq arg (or arg 1))
6251   (let (info group prev out)
6252     (while (>= (decf arg) 0)
6253       (if (not (setq info (pop gnus-list-of-killed-groups)))
6254           (error "No more newsgroups to yank"))
6255       (push (setq group (nth 1 info)) out)
6256       ;; Find which newsgroup to insert this one before - search
6257       ;; backward until something suitable is found.  If there are no
6258       ;; other newsgroups in this buffer, just make this newsgroup the
6259       ;; first newsgroup.
6260       (setq prev (gnus-group-group-name))
6261       (gnus-group-change-level
6262        info (gnus-info-level (cdr info)) gnus-level-killed
6263        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6264        t)
6265       (gnus-group-insert-group-line-info group))
6266     (forward-line -1)
6267     (gnus-group-position-point)
6268     (if (< (length out) 2) (car out) (nreverse out))))
6269
6270 (defun gnus-group-kill-level (level)
6271   "Kill all groups that is on a certain LEVEL."
6272   (interactive "nKill all groups on level: ")
6273   (cond
6274    ((= level gnus-level-zombie)
6275     (setq gnus-killed-list
6276           (nconc gnus-zombie-list gnus-killed-list))
6277     (setq gnus-zombie-list nil))
6278    ((and (< level gnus-level-zombie)
6279          (> level 0)
6280          (or gnus-expert-user
6281              (gnus-yes-or-no-p
6282               (format
6283                "Do you really want to kill all groups on level %d? "
6284                level))))
6285     (let* ((prev gnus-newsrc-alist)
6286            (alist (cdr prev)))
6287       (while alist
6288         (if (= (gnus-info-level level) level)
6289             (setcdr prev (cdr alist))
6290           (setq prev alist))
6291         (setq alist (cdr alist)))
6292       (gnus-make-hashtable-from-newsrc-alist)
6293       (gnus-group-list-groups)))
6294    (t
6295     (error "Can't kill; illegal level: %d" level))))
6296
6297 (defun gnus-group-list-all-groups (&optional arg)
6298   "List all newsgroups with level ARG or lower.
6299 Default is gnus-level-unsubscribed, which lists all subscribed and most
6300 unsubscribed groups."
6301   (interactive "P")
6302   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6303
6304 ;; Redefine this to list ALL killed groups if prefix arg used.
6305 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6306 (defun gnus-group-list-killed (&optional arg)
6307   "List all killed newsgroups in the group buffer.
6308 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6309 entail asking the server for the groups."
6310   (interactive "P")
6311   ;; Find all possible killed newsgroups if arg.
6312   (when arg
6313     (gnus-get-killed-groups))
6314   (if (not gnus-killed-list)
6315       (gnus-message 6 "No killed groups")
6316     (let (gnus-group-list-mode)
6317       (funcall gnus-group-prepare-function
6318                gnus-level-killed t gnus-level-killed))
6319     (goto-char (point-min)))
6320   (gnus-group-position-point))
6321
6322 (defun gnus-group-list-zombies ()
6323   "List all zombie newsgroups in the group buffer."
6324   (interactive)
6325   (if (not gnus-zombie-list)
6326       (gnus-message 6 "No zombie groups")
6327     (let (gnus-group-list-mode)
6328       (funcall gnus-group-prepare-function
6329                gnus-level-zombie t gnus-level-zombie))
6330     (goto-char (point-min)))
6331   (gnus-group-position-point))
6332
6333 (defun gnus-group-list-active ()
6334   "List all groups that are available from the server(s)."
6335   (interactive)
6336   ;; First we make sure that we have really read the active file.
6337   (unless (gnus-read-active-file-p)
6338     (let ((gnus-read-active-file t))
6339       (gnus-read-active-file)))
6340   ;; Find all groups and sort them.
6341   (let ((groups
6342          (sort
6343           (let (list)
6344             (mapatoms
6345              (lambda (sym)
6346                (and (symbol-value sym)
6347                     (setq list (cons (symbol-name sym) list))))
6348              gnus-active-hashtb)
6349             list)
6350           'string<))
6351         (buffer-read-only nil))
6352     (erase-buffer)
6353     (while groups
6354       (gnus-group-insert-group-line-info (pop groups)))
6355     (goto-char (point-min))))
6356
6357 (defun gnus-activate-all-groups (level)
6358   "Activate absolutely all groups."
6359   (interactive (list 7))
6360   (let ((gnus-activate-level level)
6361         (gnus-activate-foreign-newsgroups level))
6362     (gnus-group-get-new-news)))
6363
6364 (defun gnus-group-get-new-news (&optional arg)
6365   "Get newly arrived articles.
6366 If ARG is a number, it specifies which levels you are interested in
6367 re-scanning.  If ARG is non-nil and not a number, this will force
6368 \"hard\" re-reading of the active files from all servers."
6369   (interactive "P")
6370   (run-hooks 'gnus-get-new-news-hook)
6371   ;; We might read in new NoCeM messages here.
6372   (when (and gnus-use-nocem 
6373              (null arg))
6374     (gnus-nocem-scan-groups))
6375   ;; If ARG is not a number, then we read the active file.
6376   (when (and arg (not (numberp arg)))
6377     (let ((gnus-read-active-file t))
6378       (gnus-read-active-file))
6379     (setq arg nil))
6380
6381   (setq arg (gnus-group-default-level arg t))
6382   (if (and gnus-read-active-file (not arg))
6383       (progn
6384         (gnus-read-active-file)
6385         (gnus-get-unread-articles arg))
6386     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6387       (gnus-get-unread-articles arg)))
6388   (run-hooks 'gnus-after-getting-new-news-hook)
6389   (gnus-group-list-groups))
6390
6391 (defun gnus-group-get-new-news-this-group (&optional n)
6392   "Check for newly arrived news in the current group (and the N-1 next groups).
6393 The difference between N and the number of newsgroup checked is returned.
6394 If N is negative, this group and the N-1 previous groups will be checked."
6395   (interactive "P")
6396   (let* ((groups (gnus-group-process-prefix n))
6397          (ret (if (numberp n) (- n (length groups)) 0))
6398          (beg (unless n (point)))
6399          group)
6400     (while (setq group (pop groups))
6401       (gnus-group-remove-mark group)
6402       (if (gnus-activate-group group 'scan)
6403           (progn
6404             (gnus-get-unread-articles-in-group
6405              (gnus-get-info group) (gnus-active group) t)
6406             (unless (gnus-virtual-group-p group)
6407               (gnus-close-group group))
6408             (gnus-group-update-group group))
6409         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6410     (when beg (goto-char beg))
6411     (when gnus-goto-next-group-when-activating
6412       (gnus-group-next-unread-group 1 t))
6413     (gnus-summary-position-point)
6414     ret))
6415
6416 (defun gnus-group-fetch-faq (group &optional faq-dir)
6417   "Fetch the FAQ for the current group."
6418   (interactive
6419    (list
6420     (gnus-group-real-name (gnus-group-group-name))
6421     (cond (current-prefix-arg
6422            (completing-read
6423             "Faq dir: " (and (listp gnus-group-faq-directory)
6424                              gnus-group-faq-directory))))))
6425   (or faq-dir
6426       (setq faq-dir (if (listp gnus-group-faq-directory)
6427                         (car gnus-group-faq-directory)
6428                       gnus-group-faq-directory)))
6429   (or group (error "No group name given"))
6430   (let ((file (concat (file-name-as-directory faq-dir)
6431                       (gnus-group-real-name group))))
6432     (if (not (file-exists-p file))
6433         (error "No such file: %s" file)
6434       (find-file file))))
6435
6436 (defun gnus-group-describe-group (force &optional group)
6437   "Display a description of the current newsgroup."
6438   (interactive (list current-prefix-arg (gnus-group-group-name)))
6439   (and force (setq gnus-description-hashtb nil))
6440   (let ((method (gnus-find-method-for-group group))
6441         desc)
6442     (or group (error "No group name given"))
6443     (and (or (and gnus-description-hashtb
6444                   ;; We check whether this group's method has been
6445                   ;; queried for a description file.
6446                   (gnus-gethash
6447                    (gnus-group-prefixed-name "" method)
6448                    gnus-description-hashtb))
6449              (setq desc (gnus-group-get-description group))
6450              (gnus-read-descriptions-file method))
6451          (gnus-message 1
6452           (or desc (gnus-gethash group gnus-description-hashtb)
6453               "No description available")))))
6454
6455 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6456 (defun gnus-group-describe-all-groups (&optional force)
6457   "Pop up a buffer with descriptions of all newsgroups."
6458   (interactive "P")
6459   (and force (setq gnus-description-hashtb nil))
6460   (if (not (or gnus-description-hashtb
6461                (gnus-read-all-descriptions-files)))
6462       (error "Couldn't request descriptions file"))
6463   (let ((buffer-read-only nil)
6464         b)
6465     (erase-buffer)
6466     (mapatoms
6467      (lambda (group)
6468        (setq b (point))
6469        (insert (format "      *: %-20s %s\n" (symbol-name group)
6470                        (symbol-value group)))
6471        (gnus-add-text-properties
6472         b (1+ b) (list 'gnus-group group
6473                        'gnus-unread t 'gnus-marked nil
6474                        'gnus-level (1+ gnus-level-subscribed))))
6475      gnus-description-hashtb)
6476     (goto-char (point-min))
6477     (gnus-group-position-point)))
6478
6479 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6480 (defun gnus-group-apropos (regexp &optional search-description)
6481   "List all newsgroups that have names that match a regexp."
6482   (interactive "sGnus apropos (regexp): ")
6483   (let ((prev "")
6484         (obuf (current-buffer))
6485         groups des)
6486     ;; Go through all newsgroups that are known to Gnus.
6487     (mapatoms
6488      (lambda (group)
6489        (and (symbol-name group)
6490             (string-match regexp (symbol-name group))
6491             (setq groups (cons (symbol-name group) groups))))
6492      gnus-active-hashtb)
6493     ;; Also go through all descriptions that are known to Gnus.
6494     (when search-description
6495       (mapatoms
6496        (lambda (group)
6497          (and (string-match regexp (symbol-value group))
6498               (gnus-active (symbol-name group))
6499               (setq groups (cons (symbol-name group) groups))))
6500        gnus-description-hashtb))
6501     (if (not groups)
6502         (gnus-message 3 "No groups matched \"%s\"." regexp)
6503       ;; Print out all the groups.
6504       (save-excursion
6505         (pop-to-buffer "*Gnus Help*")
6506         (buffer-disable-undo (current-buffer))
6507         (erase-buffer)
6508         (setq groups (sort groups 'string<))
6509         (while groups
6510           ;; Groups may be entered twice into the list of groups.
6511           (if (not (string= (car groups) prev))
6512               (progn
6513                 (insert (setq prev (car groups)) "\n")
6514                 (if (and gnus-description-hashtb
6515                          (setq des (gnus-gethash (car groups)
6516                                                  gnus-description-hashtb)))
6517                     (insert "  " des "\n"))))
6518           (setq groups (cdr groups)))
6519         (goto-char (point-min))))
6520     (pop-to-buffer obuf)))
6521
6522 (defun gnus-group-description-apropos (regexp)
6523   "List all newsgroups that have names or descriptions that match a regexp."
6524   (interactive "sGnus description apropos (regexp): ")
6525   (if (not (or gnus-description-hashtb
6526                (gnus-read-all-descriptions-files)))
6527       (error "Couldn't request descriptions file"))
6528   (gnus-group-apropos regexp t))
6529
6530 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6531 (defun gnus-group-list-matching (level regexp &optional all lowest)
6532   "List all groups with unread articles that match REGEXP.
6533 If the prefix LEVEL is non-nil, it should be a number that says which
6534 level to cut off listing groups.
6535 If ALL, also list groups with no unread articles.
6536 If LOWEST, don't list groups with level lower than LOWEST.
6537
6538 This command may read the active file."
6539   (interactive "P\nsList newsgroups matching: ")
6540   ;; First make sure active file has been read.
6541   (when (and level
6542              (>= (prefix-numeric-value level) gnus-level-killed))
6543     (gnus-get-killed-groups))
6544   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6545                            all (or lowest 1) regexp)
6546   (goto-char (point-min))
6547   (gnus-group-position-point))
6548
6549 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6550   "List all groups that match REGEXP.
6551 If the prefix LEVEL is non-nil, it should be a number that says which
6552 level to cut off listing groups.
6553 If LOWEST, don't list groups with level lower than LOWEST."
6554   (interactive "P\nsList newsgroups matching: ")
6555   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6556
6557 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6558 (defun gnus-group-save-newsrc (&optional force)
6559   "Save the Gnus startup files.
6560 If FORCE, force saving whether it is necessary or not."
6561   (interactive "P")
6562   (gnus-save-newsrc-file force))
6563
6564 (defun gnus-group-restart (&optional arg)
6565   "Force Gnus to read the .newsrc file."
6566   (interactive "P")
6567   (when (gnus-yes-or-no-p
6568          (format "Are you sure you want to read %s? "
6569                  gnus-current-startup-file))
6570     (gnus-save-newsrc-file)
6571     (gnus-setup-news 'force)
6572     (gnus-group-list-groups arg)))
6573
6574 (defun gnus-group-read-init-file ()
6575   "Read the Gnus elisp init file."
6576   (interactive)
6577   (gnus-read-init-file))
6578
6579 (defun gnus-group-check-bogus-groups (&optional silent)
6580   "Check bogus newsgroups.
6581 If given a prefix, don't ask for confirmation before removing a bogus
6582 group."
6583   (interactive "P")
6584   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6585   (gnus-group-list-groups))
6586
6587 (defun gnus-group-edit-global-kill (&optional article group)
6588   "Edit the global kill file.
6589 If GROUP, edit that local kill file instead."
6590   (interactive "P")
6591   (setq gnus-current-kill-article article)
6592   (gnus-kill-file-edit-file group)
6593   (gnus-message
6594    6
6595    (substitute-command-keys
6596     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6597             (if group "local" "global")))))
6598
6599 (defun gnus-group-edit-local-kill (article group)
6600   "Edit a local kill file."
6601   (interactive (list nil (gnus-group-group-name)))
6602   (gnus-group-edit-global-kill article group))
6603
6604 (defun gnus-group-force-update ()
6605   "Update `.newsrc' file."
6606   (interactive)
6607   (gnus-save-newsrc-file))
6608
6609 (defun gnus-group-suspend ()
6610   "Suspend the current Gnus session.
6611 In fact, cleanup buffers except for group mode buffer.
6612 The hook gnus-suspend-gnus-hook is called before actually suspending."
6613   (interactive)
6614   (run-hooks 'gnus-suspend-gnus-hook)
6615   ;; Kill Gnus buffers except for group mode buffer.
6616   (let ((group-buf (get-buffer gnus-group-buffer)))
6617     ;; Do this on a separate list in case the user does a ^G before we finish
6618     (let ((gnus-buffer-list
6619            (delq group-buf (delq gnus-dribble-buffer
6620                                  (append gnus-buffer-list nil)))))
6621       (while gnus-buffer-list
6622         (gnus-kill-buffer (car gnus-buffer-list))
6623         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6624     (if group-buf
6625         (progn
6626           (setq gnus-buffer-list (list group-buf))
6627           (bury-buffer group-buf)
6628           (delete-windows-on group-buf t)))))
6629
6630 (defun gnus-group-clear-dribble ()
6631   "Clear all information from the dribble buffer."
6632   (interactive)
6633   (gnus-dribble-clear)
6634   (gnus-message 7 "Cleared dribble buffer"))
6635
6636 (defun gnus-group-exit ()
6637   "Quit reading news after updating .newsrc.eld and .newsrc.
6638 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6639   (interactive)
6640   (when 
6641       (or noninteractive                ;For gnus-batch-kill
6642           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6643           (not gnus-interactive-exit)   ;Without confirmation
6644           gnus-expert-user
6645           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6646     (run-hooks 'gnus-exit-gnus-hook)
6647     ;; Offer to save data from non-quitted summary buffers.
6648     (gnus-offer-save-summaries)
6649     ;; Save the newsrc file(s).
6650     (gnus-save-newsrc-file)
6651     ;; Kill-em-all.
6652     (gnus-close-backends)
6653     ;; Reset everything.
6654     (gnus-clear-system)
6655     ;; Allow the user to do things after cleaning up.
6656     (run-hooks 'gnus-after-exiting-gnus-hook)))
6657
6658 (defun gnus-close-backends ()
6659   ;; Send a close request to all backends that support such a request.
6660   (let ((methods gnus-valid-select-methods)
6661         func)
6662     (while methods
6663       (if (fboundp (setq func (intern (concat (caar methods)
6664                                               "-request-close"))))
6665           (funcall func))
6666       (setq methods (cdr methods)))))
6667
6668 (defun gnus-group-quit ()
6669   "Quit reading news without updating .newsrc.eld or .newsrc.
6670 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6671   (interactive)
6672   (when (or noninteractive              ;For gnus-batch-kill
6673             (zerop (buffer-size))
6674             (not (gnus-server-opened gnus-select-method))
6675             gnus-expert-user
6676             (not gnus-current-startup-file)
6677             (gnus-yes-or-no-p
6678              (format "Quit reading news without saving %s? "
6679                      (file-name-nondirectory gnus-current-startup-file))))
6680     (run-hooks 'gnus-exit-gnus-hook)
6681     (if gnus-use-full-window
6682         (delete-other-windows)
6683       (gnus-remove-some-windows))
6684     (gnus-dribble-save)
6685     (gnus-close-backends)
6686     (gnus-clear-system)
6687     ;; Allow the user to do things after cleaning up.
6688     (run-hooks 'gnus-after-exiting-gnus-hook)))
6689
6690 (defun gnus-offer-save-summaries ()
6691   "Offer to save all active summary buffers."
6692   (save-excursion
6693     (let ((buflist (buffer-list))
6694           buffers bufname)
6695       ;; Go through all buffers and find all summaries.
6696       (while buflist
6697         (and (setq bufname (buffer-name (car buflist)))
6698              (string-match "Summary" bufname)
6699              (save-excursion
6700                (set-buffer bufname)
6701                ;; We check that this is, indeed, a summary buffer.
6702                (and (eq major-mode 'gnus-summary-mode)
6703                     ;; Also make sure this isn't bogus.
6704                     gnus-newsgroup-prepared))
6705              (push bufname buffers))
6706         (setq buflist (cdr buflist)))
6707       ;; Go through all these summary buffers and offer to save them.
6708       (when buffers
6709         (map-y-or-n-p
6710          "Update summary buffer %s? "
6711          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6712          buffers)))))
6713
6714 (defun gnus-group-describe-briefly ()
6715   "Give a one line description of the group mode commands."
6716   (interactive)
6717   (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")))
6718
6719 (defun gnus-group-browse-foreign-server (method)
6720   "Browse a foreign news server.
6721 If called interactively, this function will ask for a select method
6722  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6723 If not, METHOD should be a list where the first element is the method
6724 and the second element is the address."
6725   (interactive
6726    (list (let ((how (completing-read
6727                      "Which backend: "
6728                      (append gnus-valid-select-methods gnus-server-alist)
6729                      nil t (cons "nntp" 0) 'gnus-method-history)))
6730            ;; We either got a backend name or a virtual server name.
6731            ;; If the first, we also need an address.
6732            (if (assoc how gnus-valid-select-methods)
6733                (list (intern how)
6734                      ;; Suggested by mapjph@bath.ac.uk.
6735                      (completing-read
6736                       "Address: "
6737                       (mapcar (lambda (server) (list server))
6738                               gnus-secondary-servers)))
6739              ;; We got a server name, so we find the method.
6740              (gnus-server-to-method how)))))
6741   (gnus-browse-foreign-server method))
6742
6743 \f
6744 ;;;
6745 ;;; Gnus summary mode
6746 ;;;
6747
6748 (defvar gnus-summary-mode-map nil)
6749
6750 (put 'gnus-summary-mode 'mode-class 'special)
6751
6752 (unless gnus-summary-mode-map
6753   (setq gnus-summary-mode-map (make-keymap))
6754   (suppress-keymap gnus-summary-mode-map)
6755
6756   ;; Non-orthogonal keys
6757
6758   (gnus-define-keys gnus-summary-mode-map
6759     " " gnus-summary-next-page
6760     "\177" gnus-summary-prev-page
6761     [delete] gnus-summary-prev-page
6762     "\r" gnus-summary-scroll-up
6763     "n" gnus-summary-next-unread-article
6764     "p" gnus-summary-prev-unread-article
6765     "N" gnus-summary-next-article
6766     "P" gnus-summary-prev-article
6767     "\M-\C-n" gnus-summary-next-same-subject
6768     "\M-\C-p" gnus-summary-prev-same-subject
6769     "\M-n" gnus-summary-next-unread-subject
6770     "\M-p" gnus-summary-prev-unread-subject
6771     "." gnus-summary-first-unread-article
6772     "," gnus-summary-best-unread-article
6773     "\M-s" gnus-summary-search-article-forward
6774     "\M-r" gnus-summary-search-article-backward
6775     "<" gnus-summary-beginning-of-article
6776     ">" gnus-summary-end-of-article
6777     "j" gnus-summary-goto-article
6778     "^" gnus-summary-refer-parent-article
6779     "\M-^" gnus-summary-refer-article
6780     "u" gnus-summary-tick-article-forward
6781     "!" gnus-summary-tick-article-forward
6782     "U" gnus-summary-tick-article-backward
6783     "d" gnus-summary-mark-as-read-forward
6784     "D" gnus-summary-mark-as-read-backward
6785     "E" gnus-summary-mark-as-expirable
6786     "\M-u" gnus-summary-clear-mark-forward
6787     "\M-U" gnus-summary-clear-mark-backward
6788     "k" gnus-summary-kill-same-subject-and-select
6789     "\C-k" gnus-summary-kill-same-subject
6790     "\M-\C-k" gnus-summary-kill-thread
6791     "\M-\C-l" gnus-summary-lower-thread
6792     "e" gnus-summary-edit-article
6793     "#" gnus-summary-mark-as-processable
6794     "\M-#" gnus-summary-unmark-as-processable
6795     "\M-\C-t" gnus-summary-toggle-threads
6796     "\M-\C-s" gnus-summary-show-thread
6797     "\M-\C-h" gnus-summary-hide-thread
6798     "\M-\C-f" gnus-summary-next-thread
6799     "\M-\C-b" gnus-summary-prev-thread
6800     "\M-\C-u" gnus-summary-up-thread
6801     "\M-\C-d" gnus-summary-down-thread
6802     "&" gnus-summary-execute-command
6803     "c" gnus-summary-catchup-and-exit
6804     "\C-w" gnus-summary-mark-region-as-read
6805     "\C-t" gnus-summary-toggle-truncation
6806     "?" gnus-summary-mark-as-dormant
6807     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6808     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6809     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6810     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6811     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6812     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6813     "=" gnus-summary-expand-window
6814     "\C-x\C-s" gnus-summary-reselect-current-group
6815     "\M-g" gnus-summary-rescan-group
6816     "w" gnus-summary-stop-page-breaking
6817     "\C-c\C-r" gnus-summary-caesar-message
6818     "\M-t" gnus-summary-toggle-mime
6819     "f" gnus-summary-followup
6820     "F" gnus-summary-followup-with-original
6821     "C" gnus-summary-cancel-article
6822     "r" gnus-summary-reply
6823     "R" gnus-summary-reply-with-original
6824     "\C-c\C-f" gnus-summary-mail-forward
6825     "o" gnus-summary-save-article
6826     "\C-o" gnus-summary-save-article-mail
6827     "|" gnus-summary-pipe-output
6828     "\M-k" gnus-summary-edit-local-kill
6829     "\M-K" gnus-summary-edit-global-kill
6830     "V" gnus-version
6831     "\C-c\C-d" gnus-summary-describe-group
6832     "q" gnus-summary-exit
6833     "Q" gnus-summary-exit-no-update
6834     "\C-c\C-i" gnus-info-find-node
6835     gnus-mouse-2 gnus-mouse-pick-article
6836     "m" gnus-summary-mail-other-window
6837     "a" gnus-summary-post-news
6838     "x" gnus-summary-limit-to-unread
6839     "s" gnus-summary-isearch-article
6840     "t" gnus-article-hide-headers
6841     "g" gnus-summary-show-article
6842     "l" gnus-summary-goto-last-article
6843     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6844     "\C-d" gnus-summary-enter-digest-group
6845     "\C-c\C-b" gnus-bug
6846     "*" gnus-cache-enter-article
6847     "\M-*" gnus-cache-remove-article
6848     "\M-&" gnus-summary-universal-argument
6849     "\C-l" gnus-recenter
6850     "I" gnus-summary-increase-score
6851     "L" gnus-summary-lower-score
6852
6853     "V" gnus-summary-score-map
6854     "X" gnus-uu-extract-map
6855     "S" gnus-summary-send-map)
6856
6857   ;; Sort of orthogonal keymap
6858   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6859     "t" gnus-summary-tick-article-forward
6860     "!" gnus-summary-tick-article-forward
6861     "d" gnus-summary-mark-as-read-forward
6862     "r" gnus-summary-mark-as-read-forward
6863     "c" gnus-summary-clear-mark-forward
6864     " " gnus-summary-clear-mark-forward
6865     "e" gnus-summary-mark-as-expirable
6866     "x" gnus-summary-mark-as-expirable
6867     "?" gnus-summary-mark-as-dormant
6868     "b" gnus-summary-set-bookmark
6869     "B" gnus-summary-remove-bookmark
6870     "#" gnus-summary-mark-as-processable
6871     "\M-#" gnus-summary-unmark-as-processable
6872     "S" gnus-summary-limit-include-expunged
6873     "C" gnus-summary-catchup
6874     "H" gnus-summary-catchup-to-here
6875     "\C-c" gnus-summary-catchup-all
6876     "k" gnus-summary-kill-same-subject-and-select
6877     "K" gnus-summary-kill-same-subject
6878     "P" gnus-uu-mark-map)
6879
6880   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6881     "c" gnus-summary-clear-above
6882     "u" gnus-summary-tick-above
6883     "m" gnus-summary-mark-above
6884     "k" gnus-summary-kill-below)
6885
6886   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6887     "/" gnus-summary-limit-to-subject
6888     "n" gnus-summary-limit-to-articles
6889     "w" gnus-summary-pop-limit
6890     "s" gnus-summary-limit-to-subject
6891     "a" gnus-summary-limit-to-author
6892     "u" gnus-summary-limit-to-unread
6893     "m" gnus-summary-limit-to-marks
6894     "v" gnus-summary-limit-to-score
6895     "D" gnus-summary-limit-include-dormant
6896     "d" gnus-summary-limit-exclude-dormant
6897     ;;  "t" gnus-summary-limit-exclude-thread
6898     "E" gnus-summary-limit-include-expunged
6899     "c" gnus-summary-limit-exclude-childless-dormant
6900     "C" gnus-summary-limit-mark-excluded-as-read)
6901
6902   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6903     "n" gnus-summary-next-unread-article
6904     "p" gnus-summary-prev-unread-article
6905     "N" gnus-summary-next-article
6906     "P" gnus-summary-prev-article
6907     "\C-n" gnus-summary-next-same-subject
6908     "\C-p" gnus-summary-prev-same-subject
6909     "\M-n" gnus-summary-next-unread-subject
6910     "\M-p" gnus-summary-prev-unread-subject
6911     "f" gnus-summary-first-unread-article
6912     "b" gnus-summary-best-unread-article
6913     "j" gnus-summary-goto-article
6914     "g" gnus-summary-goto-subject
6915     "l" gnus-summary-goto-last-article
6916     "p" gnus-summary-pop-article)
6917
6918   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6919     "k" gnus-summary-kill-thread
6920     "l" gnus-summary-lower-thread
6921     "i" gnus-summary-raise-thread
6922     "T" gnus-summary-toggle-threads
6923     "t" gnus-summary-rethread-current
6924     "^" gnus-summary-reparent-thread
6925     "s" gnus-summary-show-thread
6926     "S" gnus-summary-show-all-threads
6927     "h" gnus-summary-hide-thread
6928     "H" gnus-summary-hide-all-threads
6929     "n" gnus-summary-next-thread
6930     "p" gnus-summary-prev-thread
6931     "u" gnus-summary-up-thread
6932     "o" gnus-summary-top-thread
6933     "d" gnus-summary-down-thread
6934     "#" gnus-uu-mark-thread
6935     "\M-#" gnus-uu-unmark-thread)
6936
6937   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6938     "c" gnus-summary-catchup-and-exit
6939     "C" gnus-summary-catchup-all-and-exit
6940     "E" gnus-summary-exit-no-update
6941     "Q" gnus-summary-exit
6942     "Z" gnus-summary-exit
6943     "n" gnus-summary-catchup-and-goto-next-group
6944     "R" gnus-summary-reselect-current-group
6945     "G" gnus-summary-rescan-group
6946     "N" gnus-summary-next-group
6947     "P" gnus-summary-prev-group)
6948
6949   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6950     " " gnus-summary-next-page
6951     "n" gnus-summary-next-page
6952     "\177" gnus-summary-prev-page
6953     [delete] gnus-summary-prev-page
6954     "p" gnus-summary-prev-page
6955     "\r" gnus-summary-scroll-up
6956     "<" gnus-summary-beginning-of-article
6957     ">" gnus-summary-end-of-article
6958     "b" gnus-summary-beginning-of-article
6959     "e" gnus-summary-end-of-article
6960     "^" gnus-summary-refer-parent-article
6961     "r" gnus-summary-refer-parent-article
6962     "R" gnus-summary-refer-references
6963     "g" gnus-summary-show-article
6964     "s" gnus-summary-isearch-article)
6965
6966   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6967     "b" gnus-article-add-buttons
6968     "B" gnus-article-add-buttons-to-head
6969     "o" gnus-article-treat-overstrike
6970     ;;  "w" gnus-article-word-wrap
6971     "w" gnus-article-fill-cited-article
6972     "c" gnus-article-remove-cr
6973     "L" gnus-article-remove-trailing-blank-lines
6974     "q" gnus-article-de-quoted-unreadable
6975     "f" gnus-article-display-x-face
6976     "l" gnus-summary-stop-page-breaking
6977     "r" gnus-summary-caesar-message
6978     "t" gnus-article-hide-headers
6979     "v" gnus-summary-verbose-headers
6980     "m" gnus-summary-toggle-mime)
6981
6982   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6983     "a" gnus-article-hide
6984     "h" gnus-article-hide-headers
6985     "b" gnus-article-hide-boring-headers
6986     "s" gnus-article-hide-signature
6987     "c" gnus-article-hide-citation
6988     "p" gnus-article-hide-pgp
6989     "\C-c" gnus-article-hide-citation-maybe)
6990
6991   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6992     "a" gnus-article-highlight
6993     "h" gnus-article-highlight-headers
6994     "c" gnus-article-highlight-citation
6995     "s" gnus-article-highlight-signature)
6996
6997   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6998     "z" gnus-article-date-ut
6999     "u" gnus-article-date-ut
7000     "l" gnus-article-date-local
7001     "e" gnus-article-date-lapsed
7002     "o" gnus-article-date-original)
7003
7004   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7005     "v" gnus-version
7006     "f" gnus-summary-fetch-faq
7007     "d" gnus-summary-describe-group
7008     "h" gnus-summary-describe-briefly
7009     "i" gnus-info-find-node)
7010
7011   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7012     "e" gnus-summary-expire-articles
7013     "\M-\C-e" gnus-summary-expire-articles-now
7014     "\177" gnus-summary-delete-article
7015     [delete] gnus-summary-delete-article
7016     "m" gnus-summary-move-article
7017     "r" gnus-summary-respool-article
7018     "w" gnus-summary-edit-article
7019     "c" gnus-summary-copy-article
7020     "B" gnus-summary-crosspost-article
7021     "q" gnus-summary-respool-query
7022     "i" gnus-summary-import-article)
7023
7024   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7025     "o" gnus-summary-save-article
7026     "m" gnus-summary-save-article-mail
7027     "r" gnus-summary-save-article-rmail
7028     "f" gnus-summary-save-article-file
7029     "b" gnus-summary-save-article-body-file
7030     "h" gnus-summary-save-article-folder
7031     "v" gnus-summary-save-article-vm
7032     "p" gnus-summary-pipe-output
7033     "s" gnus-soup-add-article)
7034   )
7035
7036 \f
7037
7038 (defun gnus-summary-mode (&optional group)
7039   "Major mode for reading articles.
7040
7041 All normal editing commands are switched off.
7042 \\<gnus-summary-mode-map>
7043 Each line in this buffer represents one article.  To read an
7044 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7045 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7046 respectively.
7047
7048 You can also post articles and send mail from this buffer.  To
7049 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7050 of an article, type `\\[gnus-summary-reply]'.
7051
7052 There are approx. one gazillion commands you can execute in this
7053 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7054
7055 The following commands are available:
7056
7057 \\{gnus-summary-mode-map}"
7058   (interactive)
7059   (when (and menu-bar-mode
7060              (gnus-visual-p 'summary-menu 'menu))
7061     (gnus-summary-make-menu-bar))
7062   (kill-all-local-variables)
7063   (let ((locals gnus-summary-local-variables))
7064     (while locals
7065       (if (consp (car locals))
7066           (progn
7067             (make-local-variable (caar locals))
7068             (set (caar locals) (eval (cdar locals))))
7069         (make-local-variable (car locals))
7070         (set (car locals) nil))
7071       (setq locals (cdr locals))))
7072   (gnus-make-thread-indent-array)
7073   (gnus-simplify-mode-line)
7074   (setq major-mode 'gnus-summary-mode)
7075   (setq mode-name "Summary")
7076   (make-local-variable 'minor-mode-alist)
7077   (use-local-map gnus-summary-mode-map)
7078   (buffer-disable-undo (current-buffer))
7079   (setq buffer-read-only t)             ;Disable modification
7080   (setq truncate-lines t)
7081   (setq selective-display t)
7082   (setq selective-display-ellipses t)   ;Display `...'
7083   (setq buffer-display-table gnus-summary-display-table)
7084   (setq gnus-newsgroup-name group)
7085   (make-local-variable 'gnus-summary-line-format)
7086   (make-local-variable 'gnus-summary-line-format-spec)
7087   (make-local-variable 'gnus-summary-mark-positions)
7088   (run-hooks 'gnus-summary-mode-hook))
7089
7090 (defun gnus-summary-make-display-table ()
7091   ;; Change the display table.  Odd characters have a tendency to mess
7092   ;; up nicely formatted displays - we make all possible glyphs
7093   ;; display only a single character.
7094
7095   ;; We start from the standard display table, if any.
7096   (setq gnus-summary-display-table
7097         (or (copy-sequence standard-display-table)
7098             (make-display-table)))
7099   ;; Nix out all the control chars...
7100   (let ((i 32))
7101     (while (>= (setq i (1- i)) 0)
7102       (aset gnus-summary-display-table i [??])))
7103   ;; ... but not newline and cr, of course. (cr is necessary for the
7104   ;; selective display).
7105   (aset gnus-summary-display-table ?\n nil)
7106   (aset gnus-summary-display-table ?\r nil)
7107   ;; We nix out any glyphs over 126 that are not set already.
7108   (let ((i 256))
7109     (while (>= (setq i (1- i)) 127)
7110       ;; Only modify if the entry is nil.
7111       (or (aref gnus-summary-display-table i)
7112           (aset gnus-summary-display-table i [??])))))
7113
7114 (defun gnus-summary-clear-local-variables ()
7115   (let ((locals gnus-summary-local-variables))
7116     (while locals
7117       (if (consp (car locals))
7118           (and (vectorp (caar locals))
7119                (set (caar locals) nil))
7120         (and (vectorp (car locals))
7121              (set (car locals) nil)))
7122       (setq locals (cdr locals)))))
7123
7124 ;; Summary data functions.
7125
7126 (defmacro gnus-data-number (data)
7127   `(car ,data))
7128
7129 (defmacro gnus-data-set-number (data number)
7130   `(setcar ,data ,number))
7131
7132 (defmacro gnus-data-mark (data)
7133   `(nth 1 ,data))
7134
7135 (defmacro gnus-data-set-mark (data mark)
7136   `(setcar (nthcdr 1 ,data) ,mark))
7137
7138 (defmacro gnus-data-pos (data)
7139   `(nth 2 ,data))
7140
7141 (defmacro gnus-data-set-pos (data pos)
7142   `(setcar (nthcdr 2 ,data) ,pos))
7143
7144 (defmacro gnus-data-header (data)
7145   `(nth 3 ,data))
7146
7147 (defmacro gnus-data-level (data)
7148   `(nth 4 ,data))
7149
7150 (defmacro gnus-data-unread-p (data)
7151   `(= (nth 1 ,data) gnus-unread-mark))
7152
7153 (defmacro gnus-data-pseudo-p (data)
7154   `(consp (nth 3 ,data)))
7155
7156 (defmacro gnus-data-find (number)
7157   `(assq ,number gnus-newsgroup-data))
7158
7159 (defmacro gnus-data-find-list (number &optional data)
7160   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7161      (memq (assq ,number bdata)
7162            bdata)))
7163
7164 (defmacro gnus-data-make (number mark pos header level)
7165   `(list ,number ,mark ,pos ,header ,level))
7166
7167 (defun gnus-data-enter (after-article number mark pos header level offset)
7168   (let ((data (gnus-data-find-list after-article)))
7169     (or data (error "No such article: %d" after-article))
7170     (setcdr data (cons (gnus-data-make number mark pos header level)
7171                        (cdr data)))
7172     (setq gnus-newsgroup-data-reverse nil)
7173     (gnus-data-update-list (cddr data) offset)))
7174
7175 (defun gnus-data-enter-list (after-article list &optional offset)
7176   (when list
7177     (let ((data (and after-article (gnus-data-find-list after-article)))
7178           (ilist list))
7179       (or data (not after-article) (error "No such article: %d" after-article))
7180       ;; Find the last element in the list to be spliced into the main
7181       ;; list.
7182       (while (cdr list)
7183         (setq list (cdr list)))
7184       (if (not data)
7185           (progn
7186             (setcdr list gnus-newsgroup-data)
7187             (setq gnus-newsgroup-data ilist)
7188             (and offset (gnus-data-update-list (cdr list) offset)))
7189         (setcdr list (cdr data))
7190         (setcdr data ilist)
7191         (and offset (gnus-data-update-list (cdr data) offset)))
7192       (setq gnus-newsgroup-data-reverse nil))))
7193
7194 (defun gnus-data-remove (article &optional offset)
7195   (let ((data gnus-newsgroup-data))
7196     (if (= (gnus-data-number (car data)) article)
7197         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7198               gnus-newsgroup-data-reverse nil)
7199       (while (cdr data)
7200         (and (= (gnus-data-number (cadr data)) article)
7201              (progn
7202                (setcdr data (cddr data))
7203                (and offset (gnus-data-update-list (cdr data) offset))
7204                (setq data nil
7205                      gnus-newsgroup-data-reverse nil)))
7206         (setq data (cdr data))))))
7207
7208 (defmacro gnus-data-list (backward)
7209   `(if ,backward
7210        (or gnus-newsgroup-data-reverse
7211            (setq gnus-newsgroup-data-reverse
7212                  (reverse gnus-newsgroup-data)))
7213      gnus-newsgroup-data))
7214
7215 (defun gnus-data-update-list (data offset)
7216   "Add OFFSET to the POS of all data entries in DATA."
7217   (while data
7218     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7219     (setq data (cdr data))))
7220
7221 (defun gnus-data-compute-positions ()
7222   "Compute the positions of all articles."
7223   (let ((data gnus-newsgroup-data)
7224         pos)
7225     (while data
7226       (when (setq pos (text-property-any
7227                        (point-min) (point-max)
7228                        'gnus-number (gnus-data-number (car data))))
7229         (gnus-data-set-pos (car data) (+ pos 3)))
7230       (setq data (cdr data)))))
7231
7232 (defun gnus-summary-article-pseudo-p (article)
7233   "Say whether this article is a pseudo article or not."
7234   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7235
7236 (defun gnus-article-parent-p (number)
7237   "Say whether this article is a parent or not."
7238   (let ((data (gnus-data-find-list number)))
7239     (and (cdr data)                     ; There has to be an article after...
7240          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7241             (gnus-data-level (nth 1 data))))))
7242
7243 (defun gnus-article-children (number)
7244   "Return a list of all children to NUMBER."
7245   (let* ((data (gnus-data-find-list number))
7246          (level (gnus-data-level (car data)))
7247          children)
7248     (setq data (cdr data))
7249     (while (and data            
7250                 (= (gnus-data-level (car data)) (1+ level)))
7251       (push (gnus-data-number (car data)) children)
7252       (setq data (cdr data)))
7253     children))
7254
7255 (defmacro gnus-summary-skip-intangible ()
7256   "If the current article is intangible, then jump to a different article."
7257   '(let ((to (get-text-property (point) 'gnus-intangible)))
7258     (and to (gnus-summary-goto-subject to))))
7259
7260 (defmacro gnus-summary-article-intangible-p ()
7261   "Say whether this article is intangible or not."
7262   '(get-text-property (point) 'gnus-intangible))
7263
7264 ;; Some summary mode macros.
7265
7266 (defmacro gnus-summary-article-number ()
7267   "The article number of the article on the current line.
7268 If there isn's an article number here, then we return the current
7269 article number."
7270   '(progn
7271      (gnus-summary-skip-intangible)
7272      (or (get-text-property (point) 'gnus-number)
7273          (gnus-summary-last-subject))))
7274
7275 (defmacro gnus-summary-article-header (&optional number)
7276   `(gnus-data-header (gnus-data-find
7277                       ,(or number '(gnus-summary-article-number)))))
7278
7279 (defmacro gnus-summary-thread-level (&optional number)
7280   `(if (and (eq gnus-summary-make-false-root 'dummy)
7281             (get-text-property (point) 'gnus-intangible))
7282        0
7283      (gnus-data-level (gnus-data-find
7284                        ,(or number '(gnus-summary-article-number))))))
7285
7286 (defmacro gnus-summary-article-mark (&optional number)
7287   `(gnus-data-mark (gnus-data-find
7288                     ,(or number '(gnus-summary-article-number)))))
7289
7290 (defmacro gnus-summary-article-pos (&optional number)
7291   `(gnus-data-pos (gnus-data-find
7292                    ,(or number '(gnus-summary-article-number)))))
7293
7294 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7295 (defmacro gnus-summary-article-subject (&optional number)
7296   "Return current subject string or nil if nothing."
7297   `(let ((headers
7298           ,(if number
7299                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7300              '(gnus-data-header (assq (gnus-summary-article-number)
7301                                       gnus-newsgroup-data)))))
7302      (and headers
7303           (vectorp headers)
7304           (mail-header-subject headers))))
7305
7306 (defmacro gnus-summary-article-score (&optional number)
7307   "Return current article score."
7308   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7309                   gnus-newsgroup-scored))
7310        gnus-summary-default-score 0))
7311
7312 (defun gnus-summary-article-children (&optional number)
7313   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7314          (level (gnus-data-level (car data)))
7315          l children)
7316     (while (and (setq data (cdr data))
7317                 (> (setq l (gnus-data-level (car data))) level))
7318       (and (= (1+ level) l)
7319            (setq children (cons (gnus-data-number (car data))
7320                                 children))))
7321     (nreverse children)))
7322
7323 (defun gnus-summary-article-parent (&optional number)
7324   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7325                                     (gnus-data-list t)))
7326          (level (gnus-data-level (car data))))
7327     (if (zerop level)
7328         () ; This is a root.
7329       ;; We search until we find an article with a level less than
7330       ;; this one.  That function has to be the parent.
7331       (while (and (setq data (cdr data))
7332                   (not (< (gnus-data-level (car data)) level))))
7333       (and data (gnus-data-number (car data))))))
7334
7335 (defun gnus-unread-mark-p (mark)
7336   "Say whether MARK is the unread mark."
7337   (= mark gnus-unread-mark))
7338
7339 (defun gnus-read-mark-p (mark)
7340   "Say whether MARK is one of the marks that mark as read.
7341 This is all marks except unread, ticked, dormant, and expirable."
7342   (not (or (= mark gnus-unread-mark)
7343            (= mark gnus-ticked-mark)
7344            (= mark gnus-dormant-mark)
7345            (= mark gnus-expirable-mark))))
7346
7347 ;; Various summary mode internalish functions.
7348
7349 (defun gnus-mouse-pick-article (e)
7350   (interactive "e")
7351   (mouse-set-point e)
7352   (gnus-summary-next-page nil t))
7353
7354 (defun gnus-summary-setup-buffer (group)
7355   "Initialize summary buffer."
7356   (let ((buffer (concat "*Summary " group "*")))
7357     (if (get-buffer buffer)
7358         (progn
7359           (set-buffer buffer)
7360           (setq gnus-summary-buffer (current-buffer))
7361           (not gnus-newsgroup-prepared))
7362       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7363       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7364       (gnus-add-current-to-buffer-list)
7365       (gnus-summary-mode group)
7366       (when gnus-carpal
7367         (gnus-carpal-setup-buffer 'summary))
7368       (unless gnus-single-article-buffer
7369         (make-local-variable 'gnus-article-buffer)
7370         (make-local-variable 'gnus-article-current)
7371         (make-local-variable 'gnus-original-article-buffer))
7372       (setq gnus-newsgroup-name group)
7373       t)))
7374
7375 (defun gnus-set-global-variables ()
7376   ;; Set the global equivalents of the summary buffer-local variables
7377   ;; to the latest values they had.  These reflect the summary buffer
7378   ;; that was in action when the last article was fetched.
7379   (when (eq major-mode 'gnus-summary-mode)
7380     (setq gnus-summary-buffer (current-buffer))
7381     (let ((name gnus-newsgroup-name)
7382           (marked gnus-newsgroup-marked)
7383           (unread gnus-newsgroup-unreads)
7384           (headers gnus-current-headers)
7385           (data gnus-newsgroup-data)
7386           (summary gnus-summary-buffer)
7387           (article-buffer gnus-article-buffer)
7388           (original gnus-original-article-buffer)
7389           (gac gnus-article-current)
7390           (score-file gnus-current-score-file))
7391       (save-excursion
7392         (set-buffer gnus-group-buffer)
7393         (setq gnus-newsgroup-name name)
7394         (setq gnus-newsgroup-marked marked)
7395         (setq gnus-newsgroup-unreads unread)
7396         (setq gnus-current-headers headers)
7397         (setq gnus-newsgroup-data data)
7398         (setq gnus-article-current gac)
7399         (setq gnus-summary-buffer summary)
7400         (setq gnus-article-buffer article-buffer)
7401         (setq gnus-original-article-buffer original)
7402         (setq gnus-current-score-file score-file)))))
7403
7404 (defun gnus-summary-last-article-p (&optional article)
7405   "Return whether ARTICLE is the last article in the buffer."
7406   (if (not (setq article (or article (gnus-summary-article-number))))
7407       t ; All non-existant numbers are the last article. :-)
7408     (not (cdr (gnus-data-find-list article)))))
7409
7410 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7411   "Insert a dummy root in the summary buffer."
7412   (beginning-of-line)
7413   (gnus-add-text-properties
7414    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7415    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7416
7417 (defun gnus-make-thread-indent-array ()
7418   (let ((n 200))
7419     (unless (and gnus-thread-indent-array
7420                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7421       (setq gnus-thread-indent-array (make-vector 201 "")
7422             gnus-thread-indent-array-level gnus-thread-indent-level)
7423       (while (>= n 0)
7424         (aset gnus-thread-indent-array n
7425               (make-string (* n gnus-thread-indent-level) ? ))
7426         (setq n (1- n))))))
7427
7428 (defun gnus-summary-insert-line
7429   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7430                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7431                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7432   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7433          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7434          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7435          (gnus-tmp-score-char
7436           (if (or (null gnus-summary-default-score)
7437                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7438                       gnus-summary-zcore-fuzz)) ? 
7439             (if (< gnus-tmp-score gnus-summary-default-score)
7440                 gnus-score-below-mark gnus-score-over-mark)))
7441          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7442                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7443                                   gnus-cached-mark)
7444                                  (gnus-tmp-replied gnus-replied-mark)
7445                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7446                                   gnus-saved-mark)
7447                                  (t gnus-unread-mark)))
7448          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7449          (gnus-tmp-name
7450           (cond
7451            ((string-match "(.+)" gnus-tmp-from)
7452             (substring gnus-tmp-from
7453                        (1+ (match-beginning 0)) (1- (match-end 0))))
7454            ((string-match "<[^>]+> *$" gnus-tmp-from)
7455             (let ((beg (match-beginning 0)))
7456               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7457                        (substring gnus-tmp-from (1+ (match-beginning 0))
7458                                   (1- (match-end 0))))
7459                   (substring gnus-tmp-from 0 beg))))
7460            (t gnus-tmp-from)))
7461          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7462          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7463          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7464          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7465          (buffer-read-only nil))
7466     (when (string= gnus-tmp-name "")
7467       (setq gnus-tmp-name gnus-tmp-from))
7468     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7469     (put-text-property
7470      (point)
7471      (progn (eval gnus-summary-line-format-spec) (point))
7472      'gnus-number gnus-tmp-number)
7473     (when (gnus-visual-p 'summary-highlight 'highlight)
7474       (forward-line -1)
7475       (run-hooks 'gnus-summary-update-hook)
7476       (forward-line 1))))
7477
7478 (defun gnus-summary-update-line (&optional dont-update)
7479   ;; Update summary line after change.
7480   (when (and gnus-summary-default-score
7481              (not gnus-summary-inhibit-highlight))
7482     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7483            (article (gnus-summary-article-number))
7484            (score (gnus-summary-article-score article)))
7485       (unless dont-update
7486         (if (and gnus-summary-mark-below
7487                  (< (gnus-summary-article-score)
7488                     gnus-summary-mark-below))
7489             ;; This article has a low score, so we mark it as read.
7490             (when (memq article gnus-newsgroup-unreads)
7491               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7492           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7493             ;; This article was previously marked as read on account
7494             ;; of a low score, but now it has risen, so we mark it as
7495             ;; unread.
7496             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7497         (gnus-summary-update-mark
7498          (if (or (null gnus-summary-default-score)
7499                  (<= (abs (- score gnus-summary-default-score))
7500                      gnus-summary-zcore-fuzz)) ? 
7501            (if (< score gnus-summary-default-score)
7502                gnus-score-below-mark gnus-score-over-mark)) 'score))
7503       ;; Do visual highlighting.
7504       (when (gnus-visual-p 'summary-highlight 'highlight)
7505         (run-hooks 'gnus-summary-update-hook)))))
7506
7507 (defvar gnus-tmp-new-adopts nil)
7508
7509 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7510   ;; Sum up all elements (and sub-elements) in a list.
7511   (let* ((number
7512           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7513           (cond
7514            ((and (consp thread) (cdr thread))
7515             (apply
7516              '+ 1 (mapcar
7517                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7518            ((null thread)
7519             1)
7520            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7521             1)
7522            (t 1))))
7523     (when (and level (zerop level) gnus-tmp-new-adopts)
7524       (incf number
7525             (apply '+ (mapcar
7526                        'gnus-summary-number-of-articles-in-thread
7527                        gnus-tmp-new-adopts))))
7528     (if char
7529         (if (> number 1) gnus-not-empty-thread-mark
7530           gnus-empty-thread-mark)
7531       number)))
7532
7533 (defun gnus-summary-set-local-parameters (group)
7534  "Go through the local params of GROUP and set all variable specs in that list."
7535   (let ((params (gnus-info-params (gnus-get-info group)))
7536         elem)
7537     (while params
7538       (setq elem (car params)
7539             params (cdr params))
7540       (and (consp elem)                 ; Has to be a cons.
7541            (consp (cdr elem))           ; The cdr has to be a list.
7542            (symbolp (car elem))         ; Has to be a symbol in there.
7543            (not (memq (car elem) 
7544                       '(quit-config to-address to-list to-group)))
7545            (progn                       ; So we set it.
7546              (make-local-variable (car elem))
7547              (set (car elem) (eval (nth 1 elem))))))))
7548
7549 (defun gnus-summary-read-group (group &optional show-all no-article
7550                                       kill-buffer no-display)
7551   "Start reading news in newsgroup GROUP.
7552 If SHOW-ALL is non-nil, already read articles are also listed.
7553 If NO-ARTICLE is non-nil, no article is selected initially.
7554 If NO-DISPLAY, don't generate a summary buffer."
7555   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7556   (let* ((new-group (gnus-summary-setup-buffer group))
7557          (quit-config (gnus-group-quit-config group))
7558          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7559     (cond
7560      ;; This summary buffer exists already, so we just select it.
7561      ((not new-group)
7562       (gnus-set-global-variables)
7563       (when kill-buffer
7564         (gnus-kill-or-deaden-summary kill-buffer))
7565       (gnus-configure-windows 'summary 'force)
7566       (gnus-set-mode-line 'summary)
7567       (gnus-summary-position-point)
7568       (message "")
7569       t)
7570      ;; We couldn't select this group.
7571      ((null did-select)
7572       (when (and (eq major-mode 'gnus-summary-mode)
7573                  (not (equal (current-buffer) kill-buffer)))
7574         (kill-buffer (current-buffer))
7575         (if (not quit-config)
7576             (progn
7577               (set-buffer gnus-group-buffer)
7578               (gnus-group-jump-to-group group)
7579               (gnus-group-next-unread-group 1))
7580           (if (not (buffer-name (car quit-config)))
7581               (gnus-configure-windows 'group 'force)
7582             (set-buffer (car quit-config))
7583             (and (eq major-mode 'gnus-summary-mode)
7584                  (gnus-set-global-variables))
7585             (gnus-configure-windows (cdr quit-config)))))
7586       (gnus-message 3 "Can't select group")
7587       nil)
7588      ;; The user did a `C-g' while prompting for number of articles,
7589      ;; so we exit this group.
7590      ((eq did-select 'quit)
7591       (and (eq major-mode 'gnus-summary-mode)
7592            (not (equal (current-buffer) kill-buffer))
7593            (kill-buffer (current-buffer)))
7594       (when kill-buffer
7595         (gnus-kill-or-deaden-summary kill-buffer))
7596       (if (not quit-config)
7597           (progn
7598             (set-buffer gnus-group-buffer)
7599             (gnus-group-jump-to-group group)
7600             (gnus-group-next-unread-group 1)
7601             (gnus-configure-windows 'group 'force))
7602         (if (not (buffer-name (car quit-config)))
7603             (gnus-configure-windows 'group 'force)
7604           (set-buffer (car quit-config))
7605           (and (eq major-mode 'gnus-summary-mode)
7606                (gnus-set-global-variables))
7607           (gnus-configure-windows (cdr quit-config))))
7608       ;; Finally signal the quit.
7609       (signal 'quit nil))
7610      ;; The group was successfully selected.
7611      (t
7612       (gnus-set-global-variables)
7613       ;; Save the active value in effect when the group was entered.
7614       (setq gnus-newsgroup-active
7615             (gnus-copy-sequence
7616              (gnus-active gnus-newsgroup-name)))
7617       ;; You can change the summary buffer in some way with this hook.
7618       (run-hooks 'gnus-select-group-hook)
7619       ;; Set any local variables in the group parameters.
7620       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7621       (gnus-update-format-specifications)
7622       ;; Do score processing.
7623       (when gnus-use-scoring
7624         (gnus-possibly-score-headers))
7625       ;; Check whether to fill in the gaps in the threads.
7626       (when gnus-build-sparse-threads
7627         (gnus-build-sparse-threads))
7628       ;; Find the initial limit.
7629       (if gnus-show-threads
7630           (if show-all
7631               (let ((gnus-newsgroup-dormant nil))
7632                 (gnus-summary-initial-limit show-all))
7633             (gnus-summary-initial-limit show-all))
7634         (setq gnus-newsgroup-limit 
7635               (mapcar 
7636                (lambda (header) (mail-header-number header))
7637                gnus-newsgroup-headers)))
7638       ;; Generate the summary buffer.
7639       (unless no-display
7640         (gnus-summary-prepare))
7641       (when gnus-use-trees
7642         (gnus-tree-open group)
7643         (setq gnus-summary-highlight-line-function
7644               'gnus-tree-highlight-article))
7645       ;; If the summary buffer is empty, but there are some low-scored
7646       ;; articles or some excluded dormants, we include these in the
7647       ;; buffer.
7648       (when (and (zerop (buffer-size))
7649                  (not no-display))
7650         (cond (gnus-newsgroup-dormant
7651                (gnus-summary-limit-include-dormant))
7652               ((and gnus-newsgroup-scored show-all)
7653                (gnus-summary-limit-include-expunged))))
7654       ;; Function `gnus-apply-kill-file' must be called in this hook.
7655       (run-hooks 'gnus-apply-kill-hook)
7656       (if (and (zerop (buffer-size))
7657                (not no-display))
7658           (progn
7659             ;; This newsgroup is empty.
7660             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7661             (gnus-message 6 "No unread news")
7662             (when kill-buffer
7663               (gnus-kill-or-deaden-summary kill-buffer))
7664             ;; Return nil from this function.
7665             nil)
7666         ;; Hide conversation thread subtrees.  We cannot do this in
7667         ;; gnus-summary-prepare-hook since kill processing may not
7668         ;; work with hidden articles.
7669         (and gnus-show-threads
7670              gnus-thread-hide-subtree
7671              (gnus-summary-hide-all-threads))
7672         ;; Show first unread article if requested.
7673         (if (and (not no-article)
7674                  (not no-display)
7675                  gnus-newsgroup-unreads
7676                  gnus-auto-select-first)
7677             (unless (if (eq gnus-auto-select-first 'best)
7678                         (gnus-summary-best-unread-article)
7679                       (gnus-summary-first-unread-article))
7680               (gnus-configure-windows 'summary))
7681           ;; Don't select any articles, just move point to the first
7682           ;; article in the group.
7683           (goto-char (point-min))
7684           (gnus-summary-position-point)
7685           (gnus-set-mode-line 'summary)
7686           (gnus-configure-windows 'summary 'force))
7687         ;; If we are in async mode, we send some info to the backend.
7688         (when gnus-newsgroup-async
7689           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7690         (when kill-buffer
7691           (gnus-kill-or-deaden-summary kill-buffer))
7692         (when (get-buffer-window gnus-group-buffer t)
7693           ;; Gotta use windows, because recenter does wierd stuff if
7694           ;; the current buffer ain't the displayed window.
7695           (let ((owin (selected-window)))
7696             (select-window (get-buffer-window gnus-group-buffer t))
7697             (when (gnus-group-goto-group group)
7698               (recenter))
7699             (select-window owin))))
7700       ;; Mark this buffer as "prepared".
7701       (setq gnus-newsgroup-prepared t)
7702       t))))
7703
7704 (defun gnus-summary-prepare ()
7705   "Generate the summary buffer."
7706   (let ((buffer-read-only nil))
7707     (erase-buffer)
7708     (setq gnus-newsgroup-data nil
7709           gnus-newsgroup-data-reverse nil)
7710     (run-hooks 'gnus-summary-generate-hook)
7711     ;; Generate the buffer, either with threads or without.
7712     (when gnus-newsgroup-headers
7713       (gnus-summary-prepare-threads
7714        (if gnus-show-threads
7715            (gnus-sort-gathered-threads
7716             (funcall gnus-summary-thread-gathering-function
7717                      (gnus-sort-threads
7718                       (gnus-cut-threads (gnus-make-threads)))))
7719          ;; Unthreaded display.
7720          (gnus-sort-articles gnus-newsgroup-headers))))
7721     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7722     ;; Call hooks for modifying summary buffer.
7723     (goto-char (point-min))
7724     (run-hooks 'gnus-summary-prepare-hook)))
7725
7726 (defun gnus-gather-threads-by-subject (threads)
7727   "Gather threads by looking at Subject headers."
7728   (if (not gnus-summary-make-false-root)
7729       threads
7730     (let ((hashtb (gnus-make-hashtable 1023))
7731           (prev threads)
7732           (result threads)
7733           subject hthread whole-subject)
7734       (while threads
7735         (setq whole-subject (mail-header-subject (caar threads)))
7736         (setq subject
7737               (cond
7738                ;; Truncate the subject.
7739                ((numberp gnus-summary-gather-subject-limit)
7740                 (setq subject (gnus-simplify-subject-re whole-subject))
7741                 (if (> (length subject) gnus-summary-gather-subject-limit)
7742                     (substring subject 0 gnus-summary-gather-subject-limit)
7743                   subject))
7744                ;; Fuzzily simplify it.
7745                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7746                 (gnus-simplify-subject-fuzzy whole-subject))
7747                ;; Just remove the leading "Re:".
7748                (t
7749                 (gnus-simplify-subject-re whole-subject))))
7750
7751         (if (and gnus-summary-gather-exclude-subject
7752                  (string-match gnus-summary-gather-exclude-subject
7753                                subject))
7754             ()          ; We don't want to do anything with this article.
7755           ;; We simplify the subject before looking it up in the
7756           ;; hash table.
7757
7758           (if (setq hthread (gnus-gethash subject hashtb))
7759               (progn
7760                 ;; We enter a dummy root into the thread, if we
7761                 ;; haven't done that already.
7762                 (unless (stringp (caar hthread))
7763                   (setcar hthread (list whole-subject (car hthread))))
7764                 ;; We add this new gathered thread to this gathered
7765                 ;; thread.
7766                 (setcdr (car hthread)
7767                         (nconc (cdar hthread) (list (car threads))))
7768                 ;; Remove it from the list of threads.
7769                 (setcdr prev (cdr threads))
7770                 (setq threads prev))
7771             ;; Enter this thread into the hash table.
7772             (gnus-sethash subject threads hashtb)))
7773         (setq prev threads)
7774         (setq threads (cdr threads)))
7775       result)))
7776
7777 (defun gnus-gather-threads-by-references (threads)
7778   "Gather threads by looking at References headers."
7779   (let ((idhashtb (gnus-make-hashtable 1023))
7780         (thhashtb (gnus-make-hashtable 1023))
7781         (prev threads)
7782         (result threads)
7783         ids references id gthread gid entered)
7784     (while threads
7785       (when (setq references (mail-header-references (caar threads)))
7786         (setq id (mail-header-id (caar threads)))
7787         (setq ids (gnus-split-references references))
7788         (setq entered nil)
7789         (while ids
7790           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7791               (progn
7792                 (gnus-sethash (car ids) id idhashtb)
7793                 (gnus-sethash id threads thhashtb))
7794             (setq gthread (gnus-gethash gid thhashtb))
7795             (unless entered
7796               ;; We enter a dummy root into the thread, if we
7797               ;; haven't done that already.
7798               (unless (stringp (caar gthread))
7799                 (setcar gthread (list (mail-header-subject (caar gthread))
7800                                       (car gthread))))
7801               ;; We add this new gathered thread to this gathered
7802               ;; thread.
7803               (setcdr (car gthread)
7804                       (nconc (cdar gthread) (list (car threads)))))
7805             ;; Add it into the thread hash table.
7806             (gnus-sethash id gthread thhashtb)
7807             (setq entered t)
7808             ;; Remove it from the list of threads.
7809             (setcdr prev (cdr threads))
7810             (setq threads prev))
7811           (setq ids (cdr ids))))
7812       (setq prev threads)
7813       (setq threads (cdr threads)))
7814     result))
7815
7816 (defun gnus-sort-gathered-threads (threads)
7817   "Sort subtreads inside each gathered thread by article number."
7818   (let ((result threads))
7819     (while threads
7820       (when (stringp (caar threads))
7821         (setcdr (car threads)
7822                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7823       (setq threads (cdr threads)))
7824     result))
7825
7826 (defun gnus-make-threads ()
7827   "Go through the dependency hashtb and find the roots.  Return all threads."
7828   (let (threads)
7829     (mapatoms
7830      (lambda (refs)
7831        (unless (car (symbol-value refs))
7832          ;; These threads do not refer back to any other articles,
7833          ;; so they're roots.
7834          (setq threads (append (cdr (symbol-value refs)) threads))))
7835      gnus-newsgroup-dependencies)
7836     threads))
7837
7838 (defun gnus-build-sparse-threads ()
7839   (let ((headers gnus-newsgroup-headers)
7840         (deps gnus-newsgroup-dependencies)
7841         header references generation relations 
7842         cthread subject child end pthread relation)
7843     ;; First we create an alist of generations/relations, where 
7844     ;; generations is how much we trust the ralation, and the relation
7845     ;; is parent/child.
7846     (gnus-message 7 "Making sparse threads...")
7847     (save-excursion
7848       (nnheader-set-temp-buffer " *gnus sparse threads*")
7849       (while (setq header (pop headers))
7850         (when (and (setq references (mail-header-references header))
7851                    (not (string= references "")))
7852           (insert references)
7853           (setq child (mail-header-id header)
7854                 subject (mail-header-subject header))
7855           (setq generation 0)
7856           (while (search-backward ">" nil t)
7857             (setq end (1+ (point)))
7858             (when (search-backward "<" nil t)
7859               (push (list (incf generation) 
7860                           child (setq child (buffer-substring (point) end))
7861                           subject)
7862                     relations)))
7863           (push (list (1+ generation) child nil subject) relations)
7864           (erase-buffer)))
7865       (kill-buffer (current-buffer)))
7866     ;; Sort over trustworthiness.
7867     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7868     (while (setq relation (pop relations))
7869       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7870                 (unless (car (symbol-value cthread))
7871                   ;; Make this article the parent of these threads.
7872                   (setcar (symbol-value cthread)
7873                           (vector gnus-reffed-article-number 
7874                                   (cadddr relation) 
7875                                   "" ""
7876                                   (cadr relation) 
7877                                   (or (caddr relation) "") 0 0 "")))
7878               (set cthread (list (vector gnus-reffed-article-number
7879                                          (cadddr relation) 
7880                                          "" "" (cadr relation) 
7881                                          (or (caddr relation) "") 0 0 ""))))
7882         (push gnus-reffed-article-number gnus-newsgroup-limit)
7883         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7884         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7885               gnus-newsgroup-reads)
7886         (decf gnus-reffed-article-number)
7887         ;; Make this new thread the child of its parent.
7888         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7889             (setcdr (symbol-value pthread)
7890                     (nconc (cdr (symbol-value pthread))
7891                            (list (symbol-value cthread))))
7892           (set pthread (list nil (symbol-value cthread))))))
7893     (gnus-message 7 "Making sparse threads...done")))
7894
7895 (defun gnus-build-old-threads ()
7896   ;; Look at all the articles that refer back to old articles, and
7897   ;; fetch the headers for the articles that aren't there.  This will
7898   ;; build complete threads - if the roots haven't been expired by the
7899   ;; server, that is.
7900   (let (id heads)
7901     (mapatoms
7902      (lambda (refs)
7903        (when (not (car (symbol-value refs)))
7904          (setq heads (cdr (symbol-value refs)))
7905          (while heads
7906            (if (memq (mail-header-number (caar heads))
7907                      gnus-newsgroup-dormant)
7908                (setq heads (cdr heads))
7909              (setq id (symbol-name refs))
7910              (while (and (setq id (gnus-build-get-header id))
7911                          (not (car (gnus-gethash
7912                                     id gnus-newsgroup-dependencies)))))
7913              (setq heads nil)))))
7914      gnus-newsgroup-dependencies)))
7915
7916 (defun gnus-build-get-header (id)
7917   ;; Look through the buffer of NOV lines and find the header to
7918   ;; ID.  Enter this line into the dependencies hash table, and return
7919   ;; the id of the parent article (if any).
7920   (let ((deps gnus-newsgroup-dependencies)
7921         found header)
7922     (prog1
7923         (save-excursion
7924           (set-buffer nntp-server-buffer)
7925           (goto-char (point-min))
7926           (while (and (not found) (search-forward id nil t))
7927             (beginning-of-line)
7928             (setq found (looking-at
7929                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7930                                  (regexp-quote id))))
7931             (or found (beginning-of-line 2)))
7932           (when found
7933             (beginning-of-line)
7934             (and
7935              (setq header (gnus-nov-parse-line
7936                            (read (current-buffer)) deps))
7937              (gnus-parent-id (mail-header-references header)))))
7938       (when header
7939         (let ((number (mail-header-number header)))
7940           (push number gnus-newsgroup-limit)
7941           (push header gnus-newsgroup-headers)
7942           (if (memq number gnus-newsgroup-unselected)
7943               (progn
7944                 (push number gnus-newsgroup-unreads)
7945                 (setq gnus-newsgroup-unselected
7946                       (delq number gnus-newsgroup-unselected)))
7947             (push number gnus-newsgroup-ancient)))))))
7948
7949 (defun gnus-summary-update-article (article &optional header)
7950   "Update ARTICLE in the summary buffer."
7951   (set-buffer gnus-summary-buffer)
7952   (let* ((header (or header (gnus-summary-article-header article)))
7953          (id (mail-header-id header))
7954          (data (gnus-data-find article))
7955          (thread (gnus-id-to-thread id))
7956          (parent
7957           (gnus-id-to-thread (or (gnus-parent-id 
7958                                   (mail-header-references header))
7959                                  "tull")))
7960          (buffer-read-only nil)
7961          (old (car thread))
7962          (number (mail-header-number header))
7963          pos)
7964     (when thread
7965       (setcar thread nil)
7966       (when parent
7967         (delq thread parent))
7968       (if (gnus-summary-insert-subject id header)
7969           ;; Set the (possibly) new article number in the data structure.
7970           (gnus-data-set-number data (gnus-id-to-article id))
7971         (setcar thread old)
7972         nil))))
7973
7974 (defun gnus-rebuild-thread (id)
7975   "Rebuild the thread containing ID."
7976   (let ((buffer-read-only nil)
7977         current thread data)
7978     (if (not gnus-show-threads)
7979         (setq thread (list (car (gnus-id-to-thread id))))
7980       ;; Get the thread this article is part of.
7981       (setq thread (gnus-remove-thread id)))
7982     (setq current (save-excursion
7983                     (and (zerop (forward-line -1))
7984                          (gnus-summary-article-number))))
7985     ;; If this is a gathered thread, we have to go some re-gathering.
7986     (when (stringp (car thread))
7987       (let ((subject (car thread))
7988             roots thr)
7989         (setq thread (cdr thread))
7990         (while thread
7991           (unless (memq (setq thr (gnus-id-to-thread
7992                                       (gnus-root-id
7993                                        (mail-header-id (caar thread)))))
7994                         roots)
7995             (push thr roots))
7996           (setq thread (cdr thread)))
7997         ;; We now have all (unique) roots.
7998         (if (= (length roots) 1)
7999             ;; All the loose roots are now one solid root.
8000             (setq thread (car roots))
8001           (setq thread (cons subject (gnus-sort-threads roots))))))
8002     (let (threads)
8003       ;; We then insert this thread into the summary buffer.
8004       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8005         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8006         (setq data (nreverse gnus-newsgroup-data))
8007         (setq threads gnus-newsgroup-threads))
8008       ;; We splice the new data into the data structure.
8009       (gnus-data-enter-list current data)
8010       (gnus-data-compute-positions)
8011       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8012
8013 (defun gnus-id-to-thread (id)
8014   "Return the (sub-)thread where ID appears."
8015   (gnus-gethash id gnus-newsgroup-dependencies))
8016
8017 (defun gnus-id-to-article (id)
8018   "Return the article number of ID."
8019   (let ((thread (gnus-id-to-thread id)))
8020     (when (and thread
8021                (car thread))
8022       (mail-header-number (car thread)))))
8023
8024 (defun gnus-id-to-header (id)
8025   "Return the article headers of ID."
8026   (car (gnus-id-to-thread id)))
8027
8028 (defun gnus-article-displayed-root-p (article)
8029   "Say whether ARTICLE is a root(ish) article."
8030   (let ((level (gnus-summary-thread-level article))
8031         (refs (mail-header-references  (gnus-summary-article-header article)))
8032         particle)
8033     (cond 
8034      ((null level) nil)
8035      ((zerop level) t)
8036      ((null refs) t)
8037      ((null(gnus-parent-id refs)) t)
8038      ((and (= 1 level)
8039            (null (setq particle (gnus-id-to-article
8040                                  (gnus-parent-id refs))))
8041            (null (gnus-summary-thread-level particle)))))))
8042
8043 (defun gnus-root-id (id)
8044   "Return the id of the root of the thread where ID appears."
8045   (let (last-id prev)
8046     (while (and id (setq prev (car (gnus-gethash 
8047                                     id gnus-newsgroup-dependencies))))
8048       (setq last-id id
8049             id (gnus-parent-id (mail-header-references prev))))
8050     last-id))
8051
8052 (defun gnus-remove-thread (id &optional dont-remove)
8053   "Remove the thread that has ID in it."
8054   (let ((dep gnus-newsgroup-dependencies)
8055         headers thread last-id)
8056     ;; First go up in this thread until we find the root.
8057     (setq last-id (gnus-root-id id))
8058     (setq headers (list (car (gnus-id-to-thread last-id))
8059                         (caadr (gnus-id-to-thread last-id))))
8060     ;; We have now found the real root of this thread.  It might have
8061     ;; been gathered into some loose thread, so we have to search
8062     ;; through the threads to find the thread we wanted.
8063     (let ((threads gnus-newsgroup-threads)
8064           sub)
8065       (while threads
8066         (setq sub (car threads))
8067         (if (stringp (car sub))
8068             ;; This is a gathered threads, so we look at the roots
8069             ;; below it to find whether this article in in this
8070             ;; gathered root.
8071             (progn
8072               (setq sub (cdr sub))
8073               (while sub
8074                 (when (member (caar sub) headers)
8075                   (setq thread (car threads)
8076                         threads nil
8077                         sub nil))
8078                 (setq sub (cdr sub))))
8079           ;; It's an ordinary thread, so we check it.
8080           (when (eq (car sub) (car headers))
8081             (setq thread sub
8082                   threads nil)))
8083         (setq threads (cdr threads)))
8084       ;; If this article is in no thread, then it's a root.
8085       (if thread
8086           (unless dont-remove
8087             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8088         (setq thread (gnus-gethash last-id dep)))
8089       (when thread
8090         (prog1
8091             thread ; We return this thread.
8092           (unless dont-remove
8093             (if (stringp (car thread))
8094                 (progn
8095                   ;; If we use dummy roots, then we have to remove the
8096                   ;; dummy root as well.
8097                   (when (eq gnus-summary-make-false-root 'dummy)
8098                     ;; Uhm.
8099                     )
8100                   (setq thread (cdr thread))
8101                   (while thread
8102                     (gnus-remove-thread-1 (car thread))
8103                     (setq thread (cdr thread))))
8104               (gnus-remove-thread-1 thread))))))))
8105
8106 (defun gnus-remove-thread-1 (thread)
8107   "Remove the thread THREAD recursively."
8108   (let ((number (mail-header-number (car thread)))
8109         pos)
8110     (when (setq pos (text-property-any
8111                      (point-min) (point-max) 'gnus-number number))
8112       (goto-char pos)
8113       (gnus-delete-line)
8114       (gnus-data-remove number))
8115     (setq thread (cdr thread))
8116     (while thread
8117       (gnus-remove-thread-1 (pop thread)))))
8118
8119 (defun gnus-sort-threads (threads)
8120   "Sort THREADS."
8121   (if (not gnus-thread-sort-functions)
8122       threads
8123     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8124                     (car gnus-thread-sort-functions)
8125                   `(lambda (t1 t2)
8126                      ,(gnus-make-sort-function 
8127                        (reverse gnus-thread-sort-functions))))))
8128       (gnus-message 7 "Sorting threads...")
8129       (prog1
8130           (sort threads func)
8131         (gnus-message 7 "Sorting threads...done")))))
8132
8133 (defun gnus-sort-articles (articles)
8134   "Sort ARTICLES."
8135   (when gnus-article-sort-functions
8136     (let ((func (if (= 1 (length gnus-article-sort-functions))
8137                     (car gnus-article-sort-functions)
8138                   `(lambda (t1 t2)
8139                      ,(gnus-make-sort-function 
8140                        (reverse gnus-article-sort-functions))))))
8141       (gnus-message 7 "Sorting articles...")
8142       (prog1
8143           (setq gnus-newsgroup-headers (sort articles func))
8144         (gnus-message 7 "Sorting articles...done")))))
8145
8146 (defun gnus-make-sort-function (funs)
8147   "Return a composite sort condition based on the functions in FUNC."
8148   (if (cdr funs)
8149       `(or (,(car funs) t1 t2)
8150            (and (not (,(car funs) t2 t1))
8151                 ,(gnus-make-sort-function (cdr funs))))
8152     `(,(car funs) t1 t2)))
8153                  
8154 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8155 (defmacro gnus-thread-header (thread)
8156   ;; Return header of first article in THREAD.
8157   ;; Note that THREAD must never, ever be anything else than a variable -
8158   ;; using some other form will lead to serious barfage.
8159   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8160   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8161   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8162         (vector thread) 2))
8163
8164 (defsubst gnus-article-sort-by-number (h1 h2)
8165   "Sort articles by article number."
8166   (< (mail-header-number h1)
8167      (mail-header-number h2)))
8168
8169 (defun gnus-thread-sort-by-number (h1 h2)
8170   "Sort threads by root article number."
8171   (gnus-article-sort-by-number
8172    (gnus-thread-header h1) (gnus-thread-header h2)))
8173
8174 (defsubst gnus-article-sort-by-author (h1 h2)
8175   "Sort articles by root author."
8176   (string-lessp
8177    (let ((extract (funcall
8178                    gnus-extract-address-components
8179                    (mail-header-from h1))))
8180      (or (car extract) (cdr extract)))
8181    (let ((extract (funcall
8182                    gnus-extract-address-components
8183                    (mail-header-from h2))))
8184      (or (car extract) (cdr extract)))))
8185
8186 (defun gnus-thread-sort-by-author (h1 h2)
8187   "Sort threads by root author."
8188   (gnus-article-sort-by-author
8189    (gnus-thread-header h1)  (gnus-thread-header h2)))
8190
8191 (defsubst gnus-article-sort-by-subject (h1 h2)
8192   "Sort articles by root subject."
8193   (string-lessp
8194    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8195    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8196
8197 (defun gnus-thread-sort-by-subject (h1 h2)
8198   "Sort threads by root subject."
8199   (gnus-article-sort-by-subject
8200    (gnus-thread-header h1) (gnus-thread-header h2)))
8201
8202 (defsubst gnus-article-sort-by-date (h1 h2)
8203   "Sort articles by root article date."
8204   (string-lessp
8205    (gnus-sortable-date (mail-header-date h1))
8206    (gnus-sortable-date (mail-header-date h2))))
8207
8208 (defun gnus-thread-sort-by-date (h1 h2)
8209   "Sort threads by root article date."
8210   (gnus-article-sort-by-date
8211    (gnus-thread-header h1) (gnus-thread-header h2)))
8212
8213 (defsubst gnus-article-sort-by-score (h1 h2)
8214   "Sort articles by root article score.
8215 Unscored articles will be counted as having a score of zero."
8216   (> (or (cdr (assq (mail-header-number h1)
8217                     gnus-newsgroup-scored))
8218          gnus-summary-default-score 0)
8219      (or (cdr (assq (mail-header-number h2)
8220                     gnus-newsgroup-scored))
8221          gnus-summary-default-score 0)))
8222
8223 (defun gnus-thread-sort-by-score (h1 h2)
8224   "Sort threads by root article score."
8225   (gnus-article-sort-by-score
8226    (gnus-thread-header h1) (gnus-thread-header h2)))
8227
8228 (defun gnus-thread-sort-by-total-score (h1 h2)
8229   "Sort threads by the sum of all scores in the thread.
8230 Unscored articles will be counted as having a score of zero."
8231   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8232
8233 (defun gnus-thread-total-score (thread)
8234   ;;  This function find the total score of THREAD.
8235   (if (consp thread)
8236       (if (stringp (car thread))
8237           (apply gnus-thread-score-function 0
8238                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8239         (gnus-thread-total-score-1 thread))
8240     (gnus-thread-total-score-1 (list thread))))
8241
8242 (defun gnus-thread-total-score-1 (root)
8243   ;; This function find the total score of the thread below ROOT.
8244   (setq root (car root))
8245   (apply gnus-thread-score-function
8246          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8247              gnus-summary-default-score 0)
8248          (mapcar 'gnus-thread-total-score
8249                  (cdr (gnus-gethash (mail-header-id root)
8250                                     gnus-newsgroup-dependencies)))))
8251
8252 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8253 (defvar gnus-tmp-prev-subject nil)
8254 (defvar gnus-tmp-false-parent nil)
8255 (defvar gnus-tmp-root-expunged nil)
8256 (defvar gnus-tmp-dummy-line nil)
8257
8258 (defun gnus-summary-prepare-threads (threads)
8259   "Prepare summary buffer from THREADS and indentation LEVEL.
8260 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8261 or a straight list of headers."
8262   (gnus-message 7 "Generating summary...")
8263
8264   (setq gnus-newsgroup-threads threads)
8265   (beginning-of-line)
8266
8267   (let ((gnus-tmp-level 0)
8268         (default-score (or gnus-summary-default-score 0))
8269         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8270         thread number subject stack state gnus-tmp-gathered beg-match
8271         new-roots gnus-tmp-new-adopts thread-end
8272         gnus-tmp-header gnus-tmp-unread
8273         gnus-tmp-replied gnus-tmp-subject-or-nil
8274         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8275         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8276         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8277
8278     (setq gnus-tmp-prev-subject nil)
8279
8280     (if (vectorp (car threads))
8281         ;; If this is a straight (sic) list of headers, then a
8282         ;; threaded summary display isn't required, so we just create
8283         ;; an unthreaded one.
8284         (gnus-summary-prepare-unthreaded threads)
8285
8286       ;; Do the threaded display.
8287
8288       (while (or threads stack gnus-tmp-new-adopts new-roots)
8289
8290         (if (and (= gnus-tmp-level 0)
8291                  (not (setq gnus-tmp-dummy-line nil))
8292                  (or (not stack)
8293                      (= (caar stack) 0))
8294                  (not gnus-tmp-false-parent)
8295                  (or gnus-tmp-new-adopts new-roots))
8296             (if gnus-tmp-new-adopts
8297                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8298                       thread (list (car gnus-tmp-new-adopts))
8299                       gnus-tmp-header (caar thread)
8300                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8301               (if new-roots
8302                   (setq thread (list (car new-roots))
8303                         gnus-tmp-header (caar thread)
8304                         new-roots (cdr new-roots))))
8305
8306           (if threads
8307               ;; If there are some threads, we do them before the
8308               ;; threads on the stack.
8309               (setq thread threads
8310                     gnus-tmp-header (caar thread))
8311             ;; There were no current threads, so we pop something off
8312             ;; the stack.
8313             (setq state (car stack)
8314                   gnus-tmp-level (car state)
8315                   thread (cdr state)
8316                   stack (cdr stack)
8317                   gnus-tmp-header (caar thread))))
8318
8319         (setq gnus-tmp-false-parent nil)
8320         (setq gnus-tmp-root-expunged nil)
8321         (setq thread-end nil)
8322
8323         (if (stringp gnus-tmp-header)
8324             ;; The header is a dummy root.
8325             (cond
8326              ((eq gnus-summary-make-false-root 'adopt)
8327               ;; We let the first article adopt the rest.
8328               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8329                                                (cddar thread)))
8330               (setq gnus-tmp-gathered
8331                     (nconc (mapcar
8332                             (lambda (h) (mail-header-number (car h)))
8333                             (cddar thread))
8334                            gnus-tmp-gathered))
8335               (setq thread (cons (list (caar thread)
8336                                        (cadar thread))
8337                                  (cdr thread)))
8338               (setq gnus-tmp-level -1
8339                     gnus-tmp-false-parent t))
8340              ((eq gnus-summary-make-false-root 'empty)
8341               ;; We print adopted articles with empty subject fields.
8342               (setq gnus-tmp-gathered
8343                     (nconc (mapcar
8344                             (lambda (h) (mail-header-number (car h)))
8345                             (cddar thread))
8346                            gnus-tmp-gathered))
8347               (setq gnus-tmp-level -1))
8348              ((eq gnus-summary-make-false-root 'dummy)
8349               ;; We remember that we probably want to output a dummy
8350               ;; root.
8351               (setq gnus-tmp-dummy-line gnus-tmp-header)
8352               (setq gnus-tmp-prev-subject gnus-tmp-header))
8353              (t
8354               ;; We do not make a root for the gathered
8355               ;; sub-threads at all.
8356               (setq gnus-tmp-level -1)))
8357
8358           (setq number (mail-header-number gnus-tmp-header)
8359                 subject (mail-header-subject gnus-tmp-header))
8360
8361           (cond
8362            ;; If the thread has changed subject, we might want to make
8363            ;; this subthread into a root.
8364            ((and (null gnus-thread-ignore-subject)
8365                  (not (zerop gnus-tmp-level))
8366                  gnus-tmp-prev-subject
8367                  (not (inline
8368                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8369             (setq new-roots (nconc new-roots (list (car thread)))
8370                   thread-end t
8371                   gnus-tmp-header nil))
8372            ;; If the article lies outside the current limit,
8373            ;; then we do not display it.
8374            ((and (not (memq number gnus-newsgroup-limit))
8375                  (not gnus-tmp-dummy-line))
8376             (setq gnus-tmp-gathered
8377                   (nconc (mapcar
8378                           (lambda (h) (mail-header-number (car h)))
8379                           (cdar thread))
8380                          gnus-tmp-gathered))
8381             (setq gnus-tmp-new-adopts (if (cdar thread)
8382                                           (append gnus-tmp-new-adopts
8383                                                   (cdar thread))
8384                                         gnus-tmp-new-adopts)
8385                   thread-end t
8386                   gnus-tmp-header nil)
8387             (when (zerop gnus-tmp-level)
8388               (setq gnus-tmp-root-expunged t)))
8389            ;; Perhaps this article is to be marked as read?
8390            ((and gnus-summary-mark-below
8391                  (< (or (cdr (assq number gnus-newsgroup-scored))
8392                         default-score)
8393                     gnus-summary-mark-below)
8394                  ;; Don't touch sparse articles.
8395                  (not (memq number gnus-newsgroup-sparse)))
8396             (setq gnus-newsgroup-unreads
8397                   (delq number gnus-newsgroup-unreads))
8398             (if gnus-newsgroup-auto-expire
8399                 (push number gnus-newsgroup-expirable)
8400               (push (cons number gnus-low-score-mark)
8401                     gnus-newsgroup-reads))))
8402
8403           (when gnus-tmp-header
8404             ;; We may have an old dummy line to output before this
8405             ;; article.
8406             (when gnus-tmp-dummy-line
8407               (gnus-summary-insert-dummy-line
8408                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8409               (setq gnus-tmp-dummy-line nil))
8410
8411             ;; Compute the mark.
8412             (setq
8413              gnus-tmp-unread
8414              (cond
8415               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8416               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8417               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8418               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8419               (t (or (cdr (assq number gnus-newsgroup-reads))
8420                      gnus-ancient-mark))))
8421
8422             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8423                                   gnus-tmp-header gnus-tmp-level)
8424                   gnus-newsgroup-data)
8425
8426             ;; Actually insert the line.
8427             (setq
8428              gnus-tmp-subject-or-nil
8429              (cond
8430               ((and gnus-thread-ignore-subject
8431                     gnus-tmp-prev-subject
8432                     (not (inline (gnus-subject-equal
8433                                   gnus-tmp-prev-subject subject))))
8434                subject)
8435               ((zerop gnus-tmp-level)
8436                (if (and (eq gnus-summary-make-false-root 'empty)
8437                         (memq number gnus-tmp-gathered)
8438                         gnus-tmp-prev-subject
8439                         (inline (gnus-subject-equal
8440                                  gnus-tmp-prev-subject subject)))
8441                    gnus-summary-same-subject
8442                  subject))
8443               (t gnus-summary-same-subject)))
8444             (if (and (eq gnus-summary-make-false-root 'adopt)
8445                      (= gnus-tmp-level 1)
8446                      (memq number gnus-tmp-gathered))
8447                 (setq gnus-tmp-opening-bracket ?\<
8448                       gnus-tmp-closing-bracket ?\>)
8449               (setq gnus-tmp-opening-bracket ?\[
8450                     gnus-tmp-closing-bracket ?\]))
8451             (setq
8452              gnus-tmp-indentation
8453              (aref gnus-thread-indent-array gnus-tmp-level)
8454              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8455              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8456                                 gnus-summary-default-score 0)
8457              gnus-tmp-score-char
8458              (if (or (null gnus-summary-default-score)
8459                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8460                          gnus-summary-zcore-fuzz)) ? 
8461                (if (< gnus-tmp-score gnus-summary-default-score)
8462                    gnus-score-below-mark gnus-score-over-mark))
8463              gnus-tmp-replied
8464              (cond ((memq number gnus-newsgroup-processable)
8465                     gnus-process-mark)
8466                    ((memq number gnus-newsgroup-cached)
8467                     gnus-cached-mark)
8468                    ((memq number gnus-newsgroup-replied)
8469                     gnus-replied-mark)
8470                    (t gnus-unread-mark))
8471              gnus-tmp-from (mail-header-from gnus-tmp-header)
8472              gnus-tmp-name
8473              (cond
8474               ((string-match "(.+)" gnus-tmp-from)
8475                (substring gnus-tmp-from
8476                           (1+ (match-beginning 0)) (1- (match-end 0))))
8477               ((string-match "<[^>]+> *$" gnus-tmp-from)
8478                (setq beg-match (match-beginning 0))
8479                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8480                         (substring gnus-tmp-from (1+ (match-beginning 0))
8481                                    (1- (match-end 0))))
8482                    (substring gnus-tmp-from 0 beg-match)))
8483               (t gnus-tmp-from)))
8484             (when (string= gnus-tmp-name "")
8485               (setq gnus-tmp-name gnus-tmp-from))
8486             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8487             (put-text-property
8488              (point)
8489              (progn (eval gnus-summary-line-format-spec) (point))
8490              'gnus-number number)
8491             (when gnus-visual-p
8492               (forward-line -1)
8493               (run-hooks 'gnus-summary-update-hook)
8494               (forward-line 1))
8495
8496             (setq gnus-tmp-prev-subject subject)))
8497
8498         (when (nth 1 thread)
8499           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8500         (incf gnus-tmp-level)
8501         (setq threads (if thread-end nil (cdar thread)))
8502         (unless threads
8503           (setq gnus-tmp-level 0)))))
8504   (gnus-message 7 "Generating summary...done"))
8505
8506 (defun gnus-summary-prepare-unthreaded (headers)
8507   "Generate an unthreaded summary buffer based on HEADERS."
8508   (let (header number mark)
8509
8510     (while headers
8511       ;; We may have to root out some bad articles...
8512       (when (memq (setq number (mail-header-number
8513                                 (setq header (pop headers))))
8514                   gnus-newsgroup-limit)
8515         ;; Mark article as read when it has a low score.
8516         (when (and gnus-summary-mark-below
8517                    (< (or (cdr (assq number gnus-newsgroup-scored))
8518                           gnus-summary-default-score 0)
8519                       gnus-summary-mark-below))
8520           (setq gnus-newsgroup-unreads
8521                 (delq number gnus-newsgroup-unreads))
8522           (if gnus-newsgroup-auto-expire
8523               (push number gnus-newsgroup-expirable)
8524             (push (cons number gnus-low-score-mark)
8525                   gnus-newsgroup-reads)))
8526
8527         (setq mark
8528               (cond
8529                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8530                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8531                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8532                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8533                (t (or (cdr (assq number gnus-newsgroup-reads))
8534                       gnus-ancient-mark))))
8535         (setq gnus-newsgroup-data
8536               (cons (gnus-data-make number mark (1+ (point)) header 0)
8537                     gnus-newsgroup-data))
8538         (gnus-summary-insert-line
8539          header 0 nil mark (memq number gnus-newsgroup-replied)
8540          (memq number gnus-newsgroup-expirable)
8541          (mail-header-subject header) nil
8542          (cdr (assq number gnus-newsgroup-scored))
8543          (memq number gnus-newsgroup-processable))))))
8544
8545 (defun gnus-select-newsgroup (group &optional read-all)
8546   "Select newsgroup GROUP.
8547 If READ-ALL is non-nil, all articles in the group are selected."
8548   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8549          (info (nth 2 entry))
8550          articles fetched-articles cached)
8551
8552     (or (gnus-check-server
8553          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8554         (error "Couldn't open server"))
8555
8556     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8557         (gnus-activate-group group) ; Or we can activate it...
8558         (progn ; Or we bug out.
8559           (when (equal major-mode 'gnus-summary-mode)
8560             (kill-buffer (current-buffer)))
8561           (error "Couldn't request group %s: %s"
8562                  group (gnus-status-message group))))
8563
8564     (setq gnus-newsgroup-name group)
8565     (setq gnus-newsgroup-unselected nil)
8566     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8567
8568     (and gnus-asynchronous
8569          (gnus-check-backend-function
8570           'request-asynchronous gnus-newsgroup-name)
8571          (setq gnus-newsgroup-async
8572                (gnus-request-asynchronous gnus-newsgroup-name)))
8573
8574     ;; Adjust and set lists of article marks.
8575     (when info
8576       (gnus-adjust-marked-articles info))
8577
8578     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8579     (when (gnus-virtual-group-p group)
8580       (setq cached gnus-newsgroup-cached))
8581
8582     (setq gnus-newsgroup-unreads
8583           (gnus-set-difference
8584            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8585            gnus-newsgroup-dormant))
8586
8587     (setq gnus-newsgroup-processable nil)
8588
8589     (setq articles (gnus-articles-to-read group read-all))
8590
8591     (cond
8592      ((null articles)
8593       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8594       'quit)
8595      ((eq articles 0) nil)
8596      (t
8597       ;; Init the dependencies hash table.
8598       (setq gnus-newsgroup-dependencies
8599             (gnus-make-hashtable (length articles)))
8600       ;; Retrieve the headers and read them in.
8601       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8602       (setq gnus-newsgroup-headers
8603             (if (eq 'nov
8604                     (setq gnus-headers-retrieved-by
8605                           (gnus-retrieve-headers
8606                            articles gnus-newsgroup-name
8607                            ;; We might want to fetch old headers, but
8608                            ;; not if there is only 1 article.
8609                            (and gnus-fetch-old-headers
8610                                 (or (and
8611                                      (not (eq gnus-fetch-old-headers 'some))
8612                                      (not (numberp gnus-fetch-old-headers)))
8613                                     (> (length articles) 1))))))
8614                 (gnus-get-newsgroup-headers-xover articles)
8615               (gnus-get-newsgroup-headers)))
8616       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8617
8618       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8619       (when cached
8620         (setq gnus-newsgroup-cached cached))
8621
8622       ;; Set the initial limit.
8623       (setq gnus-newsgroup-limit (copy-sequence articles))
8624       ;; Remove canceled articles from the list of unread articles.
8625       (setq gnus-newsgroup-unreads
8626             (gnus-set-sorted-intersection
8627              gnus-newsgroup-unreads
8628              (setq fetched-articles
8629                    (mapcar (lambda (headers) (mail-header-number headers))
8630                            gnus-newsgroup-headers))))
8631       ;; Removed marked articles that do not exist.
8632       (gnus-update-missing-marks
8633        (gnus-sorted-complement fetched-articles articles))
8634       ;; We might want to build some more threads first.
8635       (and gnus-fetch-old-headers
8636            (eq gnus-headers-retrieved-by 'nov)
8637            (gnus-build-old-threads))
8638       ;; Check whether auto-expire is to be done in this group.
8639       (setq gnus-newsgroup-auto-expire
8640             (gnus-group-auto-expirable-p group))
8641       ;; Set up the article buffer now, if necessary.
8642       (unless gnus-single-article-buffer
8643         (gnus-article-setup-buffer))
8644       ;; First and last article in this newsgroup.
8645       (and gnus-newsgroup-headers
8646            (setq gnus-newsgroup-begin
8647                  (mail-header-number (car gnus-newsgroup-headers)))
8648            (setq gnus-newsgroup-end
8649                  (mail-header-number
8650                   (gnus-last-element gnus-newsgroup-headers))))
8651       (setq gnus-reffed-article-number -1)
8652       ;; GROUP is successfully selected.
8653       (or gnus-newsgroup-headers t)))))
8654
8655 (defun gnus-articles-to-read (group read-all)
8656   ;; Find out what articles the user wants to read.
8657   (let* ((articles
8658           ;; Select all articles if `read-all' is non-nil, or if there
8659           ;; are no unread articles.
8660           (if (or read-all
8661                   (and (zerop (length gnus-newsgroup-marked))
8662                        (zerop (length gnus-newsgroup-unreads))))
8663               (gnus-uncompress-range (gnus-active group))
8664             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8665                           (copy-sequence gnus-newsgroup-unreads))
8666                   '<)))
8667          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8668          (scored (length scored-list))
8669          (number (length articles))
8670          (marked (+ (length gnus-newsgroup-marked)
8671                     (length gnus-newsgroup-dormant)))
8672          (select
8673           (cond
8674            ((numberp read-all)
8675             read-all)
8676            (t
8677             (condition-case ()
8678                 (cond
8679                  ((and (or (<= scored marked) (= scored number))
8680                        (numberp gnus-large-newsgroup)
8681                        (> number gnus-large-newsgroup))
8682                   (let ((input
8683                          (read-string
8684                           (format
8685                            "How many articles from %s (default %d): "
8686                            gnus-newsgroup-name number))))
8687                     (if (string-match "^[ \t]*$" input) number input)))
8688                  ((and (> scored marked) (< scored number))
8689                   (let ((input
8690                          (read-string
8691                           (format "%s %s (%d scored, %d total): "
8692                                   "How many articles from"
8693                                   group scored number))))
8694                     (if (string-match "^[ \t]*$" input)
8695                         number input)))
8696                  (t number))
8697               (quit nil))))))
8698     (setq select (if (stringp select) (string-to-number select) select))
8699     (if (or (null select) (zerop select))
8700         select
8701       (if (and (not (zerop scored)) (<= (abs select) scored))
8702           (progn
8703             (setq articles (sort scored-list '<))
8704             (setq number (length articles)))
8705         (setq articles (copy-sequence articles)))
8706
8707       (if (< (abs select) number)
8708           (if (< select 0)
8709               ;; Select the N oldest articles.
8710               (setcdr (nthcdr (1- (abs select)) articles) nil)
8711             ;; Select the N most recent articles.
8712             (setq articles (nthcdr (- number select) articles))))
8713       (setq gnus-newsgroup-unselected
8714             (gnus-sorted-intersection
8715              gnus-newsgroup-unreads
8716              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8717       articles)))
8718
8719 (defun gnus-killed-articles (killed articles)
8720   (let (out)
8721     (while articles
8722       (if (inline (gnus-member-of-range (car articles) killed))
8723           (setq out (cons (car articles) out)))
8724       (setq articles (cdr articles)))
8725     out))
8726
8727 (defun gnus-uncompress-marks (marks)
8728   "Uncompress the mark ranges in MARKS."
8729   (let ((uncompressed '(score bookmark))
8730         out)
8731     (while marks
8732       (if (memq (caar marks) uncompressed)
8733           (push (car marks) out)
8734         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8735       (setq marks (cdr marks)))
8736     out))
8737
8738 (defun gnus-adjust-marked-articles (info)
8739   "Set all article lists and remove all marks that are no longer legal."
8740   (let* ((marked-lists (gnus-info-marks info))
8741          (active (gnus-active (gnus-info-group info)))
8742          (min (car active))
8743          (max (cdr active))
8744          (types gnus-article-mark-lists)
8745          (uncompressed '(score bookmark))
8746          marks var articles article mark)
8747
8748     (while marked-lists
8749       (setq marks (pop marked-lists))
8750       (set (setq var (intern (format "gnus-newsgroup-%s"
8751                                      (car (rassq (setq mark (car marks))
8752                                                  types)))))
8753            (if (memq (car marks) uncompressed) (cdr marks)
8754              (gnus-uncompress-range (cdr marks))))
8755
8756       (setq articles (symbol-value var))
8757
8758       ;; All articles have to be subsets of the active articles.
8759       (cond
8760        ;; Adjust "simple" lists.
8761        ((memq mark '(tick dormant expirable reply killed save))
8762         (while articles
8763           (when (or (< (setq article (pop articles)) min) (> article max))
8764             (set var (delq article (symbol-value var))))))
8765        ;; Adjust assocs.
8766        ((memq mark '(score bookmark))
8767         (while articles
8768           (when (or (< (car (setq article (pop articles))) min)
8769                     (> (car article) max))
8770             (set var (delq article (symbol-value var))))))))))
8771
8772 (defun gnus-update-missing-marks (missing)
8773   "Go through the list of MISSING articles and remove them mark lists."
8774   (when missing
8775     (let ((types gnus-article-mark-lists)
8776           var m)
8777       ;; Go through all types.
8778       (while types
8779         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8780         (when (symbol-value var)
8781           ;; This list has articles.  So we delete all missing articles
8782           ;; from it.
8783           (setq m missing)
8784           (while m
8785             (set var (delq (pop m) (symbol-value var)))))))))
8786
8787 (defun gnus-update-marks ()
8788   "Enter the various lists of marked articles into the newsgroup info list."
8789   (let ((types gnus-article-mark-lists)
8790         (info (gnus-get-info gnus-newsgroup-name))
8791         (uncompressed '(score bookmark killed))
8792         type list newmarked symbol)
8793     (when info
8794       ;; Add all marks lists that are non-nil to the list of marks lists.
8795       (while types
8796         (setq type (pop types))
8797         (when (setq list (symbol-value
8798                           (setq symbol
8799                                 (intern (format "gnus-newsgroup-%s"
8800                                                 (car type))))))
8801           (push (cons (cdr type)
8802                       (if (memq (cdr type) uncompressed) list
8803                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8804                 newmarked)))
8805
8806       ;; Enter these new marks into the info of the group.
8807       (if (nthcdr 3 info)
8808           (setcar (nthcdr 3 info) newmarked)
8809         ;; Add the marks lists to the end of the info.
8810         (when newmarked
8811           (setcdr (nthcdr 2 info) (list newmarked))))
8812
8813       ;; Cut off the end of the info if there's nothing else there.
8814       (let ((i 5))
8815         (while (and (> i 2)
8816                     (not (nth i info)))
8817           (when (nthcdr (decf i) info)
8818             (setcdr (nthcdr i info) nil)))))))
8819
8820 (defun gnus-add-marked-articles (group type articles &optional info force)
8821   ;; Add ARTICLES of TYPE to the info of GROUP.
8822   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8823   ;; add, but replace marked articles of TYPE with ARTICLES.
8824   (let ((info (or info (gnus-get-info group)))
8825         (uncompressed '(score bookmark killed))
8826         marked m)
8827     (or (not info)
8828         (and (not (setq marked (nthcdr 3 info)))
8829              (or (null articles)
8830                  (setcdr (nthcdr 2 info)
8831                          (list (list (cons type (gnus-compress-sequence
8832                                                  articles t)))))))
8833         (and (not (setq m (assq type (car marked))))
8834              (or (null articles)
8835                  (setcar marked
8836                          (cons (cons type (gnus-compress-sequence articles t) )
8837                                (car marked)))))
8838         (if force
8839             (if (null articles)
8840                 (setcar (nthcdr 3 info)
8841                         (delq (assq type (car marked)) (car marked)))
8842               (setcdr m (gnus-compress-sequence articles t)))
8843           (setcdr m (gnus-compress-sequence
8844                      (sort (nconc (gnus-uncompress-range (cdr m))
8845                                   (copy-sequence articles)) '<) t))))))
8846
8847 (defun gnus-set-mode-line (where)
8848   "This function sets the mode line of the article or summary buffers.
8849 If WHERE is `summary', the summary mode line format will be used."
8850   ;; Is this mode line one we keep updated?
8851   (when (memq where gnus-updated-mode-lines)
8852     (let (mode-string)
8853       (save-excursion
8854         ;; We evaluate this in the summary buffer since these
8855         ;; variables are buffer-local to that buffer.
8856         (set-buffer gnus-summary-buffer)
8857         ;; We bind all these variables that are used in the `eval' form
8858         ;; below.
8859         (let* ((mformat (symbol-value
8860                          (intern
8861                           (format "gnus-%s-mode-line-format-spec" where))))
8862                (gnus-tmp-group-name gnus-newsgroup-name)
8863                (gnus-tmp-article-number (or gnus-current-article 0))
8864                (gnus-tmp-unread gnus-newsgroup-unreads)
8865                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8866                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8867                (gnus-tmp-unread-and-unselected
8868                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8869                             (zerop gnus-tmp-unselected)) "")
8870                       ((zerop gnus-tmp-unselected)
8871                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8872                       (t (format "{%d(+%d) more}"
8873                                  gnus-tmp-unread-and-unticked
8874                                  gnus-tmp-unselected))))
8875                (gnus-tmp-subject
8876                 (if (and gnus-current-headers
8877                          (vectorp gnus-current-headers))
8878                     (mail-header-subject gnus-current-headers) ""))
8879                max-len
8880                gnus-tmp-header);; passed as argument to any user-format-funcs
8881           (setq mode-string (eval mformat))
8882           (setq max-len (max 4 (if gnus-mode-non-string-length
8883                                    (- (window-width)
8884                                       gnus-mode-non-string-length)
8885                                  (length mode-string))))
8886           ;; We might have to chop a bit of the string off...
8887           (when (> (length mode-string) max-len)
8888             (setq mode-string
8889                   (concat (gnus-truncate-string mode-string (- max-len 3))
8890                           "...")))
8891           ;; Pad the mode string a bit.
8892           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8893       ;; Update the mode line.
8894       (setq mode-line-buffer-identification (list mode-string))
8895       (set-buffer-modified-p t))))
8896
8897 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8898   "Go through the HEADERS list and add all Xrefs to a hash table.
8899 The resulting hash table is returned, or nil if no Xrefs were found."
8900   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8901          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8902          (xref-hashtb (make-vector 63 0))
8903          start group entry number xrefs header)
8904     (while headers
8905       (setq header (pop headers))
8906       (when (and (setq xrefs (mail-header-xref header))
8907                  (not (memq (setq number (mail-header-number header))
8908                             unreads)))
8909         (setq start 0)
8910         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8911           (setq start (match-end 0))
8912           (setq group (if prefix
8913                           (concat prefix (substring xrefs (match-beginning 1)
8914                                                     (match-end 1)))
8915                         (substring xrefs (match-beginning 1) (match-end 1))))
8916           (setq number
8917                 (string-to-int (substring xrefs (match-beginning 2)
8918                                           (match-end 2))))
8919           (if (setq entry (gnus-gethash group xref-hashtb))
8920               (setcdr entry (cons number (cdr entry)))
8921             (gnus-sethash group (cons number nil) xref-hashtb)))))
8922     (and start xref-hashtb)))
8923
8924 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8925   "Look through all the headers and mark the Xrefs as read."
8926   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8927         name entry info xref-hashtb idlist method nth4)
8928     (save-excursion
8929       (set-buffer gnus-group-buffer)
8930       (when (setq xref-hashtb
8931                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8932         (mapatoms
8933          (lambda (group)
8934            (unless (string= from-newsgroup (setq name (symbol-name group)))
8935              (setq idlist (symbol-value group))
8936              ;; Dead groups are not updated.
8937              (and (prog1
8938                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8939                             info (nth 2 entry))
8940                     (if (stringp (setq nth4 (gnus-info-method info)))
8941                         (setq nth4 (gnus-server-to-method nth4))))
8942                   ;; Only do the xrefs if the group has the same
8943                   ;; select method as the group we have just read.
8944                   (or (gnus-methods-equal-p
8945                        nth4 (gnus-find-method-for-group from-newsgroup))
8946                       virtual
8947                       (equal nth4 (setq method (gnus-find-method-for-group
8948                                                 from-newsgroup)))
8949                       (and (equal (car nth4) (car method))
8950                            (equal (nth 1 nth4) (nth 1 method))))
8951                   gnus-use-cross-reference
8952                   (or (not (eq gnus-use-cross-reference t))
8953                       virtual
8954                       ;; Only do cross-references on subscribed
8955                       ;; groups, if that is what is wanted.
8956                       (<= (gnus-info-level info) gnus-level-subscribed))
8957                   (gnus-group-make-articles-read name idlist))))
8958          xref-hashtb)))))
8959
8960 (defun gnus-group-make-articles-read (group articles)
8961   (let* ((num 0)
8962          (entry (gnus-gethash group gnus-newsrc-hashtb))
8963          (info (nth 2 entry))
8964          (active (gnus-active group))
8965          range)
8966     ;; First peel off all illegal article numbers.
8967     (if active
8968         (let ((ids articles)
8969               id first)
8970           (while ids
8971             (setq id (car ids))
8972             (if (and first (> id (cdr active)))
8973                 (progn
8974                   ;; We'll end up in this situation in one particular
8975                   ;; obscure situation.  If you re-scan a group and get
8976                   ;; a new article that is cross-posted to a different
8977                   ;; group that has not been re-scanned, you might get
8978                   ;; crossposted article that has a higher number than
8979                   ;; Gnus believes possible.  So we re-activate this
8980                   ;; group as well.  This might mean doing the
8981                   ;; crossposting thingy will *increase* the number
8982                   ;; of articles in some groups.  Tsk, tsk.
8983                   (setq active (or (gnus-activate-group group) active))))
8984             (if (or (> id (cdr active))
8985                     (< id (car active)))
8986                 (setq articles (delq id articles)))
8987             (setq ids (cdr ids)))))
8988     ;; If the read list is nil, we init it.
8989     (and active
8990          (null (gnus-info-read info))
8991          (> (car active) 1)
8992          (gnus-info-set-read info (cons 1 (1- (car active)))))
8993     ;; Then we add the read articles to the range.
8994     (gnus-info-set-read
8995      info
8996      (setq range
8997            (gnus-add-to-range
8998             (gnus-info-read info) (setq articles (sort articles '<)))))
8999     ;; Then we have to re-compute how many unread
9000     ;; articles there are in this group.
9001     (if active
9002         (progn
9003           (cond
9004            ((not range)
9005             (setq num (- (1+ (cdr active)) (car active))))
9006            ((not (listp (cdr range)))
9007             (setq num (- (cdr active) (- (1+ (cdr range))
9008                                          (car range)))))
9009            (t
9010             (while range
9011               (if (numberp (car range))
9012                   (setq num (1+ num))
9013                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9014               (setq range (cdr range)))
9015             (setq num (- (cdr active) num))))
9016           ;; Update the number of unread articles.
9017           (setcar entry num)
9018           ;; Update the group buffer.
9019           (gnus-group-update-group group t)))))
9020
9021 (defun gnus-methods-equal-p (m1 m2)
9022   (let ((m1 (or m1 gnus-select-method))
9023         (m2 (or m2 gnus-select-method)))
9024     (or (equal m1 m2)
9025         (and (eq (car m1) (car m2))
9026              (or (not (memq 'address (assoc (symbol-name (car m1))
9027                                             gnus-valid-select-methods)))
9028                  (equal (nth 1 m1) (nth 1 m2)))))))
9029
9030 (defsubst gnus-header-value ()
9031   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9032
9033 (defvar gnus-newsgroup-none-id 0)
9034
9035 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9036   (let ((cur nntp-server-buffer)
9037         (dependencies
9038          (or dependencies
9039              (save-excursion (set-buffer gnus-summary-buffer)
9040                              gnus-newsgroup-dependencies)))
9041         headers id id-dep ref-dep end ref)
9042     (save-excursion
9043       (set-buffer nntp-server-buffer)
9044       (run-hooks 'gnus-parse-headers-hook)
9045       (let ((case-fold-search t)
9046             in-reply-to header p lines)
9047         (goto-char (point-min))
9048         ;; Search to the beginning of the next header.  Error messages
9049         ;; do not begin with 2 or 3.
9050         (while (re-search-forward "^[23][0-9]+ " nil t)
9051           (setq id nil
9052                 ref nil)
9053           ;; This implementation of this function, with nine
9054           ;; search-forwards instead of the one re-search-forward and
9055           ;; a case (which basically was the old function) is actually
9056           ;; about twice as fast, even though it looks messier.  You
9057           ;; can't have everything, I guess.  Speed and elegance
9058           ;; doesn't always go hand in hand.
9059           (setq
9060            header
9061            (vector
9062             ;; Number.
9063             (prog1
9064                 (read cur)
9065               (end-of-line)
9066               (setq p (point))
9067               (narrow-to-region (point)
9068                                 (or (and (search-forward "\n.\n" nil t)
9069                                          (- (point) 2))
9070                                     (point))))
9071             ;; Subject.
9072             (progn
9073               (goto-char p)
9074               (if (search-forward "\nsubject: " nil t)
9075                   (gnus-header-value) "(none)"))
9076             ;; From.
9077             (progn
9078               (goto-char p)
9079               (if (search-forward "\nfrom: " nil t)
9080                   (gnus-header-value) "(nobody)"))
9081             ;; Date.
9082             (progn
9083               (goto-char p)
9084               (if (search-forward "\ndate: " nil t)
9085                   (gnus-header-value) ""))
9086             ;; Message-ID.
9087             (progn
9088               (goto-char p)
9089               (if (search-forward "\nmessage-id: " nil t)
9090                   (setq id (gnus-header-value))
9091                 ;; If there was no message-id, we just fake one to make
9092                 ;; subsequent routines simpler.
9093                 (setq id (concat "none+"
9094                                  (int-to-string
9095                                   (setq gnus-newsgroup-none-id
9096                                         (1+ gnus-newsgroup-none-id)))))))
9097             ;; References.
9098             (progn
9099               (goto-char p)
9100               (if (search-forward "\nreferences: " nil t)
9101                   (prog1
9102                       (gnus-header-value)
9103                     (setq end (match-end 0))
9104                     (save-excursion
9105                       (setq ref
9106                             (buffer-substring
9107                              (progn
9108                                (end-of-line)
9109                                (search-backward ">" end t)
9110                                (1+ (point)))
9111                              (progn
9112                                (search-backward "<" end t)
9113                                (point))))))
9114                 ;; Get the references from the in-reply-to header if there
9115                 ;; were no references and the in-reply-to header looks
9116                 ;; promising.
9117                 (if (and (search-forward "\nin-reply-to: " nil t)
9118                          (setq in-reply-to (gnus-header-value))
9119                          (string-match "<[^>]+>" in-reply-to))
9120                     (setq ref (substring in-reply-to (match-beginning 0)
9121                                          (match-end 0)))
9122                   (setq ref ""))))
9123             ;; Chars.
9124             0
9125             ;; Lines.
9126             (progn
9127               (goto-char p)
9128               (if (search-forward "\nlines: " nil t)
9129                   (if (numberp (setq lines (read cur)))
9130                       lines 0)
9131                 0))
9132             ;; Xref.
9133             (progn
9134               (goto-char p)
9135               (and (search-forward "\nxref: " nil t)
9136                    (gnus-header-value)))))
9137           ;; We do the threading while we read the headers.  The
9138           ;; message-id and the last reference are both entered into
9139           ;; the same hash table.  Some tippy-toeing around has to be
9140           ;; done in case an article has arrived before the article
9141           ;; which it refers to.
9142           (if (boundp (setq id-dep (intern id dependencies)))
9143               (if (and (car (symbol-value id-dep))
9144                        (not force-new))
9145                   ;; An article with this Message-ID has already
9146                   ;; been seen, so we ignore this one, except we add
9147                   ;; any additional Xrefs (in case the two articles
9148                   ;; came from different servers).
9149                   (progn
9150                     (mail-header-set-xref
9151                      (car (symbol-value id-dep))
9152                      (concat (or (mail-header-xref
9153                                   (car (symbol-value id-dep))) "")
9154                              (or (mail-header-xref header) "")))
9155                     (setq header nil))
9156                 (setcar (symbol-value id-dep) header))
9157             (set id-dep (list header)))
9158           (when header
9159             (if (boundp (setq ref-dep (intern ref dependencies)))
9160                 (setcdr (symbol-value ref-dep)
9161                         (nconc (cdr (symbol-value ref-dep))
9162                                (list (symbol-value id-dep))))
9163               (set ref-dep (list nil (symbol-value id-dep))))
9164             (setq headers (cons header headers)))
9165           (goto-char (point-max))
9166           (widen))
9167         (nreverse headers)))))
9168
9169 ;; The following macros and functions were written by Felix Lee
9170 ;; <flee@cse.psu.edu>.
9171
9172 (defmacro gnus-nov-read-integer ()
9173   '(prog1
9174        (if (= (following-char) ?\t)
9175            0
9176          (let ((num (condition-case nil (read buffer) (error nil))))
9177            (if (numberp num) num 0)))
9178      (or (eobp) (forward-char 1))))
9179
9180 (defmacro gnus-nov-skip-field ()
9181   '(search-forward "\t" eol 'move))
9182
9183 (defmacro gnus-nov-field ()
9184   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9185
9186 ;; Goes through the xover lines and returns a list of vectors
9187 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9188                                                   force-new dependencies)
9189   "Parse the news overview data in the server buffer, and return a
9190 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9191   ;; Get the Xref when the users reads the articles since most/some
9192   ;; NNTP servers do not include Xrefs when using XOVER.
9193   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9194   (let ((cur nntp-server-buffer)
9195         (dependencies (or dependencies gnus-newsgroup-dependencies))
9196         number headers header)
9197     (save-excursion
9198       (set-buffer nntp-server-buffer)
9199       ;; Allow the user to mangle the headers before parsing them.
9200       (run-hooks 'gnus-parse-headers-hook)
9201       (goto-char (point-min))
9202       (while (and sequence (not (eobp)))
9203         (setq number (read cur))
9204         (while (and sequence (< (car sequence) number))
9205           (setq sequence (cdr sequence)))
9206         (and sequence
9207              (eq number (car sequence))
9208              (progn
9209                (setq sequence (cdr sequence))
9210                (if (setq header
9211                          (inline (gnus-nov-parse-line
9212                                   number dependencies force-new)))
9213                    (setq headers (cons header headers)))))
9214         (forward-line 1))
9215       (setq headers (nreverse headers)))
9216     headers))
9217
9218 ;; This function has to be called with point after the article number
9219 ;; on the beginning of the line.
9220 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9221   (let ((none 0)
9222         (eol (gnus-point-at-eol))
9223         (buffer (current-buffer))
9224         header ref id id-dep ref-dep)
9225
9226     ;; overview: [num subject from date id refs chars lines misc]
9227     (narrow-to-region (point) eol)
9228     (or (eobp) (forward-char))
9229
9230     (condition-case nil
9231         (setq header
9232               (vector
9233                number                   ; number
9234                (gnus-nov-field)         ; subject
9235                (gnus-nov-field)         ; from
9236                (gnus-nov-field)         ; date
9237                (setq id (or (gnus-nov-field)
9238                             (concat "none+"
9239                                     (int-to-string
9240                                      (setq none (1+ none)))))) ; id
9241                (progn
9242                  (save-excursion
9243                    (let ((beg (point)))
9244                      (search-forward "\t" eol)
9245                      (if (search-backward ">" beg t)
9246                          (setq ref
9247                                (buffer-substring
9248                                 (1+ (point))
9249                                 (search-backward "<" beg t)))
9250                        (setq ref nil))))
9251                  (gnus-nov-field))      ; refs
9252                (gnus-nov-read-integer)  ; chars
9253                (gnus-nov-read-integer)  ; lines
9254                (if (= (following-char) ?\n)
9255                    nil
9256                  (gnus-nov-field))      ; misc
9257                ))
9258       (error (progn
9259                (gnus-error 4 "Strange nov line")
9260                (setq header nil)
9261                (goto-char eol))))
9262
9263     (widen)
9264
9265     ;; We build the thread tree.
9266     (when header
9267       (if (boundp (setq id-dep (intern id dependencies)))
9268           (if (and (car (symbol-value id-dep))
9269                    (not force-new))
9270               ;; An article with this Message-ID has already been seen,
9271               ;; so we ignore this one, except we add any additional
9272               ;; Xrefs (in case the two articles came from different
9273               ;; servers.
9274               (progn
9275                 (mail-header-set-xref
9276                  (car (symbol-value id-dep))
9277                  (concat (or (mail-header-xref
9278                               (car (symbol-value id-dep))) "")
9279                          (or (mail-header-xref header) "")))
9280                 (setq header nil))
9281             (setcar (symbol-value id-dep) header))
9282         (set id-dep (list header))))
9283     (if header
9284         (progn
9285           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9286               (setcdr (symbol-value ref-dep)
9287                       (nconc (cdr (symbol-value ref-dep))
9288                              (list (symbol-value id-dep))))
9289             (set ref-dep (list nil (symbol-value id-dep))))))
9290     header))
9291
9292 (defun gnus-article-get-xrefs ()
9293   "Fill in the Xref value in `gnus-current-headers', if necessary.
9294 This is meant to be called in `gnus-article-internal-prepare-hook'."
9295   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9296                                  gnus-current-headers)))
9297     (or (not gnus-use-cross-reference)
9298         (not headers)
9299         (and (mail-header-xref headers)
9300              (not (string= (mail-header-xref headers) "")))
9301         (let ((case-fold-search t)
9302               xref)
9303           (save-restriction
9304             (nnheader-narrow-to-headers)
9305             (goto-char (point-min))
9306             (if (or (and (eq (downcase (following-char)) ?x)
9307                          (looking-at "Xref:"))
9308                     (search-forward "\nXref:" nil t))
9309                 (progn
9310                   (goto-char (1+ (match-end 0)))
9311                   (setq xref (buffer-substring (point)
9312                                                (progn (end-of-line) (point))))
9313                   (mail-header-set-xref headers xref))))))))
9314
9315 (defun gnus-summary-insert-subject (id &optional old-header)
9316   "Find article ID and insert the summary line for that article."
9317   (let ((header (gnus-read-header id))
9318         (number (and (numberp id) id))
9319         pos)
9320     (when header
9321       ;; Rebuild the thread that this article is part of and go to the
9322       ;; article we have fetched.
9323       (when old-header
9324         (when (setq pos (text-property-any
9325                          (point-min) (point-max) 'gnus-number 
9326                          (mail-header-number old-header)))
9327           (goto-char pos)
9328           (gnus-delete-line)
9329           (gnus-data-remove (mail-header-number old-header))))
9330       (when old-header
9331         (mail-header-set-number header (mail-header-number old-header)))
9332       (setq gnus-newsgroup-sparse
9333             (delq (mail-header-number header) gnus-newsgroup-sparse))
9334       (gnus-rebuild-thread (mail-header-id header))
9335       (gnus-summary-goto-subject (setq number (mail-header-number header))
9336                                  nil t))
9337     (when (and (numberp number)
9338                (> number 0))
9339       ;; We have to update the boundaries even if we can't fetch the
9340       ;; article if ID is a number -- so that the next `P' or `N'
9341       ;; command will fetch the previous (or next) article even
9342       ;; if the one we tried to fetch this time has been canceled.
9343       (and (> number gnus-newsgroup-end)
9344            (setq gnus-newsgroup-end number))
9345       (and (< number gnus-newsgroup-begin)
9346            (setq gnus-newsgroup-begin number))
9347       (setq gnus-newsgroup-unselected
9348             (delq number gnus-newsgroup-unselected)))
9349     ;; Report back a success?
9350     (and header (mail-header-number header))))
9351
9352 (defun gnus-summary-work-articles (n)
9353   "Return a list of articles to be worked upon.  The prefix argument,
9354 the list of process marked articles, and the current article will be
9355 taken into consideration."
9356   (cond
9357    ((and n (numberp n))
9358     ;; A numerical prefix has been given.
9359     (let ((backward (< n 0))
9360           (n (abs n))
9361           articles article)
9362       (save-excursion
9363         (while
9364             (and (> n 0)
9365                  (push (setq article (gnus-summary-article-number))
9366                        articles)
9367                  (if backward
9368                      (gnus-summary-find-prev nil article)
9369                    (gnus-summary-find-next nil article)))
9370           (decf n)))
9371       (nreverse articles)))
9372    ((and (boundp 'transient-mark-mode)
9373          transient-mark-mode
9374          mark-active)
9375     ;; Work on the region between point and mark.
9376     (let ((max (max (point) (mark)))
9377           articles article)
9378       (save-excursion
9379         (goto-char (min (point) (mark)))
9380         (while
9381             (and
9382              (push (setq article (gnus-summary-article-number)) articles)
9383              (gnus-summary-find-next nil article)
9384              (< (point) max)))
9385         (nreverse articles))))
9386    (gnus-newsgroup-processable
9387     ;; There are process-marked articles present.
9388     (reverse gnus-newsgroup-processable))
9389    (t
9390     ;; Just return the current article.
9391     (list (gnus-summary-article-number)))))
9392
9393 (defun gnus-summary-search-group (&optional backward use-level)
9394   "Search for next unread newsgroup.
9395 If optional argument BACKWARD is non-nil, search backward instead."
9396   (save-excursion
9397     (set-buffer gnus-group-buffer)
9398     (if (gnus-group-search-forward
9399          backward nil (if use-level (gnus-group-group-level) nil))
9400         (gnus-group-group-name))))
9401
9402 (defun gnus-summary-best-group (&optional exclude-group)
9403   "Find the name of the best unread group.
9404 If EXCLUDE-GROUP, do not go to this group."
9405   (save-excursion
9406     (set-buffer gnus-group-buffer)
9407     (save-excursion
9408       (gnus-group-best-unread-group exclude-group))))
9409
9410 (defun gnus-summary-find-next (&optional unread article backward)
9411   (if backward (gnus-summary-find-prev)
9412     (let* ((dummy (gnus-summary-article-intangible-p))
9413            (article (or article (gnus-summary-article-number)))
9414            (arts (gnus-data-find-list article))
9415            result)
9416       (when (and (not dummy)
9417                  (or (not gnus-summary-check-current)
9418                      (not unread)
9419                      (not (gnus-data-unread-p (car arts)))))
9420         (setq arts (cdr arts)))
9421       (when (setq result
9422                   (if unread
9423                       (progn
9424                         (while arts
9425                           (when (gnus-data-unread-p (car arts))
9426                             (setq result (car arts)
9427                                   arts nil))
9428                           (setq arts (cdr arts)))
9429                         result)
9430                     (car arts)))
9431         (goto-char (gnus-data-pos result))
9432         (gnus-data-number result)))))
9433
9434 (defun gnus-summary-find-prev (&optional unread article)
9435   (let* ((article (or article (gnus-summary-article-number)))
9436          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9437          result)
9438     (when (or (not gnus-summary-check-current)
9439               (not unread)
9440               (not (gnus-data-unread-p (car arts))))
9441       (setq arts (cdr arts)))
9442     (if (setq result
9443               (if unread
9444                   (progn
9445                     (while arts
9446                       (and (gnus-data-unread-p (car arts))
9447                            (setq result (car arts)
9448                                  arts nil))
9449                       (setq arts (cdr arts)))
9450                     result)
9451                 (car arts)))
9452         (progn
9453           (goto-char (gnus-data-pos result))
9454           (gnus-data-number result)))))
9455
9456 (defun gnus-summary-find-subject (subject &optional unread backward article)
9457   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9458          (article (or article (gnus-summary-article-number)))
9459          (articles (gnus-data-list backward))
9460          (arts (gnus-data-find-list article articles))
9461          result)
9462     (when (or (not gnus-summary-check-current)
9463               (not unread)
9464               (not (gnus-data-unread-p (car arts))))
9465       (setq arts (cdr arts)))
9466     (while arts
9467       (and (or (not unread)
9468                (gnus-data-unread-p (car arts)))
9469            (vectorp (gnus-data-header (car arts)))
9470            (gnus-subject-equal
9471             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9472            (setq result (car arts)
9473                  arts nil))
9474       (setq arts (cdr arts)))
9475     (and result
9476          (goto-char (gnus-data-pos result))
9477          (gnus-data-number result))))
9478
9479 (defun gnus-summary-search-forward (&optional unread subject backward)
9480   "Search forward for an article.
9481 If UNREAD, look for unread articles.  If SUBJECT, look for
9482 articles with that subject.  If BACKWARD, search backward instead."
9483   (cond (subject (gnus-summary-find-subject subject unread backward))
9484         (backward (gnus-summary-find-prev unread))
9485         (t (gnus-summary-find-next unread))))
9486
9487 (defun gnus-recenter (&optional n)
9488   "Center point in window and redisplay frame.
9489 Also do horizontal recentering."
9490   (interactive "P")
9491   (when (and gnus-auto-center-summary
9492              (not (eq gnus-auto-center-summary 'vertical)))
9493     (gnus-horizontal-recenter))
9494   (recenter n))
9495
9496 (defun gnus-summary-recenter ()
9497   "Center point in the summary window.
9498 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9499 displayed, no centering will be performed."
9500   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9501   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9502   (let* ((top (cond ((< (window-height) 4) 0)
9503                     ((< (window-height) 7) 1)
9504                     (t 2)))
9505          (height (1- (window-height)))
9506          (bottom (save-excursion (goto-char (point-max))
9507                                  (forward-line (- height))
9508                                  (point)))
9509          (window (get-buffer-window (current-buffer))))
9510     ;; The user has to want it.
9511     (when gnus-auto-center-summary
9512       (when (get-buffer-window gnus-article-buffer)
9513        ;; Only do recentering when the article buffer is displayed,
9514        ;; Set the window start to either `bottom', which is the biggest
9515        ;; possible valid number, or the second line from the top,
9516        ;; whichever is the least.
9517        (set-window-start
9518         window (min bottom (save-excursion 
9519                              (forward-line (- top)) (point)))))
9520       ;; Do horizontal recentering while we're at it.
9521       (when (and (get-buffer-window (current-buffer) t)
9522                  (not (eq gnus-auto-center-summary 'vertical)))
9523         (let ((selected (selected-window)))
9524           (select-window (get-buffer-window (current-buffer) t))
9525           (gnus-summary-position-point)
9526           (gnus-horizontal-recenter)
9527           (select-window selected))))))
9528
9529 (defun gnus-horizontal-recenter ()
9530   "Recenter the current buffer horizontally."
9531   (if (< (current-column) (/ (window-width) 2))
9532       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9533     (let* ((orig (point))
9534            (end (window-end (get-buffer-window (current-buffer) t)))
9535            (max 0))
9536       ;; Find the longest line currently displayed in the window.
9537       (goto-char (window-start))
9538       (while (and (not (eobp)) 
9539                   (< (point) end))
9540         (end-of-line)
9541         (setq max (max max (current-column)))
9542         (forward-line 1))
9543       (goto-char orig)
9544       ;; Scroll horizontally to center (sort of) the point.
9545       (if (> max (window-width))
9546           (set-window-hscroll 
9547            (get-buffer-window (current-buffer) t)
9548            (min (- (current-column) (/ (window-width) 3))
9549                 (+ 2 (- max (window-width)))))
9550         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9551       max)))
9552
9553 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9554 (defun gnus-short-group-name (group &optional levels)
9555   "Collapse GROUP name LEVELS."
9556   (let* ((name "") 
9557          (foreign "")
9558          (depth 0) 
9559          (skip 1)
9560          (levels (or levels
9561                      (progn
9562                        (while (string-match "\\." group skip)
9563                          (setq skip (match-end 0)
9564                                depth (+ depth 1)))
9565                        depth))))
9566     (if (string-match ":" group)
9567         (setq foreign (substring group 0 (match-end 0))
9568               group (substring group (match-end 0))))
9569     (while group
9570       (if (and (string-match "\\." group)
9571                (> levels (- gnus-group-uncollapsed-levels 1)))
9572           (setq name (concat name (substring group 0 1))
9573                 group (substring group (match-end 0))
9574                 levels (- levels 1)
9575                 name (concat name "."))
9576         (setq name (concat foreign name group)
9577               group nil)))
9578     name))
9579
9580 (defun gnus-summary-jump-to-group (newsgroup)
9581   "Move point to NEWSGROUP in group mode buffer."
9582   ;; Keep update point of group mode buffer if visible.
9583   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9584       (save-window-excursion
9585         ;; Take care of tree window mode.
9586         (if (get-buffer-window gnus-group-buffer)
9587             (pop-to-buffer gnus-group-buffer))
9588         (gnus-group-jump-to-group newsgroup))
9589     (save-excursion
9590       ;; Take care of tree window mode.
9591       (if (get-buffer-window gnus-group-buffer)
9592           (pop-to-buffer gnus-group-buffer)
9593         (set-buffer gnus-group-buffer))
9594       (gnus-group-jump-to-group newsgroup))))
9595
9596 ;; This function returns a list of article numbers based on the
9597 ;; difference between the ranges of read articles in this group and
9598 ;; the range of active articles.
9599 (defun gnus-list-of-unread-articles (group)
9600   (let* ((read (gnus-info-read (gnus-get-info group)))
9601          (active (gnus-active group))
9602          (last (cdr active))
9603          first nlast unread)
9604     ;; If none are read, then all are unread.
9605     (if (not read)
9606         (setq first (car active))
9607       ;; If the range of read articles is a single range, then the
9608       ;; first unread article is the article after the last read
9609       ;; article.  Sounds logical, doesn't it?
9610       (if (not (listp (cdr read)))
9611           (setq first (1+ (cdr read)))
9612         ;; `read' is a list of ranges.
9613         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9614                                 (caar read))) 1)
9615             (setq first 1))
9616         (while read
9617           (if first
9618               (while (< first nlast)
9619                 (setq unread (cons first unread))
9620                 (setq first (1+ first))))
9621           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9622           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9623           (setq read (cdr read)))))
9624     ;; And add the last unread articles.
9625     (while (<= first last)
9626       (setq unread (cons first unread))
9627       (setq first (1+ first)))
9628     ;; Return the list of unread articles.
9629     (nreverse unread)))
9630
9631 (defun gnus-list-of-read-articles (group)
9632   "Return a list of unread, unticked and non-dormant articles."
9633   (let* ((info (gnus-get-info group))
9634          (marked (gnus-info-marks info))
9635          (active (gnus-active group)))
9636     (and info active
9637          (gnus-set-difference
9638           (gnus-sorted-complement
9639            (gnus-uncompress-range active)
9640            (gnus-list-of-unread-articles group))
9641           (append
9642            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9643            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9644
9645 ;; Various summary commands
9646
9647 (defun gnus-summary-universal-argument (arg)
9648   "Perform any operation on all articles that are process/prefixed."
9649   (interactive "P")
9650   (gnus-set-global-variables)
9651   (let ((articles (gnus-summary-work-articles arg))
9652         func article)
9653     (if (eq
9654          (setq
9655           func
9656           (key-binding
9657            (read-key-sequence
9658             (substitute-command-keys
9659              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9660              ))))
9661          'undefined)
9662         (gnus-error 1 "Undefined key")
9663       (save-excursion
9664         (while articles
9665           (gnus-summary-goto-subject (setq article (pop articles)))
9666           (command-execute func)
9667           (gnus-summary-remove-process-mark article)))))
9668   (gnus-summary-position-point))
9669
9670 (defun gnus-summary-toggle-truncation (&optional arg)
9671   "Toggle truncation of summary lines.
9672 With arg, turn line truncation on iff arg is positive."
9673   (interactive "P")
9674   (setq truncate-lines
9675         (if (null arg) (not truncate-lines)
9676           (> (prefix-numeric-value arg) 0)))
9677   (redraw-display))
9678
9679 (defun gnus-summary-reselect-current-group (&optional all rescan)
9680   "Exit and then reselect the current newsgroup.
9681 The prefix argument ALL means to select all articles."
9682   (interactive "P")
9683   (gnus-set-global-variables)
9684   (let ((current-subject (gnus-summary-article-number))
9685         (group gnus-newsgroup-name))
9686     (setq gnus-newsgroup-begin nil)
9687     (gnus-summary-exit)
9688     ;; We have to adjust the point of group mode buffer because the
9689     ;; current point was moved to the next unread newsgroup by
9690     ;; exiting.
9691     (gnus-summary-jump-to-group group)
9692     (when rescan
9693       (save-excursion
9694         (gnus-group-get-new-news-this-group 1)))
9695     (gnus-group-read-group all t)
9696     (gnus-summary-goto-subject current-subject)))
9697
9698 (defun gnus-summary-rescan-group (&optional all)
9699   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9700   (interactive "P")
9701   (gnus-summary-reselect-current-group all t))
9702
9703 (defun gnus-summary-update-info ()
9704   (let* ((group gnus-newsgroup-name))
9705     (when gnus-newsgroup-kill-headers
9706       (setq gnus-newsgroup-killed
9707             (gnus-compress-sequence
9708              (nconc
9709               (gnus-set-sorted-intersection
9710                (gnus-uncompress-range gnus-newsgroup-killed)
9711                (setq gnus-newsgroup-unselected
9712                      (sort gnus-newsgroup-unselected '<)))
9713               (setq gnus-newsgroup-unreads
9714                     (sort gnus-newsgroup-unreads '<))) t)))
9715     (unless (listp (cdr gnus-newsgroup-killed))
9716       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9717     (let ((headers gnus-newsgroup-headers))
9718       (run-hooks 'gnus-exit-group-hook)
9719       (unless gnus-save-score
9720         (setq gnus-newsgroup-scored nil))
9721       ;; Set the new ranges of read articles.
9722       (gnus-update-read-articles
9723        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9724       ;; Set the current article marks.
9725       (gnus-update-marks)
9726       ;; Do the cross-ref thing.
9727       (when gnus-use-cross-reference
9728         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9729       ;; Do adaptive scoring, and possibly save score files.
9730       (when gnus-newsgroup-adaptive
9731         (gnus-score-adaptive))
9732       (when gnus-use-scoring
9733         (gnus-score-save))
9734       ;; Do not switch windows but change the buffer to work.
9735       (set-buffer gnus-group-buffer)
9736       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9737           (gnus-group-update-group group)))))
9738
9739 (defun gnus-summary-exit (&optional temporary)
9740   "Exit reading current newsgroup, and then return to group selection mode.
9741 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9742   (interactive)
9743   (gnus-set-global-variables)
9744   (gnus-kill-save-kill-buffer)
9745   (let* ((group gnus-newsgroup-name)
9746          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9747          (mode major-mode)
9748          (buf (current-buffer)))
9749     (run-hooks 'gnus-summary-prepare-exit-hook)
9750     ;; If we have several article buffers, we kill them at exit.
9751     (unless gnus-single-article-buffer
9752       (gnus-kill-buffer gnus-original-article-buffer)
9753       (setq gnus-article-current nil))
9754     (when gnus-use-cache
9755       (gnus-cache-possibly-remove-articles)
9756       (gnus-cache-save-buffers))
9757     (when gnus-use-trees
9758       (gnus-tree-close group))
9759     ;; Make all changes in this group permanent.
9760     (unless quit-config
9761       (gnus-summary-update-info))
9762     (gnus-close-group group)
9763     ;; Make sure where I was, and go to next newsgroup.
9764     (set-buffer gnus-group-buffer)
9765     (unless quit-config
9766       (gnus-group-jump-to-group group)
9767       (gnus-group-next-unread-group 1))
9768     (run-hooks 'gnus-summary-exit-hook)
9769     (if temporary
9770         nil                             ;Nothing to do.
9771       ;; If we have several article buffers, we kill them at exit.
9772       (unless gnus-single-article-buffer
9773         (gnus-kill-buffer gnus-article-buffer)
9774         (gnus-kill-buffer gnus-original-article-buffer)
9775         (setq gnus-article-current nil))
9776       (set-buffer buf)
9777       (if (not gnus-kill-summary-on-exit)
9778           (gnus-deaden-summary)
9779         ;; We set all buffer-local variables to nil.  It is unclear why
9780         ;; this is needed, but if we don't, buffer-local variables are
9781         ;; not garbage-collected, it seems.  This would the lead to en
9782         ;; ever-growing Emacs.
9783         (gnus-summary-clear-local-variables)
9784         (when (get-buffer gnus-article-buffer)
9785           (bury-buffer gnus-article-buffer))
9786         ;; We clear the global counterparts of the buffer-local
9787         ;; variables as well, just to be on the safe side.
9788         (gnus-configure-windows 'group 'force)
9789         (gnus-summary-clear-local-variables)
9790         ;; Return to group mode buffer.
9791         (if (eq mode 'gnus-summary-mode)
9792             (gnus-kill-buffer buf)))
9793       (setq gnus-current-select-method gnus-select-method)
9794       (pop-to-buffer gnus-group-buffer)
9795       ;; Clear the current group name.
9796       (if (not quit-config)
9797           (progn
9798             (gnus-group-jump-to-group group)
9799             (gnus-group-next-unread-group 1)
9800             (gnus-configure-windows 'group 'force))
9801         (if (not (buffer-name (car quit-config)))
9802             (gnus-configure-windows 'group 'force)
9803           (set-buffer (car quit-config))
9804           (and (eq major-mode 'gnus-summary-mode)
9805                (gnus-set-global-variables))
9806           (gnus-configure-windows (cdr quit-config))))
9807       (unless quit-config
9808         (setq gnus-newsgroup-name nil)))))
9809
9810 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9811 (defun gnus-summary-exit-no-update (&optional no-questions)
9812   "Quit reading current newsgroup without updating read article info."
9813   (interactive)
9814   (gnus-set-global-variables)
9815   (let* ((group gnus-newsgroup-name)
9816          (quit-config (gnus-group-quit-config group)))
9817     (when (or no-questions
9818               gnus-expert-user
9819               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9820       ;; If we have several article buffers, we kill them at exit.
9821       (unless gnus-single-article-buffer
9822         (gnus-kill-buffer gnus-article-buffer)
9823         (gnus-kill-buffer gnus-original-article-buffer)
9824         (setq gnus-article-current nil))
9825       (if (not gnus-kill-summary-on-exit)
9826           (gnus-deaden-summary)
9827         (gnus-close-group group)
9828         (gnus-summary-clear-local-variables)
9829         (set-buffer gnus-group-buffer)
9830         (gnus-summary-clear-local-variables)
9831         (when (get-buffer gnus-summary-buffer)
9832           (kill-buffer gnus-summary-buffer)))
9833       (unless gnus-single-article-buffer
9834         (setq gnus-article-current nil))
9835       (when gnus-use-trees
9836         (gnus-tree-close group))
9837       (when (get-buffer gnus-article-buffer)
9838         (bury-buffer gnus-article-buffer))
9839       ;; Return to the group buffer.
9840       (gnus-configure-windows 'group 'force)
9841       ;; Clear the current group name.
9842       (setq gnus-newsgroup-name nil)
9843       (when (equal (gnus-group-group-name) group)
9844         (gnus-group-next-unread-group 1))
9845       (when quit-config
9846         (if (not (buffer-name (car quit-config)))
9847             (gnus-configure-windows 'group 'force)
9848           (set-buffer (car quit-config))
9849           (when (eq major-mode 'gnus-summary-mode)
9850             (gnus-set-global-variables))
9851           (gnus-configure-windows (cdr quit-config)))))))
9852
9853 ;;; Dead summaries.
9854
9855 (defvar gnus-dead-summary-mode-map nil)
9856
9857 (if gnus-dead-summary-mode-map
9858     nil
9859   (setq gnus-dead-summary-mode-map (make-keymap))
9860   (suppress-keymap gnus-dead-summary-mode-map)
9861   (substitute-key-definition
9862    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9863   (let ((keys '("\C-d" "\r" "\177")))
9864     (while keys
9865       (define-key gnus-dead-summary-mode-map
9866         (pop keys) 'gnus-summary-wake-up-the-dead))))
9867
9868 (defvar gnus-dead-summary-mode nil
9869   "Minor mode for Gnus summary buffers.")
9870
9871 (defun gnus-dead-summary-mode (&optional arg)
9872   "Minor mode for Gnus summary buffers."
9873   (interactive "P")
9874   (when (eq major-mode 'gnus-summary-mode)
9875     (make-local-variable 'gnus-dead-summary-mode)
9876     (setq gnus-dead-summary-mode
9877           (if (null arg) (not gnus-dead-summary-mode)
9878             (> (prefix-numeric-value arg) 0)))
9879     (when gnus-dead-summary-mode
9880       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9881         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9882       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9883         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9884               minor-mode-map-alist)))))
9885
9886 (defun gnus-deaden-summary ()
9887   "Make the current summary buffer into a dead summary buffer."
9888   ;; Kill any previous dead summary buffer.
9889   (when (and gnus-dead-summary
9890              (buffer-name gnus-dead-summary))
9891     (save-excursion
9892       (set-buffer gnus-dead-summary)
9893       (when gnus-dead-summary-mode
9894         (kill-buffer (current-buffer)))))
9895   ;; Make this the current dead summary.
9896   (setq gnus-dead-summary (current-buffer))
9897   (gnus-dead-summary-mode 1)
9898   (let ((name (buffer-name)))
9899     (when (string-match "Summary" name)
9900       (rename-buffer
9901        (concat (substring name 0 (match-beginning 0)) "Dead "
9902                (substring name (match-beginning 0))) t))))
9903
9904 (defun gnus-kill-or-deaden-summary (buffer)
9905   "Kill or deaden the summary BUFFER."
9906   (when (and (buffer-name buffer)
9907              (not gnus-single-article-buffer))
9908     (save-excursion
9909       (set-buffer buffer)
9910       (gnus-kill-buffer gnus-article-buffer)
9911       (gnus-kill-buffer gnus-original-article-buffer)))
9912   (cond (gnus-kill-summary-on-exit
9913          (when (and gnus-use-trees
9914                     (and (get-buffer buffer)
9915                          (buffer-name (get-buffer buffer))))
9916            (save-excursion
9917              (set-buffer (get-buffer buffer))
9918              (gnus-tree-close gnus-newsgroup-name)))
9919          (gnus-kill-buffer buffer))
9920         ((and (get-buffer buffer)
9921               (buffer-name (get-buffer buffer)))
9922          (save-excursion
9923            (set-buffer buffer)
9924            (gnus-deaden-summary)))))
9925
9926 (defun gnus-summary-wake-up-the-dead (&rest args)
9927   "Wake up the dead summary buffer."
9928   (interactive)
9929   (gnus-dead-summary-mode -1)
9930   (let ((name (buffer-name)))
9931     (when (string-match "Dead " name)
9932       (rename-buffer
9933        (concat (substring name 0 (match-beginning 0))
9934                (substring name (match-end 0))) t)))
9935   (gnus-message 3 "This dead summary is now alive again"))
9936
9937 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9938 (defun gnus-summary-fetch-faq (&optional faq-dir)
9939   "Fetch the FAQ for the current group.
9940 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9941 in."
9942   (interactive
9943    (list
9944     (if current-prefix-arg
9945         (completing-read
9946          "Faq dir: " (and (listp gnus-group-faq-directory)
9947                           gnus-group-faq-directory)))))
9948   (let (gnus-faq-buffer)
9949     (and (setq gnus-faq-buffer
9950                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9951          (gnus-configure-windows 'summary-faq))))
9952
9953 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9954 (defun gnus-summary-describe-group (&optional force)
9955   "Describe the current newsgroup."
9956   (interactive "P")
9957   (gnus-group-describe-group force gnus-newsgroup-name))
9958
9959 (defun gnus-summary-describe-briefly ()
9960   "Describe summary mode commands briefly."
9961   (interactive)
9962   (gnus-message 6
9963                 (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")))
9964
9965 ;; Walking around group mode buffer from summary mode.
9966
9967 (defun gnus-summary-next-group (&optional no-article target-group backward)
9968   "Exit current newsgroup and then select next unread newsgroup.
9969 If prefix argument NO-ARTICLE is non-nil, no article is selected
9970 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9971 previous group instead."
9972   (interactive "P")
9973   (gnus-set-global-variables)
9974   (let ((current-group gnus-newsgroup-name)
9975         (current-buffer (current-buffer))
9976         entered)
9977     ;; First we semi-exit this group to update Xrefs and all variables.
9978     ;; We can't do a real exit, because the window conf must remain
9979     ;; the same in case the user is prompted for info, and we don't
9980     ;; want the window conf to change before that...
9981     (gnus-summary-exit t)
9982     (while (not entered)
9983       ;; Then we find what group we are supposed to enter.
9984       (set-buffer gnus-group-buffer)
9985       (gnus-group-jump-to-group current-group)
9986       (setq target-group
9987             (or target-group
9988                 (if (eq gnus-keep-same-level 'best)
9989                     (gnus-summary-best-group gnus-newsgroup-name)
9990                   (gnus-summary-search-group backward gnus-keep-same-level))))
9991       (if (not target-group)
9992           ;; There are no further groups, so we return to the group
9993           ;; buffer.
9994           (progn
9995             (gnus-message 5 "Returning to the group buffer")
9996             (setq entered t)
9997             (set-buffer current-buffer)
9998             (gnus-summary-exit))
9999         ;; We try to enter the target group.
10000         (gnus-group-jump-to-group target-group)
10001         (let ((unreads (gnus-group-group-unread)))
10002           (if (and (or (eq t unreads)
10003                        (and unreads (not (zerop unreads))))
10004                    (gnus-summary-read-group
10005                     target-group nil no-article current-buffer))
10006               (setq entered t)
10007             (setq current-group target-group
10008                   target-group nil)))))))
10009
10010 (defun gnus-summary-prev-group (&optional no-article)
10011   "Exit current newsgroup and then select previous unread newsgroup.
10012 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10013   (interactive "P")
10014   (gnus-summary-next-group no-article nil t))
10015
10016 ;; Walking around summary lines.
10017
10018 (defun gnus-summary-first-subject (&optional unread)
10019   "Go to the first unread subject.
10020 If UNREAD is non-nil, go to the first unread article.
10021 Returns the article selected or nil if there are no unread articles."
10022   (interactive "P")
10023   (prog1
10024       (cond
10025        ;; Empty summary.
10026        ((null gnus-newsgroup-data)
10027         (gnus-message 3 "No articles in the group")
10028         nil)
10029        ;; Pick the first article.
10030        ((not unread)
10031         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10032         (gnus-data-number (car gnus-newsgroup-data)))
10033        ;; No unread articles.
10034        ((null gnus-newsgroup-unreads)
10035         (gnus-message 3 "No more unread articles")
10036         nil)
10037        ;; Find the first unread article.
10038        (t
10039         (let ((data gnus-newsgroup-data))
10040           (while (and data
10041                       (not (gnus-data-unread-p (car data))))
10042             (setq data (cdr data)))
10043           (if data
10044               (progn
10045                 (goto-char (gnus-data-pos (car data)))
10046                 (gnus-data-number (car data)))))))
10047     (gnus-summary-position-point)))
10048
10049 (defun gnus-summary-next-subject (n &optional unread dont-display)
10050   "Go to next N'th summary line.
10051 If N is negative, go to the previous N'th subject line.
10052 If UNREAD is non-nil, only unread articles are selected.
10053 The difference between N and the actual number of steps taken is
10054 returned."
10055   (interactive "p")
10056   (let ((backward (< n 0))
10057         (n (abs n)))
10058     (while (and (> n 0)
10059                 (if backward
10060                     (gnus-summary-find-prev unread)
10061                   (gnus-summary-find-next unread)))
10062       (setq n (1- n)))
10063     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10064                                (if unread " unread" "")))
10065     (unless dont-display
10066       (gnus-summary-recenter)
10067       (gnus-summary-position-point))
10068     n))
10069
10070 (defun gnus-summary-next-unread-subject (n)
10071   "Go to next N'th unread summary line."
10072   (interactive "p")
10073   (gnus-summary-next-subject n t))
10074
10075 (defun gnus-summary-prev-subject (n &optional unread)
10076   "Go to previous N'th summary line.
10077 If optional argument UNREAD is non-nil, only unread article is selected."
10078   (interactive "p")
10079   (gnus-summary-next-subject (- n) unread))
10080
10081 (defun gnus-summary-prev-unread-subject (n)
10082   "Go to previous N'th unread summary line."
10083   (interactive "p")
10084   (gnus-summary-next-subject (- n) t))
10085
10086 (defun gnus-summary-goto-subject (article &optional force silent)
10087   "Go the subject line of ARTICLE.
10088 If FORCE, also allow jumping to articles not currently shown."
10089   (let ((b (point))
10090         (data (gnus-data-find article)))
10091     ;; We read in the article if we have to.
10092     (and (not data)
10093          force
10094          (gnus-summary-insert-subject article)
10095          (setq data (gnus-data-find article)))
10096     (goto-char b)
10097     (if (not data)
10098         (progn
10099           (unless silent
10100             (gnus-message 3 "Can't find article %d" article))
10101           nil)
10102       (goto-char (gnus-data-pos data))
10103       article)))
10104
10105 ;; Walking around summary lines with displaying articles.
10106
10107 (defun gnus-summary-expand-window (&optional arg)
10108   "Make the summary buffer take up the entire Emacs frame.
10109 Given a prefix, will force an `article' buffer configuration."
10110   (interactive "P")
10111   (gnus-set-global-variables)
10112   (if arg
10113       (gnus-configure-windows 'article 'force)
10114     (gnus-configure-windows 'summary 'force)))
10115
10116 (defun gnus-summary-display-article (article &optional all-header)
10117   "Display ARTICLE in article buffer."
10118   (gnus-set-global-variables)
10119   (if (null article)
10120       nil
10121     (prog1
10122         (if gnus-summary-display-article-function
10123             (funcall gnus-summary-display-article-function article all-header)
10124           (gnus-article-prepare article all-header))
10125       (run-hooks 'gnus-select-article-hook)
10126       (unless (zerop gnus-current-article)
10127         (gnus-summary-goto-subject gnus-current-article))
10128       (gnus-summary-recenter)
10129       (when gnus-use-trees
10130         (gnus-possibly-generate-tree article)
10131         (gnus-highlight-selected-tree article))
10132       ;; Successfully display article.
10133       (gnus-article-set-window-start
10134        (cdr (assq article gnus-newsgroup-bookmarks))))))
10135
10136 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10137   "Select the current article.
10138 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10139 non-nil, the article will be re-fetched even if it already present in
10140 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10141 be displayed."
10142   ;; Make sure we are in the summary buffer to work around bbdb bug.
10143   (unless (eq major-mode 'gnus-summary-mode)
10144     (set-buffer gnus-summary-buffer))
10145   (let ((article (or article (gnus-summary-article-number)))
10146         (all-headers (not (not all-headers))) ;Must be T or NIL.
10147         gnus-summary-display-article-function
10148         did)
10149     (and (not pseudo)
10150          (gnus-summary-article-pseudo-p article)
10151          (error "This is a pseudo-article."))
10152     (prog1
10153         (save-excursion
10154           (set-buffer gnus-summary-buffer)
10155           (if (or (and gnus-single-article-buffer
10156                        (or (null gnus-current-article)
10157                            (null gnus-article-current)
10158                            (null (get-buffer gnus-article-buffer))
10159                            (not (eq article (cdr gnus-article-current)))
10160                            (not (equal (car gnus-article-current)
10161                                        gnus-newsgroup-name))))
10162                   (and (not gnus-single-article-buffer)
10163                        (or (null gnus-current-article)
10164                            (not (eq gnus-current-article article))))
10165                   force)
10166               ;; The requested article is different from the current article.
10167               (prog1
10168                   (gnus-summary-display-article article all-headers)
10169                 (setq did article))
10170             (if (or all-headers gnus-show-all-headers)
10171                 (gnus-article-show-all-headers))
10172             'old))
10173       (if did
10174           (gnus-article-set-window-start
10175            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10176
10177 (defun gnus-summary-set-current-mark (&optional current-mark)
10178   "Obsolete function."
10179   nil)
10180
10181 (defun gnus-summary-next-article (&optional unread subject backward push)
10182   "Select the next article.
10183 If UNREAD, only unread articles are selected.
10184 If SUBJECT, only articles with SUBJECT are selected.
10185 If BACKWARD, the previous article is selected instead of the next."
10186   (interactive "P")
10187   (gnus-set-global-variables)
10188   (cond
10189    ;; Is there such an article?
10190    ((and (gnus-summary-search-forward unread subject backward)
10191          (or (gnus-summary-display-article (gnus-summary-article-number))
10192              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10193     (gnus-summary-position-point))
10194    ;; If not, we try the first unread, if that is wanted.
10195    ((and subject
10196          gnus-auto-select-same
10197          (or (gnus-summary-first-unread-article)
10198              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10199     (gnus-summary-position-point)
10200     (gnus-message 6 "Wrapped"))
10201    ;; Try to get next/previous article not displayed in this group.
10202    ((and gnus-auto-extend-newsgroup
10203          (not unread) (not subject))
10204     (gnus-summary-goto-article
10205      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10206      nil t))
10207    ;; Go to next/previous group.
10208    (t
10209     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10210         (gnus-summary-jump-to-group gnus-newsgroup-name))
10211     (let ((cmd last-command-char)
10212           (group
10213            (if (eq gnus-keep-same-level 'best)
10214                (gnus-summary-best-group gnus-newsgroup-name)
10215              (gnus-summary-search-group backward gnus-keep-same-level))))
10216       ;; For some reason, the group window gets selected.  We change
10217       ;; it back.
10218       (select-window (get-buffer-window (current-buffer)))
10219       ;; Select next unread newsgroup automagically.
10220       (cond
10221        ((not gnus-auto-select-next)
10222         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10223        ((or (eq gnus-auto-select-next 'quietly)
10224             (and (eq gnus-auto-select-next 'slightly-quietly)
10225                  push)
10226             (and (eq gnus-auto-select-next 'almost-quietly)
10227                  (gnus-summary-last-article-p)))
10228         ;; Select quietly.
10229         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10230             (gnus-summary-exit)
10231           (gnus-message 7 "No more%s articles (%s)..."
10232                         (if unread " unread" "")
10233                         (if group (concat "selecting " group)
10234                           "exiting"))
10235           (gnus-summary-next-group nil group backward)))
10236        (t
10237         (gnus-summary-walk-group-buffer
10238          gnus-newsgroup-name cmd unread backward)))))))
10239
10240 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10241   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10242                       (?\C-p (gnus-group-prev-unread-group 1))))
10243         keve key group ended)
10244     (save-excursion
10245       (set-buffer gnus-group-buffer)
10246       (gnus-summary-jump-to-group from-group)
10247       (setq group
10248             (if (eq gnus-keep-same-level 'best)
10249                 (gnus-summary-best-group gnus-newsgroup-name)
10250               (gnus-summary-search-group backward gnus-keep-same-level))))
10251     (while (not ended)
10252       (gnus-message
10253        5 "No more%s articles%s" (if unread " unread" "")
10254        (if (and group
10255                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10256            (format " (Type %s for %s [%s])"
10257                    (single-key-description cmd) group
10258                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10259          (format " (Type %s to exit %s)"
10260                  (single-key-description cmd)
10261                  gnus-newsgroup-name)))
10262       ;; Confirm auto selection.
10263       (setq key (car (setq keve (gnus-read-event-char))))
10264       (setq ended t)
10265       (cond
10266        ((assq key keystrokes)
10267         (let ((obuf (current-buffer)))
10268           (switch-to-buffer gnus-group-buffer)
10269           (and group
10270                (gnus-group-jump-to-group group))
10271           (eval (cadr (assq key keystrokes)))
10272           (setq group (gnus-group-group-name))
10273           (switch-to-buffer obuf))
10274         (setq ended nil))
10275        ((equal key cmd)
10276         (if (or (not group)
10277                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10278             (gnus-summary-exit)
10279           (gnus-summary-next-group nil group backward)))
10280        (t
10281         (push (cdr keve) unread-command-events))))))
10282
10283 (defun gnus-read-event-char ()
10284   "Get the next event."
10285   (let ((event (read-event)))
10286     (cons (and (numberp event) event) event)))
10287
10288 (defun gnus-summary-next-unread-article ()
10289   "Select unread article after current one."
10290   (interactive)
10291   (gnus-summary-next-article t (and gnus-auto-select-same
10292                                     (gnus-summary-article-subject))))
10293
10294 (defun gnus-summary-prev-article (&optional unread subject)
10295   "Select the article after the current one.
10296 If UNREAD is non-nil, only unread articles are selected."
10297   (interactive "P")
10298   (gnus-summary-next-article unread subject t))
10299
10300 (defun gnus-summary-prev-unread-article ()
10301   "Select unred article before current one."
10302   (interactive)
10303   (gnus-summary-prev-article t (and gnus-auto-select-same
10304                                     (gnus-summary-article-subject))))
10305
10306 (defun gnus-summary-next-page (&optional lines circular)
10307   "Show next page of the selected article.
10308 If at the end of the current article, select the next article.
10309 LINES says how many lines should be scrolled up.
10310
10311 If CIRCULAR is non-nil, go to the start of the article instead of
10312 selecting the next article when reaching the end of the current
10313 article."
10314   (interactive "P")
10315   (setq gnus-summary-buffer (current-buffer))
10316   (gnus-set-global-variables)
10317   (let ((article (gnus-summary-article-number))
10318         (endp nil))
10319     (gnus-configure-windows 'article)
10320     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10321         (if (and (eq gnus-summary-goto-unread 'never)
10322                  (not (gnus-summary-last-article-p article)))
10323             (gnus-summary-next-article)
10324           (gnus-summary-next-unread-article))
10325       (if (or (null gnus-current-article)
10326               (null gnus-article-current)
10327               (/= article (cdr gnus-article-current))
10328               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10329           ;; Selected subject is different from current article's.
10330           (gnus-summary-display-article article)
10331         (gnus-eval-in-buffer-window
10332          gnus-article-buffer
10333          (setq endp (gnus-article-next-page lines)))
10334         (if endp
10335             (cond (circular
10336                    (gnus-summary-beginning-of-article))
10337                   (lines
10338                    (gnus-message 3 "End of message"))
10339                   ((null lines)
10340                    (if (and (eq gnus-summary-goto-unread 'never)
10341                             (not (gnus-summary-last-article-p article)))
10342                        (gnus-summary-next-article)
10343                      (gnus-summary-next-unread-article)))))))
10344     (gnus-summary-recenter)
10345     (gnus-summary-position-point)))
10346
10347 (defun gnus-summary-prev-page (&optional lines)
10348   "Show previous page of selected article.
10349 Argument LINES specifies lines to be scrolled down."
10350   (interactive "P")
10351   (gnus-set-global-variables)
10352   (let ((article (gnus-summary-article-number)))
10353     (gnus-configure-windows 'article)
10354     (if (or (null gnus-current-article)
10355             (null gnus-article-current)
10356             (/= article (cdr gnus-article-current))
10357             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10358         ;; Selected subject is different from current article's.
10359         (gnus-summary-display-article article)
10360       (gnus-summary-recenter)
10361       (gnus-eval-in-buffer-window gnus-article-buffer
10362                                   (gnus-article-prev-page lines))))
10363   (gnus-summary-position-point))
10364
10365 (defun gnus-summary-scroll-up (lines)
10366   "Scroll up (or down) one line current article.
10367 Argument LINES specifies lines to be scrolled up (or down if negative)."
10368   (interactive "p")
10369   (gnus-set-global-variables)
10370   (gnus-configure-windows 'article)
10371   (gnus-summary-show-thread)
10372   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10373     (gnus-eval-in-buffer-window
10374      gnus-article-buffer
10375      (cond ((> lines 0)
10376             (if (gnus-article-next-page lines)
10377                 (gnus-message 3 "End of message")))
10378            ((< lines 0)
10379             (gnus-article-prev-page (- lines))))))
10380   (gnus-summary-recenter)
10381   (gnus-summary-position-point))
10382
10383 (defun gnus-summary-next-same-subject ()
10384   "Select next article which has the same subject as current one."
10385   (interactive)
10386   (gnus-set-global-variables)
10387   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10388
10389 (defun gnus-summary-prev-same-subject ()
10390   "Select previous article which has the same subject as current one."
10391   (interactive)
10392   (gnus-set-global-variables)
10393   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10394
10395 (defun gnus-summary-next-unread-same-subject ()
10396   "Select next unread article which has the same subject as current one."
10397   (interactive)
10398   (gnus-set-global-variables)
10399   (gnus-summary-next-article t (gnus-summary-article-subject)))
10400
10401 (defun gnus-summary-prev-unread-same-subject ()
10402   "Select previous unread article which has the same subject as current one."
10403   (interactive)
10404   (gnus-set-global-variables)
10405   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10406
10407 (defun gnus-summary-first-unread-article ()
10408   "Select the first unread article.
10409 Return nil if there are no unread articles."
10410   (interactive)
10411   (gnus-set-global-variables)
10412   (prog1
10413       (if (gnus-summary-first-subject t)
10414           (progn
10415             (gnus-summary-show-thread)
10416             (gnus-summary-first-subject t)
10417             (gnus-summary-display-article (gnus-summary-article-number))))
10418     (gnus-summary-position-point)))
10419
10420 (defun gnus-summary-best-unread-article ()
10421   "Select the unread article with the highest score."
10422   (interactive)
10423   (gnus-set-global-variables)
10424   (let ((best -1000000)
10425         (data gnus-newsgroup-data)
10426         article score)
10427     (while data
10428       (and (gnus-data-unread-p (car data))
10429            (> (setq score
10430                     (gnus-summary-article-score (gnus-data-number (car data))))
10431               best)
10432            (setq best score
10433                  article (gnus-data-number (car data))))
10434       (setq data (cdr data)))
10435     (prog1
10436         (if article
10437             (gnus-summary-goto-article article)
10438           (error "No unread articles"))
10439       (gnus-summary-position-point))))
10440
10441 (defun gnus-summary-last-subject ()
10442   "Go to the last displayed subject line in the group."
10443   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10444     (when article
10445       (gnus-summary-goto-subject article))))
10446
10447 (defun gnus-summary-goto-article (article &optional all-headers force)
10448   "Fetch ARTICLE and display it if it exists.
10449 If ALL-HEADERS is non-nil, no header lines are hidden."
10450   (interactive
10451    (list
10452     (string-to-int
10453      (completing-read
10454       "Article number: "
10455       (mapcar (lambda (number) (list (int-to-string number)))
10456               gnus-newsgroup-limit)))
10457     current-prefix-arg
10458     t))
10459   (prog1
10460       (if (gnus-summary-goto-subject article force)
10461           (gnus-summary-display-article article all-headers)
10462         (gnus-message 4 "Couldn't go to article %s" article) nil)
10463     (gnus-summary-position-point)))
10464
10465 (defun gnus-summary-goto-last-article ()
10466   "Go to the previously read article."
10467   (interactive)
10468   (prog1
10469       (and gnus-last-article
10470            (gnus-summary-goto-article gnus-last-article))
10471     (gnus-summary-position-point)))
10472
10473 (defun gnus-summary-pop-article (number)
10474   "Pop one article off the history and go to the previous.
10475 NUMBER articles will be popped off."
10476   (interactive "p")
10477   (let (to)
10478     (setq gnus-newsgroup-history
10479           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10480     (if to
10481         (gnus-summary-goto-article (car to))
10482       (error "Article history empty")))
10483   (gnus-summary-position-point))
10484
10485 ;; Summary commands and functions for limiting the summary buffer.
10486
10487 (defun gnus-summary-limit-to-articles (n)
10488   "Limit the summary buffer to the next N articles.
10489 If not given a prefix, use the process marked articles instead."
10490   (interactive "P")
10491   (gnus-set-global-variables)
10492   (prog1
10493       (let ((articles (gnus-summary-work-articles n)))
10494         (setq gnus-newsgroup-processable nil)
10495         (gnus-summary-limit articles))
10496     (gnus-summary-position-point)))
10497
10498 (defun gnus-summary-pop-limit (&optional total)
10499   "Restore the previous limit.
10500 If given a prefix, remove all limits."
10501   (interactive "P")
10502   (gnus-set-global-variables)
10503   (when total 
10504     (setq gnus-newsgroup-limits
10505           (list (mapcar (lambda (h) (mail-header-number h))
10506                         gnus-newsgroup-headers))))
10507   (unless gnus-newsgroup-limits
10508     (error "No limit to pop"))
10509   (prog1
10510       (gnus-summary-limit nil 'pop)
10511     (gnus-summary-position-point)))
10512
10513 (defun gnus-summary-limit-to-subject (subject &optional header)
10514   "Limit the summary buffer to articles that have subjects that match a regexp."
10515   (interactive "sRegexp: ")
10516   (unless header
10517     (setq header "subject"))
10518   (when (not (equal "" subject))
10519     (prog1
10520         (let ((articles (gnus-summary-find-matching
10521                          (or header "subject") subject 'all)))
10522           (or articles (error "Found no matches for \"%s\"" subject))
10523           (gnus-summary-limit articles))
10524       (gnus-summary-position-point))))
10525
10526 (defun gnus-summary-limit-to-author (from)
10527   "Limit the summary buffer to articles that have authors that match a regexp."
10528   (interactive "sRegexp: ")
10529   (gnus-summary-limit-to-subject from "from"))
10530
10531 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10532 (make-obsolete
10533  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10534
10535 (defun gnus-summary-limit-to-unread (&optional all)
10536   "Limit the summary buffer to articles that are not marked as read.
10537 If ALL is non-nil, limit strictly to unread articles."
10538   (interactive "P")
10539   (if all
10540       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10541     (gnus-summary-limit-to-marks
10542      ;; Concat all the marks that say that an article is read and have
10543      ;; those removed.
10544      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10545            gnus-killed-mark gnus-kill-file-mark
10546            gnus-low-score-mark gnus-expirable-mark
10547            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10548      'reverse)))
10549
10550 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10551 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10552
10553 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10554   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10555 If REVERSE, limit the summary buffer to articles that are not marked
10556 with MARKS.  MARKS can either be a string of marks or a list of marks.
10557 Returns how many articles were removed."
10558   (interactive "sMarks: ")
10559   (gnus-set-global-variables)
10560   (prog1
10561       (let ((data gnus-newsgroup-data)
10562             (marks (if (listp marks) marks
10563                      (append marks nil))) ; Transform to list.
10564             articles)
10565         (while data
10566           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10567                  (memq (gnus-data-mark (car data)) marks))
10568                (setq articles (cons (gnus-data-number (car data)) articles)))
10569           (setq data (cdr data)))
10570         (gnus-summary-limit articles))
10571     (gnus-summary-position-point)))
10572
10573 (defun gnus-summary-limit-to-score (&optional score)
10574   "Limit to articles with score at or above SCORE."
10575   (interactive "P")
10576   (gnus-set-global-variables)
10577   (setq score (if score
10578                   (prefix-numeric-value score)
10579                 (or gnus-summary-default-score 0)))
10580   (let ((data gnus-newsgroup-data)
10581         articles)
10582     (while data
10583       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10584                 score)
10585         (push (gnus-data-number (car data)) articles))
10586       (setq data (cdr data)))
10587     (prog1
10588         (gnus-summary-limit articles)
10589       (gnus-summary-position-point))))
10590
10591 (defun gnus-summary-limit-include-dormant ()
10592   "Display all the hidden articles that are marked as dormant."
10593   (interactive)
10594   (gnus-set-global-variables)
10595   (or gnus-newsgroup-dormant
10596       (error "There are no dormant articles in this group"))
10597   (prog1
10598       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10599     (gnus-summary-position-point)))
10600
10601 (defun gnus-summary-limit-exclude-dormant ()
10602   "Hide all dormant articles."
10603   (interactive)
10604   (gnus-set-global-variables)
10605   (prog1
10606       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10607     (gnus-summary-position-point)))
10608
10609 (defun gnus-summary-limit-exclude-childless-dormant ()
10610   "Hide all dormant articles that have no children."
10611   (interactive)
10612   (gnus-set-global-variables)
10613   (let ((data (gnus-data-list t))
10614         articles d children)
10615     ;; Find all articles that are either not dormant or have
10616     ;; children.
10617     (while (setq d (pop data))
10618       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10619                 (and (setq children 
10620                            (gnus-article-children (gnus-data-number d)))
10621                      (let (found)
10622                        (while children
10623                          (when (memq (car children) articles)
10624                            (setq children nil
10625                                  found t))
10626                          (pop children))
10627                        found)))
10628         (push (gnus-data-number d) articles)))
10629     ;; Do the limiting.
10630     (prog1
10631         (gnus-summary-limit articles)
10632       (gnus-summary-position-point))))
10633
10634 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10635   "Mark all unread excluded articles as read.
10636 If ALL, mark even excluded ticked and dormants as read."
10637   (interactive "P")
10638   (let ((articles (gnus-sorted-complement
10639                    (sort
10640                     (mapcar (lambda (h) (mail-header-number h))
10641                             gnus-newsgroup-headers)
10642                     '<)
10643                    (sort gnus-newsgroup-limit '<)))
10644         article)
10645     (setq gnus-newsgroup-unreads nil)
10646     (if all
10647         (setq gnus-newsgroup-dormant nil
10648               gnus-newsgroup-marked nil
10649               gnus-newsgroup-reads
10650               (nconc
10651                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10652                gnus-newsgroup-reads))
10653       (while (setq article (pop articles))
10654         (unless (or (memq article gnus-newsgroup-dormant)
10655                     (memq article gnus-newsgroup-marked))
10656           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10657
10658 (defun gnus-summary-limit (articles &optional pop)
10659   (if pop
10660       ;; We pop the previous limit off the stack and use that.
10661       (setq articles (car gnus-newsgroup-limits)
10662             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10663     ;; We use the new limit, so we push the old limit on the stack.
10664     (setq gnus-newsgroup-limits
10665           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10666   ;; Set the limit.
10667   (setq gnus-newsgroup-limit articles)
10668   (let ((total (length gnus-newsgroup-data))
10669         (data (gnus-data-find-list (gnus-summary-article-number)))
10670         found)
10671     ;; This will do all the work of generating the new summary buffer
10672     ;; according to the new limit.
10673     (gnus-summary-prepare)
10674     ;; Hide any threads, possibly.
10675     (and gnus-show-threads
10676          gnus-thread-hide-subtree
10677          (gnus-summary-hide-all-threads))
10678     ;; Try to return to the article you were at, or one in the
10679     ;; neighborhood.
10680     (if data
10681         ;; We try to find some article after the current one.
10682         (while data
10683           (and (gnus-summary-goto-subject
10684                 (gnus-data-number (car data)) nil t)
10685                (setq data nil
10686                      found t))
10687           (setq data (cdr data))))
10688     (or found
10689         ;; If there is no data, that means that we were after the last
10690         ;; article.  The same goes when we can't find any articles
10691         ;; after the current one.
10692         (progn
10693           (goto-char (point-max))
10694           (gnus-summary-find-prev)))
10695     ;; We return how many articles were removed from the summary
10696     ;; buffer as a result of the new limit.
10697     (- total (length gnus-newsgroup-data))))
10698
10699 (defsubst gnus-cut-thread (thread)
10700   "Go forwards in the thread until we find an article that we want to display."
10701   (when (eq gnus-fetch-old-headers 'some)
10702     ;; Deal with old-fetched headers.
10703     (while (and thread
10704                 (memq (mail-header-number (car thread)) 
10705                       gnus-newsgroup-ancient)
10706                 (<= (length (cdr thread)) 1))
10707       (setq thread (cadr thread))))
10708   ;; Deal with sparse threads.
10709   (when (or (eq gnus-build-sparse-threads 'some)
10710             (eq gnus-build-sparse-threads 'more))
10711     (while (and thread
10712                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10713                 (= (length (cdr thread)) 1))
10714       (setq thread (cadr thread))))
10715   thread)
10716
10717 (defun gnus-cut-threads (threads)
10718   "Cut off all uninteresting articles from the beginning of threads."
10719   (when (or (eq gnus-fetch-old-headers 'some)
10720             (eq gnus-build-sparse-threads 'some)
10721             (eq gnus-build-sparse-threads 'more))
10722     (let ((th threads))
10723       (while th
10724         (setcar th (gnus-cut-thread (car th)))
10725         (setq th (cdr th)))))
10726   ;; Remove nixed out threads.
10727   (delq nil threads))
10728
10729 (defun gnus-summary-initial-limit (&optional show-if-empty)
10730   "Figure out what the initial limit is supposed to be on group entry.
10731 This entails weeding out unwanted dormants, low-scored articles,
10732 fetch-old-headers verbiage, and so on."
10733   ;; Most groups have nothing to remove.
10734   (if (or gnus-inhibit-limiting
10735           (and (null gnus-newsgroup-dormant)
10736                (not (eq gnus-fetch-old-headers 'some))
10737                (null gnus-summary-expunge-below)
10738                (not (eq gnus-build-sparse-threads 'some))
10739                (not (eq gnus-build-sparse-threads 'more))
10740                (null gnus-thread-expunge-below)
10741                (not gnus-use-nocem)))
10742       () ; Do nothing.
10743     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10744     (setq gnus-newsgroup-limit nil)
10745     (mapatoms
10746      (lambda (node)
10747        (unless (car (symbol-value node))
10748          ;; These threads have no parents -- they are roots.
10749          (let ((nodes (cdr (symbol-value node)))
10750                thread)
10751            (while nodes
10752              (if (and gnus-thread-expunge-below
10753                       (< (gnus-thread-total-score (car nodes))
10754                          gnus-thread-expunge-below))
10755                  (gnus-expunge-thread (pop nodes))
10756                (setq thread (pop nodes))
10757                (gnus-summary-limit-children thread))))))
10758      gnus-newsgroup-dependencies)
10759     ;; If this limitation resulted in an empty group, we might
10760     ;; pop the previous limit and use it instead.
10761     (when (and (not gnus-newsgroup-limit)
10762                show-if-empty)
10763       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10764     gnus-newsgroup-limit))
10765
10766 (defun gnus-summary-limit-children (thread)
10767   "Return 1 if this subthread is visible and 0 if it is not."
10768   ;; First we get the number of visible children to this thread.  This
10769   ;; is done by recursing down the thread using this function, so this
10770   ;; will really go down to a leaf article first, before slowly
10771   ;; working its way up towards the root.
10772   (when thread
10773     (let ((children
10774            (if (cdr thread)
10775                (apply '+ (mapcar 'gnus-summary-limit-children
10776                                  (cdr thread)))
10777              0))
10778           (number (mail-header-number (car thread)))
10779           score)
10780       (if (or
10781            ;; If this article is dormant and has absolutely no visible
10782            ;; children, then this article isn't visible.
10783            (and (memq number gnus-newsgroup-dormant)
10784                 (= children 0))
10785            ;; If this is a "fetch-old-headered" and there is only one
10786            ;; visible child (or less), then we don't want this article.
10787            (and (eq gnus-fetch-old-headers 'some)
10788                 (memq number gnus-newsgroup-ancient)
10789                 (zerop children))
10790            ;; If this is a sparsely inserted article with no children,
10791            ;; we don't want it.
10792            (and (eq gnus-build-sparse-threads 'some)
10793                 (memq number gnus-newsgroup-sparse)
10794                 (zerop children))
10795            ;; If we use expunging, and this article is really
10796            ;; low-scored, then we don't want this article.
10797            (when (and gnus-summary-expunge-below
10798                       (< (setq score
10799                                (or (cdr (assq number gnus-newsgroup-scored))
10800                                    gnus-summary-default-score))
10801                          gnus-summary-expunge-below))
10802              ;; We increase the expunge-tally here, but that has
10803              ;; nothing to do with the limits, really.
10804              (incf gnus-newsgroup-expunged-tally)
10805              ;; We also mark as read here, if that's wanted.
10806              (when (and gnus-summary-mark-below
10807                         (< score gnus-summary-mark-below))
10808                (setq gnus-newsgroup-unreads
10809                      (delq number gnus-newsgroup-unreads))
10810                (if gnus-newsgroup-auto-expire
10811                    (push number gnus-newsgroup-expirable)
10812                  (push (cons number gnus-low-score-mark)
10813                        gnus-newsgroup-reads)))
10814              t)
10815            (and gnus-use-nocem
10816                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10817           ;; Nope, invisible article.
10818           0
10819         ;; Ok, this article is to be visible, so we add it to the limit
10820         ;; and return 1.
10821         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10822         1))))
10823
10824 (defun gnus-expunge-thread (thread)
10825   "Mark all articles in THREAD as read."
10826   (let* ((number (mail-header-number (car thread))))
10827     (incf gnus-newsgroup-expunged-tally)
10828     ;; We also mark as read here, if that's wanted.
10829     (setq gnus-newsgroup-unreads
10830           (delq number gnus-newsgroup-unreads))
10831     (if gnus-newsgroup-auto-expire
10832         (push number gnus-newsgroup-expirable)
10833       (push (cons number gnus-low-score-mark)
10834             gnus-newsgroup-reads)))
10835   ;; Go recursively through all subthreads.
10836   (mapcar 'gnus-expunge-thread (cdr thread)))
10837
10838 ;; Summary article oriented commands
10839
10840 (defun gnus-summary-refer-parent-article (n)
10841   "Refer parent article N times.
10842 The difference between N and the number of articles fetched is returned."
10843   (interactive "p")
10844   (gnus-set-global-variables)
10845   (while
10846       (and
10847        (> n 0)
10848        (let* ((header (gnus-summary-article-header))
10849               (ref
10850                ;; If we try to find the parent of the currently
10851                ;; displayed article, then we take a look at the actual
10852                ;; References header, since this is slightly more
10853                ;; reliable than the References field we got from the
10854                ;; server.
10855                (if (and (eq (mail-header-number header)
10856                             (cdr gnus-article-current))
10857                         (equal gnus-newsgroup-name
10858                                (car gnus-article-current)))
10859                    (save-excursion
10860                      (set-buffer gnus-original-article-buffer)
10861                      (nnheader-narrow-to-headers)
10862                      (prog1
10863                          (mail-fetch-field "references")
10864                        (widen)))
10865                  ;; It's not the current article, so we take a bet on
10866                  ;; the value we got from the server.
10867                  (mail-header-references header))))
10868          (if (setq ref (or ref (mail-header-references header)))
10869              (or (gnus-summary-refer-article (gnus-parent-id ref))
10870                  (gnus-message 1 "Couldn't find parent"))
10871            (gnus-message 1 "No references in article %d"
10872                          (gnus-summary-article-number))
10873            nil)))
10874     (setq n (1- n)))
10875   (gnus-summary-position-point)
10876   n)
10877
10878 (defun gnus-summary-refer-references ()
10879   "Fetch all articles mentioned in the References header.
10880 Return how many articles were fetched."
10881   (interactive)
10882   (gnus-set-global-variables)
10883   (let ((ref (mail-header-references (gnus-summary-article-header)))
10884         (current (gnus-summary-article-number))
10885         (n 0))
10886     ;; For each Message-ID in the References header...
10887     (while (string-match "<[^>]*>" ref)
10888       (incf n)
10889       ;; ... fetch that article.
10890       (gnus-summary-refer-article
10891        (prog1 (match-string 0 ref)
10892          (setq ref (substring ref (match-end 0))))))
10893     (gnus-summary-goto-subject current)
10894     (gnus-summary-position-point)
10895     n))
10896
10897 (defun gnus-summary-refer-article (message-id)
10898   "Fetch an article specified by MESSAGE-ID."
10899   (interactive "sMessage-ID: ")
10900   (when (and (stringp message-id)
10901              (not (zerop (length message-id))))
10902     ;; Construct the correct Message-ID if necessary.
10903     ;; Suggested by tale@pawl.rpi.edu.
10904     (unless (string-match "^<" message-id)
10905       (setq message-id (concat "<" message-id)))
10906     (unless (string-match ">$" message-id)
10907       (setq message-id (concat message-id ">")))
10908     (let ((header (car (gnus-gethash message-id
10909                                      gnus-newsgroup-dependencies))))
10910       (if header
10911           ;; The article is present in the buffer, to we just go to it.
10912           (gnus-summary-goto-article (mail-header-number header) nil t)
10913         ;; We fetch the article
10914         (let ((gnus-override-method 
10915                (and (gnus-news-group-p gnus-newsgroup-name)
10916                     gnus-refer-article-method))
10917               number)
10918           ;; Start the special refer-article method, if necessary.
10919           (when (and gnus-refer-article-method
10920                      (gnus-news-group-p gnus-newsgroup-name))
10921             (gnus-check-server gnus-refer-article-method))
10922           ;; Fetch the header, and display the article.
10923           (if (setq number (gnus-summary-insert-subject message-id))
10924               (gnus-summary-select-article nil nil nil number)
10925             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10926
10927 (defun gnus-summary-enter-digest-group (&optional force)
10928   "Enter a digest group based on the current article."
10929   (interactive "P")
10930   (gnus-set-global-variables)
10931   (gnus-summary-select-article)
10932   (let ((name (format "%s-%d"
10933                       (gnus-group-prefixed-name
10934                        gnus-newsgroup-name (list 'nndoc ""))
10935                       gnus-current-article))
10936         (ogroup gnus-newsgroup-name)
10937         (case-fold-search t)
10938         (buf (current-buffer))
10939         dig)
10940     (save-excursion
10941       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10942       (insert-buffer-substring gnus-original-article-buffer)
10943       (narrow-to-region
10944        (goto-char (point-min))
10945        (or (search-forward "\n\n" nil t) (point)))
10946       (goto-char (point-min))
10947       (delete-matching-lines "^\\(Path\\):\\|^From ")
10948       (widen))
10949     (unwind-protect
10950         (if (gnus-group-read-ephemeral-group
10951              name `(nndoc ,name (nndoc-address
10952                                  ,(get-buffer dig))
10953                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10954             ;; Make all postings to this group go to the parent group.
10955             (nconc (gnus-info-params (gnus-get-info name))
10956                    (list (cons 'to-group ogroup)))
10957           ;; Couldn't select this doc group.
10958           (switch-to-buffer buf)
10959           (gnus-set-global-variables)
10960           (gnus-configure-windows 'summary)
10961           (gnus-message 3 "Article couldn't be entered?"))
10962       (kill-buffer dig))))
10963
10964 (defun gnus-summary-isearch-article (&optional regexp-p)
10965   "Do incremental search forward on the current article.
10966 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10967   (interactive "P")
10968   (gnus-set-global-variables)
10969   (gnus-summary-select-article)
10970   (gnus-configure-windows 'article)
10971   (gnus-eval-in-buffer-window
10972    gnus-article-buffer
10973    (goto-char (point-min))
10974    (isearch-forward regexp-p)))
10975
10976 (defun gnus-summary-search-article-forward (regexp &optional backward)
10977   "Search for an article containing REGEXP forward.
10978 If BACKWARD, search backward instead."
10979   (interactive
10980    (list (read-string
10981           (format "Search article %s (regexp%s): "
10982                   (if current-prefix-arg "backward" "forward")
10983                   (if gnus-last-search-regexp
10984                       (concat ", default " gnus-last-search-regexp)
10985                     "")))
10986          current-prefix-arg))
10987   (gnus-set-global-variables)
10988   (if (string-equal regexp "")
10989       (setq regexp (or gnus-last-search-regexp ""))
10990     (setq gnus-last-search-regexp regexp))
10991   (if (gnus-summary-search-article regexp backward)
10992       (gnus-article-set-window-start
10993        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10994     (error "Search failed: \"%s\"" regexp)))
10995
10996 (defun gnus-summary-search-article-backward (regexp)
10997   "Search for an article containing REGEXP backward."
10998   (interactive
10999    (list (read-string
11000           (format "Search article backward (regexp%s): "
11001                   (if gnus-last-search-regexp
11002                       (concat ", default " gnus-last-search-regexp)
11003                     "")))))
11004   (gnus-summary-search-article-forward regexp 'backward))
11005
11006 (defun gnus-summary-search-article (regexp &optional backward)
11007   "Search for an article containing REGEXP.
11008 Optional argument BACKWARD means do search for backward.
11009 gnus-select-article-hook is not called during the search."
11010   (let ((gnus-select-article-hook nil)  ;Disable hook.
11011         (gnus-article-display-hook nil)
11012         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11013         (re-search
11014          (if backward
11015              (function re-search-backward) (function re-search-forward)))
11016         (found nil)
11017         (last nil))
11018     ;; Hidden thread subtrees must be searched for ,too.
11019     (gnus-summary-show-all-threads)
11020     ;; First of all, search current article.
11021     ;; We don't want to read article again from NNTP server nor reset
11022     ;; current point.
11023     (gnus-summary-select-article)
11024     (gnus-message 9 "Searching article: %d..." gnus-current-article)
11025     (setq last gnus-current-article)
11026     (gnus-eval-in-buffer-window
11027      gnus-article-buffer
11028      (save-restriction
11029        (widen)
11030        ;; Begin search from current point.
11031        (setq found (funcall re-search regexp nil t))))
11032     ;; Then search next articles.
11033     (while (and (not found)
11034                 (gnus-summary-display-article
11035                  (if backward (gnus-summary-find-prev)
11036                    (gnus-summary-find-next))))
11037       (gnus-message 9 "Searching article: %d..." gnus-current-article)
11038       (gnus-eval-in-buffer-window
11039        gnus-article-buffer
11040        (save-restriction
11041          (widen)
11042          (goto-char (if backward (point-max) (point-min)))
11043          (setq found (funcall re-search regexp nil t)))))
11044     (message "")
11045     ;; Adjust article pointer.
11046     (or (eq last gnus-current-article)
11047         (setq gnus-last-article last))
11048     ;; Return T if found such article.
11049     found))
11050
11051 (defun gnus-summary-find-matching (header regexp &optional backward unread
11052                                           not-case-fold)
11053   "Return a list of all articles that match REGEXP on HEADER.
11054 The search stars on the current article and goes forwards unless
11055 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11056 If UNREAD is non-nil, only unread articles will
11057 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11058 in the comparisons."
11059   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11060                 (gnus-data-find-list
11061                  (gnus-summary-article-number) (gnus-data-list backward))))
11062         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11063         (case-fold-search (not not-case-fold))
11064         articles d)
11065     (or (fboundp (intern (concat "mail-header-" header)))
11066         (error "%s is not a valid header" header))
11067     (while data
11068       (setq d (car data))
11069       (and (or (not unread)             ; We want all articles...
11070                (gnus-data-unread-p d))  ; Or just unreads.
11071            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11072            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11073            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11074       (setq data (cdr data)))
11075     (nreverse articles)))
11076
11077 (defun gnus-summary-execute-command (header regexp command &optional backward)
11078   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11079 If HEADER is an empty string (or nil), the match is done on the entire
11080 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11081   (interactive
11082    (list (let ((completion-ignore-case t))
11083            (completing-read
11084             "Header name: "
11085             (mapcar (lambda (string) (list string))
11086                     '("Number" "Subject" "From" "Lines" "Date"
11087                       "Message-ID" "Xref" "References" "Body"))
11088             nil 'require-match))
11089          (read-string "Regexp: ")
11090          (read-key-sequence "Command: ")
11091          current-prefix-arg))
11092   (when (equal header "Body")
11093     (setq header ""))
11094   (gnus-set-global-variables)
11095   ;; Hidden thread subtrees must be searched as well.
11096   (gnus-summary-show-all-threads)
11097   ;; We don't want to change current point nor window configuration.
11098   (save-excursion
11099     (save-window-excursion
11100       (gnus-message 6 "Executing %s..." (key-description command))
11101       ;; We'd like to execute COMMAND interactively so as to give arguments.
11102       (gnus-execute header regexp
11103                     `(lambda () (call-interactively ',(key-binding command)))
11104                     backward)
11105       (gnus-message 6 "Executing %s...done" (key-description command)))))
11106
11107 (defun gnus-summary-beginning-of-article ()
11108   "Scroll the article back to the beginning."
11109   (interactive)
11110   (gnus-set-global-variables)
11111   (gnus-summary-select-article)
11112   (gnus-configure-windows 'article)
11113   (gnus-eval-in-buffer-window
11114    gnus-article-buffer
11115    (widen)
11116    (goto-char (point-min))
11117    (and gnus-break-pages (gnus-narrow-to-page))))
11118
11119 (defun gnus-summary-end-of-article ()
11120   "Scroll to the end of the article."
11121   (interactive)
11122   (gnus-set-global-variables)
11123   (gnus-summary-select-article)
11124   (gnus-configure-windows 'article)
11125   (gnus-eval-in-buffer-window
11126    gnus-article-buffer
11127    (widen)
11128    (goto-char (point-max))
11129    (recenter -3)
11130    (and gnus-break-pages (gnus-narrow-to-page))))
11131
11132 (defun gnus-summary-show-article (&optional arg)
11133   "Force re-fetching of the current article.
11134 If ARG (the prefix) is non-nil, show the raw article without any
11135 article massaging functions being run."
11136   (interactive "P")
11137   (gnus-set-global-variables)
11138   (if (not arg)
11139       ;; Select the article the normal way.
11140       (gnus-summary-select-article nil 'force)
11141     ;; Bind the article treatment functions to nil.
11142     (let ((gnus-have-all-headers t)
11143           gnus-article-display-hook
11144           gnus-article-prepare-hook
11145           gnus-break-pages
11146           gnus-visual)
11147       (gnus-summary-select-article nil 'force)))
11148   (gnus-summary-goto-subject gnus-current-article)
11149 ;  (gnus-configure-windows 'article)
11150   (gnus-summary-position-point))
11151
11152 (defun gnus-summary-verbose-headers (&optional arg)
11153   "Toggle permanent full header display.
11154 If ARG is a positive number, turn header display on.
11155 If ARG is a negative number, turn header display off."
11156   (interactive "P")
11157   (gnus-set-global-variables)
11158   (gnus-summary-toggle-header arg)
11159   (setq gnus-show-all-headers
11160         (cond ((or (not (numberp arg))
11161                    (zerop arg))
11162                (not gnus-show-all-headers))
11163               ((natnump arg)
11164                t))))
11165
11166 (defun gnus-summary-toggle-header (&optional arg)
11167   "Show the headers if they are hidden, or hide them if they are shown.
11168 If ARG is a positive number, show the entire header.
11169 If ARG is a negative number, hide the unwanted header lines."
11170   (interactive "P")
11171   (gnus-set-global-variables)
11172   (save-excursion
11173     (set-buffer gnus-article-buffer)
11174     (let* ((buffer-read-only nil)
11175            (inhibit-point-motion-hooks t)
11176            (hidden (text-property-any
11177                     (goto-char (point-min)) (search-forward "\n\n")
11178                     'invisible t))
11179            e)
11180       (goto-char (point-min))
11181       (when (search-forward "\n\n" nil t)
11182         (delete-region (point-min) (1- (point))))
11183       (goto-char (point-min))
11184       (save-excursion
11185         (set-buffer gnus-original-article-buffer)
11186         (goto-char (point-min))
11187         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11188       (insert-buffer-substring gnus-original-article-buffer 1 e)
11189       (let ((gnus-inhibit-hiding t))
11190         (run-hooks 'gnus-article-display-hook))
11191       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11192           (gnus-article-hide-headers)))))
11193
11194 (defun gnus-summary-show-all-headers ()
11195   "Make all header lines visible."
11196   (interactive)
11197   (gnus-set-global-variables)
11198   (gnus-article-show-all-headers))
11199
11200 (defun gnus-summary-toggle-mime (&optional arg)
11201   "Toggle MIME processing.
11202 If ARG is a positive number, turn MIME processing on."
11203   (interactive "P")
11204   (gnus-set-global-variables)
11205   (setq gnus-show-mime
11206         (if (null arg) (not gnus-show-mime)
11207           (> (prefix-numeric-value arg) 0)))
11208   (gnus-summary-select-article t 'force))
11209
11210 (defun gnus-summary-caesar-message (&optional arg)
11211   "Caesar rotate the current article by 13.
11212 The numerical prefix specifies how manu places to rotate each letter
11213 forward."
11214   (interactive "P")
11215   (gnus-set-global-variables)
11216   (gnus-summary-select-article)
11217   (let ((mail-header-separator ""))
11218     (gnus-eval-in-buffer-window
11219      gnus-article-buffer
11220      (save-restriction
11221        (widen)
11222        (let ((start (window-start)))
11223          (news-caesar-buffer-body arg)
11224          (set-window-start (get-buffer-window (current-buffer)) start))))))
11225
11226 (defun gnus-summary-stop-page-breaking ()
11227   "Stop page breaking in the current article."
11228   (interactive)
11229   (gnus-set-global-variables)
11230   (gnus-summary-select-article)
11231   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11232
11233 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11234   "Move the current article to a different newsgroup.
11235 If N is a positive number, move the N next articles.
11236 If N is a negative number, move the N previous articles.
11237 If N is nil and any articles have been marked with the process mark,
11238 move those articles instead.
11239 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11240 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11241 re-spool using this method.
11242
11243 For this function to work, both the current newsgroup and the
11244 newsgroup that you want to move to have to support the `request-move'
11245 and `request-accept' functions."
11246   (interactive "P")
11247   (unless action (setq action 'move))
11248   (gnus-set-global-variables)
11249   ;; Check whether the source group supports the required functions.
11250   (cond ((and (eq action 'move)
11251               (not (gnus-check-backend-function
11252                     'request-move-article gnus-newsgroup-name)))
11253          (error "The current group does not support article moving"))
11254         ((and (eq action 'crosspost)
11255               (not (gnus-check-backend-function
11256                     'request-replace-article gnus-newsgroup-name)))
11257          (error "The current group does not support article editing")))
11258   (let ((articles (gnus-summary-work-articles n))
11259         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11260         (names '((move "Move" "Moving")
11261                  (copy "Copy" "Copying")
11262                  (crosspost "Crosspost" "Crossposting")))
11263         (copy-buf (save-excursion
11264                     (nnheader-set-temp-buffer " *copy article*")))
11265         art-group to-method new-xref article to-groups)
11266     (unless (assq action names)
11267       (error "Unknown action %s" action))
11268     ;; Read the newsgroup name.
11269     (when (and (not to-newsgroup)
11270                (not select-method))
11271       (setq to-newsgroup
11272             (gnus-read-move-group-name
11273              (cadr (assq action names))
11274              (symbol-value (intern (format "gnus-current-%s-group" action)))
11275              articles prefix))
11276       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11277     (setq to-method (or select-method 
11278                         (gnus-find-method-for-group to-newsgroup)))
11279     ;; Check the method we are to move this article to...
11280     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11281         (error "%s does not support article copying" (car to-method)))
11282     (or (gnus-check-server to-method)
11283         (error "Can't open server %s" (car to-method)))
11284     (gnus-message 6 "%s to %s: %s..."
11285                   (caddr (assq action names))
11286                   (or (car select-method) to-newsgroup) articles)
11287     (while articles
11288       (setq article (pop articles))
11289       (setq
11290        art-group
11291        (cond
11292         ;; Move the article.
11293         ((eq action 'move)
11294          (gnus-request-move-article
11295           article                       ; Article to move
11296           gnus-newsgroup-name           ; From newsgrouo
11297           (nth 1 (gnus-find-method-for-group
11298                   gnus-newsgroup-name)) ; Server
11299           (list 'gnus-request-accept-article
11300                 to-newsgroup (list 'quote select-method)
11301                 (not articles))         ; Accept form
11302           (not articles)))              ; Only save nov last time
11303         ;; Copy the article.
11304         ((eq action 'copy)
11305          (save-excursion
11306            (set-buffer copy-buf)
11307            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11308            (gnus-request-accept-article
11309             to-newsgroup select-method (not articles))))
11310         ;; Crosspost the article.
11311         ((eq action 'crosspost)
11312          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11313            (setq new-xref (concat gnus-newsgroup-name ":" article))
11314            (if (and xref (not (string= xref "")))
11315                (progn
11316                  (when (string-match "^Xref: " xref)
11317                    (setq xref (substring xref (match-end 0))))
11318                  (setq new-xref (concat xref " " new-xref)))
11319              (setq new-xref (concat (system-name) " " new-xref)))
11320            (save-excursion
11321              (set-buffer copy-buf)
11322              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11323              (nnheader-replace-header "xref" new-xref)
11324              (gnus-request-accept-article
11325               to-newsgroup select-method (not articles)))))))
11326       (if (not art-group)
11327           (gnus-message 1 "Couldn't %s article %s"
11328                         (cadr (assq action names)) article)
11329         (let* ((entry
11330                 (or
11331                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11332                  (gnus-gethash
11333                   (gnus-group-prefixed-name
11334                    (car art-group)
11335                    (or select-method 
11336                        (gnus-find-method-for-group to-newsgroup)))
11337                   gnus-newsrc-hashtb)))
11338                (info (nth 2 entry))
11339                (to-group (gnus-info-group info)))
11340           ;; Update the group that has been moved to.
11341           (when (and info
11342                      (memq action '(move copy)))
11343             (unless (member to-group to-groups)
11344               (push to-group to-groups))
11345
11346             (unless (memq article gnus-newsgroup-unreads)
11347               (gnus-info-set-read
11348                info (gnus-add-to-range (gnus-info-read info)
11349                                        (list (cdr art-group)))))
11350
11351             ;; Copy any marks over to the new group.
11352             (let ((marks gnus-article-mark-lists)
11353                   (to-article (cdr art-group)))
11354
11355               ;; See whether the article is to be put in the cache.
11356               (when gnus-use-cache
11357                 (gnus-cache-possibly-enter-article
11358                  to-group to-article
11359                  (let ((header (copy-sequence
11360                                 (gnus-summary-article-header article))))
11361                    (mail-header-set-number header to-article)
11362                    header)
11363                  (memq article gnus-newsgroup-marked)
11364                  (memq article gnus-newsgroup-dormant)
11365                  (memq article gnus-newsgroup-unreads)))
11366
11367               (while marks
11368                 (when (memq article (symbol-value
11369                                      (intern (format "gnus-newsgroup-%s"
11370                                                      (caar marks)))))
11371                   ;; If the other group is the same as this group,
11372                   ;; then we have to add the mark to the list.
11373                   (when (equal to-group gnus-newsgroup-name)
11374                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11375                          (cons to-article
11376                                (symbol-value
11377                                 (intern (format "gnus-newsgroup-%s"
11378                                                 (caar marks)))))))
11379                   ;; Copy mark to other group.
11380                   (gnus-add-marked-articles
11381                    to-group (cdar marks) (list to-article) info))
11382                 (setq marks (cdr marks)))))
11383
11384           ;; Update the Xref header in this article to point to
11385           ;; the new crossposted article we have just created.
11386           (when (eq action 'crosspost)
11387             (save-excursion
11388               (set-buffer copy-buf)
11389               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11390               (nnheader-replace-header
11391                "xref" (concat new-xref " " (gnus-group-prefixed-name
11392                                             (car art-group) to-method)
11393                               ":" (cdr art-group)))
11394               (gnus-request-replace-article
11395                article gnus-newsgroup-name (current-buffer)))))
11396
11397         (gnus-summary-goto-subject article)
11398         (when (eq action 'move)
11399           (gnus-summary-mark-article article gnus-canceled-mark)))
11400       (gnus-summary-remove-process-mark article))
11401     ;; Re-activate all groups that have been moved to.
11402     (while to-groups
11403       (gnus-activate-group (pop to-groups)))
11404     
11405     (gnus-kill-buffer copy-buf)
11406     (gnus-summary-position-point)
11407     (gnus-set-mode-line 'summary)))
11408
11409 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11410   "Move the current article to a different newsgroup.
11411 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11412 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11413 re-spool using this method."
11414   (interactive "P")
11415   (gnus-summary-move-article n nil select-method 'copy))
11416
11417 (defun gnus-summary-crosspost-article (&optional n)
11418   "Crosspost the current article to some other group."
11419   (interactive "P")
11420   (gnus-summary-move-article n nil nil 'crosspost))
11421
11422 (defvar gnus-summary-respool-default-method nil
11423   "Default method for respooling an article.  
11424 If nil, use to the current newsgroup method.")
11425
11426 (defun gnus-summary-respool-article (&optional n method)
11427   "Respool the current article.
11428 The article will be squeezed through the mail spooling process again,
11429 which means that it will be put in some mail newsgroup or other
11430 depending on `nnmail-split-methods'.
11431 If N is a positive number, respool the N next articles.
11432 If N is a negative number, respool the N previous articles.
11433 If N is nil and any articles have been marked with the process mark,
11434 respool those articles instead.
11435
11436 Respooling can be done both from mail groups and \"real\" newsgroups.
11437 In the former case, the articles in question will be moved from the
11438 current group into whatever groups they are destined to.  In the
11439 latter case, they will be copied into the relevant groups."
11440   (interactive 
11441    (list current-prefix-arg
11442          (let* ((methods (gnus-methods-using 'respool))
11443                 (methname
11444                  (symbol-name (or gnus-summary-respool-default-method
11445                                   (car (gnus-find-method-for-group
11446                                         gnus-newsgroup-name)))))
11447                 (method
11448                  (gnus-completing-read 
11449                   methname "What backend do you want to use when? "
11450                   methods nil t nil 'gnus-method-history))
11451                 ms)
11452            (cond
11453             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11454              (list (intern method) ""))
11455             ((= 1 (length ms))
11456              (car ms))
11457             (t
11458              (cdr (completing-read 
11459                    "Server name: "
11460                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11461   (gnus-set-global-variables)
11462   (unless method
11463     (error "No method given for respooling"))
11464   (if (assoc (symbol-name
11465               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11466              (gnus-methods-using 'respool))
11467       (gnus-summary-move-article n nil method)
11468     (gnus-summary-copy-article n nil method)))
11469
11470 (defun gnus-summary-import-article (file)
11471   "Import a random file into a mail newsgroup."
11472   (interactive "fImport file: ")
11473   (gnus-set-global-variables)
11474   (let ((group gnus-newsgroup-name)
11475         (now (current-time))
11476         atts lines)
11477     (or (gnus-check-backend-function 'request-accept-article group)
11478         (error "%s does not support article importing" group))
11479     (or (file-readable-p file)
11480         (not (file-regular-p file))
11481         (error "Can't read %s" file))
11482     (save-excursion
11483       (set-buffer (get-buffer-create " *import file*"))
11484       (buffer-disable-undo (current-buffer))
11485       (erase-buffer)
11486       (insert-file-contents file)
11487       (goto-char (point-min))
11488       (unless (nnheader-article-p)
11489         ;; This doesn't look like an article, so we fudge some headers.
11490         (setq atts (file-attributes file)
11491               lines (count-lines (point-min) (point-max)))
11492         (insert "From: " (read-string "From: ") "\n"
11493                 "Subject: " (read-string "Subject: ") "\n"
11494                 "Date: " (timezone-make-date-arpa-standard
11495                           (current-time-string (nth 5 atts))
11496                           (current-time-zone now)
11497                           (current-time-zone now)) "\n"
11498                 "Message-ID: " (gnus-inews-message-id) "\n"
11499                 "Lines: " (int-to-string lines) "\n"
11500                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11501       (gnus-request-accept-article group nil t)
11502       (kill-buffer (current-buffer)))))
11503
11504 (defun gnus-summary-expire-articles (&optional now)
11505   "Expire all articles that are marked as expirable in the current group."
11506   (interactive)
11507   (gnus-set-global-variables)
11508   (when (gnus-check-backend-function
11509          'request-expire-articles gnus-newsgroup-name)
11510     ;; This backend supports expiry.
11511     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11512            (expirable (if total
11513                           (gnus-list-of-read-articles gnus-newsgroup-name)
11514                         (setq gnus-newsgroup-expirable
11515                               (sort gnus-newsgroup-expirable '<))))
11516            (expiry-wait (if now 'immediate
11517                           (gnus-group-get-parameter
11518                            gnus-newsgroup-name 'expiry-wait)))
11519            es)
11520       (when expirable
11521         ;; There are expirable articles in this group, so we run them
11522         ;; through the expiry process.
11523         (gnus-message 6 "Expiring articles...")
11524         ;; The list of articles that weren't expired is returned.
11525         (if expiry-wait
11526             (let ((nnmail-expiry-wait-function nil)
11527                   (nnmail-expiry-wait expiry-wait))
11528               (setq es (gnus-request-expire-articles
11529                         expirable gnus-newsgroup-name)))
11530           (setq es (gnus-request-expire-articles
11531                     expirable gnus-newsgroup-name)))
11532         (or total (setq gnus-newsgroup-expirable es))
11533         ;; We go through the old list of expirable, and mark all
11534         ;; really expired articles as nonexistent.
11535         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11536           (let ((gnus-use-cache nil))
11537             (while expirable
11538               (unless (memq (car expirable) es)
11539                 (when (gnus-data-find (car expirable))
11540                   (gnus-summary-mark-article
11541                    (car expirable) gnus-canceled-mark)))
11542               (setq expirable (cdr expirable)))))
11543         (gnus-message 6 "Expiring articles...done")))))
11544
11545 (defun gnus-summary-expire-articles-now ()
11546   "Expunge all expirable articles in the current group.
11547 This means that *all* articles that are marked as expirable will be
11548 deleted forever, right now."
11549   (interactive)
11550   (gnus-set-global-variables)
11551   (or gnus-expert-user
11552       (gnus-y-or-n-p
11553        "Are you really, really, really sure you want to delete all these messages? ")
11554       (error "Phew!"))
11555   (gnus-summary-expire-articles t))
11556
11557 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11558 (defun gnus-summary-delete-article (&optional n)
11559   "Delete the N next (mail) articles.
11560 This command actually deletes articles.  This is not a marking
11561 command.  The article will disappear forever from your life, never to
11562 return.
11563 If N is negative, delete backwards.
11564 If N is nil and articles have been marked with the process mark,
11565 delete these instead."
11566   (interactive "P")
11567   (gnus-set-global-variables)
11568   (or (gnus-check-backend-function 'request-expire-articles
11569                                    gnus-newsgroup-name)
11570       (error "The current newsgroup does not support article deletion."))
11571   ;; Compute the list of articles to delete.
11572   (let ((articles (gnus-summary-work-articles n))
11573         not-deleted)
11574     (if (and gnus-novice-user
11575              (not (gnus-y-or-n-p
11576                    (format "Do you really want to delete %s forever? "
11577                            (if (> (length articles) 1) 
11578                                (format "these %s articles" (length articles))
11579                              "this article")))))
11580         ()
11581       ;; Delete the articles.
11582       (setq not-deleted (gnus-request-expire-articles
11583                          articles gnus-newsgroup-name 'force))
11584       (while articles
11585         (gnus-summary-remove-process-mark (car articles))
11586         ;; The backend might not have been able to delete the article
11587         ;; after all.
11588         (or (memq (car articles) not-deleted)
11589             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11590         (setq articles (cdr articles))))
11591     (gnus-summary-position-point)
11592     (gnus-set-mode-line 'summary)
11593     not-deleted))
11594
11595 (defun gnus-summary-edit-article (&optional force)
11596   "Enter into a buffer and edit the current article.
11597 This will have permanent effect only in mail groups.
11598 If FORCE is non-nil, allow editing of articles even in read-only
11599 groups."
11600   (interactive "P")
11601   (save-excursion
11602     (set-buffer gnus-summary-buffer)
11603     (gnus-set-global-variables)
11604     (when (and (not force)
11605                (gnus-group-read-only-p))
11606       (error "The current newsgroup does not support article editing."))
11607     (gnus-summary-select-article t nil t)
11608     (gnus-configure-windows 'article)
11609     (select-window (get-buffer-window gnus-article-buffer))
11610     (gnus-message 6 "C-c C-c to end edits")
11611     (setq buffer-read-only nil)
11612     (text-mode)
11613     (use-local-map (copy-keymap (current-local-map)))
11614     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11615     (buffer-enable-undo)
11616     (widen)
11617     (goto-char (point-min))
11618     (search-forward "\n\n" nil t)))
11619
11620 (defun gnus-summary-edit-article-done ()
11621   "Make edits to the current article permanent."
11622   (interactive)
11623   (if (gnus-group-read-only-p)
11624       (progn
11625         (gnus-summary-edit-article-postpone)
11626         (gnus-error
11627          1 "The current newsgroup does not support article editing."))
11628     (let ((buf (format "%s" (buffer-string))))
11629       (erase-buffer)
11630       (insert buf)
11631       (if (not (gnus-request-replace-article
11632                 (cdr gnus-article-current) (car gnus-article-current)
11633                 (current-buffer)))
11634           (error "Couldn't replace article.")
11635         (gnus-article-mode)
11636         (use-local-map gnus-article-mode-map)
11637         (setq buffer-read-only t)
11638         (buffer-disable-undo (current-buffer))
11639         (gnus-configure-windows 'summary)
11640         (gnus-summary-update-article (cdr gnus-article-current))
11641         (when gnus-use-cache
11642           (gnus-cache-update-article 
11643            (cdr gnus-article-current) (car gnus-article-current)))
11644         (when gnus-keep-backlog
11645           (gnus-backlog-remove-article 
11646            (car gnus-article-current) (cdr gnus-article-current))))
11647       (save-excursion
11648         (when (get-buffer gnus-original-article-buffer)
11649           (set-buffer gnus-original-article-buffer)
11650           (setq gnus-original-article nil)))
11651       (setq gnus-article-current nil
11652             gnus-current-article nil)
11653       (run-hooks 'gnus-article-display-hook)
11654       (and (gnus-visual-p 'summary-highlight 'highlight)
11655            (run-hooks 'gnus-visual-mark-article-hook)))))
11656
11657 (defun gnus-summary-edit-article-postpone ()
11658   "Postpone changes to the current article."
11659   (interactive)
11660   (gnus-article-mode)
11661   (use-local-map gnus-article-mode-map)
11662   (setq buffer-read-only t)
11663   (buffer-disable-undo (current-buffer))
11664   (gnus-configure-windows 'summary)
11665   (and (gnus-visual-p 'summary-highlight 'highlight)
11666        (run-hooks 'gnus-visual-mark-article-hook)))
11667
11668 (defun gnus-summary-respool-query ()
11669   "Query where the respool algorithm would put this article."
11670   (interactive)
11671   (gnus-set-global-variables)
11672   (gnus-summary-select-article)
11673   (save-excursion
11674     (set-buffer gnus-article-buffer)
11675     (save-restriction
11676       (goto-char (point-min))
11677       (search-forward "\n\n")
11678       (narrow-to-region (point-min) (point))
11679       (pp-eval-expression
11680        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11681
11682 ;; Summary score commands.
11683
11684 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11685
11686 (defun gnus-summary-raise-score (n)
11687   "Raise the score of the current article by N."
11688   (interactive "p")
11689   (gnus-set-global-variables)
11690   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11691
11692 (defun gnus-summary-set-score (n)
11693   "Set the score of the current article to N."
11694   (interactive "p")
11695   (gnus-set-global-variables)
11696   (save-excursion
11697     (gnus-summary-show-thread)
11698     (let ((buffer-read-only nil))
11699       ;; Set score.
11700       (gnus-summary-update-mark
11701        (if (= n (or gnus-summary-default-score 0)) ? 
11702          (if (< n (or gnus-summary-default-score 0))
11703              gnus-score-below-mark gnus-score-over-mark)) 'score))
11704     (let* ((article (gnus-summary-article-number))
11705            (score (assq article gnus-newsgroup-scored)))
11706       (if score (setcdr score n)
11707         (setq gnus-newsgroup-scored
11708               (cons (cons article n) gnus-newsgroup-scored))))
11709     (gnus-summary-update-line)))
11710
11711 (defun gnus-summary-current-score ()
11712   "Return the score of the current article."
11713   (interactive)
11714   (gnus-set-global-variables)
11715   (gnus-message 1 "%s" (gnus-summary-article-score)))
11716
11717 ;; Summary marking commands.
11718
11719 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11720   "Mark articles which has the same subject as read, and then select the next.
11721 If UNMARK is positive, remove any kind of mark.
11722 If UNMARK is negative, tick articles."
11723   (interactive "P")
11724   (gnus-set-global-variables)
11725   (if unmark
11726       (setq unmark (prefix-numeric-value unmark)))
11727   (let ((count
11728          (gnus-summary-mark-same-subject
11729           (gnus-summary-article-subject) unmark)))
11730     ;; Select next unread article.  If auto-select-same mode, should
11731     ;; select the first unread article.
11732     (gnus-summary-next-article t (and gnus-auto-select-same
11733                                       (gnus-summary-article-subject)))
11734     (gnus-message 7 "%d article%s marked as %s"
11735                   count (if (= count 1) " is" "s are")
11736                   (if unmark "unread" "read"))))
11737
11738 (defun gnus-summary-kill-same-subject (&optional unmark)
11739   "Mark articles which has the same subject as read.
11740 If UNMARK is positive, remove any kind of mark.
11741 If UNMARK is negative, tick articles."
11742   (interactive "P")
11743   (gnus-set-global-variables)
11744   (if unmark
11745       (setq unmark (prefix-numeric-value unmark)))
11746   (let ((count
11747          (gnus-summary-mark-same-subject
11748           (gnus-summary-article-subject) unmark)))
11749     ;; If marked as read, go to next unread subject.
11750     (if (null unmark)
11751         ;; Go to next unread subject.
11752         (gnus-summary-next-subject 1 t))
11753     (gnus-message 7 "%d articles are marked as %s"
11754                   count (if unmark "unread" "read"))))
11755
11756 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11757   "Mark articles with same SUBJECT as read, and return marked number.
11758 If optional argument UNMARK is positive, remove any kinds of marks.
11759 If optional argument UNMARK is negative, mark articles as unread instead."
11760   (let ((count 1))
11761     (save-excursion
11762       (cond
11763        ((null unmark)                   ; Mark as read.
11764         (while (and
11765                 (progn
11766                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11767                   (gnus-summary-show-thread) t)
11768                 (gnus-summary-find-subject subject))
11769           (setq count (1+ count))))
11770        ((> unmark 0)                    ; Tick.
11771         (while (and
11772                 (progn
11773                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11774                   (gnus-summary-show-thread) t)
11775                 (gnus-summary-find-subject subject))
11776           (setq count (1+ count))))
11777        (t                               ; Mark as unread.
11778         (while (and
11779                 (progn
11780                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11781                   (gnus-summary-show-thread) t)
11782                 (gnus-summary-find-subject subject))
11783           (setq count (1+ count)))))
11784       (gnus-set-mode-line 'summary)
11785       ;; Return the number of marked articles.
11786       count)))
11787
11788 (defun gnus-summary-mark-as-processable (n &optional unmark)
11789   "Set the process mark on the next N articles.
11790 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11791 the process mark instead.  The difference between N and the actual
11792 number of articles marked is returned."
11793   (interactive "p")
11794   (gnus-set-global-variables)
11795   (let ((backward (< n 0))
11796         (n (abs n)))
11797     (while (and
11798             (> n 0)
11799             (if unmark
11800                 (gnus-summary-remove-process-mark
11801                  (gnus-summary-article-number))
11802               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11803             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11804       (setq n (1- n)))
11805     (if (/= 0 n) (gnus-message 7 "No more articles"))
11806     (gnus-summary-recenter)
11807     (gnus-summary-position-point)
11808     n))
11809
11810 (defun gnus-summary-unmark-as-processable (n)
11811   "Remove the process mark from the next N articles.
11812 If N is negative, mark backward instead.  The difference between N and
11813 the actual number of articles marked is returned."
11814   (interactive "p")
11815   (gnus-set-global-variables)
11816   (gnus-summary-mark-as-processable n t))
11817
11818 (defun gnus-summary-unmark-all-processable ()
11819   "Remove the process mark from all articles."
11820   (interactive)
11821   (gnus-set-global-variables)
11822   (save-excursion
11823     (while gnus-newsgroup-processable
11824       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11825   (gnus-summary-position-point))
11826
11827 (defun gnus-summary-mark-as-expirable (n)
11828   "Mark N articles forward as expirable.
11829 If N is negative, mark backward instead.  The difference between N and
11830 the actual number of articles marked is returned."
11831   (interactive "p")
11832   (gnus-set-global-variables)
11833   (gnus-summary-mark-forward n gnus-expirable-mark))
11834
11835 (defun gnus-summary-mark-article-as-replied (article)
11836   "Mark ARTICLE replied and update the summary line."
11837   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11838   (let ((buffer-read-only nil))
11839     (when (gnus-summary-goto-subject article)
11840       (gnus-summary-update-secondary-mark article))))
11841
11842 (defun gnus-summary-set-bookmark (article)
11843   "Set a bookmark in current article."
11844   (interactive (list (gnus-summary-article-number)))
11845   (gnus-set-global-variables)
11846   (if (or (not (get-buffer gnus-article-buffer))
11847           (not gnus-current-article)
11848           (not gnus-article-current)
11849           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11850       (error "No current article selected"))
11851   ;; Remove old bookmark, if one exists.
11852   (let ((old (assq article gnus-newsgroup-bookmarks)))
11853     (if old (setq gnus-newsgroup-bookmarks
11854                   (delq old gnus-newsgroup-bookmarks))))
11855   ;; Set the new bookmark, which is on the form
11856   ;; (article-number . line-number-in-body).
11857   (setq gnus-newsgroup-bookmarks
11858         (cons
11859          (cons article
11860                (save-excursion
11861                  (set-buffer gnus-article-buffer)
11862                  (count-lines
11863                   (min (point)
11864                        (save-excursion
11865                          (goto-char (point-min))
11866                          (search-forward "\n\n" nil t)
11867                          (point)))
11868                   (point))))
11869          gnus-newsgroup-bookmarks))
11870   (gnus-message 6 "A bookmark has been added to the current article."))
11871
11872 (defun gnus-summary-remove-bookmark (article)
11873   "Remove the bookmark from the current article."
11874   (interactive (list (gnus-summary-article-number)))
11875   (gnus-set-global-variables)
11876   ;; Remove old bookmark, if one exists.
11877   (let ((old (assq article gnus-newsgroup-bookmarks)))
11878     (if old
11879         (progn
11880           (setq gnus-newsgroup-bookmarks
11881                 (delq old gnus-newsgroup-bookmarks))
11882           (gnus-message 6 "Removed bookmark."))
11883       (gnus-message 6 "No bookmark in current article."))))
11884
11885 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11886 (defun gnus-summary-mark-as-dormant (n)
11887   "Mark N articles forward as dormant.
11888 If N is negative, mark backward instead.  The difference between N and
11889 the actual number of articles marked is returned."
11890   (interactive "p")
11891   (gnus-set-global-variables)
11892   (gnus-summary-mark-forward n gnus-dormant-mark))
11893
11894 (defun gnus-summary-set-process-mark (article)
11895   "Set the process mark on ARTICLE and update the summary line."
11896   (setq gnus-newsgroup-processable
11897         (cons article
11898               (delq article gnus-newsgroup-processable)))
11899   (when (gnus-summary-goto-subject article)
11900     (gnus-summary-show-thread)
11901     (gnus-summary-update-secondary-mark article)))
11902
11903 (defun gnus-summary-remove-process-mark (article)
11904   "Remove the process mark from ARTICLE and update the summary line."
11905   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11906   (when (gnus-summary-goto-subject article)
11907     (gnus-summary-show-thread)
11908     (gnus-summary-update-secondary-mark article)))
11909
11910 (defun gnus-summary-set-saved-mark (article)
11911   "Set the process mark on ARTICLE and update the summary line."
11912   (push article gnus-newsgroup-saved)
11913   (when (gnus-summary-goto-subject article)
11914     (gnus-summary-update-secondary-mark article)))
11915
11916 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11917   "Mark N articles as read forwards.
11918 If N is negative, mark backwards instead.
11919 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11920 marked as unread.
11921 The difference between N and the actual number of articles marked is
11922 returned."
11923   (interactive "p")
11924   (gnus-set-global-variables)
11925   (let ((backward (< n 0))
11926         (gnus-summary-goto-unread
11927          (and gnus-summary-goto-unread
11928               (not (eq gnus-summary-goto-unread 'never))
11929               (not (memq mark (list gnus-unread-mark
11930                                     gnus-ticked-mark gnus-dormant-mark)))))
11931         (n (abs n))
11932         (mark (or mark gnus-del-mark)))
11933     (while (and (> n 0)
11934                 (gnus-summary-mark-article nil mark no-expire)
11935                 (zerop (gnus-summary-next-subject
11936                         (if backward -1 1)
11937                         (and gnus-summary-goto-unread
11938                              (not (eq gnus-summary-goto-unread 'never)))
11939                         t)))
11940       (setq n (1- n)))
11941     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11942     (gnus-summary-recenter)
11943     (gnus-summary-position-point)
11944     (gnus-set-mode-line 'summary)
11945     n))
11946
11947 (defun gnus-summary-mark-article-as-read (mark)
11948   "Mark the current article quickly as read with MARK."
11949   (let ((article (gnus-summary-article-number)))
11950     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11951     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11952     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11953     (setq gnus-newsgroup-reads
11954           (cons (cons article mark) gnus-newsgroup-reads))
11955     ;; Possibly remove from cache, if that is used.
11956     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11957     ;; Allow the backend to change the mark.
11958     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11959     ;; Check for auto-expiry.
11960     (when (and gnus-newsgroup-auto-expire
11961                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11962                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11963                    (= mark gnus-ancient-mark)
11964                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11965       (setq mark gnus-expirable-mark)
11966       (push article gnus-newsgroup-expirable))
11967     ;; Set the mark in the buffer.
11968     (gnus-summary-update-mark mark 'unread)
11969     t))
11970
11971 (defun gnus-summary-mark-article-as-unread (mark)
11972   "Mark the current article quickly as unread with MARK."
11973   (let ((article (gnus-summary-article-number)))
11974     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11975     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11976     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11977     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11978     (cond ((= mark gnus-ticked-mark)
11979            (push article gnus-newsgroup-marked))
11980           ((= mark gnus-dormant-mark)
11981            (push article gnus-newsgroup-dormant))
11982           (t
11983            (push article gnus-newsgroup-unreads)))
11984     (setq gnus-newsgroup-reads
11985           (delq (assq article gnus-newsgroup-reads)
11986                 gnus-newsgroup-reads))
11987
11988     ;; See whether the article is to be put in the cache.
11989     (and gnus-use-cache
11990          (vectorp (gnus-summary-article-header article))
11991          (save-excursion
11992            (gnus-cache-possibly-enter-article
11993             gnus-newsgroup-name article
11994             (gnus-summary-article-header article)
11995             (= mark gnus-ticked-mark)
11996             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11997
11998     ;; Fix the mark.
11999     (gnus-summary-update-mark mark 'unread)
12000     t))
12001
12002 (defun gnus-summary-mark-article (&optional article mark no-expire)
12003   "Mark ARTICLE with MARK.  MARK can be any character.
12004 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12005 `??' (dormant) and `?E' (expirable).
12006 If MARK is nil, then the default character `?D' is used.
12007 If ARTICLE is nil, then the article on the current line will be
12008 marked."
12009   ;; The mark might be a string.
12010   (and (stringp mark)
12011        (setq mark (aref mark 0)))
12012   ;; If no mark is given, then we check auto-expiring.
12013   (and (not no-expire)
12014        gnus-newsgroup-auto-expire
12015        (or (not mark)
12016            (and (numberp mark)
12017                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12018                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12019                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12020        (setq mark gnus-expirable-mark))
12021   (let* ((mark (or mark gnus-del-mark))
12022          (article (or article (gnus-summary-article-number))))
12023     (or article (error "No article on current line"))
12024     (if (or (= mark gnus-unread-mark)
12025             (= mark gnus-ticked-mark)
12026             (= mark gnus-dormant-mark))
12027         (gnus-mark-article-as-unread article mark)
12028       (gnus-mark-article-as-read article mark))
12029
12030     ;; See whether the article is to be put in the cache.
12031     (and gnus-use-cache
12032          (not (= mark gnus-canceled-mark))
12033          (vectorp (gnus-summary-article-header article))
12034          (save-excursion
12035            (gnus-cache-possibly-enter-article
12036             gnus-newsgroup-name article
12037             (gnus-summary-article-header article)
12038             (= mark gnus-ticked-mark)
12039             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12040
12041     (if (gnus-summary-goto-subject article nil t)
12042         (let ((buffer-read-only nil))
12043           (gnus-summary-show-thread)
12044           ;; Fix the mark.
12045           (gnus-summary-update-mark mark 'unread)
12046           t))))
12047
12048 (defun gnus-summary-update-secondary-mark (article)
12049   "Update the secondary (read, process, cache) mark."
12050   (gnus-summary-update-mark
12051    (cond ((memq article gnus-newsgroup-processable)
12052           gnus-process-mark)
12053          ((memq article gnus-newsgroup-cached)
12054           gnus-cached-mark)
12055          ((memq article gnus-newsgroup-replied)
12056           gnus-replied-mark)
12057          ((memq article gnus-newsgroup-saved)
12058           gnus-saved-mark)
12059          (t gnus-unread-mark))
12060    'replied)
12061   (when (gnus-visual-p 'summary-highlight 'highlight)
12062     (run-hooks 'gnus-summary-update-hook))
12063   t)
12064
12065 (defun gnus-summary-update-mark (mark type)
12066   (beginning-of-line)
12067   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12068         (buffer-read-only nil))
12069     (when (and forward
12070                (<= (+ forward (point)) (point-max)))
12071       ;; Go to the right position on the line.
12072       (goto-char (+ forward (point)))
12073       ;; Replace the old mark with the new mark.
12074       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12075       ;; Optionally update the marks by some user rule.
12076       (when (eq type 'unread)
12077         (gnus-data-set-mark
12078          (gnus-data-find (gnus-summary-article-number)) mark)
12079         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12080
12081 (defun gnus-mark-article-as-read (article &optional mark)
12082   "Enter ARTICLE in the pertinent lists and remove it from others."
12083   ;; Make the article expirable.
12084   (let ((mark (or mark gnus-del-mark)))
12085     (if (= mark gnus-expirable-mark)
12086         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12087       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12088     ;; Remove from unread and marked lists.
12089     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12090     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12091     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12092     (push (cons article mark) gnus-newsgroup-reads)
12093     ;; Possibly remove from cache, if that is used.
12094     (when gnus-use-cache
12095       (gnus-cache-enter-remove-article article))))
12096
12097 (defun gnus-mark-article-as-unread (article &optional mark)
12098   "Enter ARTICLE in the pertinent lists and remove it from others."
12099   (let ((mark (or mark gnus-ticked-mark)))
12100     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12101     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12102     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12103     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12104     (cond ((= mark gnus-ticked-mark)
12105            (push article gnus-newsgroup-marked))
12106           ((= mark gnus-dormant-mark)
12107            (push article gnus-newsgroup-dormant))
12108           (t
12109            (push article gnus-newsgroup-unreads)))
12110     (setq gnus-newsgroup-reads
12111           (delq (assq article gnus-newsgroup-reads)
12112                 gnus-newsgroup-reads))))
12113
12114 (defalias 'gnus-summary-mark-as-unread-forward
12115   'gnus-summary-tick-article-forward)
12116 (make-obsolete 'gnus-summary-mark-as-unread-forward
12117                'gnus-summary-tick-article-forward)
12118 (defun gnus-summary-tick-article-forward (n)
12119   "Tick N articles forwards.
12120 If N is negative, tick backwards instead.
12121 The difference between N and the number of articles ticked is returned."
12122   (interactive "p")
12123   (gnus-summary-mark-forward n gnus-ticked-mark))
12124
12125 (defalias 'gnus-summary-mark-as-unread-backward
12126   'gnus-summary-tick-article-backward)
12127 (make-obsolete 'gnus-summary-mark-as-unread-backward
12128                'gnus-summary-tick-article-backward)
12129 (defun gnus-summary-tick-article-backward (n)
12130   "Tick N articles backwards.
12131 The difference between N and the number of articles ticked is returned."
12132   (interactive "p")
12133   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12134
12135 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12136 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12137 (defun gnus-summary-tick-article (&optional article clear-mark)
12138   "Mark current article as unread.
12139 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12140 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12141   (interactive)
12142   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12143                                        gnus-ticked-mark)))
12144
12145 (defun gnus-summary-mark-as-read-forward (n)
12146   "Mark N articles as read forwards.
12147 If N is negative, mark backwards instead.
12148 The difference between N and the actual number of articles marked is
12149 returned."
12150   (interactive "p")
12151   (gnus-summary-mark-forward n gnus-del-mark t))
12152
12153 (defun gnus-summary-mark-as-read-backward (n)
12154   "Mark the N articles as read backwards.
12155 The difference between N and the actual number of articles marked is
12156 returned."
12157   (interactive "p")
12158   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12159
12160 (defun gnus-summary-mark-as-read (&optional article mark)
12161   "Mark current article as read.
12162 ARTICLE specifies the article to be marked as read.
12163 MARK specifies a string to be inserted at the beginning of the line."
12164   (gnus-summary-mark-article article mark))
12165
12166 (defun gnus-summary-clear-mark-forward (n)
12167   "Clear marks from N articles forward.
12168 If N is negative, clear backward instead.
12169 The difference between N and the number of marks cleared is returned."
12170   (interactive "p")
12171   (gnus-summary-mark-forward n gnus-unread-mark))
12172
12173 (defun gnus-summary-clear-mark-backward (n)
12174   "Clear marks from N articles backward.
12175 The difference between N and the number of marks cleared is returned."
12176   (interactive "p")
12177   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12178
12179 (defun gnus-summary-mark-unread-as-read ()
12180   "Intended to be used by `gnus-summary-mark-article-hook'."
12181   (when (memq gnus-current-article gnus-newsgroup-unreads)
12182     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12183
12184 (defun gnus-summary-mark-read-and-unread-as-read ()
12185   "Intended to be used by `gnus-summary-mark-article-hook'."
12186   (let ((mark (gnus-summary-article-mark)))
12187     (when (or (gnus-unread-mark-p mark)
12188               (gnus-read-mark-p mark))
12189       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12190
12191 (defun gnus-summary-mark-region-as-read (point mark all)
12192   "Mark all unread articles between point and mark as read.
12193 If given a prefix, mark all articles between point and mark as read,
12194 even ticked and dormant ones."
12195   (interactive "r\nP")
12196   (save-excursion
12197     (let (article)
12198       (goto-char point)
12199       (beginning-of-line)
12200       (while (and
12201               (< (point) mark)
12202               (progn
12203                 (when (or all
12204                           (memq (setq article (gnus-summary-article-number))
12205                                 gnus-newsgroup-unreads))
12206                   (gnus-summary-mark-article article gnus-del-mark))
12207                 t)
12208               (gnus-summary-find-next))))))
12209
12210 (defun gnus-summary-mark-below (score mark)
12211   "Mark articles with score less than SCORE with MARK."
12212   (interactive "P\ncMark: ")
12213   (gnus-set-global-variables)
12214   (setq score (if score
12215                   (prefix-numeric-value score)
12216                 (or gnus-summary-default-score 0)))
12217   (save-excursion
12218     (set-buffer gnus-summary-buffer)
12219     (goto-char (point-min))
12220     (while 
12221         (progn
12222           (and (< (gnus-summary-article-score) score)
12223                (gnus-summary-mark-article nil mark))
12224           (gnus-summary-find-next)))))
12225
12226 (defun gnus-summary-kill-below (&optional score)
12227   "Mark articles with score below SCORE as read."
12228   (interactive "P")
12229   (gnus-set-global-variables)
12230   (gnus-summary-mark-below score gnus-killed-mark))
12231
12232 (defun gnus-summary-clear-above (&optional score)
12233   "Clear all marks from articles with score above SCORE."
12234   (interactive "P")
12235   (gnus-set-global-variables)
12236   (gnus-summary-mark-above score gnus-unread-mark))
12237
12238 (defun gnus-summary-tick-above (&optional score)
12239   "Tick all articles with score above SCORE."
12240   (interactive "P")
12241   (gnus-set-global-variables)
12242   (gnus-summary-mark-above score gnus-ticked-mark))
12243
12244 (defun gnus-summary-mark-above (score mark)
12245   "Mark articles with score over SCORE with MARK."
12246   (interactive "P\ncMark: ")
12247   (gnus-set-global-variables)
12248   (setq score (if score
12249                   (prefix-numeric-value score)
12250                 (or gnus-summary-default-score 0)))
12251   (save-excursion
12252     (set-buffer gnus-summary-buffer)
12253     (goto-char (point-min))
12254     (while (and (progn
12255                   (if (> (gnus-summary-article-score) score)
12256                       (gnus-summary-mark-article nil mark))
12257                   t)
12258                 (gnus-summary-find-next)))))
12259
12260 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12261 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12262 (defun gnus-summary-limit-include-expunged ()
12263   "Display all the hidden articles that were expunged for low scores."
12264   (interactive)
12265   (gnus-set-global-variables)
12266   (let ((buffer-read-only nil))
12267     (let ((scored gnus-newsgroup-scored)
12268           headers h)
12269       (while scored
12270         (or (gnus-summary-goto-subject (caar scored))
12271             (and (setq h (gnus-summary-article-header (caar scored)))
12272                  (< (cdar scored) gnus-summary-expunge-below)
12273                  (setq headers (cons h headers))))
12274         (setq scored (cdr scored)))
12275       (or headers (error "No expunged articles hidden."))
12276       (goto-char (point-min))
12277       (gnus-summary-prepare-unthreaded (nreverse headers)))
12278     (goto-char (point-min))
12279     (gnus-summary-position-point)))
12280
12281 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12282   "Mark all articles not marked as unread in this newsgroup as read.
12283 If prefix argument ALL is non-nil, all articles are marked as read.
12284 If QUIETLY is non-nil, no questions will be asked.
12285 If TO-HERE is non-nil, it should be a point in the buffer.  All
12286 articles before this point will be marked as read.
12287 The number of articles marked as read is returned."
12288   (interactive "P")
12289   (gnus-set-global-variables)
12290   (prog1
12291       (if (or quietly
12292               (not gnus-interactive-catchup) ;Without confirmation?
12293               gnus-expert-user
12294               (gnus-y-or-n-p
12295                (if all
12296                    "Mark absolutely all articles as read? "
12297                  "Mark all unread articles as read? ")))
12298           (if (and not-mark
12299                    (not gnus-newsgroup-adaptive)
12300                    (not gnus-newsgroup-auto-expire))
12301               (progn
12302                 (when all
12303                   (setq gnus-newsgroup-marked nil
12304                         gnus-newsgroup-dormant nil))
12305                 (setq gnus-newsgroup-unreads nil))
12306             ;; We actually mark all articles as canceled, which we
12307             ;; have to do when using auto-expiry or adaptive scoring.
12308             (gnus-summary-show-all-threads)
12309             (if (gnus-summary-first-subject (not all))
12310                 (while (and
12311                         (if to-here (< (point) to-here) t)
12312                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12313                         (gnus-summary-find-next (not all)))))
12314             (unless to-here
12315               (setq gnus-newsgroup-unreads nil))
12316             (gnus-set-mode-line 'summary)))
12317     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12318       (if (and (not to-here) (eq 'nnvirtual (car method)))
12319           (nnvirtual-catchup-group
12320            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12321     (gnus-summary-position-point)))
12322
12323 (defun gnus-summary-catchup-to-here (&optional all)
12324   "Mark all unticked articles before the current one as read.
12325 If ALL is non-nil, also mark ticked and dormant articles as read."
12326   (interactive "P")
12327   (gnus-set-global-variables)
12328   (save-excursion
12329     (let ((beg (point)))
12330       ;; We check that there are unread articles.
12331       (when (or all (gnus-summary-find-prev))
12332         (gnus-summary-catchup all t beg))))
12333   (gnus-summary-position-point))
12334
12335 (defun gnus-summary-catchup-all (&optional quietly)
12336   "Mark all articles in this newsgroup as read."
12337   (interactive "P")
12338   (gnus-set-global-variables)
12339   (gnus-summary-catchup t quietly))
12340
12341 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12342   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12343 If prefix argument ALL is non-nil, all articles are marked as read."
12344   (interactive "P")
12345   (gnus-set-global-variables)
12346   (gnus-summary-catchup all quietly nil 'fast)
12347   ;; Select next newsgroup or exit.
12348   (if (eq gnus-auto-select-next 'quietly)
12349       (gnus-summary-next-group nil)
12350     (gnus-summary-exit)))
12351
12352 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12353   "Mark all articles in this newsgroup as read, and then exit."
12354   (interactive "P")
12355   (gnus-set-global-variables)
12356   (gnus-summary-catchup-and-exit t quietly))
12357
12358 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12359 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12360   "Mark all articles in this group as read and select the next group.
12361 If given a prefix, mark all articles, unread as well as ticked, as
12362 read."
12363   (interactive "P")
12364   (gnus-set-global-variables)
12365   (save-excursion
12366     (gnus-summary-catchup all))
12367   (gnus-summary-next-article t nil nil t))
12368
12369 ;; Thread-based commands.
12370
12371 (defun gnus-summary-articles-in-thread (&optional article)
12372   "Return a list of all articles in the current thread.
12373 If ARTICLE is non-nil, return all articles in the thread that starts
12374 with that article."
12375   (let* ((article (or article (gnus-summary-article-number)))
12376          (data (gnus-data-find-list article))
12377          (top-level (gnus-data-level (car data)))
12378          (top-subject
12379           (cond ((null gnus-thread-operation-ignore-subject)
12380                  (gnus-simplify-subject-re
12381                   (mail-header-subject (gnus-data-header (car data)))))
12382                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12383                  (gnus-simplify-subject-fuzzy
12384                   (mail-header-subject (gnus-data-header (car data)))))
12385                 (t nil)))
12386          (end-point (save-excursion
12387                       (if (gnus-summary-go-to-next-thread) 
12388                           (point) (point-max))))
12389          articles)
12390     (while (and data
12391                 (< (gnus-data-pos (car data)) end-point))
12392       (when (or (not top-subject)
12393                 (string= top-subject
12394                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12395                              (gnus-simplify-subject-fuzzy
12396                               (mail-header-subject
12397                                (gnus-data-header (car data))))
12398                            (gnus-simplify-subject-re
12399                             (mail-header-subject
12400                              (gnus-data-header (car data)))))))
12401         (push (gnus-data-number (car data)) articles))
12402       (unless (and (setq data (cdr data))
12403                    (> (gnus-data-level (car data)) top-level))
12404         (setq data nil)))
12405     ;; Return the list of articles.
12406     (nreverse articles)))
12407
12408 (defun gnus-summary-rethread-current ()
12409   "Rethread the thread the current article is part of."
12410   (interactive)
12411   (gnus-set-global-variables)
12412   (let* ((gnus-show-threads t)
12413          (article (gnus-summary-article-number))
12414          (id (mail-header-id (gnus-summary-article-header)))
12415          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12416     (unless id
12417       (error "No article on the current line"))
12418     (gnus-rebuild-thread id)
12419     (gnus-summary-goto-subject article)))
12420
12421 (defun gnus-summary-reparent-thread ()
12422   "Make current article child of the marked (or previous) article.
12423
12424 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12425 is non-nil or the Subject: of both articles are the same."
12426   (interactive)
12427   (or (not (gnus-group-read-only-p))
12428       (error "The current newsgroup does not support article editing."))
12429   (or (<= (length gnus-newsgroup-processable) 1)
12430       (error "No more than one article may be marked."))
12431   (save-window-excursion
12432     (let ((gnus-article-buffer " *reparent*")
12433           (current-article (gnus-summary-article-number))
12434           ; first grab the marked article, otherwise one line up.
12435           (parent-article (if (not (null gnus-newsgroup-processable))
12436                               (car gnus-newsgroup-processable)
12437                             (save-excursion
12438                               (if (eq (forward-line -1) 0)
12439                                   (gnus-summary-article-number)
12440                                 (error "Beginning of summary buffer."))))))
12441       (or (not (eq current-article parent-article))
12442           (error "An article may not be self-referential."))
12443       (let ((message-id (mail-header-id 
12444                          (gnus-summary-article-header parent-article))))
12445         (or (and message-id (not (equal message-id "")))
12446             (error "No message-id in desired parent."))
12447         (gnus-summary-select-article t t nil current-article)
12448         (set-buffer gnus-article-buffer)
12449         (setq buffer-read-only nil)
12450         (let ((buf (format "%s" (buffer-string))))
12451           (erase-buffer)
12452           (insert buf))
12453         (goto-char (point-min))
12454         (if (search-forward-regexp "^References: " nil t)
12455             (insert message-id " " )
12456           (insert "References: " message-id "\n"))
12457         (or (gnus-request-replace-article current-article
12458                                           (car gnus-article-current)
12459                                           gnus-article-buffer)
12460             (error "Couldn't replace article."))
12461         (set-buffer gnus-summary-buffer)
12462         (gnus-summary-unmark-all-processable)
12463         (gnus-summary-rethread-current)
12464         (gnus-message 3 "Article %d is now the child of article %d."
12465                       current-article parent-article)))))
12466
12467 (defun gnus-summary-toggle-threads (&optional arg)
12468   "Toggle showing conversation threads.
12469 If ARG is positive number, turn showing conversation threads on."
12470   (interactive "P")
12471   (gnus-set-global-variables)
12472   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12473     (setq gnus-show-threads
12474           (if (null arg) (not gnus-show-threads)
12475             (> (prefix-numeric-value arg) 0)))
12476     (gnus-summary-prepare)
12477     (gnus-summary-goto-subject current)
12478     (gnus-summary-position-point)))
12479
12480 (defun gnus-summary-show-all-threads ()
12481   "Show all threads."
12482   (interactive)
12483   (gnus-set-global-variables)
12484   (save-excursion
12485     (let ((buffer-read-only nil))
12486       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12487   (gnus-summary-position-point))
12488
12489 (defun gnus-summary-show-thread ()
12490   "Show thread subtrees.
12491 Returns nil if no thread was there to be shown."
12492   (interactive)
12493   (gnus-set-global-variables)
12494   (let ((buffer-read-only nil)
12495         (orig (point))
12496         ;; first goto end then to beg, to have point at beg after let
12497         (end (progn (end-of-line) (point)))
12498         (beg (progn (beginning-of-line) (point))))
12499     (prog1
12500         ;; Any hidden lines here?
12501         (search-forward "\r" end t)
12502       (subst-char-in-region beg end ?\^M ?\n t)
12503       (goto-char orig)
12504       (gnus-summary-position-point))))
12505
12506 (defun gnus-summary-hide-all-threads ()
12507   "Hide all thread subtrees."
12508   (interactive)
12509   (gnus-set-global-variables)
12510   (save-excursion
12511     (goto-char (point-min))
12512     (gnus-summary-hide-thread)
12513     (while (zerop (gnus-summary-next-thread 1 t))
12514       (gnus-summary-hide-thread)))
12515   (gnus-summary-position-point))
12516
12517 (defun gnus-summary-hide-thread ()
12518   "Hide thread subtrees.
12519 Returns nil if no threads were there to be hidden."
12520   (interactive)
12521   (gnus-set-global-variables)
12522   (let ((buffer-read-only nil)
12523         (start (point))
12524         (article (gnus-summary-article-number)))
12525     (goto-char start)
12526     ;; Go forward until either the buffer ends or the subthread
12527     ;; ends.
12528     (when (and (not (eobp))
12529                (or (zerop (gnus-summary-next-thread 1 t))
12530                    (goto-char (point-max))))
12531       (prog1
12532           (if (and (> (point) start)
12533                    (search-backward "\n" start t))
12534               (progn
12535                 (subst-char-in-region start (point) ?\n ?\^M)
12536                 (gnus-summary-goto-subject article))
12537             (goto-char start)
12538             nil)
12539         ;;(gnus-summary-position-point)
12540         ))))
12541
12542 (defun gnus-summary-go-to-next-thread (&optional previous)
12543   "Go to the same level (or less) next thread.
12544 If PREVIOUS is non-nil, go to previous thread instead.
12545 Return the article number moved to, or nil if moving was impossible."
12546   (let ((level (gnus-summary-thread-level))
12547         (way (if previous -1 1))
12548         (beg (point)))
12549     (forward-line way)
12550     (while (and (not (eobp))
12551                 (< level (gnus-summary-thread-level)))
12552       (forward-line way))
12553     (if (eobp)
12554         (progn
12555           (goto-char beg)
12556           nil)
12557       (setq beg (point))
12558       (prog1
12559           (gnus-summary-article-number)
12560         (goto-char beg)))))
12561
12562 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12563   "Go to the same level (or less) next thread.
12564 If PREVIOUS is non-nil, go to previous thread instead.
12565 Return the article number moved to, or nil if moving was impossible."
12566   (if (and (eq gnus-summary-make-false-root 'dummy)
12567            (gnus-summary-article-intangible-p))
12568       (let ((beg (point)))
12569         (while (and (zerop (forward-line 1))
12570                     (not (gnus-summary-article-intangible-p))
12571                     (not (zerop (save-excursion 
12572                                   (gnus-summary-thread-level))))))
12573         (if (eobp)
12574             (progn
12575               (goto-char beg)
12576               nil)
12577           (point)))
12578     (let* ((level (gnus-summary-thread-level))
12579            (article (gnus-summary-article-number))
12580            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12581            oart)
12582       (while data
12583         (if (<= (gnus-data-level (car data)) level)
12584             (setq oart (gnus-data-number (car data))
12585                   data nil)
12586           (setq data (cdr data))))
12587       (and oart
12588            (gnus-summary-goto-subject oart)))))
12589
12590 (defun gnus-summary-next-thread (n &optional silent)
12591   "Go to the same level next N'th thread.
12592 If N is negative, search backward instead.
12593 Returns the difference between N and the number of skips actually
12594 done.
12595
12596 If SILENT, don't output messages."
12597   (interactive "p")
12598   (gnus-set-global-variables)
12599   (let ((backward (< n 0))
12600         (n (abs n))
12601         old dum int)
12602     (while (and (> n 0)
12603                 (gnus-summary-go-to-next-thread backward))
12604       (decf n))
12605     (unless silent 
12606       (gnus-summary-position-point))
12607     (when (and (not silent) (/= 0 n))
12608       (gnus-message 7 "No more threads"))
12609     n))
12610
12611 (defun gnus-summary-prev-thread (n)
12612   "Go to the same level previous N'th thread.
12613 Returns the difference between N and the number of skips actually
12614 done."
12615   (interactive "p")
12616   (gnus-set-global-variables)
12617   (gnus-summary-next-thread (- n)))
12618
12619 (defun gnus-summary-go-down-thread ()
12620   "Go down one level in the current thread."
12621   (let ((children (gnus-summary-article-children)))
12622     (and children
12623          (gnus-summary-goto-subject (car children)))))
12624
12625 (defun gnus-summary-go-up-thread ()
12626   "Go up one level in the current thread."
12627   (let ((parent (gnus-summary-article-parent)))
12628     (and parent
12629          (gnus-summary-goto-subject parent))))
12630
12631 (defun gnus-summary-down-thread (n)
12632   "Go down thread N steps.
12633 If N is negative, go up instead.
12634 Returns the difference between N and how many steps down that were
12635 taken."
12636   (interactive "p")
12637   (gnus-set-global-variables)
12638   (let ((up (< n 0))
12639         (n (abs n)))
12640     (while (and (> n 0)
12641                 (if up (gnus-summary-go-up-thread)
12642                   (gnus-summary-go-down-thread)))
12643       (setq n (1- n)))
12644     (gnus-summary-position-point)
12645     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12646     n))
12647
12648 (defun gnus-summary-up-thread (n)
12649   "Go up thread N steps.
12650 If N is negative, go up instead.
12651 Returns the difference between N and how many steps down that were
12652 taken."
12653   (interactive "p")
12654   (gnus-set-global-variables)
12655   (gnus-summary-down-thread (- n)))
12656
12657 (defun gnus-summary-top-thread ()
12658   "Go to the top of the thread."
12659   (interactive)
12660   (gnus-set-global-variables)
12661   (while (gnus-summary-go-up-thread))
12662   (gnus-summary-article-number))
12663
12664 (defun gnus-summary-kill-thread (&optional unmark)
12665   "Mark articles under current thread as read.
12666 If the prefix argument is positive, remove any kinds of marks.
12667 If the prefix argument is negative, tick articles instead."
12668   (interactive "P")
12669   (gnus-set-global-variables)
12670   (if unmark
12671       (setq unmark (prefix-numeric-value unmark)))
12672   (let ((articles (gnus-summary-articles-in-thread)))
12673     (save-excursion
12674       ;; Expand the thread.
12675       (gnus-summary-show-thread)
12676       ;; Mark all the articles.
12677       (while articles
12678         (gnus-summary-goto-subject (car articles))
12679         (cond ((null unmark)
12680                (gnus-summary-mark-article-as-read gnus-killed-mark))
12681               ((> unmark 0)
12682                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12683               (t
12684                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12685         (setq articles (cdr articles))))
12686     ;; Hide killed subtrees.
12687     (and (null unmark)
12688          gnus-thread-hide-killed
12689          (gnus-summary-hide-thread))
12690     ;; If marked as read, go to next unread subject.
12691     (if (null unmark)
12692         ;; Go to next unread subject.
12693         (gnus-summary-next-subject 1 t)))
12694   (gnus-set-mode-line 'summary))
12695
12696 ;; Summary sorting commands
12697
12698 (defun gnus-summary-sort-by-number (&optional reverse)
12699   "Sort summary buffer by article number.
12700 Argument REVERSE means reverse order."
12701   (interactive "P")
12702   (gnus-summary-sort 'number reverse))
12703
12704 (defun gnus-summary-sort-by-author (&optional reverse)
12705   "Sort summary buffer by author name alphabetically.
12706 If case-fold-search is non-nil, case of letters is ignored.
12707 Argument REVERSE means reverse order."
12708   (interactive "P")
12709   (gnus-summary-sort 'author reverse))
12710
12711 (defun gnus-summary-sort-by-subject (&optional reverse)
12712   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12713 If case-fold-search is non-nil, case of letters is ignored.
12714 Argument REVERSE means reverse order."
12715   (interactive "P")
12716   (gnus-summary-sort 'subject reverse))
12717
12718 (defun gnus-summary-sort-by-date (&optional reverse)
12719   "Sort summary buffer by date.
12720 Argument REVERSE means reverse order."
12721   (interactive "P")
12722   (gnus-summary-sort 'date reverse))
12723
12724 (defun gnus-summary-sort-by-score (&optional reverse)
12725   "Sort summary buffer by score.
12726 Argument REVERSE means reverse order."
12727   (interactive "P")
12728   (gnus-summary-sort 'score reverse))
12729
12730 (defun gnus-summary-sort (predicate reverse)
12731   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12732   (gnus-set-global-variables)
12733   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12734          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12735          (gnus-thread-sort-functions
12736           (list
12737            (if (not reverse)
12738                thread
12739              `(lambda (t1 t2)
12740                 (,thread t2 t1)))))
12741          (gnus-article-sort-functions
12742           (list
12743            (if (not reverse)
12744                article
12745              `(lambda (t1 t2)
12746                 (,article t2 t1)))))
12747          (buffer-read-only)
12748          (gnus-summary-prepare-hook nil))
12749     ;; We do the sorting by regenerating the threads.
12750     (gnus-summary-prepare)
12751     ;; Hide subthreads if needed.
12752     (when (and gnus-show-threads gnus-thread-hide-subtree)
12753       (gnus-summary-hide-all-threads)))
12754   ;; If in async mode, we send some info to the backend.
12755   (when gnus-newsgroup-async
12756     (gnus-request-asynchronous
12757      gnus-newsgroup-name gnus-newsgroup-data)))
12758
12759 (defun gnus-sortable-date (date)
12760   "Make sortable string by string-lessp from DATE.
12761 Timezone package is used."
12762   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12763          (year (aref date 0))
12764          (month (aref date 1))
12765          (day (aref date 2)))
12766     (timezone-make-sortable-date
12767      year month day
12768      (timezone-make-time-string
12769       (aref date 3) (aref date 4) (aref date 5)))))
12770
12771 ;; Summary saving commands.
12772
12773 (defun gnus-summary-save-article (&optional n not-saved)
12774   "Save the current article using the default saver function.
12775 If N is a positive number, save the N next articles.
12776 If N is a negative number, save the N previous articles.
12777 If N is nil and any articles have been marked with the process mark,
12778 save those articles instead.
12779 The variable `gnus-default-article-saver' specifies the saver function."
12780   (interactive "P")
12781   (gnus-set-global-variables)
12782   (let ((articles (gnus-summary-work-articles n))
12783         file header article)
12784     (while articles
12785       (setq header (gnus-summary-article-header
12786                     (setq article (pop articles))))
12787       (if (not (vectorp header))
12788           ;; This is a pseudo-article.
12789           (if (assq 'name header)
12790               (gnus-copy-file (cdr (assq 'name header)))
12791             (gnus-message 1 "Article %d is unsaveable" article))
12792         ;; This is a real article.
12793         (save-window-excursion
12794           (gnus-summary-select-article t nil nil article))
12795         (unless gnus-save-all-headers
12796           ;; Remove headers accoring to `gnus-saved-headers'.
12797           (let ((gnus-visible-headers
12798                  (or gnus-saved-headers gnus-visible-headers)))
12799             (gnus-article-hide-headers nil t)))
12800         ;; Remove any X-Gnus lines.
12801         (save-excursion
12802           (set-buffer gnus-article-buffer)
12803           (save-restriction
12804             (let ((buffer-read-only nil))
12805               (nnheader-narrow-to-headers)
12806               (while (re-search-forward "^X-Gnus" nil t)
12807                 (gnus-delete-line)))))
12808         (save-window-excursion
12809           (if (not gnus-default-article-saver)
12810               (error "No default saver is defined.")
12811             (setq file (funcall
12812                         gnus-default-article-saver
12813                         (cond
12814                          ((not gnus-prompt-before-saving)
12815                           'default)
12816                          ((eq gnus-prompt-before-saving 'always)
12817                           nil)
12818                          (t file))))))
12819         (gnus-summary-remove-process-mark article)
12820         (unless not-saved
12821           (gnus-summary-set-saved-mark article))))
12822     (gnus-summary-position-point)
12823     n))
12824
12825 (defun gnus-summary-pipe-output (&optional arg)
12826   "Pipe the current article to a subprocess.
12827 If N is a positive number, pipe the N next articles.
12828 If N is a negative number, pipe the N previous articles.
12829 If N is nil and any articles have been marked with the process mark,
12830 pipe those articles instead."
12831   (interactive "P")
12832   (gnus-set-global-variables)
12833   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12834     (gnus-summary-save-article arg t))
12835   (gnus-configure-windows 'pipe))
12836
12837 (defun gnus-summary-save-article-mail (&optional arg)
12838   "Append the current article to an mail file.
12839 If N is a positive number, save the N next articles.
12840 If N is a negative number, save the N previous articles.
12841 If N is nil and any articles have been marked with the process mark,
12842 save those articles instead."
12843   (interactive "P")
12844   (gnus-set-global-variables)
12845   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12846     (gnus-summary-save-article arg)))
12847
12848 (defun gnus-summary-save-article-rmail (&optional arg)
12849   "Append the current article to an rmail file.
12850 If N is a positive number, save the N next articles.
12851 If N is a negative number, save the N previous articles.
12852 If N is nil and any articles have been marked with the process mark,
12853 save those articles instead."
12854   (interactive "P")
12855   (gnus-set-global-variables)
12856   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12857     (gnus-summary-save-article arg)))
12858
12859 (defun gnus-summary-save-article-file (&optional arg)
12860   "Append the current article to a file.
12861 If N is a positive number, save the N next articles.
12862 If N is a negative number, save the N previous articles.
12863 If N is nil and any articles have been marked with the process mark,
12864 save those articles instead."
12865   (interactive "P")
12866   (gnus-set-global-variables)
12867   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12868     (gnus-summary-save-article arg)))
12869
12870 (defun gnus-summary-save-article-body-file (&optional arg)
12871   "Append the current article body to a file.
12872 If N is a positive number, save the N next articles.
12873 If N is a negative number, save the N previous articles.
12874 If N is nil and any articles have been marked with the process mark,
12875 save those articles instead."
12876   (interactive "P")
12877   (gnus-set-global-variables)
12878   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12879     (gnus-summary-save-article arg)))
12880
12881 (defun gnus-get-split-value (methods)
12882   "Return a value based on the split METHODS."
12883   (let (split-name method result match)
12884     (when methods
12885       (save-excursion
12886         (set-buffer gnus-original-article-buffer)
12887         (save-restriction
12888           (nnheader-narrow-to-headers)
12889           (while methods
12890             (goto-char (point-min))
12891             (setq method (pop methods))
12892             (setq match (car method))
12893             (when (cond
12894                    ((stringp match)
12895                     ;; Regular expression.
12896                     (condition-case ()
12897                         (re-search-forward match nil t)
12898                       (error nil)))
12899                    ((gnus-functionp match)
12900                     ;; Function.
12901                     (save-restriction
12902                       (widen)
12903                       (setq result (funcall match gnus-newsgroup-name))))
12904                    ((consp match)
12905                     ;; Form.
12906                     (save-restriction
12907                       (widen)
12908                       (setq result (eval match)))))
12909               (setq split-name (append (cdr method) split-name))
12910               (cond ((stringp result)
12911                      (push result split-name))
12912                     ((consp result)
12913                      (setq split-name (append result split-name)))))))))
12914     split-name))
12915
12916 (defun gnus-read-move-group-name (prompt default articles prefix)
12917   "Read a group name."
12918   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12919          group-map
12920          (dum (mapatoms
12921                (lambda (g) 
12922                  (and (boundp g)
12923                       (symbol-name g)
12924                       (memq 'respool
12925                             (assoc (symbol-name
12926                                     (car (gnus-find-method-for-group
12927                                           (symbol-name g))))
12928                                    gnus-valid-select-methods))
12929                       (push (list (symbol-name g)) group-map)))
12930                gnus-active-hashtb))
12931          (prom
12932           (format "%s %s to:"
12933                   prompt
12934                   (if (> (length articles) 1)
12935                       (format "these %d articles" (length articles))
12936                     "this article")))
12937          (to-newsgroup
12938           (cond
12939            ((null split-name)
12940             (gnus-completing-read default prom
12941                                   group-map nil nil prefix
12942                                   'gnus-group-history))
12943            ((= 1 (length split-name))
12944             (gnus-completing-read (car split-name) prom group-map
12945                                   nil nil nil
12946                                   'gnus-group-history))
12947            (t
12948             (gnus-completing-read nil prom 
12949                                   (mapcar (lambda (el) (list el))
12950                                           (nreverse split-name))
12951                                   nil nil nil
12952                                   'gnus-group-history)))))
12953     (when to-newsgroup
12954       (if (or (string= to-newsgroup "")
12955               (string= to-newsgroup prefix))
12956           (setq to-newsgroup (or default "")))
12957       (or (gnus-active to-newsgroup)
12958           (gnus-activate-group to-newsgroup)
12959           (error "No such group: %s" to-newsgroup)))
12960     to-newsgroup))
12961
12962 (defun gnus-read-save-file-name (prompt default-name)
12963   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12964          (file
12965           ;; Let the split methods have their say.
12966           (cond
12967            ;; No split name was found.
12968            ((null split-name)
12969             (read-file-name
12970              (concat prompt " (default "
12971                      (file-name-nondirectory default-name) ") ")
12972              (file-name-directory default-name)
12973              default-name))
12974            ;; A single split name was found
12975            ((= 1 (length split-name))
12976             (read-file-name
12977              (concat prompt " (default " (car split-name) ") ")
12978              gnus-article-save-directory
12979              (concat gnus-article-save-directory (car split-name))))
12980            ;; A list of splits was found.
12981            (t
12982             (setq split-name (nreverse split-name))
12983             (let (result)
12984               (let ((file-name-history (nconc split-name file-name-history)))
12985                 (setq result
12986                       (read-file-name
12987                        (concat prompt " (`M-p' for defaults) ")
12988                        gnus-article-save-directory
12989                        (car split-name))))
12990               (car (push result file-name-history)))))))
12991     ;; If we have read a directory, we append the default file name.
12992     (when (file-directory-p file)
12993       (setq file (concat (file-name-as-directory file)
12994                          (file-name-nondirectory default-name))))
12995     ;; Possibly translate some charaters.
12996     (nnheader-translate-file-chars file)))
12997
12998 (defun gnus-article-archive-name (group)
12999   "Return the first instance of an \"Archive-name\" in the current buffer."
13000   (let ((case-fold-search t))
13001     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13002       (match-string 1))))
13003
13004 (defun gnus-summary-save-in-rmail (&optional filename)
13005   "Append this article to Rmail file.
13006 Optional argument FILENAME specifies file name.
13007 Directory to save to is default to `gnus-article-save-directory' which
13008 is initialized from the SAVEDIR environment variable."
13009   (interactive)
13010   (gnus-set-global-variables)
13011   (let ((default-name
13012           (funcall gnus-rmail-save-name gnus-newsgroup-name
13013                    gnus-current-headers gnus-newsgroup-last-rmail)))
13014     (setq filename
13015           (cond ((eq filename 'default)
13016                  default-name)
13017                 (filename filename)
13018                 (t (gnus-read-save-file-name
13019                     "Save in rmail file:" default-name))))
13020     (gnus-make-directory (file-name-directory filename))
13021     (gnus-eval-in-buffer-window
13022      gnus-original-article-buffer
13023      (save-excursion
13024        (save-restriction
13025          (widen)
13026          (gnus-output-to-rmail filename))))
13027     ;; Remember the directory name to save articles
13028     (setq gnus-newsgroup-last-rmail filename)))
13029
13030 (defun gnus-summary-save-in-mail (&optional filename)
13031   "Append this article to Unix mail file.
13032 Optional argument FILENAME specifies file name.
13033 Directory to save to is default to `gnus-article-save-directory' which
13034 is initialized from the SAVEDIR environment variable."
13035   (interactive)
13036   (gnus-set-global-variables)
13037   (let ((default-name
13038           (funcall gnus-mail-save-name gnus-newsgroup-name
13039                    gnus-current-headers gnus-newsgroup-last-mail)))
13040     (setq filename
13041           (cond ((eq filename 'default)
13042                  default-name)
13043                 (filename filename)
13044                 (t (gnus-read-save-file-name
13045                     "Save in Unix mail file:" default-name))))
13046     (setq filename
13047           (expand-file-name filename
13048                             (and default-name
13049                                  (file-name-directory default-name))))
13050     (gnus-make-directory (file-name-directory filename))
13051     (gnus-eval-in-buffer-window
13052      gnus-original-article-buffer
13053      (save-excursion
13054        (save-restriction
13055          (widen)
13056          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13057              (gnus-output-to-rmail filename)
13058            (let ((mail-use-rfc822 t))
13059              (rmail-output filename 1 t t))))))
13060     ;; Remember the directory name to save articles.
13061     (setq gnus-newsgroup-last-mail filename)))
13062
13063 (defun gnus-summary-save-in-file (&optional filename)
13064   "Append this article to file.
13065 Optional argument FILENAME specifies file name.
13066 Directory to save to is default to `gnus-article-save-directory' which
13067 is initialized from the SAVEDIR environment variable."
13068   (interactive)
13069   (gnus-set-global-variables)
13070   (let ((default-name
13071           (funcall gnus-file-save-name gnus-newsgroup-name
13072                    gnus-current-headers gnus-newsgroup-last-file)))
13073     (setq filename
13074           (cond ((eq filename 'default)
13075                  default-name)
13076                 (filename filename)
13077                 (t (gnus-read-save-file-name
13078                     "Save in file:" default-name))))
13079     (gnus-make-directory (file-name-directory filename))
13080     (gnus-eval-in-buffer-window
13081      gnus-original-article-buffer
13082      (save-excursion
13083        (save-restriction
13084          (widen)
13085          (gnus-output-to-file filename))))
13086     ;; Remember the directory name to save articles.
13087     (setq gnus-newsgroup-last-file filename)))
13088
13089 (defun gnus-summary-save-body-in-file (&optional filename)
13090   "Append this article body to a file.
13091 Optional argument FILENAME specifies file name.
13092 The directory to save in defaults to `gnus-article-save-directory' which
13093 is initialized from the SAVEDIR environment variable."
13094   (interactive)
13095   (gnus-set-global-variables)
13096   (let ((default-name
13097           (funcall gnus-file-save-name gnus-newsgroup-name
13098                    gnus-current-headers gnus-newsgroup-last-file)))
13099     (setq filename
13100           (cond ((eq filename 'default)
13101                  default-name)
13102                 (filename filename)
13103                 (t (gnus-read-save-file-name
13104                     "Save body in file:" default-name))))
13105     (gnus-make-directory (file-name-directory filename))
13106     (gnus-eval-in-buffer-window
13107      gnus-article-buffer
13108      (save-excursion
13109        (save-restriction
13110          (widen)
13111          (goto-char (point-min))
13112          (and (search-forward "\n\n" nil t)
13113               (narrow-to-region (point) (point-max)))
13114          (gnus-output-to-file filename))))
13115     ;; Remember the directory name to save articles.
13116     (setq gnus-newsgroup-last-file filename)))
13117
13118 (defun gnus-summary-save-in-pipe (&optional command)
13119   "Pipe this article to subprocess."
13120   (interactive)
13121   (gnus-set-global-variables)
13122   (setq command
13123         (cond ((eq command 'default)
13124                gnus-last-shell-command)
13125               (command command)
13126               (t (read-string "Shell command on article: "
13127                               gnus-last-shell-command))))
13128   (if (string-equal command "")
13129       (setq command gnus-last-shell-command))
13130   (gnus-eval-in-buffer-window
13131    gnus-article-buffer
13132    (save-restriction
13133      (widen)
13134      (shell-command-on-region (point-min) (point-max) command nil)))
13135   (setq gnus-last-shell-command command))
13136
13137 ;; Summary extract commands
13138
13139 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13140   (let ((buffer-read-only nil)
13141         (article (gnus-summary-article-number))
13142         after-article b e)
13143     (or (gnus-summary-goto-subject article)
13144         (error (format "No such article: %d" article)))
13145     (gnus-summary-position-point)
13146     ;; If all commands are to be bunched up on one line, we collect
13147     ;; them here.
13148     (if gnus-view-pseudos-separately
13149         ()
13150       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13151             files action)
13152         (while ps
13153           (setq action (cdr (assq 'action (car ps))))
13154           (setq files (list (cdr (assq 'name (car ps)))))
13155           (while (and ps (cdr ps)
13156                       (string= (or action "1")
13157                                (or (cdr (assq 'action (cadr ps))) "2")))
13158             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13159             (setcdr ps (cddr ps)))
13160           (if (not files)
13161               ()
13162             (if (not (string-match "%s" action))
13163                 (setq files (cons " " files)))
13164             (setq files (cons " " files))
13165             (and (assq 'execute (car ps))
13166                  (setcdr (assq 'execute (car ps))
13167                          (funcall (if (string-match "%s" action)
13168                                       'format 'concat)
13169                                   action
13170                                   (mapconcat (lambda (f) f) files " ")))))
13171           (setq ps (cdr ps)))))
13172     (if (and gnus-view-pseudos (not not-view))
13173         (while pslist
13174           (and (assq 'execute (car pslist))
13175                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13176                                      (eq gnus-view-pseudos 'not-confirm)))
13177           (setq pslist (cdr pslist)))
13178       (save-excursion
13179         (while pslist
13180           (setq after-article (or (cdr (assq 'article (car pslist)))
13181                                   (gnus-summary-article-number)))
13182           (gnus-summary-goto-subject after-article)
13183           (forward-line 1)
13184           (setq b (point))
13185           (insert "    " (file-name-nondirectory
13186                                 (cdr (assq 'name (car pslist))))
13187                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13188           (setq e (point))
13189           (forward-line -1)             ; back to `b'
13190           (gnus-add-text-properties
13191            b e (list 'gnus-number gnus-reffed-article-number
13192                      gnus-mouse-face-prop gnus-mouse-face))
13193           (gnus-data-enter
13194            after-article gnus-reffed-article-number
13195            gnus-unread-mark b (car pslist) 0 (- e b))
13196           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13197           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13198           (setq pslist (cdr pslist)))))))
13199
13200 (defun gnus-pseudos< (p1 p2)
13201   (let ((c1 (cdr (assq 'action p1)))
13202         (c2 (cdr (assq 'action p2))))
13203     (and c1 c2 (string< c1 c2))))
13204
13205 (defun gnus-request-pseudo-article (props)
13206   (cond ((assq 'execute props)
13207          (gnus-execute-command (cdr (assq 'execute props)))))
13208   (let ((gnus-current-article (gnus-summary-article-number)))
13209     (run-hooks 'gnus-mark-article-hook)))
13210
13211 (defun gnus-execute-command (command &optional automatic)
13212   (save-excursion
13213     (gnus-article-setup-buffer)
13214     (set-buffer gnus-article-buffer)
13215     (setq buffer-read-only nil)
13216     (let ((command (if automatic command (read-string "Command: " command)))
13217           ;; Just binding this here doesn't help, because there might
13218           ;; be output from the process after exiting the scope of 
13219           ;; this `let'.
13220           ;; (buffer-read-only nil)
13221           )
13222       (erase-buffer)
13223       (insert "$ " command "\n\n")
13224       (if gnus-view-pseudo-asynchronously
13225           (start-process "gnus-execute" nil "sh" "-c" command)
13226         (call-process "sh" nil t nil "-c" command)))))
13227
13228 (defun gnus-copy-file (file &optional to)
13229   "Copy FILE to TO."
13230   (interactive
13231    (list (read-file-name "Copy file: " default-directory)
13232          (read-file-name "Copy file to: " default-directory)))
13233   (gnus-set-global-variables)
13234   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13235   (and (file-directory-p to)
13236        (setq to (concat (file-name-as-directory to)
13237                         (file-name-nondirectory file))))
13238   (copy-file file to))
13239
13240 ;; Summary kill commands.
13241
13242 (defun gnus-summary-edit-global-kill (article)
13243   "Edit the \"global\" kill file."
13244   (interactive (list (gnus-summary-article-number)))
13245   (gnus-set-global-variables)
13246   (gnus-group-edit-global-kill article))
13247
13248 (defun gnus-summary-edit-local-kill ()
13249   "Edit a local kill file applied to the current newsgroup."
13250   (interactive)
13251   (gnus-set-global-variables)
13252   (setq gnus-current-headers (gnus-summary-article-header))
13253   (gnus-set-global-variables)
13254   (gnus-group-edit-local-kill
13255    (gnus-summary-article-number) gnus-newsgroup-name))
13256
13257 \f
13258 ;;;
13259 ;;; Gnus article mode
13260 ;;;
13261
13262 (put 'gnus-article-mode 'mode-class 'special)
13263
13264 (if gnus-article-mode-map
13265     nil
13266   (setq gnus-article-mode-map (make-keymap))
13267   (suppress-keymap gnus-article-mode-map)
13268
13269   (gnus-define-keys gnus-article-mode-map
13270     " " gnus-article-goto-next-page
13271     "\177" gnus-article-goto-prev-page
13272     [delete] gnus-article-goto-prev-page
13273     "\C-c^" gnus-article-refer-article
13274     "h" gnus-article-show-summary
13275     "s" gnus-article-show-summary
13276     "\C-c\C-m" gnus-article-mail
13277     "?" gnus-article-describe-briefly
13278     gnus-mouse-2 gnus-article-push-button
13279     "\r" gnus-article-press-button
13280     "\t" gnus-article-next-button
13281     "\M-\t" gnus-article-prev-button
13282     "\C-c\C-b" gnus-bug)
13283
13284   (substitute-key-definition
13285    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13286
13287 (defun gnus-article-mode ()
13288   "Major mode for displaying an article.
13289
13290 All normal editing commands are switched off.
13291
13292 The following commands are available:
13293
13294 \\<gnus-article-mode-map>
13295 \\[gnus-article-next-page]\t Scroll the article one page forwards
13296 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13297 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13298 \\[gnus-article-show-summary]\t Display the summary buffer
13299 \\[gnus-article-mail]\t Send a reply to the address near point
13300 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13301 \\[gnus-info-find-node]\t Go to the Gnus info node"
13302   (interactive)
13303   (when (and menu-bar-mode
13304              (gnus-visual-p 'article-menu 'menu))
13305     (gnus-article-make-menu-bar))
13306   (kill-all-local-variables)
13307   (gnus-simplify-mode-line)
13308   (setq mode-name "Article")
13309   (setq major-mode 'gnus-article-mode)
13310   (make-local-variable 'minor-mode-alist)
13311   (or (assq 'gnus-show-mime minor-mode-alist)
13312       (setq minor-mode-alist
13313             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13314   (use-local-map gnus-article-mode-map)
13315   (make-local-variable 'page-delimiter)
13316   (setq page-delimiter gnus-page-delimiter)
13317   (buffer-disable-undo (current-buffer))
13318   (setq buffer-read-only t)             ;Disable modification
13319   (run-hooks 'gnus-article-mode-hook))
13320
13321 (defun gnus-article-setup-buffer ()
13322   "Initialize the article buffer."
13323   (let* ((name (if gnus-single-article-buffer "*Article*"
13324                  (concat "*Article " gnus-newsgroup-name "*")))
13325          (original
13326           (progn (string-match "\\*Article" name)
13327                  (concat " *Original Article"
13328                          (substring name (match-end 0))))))
13329     (setq gnus-article-buffer name)
13330     (setq gnus-original-article-buffer original)
13331     ;; This might be a variable local to the summary buffer.
13332     (unless gnus-single-article-buffer
13333       (save-excursion
13334         (set-buffer gnus-summary-buffer)
13335         (setq gnus-article-buffer name)
13336         (setq gnus-original-article-buffer original)
13337         (gnus-set-global-variables))
13338       (make-local-variable 'gnus-summary-buffer))
13339     ;; Init original article buffer.
13340     (save-excursion
13341       (set-buffer (get-buffer-create gnus-original-article-buffer))
13342       (buffer-disable-undo (current-buffer))
13343       (setq major-mode 'gnus-original-article-mode)
13344       (make-local-variable 'gnus-original-article))
13345     (if (get-buffer name)
13346         (save-excursion
13347           (set-buffer name)
13348           (buffer-disable-undo (current-buffer))
13349           (setq buffer-read-only t)
13350           (gnus-add-current-to-buffer-list)
13351           (or (eq major-mode 'gnus-article-mode)
13352               (gnus-article-mode))
13353           (current-buffer))
13354       (save-excursion
13355         (set-buffer (get-buffer-create name))
13356         (gnus-add-current-to-buffer-list)
13357         (gnus-article-mode)
13358         (current-buffer)))))
13359
13360 ;; Set article window start at LINE, where LINE is the number of lines
13361 ;; from the head of the article.
13362 (defun gnus-article-set-window-start (&optional line)
13363   (set-window-start
13364    (get-buffer-window gnus-article-buffer)
13365    (save-excursion
13366      (set-buffer gnus-article-buffer)
13367      (goto-char (point-min))
13368      (if (not line)
13369          (point-min)
13370        (gnus-message 6 "Moved to bookmark")
13371        (search-forward "\n\n" nil t)
13372        (forward-line line)
13373        (point)))))
13374
13375 (defun gnus-kill-all-overlays ()
13376   "Delete all overlays in the current buffer."
13377   (when (fboundp 'overlay-lists)
13378     (let* ((overlayss (overlay-lists))
13379            (buffer-read-only nil)
13380            (overlays (nconc (car overlayss) (cdr overlayss))))
13381       (while overlays
13382         (delete-overlay (pop overlays))))))
13383
13384 (defun gnus-request-article-this-buffer (article group)
13385   "Get an article and insert it into this buffer."
13386   (let (do-update-line)
13387     (prog1
13388         (save-excursion
13389           (erase-buffer)
13390           (gnus-kill-all-overlays)
13391           (setq group (or group gnus-newsgroup-name))
13392
13393           ;; Open server if it has closed.
13394           (gnus-check-server (gnus-find-method-for-group group))
13395
13396           ;; Using `gnus-request-article' directly will insert the article into
13397           ;; `nntp-server-buffer' - so we'll save some time by not having to
13398           ;; copy it from the server buffer into the article buffer.
13399
13400           ;; We only request an article by message-id when we do not have the
13401           ;; headers for it, so we'll have to get those.
13402           (when (stringp article)
13403             (let ((gnus-override-method gnus-refer-article-method))
13404               (gnus-read-header article)))
13405
13406           ;; If the article number is negative, that means that this article
13407           ;; doesn't belong in this newsgroup (possibly), so we find its
13408           ;; message-id and request it by id instead of number.
13409           (when (and (numberp article)
13410                      gnus-summary-buffer
13411                      (get-buffer gnus-summary-buffer)
13412                      (buffer-name (get-buffer gnus-summary-buffer)))
13413             (save-excursion
13414               (set-buffer gnus-summary-buffer)
13415               (let ((header (gnus-summary-article-header article)))
13416                 (if (< article 0)
13417                     (cond 
13418                      ((memq article gnus-newsgroup-sparse)
13419                       ;; This is a sparse gap article.
13420                       (setq do-update-line article)
13421                       (setq article (mail-header-id header))
13422                       (let ((gnus-override-method gnus-refer-article-method))
13423                         (gnus-read-header article))
13424                       (setq gnus-newsgroup-sparse
13425                             (delq article gnus-newsgroup-sparse)))
13426                      ((vectorp header)
13427                       ;; It's a real article.
13428                       (setq article (mail-header-id header)))
13429                      (t
13430                       ;; It is an extracted pseudo-article.
13431                       (setq article 'pseudo)
13432                       (gnus-request-pseudo-article header))))
13433                 
13434                 (let ((method (gnus-find-method-for-group 
13435                                gnus-newsgroup-name)))
13436                   (if (not (eq (car method) 'nneething))
13437                       ()
13438                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13439                                        (mail-header-subject header))))
13440                       (if (file-directory-p dir)
13441                           (progn
13442                             (setq article 'nneething)
13443                             (gnus-group-enter-directory dir)))))))))
13444
13445           (cond
13446            ;; Refuse to select canceled articles.
13447            ((and (numberp article)
13448                  gnus-summary-buffer
13449                  (get-buffer gnus-summary-buffer)
13450                  (buffer-name (get-buffer gnus-summary-buffer))
13451                  (eq (cdr (save-excursion
13452                             (set-buffer gnus-summary-buffer)
13453                             (assq article gnus-newsgroup-reads)))
13454                      gnus-canceled-mark))
13455             nil)
13456            ;; We first check `gnus-original-article-buffer'.
13457            ((and (get-buffer gnus-original-article-buffer)
13458                  (numberp article)
13459                  (save-excursion
13460                    (set-buffer gnus-original-article-buffer)
13461                    (and (equal (car gnus-original-article) group)
13462                         (eq (cdr gnus-original-article) article))))
13463             (insert-buffer-substring gnus-original-article-buffer)
13464             'article)
13465            ;; Check the backlog.
13466            ((and gnus-keep-backlog
13467                  (gnus-backlog-request-article group article (current-buffer)))
13468             'article)
13469            ;; Check the cache.
13470            ((and gnus-use-cache
13471                  (numberp article)
13472                  (gnus-cache-request-article article group))
13473             'article)
13474            ;; Get the article and put into the article buffer.
13475            ((or (stringp article) (numberp article))
13476             (let ((gnus-override-method
13477                    (and (stringp article) gnus-refer-article-method))
13478                   (buffer-read-only nil))
13479               (erase-buffer)
13480               (gnus-kill-all-overlays)
13481               (if (gnus-request-article article group (current-buffer))
13482                   (progn
13483                     (and gnus-keep-backlog
13484                          (numberp article)
13485                          (gnus-backlog-enter-article
13486                           group article (current-buffer)))
13487                     'article))))
13488            ;; It was a pseudo.
13489            (t article)))
13490
13491       ;; Take the article from the original article buffer
13492       ;; and place it in the buffer it's supposed to be in.
13493       (when (and (get-buffer gnus-article-buffer)
13494                  (numberp article)
13495                  (equal (buffer-name (current-buffer))
13496                         (buffer-name (get-buffer gnus-article-buffer))))
13497         (save-excursion
13498           (if (get-buffer gnus-original-article-buffer)
13499               (set-buffer (get-buffer gnus-original-article-buffer))
13500             (set-buffer (get-buffer-create gnus-original-article-buffer))
13501             (buffer-disable-undo (current-buffer))
13502             (setq major-mode 'gnus-original-article-mode)
13503             (setq buffer-read-only t)
13504             (gnus-add-current-to-buffer-list))
13505           (let (buffer-read-only)
13506             (erase-buffer)
13507             (insert-buffer-substring gnus-article-buffer))
13508           (setq gnus-original-article (cons group article))))
13509     
13510       ;; Update sparse articles.
13511       (when (and do-update-line
13512                  (or (numberp article)
13513                      (stringp article)))
13514         (let ((buf (current-buffer)))
13515           (set-buffer gnus-summary-buffer)
13516           (gnus-summary-update-article do-update-line)
13517           (gnus-summary-goto-subject do-update-line nil t)
13518           (set-window-point (get-buffer-window (current-buffer) t)
13519                             (point))
13520           (set-buffer buf))))))
13521
13522 (defun gnus-read-header (id &optional header)
13523   "Read the headers of article ID and enter them into the Gnus system."
13524   (let ((group gnus-newsgroup-name)
13525         (gnus-override-method 
13526          (and (gnus-news-group-p gnus-newsgroup-name)
13527               gnus-refer-article-method))       
13528         where)
13529     ;; First we check to see whether the header in question is already
13530     ;; fetched.
13531     (if (stringp id)
13532         ;; This is a Message-ID.
13533         (setq header (or header (gnus-id-to-header id)))
13534       ;; This is an article number.
13535       (setq header (or header (gnus-summary-article-header id))))
13536     (if (and header
13537              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13538         ;; We have found the header.
13539         header
13540       ;; We have to really fetch the header to this article.
13541       (when (setq where
13542                   (if (gnus-check-backend-function 'request-head group)
13543                       (gnus-request-head id group)
13544                     (gnus-request-article id group)))
13545         (save-excursion
13546           (set-buffer nntp-server-buffer)
13547           (and (search-forward "\n\n" nil t)
13548                (delete-region (1- (point)) (point-max)))
13549           (goto-char (point-max))
13550           (insert ".\n")
13551           (goto-char (point-min))
13552           (insert "211 ")
13553           (princ (cond
13554                   ((numberp id) id)
13555                   ((cdr where) (cdr where))
13556                   (header (mail-header-number header))
13557                   (t gnus-reffed-article-number))
13558                  (current-buffer))
13559           (insert " Article retrieved.\n"))
13560         ;(when (and header
13561         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13562         ;  (setcar (gnus-id-to-thread id) nil))
13563         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13564             ()                          ; Malformed head.
13565           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13566             (if (and (stringp id)
13567                      (not (string= (gnus-group-real-name group)
13568                                    (car where))))
13569                 ;; If we fetched by Message-ID and the article came
13570                 ;; from a different group, we fudge some bogus article
13571                 ;; numbers for this article.
13572                 (mail-header-set-number header gnus-reffed-article-number))
13573             (decf gnus-reffed-article-number)
13574             (push header gnus-newsgroup-headers)
13575             (setq gnus-current-headers header)
13576             (push (mail-header-number header) gnus-newsgroup-limit))
13577           header)))))
13578
13579 (defun gnus-article-prepare (article &optional all-headers header)
13580   "Prepare ARTICLE in article mode buffer.
13581 ARTICLE should either be an article number or a Message-ID.
13582 If ARTICLE is an id, HEADER should be the article headers.
13583 If ALL-HEADERS is non-nil, no headers are hidden."
13584   (save-excursion
13585     ;; Make sure we start in a summary buffer.
13586     (unless (eq major-mode 'gnus-summary-mode)
13587       (set-buffer gnus-summary-buffer))
13588     (setq gnus-summary-buffer (current-buffer))
13589     ;; Make sure the connection to the server is alive.
13590     (unless (gnus-server-opened
13591              (gnus-find-method-for-group gnus-newsgroup-name))
13592       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13593       (gnus-request-group gnus-newsgroup-name t))
13594     (let* ((article (if header (mail-header-number header) article))
13595            (summary-buffer (current-buffer))
13596            (internal-hook gnus-article-internal-prepare-hook)
13597            (group gnus-newsgroup-name)
13598            result)
13599       (save-excursion
13600         (gnus-article-setup-buffer)
13601         (set-buffer gnus-article-buffer)
13602         ;; Deactivate active regions.
13603         (when (and (boundp 'transient-mark-mode)
13604                    transient-mark-mode)
13605           (setq mark-active nil))
13606         (if (not (setq result (let ((buffer-read-only nil))
13607                                 (gnus-request-article-this-buffer
13608                                  article group))))
13609             ;; There is no such article.
13610             (save-excursion
13611               (when (and (numberp article)
13612                          (not (memq article gnus-newsgroup-sparse)))
13613                 (setq gnus-article-current
13614                       (cons gnus-newsgroup-name article))
13615                 (set-buffer gnus-summary-buffer)
13616                 (setq gnus-current-article article)
13617                 (gnus-summary-mark-article article gnus-canceled-mark))
13618               (unless (memq article gnus-newsgroup-sparse)
13619                 (gnus-error
13620                  1 "No such article (may have expired or been canceled)")))
13621           (if (or (eq result 'pseudo) (eq result 'nneething))
13622               (progn
13623                 (save-excursion
13624                   (set-buffer summary-buffer)
13625                   (setq gnus-last-article gnus-current-article
13626                         gnus-newsgroup-history (cons gnus-current-article
13627                                                      gnus-newsgroup-history)
13628                         gnus-current-article 0
13629                         gnus-current-headers nil
13630                         gnus-article-current nil)
13631                   (if (eq result 'nneething)
13632                       (gnus-configure-windows 'summary)
13633                     (gnus-configure-windows 'article))
13634                   (gnus-set-global-variables))
13635                 (gnus-set-mode-line 'article))
13636             ;; The result from the `request' was an actual article -
13637             ;; or at least some text that is now displayed in the
13638             ;; article buffer.
13639             (if (and (numberp article)
13640                      (not (eq article gnus-current-article)))
13641                 ;; Seems like a new article has been selected.
13642                 ;; `gnus-current-article' must be an article number.
13643                 (save-excursion
13644                   (set-buffer summary-buffer)
13645                   (setq gnus-last-article gnus-current-article
13646                         gnus-newsgroup-history (cons gnus-current-article
13647                                                      gnus-newsgroup-history)
13648                         gnus-current-article article
13649                         gnus-current-headers
13650                         (gnus-summary-article-header gnus-current-article)
13651                         gnus-article-current
13652                         (cons gnus-newsgroup-name gnus-current-article))
13653                   (unless (vectorp gnus-current-headers)
13654                     (setq gnus-current-headers nil))
13655                   (gnus-summary-show-thread)
13656                   (run-hooks 'gnus-mark-article-hook)
13657                   (gnus-set-mode-line 'summary)
13658                   (and (gnus-visual-p 'article-highlight 'highlight)
13659                        (run-hooks 'gnus-visual-mark-article-hook))
13660                   ;; Set the global newsgroup variables here.
13661                   ;; Suggested by Jim Sisolak
13662                   ;; <sisolak@trans4.neep.wisc.edu>.
13663                   (gnus-set-global-variables)
13664                   (setq gnus-have-all-headers
13665                         (or all-headers gnus-show-all-headers))
13666                   (and gnus-use-cache
13667                        (vectorp (gnus-summary-article-header article))
13668                        (gnus-cache-possibly-enter-article
13669                         group article
13670                         (gnus-summary-article-header article)
13671                         (memq article gnus-newsgroup-marked)
13672                         (memq article gnus-newsgroup-dormant)
13673                         (memq article gnus-newsgroup-unreads)))))
13674             (when (or (numberp article)
13675                       (stringp article))
13676               ;; Hooks for getting information from the article.
13677               ;; This hook must be called before being narrowed.
13678               (let (buffer-read-only)
13679                 (run-hooks 'internal-hook)
13680                 (run-hooks 'gnus-article-prepare-hook)
13681                 ;; Decode MIME message.
13682                 (if gnus-show-mime
13683                     (if (or (not gnus-strict-mime)
13684                             (gnus-fetch-field "Mime-Version"))
13685                         (funcall gnus-show-mime-method)
13686                       (funcall gnus-decode-encoded-word-method)))
13687                 ;; Perform the article display hooks.
13688                 (run-hooks 'gnus-article-display-hook))
13689               ;; Do page break.
13690               (goto-char (point-min))
13691               (and gnus-break-pages (gnus-narrow-to-page)))
13692             (gnus-set-mode-line 'article)
13693             (gnus-configure-windows 'article)
13694             (goto-char (point-min))
13695             t))))))
13696
13697 (defun gnus-article-show-all-headers ()
13698   "Show all article headers in article mode buffer."
13699   (save-excursion
13700     (gnus-article-setup-buffer)
13701     (set-buffer gnus-article-buffer)
13702     (let ((buffer-read-only nil))
13703       (gnus-unhide-text (point-min) (point-max)))))
13704
13705 (defun gnus-article-hide-headers-if-wanted ()
13706   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13707 Provided for backwards compatibility."
13708   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13709       gnus-inhibit-hiding
13710       (gnus-article-hide-headers)))
13711
13712 (defsubst gnus-article-header-rank ()
13713   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13714   (let ((list gnus-sorted-header-list)
13715         (i 0))
13716     (while list
13717       (when (looking-at (car list))
13718         (setq list nil))
13719       (setq list (cdr list))
13720       (incf i))
13721     i))
13722
13723 (defun gnus-article-hide-headers (&optional arg delete)
13724   "Toggle whether to hide unwanted headers and possibly sort them as well.
13725 If given a negative prefix, always show; if given a positive prefix,
13726 always hide."
13727   (interactive "P")
13728   (unless (gnus-article-check-hidden-text 'headers arg)
13729     ;; This function might be inhibited.
13730     (unless gnus-inhibit-hiding
13731       (save-excursion
13732         (set-buffer gnus-article-buffer)
13733         (save-restriction
13734           (let ((buffer-read-only nil)
13735                 (props (nconc (list 'gnus-type 'headers)
13736                               gnus-hidden-properties))
13737                 (max (1+ (length gnus-sorted-header-list)))
13738                 (ignored (when (not (stringp gnus-visible-headers))
13739                            (cond ((stringp gnus-ignored-headers)
13740                                   gnus-ignored-headers)
13741                                  ((listp gnus-ignored-headers)
13742                                   (mapconcat 'identity gnus-ignored-headers
13743                                              "\\|")))))
13744                 (visible
13745                  (cond ((stringp gnus-visible-headers)
13746                         gnus-visible-headers)
13747                        ((and gnus-visible-headers
13748                              (listp gnus-visible-headers))
13749                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13750                 want-list beg)
13751             ;; First we narrow to just the headers.
13752             (widen)
13753             (goto-char (point-min))
13754             ;; Hide any "From " lines at the beginning of (mail) articles.
13755             (while (looking-at "From ")
13756               (forward-line 1))
13757             (unless (bobp)
13758               (gnus-hide-text (point-min) (point) props))
13759             ;; Then treat the rest of the header lines.
13760             (narrow-to-region
13761              (point)
13762              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13763             ;; Then we use the two regular expressions
13764             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13765             ;; select which header lines is to remain visible in the
13766             ;; article buffer.
13767             (goto-char (point-min))
13768             (while (re-search-forward "^[^ \t]*:" nil t)
13769               (beginning-of-line)
13770               ;; We add the headers we want to keep to a list and delete
13771               ;; them from the buffer.
13772               (put-text-property 
13773                (point) (1+ (point)) 'message-rank
13774                (if (or (and visible (looking-at visible))
13775                        (and ignored
13776                             (not (looking-at ignored))))
13777                    (gnus-article-header-rank) 
13778                  (+ 2 max)))
13779               (forward-line 1))
13780             (message-sort-headers-1)
13781             (when (setq beg (text-property-any 
13782                              (point-min) (point-max) 'message-rank (+ 2 max)))
13783               ;; We make the unwanted headers invisible.
13784               (if delete
13785                   (delete-region beg (point-max))
13786                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13787                 (gnus-hide-text-type beg (point-max) 'headers))
13788               ;; Work around XEmacs lossage.
13789               (put-text-property (point-min) beg 'invisible nil))))))))
13790
13791 (defun gnus-article-hide-boring-headers (&optional arg)
13792   "Toggle hiding of headers that aren't very interesting.
13793 If given a negative prefix, always show; if given a positive prefix,
13794 always hide."
13795   (interactive "P")
13796   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13797     (save-excursion
13798       (set-buffer gnus-article-buffer)
13799       (save-restriction
13800         (let ((buffer-read-only nil)
13801               (list gnus-boring-article-headers)
13802               (inhibit-point-motion-hooks t)
13803               elem)
13804           (nnheader-narrow-to-headers)
13805           (while list
13806             (setq elem (pop list))
13807             (goto-char (point-min))
13808             (cond
13809              ;; Hide empty headers.
13810              ((eq elem 'empty)
13811               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13812                 (forward-line -1)
13813                 (gnus-hide-text-type
13814                  (progn (beginning-of-line) (point))
13815                  (progn 
13816                    (end-of-line)
13817                    (if (re-search-forward "^[^ \t]" nil t)
13818                        (match-beginning 0)
13819                      (point-max)))
13820                  'boring-headers)))
13821              ;; Hide boring Newsgroups header.
13822              ((eq elem 'newsgroups)
13823               (when (equal (mail-fetch-field "newsgroups")
13824                            (gnus-group-real-name gnus-newsgroup-name))
13825                 (gnus-article-hide-header "newsgroups")))
13826              ((eq elem 'followup-to)
13827               (when (equal (mail-fetch-field "followup-to")
13828                            (mail-fetch-field "newsgroups"))
13829                 (gnus-article-hide-header "followup-to")))
13830              ((eq elem 'reply-to)
13831               (let ((from (mail-fetch-field "from"))
13832                     (reply-to (mail-fetch-field "reply-to")))
13833                 (when (and
13834                        from reply-to
13835                        (equal 
13836                         (nth 1 (funcall gnus-extract-address-components from))
13837                         (nth 1 (funcall gnus-extract-address-components
13838                                         reply-to))))
13839                   (gnus-article-hide-header "reply-to"))))
13840              ((eq elem 'date)
13841               (let ((date (mail-fetch-field "date")))
13842                 (when (and date
13843                            (< (gnus-days-between date (current-time-string))
13844                               4))
13845                   (gnus-article-hide-header "date")))))))))))
13846
13847 (defun gnus-article-hide-header (header)
13848   (save-excursion
13849     (goto-char (point-min))
13850     (when (re-search-forward (concat "^" header ":") nil t)
13851       (gnus-hide-text-type
13852        (progn (beginning-of-line) (point))
13853        (progn 
13854          (end-of-line)
13855          (if (re-search-forward "^[^ \t]" nil t)
13856              (match-beginning 0)
13857            (point-max)))
13858        'boring-headers))))
13859
13860 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13861 (defun gnus-article-treat-overstrike ()
13862   "Translate overstrikes into bold text."
13863   (interactive)
13864   (save-excursion
13865     (set-buffer gnus-article-buffer)
13866     (let ((buffer-read-only nil))
13867       (while (search-forward "\b" nil t)
13868         (let ((next (following-char))
13869               (previous (char-after (- (point) 2))))
13870           (cond ((eq next previous)
13871                  (put-text-property (- (point) 2) (point) 'invisible t)
13872                  (put-text-property (point) (1+ (point)) 'face 'bold))
13873                 ((eq next ?_)
13874                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13875                  (put-text-property
13876                   (- (point) 2) (1- (point)) 'face 'underline))
13877                 ((eq previous ?_)
13878                  (put-text-property (- (point) 2) (point) 'invisible t)
13879                  (put-text-property
13880                   (point) (1+ (point))  'face 'underline))))))))
13881
13882 (defun gnus-article-word-wrap ()
13883   "Format too long lines."
13884   (interactive)
13885   (save-excursion
13886     (set-buffer gnus-article-buffer)
13887     (let ((buffer-read-only nil))
13888       (widen)
13889       (goto-char (point-min))
13890       (search-forward "\n\n" nil t)
13891       (end-of-line 1)
13892       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13893             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13894             (adaptive-fill-mode t))
13895         (while (not (eobp))
13896           (and (>= (current-column) (min fill-column (window-width)))
13897                (/= (preceding-char) ?:)
13898                (fill-paragraph nil))
13899           (end-of-line 2))))))
13900
13901 (defun gnus-article-remove-cr ()
13902   "Remove carriage returns from an article."
13903   (interactive)
13904   (save-excursion
13905     (set-buffer gnus-article-buffer)
13906     (let ((buffer-read-only nil))
13907       (goto-char (point-min))
13908       (while (search-forward "\r" nil t)
13909         (replace-match "" t t)))))
13910
13911 (defun gnus-article-remove-trailing-blank-lines ()
13912   "Remove all trailing blank lines from the article."
13913   (interactive)
13914   (save-excursion
13915     (set-buffer gnus-article-buffer)
13916     (let ((buffer-read-only nil))
13917       (goto-char (point-max))
13918       (delete-region
13919        (point)
13920        (progn
13921          (while (looking-at "^[ \t]*$")
13922            (forward-line -1))
13923          (forward-line 1)
13924          (point))))))
13925
13926 (defun gnus-article-display-x-face (&optional force)
13927   "Look for an X-Face header and display it if present."
13928   (interactive (list 'force))
13929   (save-excursion
13930     (set-buffer gnus-article-buffer)
13931     ;; Delete the old process, if any.
13932     (when (process-status "gnus-x-face")
13933       (delete-process "gnus-x-face"))
13934     (let ((inhibit-point-motion-hooks t)
13935           (case-fold-search nil)
13936           from)
13937       (save-restriction
13938         (nnheader-narrow-to-headers)
13939         (setq from (mail-fetch-field "from"))
13940         (goto-char (point-min))
13941         (when (and gnus-article-x-face-command
13942                    (or force
13943                        ;; Check whether this face is censored.
13944                        (not gnus-article-x-face-too-ugly)
13945                        (and gnus-article-x-face-too-ugly from
13946                             (not (string-match gnus-article-x-face-too-ugly
13947                                                from))))
13948                    ;; Has to be present.
13949                    (re-search-forward "^X-Face: " nil t))
13950           ;; We now have the area of the buffer where the X-Face is stored.
13951           (let ((beg (point))
13952                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13953             ;; We display the face.
13954             (if (symbolp gnus-article-x-face-command)
13955                 ;; The command is a lisp function, so we call it.
13956                 (if (gnus-functionp gnus-article-x-face-command)
13957                     (funcall gnus-article-x-face-command beg end)
13958                   (error "%s is not a function" gnus-article-x-face-command))
13959               ;; The command is a string, so we interpret the command
13960               ;; as a, well, command, and fork it off.
13961               (let ((process-connection-type nil))
13962                 (process-kill-without-query
13963                  (start-process
13964                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13965                 (process-send-region "gnus-x-face" beg end)
13966                 (process-send-eof "gnus-x-face")))))))))
13967
13968 (defalias 'gnus-header-decode-quoted-printable 'gnus-decode-rfc1522)
13969 (defun gnus-decode-rfc1522 ()
13970   "Hack to remove QP encoding from headers."
13971   (let ((case-fold-search t)
13972         (inhibit-point-motion-hooks t)
13973         (buffer-read-only nil)
13974         string)
13975     (save-restriction
13976       (narrow-to-region
13977        (goto-char (point-min))
13978        (or (search-forward "\n\n" nil t) (point-max)))
13979
13980       (while (re-search-forward 
13981               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13982         (setq string (match-string 1))
13983         (narrow-to-region (match-beginning 0) (match-end 0))
13984         (delete-region (point-min) (point-max))
13985         (insert string)
13986         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13987         (subst-char-in-region (point-min) (point-max) ?_ ? )
13988         (widen)
13989         (goto-char (point-min))))))
13990
13991 (defun gnus-article-de-quoted-unreadable (&optional force)
13992   "Do a naive translation of a quoted-printable-encoded article.
13993 This is in no way, shape or form meant as a replacement for real MIME
13994 processing, but is simply a stop-gap measure until MIME support is
13995 written.
13996 If FORCE, decode the article whether it is marked as quoted-printable
13997 or not."
13998   (interactive (list 'force))
13999   (save-excursion
14000     (set-buffer gnus-article-buffer)
14001     (let ((case-fold-search t)
14002           (buffer-read-only nil)
14003           (type (gnus-fetch-field "content-transfer-encoding")))
14004       (gnus-decode-rfc1522)
14005       (when (or force
14006                 (and type (string-match "quoted-printable" (downcase type))))
14007         (goto-char (point-min))
14008         (search-forward "\n\n" nil 'move)
14009         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14010
14011 (defun gnus-mime-decode-quoted-printable (from to)
14012   "Decode Quoted-Printable in the region between FROM and TO."
14013   (interactive "r")
14014   (goto-char from)
14015   (while (search-forward "=" to t)
14016     (cond ((eq (following-char) ?\n)
14017            (delete-char -1)
14018            (delete-char 1))
14019           ((looking-at "[0-9A-F][0-9A-F]")
14020            (subst-char-in-region
14021             (1- (point)) (point) ?=
14022             (hexl-hex-string-to-integer
14023              (buffer-substring (point) (+ 2 (point)))))
14024            (delete-char 2))
14025           ((looking-at "=")
14026            (delete-char 1))
14027           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14028
14029 (defun gnus-article-hide-pgp (&optional arg)
14030   "Toggle hiding of any PGP headers and signatures in the current article.
14031 If given a negative prefix, always show; if given a positive prefix,
14032 always hide."
14033   (interactive "P")
14034   (unless (gnus-article-check-hidden-text 'pgp arg)
14035     (save-excursion
14036       (set-buffer gnus-article-buffer)
14037       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14038             buffer-read-only beg end)
14039         (widen)
14040         (goto-char (point-min))
14041         ;; Hide the "header".
14042         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14043              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14044         (setq beg (point))
14045         ;; Hide the actual signature.
14046         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14047              (setq end (1+ (match-beginning 0)))
14048              (gnus-hide-text
14049               end
14050               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14051                   (match-end 0)
14052                 ;; Perhaps we shouldn't hide to the end of the buffer
14053                 ;; if there is no end to the signature?
14054                 (point-max))
14055               props))
14056         ;; Hide "- " PGP quotation markers.
14057         (when (and beg end)
14058           (narrow-to-region beg end)
14059           (goto-char (point-min))
14060           (while (re-search-forward "^- " nil t)
14061             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14062           (widen))))))
14063
14064 (defun gnus-article-hide-signature (&optional arg)
14065   "Hide the signature in the current article.
14066 If given a negative prefix, always show; if given a positive prefix,
14067 always hide."
14068   (interactive "P")
14069   (unless (gnus-article-check-hidden-text 'signature arg)
14070     (save-excursion
14071       (set-buffer gnus-article-buffer)
14072       (save-restriction
14073         (let ((buffer-read-only nil))
14074           (when (gnus-narrow-to-signature)
14075             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14076
14077 (defun gnus-article-strip-leading-blank-lines ()
14078   "Remove all blank lines from the beginning of the article."
14079   (interactive)
14080   (save-excursion
14081     (set-buffer gnus-article-buffer)
14082     (let (buffer-read-only)
14083       (goto-char (point-min))
14084       (when (search-forward "\n\n" nil t)
14085         (while (looking-at "[ \t]$")
14086           (gnus-delete-line))))))
14087
14088 (defun gnus-narrow-to-signature ()
14089   "Narrow to the signature."
14090   (widen)
14091   (goto-char (point-max))
14092   (when (re-search-backward gnus-signature-separator nil t)
14093     (forward-line 1)
14094     (when (or (null gnus-signature-limit)
14095               (and (numberp gnus-signature-limit)
14096                    (< (- (point-max) (point)) gnus-signature-limit))
14097               (and (gnus-functionp gnus-signature-limit)
14098                    (funcall gnus-signature-limit))
14099               (and (stringp gnus-signature-limit)
14100                    (not (re-search-forward gnus-signature-limit nil t))))
14101       (narrow-to-region (point) (point-max))
14102       t)))
14103
14104 (defun gnus-article-check-hidden-text (type arg)
14105   "Return nil if hiding is necessary."
14106   (save-excursion
14107     (set-buffer gnus-article-buffer)
14108     (let ((hide (gnus-article-hidden-text-p type)))
14109       (cond ((or (and (null arg) (eq hide 'hidden))
14110                  (and arg (< 0 (prefix-numeric-value arg))))
14111              (gnus-article-show-hidden-text type))
14112             ((eq hide 'shown)
14113              (gnus-article-show-hidden-text type t))
14114             (t nil)))))
14115
14116 (defun gnus-article-hidden-text-p (type)
14117   "Say whether the current buffer contains hidden text of type TYPE."
14118   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14119     (when pos
14120       (if (get-text-property pos 'invisible)
14121           'hidden
14122         'shown))))
14123
14124 (defun gnus-article-hide (&optional arg force)
14125   "Hide all the gruft in the current article.
14126 This means that PGP stuff, signatures, cited text and (some)
14127 headers will be hidden.
14128 If given a prefix, show the hidden text instead."
14129   (interactive (list current-prefix-arg 'force))
14130   (gnus-article-hide-headers arg)
14131   (gnus-article-hide-pgp arg)
14132   (gnus-article-hide-citation-maybe arg force)
14133   (gnus-article-hide-signature arg))
14134
14135 (defun gnus-article-show-hidden-text (type &optional hide)
14136   "Show all hidden text of type TYPE.
14137 If HIDE, hide the text instead."
14138   (save-excursion
14139     (set-buffer gnus-article-buffer)
14140     (let ((buffer-read-only nil)
14141           (inhibit-point-motion-hooks t)
14142           (beg (point-min)))
14143       (while (gnus-goto-char (text-property-any
14144                               beg (point-max) 'gnus-type type))
14145         (setq beg (point))
14146         (forward-char)
14147         (if hide
14148             (gnus-hide-text beg (point) gnus-hidden-properties)
14149           (gnus-unhide-text beg (point)))
14150         (setq beg (point)))
14151       t)))
14152
14153 (defvar gnus-article-time-units
14154   `((year . ,(* 365.25 24 60 60))
14155     (week . ,(* 7 24 60 60))
14156     (day . ,(* 24 60 60))
14157     (hour . ,(* 60 60))
14158     (minute . 60)
14159     (second . 1))
14160   "Mapping from time units to seconds.")
14161
14162 (defun gnus-article-date-ut (&optional type highlight)
14163   "Convert DATE date to universal time in the current article.
14164 If TYPE is `local', convert to local time; if it is `lapsed', output
14165 how much time has lapsed since DATE."
14166   (interactive (list 'ut t))
14167   (let* ((header (or gnus-current-headers
14168                      (gnus-summary-article-header) ""))
14169          (date (and (vectorp header) (mail-header-date header)))
14170          (date-regexp "^Date: \\|^X-Sent: ")
14171          (now (current-time))
14172          (inhibit-point-motion-hooks t)
14173          bface eface)
14174     (when (and date (not (string= date "")))
14175       (save-excursion
14176         (set-buffer gnus-article-buffer)
14177         (save-restriction
14178           (nnheader-narrow-to-headers)
14179           (let ((buffer-read-only nil))
14180             ;; Delete any old Date headers.
14181             (if (re-search-forward date-regexp nil t)
14182                 (progn
14183                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14184                         eface (get-text-property (1- (gnus-point-at-eol))
14185                                                  'face))
14186                   (message-remove-header date-regexp t)
14187                   (beginning-of-line))
14188               (goto-char (point-max)))
14189             (insert (gnus-make-date-line date type))
14190             ;; Do highlighting.
14191             (forward-line -1)
14192             (when (and (gnus-visual-p 'article-highlight 'highlight)
14193                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14194               (put-text-property (match-beginning 1) (match-end 1)
14195                                  'face bface)
14196               (put-text-property (match-beginning 2) (match-end 2)
14197                                  'face eface))))))))
14198
14199 (defun gnus-make-date-line (date type)
14200   "Return a DATE line of TYPE."
14201   (cond
14202    ;; Convert to the local timezone.     We have to slap a
14203    ;; `condition-case' round the calls to the timezone
14204    ;; functions since they aren't particularly resistant to
14205    ;; buggy dates.
14206    ((eq type 'local)
14207     (concat "Date: " (condition-case ()
14208                          (timezone-make-date-arpa-standard date)
14209                        (error date))
14210             "\n"))
14211    ;; Convert to Universal Time.
14212    ((eq type 'ut)
14213     (concat "Date: "
14214             (condition-case ()
14215                 (timezone-make-date-arpa-standard date nil "UT")
14216               (error date))
14217             "\n"))
14218    ;; Get the original date from the article.
14219    ((eq type 'original)
14220     (concat "Date: " date "\n"))
14221    ;; Do an X-Sent lapsed format.
14222    ((eq type 'lapsed)
14223     ;; If the date is seriously mangled, the timezone
14224     ;; functions are liable to bug out, so we condition-case
14225     ;; the entire thing.
14226     (let* ((now (current-time))
14227            (real-time
14228             (condition-case ()
14229                 (gnus-time-minus
14230                  (gnus-encode-date
14231                   (timezone-make-date-arpa-standard
14232                    (current-time-string now)
14233                    (current-time-zone now) "UT"))
14234                  (gnus-encode-date
14235                   (timezone-make-date-arpa-standard
14236                    date nil "UT")))
14237               (error '(0 0))))
14238            (real-sec (+ (* (float (car real-time)) 65536)
14239                         (cadr real-time)))
14240            (sec (abs real-sec))
14241            num prev)
14242       (cond
14243        ((equal real-time '(0 0))
14244         "X-Sent: Unknown\n")
14245        ((zerop sec)
14246         "X-Sent: Now\n")
14247        (t
14248         (concat
14249          "X-Sent: "
14250          ;; This is a bit convoluted, but basically we go
14251          ;; through the time units for years, weeks, etc,
14252          ;; and divide things to see whether that results
14253          ;; in positive answers.
14254          (mapconcat
14255           (lambda (unit)
14256             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14257                 ;; The (remaining) seconds are too few to
14258                 ;; be divided into this time unit.
14259                 ""
14260               ;; It's big enough, so we output it.
14261               (setq sec (- sec (* num (cdr unit))))
14262               (prog1
14263                   (concat (if prev ", " "") (int-to-string
14264                                              (floor num))
14265                           " " (symbol-name (car unit))
14266                           (if (> num 1) "s" ""))
14267                 (setq prev t))))
14268           gnus-article-time-units "")
14269          ;; If dates are odd, then it might appear like the
14270          ;; article was sent in the future.
14271          (if (> real-sec 0)
14272              " ago\n"
14273            " in the future\n"))))))
14274    (t
14275     (error "Unknown conversion type: %s" type))))
14276
14277 (defun gnus-article-date-local (&optional highlight)
14278   "Convert the current article date to the local timezone."
14279   (interactive (list t))
14280   (gnus-article-date-ut 'local highlight))
14281
14282 (defun gnus-article-date-original (&optional highlight)
14283   "Convert the current article date to what it was originally.
14284 This is only useful if you have used some other date conversion
14285 function and want to see what the date was before converting."
14286   (interactive (list t))
14287   (gnus-article-date-ut 'original highlight))
14288
14289 (defun gnus-article-date-lapsed (&optional highlight)
14290   "Convert the current article date to time lapsed since it was sent."
14291   (interactive (list t))
14292   (gnus-article-date-ut 'lapsed highlight))
14293
14294 (defun gnus-article-maybe-highlight ()
14295   "Do some article highlighting if `gnus-visual' is non-nil."
14296   (if (gnus-visual-p 'article-highlight 'highlight)
14297       (gnus-article-highlight-some)))
14298
14299 ;; Article savers.
14300
14301 (defun gnus-output-to-rmail (file-name)
14302   "Append the current article to an Rmail file named FILE-NAME."
14303   (require 'rmail)
14304   ;; Most of these codes are borrowed from rmailout.el.
14305   (setq file-name (expand-file-name file-name))
14306   (setq rmail-default-rmail-file file-name)
14307   (let ((artbuf (current-buffer))
14308         (tmpbuf (get-buffer-create " *Gnus-output*")))
14309     (save-excursion
14310       (or (get-file-buffer file-name)
14311           (file-exists-p file-name)
14312           (if (gnus-yes-or-no-p
14313                (concat "\"" file-name "\" does not exist, create it? "))
14314               (let ((file-buffer (create-file-buffer file-name)))
14315                 (save-excursion
14316                   (set-buffer file-buffer)
14317                   (rmail-insert-rmail-file-header)
14318                   (let ((require-final-newline nil))
14319                     (write-region (point-min) (point-max) file-name t 1)))
14320                 (kill-buffer file-buffer))
14321             (error "Output file does not exist")))
14322       (set-buffer tmpbuf)
14323       (buffer-disable-undo (current-buffer))
14324       (erase-buffer)
14325       (insert-buffer-substring artbuf)
14326       (gnus-convert-article-to-rmail)
14327       ;; Decide whether to append to a file or to an Emacs buffer.
14328       (let ((outbuf (get-file-buffer file-name)))
14329         (if (not outbuf)
14330             (append-to-file (point-min) (point-max) file-name)
14331           ;; File has been visited, in buffer OUTBUF.
14332           (set-buffer outbuf)
14333           (let ((buffer-read-only nil)
14334                 (msg (and (boundp 'rmail-current-message)
14335                           (symbol-value 'rmail-current-message))))
14336             ;; If MSG is non-nil, buffer is in RMAIL mode.
14337             (if msg
14338                 (progn (widen)
14339                        (narrow-to-region (point-max) (point-max))))
14340             (insert-buffer-substring tmpbuf)
14341             (if msg
14342                 (progn
14343                   (goto-char (point-min))
14344                   (widen)
14345                   (search-backward "\^_")
14346                   (narrow-to-region (point) (point-max))
14347                   (goto-char (1+ (point-min)))
14348                   (rmail-count-new-messages t)
14349                   (rmail-show-message msg)))))))
14350     (kill-buffer tmpbuf)))
14351
14352 (defun gnus-output-to-file (file-name)
14353   "Append the current article to a file named FILE-NAME."
14354   (setq file-name (expand-file-name file-name))
14355   (let ((artbuf (current-buffer))
14356         (tmpbuf (get-buffer-create " *Gnus-output*")))
14357     (save-excursion
14358       (set-buffer tmpbuf)
14359       (buffer-disable-undo (current-buffer))
14360       (erase-buffer)
14361       (insert-buffer-substring artbuf)
14362       ;; Append newline at end of the buffer as separator, and then
14363       ;; save it to file.
14364       (goto-char (point-max))
14365       (insert "\n")
14366       (append-to-file (point-min) (point-max) file-name))
14367     (kill-buffer tmpbuf)))
14368
14369 (defun gnus-convert-article-to-rmail ()
14370   "Convert article in current buffer to Rmail message format."
14371   (let ((buffer-read-only nil))
14372     ;; Convert article directly into Babyl format.
14373     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14374     (goto-char (point-min))
14375     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14376     (while (search-forward "\n\^_" nil t) ;single char
14377       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14378     (goto-char (point-max))
14379     (insert "\^_")))
14380
14381 (defun gnus-narrow-to-page (&optional arg)
14382   "Narrow the article buffer to a page.
14383 If given a numerical ARG, move forward ARG pages."
14384   (interactive "P")
14385   (setq arg (if arg (prefix-numeric-value arg) 0))
14386   (save-excursion
14387     (set-buffer gnus-article-buffer)
14388     (goto-char (point-min))
14389     (widen)
14390     (when (gnus-visual-p 'page-marker)
14391       (let ((buffer-read-only nil))
14392         (gnus-remove-text-with-property 'gnus-prev)
14393         (gnus-remove-text-with-property 'gnus-next)))
14394     (when
14395         (cond ((< arg 0)
14396                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14397               ((> arg 0)
14398                (re-search-forward page-delimiter nil 'move arg)))
14399       (goto-char (match-end 0)))
14400     (narrow-to-region
14401      (point)
14402      (if (re-search-forward page-delimiter nil 'move)
14403          (match-beginning 0)
14404        (point)))
14405     (when (and (gnus-visual-p 'page-marker)
14406                (not (= (point-min) 1)))
14407       (save-excursion
14408         (goto-char (point-min))
14409         (gnus-insert-prev-page-button)))
14410     (when (and (gnus-visual-p 'page-marker)
14411                (not (= (1- (point-max)) (buffer-size))))
14412       (save-excursion
14413         (goto-char (point-max))
14414         (gnus-insert-next-page-button)))))
14415
14416 ;; Article mode commands
14417
14418 (defun gnus-article-goto-next-page ()
14419   "Show the next page of the article."
14420   (interactive)
14421   (when (gnus-article-next-page)
14422     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14423
14424 (defun gnus-article-goto-prev-page ()
14425   "Show the next page of the article."
14426   (interactive)
14427   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14428     (gnus-article-prev-page nil)))
14429
14430 (defun gnus-article-next-page (&optional lines)
14431   "Show the next page of the current article.
14432 If end of article, return non-nil.  Otherwise return nil.
14433 Argument LINES specifies lines to be scrolled up."
14434   (interactive "p")
14435   (move-to-window-line -1)
14436   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14437   (if (save-excursion
14438         (end-of-line)
14439         (and (pos-visible-in-window-p)  ;Not continuation line.
14440              (eobp)))
14441       ;; Nothing in this page.
14442       (if (or (not gnus-break-pages)
14443               (save-excursion
14444                 (save-restriction
14445                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14446           t                             ;Nothing more.
14447         (gnus-narrow-to-page 1)         ;Go to next page.
14448         nil)
14449     ;; More in this page.
14450     (condition-case ()
14451         (scroll-up lines)
14452       (end-of-buffer
14453        ;; Long lines may cause an end-of-buffer error.
14454        (goto-char (point-max))))
14455     (move-to-window-line 0)
14456     nil))
14457
14458 (defun gnus-article-prev-page (&optional lines)
14459   "Show previous page of current article.
14460 Argument LINES specifies lines to be scrolled down."
14461   (interactive "p")
14462   (move-to-window-line 0)
14463   (if (and gnus-break-pages
14464            (bobp)
14465            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14466       (progn
14467         (gnus-narrow-to-page -1)        ;Go to previous page.
14468         (goto-char (point-max))
14469         (recenter -1))
14470     (prog1
14471         (condition-case ()
14472             (scroll-down lines)
14473           (error nil))
14474       (move-to-window-line 0))))
14475
14476 (defun gnus-article-refer-article ()
14477   "Read article specified by message-id around point."
14478   (interactive)
14479   (let ((point (point)))
14480     (search-forward ">" nil t)          ;Move point to end of "<....>".
14481     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14482         (let ((message-id (match-string 1)))
14483           (goto-char point)
14484           (set-buffer gnus-summary-buffer)
14485           (gnus-summary-refer-article message-id))
14486       (goto-char (point))
14487       (error "No references around point"))))
14488
14489 (defun gnus-article-show-summary ()
14490   "Reconfigure windows to show summary buffer."
14491   (interactive)
14492   (gnus-configure-windows 'article)
14493   (gnus-summary-goto-subject gnus-current-article))
14494
14495 (defun gnus-article-describe-briefly ()
14496   "Describe article mode commands briefly."
14497   (interactive)
14498   (gnus-message 6
14499                 (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")))
14500
14501 (defun gnus-article-summary-command ()
14502   "Execute the last keystroke in the summary buffer."
14503   (interactive)
14504   (let ((obuf (current-buffer))
14505         (owin (current-window-configuration))
14506         func)
14507     (switch-to-buffer gnus-summary-buffer 'norecord)
14508     (setq func (lookup-key (current-local-map) (this-command-keys)))
14509     (call-interactively func)
14510     (set-buffer obuf)
14511     (set-window-configuration owin)
14512     (set-window-point (get-buffer-window (current-buffer)) (point))))
14513
14514 (defun gnus-article-summary-command-nosave ()
14515   "Execute the last keystroke in the summary buffer."
14516   (interactive)
14517   (let (func)
14518     (pop-to-buffer gnus-summary-buffer 'norecord)
14519     (setq func (lookup-key (current-local-map) (this-command-keys)))
14520     (call-interactively func)))
14521
14522 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14523   "Read a summary buffer key sequence and execute it from the article buffer."
14524   (interactive "P")
14525   (let ((nosaves
14526          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14527            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14528            "=" "^" "\M-^"))
14529         keys)
14530     (save-excursion
14531       (set-buffer gnus-summary-buffer)
14532       (push (or key last-command-event) unread-command-events)
14533       (setq keys (read-key-sequence nil)))
14534     (message "")
14535
14536     (if (member keys nosaves)
14537         (let (func)
14538           (pop-to-buffer gnus-summary-buffer 'norecord)
14539           (if (setq func (lookup-key (current-local-map) keys))
14540               (call-interactively func)
14541             (ding)))
14542       (let ((obuf (current-buffer))
14543             (owin (current-window-configuration))
14544             (opoint (point))
14545             func in-buffer)
14546         (if not-restore-window
14547             (pop-to-buffer gnus-summary-buffer 'norecord)
14548           (switch-to-buffer gnus-summary-buffer 'norecord))
14549         (setq in-buffer (current-buffer))
14550         (if (setq func (lookup-key (current-local-map) keys))
14551             (call-interactively func)
14552           (ding))
14553         (when (eq in-buffer (current-buffer))
14554           (set-buffer obuf)
14555           (unless not-restore-window
14556             (set-window-configuration owin))
14557           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14558
14559 \f
14560 ;;;
14561 ;;; Kill file handling.
14562 ;;;
14563
14564 ;;;###autoload
14565 (defalias 'gnus-batch-kill 'gnus-batch-score)
14566 ;;;###autoload
14567 (defun gnus-batch-score ()
14568   "Run batched scoring.
14569 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14570 Newsgroups is a list of strings in Bnews format.  If you want to score
14571 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14572 score the alt hierarchy, you'd say \"!alt.all\"."
14573   (interactive)
14574   (let* ((yes-and-no
14575           (gnus-newsrc-parse-options
14576            (apply (function concat)
14577                   (mapcar (lambda (g) (concat g " "))
14578                           command-line-args-left))))
14579          (gnus-expert-user t)
14580          (nnmail-spool-file nil)
14581          (gnus-use-dribble-file nil)
14582          (yes (car yes-and-no))
14583          (no (cdr yes-and-no))
14584          group newsrc entry
14585          ;; Disable verbose message.
14586          gnus-novice-user gnus-large-newsgroup)
14587     ;; Eat all arguments.
14588     (setq command-line-args-left nil)
14589     ;; Start Gnus.
14590     (gnus)
14591     ;; Apply kills to specified newsgroups in command line arguments.
14592     (setq newsrc (cdr gnus-newsrc-alist))
14593     (while newsrc
14594       (setq group (caar newsrc))
14595       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14596       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14597                (and (car entry)
14598                     (or (eq (car entry) t)
14599                         (not (zerop (car entry)))))
14600                (if yes (string-match yes group) t)
14601                (or (null no) (not (string-match no group))))
14602           (progn
14603             (gnus-summary-read-group group nil t nil t)
14604             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14605                  (gnus-summary-exit))))
14606       (setq newsrc (cdr newsrc)))
14607     ;; Exit Emacs.
14608     (switch-to-buffer gnus-group-buffer)
14609     (gnus-group-save-newsrc)))
14610
14611 (defun gnus-apply-kill-file ()
14612   "Apply a kill file to the current newsgroup.
14613 Returns the number of articles marked as read."
14614   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14615           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14616       (gnus-apply-kill-file-internal)
14617     0))
14618
14619 (defun gnus-kill-save-kill-buffer ()
14620   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14621     (when (get-file-buffer file)
14622       (save-excursion
14623         (set-buffer (get-file-buffer file))
14624         (and (buffer-modified-p) (save-buffer))
14625         (kill-buffer (current-buffer))))))
14626
14627 (defvar gnus-kill-file-name "KILL"
14628   "Suffix of the kill files.")
14629
14630 (defun gnus-newsgroup-kill-file (newsgroup)
14631   "Return the name of a kill file name for NEWSGROUP.
14632 If NEWSGROUP is nil, return the global kill file name instead."
14633   (cond 
14634    ;; The global KILL file is placed at top of the directory.
14635    ((or (null newsgroup)
14636         (string-equal newsgroup ""))
14637     (expand-file-name gnus-kill-file-name
14638                       (or gnus-kill-files-directory "~/News")))
14639    ;; Append ".KILL" to newsgroup name.
14640    ((gnus-use-long-file-name 'not-kill)
14641     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14642                               "." gnus-kill-file-name)
14643                       (or gnus-kill-files-directory "~/News")))
14644    ;; Place "KILL" under the hierarchical directory.
14645    (t
14646     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14647                               "/" gnus-kill-file-name)
14648                       (or gnus-kill-files-directory "~/News")))))
14649
14650 \f
14651 ;;;
14652 ;;; Dribble file
14653 ;;;
14654
14655 (defvar gnus-dribble-ignore nil)
14656 (defvar gnus-dribble-eval-file nil)
14657
14658 (defun gnus-dribble-file-name ()
14659   "Return the dribble file for the current .newsrc."
14660   (concat
14661    (if gnus-dribble-directory
14662        (concat (file-name-as-directory gnus-dribble-directory)
14663                (file-name-nondirectory gnus-current-startup-file))
14664      gnus-current-startup-file)
14665    "-dribble"))
14666
14667 (defun gnus-dribble-enter (string)
14668   "Enter STRING into the dribble buffer."
14669   (if (and (not gnus-dribble-ignore)
14670            gnus-dribble-buffer
14671            (buffer-name gnus-dribble-buffer))
14672       (let ((obuf (current-buffer)))
14673         (set-buffer gnus-dribble-buffer)
14674         (insert string "\n")
14675         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14676         (set-buffer obuf))))
14677
14678 (defun gnus-dribble-read-file ()
14679   "Read the dribble file from disk."
14680   (let ((dribble-file (gnus-dribble-file-name)))
14681     (save-excursion
14682       (set-buffer (setq gnus-dribble-buffer
14683                         (get-buffer-create
14684                          (file-name-nondirectory dribble-file))))
14685       (gnus-add-current-to-buffer-list)
14686       (erase-buffer)
14687       (setq buffer-file-name dribble-file)
14688       (auto-save-mode t)
14689       (buffer-disable-undo (current-buffer))
14690       (bury-buffer (current-buffer))
14691       (set-buffer-modified-p nil)
14692       (let ((auto (make-auto-save-file-name))
14693             (gnus-dribble-ignore t))
14694         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14695           ;; Load whichever file is newest -- the auto save file
14696           ;; or the "real" file.
14697           (if (file-newer-than-file-p auto dribble-file)
14698               (insert-file-contents auto)
14699             (insert-file-contents dribble-file))
14700           (unless (zerop (buffer-size))
14701             (set-buffer-modified-p t))
14702           ;; Set the file modes to reflect the .newsrc file modes.
14703           (save-buffer)
14704           (when (file-exists-p gnus-current-startup-file)
14705             (set-file-modes dribble-file
14706                             (file-modes gnus-current-startup-file)))
14707           ;; Possibly eval the file later.
14708           (when (gnus-y-or-n-p
14709                  "Auto-save file exists.  Do you want to read it? ")
14710             (setq gnus-dribble-eval-file t)))))))
14711
14712 (defun gnus-dribble-eval-file ()
14713   (if (not gnus-dribble-eval-file)
14714       ()
14715     (setq gnus-dribble-eval-file nil)
14716     (save-excursion
14717       (let ((gnus-dribble-ignore t))
14718         (set-buffer gnus-dribble-buffer)
14719         (eval-buffer (current-buffer))))))
14720
14721 (defun gnus-dribble-delete-file ()
14722   (if (file-exists-p (gnus-dribble-file-name))
14723       (delete-file (gnus-dribble-file-name)))
14724   (if gnus-dribble-buffer
14725       (save-excursion
14726         (set-buffer gnus-dribble-buffer)
14727         (let ((auto (make-auto-save-file-name)))
14728           (if (file-exists-p auto)
14729               (delete-file auto))
14730           (erase-buffer)
14731           (set-buffer-modified-p nil)))))
14732
14733 (defun gnus-dribble-save ()
14734   (if (and gnus-dribble-buffer
14735            (buffer-name gnus-dribble-buffer))
14736       (save-excursion
14737         (set-buffer gnus-dribble-buffer)
14738         (save-buffer))))
14739
14740 (defun gnus-dribble-clear ()
14741   (save-excursion
14742     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14743         (progn
14744           (set-buffer gnus-dribble-buffer)
14745           (erase-buffer)
14746           (set-buffer-modified-p nil)
14747           (setq buffer-saved-size (buffer-size))))))
14748
14749 \f
14750 ;;;
14751 ;;; Server Communication
14752 ;;;
14753
14754 (defun gnus-start-news-server (&optional confirm)
14755   "Open a method for getting news.
14756 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14757   (let (how)
14758     (if gnus-current-select-method
14759         ;; Stream is already opened.
14760         nil
14761       ;; Open NNTP server.
14762       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14763       (if confirm
14764           (progn
14765             ;; Read server name with completion.
14766             (setq gnus-nntp-server
14767                   (completing-read "NNTP server: "
14768                                    (mapcar (lambda (server) (list server))
14769                                            (cons (list gnus-nntp-server)
14770                                                  gnus-secondary-servers))
14771                                    nil nil gnus-nntp-server))))
14772
14773       (if (and gnus-nntp-server
14774                (stringp gnus-nntp-server)
14775                (not (string= gnus-nntp-server "")))
14776           (setq gnus-select-method
14777                 (cond ((or (string= gnus-nntp-server "")
14778                            (string= gnus-nntp-server "::"))
14779                        (list 'nnspool (system-name)))
14780                       ((string-match "^:" gnus-nntp-server)
14781                        (list 'nnmh gnus-nntp-server
14782                              (list 'nnmh-directory
14783                                    (file-name-as-directory
14784                                     (expand-file-name
14785                                      (concat "~/" (substring
14786                                                    gnus-nntp-server 1)))))
14787                              (list 'nnmh-get-new-mail nil)))
14788                       (t
14789                        (list 'nntp gnus-nntp-server)))))
14790
14791       (setq how (car gnus-select-method))
14792       (cond ((eq how 'nnspool)
14793              (require 'nnspool)
14794              (gnus-message 5 "Looking up local news spool..."))
14795             ((eq how 'nnmh)
14796              (require 'nnmh)
14797              (gnus-message 5 "Looking up mh spool..."))
14798             (t
14799              (require 'nntp)))
14800       (setq gnus-current-select-method gnus-select-method)
14801       (run-hooks 'gnus-open-server-hook)
14802       (or
14803        ;; gnus-open-server-hook might have opened it
14804        (gnus-server-opened gnus-select-method)
14805        (gnus-open-server gnus-select-method)
14806        (gnus-y-or-n-p
14807         (format
14808          "%s (%s) open error: '%s'.     Continue? "
14809          (car gnus-select-method) (cadr gnus-select-method)
14810          (gnus-status-message gnus-select-method)))
14811        (gnus-error 1 "Couldn't open server on %s"
14812                    (nth 1 gnus-select-method))))))
14813
14814 (defun gnus-check-group (group)
14815   "Try to make sure that the server where GROUP exists is alive."
14816   (let ((method (gnus-find-method-for-group group)))
14817     (or (gnus-server-opened method)
14818         (gnus-open-server method))))
14819
14820 (defun gnus-check-server (&optional method silent)
14821   "Check whether the connection to METHOD is down.
14822 If METHOD is nil, use `gnus-select-method'.
14823 If it is down, start it up (again)."
14824   (let ((method (or method gnus-select-method)))
14825     ;; Transform virtual server names into select methods.
14826     (when (stringp method)
14827       (setq method (gnus-server-to-method method)))
14828     (if (gnus-server-opened method)
14829         ;; The stream is already opened.
14830         t
14831       ;; Open the server.
14832       (unless silent
14833         (gnus-message 5 "Opening %s server%s..." (car method)
14834                       (if (equal (nth 1 method) "") ""
14835                         (format " on %s" (nth 1 method)))))
14836       (run-hooks 'gnus-open-server-hook)
14837       (prog1
14838           (gnus-open-server method)
14839         (unless silent
14840           (message ""))))))
14841
14842 (defun gnus-get-function (method function)
14843   "Return a function symbol based on METHOD and FUNCTION."
14844   ;; Translate server names into methods.
14845   (unless method
14846     (error "Attempted use of a nil select method"))
14847   (when (stringp method)
14848     (setq method (gnus-server-to-method method)))
14849   (let ((func (intern (format "%s-%s" (car method) function))))
14850     ;; If the functions isn't bound, we require the backend in
14851     ;; question.
14852     (unless (fboundp func)
14853       (require (car method))
14854       (unless (fboundp func)
14855         ;; This backend doesn't implement this function.
14856         (error "No such function: %s" func)))
14857     func))
14858
14859 \f
14860 ;;;
14861 ;;; Interface functions to the backends.
14862 ;;;
14863
14864 (defun gnus-open-server (method)
14865   "Open a connection to METHOD."
14866   (when (stringp method)
14867     (setq method (gnus-server-to-method method)))
14868   (let ((elem (assoc method gnus-opened-servers)))
14869     ;; If this method was previously denied, we just return nil.
14870     (if (eq (nth 1 elem) 'denied)
14871         (progn
14872           (gnus-message 1 "Denied server")
14873           nil)
14874       ;; Open the server.
14875       (let ((result
14876              (funcall (gnus-get-function method 'open-server)
14877                       (nth 1 method) (nthcdr 2 method))))
14878         ;; If this hasn't been opened before, we add it to the list.
14879         (unless elem
14880           (setq elem (list method nil)
14881                 gnus-opened-servers (cons elem gnus-opened-servers)))
14882         ;; Set the status of this server.
14883         (setcar (cdr elem) (if result 'ok 'denied))
14884         ;; Return the result from the "open" call.
14885         result))))
14886
14887 (defun gnus-close-server (method)
14888   "Close the connection to METHOD."
14889   (when (stringp method)
14890     (setq method (gnus-server-to-method method)))
14891   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14892
14893 (defun gnus-request-list (method)
14894   "Request the active file from METHOD."
14895   (when (stringp method)
14896     (setq method (gnus-server-to-method method)))
14897   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14898
14899 (defun gnus-request-list-newsgroups (method)
14900   "Request the newsgroups file from METHOD."
14901   (when (stringp method)
14902     (setq method (gnus-server-to-method method)))
14903   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14904
14905 (defun gnus-request-newgroups (date method)
14906   "Request all new groups since DATE from METHOD."
14907   (when (stringp method)
14908     (setq method (gnus-server-to-method method)))
14909   (funcall (gnus-get-function method 'request-newgroups)
14910            date (nth 1 method)))
14911
14912 (defun gnus-server-opened (method)
14913   "Check whether a connection to METHOD has been opened."
14914   (when (stringp method)
14915     (setq method (gnus-server-to-method method)))
14916   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14917
14918 (defun gnus-status-message (method)
14919   "Return the status message from METHOD.
14920 If METHOD is a string, it is interpreted as a group name.   The method
14921 this group uses will be queried."
14922   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14923                   method)))
14924     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14925
14926 (defun gnus-request-group (group &optional dont-check method)
14927   "Request GROUP.  If DONT-CHECK, no information is required."
14928   (let ((method (or method (gnus-find-method-for-group group))))
14929     (when (stringp method)
14930       (setq method (gnus-server-to-method method)))
14931     (funcall (gnus-get-function method 'request-group)
14932              (gnus-group-real-name group) (nth 1 method) dont-check)))
14933
14934 (defun gnus-request-asynchronous (group &optional articles)
14935   "Request that GROUP behave asynchronously.
14936 ARTICLES is the `data' of the group."
14937   (let ((method (gnus-find-method-for-group group)))
14938     (funcall (gnus-get-function method 'request-asynchronous)
14939              (gnus-group-real-name group) (nth 1 method) articles)))
14940
14941 (defun gnus-list-active-group (group)
14942   "Request active information on GROUP."
14943   (let ((method (gnus-find-method-for-group group))
14944         (func 'list-active-group))
14945     (when (gnus-check-backend-function func group)
14946       (funcall (gnus-get-function method func)
14947                (gnus-group-real-name group) (nth 1 method)))))
14948
14949 (defun gnus-request-group-description (group)
14950   "Request a description of GROUP."
14951   (let ((method (gnus-find-method-for-group group))
14952         (func 'request-group-description))
14953     (when (gnus-check-backend-function func group)
14954       (funcall (gnus-get-function method func)
14955                (gnus-group-real-name group) (nth 1 method)))))
14956
14957 (defun gnus-close-group (group)
14958   "Request the GROUP be closed."
14959   (let ((method (gnus-find-method-for-group group)))
14960     (funcall (gnus-get-function method 'close-group)
14961              (gnus-group-real-name group) (nth 1 method))))
14962
14963 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14964   "Request headers for ARTICLES in GROUP.
14965 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14966   (let ((method (gnus-find-method-for-group group)))
14967     (if (and gnus-use-cache (numberp (car articles)))
14968         (gnus-cache-retrieve-headers articles group fetch-old)
14969       (funcall (gnus-get-function method 'retrieve-headers)
14970                articles (gnus-group-real-name group) (nth 1 method)
14971                fetch-old))))
14972
14973 (defun gnus-retrieve-groups (groups method)
14974   "Request active information on GROUPS from METHOD."
14975   (when (stringp method)
14976     (setq method (gnus-server-to-method method)))
14977   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14978
14979 (defun gnus-request-type (group &optional article)
14980   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14981   (let ((method (gnus-find-method-for-group group)))
14982     (if (not (gnus-check-backend-function 'request-type (car method)))
14983         'unknown
14984       (funcall (gnus-get-function method 'request-type)
14985                (gnus-group-real-name group) article))))
14986
14987 (defun gnus-request-update-mark (group article mark)
14988   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14989   (let ((method (gnus-find-method-for-group group)))
14990     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14991         mark
14992       (funcall (gnus-get-function method 'request-update-mark)
14993                (gnus-group-real-name group) article mark))))
14994
14995 (defun gnus-request-article (article group &optional buffer)
14996   "Request the ARTICLE in GROUP.
14997 ARTICLE can either be an article number or an article Message-ID.
14998 If BUFFER, insert the article in that group."
14999   (let ((method (gnus-find-method-for-group group)))
15000     (funcall (gnus-get-function method 'request-article)
15001              article (gnus-group-real-name group) (nth 1 method) buffer)))
15002
15003 (defun gnus-request-head (article group)
15004   "Request the head of ARTICLE in GROUP."
15005   (let ((method (gnus-find-method-for-group group)))
15006     (funcall (gnus-get-function method 'request-head)
15007              article (gnus-group-real-name group) (nth 1 method))))
15008
15009 (defun gnus-request-body (article group)
15010   "Request the body of ARTICLE in GROUP."
15011   (let ((method (gnus-find-method-for-group group)))
15012     (funcall (gnus-get-function method 'request-body)
15013              article (gnus-group-real-name group) (nth 1 method))))
15014
15015 (defun gnus-request-post (method)
15016   "Post the current buffer using METHOD."
15017   (when (stringp method)
15018     (setq method (gnus-server-to-method method)))
15019   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
15020
15021 (defun gnus-request-scan (group method)
15022   "Request a SCAN being performed in GROUP from METHOD.
15023 If GROUP is nil, all groups on METHOD are scanned."
15024   (let ((method (if group (gnus-find-method-for-group group) method)))
15025     (funcall (gnus-get-function method 'request-scan)
15026              (and group (gnus-group-real-name group)) (nth 1 method))))
15027
15028 (defsubst gnus-request-update-info (info method)
15029   "Request that METHOD update INFO."
15030   (when (stringp method)
15031     (setq method (gnus-server-to-method method)))
15032   (when (gnus-check-backend-function 'request-update-info (car method))
15033     (funcall (gnus-get-function method 'request-update-info)
15034              (gnus-group-real-name (gnus-info-group info))
15035              info (nth 1 method))))
15036
15037 (defun gnus-request-expire-articles (articles group &optional force)
15038   (let ((method (gnus-find-method-for-group group)))
15039     (funcall (gnus-get-function method 'request-expire-articles)
15040              articles (gnus-group-real-name group) (nth 1 method)
15041              force)))
15042
15043 (defun gnus-request-move-article
15044   (article group server accept-function &optional last)
15045   (let ((method (gnus-find-method-for-group group)))
15046     (funcall (gnus-get-function method 'request-move-article)
15047              article (gnus-group-real-name group)
15048              (nth 1 method) accept-function last)))
15049
15050 (defun gnus-request-accept-article (group method &optional last)
15051   ;; Make sure there's a newline at the end of the article.
15052   (when (stringp method)
15053     (setq method (gnus-server-to-method method)))
15054   (when (and (not method)
15055              (stringp group))
15056     (setq method (gnus-find-method-for-group group)))
15057   (goto-char (point-max))
15058   (unless (bolp)
15059     (insert "\n"))
15060   (let ((func (car (or method (gnus-find-method-for-group group)))))
15061     (funcall (intern (format "%s-request-accept-article" func))
15062              (if (stringp group) (gnus-group-real-name group) group)
15063              (cadr method)
15064              last)))
15065
15066 (defun gnus-request-replace-article (article group buffer)
15067   (let ((func (car (gnus-find-method-for-group group))))
15068     (funcall (intern (format "%s-request-replace-article" func))
15069              article (gnus-group-real-name group) buffer)))
15070
15071 (defun gnus-request-associate-buffer (group)
15072   (let ((method (gnus-find-method-for-group group)))
15073     (funcall (gnus-get-function method 'request-associate-buffer)
15074              (gnus-group-real-name group))))
15075
15076 (defun gnus-request-restore-buffer (article group)
15077   "Request a new buffer restored to the state of ARTICLE."
15078   (let ((method (gnus-find-method-for-group group)))
15079     (funcall (gnus-get-function method 'request-restore-buffer)
15080              article (gnus-group-real-name group) (nth 1 method))))
15081
15082 (defun gnus-request-create-group (group &optional method)
15083   (when (stringp method)
15084     (setq method (gnus-server-to-method method)))
15085   (let ((method (or method (gnus-find-method-for-group group))))
15086     (funcall (gnus-get-function method 'request-create-group)
15087              (gnus-group-real-name group) (nth 1 method))))
15088
15089 (defun gnus-request-delete-group (group &optional force)
15090   (let ((method (gnus-find-method-for-group group)))
15091     (funcall (gnus-get-function method 'request-delete-group)
15092              (gnus-group-real-name group) force (nth 1 method))))
15093
15094 (defun gnus-request-rename-group (group new-name)
15095   (let ((method (gnus-find-method-for-group group)))
15096     (funcall (gnus-get-function method 'request-rename-group)
15097              (gnus-group-real-name group)
15098              (gnus-group-real-name new-name) (nth 1 method))))
15099
15100 (defun gnus-member-of-valid (symbol group)
15101   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15102   (memq symbol (assoc
15103                 (symbol-name (car (gnus-find-method-for-group group)))
15104                 gnus-valid-select-methods)))
15105
15106 (defun gnus-method-option-p (method option)
15107   "Return non-nil if select METHOD has OPTION as a parameter."
15108   (when (stringp method)
15109     (setq method (gnus-server-to-method method)))
15110   (memq option (assoc (format "%s" (car method))
15111                       gnus-valid-select-methods)))
15112
15113 (defun gnus-server-extend-method (group method)
15114   ;; This function "extends" a virtual server.  If the server is
15115   ;; "hello", and the select method is ("hello" (my-var "something"))
15116   ;; in the group "alt.alt", this will result in a new virtual server
15117   ;; called "hello+alt.alt".
15118   (let ((entry
15119          (gnus-copy-sequence
15120           (if (equal (car method) "native") gnus-select-method
15121             (cdr (assoc (car method) gnus-server-alist))))))
15122     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15123     (nconc entry (cdr method))))
15124
15125 (defun gnus-find-method-for-group (group &optional info)
15126   "Find the select method that GROUP uses."
15127   (or gnus-override-method
15128       (and (not group)
15129            gnus-select-method)
15130       (let ((info (or info (gnus-get-info group)))
15131             method)
15132         (if (or (not info)
15133                 (not (setq method (gnus-info-method info)))
15134                 (equal method "native"))
15135             gnus-select-method
15136           (setq method
15137                 (cond ((stringp method)
15138                        (gnus-server-to-method method))
15139                       ((stringp (car method))
15140                        (gnus-server-extend-method group method))
15141                       (t
15142                        method)))
15143           (cond ((equal (cadr method) "")
15144                  method)
15145                 ((null (cadr method))
15146                  (list (car method) ""))
15147                 (t
15148                  (gnus-server-add-address method)))))))
15149
15150 (defun gnus-check-backend-function (func group)
15151   "Check whether GROUP supports function FUNC."
15152   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15153                   group)))
15154     (fboundp (intern (format "%s-%s" method func)))))
15155
15156 (defun gnus-methods-using (feature)
15157   "Find all methods that have FEATURE."
15158   (let ((valids gnus-valid-select-methods)
15159         outs)
15160     (while valids
15161       (if (memq feature (car valids))
15162           (setq outs (cons (car valids) outs)))
15163       (setq valids (cdr valids)))
15164     outs))
15165
15166 \f
15167 ;;;
15168 ;;; Active & Newsrc File Handling
15169 ;;;
15170
15171 (defun gnus-setup-news (&optional rawfile level dont-connect)
15172   "Setup news information.
15173 If RAWFILE is non-nil, the .newsrc file will also be read.
15174 If LEVEL is non-nil, the news will be set up at level LEVEL."
15175   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15176
15177     (when init 
15178       ;; Clear some variables to re-initialize news information.
15179       (setq gnus-newsrc-alist nil
15180             gnus-active-hashtb nil)
15181       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15182       (gnus-read-newsrc-file rawfile))
15183
15184     (when (and (not (assoc "archive" gnus-server-alist))
15185                gnus-message-archive-method)
15186       (push (cons "archive" gnus-message-archive-method)
15187             gnus-server-alist))
15188
15189     ;; If we don't read the complete active file, we fill in the
15190     ;; hashtb here.
15191     (if (or (null gnus-read-active-file)
15192             (eq gnus-read-active-file 'some))
15193         (gnus-update-active-hashtb-from-killed))
15194
15195     ;; Read the active file and create `gnus-active-hashtb'.
15196     ;; If `gnus-read-active-file' is nil, then we just create an empty
15197     ;; hash table.  The partial filling out of the hash table will be
15198     ;; done in `gnus-get-unread-articles'.
15199     (and gnus-read-active-file
15200          (not level)
15201          (gnus-read-active-file))
15202
15203     (or gnus-active-hashtb
15204         (setq gnus-active-hashtb (make-vector 4095 0)))
15205
15206     ;; Initialize the cache.
15207     (when gnus-use-cache
15208       (gnus-cache-open))
15209
15210     ;; Possibly eval the dribble file.
15211     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15212
15213     (gnus-update-format-specifications)
15214
15215     ;; See whether we need to read the description file.
15216     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15217              (not gnus-description-hashtb)
15218              (not dont-connect)
15219              gnus-read-active-file)
15220         (gnus-read-all-descriptions-files))
15221
15222     ;; Find new newsgroups and treat them.
15223     (if (and init gnus-check-new-newsgroups (not level)
15224              (gnus-check-server gnus-select-method))
15225         (gnus-find-new-newsgroups))
15226
15227     ;; We might read in new NoCeM messages here.
15228     (when (and gnus-use-nocem 
15229                (not level)
15230                (not dont-connect))
15231       (gnus-nocem-scan-groups))
15232
15233     ;; Find the number of unread articles in each non-dead group.
15234     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15235       (gnus-get-unread-articles level))
15236
15237     (if (and init gnus-check-bogus-newsgroups
15238              gnus-read-active-file (not level)
15239              (gnus-server-opened gnus-select-method))
15240         (gnus-check-bogus-newsgroups))))
15241
15242 (defun gnus-find-new-newsgroups (&optional arg)
15243   "Search for new newsgroups and add them.
15244 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15245 The `-n' option line from .newsrc is respected.
15246 If ARG (the prefix), use the `ask-server' method to query
15247 the server for new groups."
15248   (interactive "P")
15249   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15250                        (null gnus-read-active-file)
15251                        (eq gnus-read-active-file 'some))
15252                    'ask-server gnus-check-new-newsgroups)))
15253     (unless (gnus-check-first-time-used)
15254       (if (or (consp check)
15255               (eq check 'ask-server))
15256           ;; Ask the server for new groups.
15257           (gnus-ask-server-for-new-groups)
15258         ;; Go through the active hashtb and look for new groups.
15259         (let ((groups 0)
15260               group new-newsgroups)
15261           (gnus-message 5 "Looking for new newsgroups...")
15262           (unless gnus-have-read-active-file
15263             (gnus-read-active-file))
15264           (setq gnus-newsrc-last-checked-date (current-time-string))
15265           (unless gnus-killed-hashtb
15266             (gnus-make-hashtable-from-killed))
15267           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15268           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15269           (mapatoms
15270            (lambda (sym)
15271              (if (or (null (setq group (symbol-name sym)))
15272                      (not (boundp sym))
15273                      (null (symbol-value sym))
15274                      (gnus-gethash group gnus-killed-hashtb)
15275                      (gnus-gethash group gnus-newsrc-hashtb))
15276                  ()
15277                (let ((do-sub (gnus-matches-options-n group)))
15278                  (cond
15279                   ((eq do-sub 'subscribe)
15280                    (setq groups (1+ groups))
15281                    (gnus-sethash group group gnus-killed-hashtb)
15282                    (funcall gnus-subscribe-options-newsgroup-method group))
15283                   ((eq do-sub 'ignore)
15284                    nil)
15285                   (t
15286                    (setq groups (1+ groups))
15287                    (gnus-sethash group group gnus-killed-hashtb)
15288                    (if gnus-subscribe-hierarchical-interactive
15289                        (setq new-newsgroups (cons group new-newsgroups))
15290                      (funcall gnus-subscribe-newsgroup-method group)))))))
15291            gnus-active-hashtb)
15292           (when new-newsgroups
15293             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15294           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15295           (if (> groups 0)
15296               (gnus-message 6 "%d new newsgroup%s arrived."
15297                             groups (if (> groups 1) "s have" " has"))
15298             (gnus-message 6 "No new newsgroups.")))))))
15299
15300 (defun gnus-matches-options-n (group)
15301   ;; Returns `subscribe' if the group is to be unconditionally
15302   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15303   ;; no match for the group.
15304
15305   ;; First we check the two user variables.
15306   (cond
15307    ((and gnus-options-subscribe
15308          (string-match gnus-options-subscribe group))
15309     'subscribe)
15310    ((and gnus-auto-subscribed-groups
15311          (string-match gnus-auto-subscribed-groups group))
15312     'subscribe)
15313    ((and gnus-options-not-subscribe
15314          (string-match gnus-options-not-subscribe group))
15315     'ignore)
15316    ;; Then we go through the list that was retrieved from the .newsrc
15317    ;; file.  This list has elements on the form
15318    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15319    ;; is in the reverse order of the options line) is returned.
15320    (t
15321     (let ((regs gnus-newsrc-options-n))
15322       (while (and regs
15323                   (not (string-match (caar regs) group)))
15324         (setq regs (cdr regs)))
15325       (and regs (cdar regs))))))
15326
15327 (defun gnus-ask-server-for-new-groups ()
15328   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15329          (methods (cons gnus-select-method
15330                         (nconc
15331                          (when gnus-message-archive-method
15332                            (list "archive"))
15333                          (append
15334                           (and (consp gnus-check-new-newsgroups)
15335                                gnus-check-new-newsgroups)
15336                           gnus-secondary-select-methods))))
15337          (groups 0)
15338          (new-date (current-time-string))
15339          group new-newsgroups got-new method hashtb
15340          gnus-override-subscribe-method)
15341     ;; Go through both primary and secondary select methods and
15342     ;; request new newsgroups.
15343     (while (setq method (gnus-server-get-method nil (pop methods)))
15344       (setq new-newsgroups nil)
15345       (setq gnus-override-subscribe-method method)
15346       (when (and (gnus-check-server method)
15347                  (gnus-request-newgroups date method))
15348         (save-excursion
15349           (setq got-new t)
15350           (setq hashtb (gnus-make-hashtable 100))
15351           (set-buffer nntp-server-buffer)
15352           ;; Enter all the new groups into a hashtable.
15353           (gnus-active-to-gnus-format method hashtb 'ignore))
15354         ;; Now all new groups from `method' are in `hashtb'.
15355         (mapatoms
15356          (lambda (group-sym)
15357            (if (or (null (setq group (symbol-name group-sym)))
15358                    (not (boundp group-sym))
15359                    (null (symbol-value group-sym))
15360                    (gnus-gethash group gnus-newsrc-hashtb)
15361                    (member group gnus-zombie-list)
15362                    (member group gnus-killed-list))
15363                ;; The group is already known.
15364                ()
15365              ;; Make this group active.
15366              (when (symbol-value group-sym)
15367                (gnus-set-active group (symbol-value group-sym)))
15368              ;; Check whether we want it or not.
15369              (let ((do-sub (gnus-matches-options-n group)))
15370                (cond
15371                 ((eq do-sub 'subscribe)
15372                  (incf groups)
15373                  (gnus-sethash group group gnus-killed-hashtb)
15374                  (funcall gnus-subscribe-options-newsgroup-method group))
15375                 ((eq do-sub 'ignore)
15376                  nil)
15377                 (t
15378                  (incf groups)
15379                  (gnus-sethash group group gnus-killed-hashtb)
15380                  (if gnus-subscribe-hierarchical-interactive
15381                      (push group new-newsgroups)
15382                    (funcall gnus-subscribe-newsgroup-method group)))))))
15383          hashtb))
15384       (when new-newsgroups
15385         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15386     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15387     (when (> groups 0)
15388       (gnus-message 6 "%d new newsgroup%s arrived."
15389                     groups (if (> groups 1) "s have" " has")))
15390     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15391     got-new))
15392
15393 (defun gnus-check-first-time-used ()
15394   (if (or (> (length gnus-newsrc-alist) 1)
15395           (file-exists-p gnus-startup-file)
15396           (file-exists-p (concat gnus-startup-file ".el"))
15397           (file-exists-p (concat gnus-startup-file ".eld")))
15398       nil
15399     (gnus-message 6 "First time user; subscribing you to default groups")
15400     (unless (gnus-read-active-file-p)
15401       (gnus-read-active-file))
15402     (setq gnus-newsrc-last-checked-date (current-time-string))
15403     (let ((groups gnus-default-subscribed-newsgroups)
15404           group)
15405       (if (eq groups t)
15406           nil
15407         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15408         (mapatoms
15409          (lambda (sym)
15410            (if (null (setq group (symbol-name sym)))
15411                ()
15412              (let ((do-sub (gnus-matches-options-n group)))
15413                (cond
15414                 ((eq do-sub 'subscribe)
15415                  (gnus-sethash group group gnus-killed-hashtb)
15416                  (funcall gnus-subscribe-options-newsgroup-method group))
15417                 ((eq do-sub 'ignore)
15418                  nil)
15419                 (t
15420                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15421          gnus-active-hashtb)
15422         (while groups
15423           (if (gnus-active (car groups))
15424               (gnus-group-change-level
15425                (car groups) gnus-level-default-subscribed gnus-level-killed))
15426           (setq groups (cdr groups)))
15427         (gnus-group-make-help-group)
15428         (and gnus-novice-user
15429              (gnus-message 7 "`A k' to list killed groups"))))))
15430
15431 (defun gnus-subscribe-group (group previous &optional method)
15432   (gnus-group-change-level
15433    (if method
15434        (list t group gnus-level-default-subscribed nil nil method)
15435      group)
15436    gnus-level-default-subscribed gnus-level-killed previous t))
15437
15438 ;; `gnus-group-change-level' is the fundamental function for changing
15439 ;; subscription levels of newsgroups.  This might mean just changing
15440 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15441 ;; again, which subscribes/unsubscribes a group, which is equally
15442 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15443 ;; from 8-9 to 1-7 means that you remove the group from the list of
15444 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15445 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15446 ;; which is trivial.
15447 ;; ENTRY can either be a string (newsgroup name) or a list (if
15448 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15449 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15450 ;; entries.
15451 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15452 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15453 ;; after.
15454 (defun gnus-group-change-level (entry level &optional oldlevel
15455                                       previous fromkilled)
15456   (let (group info active num)
15457     ;; Glean what info we can from the arguments
15458     (if (consp entry)
15459         (if fromkilled (setq group (nth 1 entry))
15460           (setq group (car (nth 2 entry))))
15461       (setq group entry))
15462     (if (and (stringp entry)
15463              oldlevel
15464              (< oldlevel gnus-level-zombie))
15465         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15466     (if (and (not oldlevel)
15467              (consp entry))
15468         (setq oldlevel (gnus-info-level (nth 2 entry)))
15469       (setq oldlevel (or oldlevel 9)))
15470     (if (stringp previous)
15471         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15472
15473     (if (and (>= oldlevel gnus-level-zombie)
15474              (gnus-gethash group gnus-newsrc-hashtb))
15475         ;; We are trying to subscribe a group that is already
15476         ;; subscribed.
15477         ()                              ; Do nothing.
15478
15479       (or (gnus-ephemeral-group-p group)
15480           (gnus-dribble-enter
15481            (format "(gnus-group-change-level %S %S %S %S %S)"
15482                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15483
15484       ;; Then we remove the newgroup from any old structures, if needed.
15485       ;; If the group was killed, we remove it from the killed or zombie
15486       ;; list.  If not, and it is in fact going to be killed, we remove
15487       ;; it from the newsrc hash table and assoc.
15488       (cond
15489        ((>= oldlevel gnus-level-zombie)
15490         (if (= oldlevel gnus-level-zombie)
15491             (setq gnus-zombie-list (delete group gnus-zombie-list))
15492           (setq gnus-killed-list (delete group gnus-killed-list))))
15493        (t
15494         (if (and (>= level gnus-level-zombie)
15495                  entry)
15496             (progn
15497               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15498               (if (nth 3 entry)
15499                   (setcdr (gnus-gethash (car (nth 3 entry))
15500                                         gnus-newsrc-hashtb)
15501                           (cdr entry)))
15502               (setcdr (cdr entry) (cdddr entry))))))
15503
15504       ;; Finally we enter (if needed) the list where it is supposed to
15505       ;; go, and change the subscription level.  If it is to be killed,
15506       ;; we enter it into the killed or zombie list.
15507       (cond 
15508        ((>= level gnus-level-zombie)
15509         ;; Remove from the hash table.
15510         (gnus-sethash group nil gnus-newsrc-hashtb)
15511         ;; We do not enter foreign groups into the list of dead
15512         ;; groups.
15513         (unless (gnus-group-foreign-p group)
15514           (if (= level gnus-level-zombie)
15515               (setq gnus-zombie-list (cons group gnus-zombie-list))
15516             (setq gnus-killed-list (cons group gnus-killed-list)))))
15517        (t
15518         ;; If the list is to be entered into the newsrc assoc, and
15519         ;; it was killed, we have to create an entry in the newsrc
15520         ;; hashtb format and fix the pointers in the newsrc assoc.
15521         (if (< oldlevel gnus-level-zombie)
15522             ;; It was alive, and it is going to stay alive, so we
15523             ;; just change the level and don't change any pointers or
15524             ;; hash table entries.
15525             (setcar (cdaddr entry) level)
15526           (if (listp entry)
15527               (setq info (cdr entry)
15528                     num (car entry))
15529             (setq active (gnus-active group))
15530             (setq num
15531                   (if active (- (1+ (cdr active)) (car active)) t))
15532             ;; Check whether the group is foreign.  If so, the
15533             ;; foreign select method has to be entered into the
15534             ;; info.
15535             (let ((method (or gnus-override-subscribe-method
15536                               (gnus-group-method group))))
15537               (if (eq method gnus-select-method)
15538                   (setq info (list group level nil))
15539                 (setq info (list group level nil nil method)))))
15540           (unless previous
15541             (setq previous
15542                   (let ((p gnus-newsrc-alist))
15543                     (while (cddr p)
15544                       (setq p (cdr p)))
15545                     p)))
15546           (setq entry (cons info (cddr previous)))
15547           (if (cdr previous)
15548               (progn
15549                 (setcdr (cdr previous) entry)
15550                 (gnus-sethash group (cons num (cdr previous))
15551                               gnus-newsrc-hashtb))
15552             (setcdr previous entry)
15553             (gnus-sethash group (cons num previous)
15554                           gnus-newsrc-hashtb))
15555           (when (cdr entry)
15556             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15557       (when gnus-group-change-level-function
15558         (funcall gnus-group-change-level-function group level oldlevel)))))
15559
15560 (defun gnus-kill-newsgroup (newsgroup)
15561   "Obsolete function.  Kills a newsgroup."
15562   (gnus-group-change-level
15563    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15564
15565 (defun gnus-check-bogus-newsgroups (&optional confirm)
15566   "Remove bogus newsgroups.
15567 If CONFIRM is non-nil, the user has to confirm the deletion of every
15568 newsgroup."
15569   (let ((newsrc (cdr gnus-newsrc-alist))
15570         bogus group entry info)
15571     (gnus-message 5 "Checking bogus newsgroups...")
15572     (unless (gnus-read-active-file-p)
15573       (gnus-read-active-file))
15574     (when (gnus-read-active-file-p)
15575       ;; Find all bogus newsgroup that are subscribed.
15576       (while newsrc
15577         (setq info (pop newsrc)
15578               group (gnus-info-group info))
15579         (unless (or (gnus-active group) ; Active
15580                     (gnus-info-method info) ; Foreign
15581                     (and confirm
15582                          (not (gnus-y-or-n-p
15583                                (format "Remove bogus newsgroup: %s " group)))))
15584           ;; Found a bogus newsgroup.
15585           (push group bogus)))
15586       ;; Remove all bogus subscribed groups by first killing them, and
15587       ;; then removing them from the list of killed groups.
15588       (while bogus
15589         (when (setq entry (gnus-gethash (setq group (pop bogus))
15590                                         gnus-newsrc-hashtb))
15591           (gnus-group-change-level entry gnus-level-killed)
15592           (setq gnus-killed-list (delete group gnus-killed-list))))
15593       ;; Then we remove all bogus groups from the list of killed and
15594       ;; zombie groups.  They are are removed without confirmation.
15595       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15596             killed)
15597         (while dead-lists
15598           (setq killed (symbol-value (car dead-lists)))
15599           (while killed
15600             (unless (gnus-active (setq group (pop killed)))
15601               ;; The group is bogus.
15602               ;; !!!Slow as hell.
15603               (set (car dead-lists)
15604                    (delete group (symbol-value (car dead-lists))))))
15605           (setq dead-lists (cdr dead-lists))))
15606       (gnus-message 5 "Checking bogus newsgroups...done"))))
15607
15608 (defun gnus-check-duplicate-killed-groups ()
15609   "Remove duplicates from the list of killed groups."
15610   (interactive)
15611   (let ((killed gnus-killed-list))
15612     (while killed
15613       (gnus-message 9 "%d" (length killed))
15614       (setcdr killed (delete (car killed) (cdr killed)))
15615       (setq killed (cdr killed)))))
15616
15617 ;; We want to inline a function from gnus-cache, so we cheat here:
15618 (eval-when-compile
15619   (provide 'gnus)
15620   (require 'gnus-cache))
15621
15622 (defun gnus-get-unread-articles-in-group (info active &optional update)
15623   (when active
15624     ;; Allow the backend to update the info in the group.
15625     (when (and update 
15626                (gnus-request-update-info
15627                 info (gnus-find-method-for-group (gnus-info-group info))))
15628       (gnus-activate-group (gnus-info-group info) nil t))
15629     (let* ((range (gnus-info-read info))
15630            (num 0))
15631       ;; If a cache is present, we may have to alter the active info.
15632       (when (and gnus-use-cache info)
15633         (inline (gnus-cache-possibly-alter-active 
15634                  (gnus-info-group info) active)))
15635       ;; Modify the list of read articles according to what articles
15636       ;; are available; then tally the unread articles and add the
15637       ;; number to the group hash table entry.
15638       (cond
15639        ((zerop (cdr active))
15640         (setq num 0))
15641        ((not range)
15642         (setq num (- (1+ (cdr active)) (car active))))
15643        ((not (listp (cdr range)))
15644         ;; Fix a single (num . num) range according to the
15645         ;; active hash table.
15646         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15647         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15648         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15649         ;; Compute number of unread articles.
15650         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15651        (t
15652         ;; The read list is a list of ranges.  Fix them according to
15653         ;; the active hash table.
15654         ;; First peel off any elements that are below the lower
15655         ;; active limit.
15656         (while (and (cdr range)
15657                     (>= (car active)
15658                         (or (and (atom (cadr range)) (cadr range))
15659                             (caadr range))))
15660           (if (numberp (car range))
15661               (setcar range
15662                       (cons (car range)
15663                             (or (and (numberp (cadr range))
15664                                      (cadr range))
15665                                 (cdadr range))))
15666             (setcdr (car range)
15667                     (or (and (numberp (nth 1 range)) (nth 1 range))
15668                         (cdadr range))))
15669           (setcdr range (cddr range)))
15670         ;; Adjust the first element to be the same as the lower limit.
15671         (if (and (not (atom (car range)))
15672                  (< (cdar range) (car active)))
15673             (setcdr (car range) (1- (car active))))
15674         ;; Then we want to peel off any elements that are higher
15675         ;; than the upper active limit.
15676         (let ((srange range))
15677           ;; Go past all legal elements.
15678           (while (and (cdr srange)
15679                       (<= (or (and (atom (cadr srange))
15680                                    (cadr srange))
15681                               (caadr srange)) (cdr active)))
15682             (setq srange (cdr srange)))
15683           (if (cdr srange)
15684               ;; Nuke all remaining illegal elements.
15685               (setcdr srange nil))
15686
15687           ;; Adjust the final element.
15688           (if (and (not (atom (car srange)))
15689                    (> (cdar srange) (cdr active)))
15690               (setcdr (car srange) (cdr active))))
15691         ;; Compute the number of unread articles.
15692         (while range
15693           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15694                                       (cdar range)))
15695                               (or (and (atom (car range)) (car range))
15696                                   (caar range)))))
15697           (setq range (cdr range)))
15698         (setq num (max 0 (- (cdr active) num)))))
15699       ;; Set the number of unread articles.
15700       (when info
15701         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15702       num)))
15703
15704 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15705 ;; and compute how many unread articles there are in each group.
15706 (defun gnus-get-unread-articles (&optional level)
15707   (let* ((newsrc (cdr gnus-newsrc-alist))
15708          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15709          (foreign-level
15710           (min
15711            (cond ((and gnus-activate-foreign-newsgroups
15712                        (not (numberp gnus-activate-foreign-newsgroups)))
15713                   (1+ gnus-level-subscribed))
15714                  ((numberp gnus-activate-foreign-newsgroups)
15715                   gnus-activate-foreign-newsgroups)
15716                  (t 0))
15717            level))
15718          info group active method)
15719     (gnus-message 5 "Checking new news...")
15720
15721     (while newsrc
15722       (setq active (gnus-active (setq group (gnus-info-group
15723                                              (setq info (pop newsrc))))))
15724
15725       ;; Check newsgroups.  If the user doesn't want to check them, or
15726       ;; they can't be checked (for instance, if the news server can't
15727       ;; be reached) we just set the number of unread articles in this
15728       ;; newsgroup to t.  This means that Gnus thinks that there are
15729       ;; unread articles, but it has no idea how many.
15730       (if (and (setq method (gnus-info-method info))
15731                (not (gnus-server-equal
15732                      gnus-select-method
15733                      (setq method (gnus-server-get-method nil method))))
15734                (not (gnus-secondary-method-p method)))
15735           ;; These groups are foreign.  Check the level.
15736           (when (<= (gnus-info-level info) foreign-level)
15737             (setq active (gnus-activate-group group 'scan))
15738             (unless (inline (gnus-virtual-group-p group))
15739               (inline (gnus-close-group group))))
15740
15741         ;; These groups are native or secondary.
15742         (when (and (<= (gnus-info-level info) level)
15743                    (not gnus-read-active-file))
15744           (setq active (gnus-activate-group group 'scan))
15745           (inline (gnus-close-group group))))
15746
15747       (if active
15748           (inline (gnus-get-unread-articles-in-group 
15749                    info active
15750                    (and method
15751                         (fboundp (intern (concat (symbol-name (car method))
15752                                                  "-request-update-info"))))))
15753         ;; The group couldn't be reached, so we nix out the number of
15754         ;; unread articles and stuff.
15755         (gnus-set-active group nil)
15756         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15757
15758     (gnus-message 5 "Checking new news...done")))
15759
15760 ;; Create a hash table out of the newsrc alist.  The `car's of the
15761 ;; alist elements are used as keys.
15762 (defun gnus-make-hashtable-from-newsrc-alist ()
15763   (let ((alist gnus-newsrc-alist)
15764         (ohashtb gnus-newsrc-hashtb)
15765         prev)
15766     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15767     (setq alist
15768           (setq prev (setq gnus-newsrc-alist
15769                            (if (equal (caar gnus-newsrc-alist)
15770                                       "dummy.group")
15771                                gnus-newsrc-alist
15772                              (cons (list "dummy.group" 0 nil) alist)))))
15773     (while alist
15774       (gnus-sethash
15775        (caar alist)
15776        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15777              prev)
15778        gnus-newsrc-hashtb)
15779       (setq prev alist
15780             alist (cdr alist)))))
15781
15782 (defun gnus-make-hashtable-from-killed ()
15783   "Create a hash table from the killed and zombie lists."
15784   (let ((lists '(gnus-killed-list gnus-zombie-list))
15785         list)
15786     (setq gnus-killed-hashtb
15787           (gnus-make-hashtable
15788            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15789     (while (setq list (pop lists))
15790       (setq list (symbol-value list))
15791       (while list
15792         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15793
15794 (defun gnus-activate-group (group &optional scan dont-check)
15795   ;; Check whether a group has been activated or not.
15796   ;; If SCAN, request a scan of that group as well.
15797   (let ((method (gnus-find-method-for-group group))
15798         active)
15799     (and (gnus-check-server method)
15800          ;; We escape all bugs and quit here to make it possible to
15801          ;; continue if a group is so out-there that it reports bugs
15802          ;; and stuff.
15803          (progn
15804            (and scan
15805                 (gnus-check-backend-function 'request-scan (car method))
15806                 (gnus-request-scan group method))
15807            t)
15808          (condition-case ()
15809              (gnus-request-group group dont-check)
15810         ;   (error nil)
15811            (quit nil))
15812          (save-excursion
15813            (set-buffer nntp-server-buffer)
15814            (goto-char (point-min))
15815            ;; Parse the result we got from `gnus-request-group'.
15816            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15817                 (progn
15818                   (goto-char (match-beginning 1))
15819                   (gnus-set-active
15820                    group (setq active (cons (read (current-buffer))
15821                                             (read (current-buffer)))))
15822                   ;; Return the new active info.
15823                   active))))))
15824
15825 (defun gnus-update-read-articles (group unread)
15826   "Update the list of read and ticked articles in GROUP using the
15827 UNREAD and TICKED lists.
15828 Note: UNSELECTED has to be sorted over `<'.
15829 Returns whether the updating was successful."
15830   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15831          (entry (gnus-gethash group gnus-newsrc-hashtb))
15832          (info (nth 2 entry))
15833          (prev 1)
15834          (unread (sort (copy-sequence unread) '<))
15835          read)
15836     (if (or (not info) (not active))
15837         ;; There is no info on this group if it was, in fact,
15838         ;; killed.  Gnus stores no information on killed groups, so
15839         ;; there's nothing to be done.
15840         ;; One could store the information somewhere temporarily,
15841         ;; perhaps...  Hmmm...
15842         ()
15843       ;; Remove any negative articles numbers.
15844       (while (and unread (< (car unread) 0))
15845         (setq unread (cdr unread)))
15846       ;; Remove any expired article numbers
15847       (while (and unread (< (car unread) (car active)))
15848         (setq unread (cdr unread)))
15849       ;; Compute the ranges of read articles by looking at the list of
15850       ;; unread articles.
15851       (while unread
15852         (if (/= (car unread) prev)
15853             (setq read (cons (if (= prev (1- (car unread))) prev
15854                                (cons prev (1- (car unread)))) read)))
15855         (setq prev (1+ (car unread)))
15856         (setq unread (cdr unread)))
15857       (when (<= prev (cdr active))
15858         (setq read (cons (cons prev (cdr active)) read)))
15859       ;; Enter this list into the group info.
15860       (gnus-info-set-read
15861        info (if (> (length read) 1) (nreverse read) read))
15862       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15863       (gnus-get-unread-articles-in-group info (gnus-active group))
15864       t)))
15865
15866 (defun gnus-make-articles-unread (group articles)
15867   "Mark ARTICLES in GROUP as unread."
15868   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15869                           (gnus-gethash (gnus-group-real-name group)
15870                                         gnus-newsrc-hashtb))))
15871          (ranges (gnus-info-read info))
15872          news article)
15873     (while articles
15874       (when (gnus-member-of-range
15875              (setq article (pop articles)) ranges)
15876         (setq news (cons article news))))
15877     (when news
15878       (gnus-info-set-read
15879        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15880       (gnus-group-update-group group t))))
15881
15882 ;; Enter all dead groups into the hashtb.
15883 (defun gnus-update-active-hashtb-from-killed ()
15884   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15885         (lists (list gnus-killed-list gnus-zombie-list))
15886         killed)
15887     (while lists
15888       (setq killed (car lists))
15889       (while killed
15890         (gnus-sethash (car killed) nil hashtb)
15891         (setq killed (cdr killed)))
15892       (setq lists (cdr lists)))))
15893
15894 (defun gnus-get-killed-groups ()
15895   "Go through the active hashtb and all all unknown groups as killed."
15896   ;; First make sure active file has been read.
15897   (unless (gnus-read-active-file-p)
15898     (let ((gnus-read-active-file t))
15899       (gnus-read-active-file)))
15900   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
15901   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
15902   (mapatoms
15903    (lambda (sym)
15904      (let ((groups 0)
15905            (group (symbol-name sym)))
15906        (if (or (null group)
15907                (gnus-gethash group gnus-killed-hashtb)
15908                (gnus-gethash group gnus-newsrc-hashtb))
15909            ()
15910          (let ((do-sub (gnus-matches-options-n group)))
15911            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
15912                ()
15913              (setq groups (1+ groups))
15914              (setq gnus-killed-list
15915                    (cons group gnus-killed-list))
15916              (gnus-sethash group group gnus-killed-hashtb))))))
15917    gnus-active-hashtb))
15918
15919 ;; Get the active file(s) from the backend(s).
15920 (defun gnus-read-active-file ()
15921   (gnus-group-set-mode-line)
15922   (let ((methods 
15923          (append
15924           (if (gnus-check-server gnus-select-method)
15925               ;; The native server is available.
15926               (cons gnus-select-method gnus-secondary-select-methods)
15927             ;; The native server is down, so we just do the
15928             ;; secondary ones.
15929             gnus-secondary-select-methods)
15930           ;; Also read from the archive server.
15931           (when gnus-message-archive-method
15932             (list "archive"))))
15933         list-type)
15934     (setq gnus-have-read-active-file nil)
15935     (save-excursion
15936       (set-buffer nntp-server-buffer)
15937       (while methods
15938         (let* ((method (if (stringp (car methods))
15939                            (gnus-server-get-method nil (car methods))
15940                          (car methods)))
15941                (where (nth 1 method))
15942                (mesg (format "Reading active file%s via %s..."
15943                              (if (and where (not (zerop (length where))))
15944                                  (concat " from " where) "")
15945                              (car method))))
15946           (gnus-message 5 mesg)
15947           (when (gnus-check-server method)
15948             ;; Request that the backend scan its incoming messages.
15949             (and (gnus-check-backend-function 'request-scan (car method))
15950                  (gnus-request-scan nil method))
15951             (cond
15952              ((and (eq gnus-read-active-file 'some)
15953                    (gnus-check-backend-function 'retrieve-groups (car method)))
15954               (let ((newsrc (cdr gnus-newsrc-alist))
15955                     (gmethod (gnus-server-get-method nil method))
15956                     groups info)
15957                 (while (setq info (pop newsrc))
15958                   (when (gnus-server-equal
15959                          (gnus-find-method-for-group 
15960                           (gnus-info-group info) info)
15961                          gmethod)
15962                     (push (gnus-group-real-name (gnus-info-group info)) 
15963                           groups)))
15964                 (when groups
15965                   (gnus-check-server method)
15966                   (setq list-type (gnus-retrieve-groups groups method))
15967                   (cond
15968                    ((not list-type)
15969                     (gnus-error
15970                      1.2 "Cannot read partial active file from %s server."
15971                      (car method)))
15972                    ((eq list-type 'active)
15973                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15974                    (t
15975                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15976              (t
15977               (if (not (gnus-request-list method))
15978                   (unless (equal method gnus-message-archive-method)
15979                     (gnus-error 1 "Cannot read active file from %s server."
15980                                 (car method)))
15981                 (gnus-active-to-gnus-format method gnus-active-hashtb)
15982                 ;; We mark this active file as read.
15983                 (push method gnus-have-read-active-file)
15984                 (gnus-message 5 "%sdone" mesg))))))
15985         (setq methods (cdr methods))))))
15986
15987 ;; Read an active file and place the results in `gnus-active-hashtb'.
15988 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15989   (unless method
15990     (setq method gnus-select-method))
15991   (let ((cur (current-buffer))
15992         (hashtb (or hashtb
15993                     (if (and gnus-active-hashtb
15994                              (not (equal method gnus-select-method)))
15995                         gnus-active-hashtb
15996                       (setq gnus-active-hashtb
15997                             (if (equal method gnus-select-method)
15998                                 (gnus-make-hashtable
15999                                  (count-lines (point-min) (point-max)))
16000                               (gnus-make-hashtable 4096)))))))
16001     ;; Delete unnecessary lines.
16002     (goto-char (point-min))
16003     (while (search-forward "\nto." nil t)
16004       (delete-region (1+ (match-beginning 0))
16005                      (progn (forward-line 1) (point))))
16006     (or (string= gnus-ignored-newsgroups "")
16007         (progn
16008           (goto-char (point-min))
16009           (delete-matching-lines gnus-ignored-newsgroups)))
16010     ;; Make the group names readable as a lisp expression even if they
16011     ;; contain special characters.
16012     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
16013     (goto-char (point-max))
16014     (while (re-search-backward "[][';?()#]" nil t)
16015       (insert ?\\))
16016     ;; If these are groups from a foreign select method, we insert the
16017     ;; group prefix in front of the group names.
16018     (and method (not (gnus-server-equal
16019                       (gnus-server-get-method nil method)
16020                       (gnus-server-get-method nil gnus-select-method)))
16021          (let ((prefix (gnus-group-prefixed-name "" method)))
16022            (goto-char (point-min))
16023            (while (and (not (eobp))
16024                        (progn (insert prefix)
16025                               (zerop (forward-line 1)))))))
16026     ;; Store the active file in a hash table.
16027     (goto-char (point-min))
16028     (if (string-match "%[oO]" gnus-group-line-format)
16029         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16030         ;; If we want information on moderated groups, we use this
16031         ;; loop...
16032         (let* ((mod-hashtb (make-vector 7 0))
16033                (m (intern "m" mod-hashtb))
16034                group max min)
16035           (while (not (eobp))
16036             (condition-case nil
16037                 (progn
16038                   (narrow-to-region (point) (gnus-point-at-eol))
16039                   (setq group (let ((obarray hashtb)) (read cur)))
16040                   (if (and (numberp (setq max (read cur)))
16041                            (numberp (setq min (read cur)))
16042                            (progn
16043                              (skip-chars-forward " \t")
16044                              (not
16045                               (or (= (following-char) ?=)
16046                                   (= (following-char) ?x)
16047                                   (= (following-char) ?j)))))
16048                       (set group (cons min max))
16049                     (set group nil))
16050                   ;; Enter moderated groups into a list.
16051                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16052                       (setq gnus-moderated-list
16053                             (cons (symbol-name group) gnus-moderated-list))))
16054               (error
16055                (and group
16056                     (symbolp group)
16057                     (set group nil))))
16058             (widen)
16059             (forward-line 1)))
16060       ;; And if we do not care about moderation, we use this loop,
16061       ;; which is faster.
16062       (let (group max min)
16063         (while (not (eobp))
16064           (condition-case ()
16065               (progn
16066                 (narrow-to-region (point) (gnus-point-at-eol))
16067                 ;; group gets set to a symbol interned in the hash table
16068                 ;; (what a hack!!) - jwz
16069                 (setq group (let ((obarray hashtb)) (read cur)))
16070                 (if (and (numberp (setq max (read cur)))
16071                          (numberp (setq min (read cur)))
16072                          (progn
16073                            (skip-chars-forward " \t")
16074                            (not
16075                             (or (= (following-char) ?=)
16076                                 (= (following-char) ?x)
16077                                 (= (following-char) ?j)))))
16078                     (set group (cons min max))
16079                   (set group nil)))
16080             (error
16081              (progn
16082                (and group
16083                     (symbolp group)
16084                     (set group nil))
16085                (or ignore-errors
16086                    (gnus-message 3 "Warning - illegal active: %s"
16087                                  (buffer-substring
16088                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16089           (widen)
16090           (forward-line 1))))))
16091
16092 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16093   ;; Parse a "groups" active file.
16094   (let ((cur (current-buffer))
16095         (hashtb (or hashtb
16096                     (if (and method gnus-active-hashtb)
16097                         gnus-active-hashtb
16098                       (setq gnus-active-hashtb
16099                             (gnus-make-hashtable
16100                              (count-lines (point-min) (point-max)))))))
16101         (prefix (and method
16102                      (not (gnus-server-equal
16103                            (gnus-server-get-method nil method)
16104                            (gnus-server-get-method nil gnus-select-method)))
16105                      (gnus-group-prefixed-name "" method))))
16106
16107     (goto-char (point-min))
16108     ;; We split this into to separate loops, one with the prefix
16109     ;; and one without to speed the reading up somewhat.
16110     (if prefix
16111         (let (min max opoint group)
16112           (while (not (eobp))
16113             (condition-case ()
16114                 (progn
16115                   (read cur) (read cur)
16116                   (setq min (read cur)
16117                         max (read cur)
16118                         opoint (point))
16119                   (skip-chars-forward " \t")
16120                   (insert prefix)
16121                   (goto-char opoint)
16122                   (set (let ((obarray hashtb)) (read cur))
16123                        (cons min max)))
16124               (error (and group (symbolp group) (set group nil))))
16125             (forward-line 1)))
16126       (let (min max group)
16127         (while (not (eobp))
16128           (condition-case ()
16129               (if (= (following-char) ?2)
16130                   (progn
16131                     (read cur) (read cur)
16132                     (setq min (read cur)
16133                           max (read cur))
16134                     (set (setq group (let ((obarray hashtb)) (read cur)))
16135                          (cons min max))))
16136             (error (and group (symbolp group) (set group nil))))
16137           (forward-line 1))))))
16138
16139 (defun gnus-read-newsrc-file (&optional force)
16140   "Read startup file.
16141 If FORCE is non-nil, the .newsrc file is read."
16142   ;; Reset variables that might be defined in the .newsrc.eld file.
16143   (let ((variables gnus-variable-list))
16144     (while variables
16145       (set (car variables) nil)
16146       (setq variables (cdr variables))))
16147   (let* ((newsrc-file gnus-current-startup-file)
16148          (quick-file (concat newsrc-file ".el")))
16149     (save-excursion
16150       ;; We always load the .newsrc.eld file.  If always contains
16151       ;; much information that can not be gotten from the .newsrc
16152       ;; file (ticked articles, killed groups, foreign methods, etc.)
16153       (gnus-read-newsrc-el-file quick-file)
16154
16155       (if (and (file-exists-p gnus-current-startup-file)
16156                (or force
16157                    (and (file-newer-than-file-p newsrc-file quick-file)
16158                         (file-newer-than-file-p newsrc-file
16159                                                 (concat quick-file "d")))
16160                    (not gnus-newsrc-alist)))
16161           ;; We read the .newsrc file.  Note that if there if a
16162           ;; .newsrc.eld file exists, it has already been read, and
16163           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16164           ;; the .newsrc file, Gnus will only use the information it
16165           ;; can find there for changing the data already read -
16166           ;; ie. reading the .newsrc file will not trash the data
16167           ;; already read (except for read articles).
16168           (save-excursion
16169             (gnus-message 5 "Reading %s..." newsrc-file)
16170             (set-buffer (find-file-noselect newsrc-file))
16171             (buffer-disable-undo (current-buffer))
16172             (gnus-newsrc-to-gnus-format)
16173             (kill-buffer (current-buffer))
16174             (gnus-message 5 "Reading %s...done" newsrc-file)))
16175
16176       ;; Read any slave files.
16177       (unless gnus-slave
16178         (gnus-master-read-slave-newsrc))
16179       
16180       ;; Convert old to new.
16181       (gnus-convert-old-newsrc))))
16182
16183 (defun gnus-continuum-version (version)
16184   "Return VERSION as a floating point number."
16185   (when (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16186     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16187            (number (match-string 2 version))
16188            major minor least)
16189       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16190       (setq major (string-to-number (match-string 1 number)))
16191       (setq minor (string-to-number (match-string 2 number)))
16192       (setq least (if (match-beginning 3)
16193                       (string-to-number (match-string 3 number))
16194                     0))
16195       (string-to-number
16196        (if (zerop major)
16197            (format "%s00%02d%02d"
16198                    (cond 
16199                     ((string= alpha "(ding)") "4.99")
16200                     ((string= alpha "September") "5.01")
16201                     ((string= alpha "Red") "5.03"))
16202                    minor least)
16203          (format "%d.%02d%20d" major minor least))))))
16204
16205 (defun gnus-convert-old-newsrc ()
16206   "Convert old newsrc into the new format, if needed."
16207   (let ((fcv (and gnus-newsrc-file-version
16208                   (gnus-continuum-version gnus-newsrc-file-version))))
16209     (cond
16210      ;; No .newsrc.eld file was loaded.
16211      ((null fcv) nil)
16212      ;; Gnus 5 .newsrc.eld was loaded.
16213      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16214       (gnus-convert-old-ticks)))))
16215
16216 (defun gnus-convert-old-ticks ()
16217   (let ((newsrc (cdr gnus-newsrc-alist))
16218         marks info dormant ticked)
16219     (while (setq info (pop newsrc))
16220       (when (setq marks (gnus-info-marks info))
16221         (setq dormant (cdr (assq 'dormant marks))
16222               ticked (cdr (assq 'tick marks)))
16223         (when (or dormant ticked)
16224           (gnus-info-set-read
16225            info
16226            (gnus-add-to-range
16227             (gnus-info-read info)
16228             (nconc (gnus-uncompress-range dormant)
16229                    (gnus-uncompress-range ticked)))))))))
16230
16231 (defun gnus-read-newsrc-el-file (file)
16232   (let ((ding-file (concat file "d")))
16233     ;; We always, always read the .eld file.
16234     (gnus-message 5 "Reading %s..." ding-file)
16235     (let (gnus-newsrc-assoc)
16236       (condition-case nil
16237           (load ding-file t t t)
16238         (error
16239          (gnus-error 1 "Error in %s" ding-file)))
16240       (when gnus-newsrc-assoc
16241         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16242     (gnus-make-hashtable-from-newsrc-alist)
16243     (when (file-newer-than-file-p file ding-file)
16244       ;; Old format quick file
16245       (gnus-message 5 "Reading %s..." file)
16246       ;; The .el file is newer than the .eld file, so we read that one
16247       ;; as well.
16248       (gnus-read-old-newsrc-el-file file))))
16249
16250 ;; Parse the old-style quick startup file
16251 (defun gnus-read-old-newsrc-el-file (file)
16252   (let (newsrc killed marked group m info)
16253     (prog1
16254         (let ((gnus-killed-assoc nil)
16255               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16256           (prog1
16257               (condition-case nil
16258                   (load file t t t)
16259                 (error nil))
16260             (setq newsrc gnus-newsrc-assoc
16261                   killed gnus-killed-assoc
16262                   marked gnus-marked-assoc)))
16263       (setq gnus-newsrc-alist nil)
16264       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16265         (if info
16266             (progn
16267               (gnus-info-set-read info (cddr group))
16268               (gnus-info-set-level
16269                info (if (nth 1 group) gnus-level-default-subscribed
16270                       gnus-level-default-unsubscribed))
16271               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16272           (push (setq info
16273                       (list (car group)
16274                             (if (nth 1 group) gnus-level-default-subscribed
16275                               gnus-level-default-unsubscribed)
16276                             (cddr group)))
16277                 gnus-newsrc-alist))
16278         ;; Copy marks into info.
16279         (when (setq m (assoc (car group) marked))
16280           (unless (nthcdr 3 info)
16281             (nconc info (list nil)))
16282           (gnus-info-set-marks
16283            info (list (cons 'tick (gnus-compress-sequence 
16284                                    (sort (cdr m) '<) t))))))
16285       (setq newsrc killed)
16286       (while newsrc
16287         (setcar newsrc (caar newsrc))
16288         (setq newsrc (cdr newsrc)))
16289       (setq gnus-killed-list killed))
16290     ;; The .el file version of this variable does not begin with
16291     ;; "options", while the .eld version does, so we just add it if it
16292     ;; isn't there.
16293     (and
16294      gnus-newsrc-options
16295      (progn
16296        (and (not (string-match "^ *options" gnus-newsrc-options))
16297             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16298        (and (not (string-match "\n$" gnus-newsrc-options))
16299             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16300        ;; Finally, if we read some options lines, we parse them.
16301        (or (string= gnus-newsrc-options "")
16302            (gnus-newsrc-parse-options gnus-newsrc-options))))
16303
16304     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16305     (gnus-make-hashtable-from-newsrc-alist)))
16306
16307 (defun gnus-make-newsrc-file (file)
16308   "Make server dependent file name by catenating FILE and server host name."
16309   (let* ((file (expand-file-name file nil))
16310          (real-file (concat file "-" (nth 1 gnus-select-method))))
16311     (if (or (file-exists-p real-file)
16312             (file-exists-p (concat real-file ".el"))
16313             (file-exists-p (concat real-file ".eld")))
16314         real-file file)))
16315
16316 (defun gnus-newsrc-to-gnus-format ()
16317   (setq gnus-newsrc-options "")
16318   (setq gnus-newsrc-options-n nil)
16319
16320   (or gnus-active-hashtb
16321       (setq gnus-active-hashtb (make-vector 4095 0)))
16322   (let ((buf (current-buffer))
16323         (already-read (> (length gnus-newsrc-alist) 1))
16324         group subscribed options-symbol newsrc Options-symbol
16325         symbol reads num1)
16326     (goto-char (point-min))
16327     ;; We intern the symbol `options' in the active hashtb so that we
16328     ;; can `eq' against it later.
16329     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16330     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16331
16332     (while (not (eobp))
16333       ;; We first read the first word on the line by narrowing and
16334       ;; then reading into `gnus-active-hashtb'.  Most groups will
16335       ;; already exist in that hashtb, so this will save some string
16336       ;; space.
16337       (narrow-to-region
16338        (point)
16339        (progn (skip-chars-forward "^ \t!:\n") (point)))
16340       (goto-char (point-min))
16341       (setq symbol
16342             (and (/= (point-min) (point-max))
16343                  (let ((obarray gnus-active-hashtb)) (read buf))))
16344       (widen)
16345       ;; Now, the symbol we have read is either `options' or a group
16346       ;; name.  If it is an options line, we just add it to a string.
16347       (cond
16348        ((or (eq symbol options-symbol)
16349             (eq symbol Options-symbol))
16350         (setq gnus-newsrc-options
16351               ;; This concating is quite inefficient, but since our
16352               ;; thorough studies show that approx 99.37% of all
16353               ;; .newsrc files only contain a single options line, we
16354               ;; don't give a damn, frankly, my dear.
16355               (concat gnus-newsrc-options
16356                       (buffer-substring
16357                        (gnus-point-at-bol)
16358                        ;; Options may continue on the next line.
16359                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16360                                 (progn (beginning-of-line) (point)))
16361                            (point)))))
16362         (forward-line -1))
16363        (symbol
16364         ;; Group names can be just numbers.  
16365         (when (numberp symbol) 
16366           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16367         (or (boundp symbol) (set symbol nil))
16368         ;; It was a group name.
16369         (setq subscribed (= (following-char) ?:)
16370               group (symbol-name symbol)
16371               reads nil)
16372         (if (eolp)
16373             ;; If the line ends here, this is clearly a buggy line, so
16374             ;; we put point a the beginning of line and let the cond
16375             ;; below do the error handling.
16376             (beginning-of-line)
16377           ;; We skip to the beginning of the ranges.
16378           (skip-chars-forward "!: \t"))
16379         ;; We are now at the beginning of the list of read articles.
16380         ;; We read them range by range.
16381         (while
16382             (cond
16383              ((looking-at "[0-9]+")
16384               ;; We narrow and read a number instead of buffer-substring/
16385               ;; string-to-int because it's faster.  narrow/widen is
16386               ;; faster than save-restriction/narrow, and save-restriction
16387               ;; produces a garbage object.
16388               (setq num1 (progn
16389                            (narrow-to-region (match-beginning 0) (match-end 0))
16390                            (read buf)))
16391               (widen)
16392               ;; If the next character is a dash, then this is a range.
16393               (if (= (following-char) ?-)
16394                   (progn
16395                     ;; We read the upper bound of the range.
16396                     (forward-char 1)
16397                     (if (not (looking-at "[0-9]+"))
16398                         ;; This is a buggy line, by we pretend that
16399                         ;; it's kinda OK.  Perhaps the user should be
16400                         ;; dinged?
16401                         (setq reads (cons num1 reads))
16402                       (setq reads
16403                             (cons
16404                              (cons num1
16405                                    (progn
16406                                      (narrow-to-region (match-beginning 0)
16407                                                        (match-end 0))
16408                                      (read buf)))
16409                              reads))
16410                       (widen)))
16411                 ;; It was just a simple number, so we add it to the
16412                 ;; list of ranges.
16413                 (setq reads (cons num1 reads)))
16414               ;; If the next char in ?\n, then we have reached the end
16415               ;; of the line and return nil.
16416               (/= (following-char) ?\n))
16417              ((= (following-char) ?\n)
16418               ;; End of line, so we end.
16419               nil)
16420              (t
16421               ;; Not numbers and not eol, so this might be a buggy
16422               ;; line...
16423               (or (eobp)
16424                   ;; If it was eob instead of ?\n, we allow it.
16425                   (progn
16426                     ;; The line was buggy.
16427                     (setq group nil)
16428                     (gnus-error 3.1 "Mangled line: %s"
16429                                 (buffer-substring (gnus-point-at-bol)
16430                                                   (gnus-point-at-eol)))))
16431               nil))
16432           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16433           ;; we allow them, because it's a common mistake to put a
16434           ;; space after the comma.
16435           (skip-chars-forward ", "))
16436
16437         ;; We have already read .newsrc.eld, so we gently update the
16438         ;; data in the hash table with the information we have just
16439         ;; read.
16440         (when group
16441           (let ((info (gnus-get-info group))
16442                 level)
16443             (if info
16444                 ;; There is an entry for this file in the alist.
16445                 (progn
16446                   (gnus-info-set-read info (nreverse reads))
16447                   ;; We update the level very gently.  In fact, we
16448                   ;; only change it if there's been a status change
16449                   ;; from subscribed to unsubscribed, or vice versa.
16450                   (setq level (gnus-info-level info))
16451                   (cond ((and (<= level gnus-level-subscribed)
16452                               (not subscribed))
16453                          (setq level (if reads
16454                                          gnus-level-default-unsubscribed
16455                                        (1+ gnus-level-default-unsubscribed))))
16456                         ((and (> level gnus-level-subscribed) subscribed)
16457                          (setq level gnus-level-default-subscribed)))
16458                   (gnus-info-set-level info level))
16459               ;; This is a new group.
16460               (setq info (list group
16461                                (if subscribed
16462                                    gnus-level-default-subscribed
16463                                  (if reads
16464                                      (1+ gnus-level-subscribed)
16465                                    gnus-level-default-unsubscribed))
16466                                (nreverse reads))))
16467             (setq newsrc (cons info newsrc))))))
16468       (forward-line 1))
16469
16470     (setq newsrc (nreverse newsrc))
16471
16472     (if (not already-read)
16473         ()
16474       ;; We now have two newsrc lists - `newsrc', which is what we
16475       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16476       ;; what we've read from .newsrc.eld.  We have to merge these
16477       ;; lists.  We do this by "attaching" any (foreign) groups in the
16478       ;; gnus-newsrc-alist to the (native) group that precedes them.
16479       (let ((rc (cdr gnus-newsrc-alist))
16480             (prev gnus-newsrc-alist)
16481             entry mentry)
16482         (while rc
16483           (or (null (nth 4 (car rc)))   ; It's a native group.
16484               (assoc (caar rc) newsrc) ; It's already in the alist.
16485               (if (setq entry (assoc (caar prev) newsrc))
16486                   (setcdr (setq mentry (memq entry newsrc))
16487                           (cons (car rc) (cdr mentry)))
16488                 (setq newsrc (cons (car rc) newsrc))))
16489           (setq prev rc
16490                 rc (cdr rc)))))
16491
16492     (setq gnus-newsrc-alist newsrc)
16493     ;; We make the newsrc hashtb.
16494     (gnus-make-hashtable-from-newsrc-alist)
16495
16496     ;; Finally, if we read some options lines, we parse them.
16497     (or (string= gnus-newsrc-options "")
16498         (gnus-newsrc-parse-options gnus-newsrc-options))))
16499
16500 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16501 ;; The return value will be a list on the form
16502 ;; ((regexp1 . ignore)
16503 ;;  (regexp2 . subscribe)...)
16504 ;; When handling new newsgroups, groups that match a `ignore' regexp
16505 ;; will be ignored, and groups that match a `subscribe' regexp will be
16506 ;; subscribed.  A line like
16507 ;; options -n !all rec.all
16508 ;; will lead to a list that looks like
16509 ;; (("^rec\\..+" . subscribe)
16510 ;;  ("^.+" . ignore))
16511 ;; So all "rec.*" groups will be subscribed, while all the other
16512 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16513 ;; different from "options -n rec.all !all".
16514 (defun gnus-newsrc-parse-options (options)
16515   (let (out eol)
16516     (save-excursion
16517       (gnus-set-work-buffer)
16518       (insert (regexp-quote options))
16519       ;; First we treat all continuation lines.
16520       (goto-char (point-min))
16521       (while (re-search-forward "\n[ \t]+" nil t)
16522         (replace-match " " t t))
16523       ;; Then we transform all "all"s into ".+"s.
16524       (goto-char (point-min))
16525       (while (re-search-forward "\\ball\\b" nil t)
16526         (replace-match ".+" t t))
16527       (goto-char (point-min))
16528       ;; We remove all other options than the "-n" ones.
16529       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16530         (replace-match " ")
16531         (forward-char -1))
16532       (goto-char (point-min))
16533
16534       ;; We are only interested in "options -n" lines - we
16535       ;; ignore the other option lines.
16536       (while (re-search-forward "[ \t]-n" nil t)
16537         (setq eol
16538               (or (save-excursion
16539                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16540                          (- (point) 2)))
16541                   (gnus-point-at-eol)))
16542         ;; Search for all "words"...
16543         (while (re-search-forward "[^ \t,\n]+" eol t)
16544           (if (= (char-after (match-beginning 0)) ?!)
16545               ;; If the word begins with a bang (!), this is a "not"
16546               ;; spec.  We put this spec (minus the bang) and the
16547               ;; symbol `ignore' into the list.
16548               (setq out (cons (cons (concat
16549                                      "^" (buffer-substring
16550                                           (1+ (match-beginning 0))
16551                                           (match-end 0)))
16552                                     'ignore) out))
16553             ;; There was no bang, so this is a "yes" spec.
16554             (setq out (cons (cons (concat "^" (match-string 0))
16555                                   'subscribe) out)))))
16556
16557       (setq gnus-newsrc-options-n out))))
16558
16559 (defun gnus-save-newsrc-file (&optional force)
16560   "Save .newsrc file."
16561   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16562   ;; from the variable gnus-newsrc-alist.
16563   (when (and (or gnus-newsrc-alist gnus-killed-list)
16564              gnus-current-startup-file)
16565     (save-excursion
16566       (if (and (or gnus-use-dribble-file gnus-slave)
16567                (not force)
16568                (or (not gnus-dribble-buffer)
16569                    (not (buffer-name gnus-dribble-buffer))
16570                    (zerop (save-excursion
16571                             (set-buffer gnus-dribble-buffer)
16572                             (buffer-size)))))
16573           (gnus-message 4 "(No changes need to be saved)")
16574         (run-hooks 'gnus-save-newsrc-hook)
16575         (if gnus-slave
16576             (gnus-slave-save-newsrc)
16577           ;; Save .newsrc.
16578           (when gnus-save-newsrc-file
16579             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16580             (gnus-gnus-to-newsrc-format)
16581             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16582           ;; Save .newsrc.eld.
16583           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16584           (make-local-variable 'version-control)
16585           (setq version-control 'never)
16586           (setq buffer-file-name
16587                 (concat gnus-current-startup-file ".eld"))
16588           (gnus-add-current-to-buffer-list)
16589           (buffer-disable-undo (current-buffer))
16590           (erase-buffer)
16591           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16592           (gnus-gnus-to-quick-newsrc-format)
16593           (run-hooks 'gnus-save-quick-newsrc-hook)
16594           (save-buffer)
16595           (kill-buffer (current-buffer))
16596           (gnus-message
16597            5 "Saving %s.eld...done" gnus-current-startup-file))
16598         (gnus-dribble-delete-file)))))
16599
16600 (defun gnus-gnus-to-quick-newsrc-format ()
16601   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16602   (insert ";; Gnus startup file.\n")
16603   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16604   (insert ";; to read .newsrc.\n")
16605   (insert "(setq gnus-newsrc-file-version "
16606           (prin1-to-string gnus-version) ")\n")
16607   (let ((variables
16608          (if gnus-save-killed-list gnus-variable-list
16609            ;; Remove the `gnus-killed-list' from the list of variables
16610            ;; to be saved, if required.
16611            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16612         ;; Peel off the "dummy" group.
16613         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16614         variable)
16615     ;; Insert the variables into the file.
16616     (while variables
16617       (when (and (boundp (setq variable (pop variables)))
16618                  (symbol-value variable))
16619         (insert "(setq " (symbol-name variable) " '")
16620         (prin1 (symbol-value variable) (current-buffer))
16621         (insert ")\n")))))
16622
16623 (defun gnus-gnus-to-newsrc-format ()
16624   ;; Generate and save the .newsrc file.
16625   (save-excursion
16626     (set-buffer (create-file-buffer gnus-current-startup-file))
16627     (let ((newsrc (cdr gnus-newsrc-alist))
16628           (standard-output (current-buffer))
16629           info ranges range method)
16630       (setq buffer-file-name gnus-current-startup-file)
16631       (buffer-disable-undo (current-buffer))
16632       (erase-buffer)
16633       ;; Write options.
16634       (if gnus-newsrc-options (insert gnus-newsrc-options))
16635       ;; Write subscribed and unsubscribed.
16636       (while (setq info (pop newsrc))
16637         ;; Don't write foreign groups to .newsrc.
16638         (when (or (null (setq method (gnus-info-method info)))
16639                   (equal method "native")
16640                   (gnus-server-equal method gnus-select-method))
16641           (insert (gnus-info-group info)
16642                   (if (> (gnus-info-level info) gnus-level-subscribed)
16643                       "!" ":"))
16644           (when (setq ranges (gnus-info-read info))
16645             (insert " ")
16646             (if (not (listp (cdr ranges)))
16647                 (if (= (car ranges) (cdr ranges))
16648                     (princ (car ranges))
16649                   (princ (car ranges))
16650                   (insert "-")
16651                   (princ (cdr ranges)))
16652               (while (setq range (pop ranges))
16653                 (if (or (atom range) (= (car range) (cdr range)))
16654                     (princ (or (and (atom range) range) (car range)))
16655                   (princ (car range))
16656                   (insert "-")
16657                   (princ (cdr range)))
16658                 (if ranges (insert ",")))))
16659           (insert "\n")))
16660       (make-local-variable 'version-control)
16661       (setq version-control 'never)
16662       ;; It has been reported that sometime the modtime on the .newsrc
16663       ;; file seems to be off.  We really do want to overwrite it, so
16664       ;; we clear the modtime here before saving.  It's a bit odd,
16665       ;; though...
16666       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16667       ;; delete the silly thing entirely first.  but this fails to provide
16668       ;; such niceties as .newsrc~ creation.
16669       (if gnus-modtime-botch
16670           (delete-file gnus-startup-file)
16671         (clear-visited-file-modtime))
16672       (run-hooks 'gnus-save-standard-newsrc-hook)
16673       (save-buffer)
16674       (kill-buffer (current-buffer)))))
16675
16676 \f
16677 ;;;
16678 ;;; Slave functions.
16679 ;;;
16680
16681 (defun gnus-slave-save-newsrc ()
16682   (save-excursion
16683     (set-buffer gnus-dribble-buffer)
16684     (let ((slave-name
16685            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16686       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16687
16688 (defun gnus-master-read-slave-newsrc ()
16689   (let ((slave-files
16690          (directory-files
16691           (file-name-directory gnus-current-startup-file)
16692           t (concat
16693              "^" (regexp-quote
16694                   (concat
16695                    (file-name-nondirectory gnus-current-startup-file)
16696                    "-slave-")))
16697           t))
16698         file)
16699     (if (not slave-files)
16700         ()                              ; There are no slave files to read.
16701       (gnus-message 7 "Reading slave newsrcs...")
16702       (save-excursion
16703         (set-buffer (get-buffer-create " *gnus slave*"))
16704         (buffer-disable-undo (current-buffer))
16705         (setq slave-files
16706               (sort (mapcar (lambda (file)
16707                               (list (nth 5 (file-attributes file)) file))
16708                             slave-files)
16709                     (lambda (f1 f2)
16710                       (or (< (caar f1) (caar f2))
16711                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16712         (while slave-files
16713           (erase-buffer)
16714           (setq file (nth 1 (car slave-files)))
16715           (insert-file-contents file)
16716           (if (condition-case ()
16717                   (progn
16718                     (eval-buffer (current-buffer))
16719                     t)
16720                 (error
16721                  (gnus-error 3.2 "Possible error in %s" file)
16722                  nil))
16723               (or gnus-slave ; Slaves shouldn't delete these files.
16724                   (condition-case ()
16725                       (delete-file file)
16726                     (error nil))))
16727           (setq slave-files (cdr slave-files))))
16728       (gnus-message 7 "Reading slave newsrcs...done"))))
16729
16730 \f
16731 ;;;
16732 ;;; Group description.
16733 ;;;
16734
16735 (defun gnus-read-all-descriptions-files ()
16736   (let ((methods (cons gnus-select-method 
16737                        (nconc
16738                         (when gnus-message-archive-method
16739                           (list "archive"))
16740                         gnus-secondary-select-methods))))
16741     (while methods
16742       (gnus-read-descriptions-file (car methods))
16743       (setq methods (cdr methods)))
16744     t))
16745
16746 (defun gnus-read-descriptions-file (&optional method)
16747   (let ((method (or method gnus-select-method)))
16748     (when (stringp method)
16749       (setq method (gnus-server-to-method method)))
16750     ;; We create the hashtable whether we manage to read the desc file
16751     ;; to avoid trying to re-read after a failed read.
16752     (or gnus-description-hashtb
16753         (setq gnus-description-hashtb
16754               (gnus-make-hashtable (length gnus-active-hashtb))))
16755     ;; Mark this method's desc file as read.
16756     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16757                   gnus-description-hashtb)
16758
16759     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16760     (cond
16761      ((not (gnus-check-server method))
16762       (gnus-message 1 "Couldn't open server")
16763       nil)
16764      ((not (gnus-request-list-newsgroups method))
16765       (gnus-message 1 "Couldn't read newsgroups descriptions")
16766       nil)
16767      (t
16768       (let (group)
16769         (save-excursion
16770           (save-restriction
16771             (set-buffer nntp-server-buffer)
16772             (goto-char (point-min))
16773             (if (or (search-forward "\n.\n" nil t)
16774                     (goto-char (point-max)))
16775                 (progn
16776                   (beginning-of-line)
16777                   (narrow-to-region (point-min) (point))))
16778             (goto-char (point-min))
16779             (while (not (eobp))
16780               ;; If we get an error, we set group to 0, which is not a
16781               ;; symbol...
16782               (setq group
16783                     (condition-case ()
16784                         (let ((obarray gnus-description-hashtb))
16785                           ;; Group is set to a symbol interned in this
16786                           ;; hash table.
16787                           (read nntp-server-buffer))
16788                       (error 0)))
16789               (skip-chars-forward " \t")
16790               ;; ...  which leads to this line being effectively ignored.
16791               (and (symbolp group)
16792                    (set group (buffer-substring
16793                                (point) (progn (end-of-line) (point)))))
16794               (forward-line 1))))
16795         (gnus-message 5 "Reading descriptions file...done")
16796         t)))))
16797
16798 (defun gnus-group-get-description (group)
16799   "Get the description of a group by sending XGTITLE to the server."
16800   (when (gnus-request-group-description group)
16801     (save-excursion
16802       (set-buffer nntp-server-buffer)
16803       (goto-char (point-min))
16804       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16805         (match-string 1)))))
16806
16807 \f
16808 ;;;
16809 ;;; Buffering of read articles.
16810 ;;;
16811
16812 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16813 (defvar gnus-backlog-articles nil)
16814 (defvar gnus-backlog-hashtb nil)
16815
16816 (defun gnus-backlog-buffer ()
16817   "Return the backlog buffer."
16818   (or (get-buffer gnus-backlog-buffer)
16819       (save-excursion
16820         (set-buffer (get-buffer-create gnus-backlog-buffer))
16821         (buffer-disable-undo (current-buffer))
16822         (setq buffer-read-only t)
16823         (gnus-add-current-to-buffer-list)
16824         (get-buffer gnus-backlog-buffer))))
16825
16826 (defun gnus-backlog-setup ()
16827   "Initialize backlog variables."
16828   (unless gnus-backlog-hashtb
16829     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16830
16831 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16832
16833 (defun gnus-backlog-shutdown ()
16834   "Clear all backlog variables and buffers."
16835   (when (get-buffer gnus-backlog-buffer)
16836     (kill-buffer gnus-backlog-buffer))
16837   (setq gnus-backlog-hashtb nil
16838         gnus-backlog-articles nil))
16839
16840 (defun gnus-backlog-enter-article (group number buffer)
16841   (gnus-backlog-setup)
16842   (let ((ident (intern (concat group ":" (int-to-string number))
16843                        gnus-backlog-hashtb))
16844         b)
16845     (if (memq ident gnus-backlog-articles)
16846         () ; It's already kept.
16847       ;; Remove the oldest article, if necessary.
16848       (and (numberp gnus-keep-backlog)
16849            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16850            (gnus-backlog-remove-oldest-article))
16851       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16852       ;; Insert the new article.
16853       (save-excursion
16854         (set-buffer (gnus-backlog-buffer))
16855         (let (buffer-read-only)
16856           (goto-char (point-max))
16857           (or (bolp) (insert "\n"))
16858           (setq b (point))
16859           (insert-buffer-substring buffer)
16860           ;; Tag the beginning of the article with the ident.
16861           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16862
16863 (defun gnus-backlog-remove-oldest-article ()
16864   (save-excursion
16865     (set-buffer (gnus-backlog-buffer))
16866     (goto-char (point-min))
16867     (if (zerop (buffer-size))
16868         () ; The buffer is empty.
16869       (let ((ident (get-text-property (point) 'gnus-backlog))
16870             buffer-read-only)
16871         ;; Remove the ident from the list of articles.
16872         (when ident
16873           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16874         ;; Delete the article itself.
16875         (delete-region
16876          (point) (next-single-property-change
16877                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16878
16879 (defun gnus-backlog-remove-article (group number)
16880   "Remove article NUMBER in GROUP from the backlog."
16881   (when (numberp number)
16882     (gnus-backlog-setup)
16883     (let ((ident (intern (concat group ":" (int-to-string number))
16884                          gnus-backlog-hashtb))
16885           beg end)
16886       (when (memq ident gnus-backlog-articles)
16887         ;; It was in the backlog.
16888         (save-excursion
16889           (set-buffer (gnus-backlog-buffer))
16890           (when (setq beg (text-property-any
16891                            (point-min) (point-max) 'gnus-backlog
16892                            ident))
16893             ;; Find the end (i. e., the beginning of the next article).
16894             (setq end
16895                   (next-single-property-change
16896                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
16897             (delete-region beg end)
16898             ;; Return success.
16899             t))))))
16900
16901 (defun gnus-backlog-request-article (group number buffer)
16902   (when (numberp number)
16903     (gnus-backlog-setup)
16904     (let ((ident (intern (concat group ":" (int-to-string number))
16905                          gnus-backlog-hashtb))
16906           beg end)
16907       (when (memq ident gnus-backlog-articles)
16908         ;; It was in the backlog.
16909         (save-excursion
16910           (set-buffer (gnus-backlog-buffer))
16911           (if (not (setq beg (text-property-any
16912                               (point-min) (point-max) 'gnus-backlog
16913                               ident)))
16914               ;; It wasn't in the backlog after all.
16915               (ignore
16916                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16917             ;; Find the end (i. e., the beginning of the next article).
16918             (setq end
16919                   (next-single-property-change
16920                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16921         (let ((buffer-read-only nil))
16922           (erase-buffer)
16923           (insert-buffer-substring gnus-backlog-buffer beg end)
16924           t)))))
16925
16926 ;; Allow redefinition of Gnus functions.
16927
16928 (gnus-ems-redefine)
16929
16930 (provide 'gnus)
16931
16932 ;;; gnus.el ends here