*** 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 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval '(run-hooks 'gnus-load-hook))
29
30 (require 'mail-utils)
31 (require 'timezone)
32 (require 'nnheader)
33
34 (eval-when-compile (require 'cl))
35
36 ;; Site dependent variables.  These variables should be defined in
37 ;; paths.el.
38
39 (defvar gnus-default-nntp-server nil
40   "Specify a default NNTP server.
41 This variable should be defined in paths.el, and should never be set
42 by the user.
43 If you want to change servers, you should use `gnus-select-method'.
44 See the documentation to that variable.")
45
46 (defconst gnus-backup-default-subscribed-newsgroups 
47   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
48   "Default default new newsgroups the first time Gnus is run.
49 Should be set in paths.el, and shouldn't be touched by the user.")
50
51 (defvar gnus-local-domain nil
52   "Local domain name without a host name.
53 The DOMAINNAME environment variable is used instead if it is defined.
54 If the `system-name' function returns the full Internet name, there is
55 no need to set this variable.")
56
57 (defvar gnus-local-organization nil
58   "String with a description of what organization (if any) the user belongs to.
59 The ORGANIZATION environment variable is used instead if it is defined.
60 If this variable contains a function, this function will be called
61 with the current newsgroup name as the argument.  The function should
62 return a string.
63
64 In any case, if the string (either in the variable, in the environment
65 variable, or returned by the function) is a file name, the contents of
66 this file will be used as the organization.")
67
68 (defvar gnus-use-generic-from nil
69   "If nil, the full host name will be the system name prepended to the domain name.
70 If this is a string, the full host name will be this string.
71 If this is non-nil, non-string, the domain name will be used as the
72 full host name.")
73
74 (defvar gnus-use-generic-path nil
75   "If nil, use the NNTP server name in the Path header.
76 If stringp, use this; if non-nil, use no host name (user name only).")
77
78
79 ;; Customization variables
80
81 ;; Don't touch this variable.
82 (defvar gnus-nntp-service "nntp"
83   "*NNTP service name (\"nntp\" or 119).
84 This is an obsolete variable, which is scarcely used.  If you use an
85 nntp server for your newsgroup and want to change the port number
86 used to 899, you would say something along these lines:
87
88  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
89
90 (defvar gnus-nntpserver-file "/etc/nntpserver"
91   "*A file with only the name of the nntp server in it.")
92
93 ;; This function is used to check both the environment variable
94 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
95 ;; an nntp server name default.
96 (defun gnus-getenv-nntpserver ()
97   (or (getenv "NNTPSERVER")
98       (and (file-readable-p gnus-nntpserver-file)
99            (save-excursion
100              (set-buffer (get-buffer-create " *gnus nntp*"))
101              (buffer-disable-undo (current-buffer))
102              (insert-file-contents gnus-nntpserver-file)
103              (let ((name (buffer-string)))
104                (prog1
105                    (if (string-match "^[ \t\n]*$" name)
106                        nil
107                      name)
108                  (kill-buffer (current-buffer))))))))
109                  
110 (defvar gnus-select-method 
111   (nconc
112    (list 'nntp (or (condition-case ()
113                        (gnus-getenv-nntpserver)
114                      (error nil))
115                    (if (and gnus-default-nntp-server
116                             (not (string= gnus-default-nntp-server "")))
117                        gnus-default-nntp-server)
118                    (system-name)))
119    (if (or (null gnus-nntp-service)
120            (equal gnus-nntp-service "nntp"))
121        nil 
122      (list gnus-nntp-service)))
123   "*Default method for selecting a newsgroup.
124 This variable should be a list, where the first element is how the
125 news is to be fetched, the second is the address. 
126
127 For instance, if you want to get your news via NNTP from
128 \"flab.flab.edu\", you could say:
129
130 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
131
132 If you want to use your local spool, say:
133
134 (setq gnus-select-method (list 'nnspool (system-name)))
135
136 If you use this variable, you must set `gnus-nntp-server' to nil.
137
138 There is a lot more to know about select methods and virtual servers -
139 see the manual for details.")
140
141 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
142 (defvar gnus-post-method nil
143   "*Preferred method for posting USENET news.
144 If this variable is nil, Gnus will use the current method to decide
145 which method to use when posting.  If it is non-nil, it will override
146 the current method.  This method will not be used in mail groups and
147 the like, only in \"real\" newsgroups.
148
149 The value must be a valid method as discussed in the documentation of
150 `gnus-select-method'.")
151
152 (defvar gnus-refer-article-method nil
153   "*Preferred method for fetching an article by Message-ID.
154 If you are reading news from the local spool (with nnspool), fetching
155 articles by Message-ID is painfully slow.  By setting this method to an
156 nntp method, you might get acceptable results.
157
158 The value of this variable must be a valid select method as discussed
159 in the documentation of `gnus-select-method'")
160
161 (defvar gnus-secondary-select-methods nil
162   "*A list of secondary methods that will be used for reading news.
163 This is a list where each element is a complete select method (see
164 `gnus-select-method').  
165
166 If, for instance, you want to read your mail with the nnml backend,
167 you could set this variable:
168
169 (setq gnus-secondary-select-methods '((nnml \"\")))")
170
171 (defvar gnus-secondary-servers nil
172   "*List of NNTP servers that the user can choose between interactively.
173 To make Gnus query you for a server, you have to give `gnus' a
174 non-numeric prefix - `C-u M-x gnus', in short.")
175
176 (defvar gnus-nntp-server nil
177   "*The name of the host running the NNTP server.
178 This variable is semi-obsolete.  Use the `gnus-select-method'
179 variable instead.")
180
181 (defvar gnus-startup-file "~/.newsrc"
182   "*Your `.newsrc' file.
183 `.newsrc-SERVER' will be used instead if that exists.")
184
185 (defvar gnus-init-file "~/.gnus"
186   "*Your Gnus elisp startup file.
187 If a file with the .el or .elc suffixes exist, it will be read
188 instead.") 
189
190 (defvar gnus-group-faq-directory
191   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
192 ;    "/ftp@ftp.uu.net:/usenet/news.answers/"
193     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
194     "/ftp@rtfm.mit.edu:/pub/usenet/news.answers/"
195     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
196 ;    "/ftp@ftp.Germany.EU.net:/pub/newsarchive/news.answers/"
197     "/ftp@ftp.sunet.se:/pub/usenet/"
198     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
199     "/ftp@hwarang.postech.ac.kr:/pub/usenet/news.answers/"
200     "/ftp@ftp.hk.super.net:/mirror/faqs/")
201   "*Directory where the group FAQs are stored.
202 This will most commonly be on a remote machine, and the file will be
203 fetched by ange-ftp.
204
205 This variable can also be a list of directories.  In that case, the
206 first element in the list will be used by default, and the others will
207 be used as backup sites.
208
209 Note that Gnus uses an aol machine as the default directory.  If this
210 feels fundamentally unclean, just think of it as a way to finally get
211 something of value back from them.
212
213 If the default site is too slow, try one of these:
214
215    North America: mirrors.aol.com                /pub/rtfm/usenet
216                   ftp.seas.gwu.edu               /pub/rtfm
217                   rtfm.mit.edu                   /pub/usenet/news.answers
218    Europe:        ftp.uni-paderborn.de           /pub/FAQ
219                   ftp.sunet.se                   /pub/usenet
220    Asia:          nctuccca.edu.tw                /USENET/FAQ
221                   hwarang.postech.ac.kr          /pub/usenet/news.answers
222                   ftp.hk.super.net               /mirror/faqs")
223
224 (defvar gnus-group-archive-directory
225   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/" 
226   "*The address of the (ding) archives.")
227
228 (defvar gnus-group-recent-archive-directory
229   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
230   "*The address of the most recent (ding) articles.")
231
232 (defvar gnus-default-subscribed-newsgroups nil
233   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
234 It should be a list of strings.
235 If it is `t', Gnus will not do anything special the first time it is
236 started; it'll just use the normal newsgroups subscription methods.")
237
238 (defvar gnus-use-cross-reference t
239   "*Non-nil means that cross referenced articles will be marked as read.
240 If nil, ignore cross references.  If t, mark articles as read in
241 subscribed newsgroups.  If neither t nor nil, mark as read in all
242 newsgroups.") 
243
244 (defvar gnus-use-dribble-file t
245   "*Non-nil means that Gnus will use a dribble file to store user updates.
246 If Emacs should crash without saving the .newsrc files, complete
247 information can be restored from the dribble file.")
248
249 (defvar gnus-dribble-directory nil
250   "*The directory where dribble files will be saved.
251 If this variable is nil, the directory where the .newsrc files are
252 saved will be used.")
253
254 (defvar gnus-asynchronous nil
255   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
256
257 (defvar gnus-kill-summary-on-exit t
258   "*If non-nil, kill the summary buffer when you exit from it.
259 If nil, the summary will become a \"*Dead Summary*\" buffer, and
260 it will be killed sometime later.")
261
262 (defvar gnus-large-newsgroup 200
263   "*The number of articles which indicates a large newsgroup.
264 If the number of articles in a newsgroup is greater than this value,
265 confirmation is required for selecting the newsgroup.")
266
267 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
268 (defvar gnus-no-groups-message "No news is horrible news"
269   "*Message displayed by Gnus when no groups are available.")
270
271 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
272   "*Non-nil means that the default name of a file to save articles in is the group name.
273 If it's nil, the directory form of the group name is used instead.
274
275 If this variable is a list, and the list contains the element
276 `not-score', long file names will not be used for score files; if it
277 contains the element `not-save', long file names will not be used for
278 saving; and if it contains the element `not-kill', long file names
279 will not be used for kill files.")
280
281 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
282   "*Name of the directory articles will be saved in (default \"~/News\").
283 Initialized from the SAVEDIR environment variable.")
284
285 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
286   "*Name of the directory where kill files will be stored (default \"~/News\").
287 Initialized from the SAVEDIR environment variable.")
288
289 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
290   "*A function to save articles in your favorite format.
291 The function must be interactively callable (in other words, it must
292 be an Emacs command).
293
294 Gnus provides the following functions:
295
296 * gnus-summary-save-in-rmail (Rmail format)
297 * gnus-summary-save-in-mail (Unix mail format)
298 * gnus-summary-save-in-folder (MH folder)
299 * gnus-summary-save-in-file (article format).
300 * gnus-summary-save-in-vm (use VM's folder format).")
301
302 (defvar gnus-prompt-before-saving 'always
303   "*This variable says how much prompting is to be done when saving articles.
304 If it is nil, no prompting will be done, and the articles will be
305 saved to the default files.  If this variable is `always', each and
306 every article that is saved will be preceded by a prompt, even when
307 saving large batches of articles.  If this variable is neither nil not
308 `always', there the user will be prompted once for a file name for
309 each invocation of the saving commands.")
310
311 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
312   "*A function generating a file name to save articles in Rmail format.
313 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
314
315 (defvar gnus-mail-save-name (function gnus-plain-save-name)
316   "*A function generating a file name to save articles in Unix mail format.
317 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
318
319 (defvar gnus-folder-save-name (function gnus-folder-save-name)
320   "*A function generating a file name to save articles in MH folder.
321 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
322
323 (defvar gnus-file-save-name (function gnus-numeric-save-name)
324   "*A function generating a file name to save articles in article format.
325 The function is called with NEWSGROUP, HEADERS, and optional
326 LAST-FILE.")
327
328 (defvar gnus-split-methods 
329   '((gnus-article-archive-name))
330   "*Variable used to suggest where articles are to be saved.
331 For instance, if you would like to save articles related to Gnus in
332 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
333 you could set this variable to something like:
334
335  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
336    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
337
338 This variable is an alist where the where the key is the match and the
339 value is a list of possible files to save in if the match is non-nil.
340
341 If the match is a string, it is used as a regexp match on the
342 article.  If the match is a symbol, that symbol will be funcalled  
343 from the buffer of the article to be saved with the newsgroup as the
344 parameter.  If it is a list, it will be evaled in the same buffer.
345
346 If this form or function returns a string, this string will be used as
347 a possible file name; and if it returns a non-nil list, that list will
348 be used as possible file names.")
349
350 (defvar gnus-save-score nil
351   "*If non-nil, save group scoring info.")
352
353 (defvar gnus-use-adaptive-scoring nil
354   "*If non-nil, use some adaptive scoring scheme.")
355
356 (defvar gnus-use-cache nil
357   "*If nil, Gnus will ignore the article cache.
358 If `passive', it will allow entering (and reading) articles
359 explicitly entered into the cache.  If anything else, use the
360 cache to the full extent of the law.")
361
362 (defvar gnus-keep-backlog nil
363   "*If non-nil, Gnus will keep read articles for later re-retrieval.
364 If it is a number N, then Gnus will only keep the last N articles
365 read.  If it is neither nil nor a number, Gnus will keep all read
366 articles.  This is not a good idea.")
367
368 (defvar gnus-use-nocem nil
369   "*If non-nil, Gnus will read NoCeM cancel messages.")
370
371 (defvar gnus-use-demon nil
372   "If non-nil, Gnus might use some demons.")
373
374 (defvar gnus-use-scoring t
375   "*If non-nil, enable scoring.")
376
377 (defvar gnus-use-picon nil
378   "*If non-nil, display picons.")
379
380 (defvar gnus-fetch-old-headers nil
381   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
382 If an unread article in the group refers to an older, already read (or
383 just marked as read) article, the old article will not normally be
384 displayed in the Summary buffer.  If this variable is non-nil, Gnus
385 will attempt to grab the headers to the old articles, and thereby
386 build complete threads.  If it has the value `some', only enough
387 headers to connect otherwise loose threads will be displayed.
388 This variable can also be a number.  In that case, no more than that
389 number of old headers will be fetched. 
390
391 The server has to support NOV for any of this to work.")
392
393 ;see gnus-cus.el
394 ;(defvar gnus-visual t
395 ;  "*If non-nil, will do various highlighting.
396 ;If nil, no mouse highlights (or any other highlights) will be
397 ;performed.  This might speed up Gnus some when generating large group
398 ;and summary buffers.")
399
400 (defvar gnus-novice-user t
401   "*Non-nil means that you are a usenet novice.
402 If non-nil, verbose messages may be displayed and confirmations may be
403 required.")
404
405 (defvar gnus-expert-user nil
406   "*Non-nil means that you will never be asked for confirmation about anything.
407 And that means *anything*.")
408
409 (defvar gnus-verbose 7
410   "*Integer that says how verbose Gnus should be.
411 The higher the number, the more messages Gnus will flash to say what
412 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
413 display most important messages; and at ten, Gnus will keep on
414 jabbering all the time.")
415
416 (defvar gnus-keep-same-level nil
417   "*Non-nil means that the next newsgroup after the current will be on the same level.
418 When you type, for instance, `n' after reading the last article in the
419 current newsgroup, you will go to the next newsgroup.  If this variable
420 is nil, the next newsgroup will be the next from the group
421 buffer. 
422 If this variable is non-nil, Gnus will either put you in the
423 next newsgroup with the same level, or, if no such newsgroup is
424 available, the next newsgroup with the lowest possible level higher
425 than the current level.
426 If this variable is `best', Gnus will make the next newsgroup the one
427 with the best level.")
428
429 (defvar gnus-summary-make-false-root 'adopt
430   "*nil means that Gnus won't gather loose threads.
431 If the root of a thread has expired or been read in a previous
432 session, the information necessary to build a complete thread has been
433 lost.  Instead of having many small sub-threads from this original thread
434 scattered all over the summary buffer, Gnus can gather them. 
435
436 If non-nil, Gnus will try to gather all loose sub-threads from an
437 original thread into one large thread.
438
439 If this variable is non-nil, it should be one of `none', `adopt',
440 `dummy' or `empty'.
441
442 If this variable is `none', Gnus will not make a false root, but just
443 present the sub-threads after another.
444 If this variable is `dummy', Gnus will create a dummy root that will
445 have all the sub-threads as children.
446 If this variable is `adopt', Gnus will make one of the \"children\"
447 the parent and mark all the step-children as such.
448 If this variable is `empty', the \"children\" are printed with empty
449 subject fields.  (Or rather, they will be printed with a string
450 given by the `gnus-summary-same-subject' variable.)")
451
452 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
453   "*A regexp to match subjects to be excluded from loose thread gathering.
454 As loose thread gathering is done on subjects only, that means that
455 there can be many false gatherings performed.  By rooting out certain
456 common subjects, gathering might become saner.")
457
458 (defvar gnus-summary-gather-subject-limit nil
459   "*Maximum length of subject comparisons when gathering loose threads.
460 Use nil to compare full subjects.  Setting this variable to a low
461 number will help gather threads that have been corrupted by
462 newsreaders chopping off subject lines, but it might also mean that
463 unrelated articles that have subject that happen to begin with the
464 same few characters will be incorrectly gathered.
465
466 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
467 comparing subjects.")
468
469 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
470 (defvar gnus-summary-same-subject ""
471   "*String indicating that the current article has the same subject as the previous.
472 This variable will only be used if the value of
473 `gnus-summary-make-false-root' is `empty'.")
474
475 (defvar gnus-summary-goto-unread t
476   "*If non-nil, marking commands will go to the next unread article.")
477
478 (defvar gnus-group-goto-unread t
479   "*If non-nil, movement commands will go to the next unread and subscribed group.")
480
481 (defvar gnus-check-new-newsgroups t
482   "*Non-nil means that Gnus will add new newsgroups at startup.
483 If this variable is `ask-server', Gnus will ask the server for new
484 groups since the last time it checked.  This means that the killed list
485 is no longer necessary, so you could set `gnus-save-killed-list' to
486 nil. 
487
488 A variant is to have this variable be a list of select methods.  Gnus
489 will then use the `ask-server' method on all these select methods to
490 query for new groups from all those servers.
491
492 Eg.
493   (setq gnus-check-new-newsgroups 
494         '((nntp \"some.server\") (nntp \"other.server\")))
495
496 If this variable is nil, then you have to tell Gnus explicitly to
497 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
498
499 (defvar gnus-check-bogus-newsgroups nil
500   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
501 If this variable is nil, then you have to tell Gnus explicitly to
502 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
503
504 (defvar gnus-read-active-file t
505   "*Non-nil means that Gnus will read the entire active file at startup.
506 If this variable is nil, Gnus will only know about the groups in your
507 `.newsrc' file.
508
509 If this variable is `some', Gnus will try to only read the relevant
510 parts of the active file from the server.  Not all servers support
511 this, and it might be quite slow with other servers, but this should
512 generally be faster than both the t and nil value.
513
514 If you set this variable to nil or `some', you probably still want to
515 be told about new newsgroups that arrive.  To do that, set
516 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
517 properly with all servers.")
518
519 (defvar gnus-level-subscribed 5
520   "*Groups with levels less than or equal to this variable are subscribed.")
521
522 (defvar gnus-level-unsubscribed 7
523   "*Groups with levels less than or equal to this variable are unsubscribed.
524 Groups with levels less than `gnus-level-subscribed', which should be
525 less than this variable, are subscribed.")
526
527 (defvar gnus-level-zombie 8
528   "*Groups with this level are zombie groups.")
529
530 (defvar gnus-level-killed 9
531   "*Groups with this level are killed.")
532
533 (defvar gnus-level-default-subscribed 3
534   "*New subscribed groups will be subscribed at this level.")
535
536 (defvar gnus-level-default-unsubscribed 6
537   "*New unsubscribed groups will be unsubscribed at this level.")
538
539 (defvar gnus-activate-level (1+ gnus-level-subscribed)
540   "*Groups higher than this level won't be activated on startup.
541 Setting this variable to something log might save lots of time when
542 you have many groups that you aren't interested in.")
543
544 (defvar gnus-activate-foreign-newsgroups 4
545   "*If nil, Gnus will not check foreign newsgroups at startup.
546 If it is non-nil, it should be a number between one and nine.  Foreign
547 newsgroups that have a level lower or equal to this number will be
548 activated on startup.  For instance, if you want to active all
549 subscribed newsgroups, but not the rest, you'd set this variable to 
550 `gnus-level-subscribed'.
551
552 If you subscribe to lots of newsgroups from different servers, startup
553 might take a while.  By setting this variable to nil, you'll save time,
554 but you won't be told how many unread articles there are in the
555 groups.")
556
557 (defvar gnus-save-newsrc-file t
558   "*Non-nil means that Gnus will save the `.newsrc' file.
559 Gnus always saves its own startup file, which is called
560 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
561 be readily understood by other newsreaders.  If you don't plan on
562 using other newsreaders, set this variable to nil to save some time on
563 exit.")
564
565 (defvar gnus-save-killed-list t
566   "*If non-nil, save the list of killed groups to the startup file.
567 This will save both time (when starting and quitting) and space (both
568 memory and disk), but it will also mean that Gnus has no record of
569 which groups are new and which are old, so the automatic new
570 newsgroups subscription methods become meaningless.  You should always
571 set `gnus-check-new-newsgroups' to `ask-server' or nil if you set this
572 variable to nil.")
573
574 (defvar gnus-interactive-catchup t
575   "*If non-nil, require your confirmation when catching up a group.")
576
577 (defvar gnus-interactive-post t
578   "*If non-nil, group name will be asked for when posting.")
579
580 (defvar gnus-interactive-exit t
581   "*If non-nil, require your confirmation when exiting Gnus.")
582
583 (defvar gnus-kill-killed t
584   "*If non-nil, Gnus will apply kill files to already killed articles.
585 If it is nil, Gnus will never apply kill files to articles that have
586 already been through the scoring process, which might very well save lots
587 of time.")
588
589 (defvar gnus-extract-address-components 'gnus-extract-address-components
590   "*Function for extracting address components from a From header.
591 Two pre-defined function exist: `gnus-extract-address-components',
592 which is the default, quite fast, and too simplistic solution, and
593 `mail-extract-address-components', which works much better, but is
594 slower.")
595
596 (defvar gnus-summary-default-score 0
597   "*Default article score level.
598 If this variable is nil, scoring will be disabled.")
599
600 (defvar gnus-summary-zcore-fuzz 0
601   "*Fuzziness factor for the zcore in the summary buffer.
602 Articles with scores closer than this to `gnus-summary-default-score'
603 will not be marked.")
604
605 (defvar gnus-simplify-subject-fuzzy-regexp nil
606   "*Strings to be removed when doing fuzzy matches.
607 This can either be a regular expression or list of regular expressions
608 that will be removed from subject strings if fuzzy subject
609 simplification is selected.")
610
611 (defvar gnus-permanently-visible-groups nil
612   "*Regexp to match groups that should always be listed in the group buffer.
613 This means that they will still be listed when there are no unread
614 articles in the groups.")
615
616 (defvar gnus-group-default-list-level gnus-level-subscribed
617   "*Default listing level. 
618 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
619
620 (defvar gnus-group-use-permanent-levels nil
621   "*If non-nil, once you set a level, Gnus will use this level.")
622
623 (defvar gnus-show-mime nil
624   "*If non-nil, do mime processing of articles.
625 The articles will simply be fed to the function given by
626 `gnus-show-mime-method'.")
627
628 (defvar gnus-strict-mime t
629   "*If nil, decode MIME header even if there is not Mime-Version field.")
630  
631 (defvar gnus-show-mime-method 'metamail-buffer
632   "*Function to process a MIME message.
633 The function is called from the article buffer.")
634
635 (defvar gnus-decode-encoded-word-method (lambda ())
636   "*Function to decode a MIME encoded-words.
637 The function is called from the article buffer.")
638  
639 (defvar gnus-show-threads t
640   "*If non-nil, display threads in summary mode.")
641
642 (defvar gnus-thread-hide-subtree nil
643   "*If non-nil, hide all threads initially.
644 If threads are hidden, you have to run the command
645 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
646 to expose hidden threads.")
647
648 (defvar gnus-thread-hide-killed t
649   "*If non-nil, hide killed threads automatically.")
650
651 (defvar gnus-thread-ignore-subject nil
652   "*If non-nil, ignore subjects and do all threading based on the Reference header.
653 If nil, which is the default, articles that have different subjects
654 from their parents will start separate threads.")
655
656 (defvar gnus-thread-operation-ignore-subject t
657   "*If non-nil, subjects will be ignored when doing thread commands.
658 This affects commands like `gnus-summary-kill-thread' and
659 `gnus-summary-lower-thread'.  
660
661 If this variable is nil, articles in the same thread with different
662 subjects will not be included in the operation in question.  If this
663 variable is `fuzzy', only articles that have subjects that are fuzzily
664 equal will be included.")
665
666 (defvar gnus-thread-indent-level 4
667   "*Number that says how much each sub-thread should be indented.")
668
669 (defvar gnus-ignored-newsgroups 
670   (purecopy (mapconcat 'identity
671                        '("^to\\."       ; not "real" groups
672                          "^[0-9. \t]+ " ; all digits in name
673                          "[][\"#'()]"   ; bogus characters
674                          )
675                        "\\|"))
676   "*A regexp to match uninteresting newsgroups in the active file.
677 Any lines in the active file matching this regular expression are
678 removed from the newsgroup list before anything else is done to it,
679 thus making them effectively non-existent.")
680
681 (defvar gnus-ignored-headers
682   "^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:"
683   "*All headers that match this regexp will be hidden.
684 This variable can also be a list of regexps of headers to be ignored.
685 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
686
687 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Resent-"
688   "*All headers that do not match this regexp will be hidden.
689 This variable can also be a list of regexp of headers to remain visible.
690 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
691
692 (defvar gnus-sorted-header-list
693   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:" 
694     "^Cc:" "^Date:" "^Organization:")
695   "*This variable is a list of regular expressions.
696 If it is non-nil, headers that match the regular expressions will
697 be placed first in the article buffer in the sequence specified by
698 this list.")
699
700 (defvar gnus-show-all-headers nil
701   "*If non-nil, don't hide any headers.")
702
703 (defvar gnus-save-all-headers t
704   "*If non-nil, don't remove any headers before saving.")
705
706 (defvar gnus-saved-headers gnus-visible-headers
707   "*Headers to keep if `gnus-save-all-headers' is nil.
708 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
709 If that variable is nil, however, all headers that match this regexp
710 will be kept while the rest will be deleted before saving.")
711
712 (defvar gnus-inhibit-startup-message nil
713   "*If non-nil, the startup message will not be displayed.")
714
715 (defvar gnus-signature-separator "^-- *$"
716   "Regexp matching signature separator.")
717
718 (defvar gnus-auto-extend-newsgroup t
719   "*If non-nil, extend newsgroup forward and backward when requested.")
720
721 (defvar gnus-auto-select-first t
722   "*If nil, don't select the first unread article when entering a group.
723 If this variable is `best', select the highest-scored unread article
724 in the group.  If neither nil nor `best', select the first unread
725 article.
726
727 If you want to prevent automatic selection of the first unread article
728 in some newsgroups, set the variable to nil in
729 `gnus-select-group-hook'.") 
730
731 (defvar gnus-auto-select-next t
732   "*If non-nil, offer to go to the next group from the end of the previous.
733 If the value is t and the next newsgroup is empty, Gnus will exit
734 summary mode and go back to group mode.  If the value is neither nil
735 nor t, Gnus will select the following unread newsgroup.  In
736 particular, if the value is the symbol `quietly', the next unread
737 newsgroup will be selected without any confirmation, and if it is
738 `almost-quietly', the next group will be selected without any
739 confirmation if you are located on the last article in the group.")
740
741 (defvar gnus-auto-select-same nil
742   "*If non-nil, select the next article with the same subject.")
743
744 (defvar gnus-summary-check-current nil
745   "*If non-nil, consider the current article when moving.
746 The \"unread\" movement commands will stay on the same line if the
747 current article is unread.")
748
749 (defvar gnus-auto-center-summary t
750   "*If non-nil, always center the current summary buffer.")
751
752 (defvar gnus-break-pages t
753   "*If non-nil, do page breaking on articles.
754 The page delimiter is specified by the `gnus-page-delimiter'
755 variable.")
756
757 (defvar gnus-page-delimiter "^\^L"
758   "*Regexp describing what to use as article page delimiters.
759 The default value is \"^\^L\", which is a form linefeed at the
760 beginning of a line.")
761
762 (defvar gnus-use-full-window t
763   "*If non-nil, use the entire Emacs screen.")
764
765 (defvar gnus-window-configuration nil
766   "Obsolete variable.  See `gnus-buffer-configuration'.")
767
768 (defvar gnus-buffer-configuration
769   '((group
770      (vertical 1.0 
771                (group 1.0 point) 
772                (if gnus-carpal (group-carpal 4))))
773     (summary
774      (vertical 1.0
775                (summary 1.0 point)
776                (if gnus-carpal (summary-carpal 4))))
777     (article
778      (vertical 1.0
779                (if gnus-use-picon
780                    '(horizontal 0.25
781                                 (summary 1.0 point)
782                                 (picon 10))
783                  '(summary 0.25 point))
784                (if gnus-carpal (summary-carpal 4)) 
785                (article 1.0)))
786     (server
787      (vertical 1.0
788                (server 1.0 point)
789                (if gnus-carpal (server-carpal 2))))
790     (browse
791      (vertical 1.0
792                (browse 1.0 point)
793                (if gnus-carpal (browse-carpal 2))))
794     (group-mail
795      (vertical 1.0
796                (mail 1.0 point)))
797     (summary-mail
798      (vertical 1.0
799                (mail 1.0 point)))
800     (summary-reply
801      (vertical 1.0
802                (article 0.5)
803                (mail 1.0 point)))
804     (info
805      (vertical 1.0
806                (nil 1.0 point)))
807     (summary-faq
808      (vertical 1.0
809                (summary 0.25)
810                (faq 1.0 point)))
811     (edit-group
812      (vertical 1.0
813                (group 0.5)
814                (edit-group 1.0 point)))
815     (edit-server
816      (vertical 1.0
817                (server 0.5)
818                (edit-server 1.0 point)))
819     (edit-score
820      (vertical 1.0
821                (summary 0.25)
822                (edit-score 1.0 point)))
823     (post
824      (vertical 1.0
825                (post 1.0 point)))
826     (reply
827      (vertical 1.0
828                (article 0.5)
829                (mail 1.0 point)))
830     (mail-forward
831      (vertical 1.0
832                (mail 1.0 point)))
833     (post-forward
834      (vertical 1.0
835                (post 1.0 point)))
836     (reply-yank
837      (vertical 1.0
838                (mail 1.0 point)))
839     (mail-bounce
840      (vertical 1.0
841                (article 0.5)
842                (mail 1.0 point)))
843     (draft
844      (vertical 1.0
845                (draft 1.0 point)))
846     (pipe
847      (vertical 1.0
848                (summary 0.25 point) 
849                (if gnus-carpal (summary-carpal 4)) 
850                ("*Shell Command Output*" 1.0)))
851     (followup
852      (vertical 1.0
853                (article 0.5)
854                (post 1.0 point)))
855     (followup-yank
856      (vertical 1.0
857                (post 1.0 point))))
858   "Window configuration for all possible Gnus buffers.
859 This variable is a list of lists.  Each of these lists has a NAME and
860 a RULE.  The NAMEs are commonsense names like `group', which names a
861 rule used when displaying the group buffer; `summary', which names a
862 rule for what happens when you enter a group and do not display an
863 article buffer; and so on.  See the value of this variable for a
864 complete list of NAMEs.
865
866 Each RULE is a list of vectors.  The first element in this vector is
867 the name of the buffer to be displayed; the second element is the
868 percentage of the screen this buffer is to occupy (a number in the
869 0.0-0.99 range); the optional third element is `point', which should
870 be present to denote which buffer point is to go to after making this
871 buffer configuration.")
872
873 (defvar gnus-window-to-buffer
874   '((group . gnus-group-buffer)
875     (summary . gnus-summary-buffer)
876     (article . gnus-article-buffer)
877     (server . gnus-server-buffer)
878     (browse . "*Gnus Browse Server*")
879     (edit-group . gnus-group-edit-buffer)
880     (edit-server . gnus-server-edit-buffer)
881     (group-carpal . gnus-carpal-group-buffer)
882     (summary-carpal . gnus-carpal-summary-buffer)
883     (server-carpal . gnus-carpal-server-buffer)
884     (browse-carpal . gnus-carpal-browse-buffer)
885     (edit-score . gnus-score-edit-buffer)
886     (mail . gnus-mail-buffer)
887     (post . gnus-post-news-buffer)
888     (faq . gnus-faq-buffer)
889     (picon . gnus-picon-buffer)
890     (draft . gnus-draft-buffer))
891   "Mapping from short symbols to buffer names or buffer variables.")
892
893 (defvar gnus-carpal nil
894   "*If non-nil, display clickable icons.")
895
896 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
897   "*Function called with a group name when new group is detected.
898 A few pre-made functions are supplied: `gnus-subscribe-randomly'
899 inserts new groups at the beginning of the list of groups;
900 `gnus-subscribe-alphabetically' inserts new groups in strict
901 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
902 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
903 for your decision; `gnus-subscribe-killed' kills all new groups.")
904
905 ;; Suggested by a bug report by Hallvard B Furuseth.
906 ;; <h.b.furuseth@usit.uio.no>. 
907 (defvar gnus-subscribe-options-newsgroup-method
908   (function gnus-subscribe-alphabetically)
909   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
910 If, for instance, you want to subscribe to all newsgroups in the
911 \"no\" and \"alt\" hierarchies, you'd put the following in your
912 .newsrc file:
913
914 options -n no.all alt.all
915
916 Gnus will the subscribe all new newsgroups in these hierarchies with
917 the subscription method in this variable.")
918
919 (defvar gnus-subscribe-hierarchical-interactive nil
920   "*If non-nil, Gnus will offer to subscribe hierarchically.
921 When a new hierarchy appears, Gnus will ask the user:
922
923 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
924
925 If the user pressed `d', Gnus will descend the hierarchy, `y' will
926 subscribe to all newsgroups in the hierarchy and `s' will skip this
927 hierarchy in its entirety.")
928
929 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
930   "*Function used for sorting the group buffer.
931 This function will be called with group info entries as the arguments
932 for the groups to be sorted.  Pre-made functions include
933 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
934 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
935 `gnus-group-sort-by-rank'.  
936
937 This variable can also be a list of sorting functions.  In that case,
938 the most significant sort function should be the last function in the
939 list.")
940
941 ;; Mark variables suggested by Thomas Michanek
942 ;; <Thomas.Michanek@telelogic.se>. 
943 (defvar gnus-unread-mark ? 
944   "*Mark used for unread articles.")
945 (defvar gnus-ticked-mark ?!
946   "*Mark used for ticked articles.")
947 (defvar gnus-dormant-mark ??
948   "*Mark used for dormant articles.")
949 (defvar gnus-del-mark ?r
950   "*Mark used for del'd articles.")
951 (defvar gnus-read-mark ?R
952   "*Mark used for read articles.")
953 (defvar gnus-expirable-mark ?E
954   "*Mark used for expirable articles.")
955 (defvar gnus-killed-mark ?K
956   "*Mark used for killed articles.")
957 (defvar gnus-souped-mark ?F
958   "*Mark used for killed articles.")
959 (defvar gnus-kill-file-mark ?X
960   "*Mark used for articles killed by kill files.")
961 (defvar gnus-low-score-mark ?Y
962   "*Mark used for articles with a low score.")
963 (defvar gnus-catchup-mark ?C
964   "*Mark used for articles that are caught up.")
965 (defvar gnus-replied-mark ?A
966   "*Mark used for articles that have been replied to.")
967 (defvar gnus-cached-mark ?*
968   "*Mark used for articles that are in the cache.")
969 (defvar gnus-saved-mark ?S
970   "*Mark used for articles that have been saved to.")
971 (defvar gnus-process-mark ?# 
972   "*Process mark.")
973 (defvar gnus-ancient-mark ?O
974   "*Mark used for ancient articles.")
975 (defvar gnus-canceled-mark ?G
976   "*Mark used for canceled articles.")
977 (defvar gnus-score-over-mark ?+
978   "*Score mark used for articles with high scores.")
979 (defvar gnus-score-below-mark ?-
980   "*Score mark used for articles with low scores.")
981 (defvar gnus-empty-thread-mark ? 
982   "*There is no thread under the article.")
983 (defvar gnus-not-empty-thread-mark ?=
984   "*There is a thread under the article.")
985
986 (defvar gnus-view-pseudo-asynchronously nil
987   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
988
989 (defvar gnus-view-pseudos nil
990   "*If `automatic', pseudo-articles will be viewed automatically.
991 If `not-confirm', pseudos will be viewed automatically, and the user
992 will not be asked to confirm the command.")
993
994 (defvar gnus-view-pseudos-separately t
995   "*If non-nil, one pseudo-article will be created for each file to be viewed.
996 If nil, all files that use the same viewing command will be given as a
997 list of parameters to that command.")
998
999 (defvar gnus-insert-pseudo-articles t
1000   "*If non-nil, insert pseudo-articles when decoding articles.")
1001
1002 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)\n"
1003   "*Format of group lines.
1004 It works along the same lines as a normal formatting string,
1005 with some simple extensions.
1006
1007 %M    Only marked articles (character, \"*\" or \" \")
1008 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1009 %L    Level of subscribedness (integer)
1010 %N    Number of unread articles (integer)
1011 %I    Number of dormant articles (integer)
1012 %i    Number of ticked and dormant (integer)
1013 %T    Number of ticked articles (integer)
1014 %R    Number of read articles (integer)
1015 %t    Total number of articles (integer)
1016 %y    Number of unread, unticked articles (integer)
1017 %G    Group name (string)
1018 %g    Qualified group name (string)
1019 %D    Group description (string)
1020 %s    Select method (string)
1021 %o    Moderated group (char, \"m\")
1022 %p    Process mark (char)
1023 %O    Moderated group (string, \"(m)\" or \"\")
1024 %P    Topic indentation (string)
1025 %n    Select from where (string)
1026 %z    A string that look like `<%s:%n>' if a foreign select method is used
1027 %u    User defined specifier.  The next character in the format string should
1028       be a letter.  Gnus will call the function gnus-user-format-function-X,
1029       where X is the letter following %u.  The function will be passed the
1030       current header as argument.  The function should return a string, which
1031       will be inserted into the buffer just like information from any other
1032       group specifier.
1033
1034 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1035 the mouse point move inside the area.  There can only be one such area.
1036
1037 Note that this format specification is not always respected.  For
1038 reasons of efficiency, when listing killed groups, this specification
1039 is ignored altogether.  If the spec is changed considerably, your
1040 output may end up looking strange when listing both alive and killed
1041 groups.
1042
1043 If you use %o or %O, reading the active file will be slower and quite
1044 a bit of extra memory will be used. %D will also worsen performance.
1045 Also note that if you change the format specification to include any
1046 of these specs, you must probably re-start Gnus to see them go into
1047 effect.") 
1048
1049 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1050   "*The format specification of the lines in the summary buffer.
1051
1052 It works along the same lines as a normal formatting string,
1053 with some simple extensions.
1054
1055 %N   Article number, left padded with spaces (string)
1056 %S   Subject (string)
1057 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1058 %n   Name of the poster (string)
1059 %a   Extracted name of the poster (string)
1060 %A   Extracted address of the poster (string)
1061 %F   Contents of the From: header (string)
1062 %x   Contents of the Xref: header (string)
1063 %D   Date of the article (string)
1064 %d   Date of the article (string) in DD-MMM format
1065 %M   Message-id of the article (string)
1066 %r   References of the article (string)
1067 %c   Number of characters in the article (integer)
1068 %L   Number of lines in the article (integer)
1069 %I   Indentation based on thread level (a string of spaces)
1070 %T   A string with two possible values: 80 spaces if the article
1071      is on thread level two or larger and 0 spaces on level one
1072 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1073 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1074 %[   Opening bracket (character, \"[\" or \"<\")
1075 %]   Closing bracket (character, \"]\" or \">\")
1076 %>   Spaces of length thread-level (string)
1077 %<   Spaces of length (- 20 thread-level) (string)
1078 %i   Article score (number)
1079 %z   Article zcore (character)
1080 %t   Number of articles under the current thread (number).
1081 %e   Whether the thread is empty or not (character).
1082 %u   User defined specifier.  The next character in the format string should
1083      be a letter.  Gnus will call the function gnus-user-format-function-X,
1084      where X is the letter following %u.  The function will be passed the
1085      current header as argument.  The function should return a string, which
1086      will be inserted into the summary just like information from any other
1087      summary specifier.
1088
1089 Text between %( and %) will be highlighted with `gnus-mouse-face'
1090 when the mouse point is placed inside the area.  There can only be one
1091 such area.
1092
1093 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1094 with care.  For reasons of efficiency, Gnus will compute what column
1095 these characters will end up in, and \"hard-code\" that.  This means that
1096 it is illegal to have these specs after a variable-length spec.  Well,
1097 you might not be arrested, but your summary buffer will look strange,
1098 which is bad enough.
1099
1100 The smart choice is to have these specs as for to the left as
1101 possible. 
1102
1103 This restriction may disappear in later versions of Gnus.")
1104
1105 (defvar gnus-summary-dummy-line-format 
1106   "*  %(:                          :%) %S\n"
1107   "*The format specification for the dummy roots in the summary buffer.
1108 It works along the same lines as a normal formatting string,
1109 with some simple extensions.
1110
1111 %S  The subject")
1112
1113 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1114   "*The format specification for the summary mode line.
1115 It works along the same lines as a normal formatting string,
1116 with some simple extensions:
1117
1118 %G  Group name
1119 %p  Unprefixed group name
1120 %A  Current article number
1121 %V  Gnus version
1122 %U  Number of unread articles in the group
1123 %e  Number of unselected articles in the group
1124 %Z  A string with unread/unselected article counts
1125 %g  Shortish group name
1126 %S  Subject of the current article
1127 %u  User-defined spec
1128 %s  Current score file name
1129 %d  Number of dormant articles
1130 %r  Number of articles that have been marked as read in this session
1131 %E  Number of articles expunged by the score files")
1132
1133 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1134   "*The format specification for the article mode line.
1135 See `gnus-summary-mode-line-format' for a closer description.")
1136
1137 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1138   "*The format specification for the group mode line.
1139 It works along the same lines as a normal formatting string,
1140 with some simple extensions:
1141
1142 %S   The native news server.
1143 %M   The native select method.")
1144
1145 (defvar gnus-valid-select-methods
1146   '(("nntp" post address prompt-address)
1147     ("nnspool" post)
1148     ("nnvirtual" post-mail virtual prompt-address) 
1149     ("nnmbox" mail respool) 
1150     ("nnml" mail respool)
1151     ("nnmh" mail respool) 
1152     ("nndir" post-mail prompt-address address)
1153     ("nneething" none prompt-address)
1154     ("nndoc" none prompt-address) 
1155     ("nnbabyl" mail respool) 
1156     ("nnkiboze" post virtual) 
1157     ("nnsoup" post-mail)
1158     ("nnfolder" mail respool))
1159   "An alist of valid select methods.
1160 The first element of each list lists should be a string with the name
1161 of the select method.  The other elements may be be the category of
1162 this method (ie. `post', `mail', `none' or whatever) or other
1163 properties that this method has (like being respoolable).
1164 If you implement a new select method, all you should have to change is
1165 this variable.  I think.")
1166
1167 (defvar gnus-updated-mode-lines '(group article summary)
1168   "*List of buffers that should update their mode lines.
1169 The list may contain the symbols `group', `article' and `summary'.  If
1170 the corresponding symbol is present, Gnus will keep that mode line
1171 updated with information that may be pertinent. 
1172 If this variable is nil, screen refresh may be quicker.")
1173
1174 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1175 (defvar gnus-mode-non-string-length nil
1176   "*Max length of mode-line non-string contents.
1177 If this is nil, Gnus will take space as is needed, leaving the rest
1178 of the modeline intact.")
1179
1180 ;see gnus-cus.el
1181 ;(defvar gnus-mouse-face 'highlight
1182 ;  "*Face used for mouse highlighting in Gnus.
1183 ;No mouse highlights will be done if `gnus-visual' is nil.")
1184
1185 (defvar gnus-summary-mark-below nil
1186   "*Mark all articles with a score below this variable as read.
1187 This variable is local to each summary buffer and usually set by the
1188 score file.")  
1189
1190 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1191   "*List of functions used for sorting articles in the summary buffer.")
1192
1193 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1194   "*List of functions used for sorting threads in the summary buffer.
1195 By default, threads are sorted by article number.
1196
1197 Each function takes two threads and return non-nil if the first thread
1198 should be sorted before the other.  If you use more than one function,
1199 the primary sort function should be the last.
1200
1201 Ready-mady functions include `gnus-thread-sort-by-number',
1202 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1203 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1204 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1205
1206 (defvar gnus-thread-score-function '+
1207   "*Function used for calculating the total score of a thread.
1208
1209 The function is called with the scores of the article and each
1210 subthread and should then return the score of the thread.
1211
1212 Some functions you can use are `+', `max', or `min'.")
1213
1214 (defvar gnus-summary-expunge-below nil
1215   "All articles that have a score less than this variable will be expunged.")
1216
1217 (defvar gnus-thread-expunge-below nil
1218   "All threads that have a total score less than this variable will be expunged.
1219 See `gnus-thread-score-function' for en explanation of what a 
1220 \"thread score\" is.")
1221
1222 (defvar gnus-auto-subscribed-groups 
1223   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1224   "*All new groups that match this regexp will be subscribed automatically.
1225 Note that this variable only deals with new groups.  It has no effect
1226 whatsoever on old groups.")
1227
1228 (defvar gnus-options-subscribe nil
1229   "*All new groups matching this regexp will be subscribed unconditionally.
1230 Note that this variable deals only with new newsgroups.  This variable
1231 does not affect old newsgroups.")
1232
1233 (defvar gnus-options-not-subscribe nil
1234   "*All new groups matching this regexp will be ignored.
1235 Note that this variable deals only with new newsgroups.  This variable
1236 does not affect old (already subscribed) newsgroups.")
1237
1238 (defvar gnus-auto-expirable-newsgroups nil
1239   "*Groups in which to automatically mark read articles as expirable.
1240 If non-nil, this should be a regexp that should match all groups in
1241 which to perform auto-expiry.  This only makes sense for mail groups.")
1242
1243 (defvar gnus-total-expirable-newsgroups nil
1244   "*Groups in which to perform expiry of all read articles.
1245 Use with extreme caution.  All groups that match this regexp will be
1246 expiring - which means that all read articles will be deleted after
1247 (say) one week.  (This only goes for mail groups and the like, of
1248 course.)")
1249
1250 (defvar gnus-hidden-properties '(invisible t intangible t)
1251   "Property list to use for hiding text.")
1252
1253 (defvar gnus-modtime-botch nil
1254   "*Non-nil means .newsrc should be deleted prior to save.  Its use is
1255 due to the bogus appearance that .newsrc was modified on disc.")
1256
1257 ;; Hooks.
1258
1259 (defvar gnus-group-mode-hook nil
1260   "*A hook for Gnus group mode.")
1261
1262 (defvar gnus-summary-mode-hook nil
1263   "*A hook for Gnus summary mode.
1264 This hook is run before any variables are set in the summary buffer.")
1265
1266 (defvar gnus-article-mode-hook nil
1267   "*A hook for Gnus article mode.")
1268
1269 (defun gnus-summary-prepare-exit-hook nil
1270   "*A hook called when preparing to exit from the summary buffer.
1271 It calls `gnus-summary-expire-articles' by default.")
1272 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1273
1274 (defun gnus-summary-exit-hook nil
1275   "*A hook called on exit from the summary buffer.")
1276
1277 (defvar gnus-open-server-hook nil
1278   "*A hook called just before opening connection to the news server.")
1279
1280 (defvar gnus-load-hook nil
1281   "*A hook run while Gnus is loaded.")
1282
1283 (defvar gnus-startup-hook nil
1284   "*A hook called at startup.
1285 This hook is called after Gnus is connected to the NNTP server.")
1286
1287 (defvar gnus-get-new-news-hook nil
1288   "*A hook run just before Gnus checks for new news.")
1289
1290 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1291   "*A function that is called to generate the group buffer.
1292 The function is called with three arguments: The first is a number;
1293 all group with a level less or equal to that number should be listed,
1294 if the second is non-nil, empty groups should also be displayed.  If
1295 the third is non-nil, it is a number.  No groups with a level lower
1296 than this number should be displayed.
1297
1298 The only current function implemented is `gnus-group-prepare-flat'.")
1299
1300 (defvar gnus-group-prepare-hook nil
1301   "*A hook called after the group buffer has been generated.
1302 If you want to modify the group buffer, you can use this hook.")
1303
1304 (defvar gnus-summary-prepare-hook nil
1305   "*A hook called after the summary buffer has been generated.
1306 If you want to modify the summary buffer, you can use this hook.")
1307
1308 (defvar gnus-summary-generate-hook nil
1309   "*A hook run just before generating the summary buffer.
1310 This hook is commonly used to customize threading variables and the
1311 like.")
1312
1313 (defvar gnus-article-prepare-hook nil
1314   "*A hook called after an article has been prepared in the article buffer.
1315 If you want to run a special decoding program like nkf, use this hook.")
1316
1317 ;(defvar gnus-article-display-hook nil
1318 ;  "*A hook called after the article is displayed in the article buffer.
1319 ;The hook is designed to change the contents of the article
1320 ;buffer.  Typical functions that this hook may contain are
1321 ;`gnus-article-hide-headers' (hide selected headers),
1322 ;`gnus-article-maybe-highlight' (perform fancy article highlighting), 
1323 ;`gnus-article-hide-signature' (hide signature) and
1324 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1325 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1326 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1327 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1328
1329 (defvar gnus-article-x-face-command
1330   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1331   "String or function to be executed to display an X-Face header.
1332 If it is a string, the command will be executed in a sub-shell
1333 asynchronously.  The compressed face will be piped to this command.") 
1334
1335 (defvar gnus-article-x-face-too-ugly nil
1336   "Regexp matching posters whose face shouldn't be shown automatically.")
1337
1338 (defvar gnus-select-group-hook nil
1339   "*A hook called when a newsgroup is selected.
1340
1341 If you'd like to simplify subjects like the
1342 `gnus-summary-next-same-subject' command does, you can use the
1343 following hook:
1344
1345  (setq gnus-select-group-hook
1346       (list
1347         (lambda ()
1348           (mapcar (lambda (header)
1349                      (mail-header-set-subject
1350                       header
1351                       (gnus-simplify-subject
1352                        (mail-header-subject header) 're-only)))
1353                   gnus-newsgroup-headers))))")
1354
1355 (defvar gnus-select-article-hook
1356   '(gnus-summary-show-thread)
1357   "*A hook called when an article is selected.
1358 The default hook shows conversation thread subtrees of the selected
1359 article automatically using `gnus-summary-show-thread'.")
1360
1361 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1362   "*A hook called to apply kill files to a group.
1363 This hook is intended to apply a kill file to the selected newsgroup.
1364 The function `gnus-apply-kill-file' is called by default.
1365
1366 Since a general kill file is too heavy to use only for a few
1367 newsgroups, I recommend you to use a lighter hook function.  For
1368 example, if you'd like to apply a kill file to articles which contains
1369 a string `rmgroup' in subject in newsgroup `control', you can use the
1370 following hook:
1371
1372  (setq gnus-apply-kill-hook
1373       (list
1374         (lambda ()
1375           (cond ((string-match \"control\" gnus-newsgroup-name)
1376                  (gnus-kill \"Subject\" \"rmgroup\")
1377                  (gnus-expunge \"X\"))))))")
1378
1379 (defvar gnus-visual-mark-article-hook 
1380   (list 'gnus-highlight-selected-summary)
1381   "*Hook run after selecting an article in the summary buffer.
1382 It is meant to be used for highlighting the article in some way.  It
1383 is not run if `gnus-visual' is nil.")
1384
1385 (defun gnus-parse-headers-hook nil
1386   "*A hook called before parsing the headers.")
1387
1388 (defvar gnus-exit-group-hook nil
1389   "*A hook called when exiting (not quitting) summary mode.")
1390
1391 (defvar gnus-suspend-gnus-hook nil
1392   "*A hook called when suspending (not exiting) Gnus.")
1393
1394 (defvar gnus-exit-gnus-hook nil
1395   "*A hook called when exiting Gnus.")
1396
1397 (defvar gnus-save-newsrc-hook nil
1398   "*A hook called before saving any of the newsrc files.")
1399
1400 (defvar gnus-save-quick-newsrc-hook nil
1401   "*A hook called just before saving the quick newsrc file.
1402 Can be used to turn version control on or off.")
1403
1404 (defvar gnus-save-standard-newsrc-hook nil
1405   "*A hook called just before saving the standard newsrc file.
1406 Can be used to turn version control on or off.")
1407
1408 (defvar gnus-summary-update-hook 
1409   (list 'gnus-summary-highlight-line)
1410   "*A hook called when a summary line is changed.
1411 The hook will not be called if `gnus-visual' is nil.
1412
1413 The default function `gnus-summary-highlight-line' will
1414 highlight the line according to the `gnus-summary-highlight'
1415 variable.")
1416
1417 (defvar gnus-mark-article-hook (list 'gnus-summary-mark-unread-as-read)
1418   "*A hook called when an article is selected for the first time.
1419 The hook is intended to mark an article as read (or unread)
1420 automatically when it is selected.")
1421
1422 ;; Remove any hilit infestation.
1423 (add-hook 'gnus-startup-hook
1424           (lambda ()
1425             (remove-hook 'gnus-summary-prepare-hook
1426                          'hilit-rehighlight-buffer-quietly)
1427             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1428             (setq gnus-mark-article-hook '(gnus-summary-mark-unread-as-read))
1429             (remove-hook 'gnus-article-prepare-hook
1430                          'hilit-rehighlight-buffer-quietly)))
1431
1432
1433 \f
1434 ;; Internal variables
1435
1436 (defconst gnus-article-mark-lists 
1437   '((marked . tick) (replied . reply) 
1438     (expirable . expire) (killed . killed)
1439     (bookmarks . bookmark) (dormant . dormant)
1440     (scored . score) (saved . save)))
1441
1442 ;; Avoid highlighting in kill files.
1443 (defvar gnus-summary-inhibit-highlight nil)
1444 (defvar gnus-newsgroup-selected-overlay nil)
1445
1446 (defvar gnus-inhibit-hiding nil)
1447 (defvar gnus-topic-indentation "")
1448 (defvar gnus-inhibit-limiting nil)
1449
1450 (defvar gnus-article-mode-map nil)
1451 (defvar gnus-dribble-buffer nil)
1452 (defvar gnus-headers-retrieved-by nil)
1453 (defvar gnus-article-reply nil)
1454 (defvar gnus-override-method nil)
1455 (defvar gnus-article-check-size nil)
1456
1457 (defvar gnus-nocem-hashtb nil)
1458
1459 (defvar gnus-current-score-file nil)
1460 (defvar gnus-newsgroup-adaptive-score-file nil)
1461 (defvar gnus-scores-exclude-files nil)
1462
1463 (defvar gnus-opened-servers nil)
1464
1465 (defvar gnus-current-move-group nil)
1466
1467 (defvar gnus-newsgroup-dependencies nil)
1468 (defvar gnus-newsgroup-async nil)
1469 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1470
1471 (defvar gnus-newsgroup-adaptive nil)
1472
1473 (defvar gnus-summary-display-table nil)
1474
1475 (defconst gnus-group-line-format-alist
1476   `((?M gnus-tmp-marked-mark ?c)
1477     (?S gnus-tmp-subscribed ?c)
1478     (?L gnus-tmp-level ?d)
1479     (?N gnus-tmp-number ?s)
1480     (?R gnus-tmp-number-of-read ?s)
1481     (?t gnus-tmp-number-total ?d)
1482     (?y gnus-tmp-number-of-unread ?s)
1483     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1484     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1485     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1486            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1487     (?g gnus-tmp-group ?s)
1488     (?G gnus-tmp-qualified-group ?s)
1489     (?c (gnus-group-short-name gnus-tmp-group) ?s)
1490     (?D gnus-tmp-newsgroup-description ?s)
1491     (?o gnus-tmp-moderated ?c)
1492     (?O gnus-tmp-moderated-string ?s)
1493     (?p gnus-tmp-process-marked ?c)
1494     (?s gnus-tmp-news-server ?s)
1495     (?n gnus-tmp-news-method ?s)
1496     (?P gnus-topic-indentation ?s)
1497     (?z gnus-tmp-news-method-string ?s)
1498     (?u gnus-tmp-user-defined ?s)))
1499
1500 (defconst gnus-summary-line-format-alist 
1501   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1502     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1503     (?s gnus-tmp-subject-or-nil ?s)
1504     (?n gnus-tmp-name ?s)
1505     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1506         ?s)
1507     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from)) 
1508             gnus-tmp-from) ?s)
1509     (?F gnus-tmp-from ?s)
1510     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1511     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1512     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1513     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1514     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1515     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1516     (?L gnus-tmp-lines ?d)
1517     (?I gnus-tmp-indentation ?s)
1518     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1519     (?R gnus-tmp-replied ?c)
1520     (?\[ gnus-tmp-opening-bracket ?c)
1521     (?\] gnus-tmp-closing-bracket ?c)
1522     (?\> (make-string gnus-tmp-level ? ) ?s)
1523     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1524     (?i gnus-tmp-score ?d)
1525     (?z gnus-tmp-score-char ?c)
1526     (?U gnus-tmp-unread ?c)
1527     (?t (gnus-summary-number-of-articles-in-thread 
1528          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1529         ?d)
1530     (?e (gnus-summary-number-of-articles-in-thread 
1531          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1532         ?c)
1533     (?u gnus-tmp-user-defined ?s))
1534   "An alist of format specifications that can appear in summary lines,
1535 and what variables they correspond with, along with the type of the
1536 variable (string, integer, character, etc).")
1537
1538 (defconst gnus-summary-dummy-line-format-alist
1539   (` ((?S gnus-tmp-subject ?s)
1540       (?N gnus-tmp-number ?d)
1541       (?u gnus-tmp-user-defined ?s))))
1542
1543 (defconst gnus-summary-mode-line-format-alist 
1544   (` ((?G gnus-tmp-group-name ?s)
1545       (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1546       (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1547       (?A gnus-tmp-article-number ?d)
1548       (?Z gnus-tmp-unread-and-unselected ?s)
1549       (?V gnus-version ?s)
1550       (?U gnus-tmp-unread ?d)
1551       (?S gnus-tmp-subject ?s)
1552       (?e gnus-tmp-unselected ?d)
1553       (?u gnus-tmp-user-defined ?s)
1554       (?d (length gnus-newsgroup-dormant) ?d)
1555       (?t (length gnus-newsgroup-marked) ?d)
1556       (?r (length gnus-newsgroup-reads) ?d)
1557       (?E gnus-newsgroup-expunged-tally ?d)
1558       (?s (gnus-current-score-file-nondirectory) ?s))))
1559
1560 (defconst gnus-article-mode-line-format-alist  
1561   gnus-summary-mode-line-format-alist)
1562
1563 (defconst gnus-group-mode-line-format-alist 
1564   (` ((?S gnus-tmp-news-server ?s)
1565       (?M gnus-tmp-news-method ?s)
1566       (?u gnus-tmp-user-defined ?s))))
1567
1568 (defvar gnus-have-read-active-file nil)
1569
1570 (defconst gnus-maintainer
1571   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1572   "The mail address of the Gnus maintainers.")
1573
1574 (defconst gnus-version "September Gnus v0.23"
1575   "Version number for this version of Gnus.")
1576
1577 (defvar gnus-info-nodes
1578   '((gnus-group-mode            "(gnus)The Group Buffer")
1579     (gnus-summary-mode          "(gnus)The Summary Buffer")
1580     (gnus-article-mode          "(gnus)The Article Buffer"))
1581   "Assoc list of major modes and related Info nodes.")
1582
1583 (defvar gnus-group-buffer "*Group*")
1584 (defvar gnus-summary-buffer "*Summary*")
1585 (defvar gnus-article-buffer "*Article*")
1586 (defvar gnus-server-buffer "*Server*")
1587
1588 (defvar gnus-work-buffer " *gnus work*")
1589
1590 (defvar gnus-original-article-buffer " *Original Article*")
1591 (defvar gnus-original-article nil)
1592
1593 (defvar gnus-buffer-list nil
1594   "Gnus buffers that should be killed on exit.")
1595
1596 (defvar gnus-server-alist nil
1597   "List of available servers.")
1598
1599 (defvar gnus-slave nil
1600   "Whether this Gnus is a slave or not.")
1601
1602 (defvar gnus-variable-list
1603   '(gnus-newsrc-options gnus-newsrc-options-n
1604     gnus-newsrc-last-checked-date 
1605     gnus-newsrc-alist gnus-server-alist
1606     gnus-killed-list gnus-zombie-list
1607     gnus-topic-topology gnus-topic-alist)
1608   "Gnus variables saved in the quick startup file.")
1609
1610 (defvar gnus-newsrc-options nil
1611   "Options line in the .newsrc file.")
1612
1613 (defvar gnus-newsrc-options-n nil
1614   "List of regexps representing groups to be subscribed/ignored unconditionally.") 
1615
1616 (defvar gnus-newsrc-last-checked-date nil
1617   "Date Gnus last asked server for new newsgroups.")
1618
1619 (defvar gnus-topic-topology nil
1620   "The complete topic hierarchy.")
1621
1622 (defvar gnus-topic-alist nil
1623   "The complete topic-group alist.")
1624
1625 (defvar gnus-newsrc-alist nil
1626   "Assoc list of read articles.
1627 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1628
1629 (defvar gnus-newsrc-hashtb nil
1630   "Hashtable of gnus-newsrc-alist.")
1631
1632 (defvar gnus-killed-list nil
1633   "List of killed newsgroups.")
1634
1635 (defvar gnus-killed-hashtb nil
1636   "Hash table equivalent of gnus-killed-list.")
1637
1638 (defvar gnus-zombie-list nil
1639   "List of almost dead newsgroups.")
1640
1641 (defvar gnus-description-hashtb nil
1642   "Descriptions of newsgroups.")
1643
1644 (defvar gnus-list-of-killed-groups nil
1645   "List of newsgroups that have recently been killed by the user.")
1646
1647 (defvar gnus-active-hashtb nil
1648   "Hashtable of active articles.")
1649
1650 (defvar gnus-moderated-list nil
1651   "List of moderated newsgroups.")
1652
1653 (defvar gnus-group-marked nil)
1654
1655 (defvar gnus-current-startup-file nil
1656   "Startup file for the current host.")
1657
1658 (defvar gnus-last-search-regexp nil
1659   "Default regexp for article search command.")
1660
1661 (defvar gnus-last-shell-command nil
1662   "Default shell command on article.")
1663
1664 (defvar gnus-current-select-method nil
1665   "The current method for selecting a newsgroup.")
1666
1667 (defvar gnus-group-list-mode nil)
1668
1669 (defvar gnus-article-internal-prepare-hook nil)
1670
1671 (defvar gnus-newsgroup-name nil)
1672 (defvar gnus-newsgroup-begin nil)
1673 (defvar gnus-newsgroup-end nil)
1674 (defvar gnus-newsgroup-last-rmail nil)
1675 (defvar gnus-newsgroup-last-mail nil)
1676 (defvar gnus-newsgroup-last-folder nil)
1677 (defvar gnus-newsgroup-last-file nil)
1678 (defvar gnus-newsgroup-auto-expire nil)
1679 (defvar gnus-newsgroup-active nil)
1680
1681 (defvar gnus-newsgroup-data nil)
1682 (defvar gnus-newsgroup-data-reverse nil)
1683 (defvar gnus-newsgroup-limit nil)
1684 (defvar gnus-newsgroup-limits nil)
1685
1686 (defvar gnus-newsgroup-unreads nil
1687   "List of unread articles in the current newsgroup.")
1688
1689 (defvar gnus-newsgroup-unselected nil
1690   "List of unselected unread articles in the current newsgroup.")
1691
1692 (defvar gnus-newsgroup-reads nil
1693   "Alist of read articles and article marks in the current newsgroup.")
1694
1695 (defvar gnus-newsgroup-expunged-tally nil)
1696
1697 (defvar gnus-newsgroup-marked nil
1698   "List of ticked articles in the current newsgroup (a subset of unread art).")
1699
1700 (defvar gnus-newsgroup-killed nil
1701   "List of ranges of articles that have been through the scoring process.")
1702
1703 (defvar gnus-newsgroup-cached nil
1704   "List of articles that come from the article cache.")
1705
1706 (defvar gnus-newsgroup-saved nil
1707   "List of articles that have been saved.")
1708
1709 (defvar gnus-newsgroup-kill-headers nil)
1710
1711 (defvar gnus-newsgroup-replied nil
1712   "List of articles that have been replied to in the current newsgroup.")
1713
1714 (defvar gnus-newsgroup-expirable nil
1715   "List of articles in the current newsgroup that can be expired.")
1716
1717 (defvar gnus-newsgroup-processable nil
1718   "List of articles in the current newsgroup that can be processed.")
1719
1720 (defvar gnus-newsgroup-bookmarks nil
1721   "List of articles in the current newsgroup that have bookmarks.")
1722
1723 (defvar gnus-newsgroup-dormant nil
1724   "List of dormant articles in the current newsgroup.")
1725
1726 (defvar gnus-newsgroup-scored nil
1727   "List of scored articles in the current newsgroup.")
1728
1729 (defvar gnus-newsgroup-headers nil
1730   "List of article headers in the current newsgroup.")
1731
1732 (defvar gnus-newsgroup-threads nil)
1733
1734 (defvar gnus-newsgroup-prepared nil
1735   "Whether the current group has been prepared properly.")
1736
1737 (defvar gnus-newsgroup-ancient nil
1738   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1739
1740 (defvar gnus-current-article nil)
1741 (defvar gnus-article-current nil)
1742 (defvar gnus-current-headers nil)
1743 (defvar gnus-have-all-headers nil)
1744 (defvar gnus-last-article nil)
1745 (defvar gnus-newsgroup-history nil)
1746 (defvar gnus-current-kill-article nil)
1747
1748 ;; Save window configuration.
1749 (defvar gnus-prev-winconf nil)
1750
1751 (defvar gnus-summary-mark-positions nil)
1752 (defvar gnus-group-mark-positions nil)
1753
1754 (defvar gnus-reffed-article-number nil)
1755
1756 ;;; Let the byte-compiler know that we know about this variable.
1757 (defvar rmail-default-rmail-file)
1758
1759 (defvar gnus-cache-removeable-articles nil)
1760
1761 (defvar gnus-dead-summary nil)
1762
1763 (defconst gnus-summary-local-variables 
1764   '(gnus-newsgroup-name 
1765     gnus-newsgroup-begin gnus-newsgroup-end 
1766     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail 
1767     gnus-newsgroup-last-folder gnus-newsgroup-last-file 
1768     gnus-newsgroup-auto-expire gnus-newsgroup-unreads 
1769     gnus-newsgroup-unselected gnus-newsgroup-marked
1770     gnus-newsgroup-reads gnus-newsgroup-saved
1771     gnus-newsgroup-replied gnus-newsgroup-expirable
1772     gnus-newsgroup-processable gnus-newsgroup-killed
1773     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1774     gnus-newsgroup-headers gnus-newsgroup-threads
1775     gnus-newsgroup-prepared
1776     gnus-current-article gnus-current-headers gnus-have-all-headers
1777     gnus-last-article gnus-article-internal-prepare-hook
1778     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1779     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1780     gnus-newsgroup-async
1781     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below 
1782     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1783     gnus-newsgroup-history gnus-newsgroup-ancient
1784     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1785     gnus-newsgroup-adaptive-score-file
1786     (gnus-newsgroup-expunged-tally . 0)
1787     gnus-cache-removeable-articles gnus-newsgroup-cached
1788     gnus-newsgroup-data gnus-newsgroup-data-reverse
1789     gnus-newsgroup-limit gnus-newsgroup-limits)
1790   "Variables that are buffer-local to the summary buffers.")
1791
1792 (defconst gnus-bug-message
1793   "Sending a bug report to the Gnus Towers.
1794 ========================================
1795
1796 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1797 be sent to the Gnus Bug Exterminators. 
1798
1799 At the bottom of the buffer you'll see lots of variable settings.
1800 Please do not delete those.  They will tell the Bug People what your
1801 environment is, so that it will be easier to locate the bugs.
1802
1803 If you have found a bug that makes Emacs go \"beep\", set
1804 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET') 
1805 and include the backtrace in your bug report.
1806
1807 Please describe the bug in annoying, painstaking detail.
1808
1809 Thank you for your help in stamping out bugs.
1810 ")
1811
1812 ;;; End of variables.
1813
1814 ;; Define some autoload functions Gnus might use.
1815 (eval-and-compile
1816
1817   ;; This little mapcar goes through the list below and marks the
1818   ;; symbols in question as autoloaded functions.
1819   (mapcar 
1820    (lambda (package)
1821      (let ((interactive (nth 1 (memq ':interactive package))))
1822        (mapcar 
1823         (lambda (function)
1824           (let (keymap)
1825             (when (consp function)
1826               (setq keymap (car (memq 'keymap function)))
1827               (setq function (car function)))
1828             (autoload function (car package) nil interactive keymap)))
1829         (if (eq (nth 1 package) ':interactive)
1830             (cdddr package)
1831           (cdr package)))))
1832    '(("metamail" metamail-buffer)
1833      ("info" Info-goto-node)
1834      ("hexl" hexl-hex-string-to-integer)
1835      ("pp" pp pp-to-string pp-eval-expression)
1836      ("mail-extr" mail-extract-address-components)
1837      ("nnmail" nnmail-split-fancy nnmail-article-group)
1838      ("nnvirtual" nnvirtual-catchup-group)
1839      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1840       timezone-make-sortable-date timezone-make-time-string)
1841      ("sendmail" mail-position-on-field mail-setup)
1842      ("rmailout" rmail-output)
1843      ("rnewspost" news-mail-other-window news-reply-yank-original 
1844       news-caesar-buffer-body)
1845      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1846       rmail-show-message)
1847      ("gnus-soup" :interactive t
1848       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article 
1849       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1850      ("nnsoup" nnsoup-pack-replies)
1851      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder 
1852       gnus-Folder-save-name gnus-folder-save-name)
1853      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1854      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
1855       gnus-server-make-menu-bar gnus-article-make-menu-bar
1856       gnus-browse-make-menu-bar gnus-highlight-selected-summary
1857       gnus-summary-highlight-line gnus-carpal-setup-buffer
1858       gnus-article-add-button gnus-insert-next-page-button
1859       gnus-insert-prev-page-button)
1860      ("gnus-vis" :interactive t
1861       gnus-article-push-button gnus-article-press-button 
1862       gnus-article-highlight gnus-article-highlight-some 
1863       gnus-article-hide gnus-article-hide-signature 
1864       gnus-article-highlight-headers gnus-article-highlight-signature 
1865       gnus-article-add-buttons gnus-article-add-buttons-to-head 
1866       gnus-article-next-button gnus-article-prev-button)
1867      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
1868       gnus-demon-add-disconnection gnus-demon-add-handler
1869       gnus-demon-remove-handler)
1870      ("gnus-demon" :interactive t
1871       gnus-demon-init gnus-demon-cancel)
1872      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close)
1873      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
1874      ("gnus-cite" :interactive t
1875       gnus-article-highlight-citation gnus-article-hide-citation-maybe 
1876       gnus-article-hide-citation)
1877      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal 
1878       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author 
1879       gnus-execute gnus-expunge)
1880      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
1881       gnus-cache-possibly-remove-articles gnus-cache-request-article
1882       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
1883       gnus-cache-enter-remove-article gnus-cached-article-p
1884       gnus-cache-open gnus-cache-close)
1885      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
1886       gnus-cache-remove-article)
1887      ("gnus-score" :interactive t
1888       gnus-summary-increase-score gnus-summary-lower-score
1889       gnus-score-flush-cache gnus-score-close 
1890       gnus-score-raise-same-subject-and-select 
1891       gnus-score-raise-same-subject gnus-score-default 
1892       gnus-score-raise-thread gnus-score-lower-same-subject-and-select 
1893       gnus-score-lower-same-subject gnus-score-lower-thread 
1894       gnus-possibly-score-headers)
1895      ("gnus-score" 
1896       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
1897       gnus-current-score-file-nondirectory gnus-score-adaptive
1898       gnus-score-find-trace gnus-score-file-name)
1899      ("gnus-edit" :interactive t gnus-score-customize)
1900      ("gnus-topic" :interactive t gnus-topic-mode)
1901      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
1902      ("gnus-uu" :interactive t
1903       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward 
1904       gnus-uu-mark-series gnus-uu-mark-region 
1905       gnus-uu-mark-by-regexp gnus-uu-mark-all 
1906       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu 
1907       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar 
1908       gnus-uu-decode-unshar-and-save gnus-uu-decode-save 
1909       gnus-uu-decode-binhex gnus-uu-decode-uu-view 
1910       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view 
1911       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view 
1912       gnus-uu-decode-binhex-view)
1913      ("gnus-msg" (gnus-summary-send-map keymap)
1914       gnus-mail-yank-original gnus-mail-send-and-exit
1915       gnus-sendmail-setup-mail gnus-article-mail 
1916       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
1917      ("gnus-msg" :interactive t
1918       gnus-group-post-news gnus-group-mail gnus-summary-post-news
1919       gnus-summary-followup gnus-summary-followup-with-original
1920       gnus-summary-followup-and-reply
1921       gnus-summary-followup-and-reply-with-original
1922       gnus-summary-cancel-article gnus-summary-supersede-article
1923       gnus-post-news gnus-inews-news gnus-cancel-news
1924       gnus-summary-reply gnus-summary-reply-with-original
1925       gnus-summary-mail-forward gnus-summary-mail-other-window
1926       gnus-bug)
1927      ("gnus-picon" gnus-article-display-picon)
1928      ("gnus-vm" gnus-vm-mail-setup)
1929      ("gnus-vm" :interactive t gnus-summary-save-in-vm
1930       gnus-summary-save-article-vm gnus-yank-article))))
1931
1932 \f
1933
1934 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
1935 ;; If you want the cursor to go somewhere else, set these two
1936 ;; functions in some startup hook to whatever you want.
1937 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
1938 (defalias 'gnus-group-position-point 'gnus-goto-colon)
1939
1940 ;;; Various macros and substs.
1941
1942 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
1943   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
1944   `(let ((GnusStartBufferWindow (selected-window)))
1945      (unwind-protect
1946          (progn
1947            (pop-to-buffer ,buffer)
1948            ,@forms)
1949        (select-window GnusStartBufferWindow))))
1950
1951 (defmacro gnus-gethash (string hashtable)
1952   "Get hash value of STRING in HASHTABLE."
1953   `(symbol-value (intern-soft ,string ,hashtable)))
1954
1955 (defmacro gnus-sethash (string value hashtable)
1956   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
1957   `(set (intern ,string ,hashtable) ,value))
1958
1959 (defmacro gnus-intern-safe (string hashtable)
1960   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
1961   `(let ((symbol (intern ,string ,hashtable)))
1962      (or (boundp symbol)
1963          (set symbol nil))
1964      symbol))
1965
1966 (defmacro gnus-group-unread (group)
1967   "Get the currently computed number of unread articles in GROUP."
1968   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
1969
1970 (defmacro gnus-active (group)
1971   "Get active info on GROUP."
1972   `(gnus-gethash ,group gnus-active-hashtb))
1973
1974 (defmacro gnus-set-active (group active)
1975   "Set GROUP's active info."
1976   `(gnus-sethash ,group ,active gnus-active-hashtb))
1977
1978 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
1979 ;;   function `substring' might cut on a middle of multi-octet
1980 ;;   character.
1981 (defun gnus-truncate-string (str width)
1982   (substring str 0 width))
1983
1984 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
1985 ;; to limit the length of a string.  This function is necessary since
1986 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
1987 (defsubst gnus-limit-string (str width)
1988   (if (> (length str) width)
1989       (substring str 0 width)
1990     str))
1991
1992 (defsubst gnus-simplify-subject-re (subject)
1993   "Remove \"Re:\" from subject lines."
1994   (if (string-match "^[Rr][Ee]: *" subject)
1995       (substring subject (match-end 0))
1996     subject))
1997
1998 (defsubst gnus-goto-char (point)
1999   (and point (goto-char point)))
2000
2001 (defmacro gnus-buffer-exists-p (buffer)
2002   `(and ,buffer
2003         (funcall (if (stringp ,buffer) 'get-buffer 'buffer-name)
2004                  ,buffer)))
2005
2006 (defmacro gnus-kill-buffer (buffer)
2007   `(let ((buf ,buffer))
2008      (if (gnus-buffer-exists-p buf)
2009          (kill-buffer buf))))
2010
2011 (defsubst gnus-point-at-bol ()
2012   "Return point at the beginning of the line."
2013   (let ((p (point)))
2014     (beginning-of-line)
2015     (prog1
2016         (point)
2017       (goto-char p))))
2018
2019 (defsubst gnus-point-at-eol ()
2020   "Return point at the end of the line."
2021   (let ((p (point)))
2022     (end-of-line)
2023     (prog1
2024         (point)
2025       (goto-char p))))
2026
2027 ;; Delete the current line (and the next N lines.);
2028 (defmacro gnus-delete-line (&optional n)
2029   `(delete-region (progn (beginning-of-line) (point))
2030                   (progn (forward-line ,(or n 1)) (point))))
2031
2032 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2033 (defvar gnus-init-inhibit nil)
2034 (defun gnus-read-init-file (&optional inhibit-next)
2035   (if gnus-init-inhibit
2036       (setq gnus-init-inhibit nil)
2037     (setq gnus-init-inhibit inhibit-next)
2038     (and gnus-init-file
2039          (or (and (file-exists-p gnus-init-file) 
2040                   ;; Don't try to load a directory.
2041                   (not (file-directory-p gnus-init-file)))
2042              (file-exists-p (concat gnus-init-file ".el"))
2043              (file-exists-p (concat gnus-init-file ".elc")))
2044          (load gnus-init-file nil t))))
2045
2046 ;; Info access macros.
2047
2048 (defmacro gnus-info-group (info)
2049   `(nth 0 ,info))
2050 (defmacro gnus-info-rank (info)
2051   `(nth 1 ,info))
2052 (defmacro gnus-info-read (info)
2053   `(nth 2 ,info))
2054 (defmacro gnus-info-marks (info)
2055   `(nth 3 ,info))
2056 (defmacro gnus-info-method (info)
2057   `(nth 4 ,info))
2058 (defmacro gnus-info-params (info)
2059   `(nth 5 ,info))
2060
2061 (defmacro gnus-info-level (info)
2062   `(let ((rank (gnus-info-rank ,info)))
2063      (if (consp rank)
2064          (car rank)
2065        rank)))
2066 (defmacro gnus-info-score (info)
2067   `(let ((rank (gnus-info-rank ,info)))
2068      (or (and (consp rank) (cdr rank)) 0)))
2069
2070 (defmacro gnus-info-set-group (info group)
2071   `(setcar ,info ,group))
2072 (defmacro gnus-info-set-rank (info rank)
2073   `(setcar (nthcdr 1 ,info) ,rank))
2074 (defmacro gnus-info-set-read (info read)
2075   `(setcar (nthcdr 2 ,info) ,read))
2076 (defmacro gnus-info-set-marks (info marks)
2077   `(setcar (nthcdr 3 ,info) ,marks))
2078 (defmacro gnus-info-set-method (info method)
2079   `(setcar (nthcdr 4 ,info) ,method))
2080 (defmacro gnus-info-set-params (info params)
2081   `(setcar (nthcdr 5 ,info) ,params))
2082
2083 (defmacro gnus-info-set-level (info level)
2084   `(let ((rank (cdr ,info)))
2085      (if (consp (car rank))
2086          (setcar (car rank) ,level)
2087        (setcar rank ,level))))
2088 (defmacro gnus-info-set-score (info score)
2089   `(let ((rank (cdr ,info)))
2090      (if (consp (car rank))
2091          (setcdr (car rank) ,score)
2092        (setcar rank (cons (car rank) ,score)))))
2093
2094 (defmacro gnus-get-info (group)
2095   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2096
2097 (defun gnus-byte-code (func)
2098   "Return a form that can be `eval'ed based on FUNC."
2099   (let ((fval (symbol-function func)))
2100     (if (byte-code-function-p fval)
2101         (let ((flist (append fval nil)))
2102           (setcar flist 'byte-code)
2103           flist)
2104       (cons 'progn (cdr (cdr fval))))))
2105
2106 ;;; Load the user startup file.
2107 ;; (eval '(gnus-read-init-file 'inhibit))
2108
2109 ;;; Load the compatability functions. 
2110
2111 (require 'gnus-cus)
2112 (require 'gnus-ems)
2113
2114 \f
2115
2116 ;; Format specs.  The chunks below are the machine-generated forms
2117 ;; that are to be evaled as the result of the default format strings.
2118 ;; We write them in here to get them byte-compiled.  That way the
2119 ;; default actions will be quite fast, while still retaining the full
2120 ;; flexibility of the user-defined format specs. 
2121
2122 ;; First we have lots of dummy defvars to let the compiler know these
2123 ;; are really dynamic variables.
2124
2125 (defvar gnus-tmp-unread)
2126 (defvar gnus-tmp-replied)
2127 (defvar gnus-tmp-score-char)
2128 (defvar gnus-tmp-indentation)
2129 (defvar gnus-tmp-opening-bracket)
2130 (defvar gnus-tmp-lines)
2131 (defvar gnus-tmp-name)
2132 (defvar gnus-tmp-closing-bracket)
2133 (defvar gnus-tmp-subject-or-nil)
2134 (defvar gnus-tmp-subject)
2135 (defvar gnus-tmp-marked)
2136 (defvar gnus-tmp-marked-mark)
2137 (defvar gnus-tmp-subscribed)
2138 (defvar gnus-tmp-process-marked)
2139 (defvar gnus-tmp-number-of-unread)
2140 (defvar gnus-tmp-group-name)
2141 (defvar gnus-tmp-group)
2142 (defvar gnus-tmp-article-number)
2143 (defvar gnus-tmp-unread-and-unselected)
2144 (defvar gnus-tmp-news-method)
2145 (defvar gnus-tmp-news-server)
2146 (defvar gnus-tmp-article-number)
2147 (defvar gnus-mouse-face)
2148 (defvar gnus-mouse-face-prop)
2149
2150 (defun gnus-summary-line-format-spec ()
2151   (insert gnus-tmp-unread gnus-tmp-replied 
2152           gnus-tmp-score-char gnus-tmp-indentation)
2153   (put-text-property
2154    (point)
2155    (progn
2156      (insert 
2157       gnus-tmp-opening-bracket 
2158       (format "%4d: %-20s" 
2159               gnus-tmp-lines 
2160               (if (> (length gnus-tmp-name) 20) 
2161                   (substring gnus-tmp-name 0 20) 
2162                 gnus-tmp-name))
2163       gnus-tmp-closing-bracket)
2164      (point))
2165    gnus-mouse-face-prop gnus-mouse-face)
2166   (insert " " gnus-tmp-subject-or-nil "\n"))
2167
2168 (defvar gnus-summary-line-format-spec 
2169   (gnus-byte-code 'gnus-summary-line-format-spec))
2170
2171 (defun gnus-summary-dummy-line-format-spec ()
2172   (insert "*  ")
2173   (put-text-property
2174    (point)
2175    (progn
2176      (insert ":                          :")
2177      (point))
2178    gnus-mouse-face-prop gnus-mouse-face)
2179   (insert " " gnus-tmp-subject "\n"))
2180
2181 (defvar gnus-summary-dummy-line-format-spec 
2182   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2183
2184 (defun gnus-group-line-format-spec ()
2185   (insert gnus-tmp-marked-mark gnus-tmp-subscribed 
2186           gnus-tmp-process-marked
2187           gnus-topic-indentation
2188           (format "%5s: " gnus-tmp-number-of-unread))
2189   (put-text-property 
2190    (point)
2191    (progn
2192      (insert gnus-tmp-group "\n")
2193      (1- (point)))
2194    gnus-mouse-face-prop gnus-mouse-face))
2195 (defvar gnus-group-line-format-spec 
2196   (gnus-byte-code 'gnus-group-line-format-spec))
2197
2198 (defvar gnus-old-specs 
2199   '((group . "%M%S%p%5y: %(%g%)\n")
2200     (summary-dummy . "*  :                          : %S\n")
2201     (summary . "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n")))
2202
2203 (defvar gnus-article-mode-line-format-spec nil)
2204 (defvar gnus-summary-mode-line-format-spec nil)
2205 (defvar gnus-group-mode-line-format-spec nil)
2206
2207 ;;; Phew.  All that gruft is over, fortunately.  
2208
2209 \f
2210 ;;;
2211 ;;; Gnus Utility Functions
2212 ;;;
2213
2214 (defun gnus-extract-address-components (from)
2215   (let (name address)
2216     ;; First find the address - the thing with the @ in it.  This may
2217     ;; not be accurate in mail addresses, but does the trick most of
2218     ;; the time in news messages.
2219     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2220         (setq address (substring from (match-beginning 0) (match-end 0))))
2221     ;; Then we check whether the "name <address>" format is used.
2222     (and address
2223          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2224          ;; Linear white space is not required.
2225          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2226          (and (setq name (substring from 0 (match-beginning 0)))
2227               ;; Strip any quotes from the name.
2228               (string-match "\".*\"" name)
2229               (setq name (substring name 1 (1- (match-end 0))))))
2230     ;; If not, then "address (name)" is used.
2231     (or name
2232         (and (string-match "(.+)" from)
2233              (setq name (substring from (1+ (match-beginning 0)) 
2234                                    (1- (match-end 0)))))
2235         (and (string-match "()" from)
2236              (setq name address))
2237         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2238         ;; XOVER might not support folded From headers.
2239         (and (string-match "(.*" from)
2240              (setq name (substring from (1+ (match-beginning 0)) 
2241                                    (match-end 0)))))
2242     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2243     (list (or name from) (or address from))))
2244
2245 (defun gnus-fetch-field (field)
2246   "Return the value of the header FIELD of current article."
2247   (save-excursion
2248     (save-restriction
2249       (let ((case-fold-search t))
2250         (gnus-narrow-to-headers)
2251         (mail-fetch-field field)))))
2252
2253 (defun gnus-goto-colon ()
2254   (beginning-of-line)
2255   (search-forward ":" (gnus-point-at-eol) t))
2256
2257 (defun gnus-narrow-to-headers ()
2258   "Narrow to the head of an article."
2259   (widen)
2260   (narrow-to-region
2261    (goto-char (point-min))
2262    (if (search-forward "\n\n" nil t)
2263        (1- (point))
2264      (point-max)))
2265   (goto-char (point-min)))
2266
2267 ;;;###autoload
2268 (defun gnus-update-format (var)
2269   "Update the format specification near point."
2270   (interactive
2271    (list
2272     (save-excursion
2273       (eval-defun nil)
2274       ;; Find the end of the current word.
2275       (re-search-forward "[ \t\n]" nil t)
2276       ;; Search backward.
2277       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2278         (match-string 1)))))
2279   (set
2280    (intern (format "%s-spec" var))
2281    (gnus-parse-format (symbol-value (intern var))
2282                       (symbol-value (intern (format "%s-alist" var)))
2283                       (not (string-match "mode" var))))
2284   (pop-to-buffer "*Gnus Format*")
2285   (erase-buffer)
2286   (lisp-interaction-mode)
2287   (insert (pp-to-string (symbol-value (intern (format "%s-spec" var))))))
2288
2289
2290 (defun gnus-update-format-specifications (&optional force)
2291   (gnus-make-thread-indent-array)
2292
2293   (when force
2294     (setq gnus-old-specs nil))
2295
2296   (let ((formats '(summary summary-dummy group 
2297                            summary-mode group-mode article-mode))
2298         old-format new-format)
2299     (while formats
2300       (setq new-format (symbol-value
2301                         (intern (format "gnus-%s-line-format" (car formats)))))
2302       (or (and (setq old-format (cdr (assq (car formats) gnus-old-specs)))
2303                (equal old-format new-format))
2304           (set (intern (format "gnus-%s-line-format-spec" (car formats)))
2305                (if (not (stringp new-format)) new-format
2306                  (gnus-parse-format
2307                   new-format
2308                   (symbol-value 
2309                    (intern (format "gnus-%s-line-format-alist"
2310                                    (if (eq (car formats) 'article-mode)
2311                                        'summary-mode (car formats)))))
2312                   (not (string-match "mode$" (symbol-name (car formats))))))))
2313       (setq gnus-old-specs (cons (cons (car formats) new-format)
2314                                  (delq (assq (car formats) gnus-old-specs)
2315                                        gnus-old-specs)))
2316       (setq formats (cdr formats))))
2317       
2318   (gnus-update-group-mark-positions)
2319   (gnus-update-summary-mark-positions)
2320
2321   (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
2322            (not gnus-description-hashtb)
2323            gnus-read-active-file)
2324       (gnus-read-all-descriptions-files)))
2325
2326 (defun gnus-update-summary-mark-positions ()
2327   (save-excursion
2328     (let ((gnus-replied-mark 129)
2329           (gnus-score-below-mark 130)
2330           (gnus-score-over-mark 130)
2331           (thread nil)
2332           (gnus-visual nil)
2333           pos)
2334       (gnus-set-work-buffer)
2335       (gnus-summary-insert-line 
2336        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2337       (goto-char (point-min))
2338       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2339                                          (- (point) 2)))))
2340       (goto-char (point-min))
2341       (setq pos (cons (cons 'replied (and (search-forward "\201" nil t)
2342                                           (- (point) 2))) pos))
2343       (goto-char (point-min))
2344       (setq pos (cons (cons 'score (and (search-forward "\202" nil t)
2345                                         (- (point) 2))) pos))
2346       (setq gnus-summary-mark-positions pos))))
2347
2348 (defun gnus-update-group-mark-positions ()
2349   (save-excursion
2350     (let ((gnus-process-mark 128)
2351           (gnus-group-marked '("dummy.group")))
2352       (gnus-set-active "dummy.group" '(0 . 0))
2353       (gnus-set-work-buffer)
2354       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2355       (goto-char (point-min))
2356       (setq gnus-group-mark-positions
2357             (list (cons 'process (and (search-forward "\200" nil t)
2358                                       (- (point) 2))))))))
2359
2360 (defvar gnus-mouse-face-0 'highlight)
2361 (defvar gnus-mouse-face-1 'highlight)
2362 (defvar gnus-mouse-face-2 'highlight)
2363 (defvar gnus-mouse-face-3 'highlight)
2364 (defvar gnus-mouse-face-4 'highlight)
2365
2366 (defun gnus-mouse-face-function (form type)
2367   `(put-text-property
2368     (point) (progn ,@form (point))
2369     gnus-mouse-face-prop 
2370     ,(if (equal type 0)
2371          'gnus-mouse-face
2372        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2373
2374 (defvar gnus-face-0 'bold)
2375 (defvar gnus-face-1 'italic)
2376 (defvar gnus-face-2 'bold-italic)
2377 (defvar gnus-face-3 'bold)
2378 (defvar gnus-face-4 'bold)
2379
2380 (defun gnus-face-face-function (form type)
2381   `(put-text-property
2382     (point) (progn ,@form (point))
2383     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2384
2385 (defun gnus-max-width-function (el max-width)
2386   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2387   (if (symbolp el)
2388       `(if (> (length ,el) ,max-width)
2389            (substring ,el 0 ,max-width)
2390          ,el)
2391     `(let ((val (eval ,el)))
2392        (if (numberp val)
2393            (setq val (int-to-string val)))
2394        (if (> (length val) ,max-width)
2395            (substring val 0 ,max-width))
2396        val)))
2397
2398 (defun gnus-parse-format (format spec-alist &optional insert)
2399   ;; This function parses the FORMAT string with the help of the
2400   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2401   ;; string.  If the FORMAT string contains the specifiers %( and %)
2402   ;; the text between them will have the mouse-face text property.
2403   (if (string-match 
2404        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2405        format)
2406       (gnus-parse-complex-format format spec-alist)
2407     ;; This is a simple format.
2408     (gnus-parse-simple-format format spec-alist insert)))
2409
2410 (defun gnus-parse-complex-format (format spec-alist)
2411   (save-excursion
2412     (gnus-set-work-buffer)
2413     (insert format)
2414     (goto-char (point-min))
2415     (while (re-search-forward "\"" nil t)
2416       (replace-match "\\\"" nil t))
2417     (goto-char (point-min))
2418     (insert "(\"")
2419     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2420       (let ((number (if (match-beginning 1)
2421                         (match-string 1) "0"))
2422             (delim (aref (match-string 2) 0)))
2423         (if (or (= delim ?\() (= delim ?\{))
2424             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2425                                    " " number " \""))
2426           (replace-match "\")\""))))
2427     (goto-char (point-max))
2428     (insert "\")")
2429     (goto-char (point-min))
2430     (let ((form (read (current-buffer))))
2431       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2432
2433 (defun gnus-complex-form-to-spec (form spec-alist)
2434   (delq nil
2435         (mapcar
2436          (lambda (sform)
2437            (if (stringp sform)
2438                (gnus-parse-simple-format sform spec-alist t)
2439              (funcall (intern (format "gnus-%s-face-function"
2440                                       (car sform)))
2441                       (gnus-complex-form-to-spec 
2442                        (cdr (cdr sform)) spec-alist)
2443                       (nth 1 sform))))
2444          form)))
2445     
2446 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2447   ;; This function parses the FORMAT string with the help of the
2448   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2449   ;; string.  
2450   (let ((max-width 0)
2451         spec flist fstring newspec elem beg result dontinsert)
2452     (save-excursion
2453       (gnus-set-work-buffer)
2454       (insert format)
2455       (goto-char (point-min))
2456       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2457                                 nil t)
2458         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2459               (setq newspec "%"
2460                     beg (1+ (match-beginning 0)))
2461           ;; First check if there are any specs that look anything like
2462           ;; "%12,12A", ie. with a "max width specification".  These have
2463           ;; to be treated specially.
2464           (if (setq beg (match-beginning 1))
2465               (setq max-width 
2466                     (string-to-int 
2467                      (buffer-substring 
2468                       (1+ (match-beginning 1)) (match-end 1))))
2469             (setq max-width 0)
2470             (setq beg (match-beginning 2)))
2471           ;; Find the specification from `spec-alist'.
2472           (unless (setq elem (cdr (assq spec spec-alist)))
2473             (setq elem '("*" ?s)))
2474           ;; Treat user defined format specifiers specially.
2475           (when (eq (car elem) 'gnus-tmp-user-defined)
2476             (setq elem
2477                   (list 
2478                    (list (intern (concat "gnus-user-format-function-"
2479                                          (match-string 3)))
2480                          'gnus-tmp-header) ?s))
2481             (delete-region (match-beginning 3) (match-end 3)))
2482           (if (not (zerop max-width))
2483               (let ((el (car elem)))
2484                 (cond ((= (car (cdr elem)) ?c) 
2485                        (setq el (list 'char-to-string el)))
2486                       ((= (car (cdr elem)) ?d)
2487                        (numberp el) (setq el (list 'int-to-string el))))
2488                 (setq flist (cons (gnus-max-width-function el max-width)
2489                                   flist))
2490                 (setq newspec ?s))
2491             (progn
2492               (setq flist (cons (car elem) flist))
2493               (setq newspec (car (cdr elem))))))
2494         ;; Remove the old specification (and possibly a ",12" string).
2495         (delete-region beg (match-end 2))
2496         ;; Insert the new specification.
2497         (goto-char beg)
2498         (insert newspec))
2499       (setq fstring (buffer-substring 1 (point-max))))
2500     ;; Do some postprocessing to increase efficiency.
2501     (setq 
2502      result
2503      (cond 
2504       ;; Emptyness.
2505       ((string= fstring "")
2506        nil)
2507       ;; Not a format string.
2508       ((not (string-match "%" fstring))
2509        (list fstring))
2510       ;; A format string with just a single string spec.
2511       ((string= fstring "%s")
2512        (list (car flist)))
2513       ;; A single character.
2514       ((string= fstring "%c")
2515        (list (car flist)))
2516       ;; A single number.
2517       ((string= fstring "%d")
2518        (setq dontinsert)
2519        (if insert
2520            (list `(princ ,(car flist)))
2521          (list `(int-to-string ,(car flist)))))
2522       ;; Just lots of chars and strings.
2523       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2524        (nreverse flist))
2525       ;; A single string spec at the beginning of the spec.
2526       ((string-match "\\`%[sc][^%]+\\'" fstring)
2527        (list (car flist) (substring fstring 2)))
2528       ;; A single string spec in the middle of the spec.
2529       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2530        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2531       ;; A single string spec in the end of the spec.
2532       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2533        (list (match-string 1 fstring) (car flist)))
2534       ;; A more complex spec.
2535       (t
2536        (list (cons 'format (cons fstring (nreverse flist)))))))
2537
2538     (if insert
2539         (when result
2540           (if dontinsert
2541               result
2542             (cons 'insert result)))
2543       (or (car result) ""))))
2544
2545 (defun gnus-eval-format (format &optional alist props)
2546   "Eval the format variable FORMAT, using ALIST.
2547 If INSERT, insert the result."
2548   (let ((form (gnus-parse-format format alist props)))
2549     (if props
2550         (add-text-properties (point) (progn (eval form) (point)) props)
2551       (eval form))))
2552
2553 (defun gnus-remove-text-with-property (prop)
2554   "Delete all text in the current buffer with text property PROP."
2555   (save-excursion
2556     (goto-char (point-min))
2557     (while (not (eobp))
2558       (when (get-text-property (point) prop)
2559         (delete-char 1))
2560       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2561
2562 (defun gnus-set-work-buffer ()
2563   (if (get-buffer gnus-work-buffer)
2564       (progn
2565         (set-buffer gnus-work-buffer)
2566         (erase-buffer))
2567     (set-buffer (get-buffer-create gnus-work-buffer))
2568     (kill-all-local-variables)
2569     (buffer-disable-undo (current-buffer))
2570     (gnus-add-current-to-buffer-list)))
2571
2572 ;; Article file names when saving.
2573
2574 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2575   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2576 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2577 Otherwise, it is like ~/News/news/group/num."
2578   (let ((default
2579           (expand-file-name
2580            (concat (if (gnus-use-long-file-name 'not-save)
2581                        (gnus-capitalize-newsgroup newsgroup)
2582                      (gnus-newsgroup-directory-form newsgroup))
2583                    "/" (int-to-string (mail-header-number headers)))
2584            (or gnus-article-save-directory "~/News"))))
2585     (if (and last-file
2586              (string-equal (file-name-directory default)
2587                            (file-name-directory last-file))
2588              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2589         default
2590       (or last-file default))))
2591
2592 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2593   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2594 If variable `gnus-use-long-file-name' is non-nil, it is
2595 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2596   (let ((default
2597           (expand-file-name
2598            (concat (if (gnus-use-long-file-name 'not-save)
2599                        newsgroup
2600                      (gnus-newsgroup-directory-form newsgroup))
2601                    "/" (int-to-string (mail-header-number headers)))
2602            (or gnus-article-save-directory "~/News"))))
2603     (if (and last-file
2604              (string-equal (file-name-directory default)
2605                            (file-name-directory last-file))
2606              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2607         default
2608       (or last-file default))))
2609
2610 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2611   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2612 If variable `gnus-use-long-file-name' is non-nil, it is
2613 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2614   (or last-file
2615       (expand-file-name
2616        (if (gnus-use-long-file-name 'not-save)
2617            (gnus-capitalize-newsgroup newsgroup)
2618          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2619        (or gnus-article-save-directory "~/News"))))
2620
2621 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2622   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2623 If variable `gnus-use-long-file-name' is non-nil, it is
2624 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2625   (or last-file
2626       (expand-file-name
2627        (if (gnus-use-long-file-name 'not-save)
2628            newsgroup
2629          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2630        (or gnus-article-save-directory "~/News"))))
2631
2632 ;; For subscribing new newsgroup
2633
2634 (defun gnus-subscribe-hierarchical-interactive (groups)
2635   (let ((groups (sort groups 'string<))
2636         prefixes prefix start ans group starts)
2637     (while groups
2638       (setq prefixes (list "^"))
2639       (while (and groups prefixes)
2640         (while (not (string-match (car prefixes) (car groups)))
2641           (setq prefixes (cdr prefixes)))
2642         (setq prefix (car prefixes))
2643         (setq start (1- (length prefix)))
2644         (if (and (string-match "[^\\.]\\." (car groups) start)
2645                  (cdr groups)
2646                  (setq prefix 
2647                        (concat "^" (substring (car groups) 0 (match-end 0))))
2648                  (string-match prefix (car (cdr groups))))
2649             (progn
2650               (setq prefixes (cons prefix prefixes))
2651               (message "Descend hierarchy %s? ([y]nsq): " 
2652                        (substring prefix 1 (1- (length prefix))))
2653               (setq ans (read-char))
2654               (cond ((= ans ?n)
2655                      (while (and groups 
2656                                  (string-match prefix 
2657                                                (setq group (car groups))))
2658                        (setq gnus-killed-list 
2659                              (cons group gnus-killed-list))
2660                        (gnus-sethash group group gnus-killed-hashtb)
2661                        (setq groups (cdr groups)))
2662                      (setq starts (cdr starts)))
2663                     ((= ans ?s)
2664                      (while (and groups 
2665                                  (string-match prefix 
2666                                                (setq group (car groups))))
2667                        (gnus-sethash group group gnus-killed-hashtb)
2668                        (gnus-subscribe-alphabetically (car groups))
2669                        (setq groups (cdr groups)))
2670                      (setq starts (cdr starts)))
2671                     ((= ans ?q)
2672                      (while groups
2673                        (setq group (car groups))
2674                        (setq gnus-killed-list (cons group gnus-killed-list))
2675                        (gnus-sethash group group gnus-killed-hashtb)
2676                        (setq groups (cdr groups))))
2677                     (t nil)))
2678           (message "Subscribe %s? ([n]yq)" (car groups))
2679           (setq ans (read-char))
2680           (setq group (car groups))
2681           (cond ((= ans ?y)
2682                  (gnus-subscribe-alphabetically (car groups))
2683                  (gnus-sethash group group gnus-killed-hashtb))
2684                 ((= ans ?q)
2685                  (while groups
2686                    (setq group (car groups))
2687                    (setq gnus-killed-list (cons group gnus-killed-list))
2688                    (gnus-sethash group group gnus-killed-hashtb)
2689                    (setq groups (cdr groups))))
2690                 (t 
2691                  (setq gnus-killed-list (cons group gnus-killed-list))
2692                  (gnus-sethash group group gnus-killed-hashtb)))
2693           (setq groups (cdr groups)))))))
2694
2695 (defun gnus-subscribe-randomly (newsgroup)
2696   "Subscribe new NEWSGROUP by making it the first newsgroup."
2697   (gnus-subscribe-newsgroup newsgroup))
2698
2699 (defun gnus-subscribe-alphabetically (newgroup)
2700   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2701   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2702   (let ((groups (cdr gnus-newsrc-alist))
2703         before)
2704     (while (and (not before) groups)
2705       (if (string< newgroup (car (car groups)))
2706           (setq before (car (car groups)))
2707         (setq groups (cdr groups))))
2708     (gnus-subscribe-newsgroup newgroup before)))
2709
2710 (defun gnus-subscribe-hierarchically (newgroup)
2711   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2712   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2713   (save-excursion
2714     (set-buffer (find-file-noselect gnus-current-startup-file))
2715     (let ((groupkey newgroup)
2716           before)
2717       (while (and (not before) groupkey)
2718         (goto-char (point-min))
2719         (let ((groupkey-re
2720                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2721           (while (and (re-search-forward groupkey-re nil t)
2722                       (progn
2723                         (setq before (match-string 1))
2724                         (string< before newgroup)))))
2725         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2726         (setq groupkey
2727               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2728                   (substring groupkey (match-beginning 1) (match-end 1)))))
2729       (gnus-subscribe-newsgroup newgroup before))))
2730
2731 (defun gnus-subscribe-interactively (group)
2732   "Subscribe the new GROUP interactively.
2733 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2734 it is killed."
2735   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2736       (gnus-subscribe-hierarchically group)
2737     (push group gnus-killed-list)))
2738
2739 (defun gnus-subscribe-zombies (group)
2740   "Make the new GROUP into a zombie group."
2741   (push group gnus-zombie-list))
2742
2743 (defun gnus-subscribe-killed (group)
2744   "Make the new GROUP a killed group."
2745   (push group gnus-killed-list))
2746
2747 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2748   "Subscribe new NEWSGROUP.
2749 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2750 the first newsgroup."
2751   ;; We subscribe the group by changing its level to `subscribed'.
2752   (gnus-group-change-level 
2753    newsgroup gnus-level-default-subscribed
2754    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2755   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2756
2757 ;; For directories
2758
2759 (defun gnus-newsgroup-directory-form (newsgroup)
2760   "Make hierarchical directory name from NEWSGROUP name."
2761   (let ((newsgroup (gnus-newsgroup-saveable-name newsgroup))
2762         (len (length newsgroup))
2763         idx)
2764     ;; If this is a foreign group, we don't want to translate the
2765     ;; entire name.  
2766     (if (setq idx (string-match ":" newsgroup))
2767         (aset newsgroup idx ?/)
2768       (setq idx 0))
2769     ;; Replace all occurrences of `.' with `/'.
2770     (while (< idx len)
2771       (if (= (aref newsgroup idx) ?.)
2772           (aset newsgroup idx ?/))
2773       (setq idx (1+ idx)))
2774     newsgroup))
2775
2776 (defun gnus-newsgroup-saveable-name (group)
2777   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2778   ;; with dots.
2779   (gnus-replace-chars-in-string group ?/ ?.))
2780
2781 (defun gnus-make-directory (dir)
2782   "Make DIRECTORY recursively."
2783   ;; Why don't we use `(make-directory dir 'parents)'? That's just one
2784   ;; of the many mysteries of the universe.
2785   (let* ((dir (expand-file-name dir default-directory))
2786          dirs err)
2787     (if (string-match "/$" dir)
2788         (setq dir (substring dir 0 (match-beginning 0))))
2789     ;; First go down the path until we find a directory that exists.
2790     (while (not (file-exists-p dir))
2791       (setq dirs (cons dir dirs))
2792       (string-match "/[^/]+$" dir)
2793       (setq dir (substring dir 0 (match-beginning 0))))
2794     ;; Then create all the subdirs.
2795     (while (and dirs (not err))
2796       (condition-case ()
2797           (make-directory (car dirs))
2798         (error (setq err t)))
2799       (setq dirs (cdr dirs)))
2800     ;; We return whether we were successful or not. 
2801     (not dirs)))
2802
2803 (defun gnus-capitalize-newsgroup (newsgroup)
2804   "Capitalize NEWSGROUP name."
2805   (and (not (zerop (length newsgroup)))
2806        (concat (char-to-string (upcase (aref newsgroup 0)))
2807                (substring newsgroup 1))))
2808
2809 ;; Var
2810
2811 (defun gnus-simplify-subject (subject &optional re-only)
2812   "Remove `Re:' and words in parentheses.
2813 If optional argument RE-ONLY is non-nil, strip `Re:' only."
2814   (let ((case-fold-search t))           ;Ignore case.
2815     ;; Remove `Re:' and `Re^N:'.
2816     (if (string-match "^re:[ \t]*" subject)
2817         (setq subject (substring subject (match-end 0))))
2818     ;; Remove words in parentheses from end.
2819     (or re-only
2820         (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
2821           (setq subject (substring subject 0 (match-beginning 0)))))
2822     ;; Return subject string.
2823     subject))
2824
2825 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
2826 ;; all whitespace.
2827 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
2828 (defun gnus-simplify-buffer-fuzzy ()
2829   (goto-char (point-min))
2830   (while (or
2831           (looking-at "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*")
2832           (looking-at "^[[].*:[ \t].*[]]$"))
2833     (goto-char (point-min))
2834     (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
2835                               nil t)
2836       (replace-match "" t t))
2837     (goto-char (point-min))
2838     (while (re-search-forward "^[[].*:[ \t].*[]]$" nil t)
2839       (goto-char (match-end 0))
2840       (delete-char -1)
2841       (delete-region 
2842        (progn (goto-char (match-beginning 0)))
2843        (re-search-forward ":"))))
2844   (goto-char (point-min))
2845   (while (re-search-forward "[ \t\n]*([^()]*)[ \t]*$" nil t)
2846     (replace-match "" t t))
2847   (goto-char (point-min))
2848   (while (re-search-forward "[ \t]+" nil t)
2849     (replace-match " " t t))
2850   (goto-char (point-min))
2851   (while (re-search-forward "[ \t]+$" nil t)
2852     (replace-match "" t t))
2853   (goto-char (point-min))
2854   (while (re-search-forward "^[ \t]+" nil t)
2855     (replace-match "" t t))
2856   (goto-char (point-min))
2857   (if gnus-simplify-subject-fuzzy-regexp
2858       (if (listp gnus-simplify-subject-fuzzy-regexp)
2859           (let ((list gnus-simplify-subject-fuzzy-regexp))
2860             (while list
2861               (goto-char (point-min))
2862               (while (re-search-forward (car list) nil t)
2863                 (replace-match "" t t))
2864               (setq list (cdr list))))
2865         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
2866           (replace-match "" t t)))))
2867
2868 (defun gnus-simplify-subject-fuzzy (subject)
2869   "Siplify a subject string fuzzily."
2870   (let ((case-fold-search t))
2871     (save-excursion
2872       (gnus-set-work-buffer)
2873       (insert subject)
2874       (inline (gnus-simplify-buffer-fuzzy))
2875       (buffer-string))))
2876
2877 ;; Add the current buffer to the list of buffers to be killed on exit. 
2878 (defun gnus-add-current-to-buffer-list ()
2879   (or (memq (current-buffer) gnus-buffer-list)
2880       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
2881
2882 (defun gnus-string> (s1 s2)
2883   (not (or (string< s1 s2)
2884            (string= s1 s2))))
2885
2886 ;; Functions accessing headers.
2887 ;; Functions are more convenient than macros in some cases.
2888
2889 (defun gnus-header-number (header)
2890   (mail-header-number header))
2891
2892 (defun gnus-header-subject (header)
2893   (mail-header-subject header))
2894
2895 (defun gnus-header-from (header)
2896   (mail-header-from header))
2897
2898 (defun gnus-header-xref (header)
2899   (mail-header-xref header))
2900
2901 (defun gnus-header-lines (header)
2902   (mail-header-lines header))
2903
2904 (defun gnus-header-date (header)
2905   (mail-header-date header))
2906
2907 (defun gnus-header-id (header)
2908   (mail-header-id header))
2909
2910 (defun gnus-header-message-id (header)
2911   (mail-header-id header))
2912
2913 (defun gnus-header-chars (header)
2914   (mail-header-chars header))
2915
2916 (defun gnus-header-references (header)
2917   (mail-header-references header))
2918
2919 ;;; General various misc type functions.
2920
2921 (defun gnus-clear-system ()
2922   "Clear all variables and buffers."
2923   ;; Clear Gnus variables.
2924   (let ((variables gnus-variable-list))
2925     (while variables
2926       (set (car variables) nil)
2927       (setq variables (cdr variables))))
2928   ;; Clear other internal variables.
2929   (setq gnus-list-of-killed-groups nil
2930         gnus-have-read-active-file nil
2931         gnus-newsrc-alist nil
2932         gnus-newsrc-hashtb nil
2933         gnus-killed-list nil
2934         gnus-zombie-list nil
2935         gnus-killed-hashtb nil
2936         gnus-active-hashtb nil
2937         gnus-moderated-list nil
2938         gnus-description-hashtb nil
2939         gnus-newsgroup-headers nil
2940         gnus-newsgroup-name nil
2941         gnus-server-alist nil
2942         gnus-opened-servers nil
2943         gnus-current-select-method nil)
2944   ;; Reset any score variables.
2945   (and gnus-use-scoring (gnus-score-close))
2946   ;; Kill the startup file.
2947   (and gnus-current-startup-file
2948        (get-file-buffer gnus-current-startup-file)
2949        (kill-buffer (get-file-buffer gnus-current-startup-file)))
2950   ;; Save any cache buffers.
2951   (and gnus-use-cache (gnus-cache-save-buffers))
2952   ;; Clear the dribble buffer.
2953   (gnus-dribble-clear)
2954   ;; Close down NoCeM.
2955   (and gnus-use-nocem (gnus-nocem-close))
2956   ;; Shut down the demons.
2957   (and gnus-use-demon (gnus-demon-cancel))
2958   ;; Kill global KILL file buffer.
2959   (if (get-file-buffer (gnus-newsgroup-kill-file nil))
2960       (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
2961   (gnus-kill-buffer nntp-server-buffer)
2962   ;; Backlog.
2963   (and gnus-keep-backlog (gnus-backlog-shutdown))
2964   ;; Kill Gnus buffers.
2965   (while gnus-buffer-list
2966     (gnus-kill-buffer (car gnus-buffer-list))
2967     (setq gnus-buffer-list (cdr gnus-buffer-list))))
2968
2969 (defun gnus-windows-old-to-new (setting)
2970   ;; First we take care of the really, really old Gnus 3 actions.
2971   (if (symbolp setting)
2972       (setq setting 
2973             (cond ((memq setting '(SelectArticle))
2974                    'article)
2975                   ((memq setting '(SelectSubject ExpandSubject))
2976                    'summary)
2977                   ((memq setting '(SelectNewsgroup ExitNewsgroup))
2978                    'group)
2979                   (t setting))))
2980   (if (or (listp setting)
2981           (not (and gnus-window-configuration
2982                     (memq setting '(group summary article)))))
2983       setting
2984     (let* ((setting (if (eq setting 'group) 
2985                         (if (assq 'newsgroup gnus-window-configuration)
2986                             'newsgroup
2987                           'newsgroups) setting))
2988            (elem (car (cdr (assq setting gnus-window-configuration))))
2989            (total (apply '+ elem))
2990            (types '(group summary article))
2991            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
2992            (i 0)
2993            perc
2994            out)
2995       (while (< i 3)
2996         (or (not (numberp (nth i elem)))
2997             (zerop (nth i elem))
2998             (progn
2999               (setq perc  (/ (* 1.0 (nth 0 elem)) total))
3000               (setq out (cons (if (eq pbuf (nth i types))
3001                                   (vector (nth i types) perc 'point)
3002                                 (vector (nth i types) perc))
3003                               out))))
3004         (setq i (1+ i)))
3005       (list (nreverse out)))))
3006            
3007 (defun gnus-add-configuration (conf)
3008   (setq gnus-buffer-configuration 
3009         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3010                          gnus-buffer-configuration))))
3011
3012 (defun gnus-configure-frame (split &optional window)
3013   "Split WINDOW according to SPLIT."
3014   (unless window
3015     (setq window (get-buffer-window (current-buffer))))
3016   (select-window window)
3017   ;; This might be an old-stylee buffer config.
3018   (when (vectorp split)
3019     (setq split (append split nil)))
3020   (when (or (consp (car split))
3021             (vectorp (car split)))
3022     (push 1.0 split)
3023     (push 'vertical split))
3024   ;; The SPLIT might be something that is to be evaled to 
3025   ;; return a new SPLIT.
3026   (while (and (not (assq (car split) gnus-window-to-buffer))
3027               (gnus-functionp (car split)))
3028     (setq split (eval split)))
3029   (let* ((type (car split))
3030          (subs (cdr (cdr split)))
3031          (len (if (eq type 'horizontal) (window-width) (window-height) ))
3032          (total 0)
3033          s result new-win rest comp-subs size sub)
3034     (cond
3035      ;; Nothing to do here.
3036      ((null split))
3037      ;; This is a buffer to be selected.
3038      ((not (or (eq type 'horizontal) (eq type 'vertical)))
3039       (let ((buffer (cdr (assq type gnus-window-to-buffer))))
3040         (unless buffer
3041           (error "Illegal buffer type: %s" type))
3042         (switch-to-buffer (get-buffer (if (symbolp buffer) 
3043                                           (symbol-value buffer)
3044                                         buffer)))
3045         ;; We return the window if it has the `point' spec.
3046         (and (memq 'point split) window)))
3047      ;; This is a normal split
3048      (t
3049       (when (> (length subs) 0)
3050         ;; First we have to compute the sizes of all new windows.
3051         (while subs
3052           (setq sub (append (pop subs) nil))
3053           (while (and (not (assq (car sub) gnus-window-to-buffer))
3054                       (gnus-functionp (car sub)))
3055             (setq sub (eval sub)))
3056           (when sub
3057             (push sub comp-subs)
3058             (setq size (cadar comp-subs))
3059             (cond ((equal size 1.0)
3060                    (setq rest (car comp-subs))
3061                    (setq s 0))
3062                   ((floatp size)
3063                    (setq s (floor (* size len))))
3064                   ((integerp size)
3065                    (setq s size))
3066                   (t
3067                    (error "Illegal size: %s" size)))
3068             ;; Try to make sure that we are inside the safe limits.
3069             (cond ((zerop s))
3070                   ((and (eq type 'horizontal)
3071                         (< s 10))
3072                    (setq s 10))
3073                   ((and (eq type 'vertical)
3074                         (< s 4))
3075                    (setq s 4)))
3076             (setcar (cdar comp-subs) s)
3077             (incf total s)))
3078         ;; Take care of the "1.0" spec.
3079         (if rest
3080             (setcar (cdr rest) (- len total))
3081           (error "No 1.0 specs in %s" split))
3082         ;; The we do the actual splitting in a nice recursive
3083         ;; fashion.
3084         (setq comp-subs (nreverse comp-subs))
3085         (while comp-subs
3086           (if (null (cdr comp-subs))
3087               (setq new-win window)
3088             (setq new-win
3089                   (split-window window (cadar comp-subs)
3090                                 (eq type 'horizontal))))
3091           (setq result (or (gnus-configure-frame 
3092                             (car comp-subs) window) result))
3093           (select-window new-win)
3094           (setq window new-win)
3095           (setq comp-subs (cdr comp-subs))))
3096       ;; Return the proper window, if any.
3097       (when result
3098         (select-window result))))))
3099
3100 (defun gnus-configure-windows (setting &optional force)
3101   (setq setting (gnus-windows-old-to-new setting))
3102   (let ((split (if (symbolp setting)
3103                    (car (cdr (assq setting gnus-buffer-configuration)))
3104                  setting))
3105         (in-buf (current-buffer))
3106         rule val w height hor ohor heights sub jump-buffer
3107         rel total to-buf all-visible)
3108
3109     (unless split
3110       (error "No such setting: %s" setting))
3111
3112     (if (and (not force) (setq all-visible (gnus-all-windows-visible-p split)))
3113         ;; All the windows mentioned are already visible, so we just
3114         ;; put point in the assigned buffer, and do not touch the
3115         ;; winconf. 
3116         (select-window all-visible)
3117
3118       ;; Either remove all windows or just remove all Gnus windows.
3119       (if gnus-use-full-window
3120           (delete-other-windows)
3121         (gnus-remove-some-windows)
3122         (switch-to-buffer nntp-server-buffer))
3123
3124       (switch-to-buffer nntp-server-buffer)
3125       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3126
3127 (defun gnus-all-windows-visible-p (split)
3128   (when (vectorp split)
3129     (setq split (append split nil)))
3130   (when (or (consp (car split))
3131             (vectorp (car split)))
3132     (push 1.0 split)
3133     (push 'vertical split))
3134   ;; The SPLIT might be something that is to be evaled to 
3135   ;; return a new SPLIT.
3136   (while (and (not (assq (car split) gnus-window-to-buffer))
3137               (gnus-functionp (car split)))
3138     (setq split (eval split)))
3139   (let* ((type (elt split 0)))
3140     (cond
3141      ((null split)
3142       t)
3143      ((not (or (eq type 'horizontal) (eq type 'vertical)))
3144       (let ((buffer (cdr (assq type gnus-window-to-buffer)))
3145             win)
3146         (unless buffer
3147           (error "Illegal buffer type: %s" type))
3148         (setq win
3149               (get-buffer-window (get-buffer (if (symbolp buffer) 
3150                                                  (symbol-value buffer)
3151                                                buffer))))
3152         (when win
3153           (if (memq 'point split)
3154               win
3155             t))))
3156      (t
3157       (let ((n (mapcar 'gnus-all-windows-visible-p
3158                        (cdr (cdr split))))
3159             (win t))
3160         (while n
3161           (cond ((windowp (car n))
3162                  (setq win (car n)))
3163                 ((null (car n))
3164                  (setq win nil)))
3165           (setq n (cdr n)))
3166         win)))))
3167
3168 (defun gnus-window-top-edge (&optional window)
3169   (nth 1 (window-edges window)))
3170
3171 (defun gnus-remove-some-windows ()
3172   (let ((buffers gnus-window-to-buffer)
3173         buf bufs lowest-buf lowest)
3174     (save-excursion
3175       ;; Remove windows on all known Gnus buffers.
3176       (while buffers
3177         (setq buf (cdr (car buffers)))
3178         (if (symbolp buf)
3179             (setq buf (and (boundp buf) (symbol-value buf))))
3180         (and buf 
3181              (get-buffer-window buf)
3182              (progn
3183                (setq bufs (cons buf bufs))
3184                (pop-to-buffer buf)
3185                (if (or (not lowest)
3186                        (< (gnus-window-top-edge) lowest))
3187                    (progn
3188                      (setq lowest (gnus-window-top-edge))
3189                      (setq lowest-buf buf)))))
3190         (setq buffers (cdr buffers)))
3191       ;; Remove windows on *all* summary buffers.
3192       (let (wins)
3193         (walk-windows
3194          (lambda (win)
3195            (let ((buf (window-buffer win)))
3196              (if (string-match  "^\\*Summary" (buffer-name buf))
3197                  (progn
3198                    (setq bufs (cons buf bufs))
3199                    (pop-to-buffer buf)
3200                    (if (or (not lowest)
3201                            (< (gnus-window-top-edge) lowest))
3202                        (progn
3203                          (setq lowest-buf buf)
3204                          (setq lowest (gnus-window-top-edge))))))))))
3205       (and lowest-buf 
3206            (progn
3207              (pop-to-buffer lowest-buf)
3208              (switch-to-buffer nntp-server-buffer)))
3209       (while bufs
3210         (and (not (eq (car bufs) lowest-buf))
3211              (delete-windows-on (car bufs)))
3212         (setq bufs (cdr bufs))))))
3213                           
3214 (defun gnus-version ()
3215   "Version numbers of this version of Gnus."
3216   (interactive)
3217   (let ((methods gnus-valid-select-methods)
3218         (mess gnus-version)
3219         meth)
3220     ;; Go through all the legal select methods and add their version
3221     ;; numbers to the total version string.  Only the backends that are
3222     ;; currently in use will have their message numbers taken into
3223     ;; consideration. 
3224     (while methods
3225       (setq meth (intern (concat (car (car methods)) "-version")))
3226       (and (boundp meth)
3227            (stringp (symbol-value meth))
3228            (setq mess (concat mess "; " (symbol-value meth))))
3229       (setq methods (cdr methods)))
3230     (gnus-message 2 mess)))
3231
3232 (defun gnus-info-find-node ()
3233   "Find Info documentation of Gnus."
3234   (interactive)
3235   ;; Enlarge info window if needed.
3236   (let ((mode major-mode))
3237     (gnus-configure-windows 'info)
3238     (Info-goto-node (car (cdr (assq mode gnus-info-nodes))))))
3239
3240 (defun gnus-replace-chars-in-string (string &rest pairs)
3241   "Replace characters in STRING from FROM to TO."
3242   (let ((string (substring string 0))   ;Copy string.
3243         (len (length string))
3244         (idx 0)
3245         sym to)
3246     (or (zerop (% (length pairs) 2)) 
3247         (error "Odd number of translation pairs"))
3248     (setplist 'sym pairs)
3249     ;; Replace all occurrences of FROM with TO.
3250     (while (< idx len)
3251       (if (setq to (get 'sym (aref string idx)))
3252           (aset string idx to))
3253       (setq idx (1+ idx)))
3254     string))
3255
3256 (defun gnus-days-between (date1 date2)
3257   ;; Return the number of days between date1 and date2.
3258   (- (gnus-day-number date1) (gnus-day-number date2)))
3259
3260 (defun gnus-day-number (date)
3261   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3262                      (timezone-parse-date date))))
3263     (timezone-absolute-from-gregorian 
3264      (nth 1 dat) (nth 2 dat) (car dat))))
3265
3266 ;; Returns a floating point number that says how many seconds have
3267 ;; lapsed between Jan 1 12:00:00 1970 and DATE.
3268 (defun gnus-seconds-since-epoch (date)
3269   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
3270                         (timezone-parse-date date)))
3271          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
3272                         (timezone-parse-time
3273                          (aref (timezone-parse-date date) 3))))
3274          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
3275                         (timezone-parse-date "Jan 1 12:00:00 1970")))
3276          (tday (- (timezone-absolute-from-gregorian 
3277                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
3278                   (timezone-absolute-from-gregorian 
3279                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
3280     (+ (nth 2 ttime)
3281        (* (nth 1 ttime) 60)
3282        (* 1.0 (nth 0 ttime) 60 60)
3283        (* 1.0 tday 60 60 24))))
3284
3285 (defun gnus-file-newer-than (file date)
3286   (let ((fdate (nth 5 (file-attributes file))))
3287     (or (> (car fdate) (car date))
3288         (and (= (car fdate) (car date))
3289              (> (nth 1 fdate) (nth 1 date))))))
3290
3291 (defun gnus-group-read-only-p (&optional group)
3292   "Check whether GROUP supports editing or not.
3293 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3294 that that variable is buffer-local to the summary buffers."
3295   (let ((group (or group gnus-newsgroup-name)))
3296     (not (gnus-check-backend-function 'request-replace-article group))))
3297
3298 (defun gnus-group-total-expirable-p (group)
3299   "Check whether GROUP is total-expirable or not."
3300   (let ((params (gnus-info-params (gnus-get-info group))))
3301     (or (memq 'total-expire params) 
3302         (cdr (assq 'total-expire params)) ; (total-expire . t)
3303         (and gnus-total-expirable-newsgroups ; Check var.
3304              (string-match gnus-total-expirable-newsgroups group)))))
3305
3306 (defun gnus-group-auto-expirable-p (group)
3307   "Check whether GROUP is total-expirable or not."
3308   (let ((params (gnus-info-params (gnus-get-info group))))
3309     (or (memq 'auto-expire params) 
3310         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3311         (and gnus-auto-expirable-newsgroups ; Check var.
3312              (string-match gnus-auto-expirable-newsgroups group)))))
3313
3314 (defsubst gnus-simplify-subject-fully (subject)
3315   "Simplify a subject string according to the user's wishes."
3316   (cond
3317    ((null gnus-summary-gather-subject-limit)
3318     (gnus-simplify-subject-re subject))
3319    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3320     (gnus-simplify-subject-fuzzy subject))
3321    ((numberp gnus-summary-gather-subject-limit)
3322     (gnus-limit-string subject gnus-summary-gather-subject-limit))
3323    (t
3324     subject)))
3325
3326 (defsubst gnus-subject-equal (s1 s2 &optional simple-first) 
3327   "Check whether two subjects are equal.  If optional argument
3328 simple-first is t, first argument is already simplified."
3329   (cond
3330    ((null simple-first)
3331     (equal (gnus-simplify-subject-fully s1)
3332            (gnus-simplify-subject-fully s2)))
3333    (t
3334     (equal s1
3335            (gnus-simplify-subject-fully s2)))))
3336
3337 ;; Returns a list of writable groups.
3338 (defun gnus-writable-groups ()
3339   (let ((alist gnus-newsrc-alist)
3340         groups)
3341     (while alist
3342       (or (gnus-group-read-only-p (car (car alist)))
3343           (setq groups (cons (car (car alist)) groups)))
3344       (setq alist (cdr alist)))
3345     (nreverse groups)))
3346
3347 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3348 ;; the echo area.
3349 (defun gnus-y-or-n-p (prompt)
3350   (prog1
3351       (y-or-n-p prompt)
3352     (message "")))
3353
3354 (defun gnus-yes-or-no-p (prompt)
3355   (prog1
3356       (yes-or-no-p prompt)
3357     (message "")))
3358
3359 ;; Check whether to use long file names.
3360 (defun gnus-use-long-file-name (symbol)
3361   ;; The variable has to be set...
3362   (and gnus-use-long-file-name
3363        ;; If it isn't a list, then we return t.
3364        (or (not (listp gnus-use-long-file-name))
3365            ;; If it is a list, and the list contains `symbol', we
3366            ;; return nil.  
3367            (not (memq symbol gnus-use-long-file-name)))))
3368
3369 ;; I suspect there's a better way, but I haven't taken the time to do
3370 ;; it yet. -erik selberg@cs.washington.edu
3371 (defun gnus-dd-mmm (messy-date)
3372   "Return a string like DD-MMM from a big messy string"
3373   (let ((datevec (timezone-parse-date messy-date)))
3374     (format "%2s-%s"
3375             (or (aref datevec 2) "??")
3376             (capitalize
3377              (or (car 
3378                   (nth (1- (string-to-number (aref datevec 1)))
3379                        timezone-months-assoc))
3380                  "???")))))
3381
3382 ;; Make a hash table (default and minimum size is 255).
3383 ;; Optional argument HASHSIZE specifies the table size.
3384 (defun gnus-make-hashtable (&optional hashsize)
3385   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3386
3387 ;; Make a number that is suitable for hashing; bigger than MIN and one
3388 ;; less than 2^x.
3389 (defun gnus-create-hash-size (min)
3390   (let ((i 1))
3391     (while (< i min)
3392       (setq i (* 2 i)))
3393     (1- i)))
3394
3395 ;; Show message if message has a lower level than `gnus-verbose'. 
3396 ;; Guide-line for numbers:
3397 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3398 ;; for things that take a long time, 7 - not very important messages
3399 ;; on stuff, 9 - messages inside loops.
3400 (defun gnus-message (level &rest args)
3401   (if (<= level gnus-verbose)
3402       (apply 'message args)
3403     ;; We have to do this format thingie here even if the result isn't
3404     ;; shown - the return value has to be the same as the return value
3405     ;; from `message'.
3406     (apply 'format args)))
3407
3408 (defun gnus-functionp (form)
3409   "Return non-nil if FORM is funcallable."
3410   (or (and (symbolp form) (fboundp form))
3411       (and (listp form) (eq (car form) 'lambda))))
3412
3413 ;; Generate a unique new group name.
3414 (defun gnus-generate-new-group-name (leaf)
3415   (let ((name leaf)
3416         (num 0))
3417     (while (gnus-gethash name gnus-newsrc-hashtb)
3418       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3419     name))
3420
3421 ;; Find out whether the gnus-visual TYPE is wanted.
3422 (defun gnus-visual-p (&optional type class)
3423   (and gnus-visual                      ; Has to be non-nil, at least.
3424        (if (not type)                   ; We don't care about type.
3425            gnus-visual
3426          (if (listp gnus-visual)        ; It's a list, so we check it.
3427              (or (memq type gnus-visual)
3428                  (memq class gnus-visual))
3429            t))))
3430
3431 (defun gnus-parent-id (references)
3432   "Return the last Message-ID in REFERENCES."
3433   (and references
3434        (string-match "\\(<[^<>]+>\\) *$" references)
3435        (substring references (match-beginning 1) (match-end 1))))
3436
3437 (defun gnus-ephemeral-group-p (group)
3438   "Say whether GROUP is ephemeral or not."
3439   (assoc 'quit-config (gnus-find-method-for-group group)))
3440
3441 (defun gnus-group-quit-config (group)
3442   "Return the quit-config of GROUP."
3443   (nth 1 (assoc 'quit-config (gnus-find-method-for-group group))))
3444
3445 (defun gnus-simplify-mode-line ()
3446   "Make mode lines a bit simpler."
3447   (setq mode-line-modified "-- ")
3448   (when (listp mode-line-format)
3449     (make-local-variable 'mode-line-format)
3450     (setq mode-line-format (copy-sequence mode-line-format))
3451     (and (equal (nth 3 mode-line-format) "   ")
3452          (setcar (nthcdr 3 mode-line-format) ""))))
3453
3454 ;;; List and range functions
3455
3456 (defun gnus-last-element (list)
3457   "Return last element of LIST."
3458   (while (cdr list)
3459     (setq list (cdr list)))
3460   (car list))
3461
3462 (defun gnus-copy-sequence (list)
3463   "Do a complete, total copy of a list."
3464   (if (and (consp list) (not (consp (cdr list))))
3465       (cons (car list) (cdr list))
3466     (mapcar (lambda (elem) (if (consp elem) 
3467                                (if (consp (cdr elem))
3468                                    (gnus-copy-sequence elem)
3469                                  (cons (car elem) (cdr elem)))
3470                              elem))
3471             list)))
3472
3473 (defun gnus-set-difference (list1 list2)
3474   "Return a list of elements of LIST1 that do not appear in LIST2."
3475   (let ((list1 (copy-sequence list1)))
3476     (while list2
3477       (setq list1 (delq (car list2) list1))
3478       (setq list2 (cdr list2)))
3479     list1))
3480
3481 (defun gnus-sorted-complement (list1 list2)
3482   "Return a list of elements of LIST1 that do not appear in LIST2.
3483 Both lists have to be sorted over <."
3484   (let (out)
3485     (if (or (null list1) (null list2))
3486         (or list1 list2)
3487       (while (and list1 list2)
3488         (cond ((= (car list1) (car list2))
3489                (setq list1 (cdr list1)
3490                      list2 (cdr list2)))
3491               ((< (car list1) (car list2))
3492                (setq out (cons (car list1) out))
3493                (setq list1 (cdr list1)))
3494               (t
3495                (setq out (cons (car list2) out))
3496                (setq list2 (cdr list2)))))
3497       (nconc (nreverse out) (or list1 list2)))))
3498
3499 (defun gnus-intersection (list1 list2)      
3500   (let ((result nil))
3501     (while list2
3502       (if (memq (car list2) list1)
3503           (setq result (cons (car list2) result)))
3504       (setq list2 (cdr list2)))
3505     result))
3506
3507 (defun gnus-sorted-intersection (list1 list2)
3508   ;; LIST1 and LIST2 have to be sorted over <.
3509   (let (out)
3510     (while (and list1 list2)
3511       (cond ((= (car list1) (car list2))
3512              (setq out (cons (car list1) out)
3513                    list1 (cdr list1)
3514                    list2 (cdr list2)))
3515             ((< (car list1) (car list2))
3516              (setq list1 (cdr list1)))
3517             (t
3518              (setq list2 (cdr list2)))))
3519     (nreverse out)))
3520
3521 (defun gnus-set-sorted-intersection (list1 list2)
3522   ;; LIST1 and LIST2 have to be sorted over <.
3523   ;; This function modifies LIST1.
3524   (let* ((top (cons nil list1))
3525          (prev top))
3526     (while (and list1 list2)
3527       (cond ((= (car list1) (car list2))
3528              (setq prev list1
3529                    list1 (cdr list1)
3530                    list2 (cdr list2)))
3531             ((< (car list1) (car list2))
3532              (setcdr prev (cdr list1))
3533              (setq list1 (cdr list1)))
3534             (t
3535              (setq list2 (cdr list2)))))
3536     (setcdr prev nil)
3537     (cdr top)))
3538
3539 (defun gnus-compress-sequence (numbers &optional always-list)
3540   "Convert list of numbers to a list of ranges or a single range.
3541 If ALWAYS-LIST is non-nil, this function will always release a list of
3542 ranges."
3543   (let* ((first (car numbers))
3544          (last (car numbers))
3545          result)
3546     (if (null numbers)
3547         nil
3548       (if (not (listp (cdr numbers)))
3549           numbers
3550         (while numbers
3551           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3552                 ((= (1+ last) (car numbers)) ;Still in sequence
3553                  (setq last (car numbers)))
3554                 (t                      ;End of one sequence
3555                  (setq result 
3556                        (cons (if (= first last) first
3557                                (cons first last)) result))
3558                  (setq first (car numbers))
3559                  (setq last  (car numbers))))
3560           (setq numbers (cdr numbers)))
3561         (if (and (not always-list) (null result))
3562             (if (= first last) (list first) (cons first last))
3563           (nreverse (cons (if (= first last) first (cons first last))
3564                           result)))))))
3565
3566 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3567 (defun gnus-uncompress-range (ranges)
3568   "Expand a list of ranges into a list of numbers.
3569 RANGES is either a single range on the form `(num . num)' or a list of
3570 these ranges."
3571   (let (first last result)
3572     (cond 
3573      ((null ranges)
3574       nil)
3575      ((not (listp (cdr ranges)))
3576       (setq first (car ranges))
3577       (setq last (cdr ranges))
3578       (while (<= first last)
3579         (setq result (cons first result))
3580         (setq first (1+ first)))
3581       (nreverse result))
3582      (t
3583       (while ranges
3584         (if (atom (car ranges))
3585             (if (numberp (car ranges))
3586                 (setq result (cons (car ranges) result)))
3587           (setq first (car (car ranges)))
3588           (setq last  (cdr (car ranges)))
3589           (while (<= first last)
3590             (setq result (cons first result))
3591             (setq first (1+ first))))
3592         (setq ranges (cdr ranges)))
3593       (nreverse result)))))
3594
3595 (defun gnus-add-to-range (ranges list)
3596   "Return a list of ranges that has all articles from both RANGES and LIST.
3597 Note: LIST has to be sorted over `<'."
3598   (if (not ranges)
3599       (gnus-compress-sequence list t)
3600     (setq list (copy-sequence list))
3601     (or (listp (cdr ranges))
3602         (setq ranges (list ranges)))
3603     (let ((out ranges)
3604           ilist lowest highest temp)
3605       (while (and ranges list)
3606         (setq ilist list)
3607         (setq lowest (or (and (atom (car ranges)) (car ranges))
3608                          (car (car ranges))))
3609         (while (and list (cdr list) (< (car (cdr list)) lowest))
3610           (setq list (cdr list)))
3611         (if (< (car ilist) lowest)
3612             (progn
3613               (setq temp list)
3614               (setq list (cdr list))
3615               (setcdr temp nil)
3616               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3617         (setq highest (or (and (atom (car ranges)) (car ranges))
3618                           (cdr (car ranges))))
3619         (while (and list (<= (car list) highest))
3620           (setq list (cdr list)))
3621         (setq ranges (cdr ranges)))
3622       (if list
3623           (setq out (nconc (gnus-compress-sequence list t) out)))
3624       (setq out (sort out (lambda (r1 r2) 
3625                             (< (or (and (atom r1) r1) (car r1))
3626                                (or (and (atom r2) r2) (car r2))))))
3627       (setq ranges out)
3628       (while ranges
3629         (if (atom (car ranges))
3630             (if (cdr ranges)
3631                 (if (atom (car (cdr ranges)))
3632                     (if (= (1+ (car ranges)) (car (cdr ranges)))
3633                         (progn
3634                           (setcar ranges (cons (car ranges) 
3635                                                (car (cdr ranges))))
3636                           (setcdr ranges (cdr (cdr ranges)))))
3637                   (if (= (1+ (car ranges)) (car (car (cdr ranges))))
3638                       (progn
3639                         (setcar (car (cdr ranges)) (car ranges))
3640                         (setcar ranges (car (cdr ranges)))
3641                         (setcdr ranges (cdr (cdr ranges)))))))
3642           (if (cdr ranges)
3643               (if (atom (car (cdr ranges)))
3644                   (if (= (1+ (cdr (car ranges))) (car (cdr ranges)))
3645                       (progn
3646                         (setcdr (car ranges) (car (cdr ranges)))
3647                         (setcdr ranges (cdr (cdr ranges)))))
3648                 (if (= (1+ (cdr (car ranges))) (car (car (cdr ranges))))
3649                     (progn
3650                       (setcdr (car ranges) (cdr (car (cdr ranges))))
3651                       (setcdr ranges (cdr (cdr ranges))))))))
3652         (setq ranges (cdr ranges)))
3653       out)))
3654
3655 (defun gnus-remove-from-range (ranges list)
3656   "Return a list of ranges that has all articles from LIST removed from RANGES.
3657 Note: LIST has to be sorted over `<'."
3658   ;; !!! This function shouldn't look like this, but I've got a headache.
3659   (gnus-compress-sequence 
3660    (gnus-sorted-complement
3661     (gnus-uncompress-range ranges) list)))
3662
3663 (defun gnus-member-of-range (number ranges)
3664   (if (not (listp (cdr ranges)))
3665       (and (>= number (car ranges)) 
3666            (<= number (cdr ranges)))
3667     (let ((not-stop t))
3668       (while (and ranges 
3669                   (if (numberp (car ranges))
3670                       (>= number (car ranges))
3671                     (>= number (car (car ranges))))
3672                   not-stop)
3673         (if (if (numberp (car ranges))
3674                 (= number (car ranges))
3675               (and (>= number (car (car ranges)))
3676                    (<= number (cdr (car ranges)))))
3677             (setq not-stop nil))
3678         (setq ranges (cdr ranges)))
3679       (not not-stop))))
3680
3681 (defun gnus-range-length (range)
3682   "Return the length RANGE would have if uncompressed."
3683   (length (gnus-uncompress-range range)))
3684
3685 (defun gnus-sublist-p (list sublist)
3686   "Test whether all elements in SUBLIST are members of LIST."
3687   (let ((sublistp t))
3688     (while sublist
3689       (unless (memq (pop sublist) list)
3690         (setq sublistp nil
3691               sublist nil)))
3692     sublistp))
3693
3694 \f
3695 ;;;
3696 ;;; Gnus group mode
3697 ;;;
3698
3699 (defvar gnus-group-mode-map nil)
3700 (defvar gnus-group-group-map nil)
3701 (defvar gnus-group-mark-map nil)
3702 (defvar gnus-group-list-map nil)
3703 (defvar gnus-group-sort-map nil)
3704 (defvar gnus-group-soup-map nil)
3705 (defvar gnus-group-sub-map nil)
3706 (defvar gnus-group-help-map nil)
3707 (defvar gnus-group-score-map nil)
3708 (put 'gnus-group-mode 'mode-class 'special)
3709
3710 (if gnus-group-mode-map
3711     nil
3712   (setq gnus-group-mode-map (make-keymap))
3713   (suppress-keymap gnus-group-mode-map)
3714   (define-key gnus-group-mode-map " " 'gnus-group-read-group)
3715   (define-key gnus-group-mode-map "=" 'gnus-group-select-group)
3716   (define-key gnus-group-mode-map "\M- " 'gnus-group-unhidden-select-group)
3717   (define-key gnus-group-mode-map "\r" 'gnus-group-select-group)
3718   (define-key gnus-group-mode-map "\M-\r" 'gnus-group-quick-select-group)
3719   (define-key gnus-group-mode-map "j" 'gnus-group-jump-to-group)
3720   (define-key gnus-group-mode-map "n" 'gnus-group-next-unread-group)
3721   (define-key gnus-group-mode-map "p" 'gnus-group-prev-unread-group)
3722   (define-key gnus-group-mode-map "\177" 'gnus-group-prev-unread-group)
3723   (define-key gnus-group-mode-map "N" 'gnus-group-next-group)
3724   (define-key gnus-group-mode-map "P" 'gnus-group-prev-group)
3725   (define-key gnus-group-mode-map
3726     "\M-n" 'gnus-group-next-unread-group-same-level)
3727   (define-key gnus-group-mode-map 
3728     "\M-p" 'gnus-group-prev-unread-group-same-level)
3729   (define-key gnus-group-mode-map "," 'gnus-group-best-unread-group)
3730   (define-key gnus-group-mode-map "." 'gnus-group-first-unread-group)
3731   (define-key gnus-group-mode-map "u" 'gnus-group-unsubscribe-current-group)
3732   (define-key gnus-group-mode-map "U" 'gnus-group-unsubscribe-group)
3733   (define-key gnus-group-mode-map "c" 'gnus-group-catchup-current)
3734   (define-key gnus-group-mode-map "C" 'gnus-group-catchup-current-all)
3735   (define-key gnus-group-mode-map "l" 'gnus-group-list-groups)
3736   (define-key gnus-group-mode-map "L" 'gnus-group-list-all-groups)
3737   (define-key gnus-group-mode-map "m" 'gnus-group-mail)
3738   (define-key gnus-group-mode-map "g" 'gnus-group-get-new-news)
3739   (define-key gnus-group-mode-map "\M-g" 'gnus-group-get-new-news-this-group)
3740   (define-key gnus-group-mode-map "R" 'gnus-group-restart)
3741   (define-key gnus-group-mode-map "r" 'gnus-group-read-init-file)
3742   (define-key gnus-group-mode-map "B" 'gnus-group-browse-foreign-server)
3743   (define-key gnus-group-mode-map "b" 'gnus-group-check-bogus-groups)
3744   (define-key gnus-group-mode-map "F" 'gnus-find-new-newsgroups)
3745   (define-key gnus-group-mode-map "\C-c\C-d" 'gnus-group-describe-group)
3746   (define-key gnus-group-mode-map "\M-d" 'gnus-group-describe-all-groups)
3747   (define-key gnus-group-mode-map "\C-c\C-a" 'gnus-group-apropos)
3748   (define-key gnus-group-mode-map "\C-c\M-\C-a" 'gnus-group-description-apropos)
3749   (define-key gnus-group-mode-map "a" 'gnus-group-post-news)
3750   (define-key gnus-group-mode-map "\ek" 'gnus-group-edit-local-kill)
3751   (define-key gnus-group-mode-map "\eK" 'gnus-group-edit-global-kill)
3752   (define-key gnus-group-mode-map "\C-k" 'gnus-group-kill-group)
3753   (define-key gnus-group-mode-map "\C-y" 'gnus-group-yank-group)
3754   (define-key gnus-group-mode-map "\C-w" 'gnus-group-kill-region)
3755   (define-key gnus-group-mode-map "\C-x\C-t" 'gnus-group-transpose-groups)
3756   (define-key gnus-group-mode-map "\C-c\C-l" 'gnus-group-list-killed)
3757   (define-key gnus-group-mode-map "\C-c\C-x" 'gnus-group-expire-articles)
3758   (define-key gnus-group-mode-map "\C-c\M-\C-x" 'gnus-group-expire-all-groups)
3759   (define-key gnus-group-mode-map "V" 'gnus-version)
3760   (define-key gnus-group-mode-map "s" 'gnus-group-save-newsrc)
3761   (define-key gnus-group-mode-map "z" 'gnus-group-suspend)
3762   (define-key gnus-group-mode-map "Z" 'gnus-group-clear-dribble)
3763   (define-key gnus-group-mode-map "q" 'gnus-group-exit)
3764   (define-key gnus-group-mode-map "Q" 'gnus-group-quit)
3765   (define-key gnus-group-mode-map "?" 'gnus-group-describe-briefly)
3766   (define-key gnus-group-mode-map "\C-c\C-i" 'gnus-info-find-node)
3767   (define-key gnus-group-mode-map "\M-e" 'gnus-group-edit-group-method)
3768   (define-key gnus-group-mode-map "^" 'gnus-group-enter-server-mode)
3769   (define-key gnus-group-mode-map gnus-mouse-2 'gnus-mouse-pick-group)
3770   (define-key gnus-group-mode-map "<" 'beginning-of-buffer)
3771   (define-key gnus-group-mode-map ">" 'end-of-buffer)
3772   (define-key gnus-group-mode-map "\C-c\C-b" 'gnus-bug)
3773   (define-key gnus-group-mode-map "\C-c\C-s" 'gnus-group-sort-groups)
3774   (define-key gnus-group-mode-map "t" 'gnus-topic-mode)
3775   (define-key gnus-group-mode-map "\C-c\M-g" 'gnus-activate-all-groups)
3776
3777   (define-key gnus-group-mode-map "#" 'gnus-group-mark-group)
3778   (define-key gnus-group-mode-map "\M-#" 'gnus-group-unmark-group)
3779   (define-prefix-command 'gnus-group-mark-map)
3780   (define-key gnus-group-mode-map "M" 'gnus-group-mark-map)
3781   (define-key gnus-group-mark-map "m" 'gnus-group-mark-group)
3782   (define-key gnus-group-mark-map "u" 'gnus-group-unmark-group)
3783   (define-key gnus-group-mark-map "w" 'gnus-group-mark-region)
3784   (define-key gnus-group-mark-map "r" 'gnus-group-mark-regexp)
3785
3786   (define-prefix-command 'gnus-group-group-map)
3787   (define-key gnus-group-mode-map "G" 'gnus-group-group-map)
3788   (define-key gnus-group-group-map "d" 'gnus-group-make-directory-group)
3789   (define-key gnus-group-group-map "h" 'gnus-group-make-help-group)
3790   (define-key gnus-group-group-map "a" 'gnus-group-make-archive-group)
3791   (define-key gnus-group-group-map "k" 'gnus-group-make-kiboze-group)
3792   (define-key gnus-group-group-map "m" 'gnus-group-make-group)
3793   (define-key gnus-group-group-map "E" 'gnus-group-edit-group)
3794   (define-key gnus-group-group-map "e" 'gnus-group-edit-group-method)
3795   (define-key gnus-group-group-map "p" 'gnus-group-edit-group-parameters)
3796   (define-key gnus-group-group-map "v" 'gnus-group-add-to-virtual)
3797   (define-key gnus-group-group-map "V" 'gnus-group-make-empty-virtual)
3798   (define-key gnus-group-group-map "D" 'gnus-group-enter-directory)
3799   (define-key gnus-group-group-map "f" 'gnus-group-make-doc-group)
3800   (define-key gnus-group-group-map "r" 'gnus-group-rename-group)
3801   (define-key gnus-group-group-map "\177" 'gnus-group-delete-group)
3802
3803   (define-prefix-command 'gnus-group-soup-map)
3804   (define-key gnus-group-group-map "s" 'gnus-group-soup-map)
3805   (define-key gnus-group-soup-map "b" 'gnus-group-brew-soup)
3806   (define-key gnus-group-soup-map "w" 'gnus-soup-save-areas)
3807   (define-key gnus-group-soup-map "s" 'gnus-soup-send-replies)
3808   (define-key gnus-group-soup-map "p" 'gnus-soup-pack-packet)
3809   (define-key gnus-group-soup-map "r" 'nnsoup-pack-replies)
3810
3811   (define-prefix-command 'gnus-group-sort-map)
3812   (define-key gnus-group-group-map "S" 'gnus-group-sort-map)
3813   (define-key gnus-group-sort-map "s" 'gnus-group-sort-groups)
3814   (define-key gnus-group-sort-map "a" 'gnus-group-sort-groups-by-alphabet)
3815   (define-key gnus-group-sort-map "u" 'gnus-group-sort-groups-by-unread)
3816   (define-key gnus-group-sort-map "l" 'gnus-group-sort-groups-by-level)
3817   (define-key gnus-group-sort-map "v" 'gnus-group-sort-groups-by-score)
3818   (define-key gnus-group-sort-map "r" 'gnus-group-sort-groups-by-rank)
3819   (define-key gnus-group-sort-map "m" 'gnus-group-sort-groups-by-method)
3820
3821   (define-prefix-command 'gnus-group-list-map)
3822   (define-key gnus-group-mode-map "A" 'gnus-group-list-map)
3823   (define-key gnus-group-list-map "k" 'gnus-group-list-killed)
3824   (define-key gnus-group-list-map "z" 'gnus-group-list-zombies)
3825   (define-key gnus-group-list-map "s" 'gnus-group-list-groups)
3826   (define-key gnus-group-list-map "u" 'gnus-group-list-all-groups)
3827   (define-key gnus-group-list-map "A" 'gnus-group-list-active)
3828   (define-key gnus-group-list-map "a" 'gnus-group-apropos)
3829   (define-key gnus-group-list-map "d" 'gnus-group-description-apropos)
3830   (define-key gnus-group-list-map "m" 'gnus-group-list-matching)
3831   (define-key gnus-group-list-map "M" 'gnus-group-list-all-matching)
3832   (define-key gnus-group-list-map "l" 'gnus-group-list-level)
3833
3834   (define-prefix-command 'gnus-group-score-map)
3835   (define-key gnus-group-mode-map "W" 'gnus-group-score-map)
3836   (define-key gnus-group-score-map "f" 'gnus-score-flush-cache)
3837
3838   (define-prefix-command 'gnus-group-help-map)
3839   (define-key gnus-group-mode-map "H" 'gnus-group-help-map)
3840   (define-key gnus-group-help-map "f" 'gnus-group-fetch-faq)
3841
3842   (define-prefix-command 'gnus-group-sub-map)
3843   (define-key gnus-group-mode-map "S" 'gnus-group-sub-map)
3844   (define-key gnus-group-sub-map "l" 'gnus-group-set-current-level)
3845   (define-key gnus-group-sub-map "t" 'gnus-group-unsubscribe-current-group)
3846   (define-key gnus-group-sub-map "s" 'gnus-group-unsubscribe-group)
3847   (define-key gnus-group-sub-map "k" 'gnus-group-kill-group)
3848   (define-key gnus-group-sub-map "y" 'gnus-group-yank-group)
3849   (define-key gnus-group-sub-map "w" 'gnus-group-kill-region)
3850   (define-key gnus-group-sub-map "\C-k" 'gnus-group-kill-level)
3851   (define-key gnus-group-sub-map "z" 'gnus-group-kill-all-zombies))
3852
3853 (defun gnus-group-mode ()
3854   "Major mode for reading news.
3855
3856 All normal editing commands are switched off.
3857 \\<gnus-group-mode-map>
3858 The group buffer lists (some of) the groups available.  For instance,
3859 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
3860 lists all zombie groups. 
3861
3862 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe 
3863 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'. 
3864
3865 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]'). 
3866
3867 The following commands are available:
3868
3869 \\{gnus-group-mode-map}"
3870   (interactive)
3871   (when (and menu-bar-mode
3872              (gnus-visual-p 'group-menu 'menu))
3873     (gnus-group-make-menu-bar))
3874   (kill-all-local-variables)
3875   (gnus-simplify-mode-line)
3876   (setq major-mode 'gnus-group-mode)
3877   (setq mode-name "Group")
3878   (gnus-group-set-mode-line)
3879   (setq mode-line-process nil)
3880   (use-local-map gnus-group-mode-map)
3881   (buffer-disable-undo (current-buffer))
3882   (setq truncate-lines t)
3883   (setq buffer-read-only t)
3884   (run-hooks 'gnus-group-mode-hook))
3885
3886 (defun gnus-mouse-pick-group (e)
3887   "Enter the group under the mouse pointer."
3888   (interactive "e")
3889   (mouse-set-point e)
3890   (gnus-group-read-group nil))
3891
3892 ;; Look at LEVEL and find out what the level is really supposed to be.
3893 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
3894 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
3895 (defun gnus-group-default-level (&optional level number-or-nil)
3896   (cond  
3897    (gnus-group-use-permanent-levels
3898     (setq gnus-group-default-list-level 
3899           (or level gnus-group-default-list-level))
3900     (or gnus-group-default-list-level gnus-level-subscribed))
3901    (number-or-nil
3902     level)
3903    (t
3904     (or level gnus-group-default-list-level gnus-level-subscribed))))
3905   
3906 ;;;###autoload
3907 (defun gnus-slave-no-server (&optional arg)
3908   "Read network news as a slave, without connecting to local server"
3909   (interactive "P")
3910   (gnus-no-server arg t))
3911
3912 ;;;###autoload
3913 (defun gnus-no-server (&optional arg slave)
3914   "Read network news.
3915 If ARG is a positive number, Gnus will use that as the
3916 startup level.  If ARG is nil, Gnus will be started at level 2. 
3917 If ARG is non-nil and not a positive number, Gnus will
3918 prompt the user for the name of an NNTP server to use.
3919 As opposed to `gnus', this command will not connect to the local server."
3920   (interactive "P")
3921   (make-local-variable 'gnus-group-use-permanent-levels)
3922   (setq gnus-group-use-permanent-levels t)
3923   (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
3924
3925 ;;;###autoload
3926 (defun gnus-slave (&optional arg)
3927   "Read news as a slave."
3928   (interactive "P")
3929   (gnus arg nil 'slave))
3930
3931 ;;;###autoload
3932 (defun gnus (&optional arg dont-connect slave)
3933   "Read network news.
3934 If ARG is non-nil and a positive number, Gnus will use that as the
3935 startup level.  If ARG is non-nil and not a positive number, Gnus will
3936 prompt the user for the name of an NNTP server to use."
3937   (interactive "P")
3938   (if (get-buffer gnus-group-buffer)
3939       (progn
3940         (switch-to-buffer gnus-group-buffer)
3941         (gnus-group-get-new-news))
3942
3943     (gnus-clear-system)
3944     (nnheader-init-server-buffer)
3945     (gnus-read-init-file)
3946     (setq gnus-slave slave)
3947
3948     (gnus-group-setup-buffer)
3949     (let ((buffer-read-only nil))
3950       (erase-buffer)
3951       (if (not gnus-inhibit-startup-message)
3952           (progn
3953             (gnus-group-startup-message)
3954             (sit-for 0))))
3955     
3956     (let ((level (and arg (numberp arg) (> arg 0) arg))
3957           did-connect)
3958       (unwind-protect
3959           (progn
3960             (or dont-connect 
3961                 (setq did-connect
3962                       (gnus-start-news-server (and arg (not level))))))
3963         (if (and (not dont-connect) 
3964                  (not did-connect))
3965             (gnus-group-quit)
3966           (run-hooks 'gnus-startup-hook)
3967           ;; NNTP server is successfully open. 
3968
3969           ;; Find the current startup file name.
3970           (setq gnus-current-startup-file 
3971                 (gnus-make-newsrc-file gnus-startup-file))
3972
3973           ;; Read the dribble file.
3974           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
3975
3976           (gnus-summary-make-display-table)
3977           ;; Do the actual startup.
3978           (gnus-setup-news nil level)
3979           ;; Generate the group buffer.
3980           (gnus-group-list-groups level)
3981           (gnus-configure-windows 'group)
3982           (gnus-group-set-mode-line))))))
3983
3984 (defun gnus-unload ()
3985   "Unload all Gnus features."
3986   (interactive)
3987   (or (boundp 'load-history)
3988       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
3989   (let ((history load-history)
3990         feature)
3991     (while history
3992       (and (string-match "^gnus" (car (car history)))
3993            (setq feature (cdr (assq 'provide (car history))))
3994            (unload-feature feature 'force))
3995       (setq history (cdr history)))))
3996
3997 (defun gnus-compile ()
3998   "Byte-compile the Gnus startup file.
3999 This will also compile the user-defined format specs."
4000   (interactive)
4001   (let ((file (concat (make-temp-name "/tmp/gnuss") ".el")))
4002     (save-excursion
4003       (gnus-message 7 "Compiling user file...")
4004       (nnheader-set-temp-buffer " *compile gnus*")
4005       (and (file-exists-p gnus-init-file)
4006            (insert-file gnus-init-file))
4007       (goto-char (point-max))
4008
4009       (let ((formats '(summary summary-dummy group 
4010                                summary-mode group-mode article-mode))
4011             format fs)
4012         
4013         (while formats
4014           (setq format (symbol-name (car formats))
4015                 formats (cdr formats)
4016                 fs (cons (symbol-value 
4017                           (intern (format "gnus-%s-line-format" format)))
4018                          fs))
4019           (insert "(defun gnus-" format "-line-format-spec ()\n")
4020           (insert 
4021            (prin1-to-string
4022             (symbol-value 
4023              (intern (format "gnus-%s-line-format-spec" format)))))
4024           (insert ")\n")
4025           (insert "(setq gnus-" format 
4026                   "-line-format-spec (list 'gnus-byte-code 'gnus-"
4027                   format "-line-format-spec))\n"))
4028
4029         (insert "(setq gnus-old-specs '" (prin1-to-string fs) ")\n")
4030
4031         (write-region (point-min) (point-max) file nil 'silent)
4032         (byte-compile-file file)
4033         (rename-file
4034          (concat file "c") 
4035          (concat gnus-init-file 
4036                  (if (string-match "\\.el$" gnus-init-file) "c" ".elc"))
4037          t)
4038         (when (file-exists-p file)
4039           (delete-file file))
4040         (kill-buffer (current-buffer)))
4041       (gnus-message 7 "Compiling user file...done"))))
4042
4043 (defun gnus-indent-rigidly (start end arg)
4044   "Indent rigidly using only spaces and no tabs."
4045   (save-excursion
4046     (save-restriction
4047       (narrow-to-region start end)
4048       (indent-rigidly start end arg)
4049       (goto-char (point-min))
4050       (while (search-forward "\t" nil t)
4051         (replace-match "        " t t)))))
4052
4053 (defun gnus-group-startup-message (&optional x y)
4054   "Insert startup message in current buffer."
4055   ;; Insert the message.
4056   (erase-buffer)
4057   (insert
4058    (format "              %s
4059           _    ___ _             _      
4060           _ ___ __ ___  __    _ ___     
4061           __   _     ___    __  ___     
4062               _           ___     _     
4063              _  _ __             _      
4064              ___   __            _      
4065                    __           _       
4066                     _      _   _        
4067                    _      _    _        
4068                       _  _    _         
4069                   __  ___               
4070                  _   _ _     _          
4071                 _   _                   
4072               _    _                    
4073              _    _                     
4074             _                         
4075           __                             
4076
4077
4078            ""))
4079   ;; And then hack it.
4080   (gnus-indent-rigidly (point-min) (point-max) 
4081                        (/ (max (- (window-width) (or x 46)) 0) 2))
4082   (goto-char (point-min))
4083   (forward-line 1)
4084   (let* ((pheight (count-lines (point-min) (point-max)))
4085          (wheight (window-height))
4086          (rest (- wheight pheight)))
4087     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4088   ;; Fontify some.
4089   (goto-char (point-min))
4090   (and (search-forward "Praxis" nil t)
4091        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4092   (goto-char (point-min))
4093   (let* ((mode-string (gnus-group-set-mode-line)))
4094     (setq mode-line-buffer-identification 
4095           (list (concat gnus-version (substring (car mode-string) 4))))
4096     (set-buffer-modified-p t)))
4097
4098 (defun gnus-group-startup-message-old (&optional x y)
4099   "Insert startup message in current buffer."
4100   ;; Insert the message.
4101   (erase-buffer)
4102   (insert
4103    (format "
4104      %s
4105            A newsreader 
4106       for GNU Emacs
4107
4108         Based on GNUS 
4109              written by 
4110      Masanobu UMEDA
4111
4112        A Praxis Release
4113       larsi@ifi.uio.no
4114
4115            gnus-version))
4116   ;; And then hack it.
4117   ;; 18 is the longest line.
4118   (indent-rigidly (point-min) (point-max) 
4119                   (/ (max (- (window-width) (or x 28)) 0) 2))
4120   (goto-char (point-min))
4121   ;; +4 is fuzzy factor.
4122   (insert-char ?\n (/ (max (- (window-height) (or y 12)) 0) 2))
4123
4124   ;; Fontify some.
4125   (goto-char (point-min))
4126   (search-forward "Praxis")
4127   (put-text-property (match-beginning 0) (match-end 0) 'face 'bold)
4128   (goto-char (point-min)))
4129
4130 (defun gnus-group-setup-buffer ()
4131   (or (get-buffer gnus-group-buffer)
4132       (progn
4133         (switch-to-buffer gnus-group-buffer)
4134         (gnus-add-current-to-buffer-list)
4135         (gnus-group-mode)
4136         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4137
4138 (defun gnus-group-list-groups (&optional level unread lowest)
4139   "List newsgroups with level LEVEL or lower that have unread articles.
4140 Default is all subscribed groups.
4141 If argument UNREAD is non-nil, groups with no unread articles are also
4142 listed." 
4143   (interactive (list (if current-prefix-arg
4144                          (prefix-numeric-value current-prefix-arg)
4145                        (or
4146                         (gnus-group-default-level nil t)
4147                         gnus-group-default-list-level
4148                         gnus-level-subscribed))))
4149   (or level
4150       (setq level (car gnus-group-list-mode)
4151             unread (cdr gnus-group-list-mode)))
4152   (setq level (gnus-group-default-level level))
4153   (gnus-group-setup-buffer)             ;May call from out of group buffer
4154   (gnus-update-format-specifications)
4155   (let ((case-fold-search nil)
4156         (group (gnus-group-group-name)))
4157     (funcall gnus-group-prepare-function level unread lowest)
4158     (if (zerop (buffer-size))
4159         (gnus-message 5 gnus-no-groups-message)
4160       (goto-char (point-min))
4161       (if (not group)
4162           ;; Go to the first group with unread articles.
4163           (gnus-group-search-forward nil nil nil t)
4164         ;; Find the right group to put point on.  If the current group
4165         ;; has disapeared in the new listing, try to find the next
4166         ;; one.  If no next one can be found, just leave point at the
4167         ;; first newsgroup in the buffer.
4168         (if (not (gnus-goto-char
4169                   (text-property-any
4170                    (point-min) (point-max) 
4171                    'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4172             (let ((newsrc (nthcdr 3 (gnus-gethash group gnus-newsrc-hashtb))))
4173               (while (and newsrc
4174                           (not (gnus-goto-char 
4175                                 (text-property-any 
4176                                  (point-min) (point-max) 'gnus-group 
4177                                  (gnus-intern-safe 
4178                                   (car (car newsrc)) gnus-active-hashtb)))))
4179                 (setq newsrc (cdr newsrc)))
4180               (or newsrc (progn (goto-char (point-max))
4181                                 (forward-line -1))))))
4182       ;; Adjust cursor point.
4183       (gnus-group-position-point))))
4184
4185 (defun gnus-group-list-level (level &optional all)
4186   "List groups on LEVEL.
4187 If ALL (the prefix), also list groups that have no unread articles."
4188   (interactive "nList groups on level: \nP")
4189   (gnus-group-list-groups level all level))
4190
4191 (defun gnus-group-prepare-flat (level &optional all lowest regexp) 
4192   "List all newsgroups with unread articles of level LEVEL or lower.
4193 If ALL is non-nil, list groups that have no unread articles.
4194 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4195 If REGEXP, only list groups matching REGEXP."
4196   (set-buffer gnus-group-buffer)
4197   (setq gnus-topic-indentation "")
4198   (let ((buffer-read-only nil)
4199         (newsrc (cdr gnus-newsrc-alist))
4200         (lowest (or lowest 1))
4201         info clevel unread group params)
4202     (erase-buffer)
4203     (if (< lowest gnus-level-zombie)
4204         ;; List living groups.
4205         (while newsrc
4206           (setq info (car newsrc)
4207                 group (gnus-info-group info)
4208                 params (gnus-info-params info)
4209                 newsrc (cdr newsrc)
4210                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4211           (and unread                   ; This group might be bogus
4212                (or (not regexp)
4213                    (string-match regexp group))
4214                (<= (setq clevel (gnus-info-level info)) level) 
4215                (>= clevel lowest)
4216                (or all                  ; We list all groups?
4217                    (eq unread t)        ; We list unactivated groups
4218                    (> unread 0)         ; We list groups with unread articles
4219                    (cdr (assq 'tick (gnus-info-marks info)))
4220                                         ; And groups with tickeds
4221                    ;; Check for permanent visibility.
4222                    (and gnus-permanently-visible-groups
4223                         (string-match gnus-permanently-visible-groups
4224                                       group))
4225                    (memq 'visible params)
4226                    (cdr (assq 'visible params)))
4227                (gnus-group-insert-group-line 
4228                 group (gnus-info-level info) 
4229                 (gnus-info-marks info) unread (gnus-info-method info)))))
4230       
4231     ;; List dead groups.
4232     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4233          (gnus-group-prepare-flat-list-dead 
4234           (setq gnus-zombie-list (sort gnus-zombie-list 'string<)) 
4235           gnus-level-zombie ?Z
4236           regexp))
4237     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4238          (gnus-group-prepare-flat-list-dead 
4239           (setq gnus-killed-list (sort gnus-killed-list 'string<)) 
4240           gnus-level-killed ?K regexp))
4241
4242     (gnus-group-set-mode-line)
4243     (setq gnus-group-list-mode (cons level all))
4244     (run-hooks 'gnus-group-prepare-hook)))
4245
4246 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4247   ;; List zombies and killed lists somehwat faster, which was
4248   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4249   ;; this by ignoring the group format specification altogether.
4250   (let (group beg)
4251     (if regexp
4252         ;; This loop is used when listing groups that match some
4253         ;; regexp. 
4254         (while groups
4255           (setq group (pop groups))
4256           (when (string-match regexp group)
4257             (add-text-properties 
4258              (point) (prog1 (1+ (point))
4259                        (insert " " mark "     *: " group "\n"))
4260              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4261                    'gnus-unread t
4262                    'gnus-level level))))
4263       ;; This loop is used when listing all groups.
4264       (while groups
4265         (add-text-properties 
4266          (point) (prog1 (1+ (point))
4267                    (insert " " mark "     *: " 
4268                            (setq group (pop groups)) "\n"))
4269          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4270                'gnus-unread t
4271                'gnus-level level))))))
4272
4273 (defmacro gnus-group-real-name (group)
4274   "Find the real name of a foreign newsgroup."
4275   `(let ((gname ,group))
4276      (if (string-match ":[^:]+$" gname)
4277          (substring gname (1+ (match-beginning 0)))
4278        gname)))
4279
4280 (defsubst gnus-server-add-address (method)
4281   (let ((method-name (symbol-name (car method))))
4282     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4283              (not (assq (intern (concat method-name "-address")) method)))
4284         (append method (list (list (intern (concat method-name "-address"))
4285                                    (nth 1 method))))
4286       method)))
4287
4288 (defsubst gnus-server-get-method (group method)
4289   ;; Input either a server name, and extended server name, or a
4290   ;; select method, and return a select method. 
4291   (cond ((stringp method)
4292          (gnus-server-to-method method))
4293         ((and (stringp (car method)) group)
4294          (gnus-server-extend-method group method))
4295         (t
4296          (gnus-server-add-address method))))
4297
4298 (defun gnus-server-to-method (server)
4299   "Map virtual server names to select methods."
4300   (or (and (equal server "native") gnus-select-method)
4301       (cdr (assoc server gnus-server-alist))))
4302
4303 (defun gnus-group-prefixed-name (group method)
4304   "Return the whole name from GROUP and METHOD."
4305   (and (stringp method) (setq method (gnus-server-to-method method)))
4306   (concat (format "%s" (car method))
4307           (if (and 
4308                (assoc (format "%s" (car method)) (gnus-methods-using 'address))
4309                (not (string= (nth 1 method) "")))
4310               (concat "+" (nth 1 method)))
4311           ":" group))
4312
4313 (defun gnus-group-real-prefix (group)
4314   "Return the prefix of the current group name."
4315   (if (string-match "^[^:]+:" group)
4316       (substring group 0 (match-end 0))
4317     ""))
4318
4319 (defun gnus-group-method-name (group)
4320   "Return the method used for selecting GROUP."
4321   (let ((prefix (gnus-group-real-prefix group)))
4322     (if (equal prefix "")
4323         gnus-select-method
4324       (if (string-match "^[^\\+]+\\+" prefix)
4325           (list (intern (substring prefix 0 (1- (match-end 0))))
4326                 (substring prefix (match-end 0) (1- (length prefix))))
4327         (list (intern (substring prefix 0 (1- (length prefix)))) "")))))
4328
4329 (defsubst gnus-secondary-method-p (method)
4330   "Return whether METHOD is a secondary select method."
4331   (let ((methods gnus-secondary-select-methods)
4332         (gmethod (gnus-server-get-method nil method)))
4333     (while (and methods
4334                 (not (equal (gnus-server-get-method nil (car methods)) 
4335                             gmethod)))
4336       (setq methods (cdr methods)))
4337     methods))
4338
4339 (defun gnus-group-foreign-p (group)
4340   "Say whether a group is foreign or not."
4341   (and (not (gnus-group-native-p group))
4342        (not (gnus-group-secondary-p group))))
4343
4344 (defun gnus-group-native-p (group)
4345   "Say whether the group is native or not."
4346   (not (string-match ":" group)))
4347
4348 (defun gnus-group-secondary-p (group)
4349   "Say whether the group is secondary or not."
4350   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4351
4352 (defun gnus-group-get-parameter (group &optional symbol)
4353   "Returns the group parameters for GROUP.
4354 If SYMBOL, return the value of that symbol in the group parameters."
4355   (let ((params (gnus-info-params (gnus-get-info group))))
4356     (if symbol
4357         (gnus-group-parameter-value params symbol)
4358       params)))
4359
4360 (defun gnus-group-parameter-value (params symbol)
4361   "Return the value of SYMBOL in group PARAMS."
4362   (or (car (memq symbol params))        ; It's either a simple symbol
4363       (cdr (assq symbol params))))      ; or a cons.
4364
4365 (defun gnus-group-add-parameter (group param)
4366   "Add parameter PARAM to GROUP."
4367   (let ((info (gnus-get-info group)))
4368     (if (not info)
4369         () ; This is a dead group.  We just ignore it.
4370       ;; Cons the new param to the old one and update.
4371       (gnus-group-set-info (cons param (gnus-info-params info)) 
4372                            group 'params))))
4373
4374 (defun gnus-group-add-score (group &optional score)
4375   "Add SCORE to the GROUP score.  
4376 If SCORE is nil, add 1 to the score of GROUP."
4377   (let ((info (gnus-get-info group)))
4378     (gnus-info-set-score info (+ (gnus-info-score info) (or score 1)))))
4379
4380 (defun gnus-summary-bubble-group ()
4381   "Increase the score of the current group.
4382 This is a handy function to add to `gnus-summary-exit-hook' to
4383 increase the score of each group you read."
4384   (gnus-group-add-score gnus-newsgroup-name))
4385
4386 (defun gnus-group-set-info (info &optional method-only-group part)
4387   (let* ((entry (gnus-gethash
4388                  (or method-only-group (gnus-info-group info))
4389                  gnus-newsrc-hashtb))
4390          (part-info info)
4391          (info (if method-only-group (nth 2 entry) info)))
4392     (when method-only-group
4393       (unless entry
4394         (error "Trying to change non-existent group %s" method-only-group))
4395       ;; We have recevied parts of the actual group info - either the
4396       ;; select method or the group parameters.  We first check
4397       ;; whether we have to extend the info, and if so, do that.
4398       (let ((len (length info))
4399             (total (if (eq part 'method) 5 6)))
4400         (when (< len total)
4401           (setcdr (nthcdr (1- len) info)
4402                   (make-list (- total len) nil)))
4403         ;; Then we enter the new info.
4404         (setcar (nthcdr (1- total) info) part-info)))
4405     (unless entry
4406       ;; This is a new group, so we just create it.
4407       (save-excursion
4408         (set-buffer gnus-group-buffer)
4409         (if (gnus-info-method info)
4410             ;; It's a foreign group...
4411             (gnus-group-make-group 
4412              (gnus-group-real-name (gnus-info-group info))
4413              (prin1-to-string (car (gnus-info-method info)))
4414              (nth 1 (gnus-info-method info)))
4415           ;; It's a native group.
4416           (gnus-group-make-group (gnus-info-group info)))
4417         (gnus-message 6 "Note: New group created")
4418         (setq entry 
4419               (gnus-gethash (gnus-group-prefixed-name 
4420                              (gnus-group-real-name (gnus-info-group info))
4421                              (or (gnus-info-method info) gnus-select-method))
4422                             gnus-newsrc-hashtb))))
4423     ;; Whether it was a new group or not, we now have the entry, so we
4424     ;; can do the update.
4425     (if entry
4426         (progn
4427           (setcar (nthcdr 2 entry) info)
4428           (when (and (not (eq (car entry) t)) 
4429                      (gnus-active (gnus-info-group info)))
4430             (let ((marked (gnus-info-marks info)))
4431               (setcar entry (length (gnus-list-of-unread-articles 
4432                                      (car info)))))))
4433       (error "No such group: %s" (gnus-info-group info)))))
4434
4435 (defun gnus-group-set-method-info (group select-method)
4436   (gnus-group-set-info select-method group 'method))
4437
4438 (defun gnus-group-set-params-info (group params)
4439   (gnus-group-set-info params group 'params))
4440
4441 (defun gnus-group-update-group-line ()
4442   "Update the current line in the group buffer."
4443   (let* ((buffer-read-only nil)
4444          (group (gnus-group-group-name))
4445          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4446     (and entry 
4447          (not (gnus-ephemeral-group-p group))
4448          (gnus-dribble-enter 
4449           (concat "(gnus-group-set-info '" 
4450                   (prin1-to-string (nth 2 entry)) ")")))
4451     (gnus-delete-line)
4452     (gnus-group-insert-group-line-info group)
4453     (forward-line -1)
4454     (gnus-group-position-point)))
4455
4456 (defun gnus-group-insert-group-line-info (group)
4457   "Insert GROUP on the current line."
4458   (let ((entry (gnus-gethash group gnus-newsrc-hashtb)) 
4459         active info)
4460     (if entry
4461         (progn
4462           ;; (Un)subscribed group.
4463           (setq info (nth 2 entry))
4464           (gnus-group-insert-group-line 
4465            group (gnus-info-level info) (gnus-info-marks info)
4466            (or (car entry) t) (gnus-info-method info)))
4467       ;; This group is dead.
4468       (gnus-group-insert-group-line 
4469        group 
4470        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4471        nil 
4472        (if (setq active (gnus-active group))
4473            (- (1+ (cdr active)) (car active)) 0) 
4474        nil))))
4475
4476 ;; Dummy function redefined when running under XEmacs.
4477 (defalias 'gnus-group-remove-excess-properties 'ignore)
4478
4479 (defun gnus-group-insert-group-line 
4480   (gnus-tmp-group gnus-tmp-level gnus-tmp-marked number
4481                   gnus-tmp-method)
4482   "Insert a group line in the group buffer."
4483   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4484          (gnus-tmp-number-total 
4485           (if gnus-tmp-active 
4486               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4487             0))
4488          (gnus-tmp-number-of-unread 
4489           (if (numberp number) (int-to-string (max 0 number))
4490             "*"))
4491          (gnus-tmp-number-of-read
4492           (if (numberp number)
4493               (max 0 (- gnus-tmp-number-total number))
4494             "*"))
4495          (gnus-tmp-subscribed
4496           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4497                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4498                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4499                 (t ?K)))
4500          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4501          (gnus-tmp-newsgroup-description 
4502           (if gnus-description-hashtb
4503               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4504             ""))
4505          (gnus-tmp-moderated
4506           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4507          (gnus-tmp-moderated-string 
4508           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4509          (gnus-tmp-method
4510           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4511          (gnus-tmp-news-server (or (car (cdr gnus-tmp-method)) ""))
4512          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4513          (gnus-tmp-news-method-string 
4514           (if gnus-tmp-method
4515               (format "(%s:%s)" (car gnus-tmp-method)
4516                       (car (cdr gnus-tmp-method))) ""))
4517          (gnus-tmp-marked-mark 
4518           (if (and (numberp number) 
4519                    (zerop number)
4520                    (cdr (assq 'tick gnus-tmp-marked)))
4521               ?* ? ))
4522          (gnus-tmp-number
4523           (cond ((eq number t) "*" )
4524                 ((numberp number) (int-to-string number))
4525                 (t number)))
4526          (gnus-tmp-process-marked
4527           (if (member gnus-tmp-group gnus-group-marked)
4528               gnus-process-mark ? ))
4529          (buffer-read-only nil)
4530          header)                        ; passed as parameter to user-funcs.
4531     (beginning-of-line)
4532     (add-text-properties
4533      (point)
4534      (prog1 (1+ (point))
4535        ;; Insert the text.
4536        (eval gnus-group-line-format-spec))
4537      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4538        gnus-unread ,(if (numberp number)
4539                         (string-to-int gnus-tmp-number-of-unread)
4540                       t)
4541        gnus-marked ,gnus-tmp-marked-mark
4542        gnus-level ,gnus-tmp-level))
4543     ;; Allow XEmacs to remove front-sticky text properties.
4544     (gnus-group-remove-excess-properties)))
4545
4546 (defun gnus-group-update-group (group &optional visible-only)
4547   "Update all lines where GROUP appear.
4548 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4549 already." 
4550   (save-excursion
4551     (set-buffer gnus-group-buffer)
4552     ;; The buffer may be narrowed.
4553     (save-restriction
4554       (widen)
4555       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4556             (loc (point-min))
4557             found buffer-read-only visible)
4558         ;; Enter the current status into the dribble buffer.
4559         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4560           (if (and entry (not (gnus-ephemeral-group-p group)))
4561               (gnus-dribble-enter 
4562                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4563                        ")"))))
4564         ;; Find all group instances.  If topics are in use, each group
4565         ;; may be listed in more than once.
4566         (while (setq loc (text-property-any 
4567                           loc (point-max) 'gnus-group ident))
4568           (setq found t)
4569           (goto-char loc)
4570           (gnus-delete-line)
4571           (gnus-group-insert-group-line-info group)
4572           (setq loc (1+ loc)))
4573         (if (or found visible-only)
4574             ()
4575           ;; No such line in the buffer, find out where it's supposed to
4576           ;; go, and insert it there (or at the end of the buffer).
4577           ;; Fix by Per Abrahamsen <amanda@iesd.auc.dk>.
4578           (let ((entry (cdr (cdr (gnus-gethash group gnus-newsrc-hashtb)))))
4579             (while (and entry (car entry)
4580                         (not
4581                          (gnus-goto-char
4582                           (text-property-any
4583                            (point-min) (point-max) 
4584                            'gnus-group (gnus-intern-safe 
4585                                         (car (car entry)) 
4586                                         gnus-active-hashtb)))))
4587               (setq entry (cdr entry)))
4588             (or entry (goto-char (point-max))))
4589           ;; Finally insert the line.
4590           (gnus-group-insert-group-line-info group))
4591         (gnus-group-set-mode-line)))))
4592
4593 (defun gnus-group-set-mode-line ()
4594   (when (memq 'group gnus-updated-mode-lines)
4595     (let* ((gformat (or gnus-group-mode-line-format-spec
4596                         (setq gnus-group-mode-line-format-spec
4597                               (gnus-parse-format 
4598                                gnus-group-mode-line-format 
4599                                gnus-group-mode-line-format-alist))))
4600            (gnus-tmp-news-server (car (cdr gnus-select-method)))
4601            (gnus-tmp-news-method (car gnus-select-method))
4602            (max-len 60)
4603            header                       ;Dummy binding for user-defined formats
4604            ;; Get the resulting string.
4605            (mode-string (eval gformat)))
4606       ;; If the line is too long, we chop it off.
4607       (when (> (length mode-string) max-len) 
4608         (setq mode-string (substring mode-string 0 (- max-len 4))))
4609       (prog1
4610           (setq mode-line-buffer-identification (list mode-string))
4611         (set-buffer-modified-p t)))))
4612
4613 (defun gnus-group-group-name ()
4614   "Get the name of the newsgroup on the current line."
4615   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4616     (and group (symbol-name group))))
4617
4618 (defun gnus-group-group-level ()
4619   "Get the level of the newsgroup on the current line."
4620   (get-text-property (gnus-point-at-bol) 'gnus-level))
4621
4622 (defun gnus-group-group-unread ()
4623   "Get the number of unread articles of the newsgroup on the current line."
4624   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4625
4626 (defun gnus-group-search-forward (&optional backward all level first-too)
4627   "Find the next newsgroup with unread articles.
4628 If BACKWARD is non-nil, find the previous newsgroup instead.
4629 If ALL is non-nil, just find any newsgroup.
4630 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4631 group exists.
4632 If FIRST-TOO, the current line is also eligible as a target."
4633   (let ((way (if backward -1 1))
4634         (low gnus-level-killed)
4635         (beg (point))
4636         pos found lev)
4637     (if (and backward (progn (beginning-of-line)) (bobp))
4638         nil
4639       (or first-too (forward-line way))
4640       (while (and 
4641               (not (eobp))
4642               (not (setq 
4643                     found 
4644                     (and (or all
4645                              (and
4646                               (let ((unread 
4647                                      (get-text-property (point) 'gnus-unread)))
4648                                 (and (numberp unread) (> unread 0)))
4649                               (setq lev (get-text-property (point)
4650                                                            'gnus-level))
4651                               (<= lev gnus-level-subscribed)))
4652                          (or (not level)
4653                              (and (setq lev (get-text-property (point)
4654                                                                'gnus-level))
4655                                   (or (= lev level)
4656                                       (and (< lev low)
4657                                            (< level lev)
4658                                            (progn
4659                                              (setq low lev)
4660                                              (setq pos (point))
4661                                              nil))))))))
4662               (zerop (forward-line way)))))
4663     (if found 
4664         (progn (gnus-group-position-point) t)
4665       (goto-char (or pos beg))
4666       (and pos t))))
4667
4668 ;;; Gnus group mode commands
4669
4670 ;; Group marking.
4671
4672 (defun gnus-group-mark-group (n &optional unmark no-advance)
4673   "Mark the current group."
4674   (interactive "p")
4675   (let ((buffer-read-only nil)
4676         group)
4677     (while 
4678         (and (> n 0) 
4679              (setq group (gnus-group-group-name))
4680              (progn
4681                (beginning-of-line)
4682                (forward-char 
4683                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
4684                (delete-char 1)
4685                (if unmark
4686                    (progn
4687                      (insert " ")
4688                      (setq gnus-group-marked (delete group gnus-group-marked)))
4689                  (insert "#")
4690                  (setq gnus-group-marked
4691                        (cons group (delete group gnus-group-marked))))
4692                t)
4693              (or no-advance (zerop (gnus-group-next-group 1))))
4694       (setq n (1- n)))
4695     (gnus-summary-position-point)
4696     n))
4697
4698 (defun gnus-group-unmark-group (n)
4699   "Remove the mark from the current group."
4700   (interactive "p")
4701   (gnus-group-mark-group n 'unmark))
4702
4703 (defun gnus-group-mark-region (unmark beg end)
4704   "Mark all groups between point and mark.
4705 If UNMARK, remove the mark instead."
4706   (interactive "P\nr")
4707   (let ((num (count-lines beg end)))
4708     (save-excursion
4709       (goto-char beg)
4710       (- num (gnus-group-mark-group num unmark)))))
4711
4712 (defun gnus-group-mark-regexp (regexp)
4713   "Mark all groups that match some regexp."
4714   (interactive "sMark (regexp): ")
4715   (let ((alist (cdr gnus-newsrc-alist))
4716         group)
4717     (while alist
4718       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
4719         (gnus-group-set-mark group)))))
4720
4721 (defun gnus-group-remove-mark (group)
4722   (if (gnus-group-goto-group group)
4723       (save-excursion
4724         (gnus-group-mark-group 1 'unmark t))
4725     (setq gnus-group-marked
4726           (cons group (delete group gnus-group-marked)))))
4727                 
4728 (defun gnus-group-set-mark (group)
4729   (if (gnus-group-goto-group group)
4730       (save-excursion
4731         (gnus-group-mark-group 1 nil t))
4732     (setq gnus-group-marked
4733           (cons group (delete group gnus-group-marked)))))
4734                 
4735 ;; Return a list of groups to work on.  Take into consideration N (the
4736 ;; prefix) and the list of marked groups.
4737 (defun gnus-group-process-prefix (n)
4738   (cond
4739    (n
4740     (setq n (prefix-numeric-value n))
4741     ;; There is a prefix, so we return a list of the N next
4742     ;; groups. 
4743     (let ((way (if (< n 0) -1 1))
4744           (n (abs n))
4745           group groups)
4746       (save-excursion
4747         (while (and (> n 0)
4748                     (setq group (gnus-group-group-name)))
4749           (setq groups (cons group groups))
4750           (setq n (1- n))
4751           (gnus-group-next-group way)))
4752       (nreverse groups)))
4753    ((and (boundp 'transient-mark-mode)
4754          transient-mark-mode
4755          mark-active)
4756     ;; Work on the region between point and mark.
4757     (let ((max (max (point) (mark)))
4758           groups)
4759       (save-excursion
4760         (goto-char (min (point) (mark)))
4761         (while 
4762             (and 
4763              (push (gnus-group-group-name) groups)
4764              (zerop (gnus-group-next-group 1))
4765              (< (point) max)))
4766         (nreverse groups))))
4767    (gnus-group-marked
4768     ;; No prefix, but a list of marked articles.
4769     (reverse gnus-group-marked))
4770    (t
4771     ;; Neither marked articles or a prefix, so we return the
4772     ;; current group.
4773     (let ((group (gnus-group-group-name)))
4774       (and group (list group))))))
4775
4776 ;; Selecting groups.
4777
4778 (defun gnus-group-read-group (&optional all no-article group)
4779   "Read news in this newsgroup.
4780 If the prefix argument ALL is non-nil, already read articles become
4781 readable.  IF ALL is a number, fetch this number of articles.  If the
4782 optional argument NO-ARTICLE is non-nil, no article will be
4783 auto-selected upon group entry.  If GROUP is non-nil, fetch that
4784 group."
4785   (interactive "P")
4786   (let ((group (or group (gnus-group-group-name)))
4787         number active marked entry)
4788     (or group (error "No group on current line"))
4789     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
4790                                             group gnus-newsrc-hashtb)))))
4791     ;; This group might be a dead group.  In that case we have to get
4792     ;; the number of unread articles from `gnus-active-hashtb'.
4793     (setq number
4794           (cond ((numberp all) all)
4795                 (entry (car entry))
4796                 ((setq active (gnus-active group))
4797                  (- (1+ (cdr active)) (car active)))))
4798     (gnus-summary-read-group 
4799      group (or all (and (numberp number) 
4800                         (zerop (+ number (length (cdr (assq 'tick marked)))
4801                                   (length (cdr (assq 'dormant marked)))))))
4802      no-article)))
4803
4804 (defun gnus-group-select-group (&optional all)
4805   "Select this newsgroup.
4806 No article is selected automatically.
4807 If ALL is non-nil, already read articles become readable.
4808 If ALL is a number, fetch this number of articles."
4809   (interactive "P")
4810   (gnus-group-read-group all t))
4811
4812 (defun gnus-group-quick-select-group (&optional all)
4813   "Select the current group \"quickly\". 
4814 This means that no highlighting or scoring will be performed."
4815   (interactive "P")
4816   (let (gnus-visual
4817         gnus-score-find-score-files-function
4818         gnus-apply-kill-hook
4819         gnus-summary-expunge-below)
4820     (gnus-group-read-group all t)))
4821
4822 (defun gnus-group-visible-select-group (&optional all)
4823   "Select the current group without hiding any articles."
4824   (interactive "P")
4825   (let ((gnus-inhibit-limiting t))
4826     (gnus-group-read-group all t)))
4827
4828 ;;;###autoload
4829 (defun gnus-fetch-group (group)
4830   "Start Gnus if necessary and enter GROUP.
4831 Returns whether the fetching was successful or not."
4832   (interactive "sGroup name: ")
4833   (or (get-buffer gnus-group-buffer)
4834       (gnus))
4835   (gnus-group-select-group))
4836
4837 ;; Enter a group that is not in the group buffer.  Non-nil is returned
4838 ;; if selection was successful.
4839 (defun gnus-group-read-ephemeral-group 
4840   (group method &optional activate quit-config)
4841   (let ((group (if (gnus-group-foreign-p group) group
4842                  (gnus-group-prefixed-name group method)))
4843         (cur (current-buffer)))
4844     (gnus-sethash 
4845      group
4846      (list t nil (list group gnus-level-default-subscribed nil nil 
4847                        (append method
4848                                (list
4849                                 (list 'quit-config 
4850                                       (if quit-config quit-config
4851                                         (cons (current-buffer) 'summary)))))))
4852      gnus-newsrc-hashtb)
4853     (set-buffer gnus-group-buffer)
4854     (or (gnus-check-server method)
4855         (error "Unable to contact server: %s" (gnus-status-message method)))
4856     (if activate (or (gnus-request-group group)
4857                      (error "Couldn't request group")))
4858     (condition-case ()
4859         (gnus-group-read-group t t group)
4860       (error nil)
4861       (quit nil))
4862     (not (equal (current-buffer) cur))))
4863   
4864 (defun gnus-group-jump-to-group (group)
4865   "Jump to newsgroup GROUP."
4866   (interactive 
4867    (list (completing-read 
4868           "Group: " gnus-active-hashtb nil 
4869           (memq gnus-select-method gnus-have-read-active-file))))
4870
4871   (if (equal group "")
4872       (error "Empty group name"))
4873
4874   (let ((b (text-property-any 
4875             (point-min) (point-max) 
4876             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4877     (if b
4878         ;; Either go to the line in the group buffer...
4879         (goto-char b)
4880       ;; ... or insert the line.
4881       (or
4882        (gnus-active group)
4883        (gnus-activate-group group)
4884        (error "%s error: %s" group (gnus-status-message group)))
4885
4886       (gnus-group-update-group group)
4887       (goto-char (text-property-any 
4888                   (point-min) (point-max)
4889                   'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
4890   ;; Adjust cursor point.
4891   (gnus-group-position-point))
4892
4893 (defun gnus-group-goto-group (group)
4894   "Goto to newsgroup GROUP."
4895   (when group
4896     (let ((b (text-property-any (point-min) (point-max) 
4897                                 'gnus-group (gnus-intern-safe
4898                                              group gnus-active-hashtb))))
4899       (and b (goto-char b)))))
4900
4901 (defun gnus-group-next-group (n)
4902   "Go to next N'th newsgroup.
4903 If N is negative, search backward instead.
4904 Returns the difference between N and the number of skips actually
4905 done."
4906   (interactive "p")
4907   (gnus-group-next-unread-group n t))
4908
4909 (defun gnus-group-next-unread-group (n &optional all level)
4910   "Go to next N'th unread newsgroup.
4911 If N is negative, search backward instead.
4912 If ALL is non-nil, choose any newsgroup, unread or not.
4913 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
4914 such group can be found, the next group with a level higher than
4915 LEVEL.
4916 Returns the difference between N and the number of skips actually
4917 made."
4918   (interactive "p")
4919   (let ((backward (< n 0))
4920         (n (abs n)))
4921     (while (and (> n 0)
4922                 (gnus-group-search-forward 
4923                  backward (or (not gnus-group-goto-unread) all) level))
4924       (setq n (1- n)))
4925     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
4926                                (if level " on this level or higher" "")))
4927     n))
4928
4929 (defun gnus-group-prev-group (n)
4930   "Go to previous N'th newsgroup.
4931 Returns the difference between N and the number of skips actually
4932 done."
4933   (interactive "p")
4934   (gnus-group-next-unread-group (- n) t))
4935
4936 (defun gnus-group-prev-unread-group (n)
4937   "Go to previous N'th unread newsgroup.
4938 Returns the difference between N and the number of skips actually
4939 done."  
4940   (interactive "p")
4941   (gnus-group-next-unread-group (- n)))
4942
4943 (defun gnus-group-next-unread-group-same-level (n)
4944   "Go to next N'th unread newsgroup on the same level.
4945 If N is negative, search backward instead.
4946 Returns the difference between N and the number of skips actually
4947 done."
4948   (interactive "p")
4949   (gnus-group-next-unread-group n t (gnus-group-group-level))
4950   (gnus-group-position-point))
4951
4952 (defun gnus-group-prev-unread-group-same-level (n)
4953   "Go to next N'th unread newsgroup on the same level.
4954 Returns the difference between N and the number of skips actually
4955 done."
4956   (interactive "p")
4957   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
4958   (gnus-group-position-point))
4959
4960 (defun gnus-group-best-unread-group (&optional exclude-group)
4961   "Go to the group with the highest level.
4962 If EXCLUDE-GROUP, do not go to that group."
4963   (interactive)
4964   (goto-char (point-min))
4965   (let ((best 100000)
4966         unread best-point)
4967     (while (setq unread (get-text-property (point) 'gnus-unread))
4968       (if (and (numberp unread) (> unread 0))
4969           (progn
4970             (if (and (< (get-text-property (point) 'gnus-level) best)
4971                      (or (not exclude-group)
4972                          (not (equal exclude-group (gnus-group-group-name)))))
4973                 (progn 
4974                   (setq best (get-text-property (point) 'gnus-level))
4975                   (setq best-point (point))))))
4976       (forward-line 1))
4977     (if best-point (goto-char best-point))
4978     (gnus-summary-position-point)
4979     (and best-point (gnus-group-group-name))))
4980
4981 (defun gnus-group-first-unread-group ()
4982   "Go to the first group with unread articles."
4983   (interactive)
4984   (prog1
4985       (let ((opoint (point))
4986             unread)
4987         (goto-char (point-min))
4988         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
4989                 (and (numberp unread)   ; Not a topic.
4990                      (not (zerop unread))) ; Has unread articles.
4991                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
4992             (point)                     ; Success.
4993           (goto-char opoint)
4994           nil))                         ; Not success.
4995     (gnus-group-position-point)))
4996
4997 (defun gnus-group-enter-server-mode ()
4998   "Jump to the server buffer."
4999   (interactive)
5000   (gnus-enter-server-buffer))
5001
5002 (defun gnus-group-make-group (name &optional method address)
5003   "Add a new newsgroup.
5004 The user will be prompted for a NAME, for a select METHOD, and an
5005 ADDRESS."
5006   (interactive
5007    (cons 
5008     (read-string "Group name: ")
5009     (let ((method
5010            (completing-read 
5011             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5012             nil t)))
5013       (if (assoc method gnus-valid-select-methods)
5014           (list method
5015                 (if (memq 'prompt-address
5016                           (assoc method gnus-valid-select-methods))
5017                     (read-string "Address: ")
5018                   ""))
5019         (list method nil)))))
5020   
5021   (save-excursion
5022     (set-buffer gnus-group-buffer)
5023     (let* ((meth (and method (if address (list (intern method) address) 
5024                                method)))
5025            (nname (if method (gnus-group-prefixed-name name meth) name))
5026            info)
5027       (and (gnus-gethash nname gnus-newsrc-hashtb)
5028            (error "Group %s already exists" nname))
5029       (gnus-group-change-level 
5030        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5031        gnus-level-default-subscribed gnus-level-killed 
5032        (and (gnus-group-group-name)
5033             (gnus-gethash (gnus-group-group-name)
5034                           gnus-newsrc-hashtb))
5035        t)
5036       (gnus-set-active nname (cons 1 0))
5037       (or (gnus-ephemeral-group-p name)
5038           (gnus-dribble-enter 
5039            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5040       (gnus-group-insert-group-line-info nname)
5041
5042       (if (assoc method gnus-valid-select-methods)
5043           (require (intern method)))
5044       (and (gnus-check-backend-function 'request-create-group nname)
5045            (gnus-request-create-group nname))
5046       t)))
5047
5048 (defun gnus-group-delete-group (group &optional force)
5049   "Delete the current group.
5050 If FORCE (the prefix) is non-nil, all the articles in the group will
5051 be deleted.  This is \"deleted\" as in \"removed forever from the face
5052 of the Earth\".  There is no undo."
5053   (interactive 
5054    (list (gnus-group-group-name)
5055          current-prefix-arg))
5056   (or group (error "No group to rename"))
5057   (or (gnus-check-backend-function 'request-delete-group group)
5058       (error "This backend does not support group deletion"))
5059   (prog1
5060       (if (not (gnus-yes-or-no-p
5061                 (format
5062                  "Do you really want to delete %s%s? " 
5063                  group (if force " and all its contents" ""))))
5064           () ; Whew!
5065         (gnus-message 6 "Deleting group %s..." group)
5066         (if (not (gnus-request-delete-group group force))
5067             (progn
5068               (gnus-message 3 "Couldn't delete group %s" group)
5069               (ding))
5070           (gnus-message 6 "Deleting group %s...done" group)
5071           (gnus-group-goto-group group)
5072           (gnus-group-kill-group 1 t)
5073           t))
5074     (gnus-group-position-point)))
5075
5076 (defun gnus-group-rename-group (group new-name)
5077   (interactive
5078    (list
5079     (gnus-group-group-name)
5080     (progn
5081       (or (gnus-check-backend-function 
5082            'request-rename-group (gnus-group-group-name))
5083           (error "This backend does not support renaming groups"))
5084       (read-string "New group name: "))))
5085
5086   (or (gnus-check-backend-function 'request-rename-group group)
5087       (error "This backend does not support renaming groups"))
5088
5089   (or group (error "No group to rename"))
5090   (and (string-match "^[ \t]*$" new-name) 
5091        (error "Not a valid group name"))
5092
5093   ;; We find the proper prefixed name.
5094   (setq new-name
5095         (gnus-group-prefixed-name 
5096          (gnus-group-real-name new-name)
5097          (gnus-info-method (gnus-get-info group))))
5098
5099   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5100   (prog1
5101       (if (not (gnus-request-rename-group group new-name))
5102           (progn
5103             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
5104             (ding))
5105         ;; We rename the group internally by killing it...
5106         (gnus-group-goto-group group)
5107         (gnus-group-kill-group)
5108         ;; ... changing its name ...
5109         (setcar (cdr (car gnus-list-of-killed-groups))
5110                 new-name)
5111         ;; ... and then yanking it.  Magic!
5112         (gnus-group-yank-group) 
5113         (gnus-set-active new-name (gnus-active group))
5114         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5115         new-name)
5116     (gnus-group-position-point)))
5117
5118
5119 (defun gnus-group-edit-group (group &optional part)
5120   "Edit the group on the current line."
5121   (interactive (list (gnus-group-group-name)))
5122   (let ((done-func '(lambda () 
5123                       "Exit editing mode and update the information."
5124                       (interactive)
5125                       (gnus-group-edit-group-done 'part 'group)))
5126         (part (or part 'info))
5127         (winconf (current-window-configuration))
5128         info)
5129     (or group (error "No group on current line"))
5130     (or (setq info (gnus-get-info group))
5131         (error "Killed group; can't be edited"))
5132     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5133     (gnus-configure-windows 'edit-group)
5134     (gnus-add-current-to-buffer-list)
5135     (emacs-lisp-mode)
5136     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5137     (use-local-map (copy-keymap emacs-lisp-mode-map))
5138     (local-set-key "\C-c\C-c" done-func)
5139     (make-local-variable 'gnus-prev-winconf)
5140     (setq gnus-prev-winconf winconf)
5141     ;; We modify the func to let it know what part it is editing.
5142     (setcar (cdr (nth 4 done-func)) (list 'quote part))
5143     (setcar (cdr (cdr (nth 4 done-func))) group)
5144     (erase-buffer)
5145     (insert
5146      (cond 
5147       ((eq part 'method)
5148        ";; Type `C-c C-c' after editing the select method.\n\n")
5149       ((eq part 'params)
5150        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5151       ((eq part 'info)
5152        ";; Type `C-c C-c' after editing the group info.\n\n")))
5153     (insert 
5154      (pp-to-string
5155       (cond ((eq part 'method)
5156              (or (gnus-info-method info) "native"))
5157             ((eq part 'params)
5158              (gnus-info-params info))
5159             (t info)))
5160      "\n")))
5161
5162 (defun gnus-group-edit-group-method (group)
5163   "Edit the select method of GROUP."
5164   (interactive (list (gnus-group-group-name)))
5165   (gnus-group-edit-group group 'method))
5166
5167 (defun gnus-group-edit-group-parameters (group)
5168   "Edit the group parameters of GROUP."
5169   (interactive (list (gnus-group-group-name)))
5170   (gnus-group-edit-group group 'params))
5171
5172 (defun gnus-group-edit-group-done (part group)
5173   "Get info from buffer, update variables and jump to the group buffer."
5174   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5175   (goto-char (point-min))
5176   (let* ((form (read (current-buffer)))
5177          (winconf gnus-prev-winconf)
5178          (new-group (when (eq part 'info)
5179                       (if (or (not (nth 4 form))
5180                               (gnus-server-equal
5181                                gnus-select-method (nth 4 form)))
5182                           (gnus-group-real-name (car form))
5183                         (gnus-group-prefixed-name
5184                          (gnus-group-real-name (car form)) (nth 4 form))))))
5185     ;; Set the info.
5186     (if (eq part 'info) 
5187         (progn
5188           (when new-group (setcar form new-group))
5189           (gnus-group-set-info form))
5190       (gnus-group-set-info form group part))
5191     (kill-buffer (current-buffer))
5192     (and winconf (set-window-configuration winconf))
5193     (set-buffer gnus-group-buffer)
5194     (when (and new-group 
5195              (not (equal new-group group)))
5196       (when (gnus-group-goto-group group)
5197         (gnus-group-kill-group 1))
5198       (gnus-activate-group new-group))
5199     (gnus-group-update-group (or new-group group))
5200     (gnus-group-position-point)))
5201
5202 (defun gnus-group-make-help-group ()
5203   "Create the Gnus documentation group."
5204   (interactive)
5205   (let ((path load-path)
5206         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5207         file dir)
5208     (and (gnus-gethash name gnus-newsrc-hashtb)
5209          (error "Documentation group already exists"))
5210     (while path
5211       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5212             file nil)
5213       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5214                 (file-exists-p
5215                  (setq file (concat (file-name-directory 
5216                                      (directory-file-name dir))
5217                                     "etc/gnus-tut.txt"))))
5218         (setq path nil)))
5219     (if (not file)
5220         (message "Couldn't find doc group")
5221       (gnus-group-make-group 
5222        (gnus-group-real-name name)
5223        (list 'nndoc name
5224              (list 'nndoc-address file)
5225              (list 'nndoc-article-type 'mbox)))))
5226   (gnus-group-position-point))
5227
5228 (defun gnus-group-make-doc-group (file type)
5229   "Create a group that uses a single file as the source."
5230   (interactive 
5231    (list (read-file-name "File name: ") 
5232          (and current-prefix-arg 'ask)))
5233   (when (eq type 'ask)
5234     (let ((err "")
5235           char found)
5236       (while (not found)
5237         (message 
5238          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5239          err)
5240         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5241                           ((= char ?b) 'babyl)
5242                           ((= char ?d) 'digest)
5243                           ((= char ?f) 'forward)
5244                           ((= char ?a) 'mmfd)
5245                           (t (setq err (format "%c unknown. " char))
5246                              nil))))
5247       (setq type found)))
5248   (let* ((file (expand-file-name file))
5249          (name (gnus-generate-new-group-name
5250                 (gnus-group-prefixed-name
5251                  (file-name-nondirectory file) '(nndoc "")))))
5252     (gnus-group-make-group 
5253      (gnus-group-real-name name)
5254      (list 'nndoc name
5255            (list 'nndoc-address file)
5256            (list 'nndoc-article-type (or type 'guess))))
5257     (forward-line -1)
5258     (gnus-group-position-point)))
5259
5260 (defun gnus-group-make-archive-group (&optional all)
5261   "Create the (ding) Gnus archive group of the most recent articles.
5262 Given a prefix, create a full group."
5263   (interactive "P")
5264   (let ((group (gnus-group-prefixed-name 
5265                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5266     (and (gnus-gethash group gnus-newsrc-hashtb)
5267          (error "Archive group already exists"))
5268     (gnus-group-make-group
5269      (gnus-group-real-name group)
5270      (list 'nndir (if all "hpc" "edu")
5271            (list 'nndir-directory  
5272                  (if all gnus-group-archive-directory 
5273                    gnus-group-recent-archive-directory)))))
5274   (forward-line -1)
5275   (gnus-group-position-point))
5276
5277 (defun gnus-group-make-directory-group (dir)
5278   "Create an nndir group.
5279 The user will be prompted for a directory.  The contents of this
5280 directory will be used as a newsgroup.  The directory should contain
5281 mail messages or news articles in files that have numeric names."
5282   (interactive
5283    (list (read-file-name "Create group from directory: ")))
5284   (or (file-exists-p dir) (error "No such directory"))
5285   (or (file-directory-p dir) (error "Not a directory"))
5286   (let ((ext "")
5287         (i 0)
5288         group)
5289     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5290       (setq group
5291             (gnus-group-prefixed-name 
5292              (concat (file-name-as-directory (directory-file-name dir))
5293                      ext)
5294              '(nndir "")))
5295       (setq ext (format "<%d>" (setq i (1+ i)))))
5296     (gnus-group-make-group 
5297      (gnus-group-real-name group)
5298      (list 'nndir group (list 'nndir-directory dir))))
5299   (forward-line -1)
5300   (gnus-group-position-point))
5301
5302 (defun gnus-group-make-kiboze-group (group address scores)
5303   "Create an nnkiboze group.
5304 The user will be prompted for a name, a regexp to match groups, and
5305 score file entries for articles to include in the group."
5306   (interactive
5307    (list
5308     (read-string "nnkiboze group name: ")
5309     (read-string "Source groups (regexp): ")
5310     (let ((headers (mapcar (lambda (group) (list group))
5311                            '("subject" "from" "number" "date" "message-id"
5312                              "references" "chars" "lines" "xref"
5313                              "followup" "all" "body" "head")))
5314           scores header regexp regexps)
5315       (while (not (equal "" (setq header (completing-read 
5316                                           "Match on header: " headers nil t))))
5317         (setq regexps nil)
5318         (while (not (equal "" (setq regexp (read-string 
5319                                             (format "Match on %s (string): "
5320                                                     header)))))
5321           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5322         (setq scores (cons (cons header regexps) scores)))
5323       scores)))
5324   (gnus-group-make-group group "nnkiboze" address)
5325   (save-excursion
5326     (gnus-set-work-buffer)
5327     (let (emacs-lisp-mode-hook)
5328       (pp scores (current-buffer)))
5329     (write-region (point-min) (point-max) 
5330                   (gnus-score-file-name (concat "nnkiboze:" group))))
5331   (forward-line -1)
5332   (gnus-group-position-point))
5333
5334 (defun gnus-group-add-to-virtual (n vgroup)
5335   "Add the current group to a virtual group."
5336   (interactive
5337    (list current-prefix-arg
5338          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5339                           "nnvirtual:")))
5340   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5341       (error "%s is not an nnvirtual group" vgroup))
5342   (let* ((groups (gnus-group-process-prefix n))
5343          (method (gnus-info-method (gnus-get-info vgroup))))
5344     (setcar (cdr method)
5345             (concat 
5346              (nth 1 method) "\\|"
5347              (mapconcat 
5348               (lambda (s) 
5349                 (gnus-group-remove-mark s)
5350                 (concat "\\(^" (regexp-quote s) "$\\)"))
5351               groups "\\|"))))
5352   (gnus-group-position-point))
5353
5354 (defun gnus-group-make-empty-virtual (group)
5355   "Create a new, fresh, empty virtual group."
5356   (interactive "sCreate new, empty virtual group: ")
5357   (let* ((method (list 'nnvirtual "^$"))
5358          (pgroup (gnus-group-prefixed-name group method)))
5359     ;; Check whether it exists already.
5360     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5361          (error "Group %s already exists." pgroup))
5362     ;; Subscribe the new group after the group on the current line.
5363     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5364     (gnus-group-update-group pgroup)
5365     (forward-line -1)
5366     (gnus-group-position-point)))
5367
5368 (defun gnus-group-enter-directory (dir)
5369   "Enter an ephemeral nneething group."
5370   (interactive "DDirectory to read: ")
5371   (let* ((method (list 'nneething dir))
5372          (leaf (gnus-group-prefixed-name
5373                 (file-name-nondirectory (directory-file-name dir))
5374                 method))
5375          (name (gnus-generate-new-group-name leaf)))
5376     (let ((nneething-read-only t))
5377       (or (gnus-group-read-ephemeral-group 
5378            name method t
5379            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5380                                       'summary 'group)))
5381           (error "Couldn't enter %s" dir)))))
5382
5383 ;; Group sorting commands
5384 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5385
5386 (defun gnus-group-sort-groups (func &optional reverse)
5387   "Sort the group buffer according to FUNC.
5388 If REVERSE, reverse the sorting order."
5389   (interactive (list gnus-group-sort-function
5390                      current-prefix-arg))
5391   (unless (listp func)
5392     (setq func (list func)))
5393   ;; We peel off the dummy group from the alist.
5394   (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5395     (pop gnus-newsrc-alist))
5396   ;; Do the sorting.
5397   (while func
5398     (setq gnus-newsrc-alist 
5399           (sort gnus-newsrc-alist (pop func))))
5400   (when reverse
5401     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5402   ;; Regenerate the hash table.
5403   (gnus-make-hashtable-from-newsrc-alist)
5404   (gnus-group-list-groups))
5405
5406 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5407   "Sort the group buffer alphabetically by group name.
5408 If REVERSE, sort in reverse order."
5409   (interactive "P")
5410   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5411
5412 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5413   "Sort the group buffer by number of unread articles.
5414 If REVERSE, sort in reverse order."
5415   (interactive "P")
5416   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5417
5418 (defun gnus-group-sort-groups-by-level (&optional reverse)
5419   "Sort the group buffer by group level.
5420 If REVERSE, sort in reverse order."
5421   (interactive "P")
5422   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5423
5424 (defun gnus-group-sort-groups-by-score (&optional reverse)
5425   "Sort the group buffer by group score.
5426 If REVERSE, sort in reverse order."
5427   (interactive "P")
5428   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5429
5430 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5431   "Sort the group buffer by group rank.
5432 If REVERSE, sort in reverse order."
5433   (interactive "P")
5434   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5435
5436 (defun gnus-group-sort-groups-by-method (&optional reverse)
5437   "Sort the group buffer alphabetically by backend name.
5438 If REVERSE, sort in reverse order."
5439   (interactive "P")
5440   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5441
5442 (defun gnus-group-sort-by-alphabet (info1 info2)
5443   "Sort alphabetically."
5444   (string< (gnus-info-group info1) (gnus-info-group info2)))
5445
5446 (defun gnus-group-sort-by-unread (info1 info2)
5447   "Sort by number of unread articles."
5448   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5449         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5450     (< (or (and (numberp n1) n1) 0)
5451        (or (and (numberp n2) n2) 0))))
5452
5453 (defun gnus-group-sort-by-level (info1 info2)
5454   "Sort by level."
5455   (< (gnus-info-level info1) (gnus-info-level info2)))
5456
5457 (defun gnus-group-sort-by-method (info1 info2)
5458   "Sort alphabetically by backend name."
5459   (string< (symbol-name (car (gnus-find-method-for-group
5460                               (gnus-info-group info1) info1)))
5461            (symbol-name (car (gnus-find-method-for-group 
5462                               (gnus-info-group info2) info2)))))
5463
5464 (defun gnus-group-sort-by-score (info1 info2)
5465   "Sort by group score."
5466   (< (gnus-info-score info1) (gnus-info-score info2)))
5467
5468 (defun gnus-group-sort-by-rank (info1 info2)
5469   "Sort by level and score."
5470   (let ((level1 (gnus-info-level info1))
5471         (level2 (gnus-info-level info2)))
5472     (or (< level1 level2)
5473         (and (= level1 level2)
5474              (< (gnus-info-score info1) (gnus-info-score info2))))))
5475
5476 ;; Group catching up.
5477
5478 (defun gnus-group-catchup-current (&optional n all)
5479   "Mark all articles not marked as unread in current newsgroup as read.
5480 If prefix argument N is numeric, the ARG next newsgroups will be
5481 caught up.  If ALL is non-nil, marked articles will also be marked as
5482 read.  Cross references (Xref: header) of articles are ignored.
5483 The difference between N and actual number of newsgroups that were
5484 caught up is returned."
5485   (interactive "P")
5486   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5487                gnus-expert-user
5488                (gnus-y-or-n-p
5489                 (if all
5490                     "Do you really want to mark all articles as read? "
5491                   "Mark all unread articles as read? "))))
5492       n
5493     (let ((groups (gnus-group-process-prefix n))
5494           (ret 0))
5495       (while groups
5496         ;; Virtual groups have to be given special treatment. 
5497         (let ((method (gnus-find-method-for-group (car groups))))
5498           (if (eq 'nnvirtual (car method))
5499               (nnvirtual-catchup-group
5500                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5501         (gnus-group-remove-mark (car groups))
5502         (if (prog1
5503                 (gnus-group-goto-group (car groups))
5504               (gnus-group-catchup (car groups) all))
5505             (gnus-group-update-group-line)
5506           (setq ret (1+ ret)))
5507         (setq groups (cdr groups)))
5508       (gnus-group-next-unread-group 1)
5509       ret)))
5510
5511 (defun gnus-group-catchup-current-all (&optional n)
5512   "Mark all articles in current newsgroup as read.
5513 Cross references (Xref: header) of articles are ignored."
5514   (interactive "P")
5515   (gnus-group-catchup-current n 'all))
5516
5517 (defun gnus-group-catchup (group &optional all)
5518   "Mark all articles in GROUP as read.
5519 If ALL is non-nil, all articles are marked as read.
5520 The return value is the number of articles that were marked as read,
5521 or nil if no action could be taken."
5522   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5523          (num (car entry))
5524          (marked (nth 3 (nth 2 entry))))
5525     (if (not (numberp (car entry)))
5526         (gnus-message 1 "Can't catch up; non-active group")
5527       ;; Do the updating only if the newsgroup isn't killed.
5528       (when entry
5529         (gnus-update-read-articles group nil)
5530         ;; Also nix out the lists of marks and dormants. 
5531         (when all 
5532           (gnus-add-marked-articles group 'tick nil nil 'force)
5533           (gnus-add-marked-articles group 'dormant nil nil 'force))
5534         num))))
5535
5536 (defun gnus-group-expire-articles (&optional n)
5537   "Expire all expirable articles in the current newsgroup."
5538   (interactive "P")
5539   (let ((groups (gnus-group-process-prefix n))
5540         group)
5541     (unless groups
5542       (error "No groups to expire"))
5543     (while groups
5544       (setq group (pop groups))
5545       (gnus-group-remove-mark group)
5546       (when (gnus-check-backend-function 'request-expire-articles group)
5547         (let* ((info (gnus-get-info group))
5548                (expirable (if (gnus-group-total-expirable-p group)
5549                               (cons nil (gnus-list-of-read-articles group))
5550                             (assq 'expire (gnus-info-marks info))))
5551                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
5552           (when expirable 
5553             (setcdr expirable
5554                     (gnus-compress-sequence
5555                      (if expiry-wait
5556                          (let ((nnmail-expiry-wait-function nil)
5557                                (nnmail-expiry-wait expiry-wait))
5558                            (gnus-request-expire-articles 
5559                             (gnus-uncompress-sequence (cdr expirable)) group))
5560                        (gnus-request-expire-articles 
5561                         (gnus-uncompress-sequence (cdr expirable))
5562                         group))))))))))
5563
5564 (defun gnus-group-expire-all-groups ()
5565   "Expire all expirable articles in all newsgroups."
5566   (interactive)
5567   (save-excursion
5568     (gnus-message 5 "Expiring...")
5569     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
5570                                      (cdr gnus-newsrc-alist))))
5571       (gnus-group-expire-articles nil)))
5572   (gnus-group-position-point)
5573   (gnus-message 5 "Expiring...done"))
5574
5575 (defun gnus-group-set-current-level (n level)
5576   "Set the level of the next N groups to LEVEL."
5577   (interactive 
5578    (list
5579     current-prefix-arg
5580     (string-to-int
5581      (let ((s (read-string 
5582                (format "Level (default %s): " (gnus-group-group-level)))))
5583        (if (string-match "^\\s-*$" s)
5584            (int-to-string (gnus-group-group-level))
5585          s)))))
5586   (or (and (>= level 1) (<= level gnus-level-killed))
5587       (error "Illegal level: %d" level))
5588   (let ((groups (gnus-group-process-prefix n))
5589         group)
5590     (while groups
5591       (setq group (car groups)
5592             groups (cdr groups))
5593       (gnus-group-remove-mark group)
5594       (gnus-message 6 "Changed level of %s from %d to %d" 
5595                     group (gnus-group-group-level) level)
5596       (gnus-group-change-level group level
5597                                (gnus-group-group-level))
5598       (gnus-group-update-group-line)))
5599   (gnus-group-position-point))
5600
5601 (defun gnus-group-unsubscribe-current-group (&optional n)
5602   "Toggle subscription of the current group.
5603 If given numerical prefix, toggle the N next groups."
5604   (interactive "P")
5605   (let ((groups (gnus-group-process-prefix n))
5606         group)
5607     (while groups
5608       (setq group (car groups)
5609             groups (cdr groups))
5610       (gnus-group-remove-mark group)
5611       (gnus-group-unsubscribe-group
5612        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
5613                  gnus-level-default-unsubscribed
5614                gnus-level-default-subscribed) t)
5615       (gnus-group-update-group-line))
5616     (gnus-group-next-group 1)))
5617
5618 (defun gnus-group-unsubscribe-group (group &optional level silent)
5619   "Toggle subscription to GROUP.
5620 Killed newsgroups are subscribed.  If SILENT, don't try to update the
5621 group line."
5622   (interactive
5623    (list (completing-read
5624           "Group: " gnus-active-hashtb nil 
5625           (memq gnus-select-method gnus-have-read-active-file))))
5626   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
5627     (cond
5628      ((string-match "^[ \t]$" group)
5629       (error "Empty group name"))
5630      (newsrc
5631       ;; Toggle subscription flag.
5632       (gnus-group-change-level 
5633        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc)) 
5634                                       gnus-level-subscribed) 
5635                                   (1+ gnus-level-subscribed)
5636                                 gnus-level-default-subscribed)))
5637       (unless silent
5638         (gnus-group-update-group group)))
5639      ((and (stringp group)
5640            (or (not (memq gnus-select-method gnus-have-read-active-file))
5641                (gnus-active group)))
5642       ;; Add new newsgroup.
5643       (gnus-group-change-level 
5644        group 
5645        (if level level gnus-level-default-subscribed) 
5646        (or (and (member group gnus-zombie-list) 
5647                 gnus-level-zombie) 
5648            gnus-level-killed)
5649        (and (gnus-group-group-name)
5650             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
5651       (unless silent
5652         (gnus-group-update-group group)))
5653      (t (error "No such newsgroup: %s" group)))
5654     (gnus-group-position-point)))
5655
5656 (defun gnus-group-transpose-groups (n)
5657   "Move the current newsgroup up N places.
5658 If given a negative prefix, move down instead.  The difference between
5659 N and the number of steps taken is returned." 
5660   (interactive "p")
5661   (or (gnus-group-group-name)
5662       (error "No group on current line"))
5663   (gnus-group-kill-group 1)
5664   (prog1
5665       (forward-line (- n))
5666     (gnus-group-yank-group)
5667     (gnus-group-position-point)))
5668
5669 (defun gnus-group-kill-all-zombies ()
5670   "Kill all zombie newsgroups."
5671   (interactive)
5672   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
5673   (setq gnus-zombie-list nil)
5674   (gnus-group-list-groups))
5675
5676 (defun gnus-group-kill-region (begin end)
5677   "Kill newsgroups in current region (excluding current point).
5678 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
5679   (interactive "r")
5680   (let ((lines
5681          ;; Count lines.
5682          (save-excursion
5683            (count-lines
5684             (progn
5685               (goto-char begin)
5686               (beginning-of-line)
5687               (point))
5688             (progn
5689               (goto-char end)
5690               (beginning-of-line)
5691               (point))))))
5692     (goto-char begin)
5693     (beginning-of-line)                 ;Important when LINES < 1
5694     (gnus-group-kill-group lines)))
5695
5696 (defun gnus-group-kill-group (&optional n discard)
5697   "Kill the next N groups.
5698 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
5699 However, only groups that were alive can be yanked; already killed 
5700 groups or zombie groups can't be yanked.
5701 The return value is the name of the group that was killed, or a list
5702 of groups killed."
5703   (interactive "P")
5704   (let ((buffer-read-only nil)
5705         (groups (gnus-group-process-prefix n))
5706         group entry level out)
5707     (if (< (length groups) 10)
5708         ;; This is faster when there are few groups.
5709         (while groups
5710           (push (setq group (pop groups)) out)
5711           (gnus-group-remove-mark group)
5712           (setq level (gnus-group-group-level))
5713           (gnus-delete-line)
5714           (if (and (not discard)
5715                    (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
5716               (setq gnus-list-of-killed-groups 
5717                     (cons (cons (car entry) (nth 2 entry)) 
5718                           gnus-list-of-killed-groups)))
5719           (gnus-group-change-level 
5720            (if entry entry group) gnus-level-killed (if entry nil level)))
5721       ;; If there are lots and lots of groups to be killed, we use
5722       ;; this thing instead.
5723       (let (entry)
5724         (setq groups (nreverse groups))
5725         (while groups
5726           (gnus-group-remove-mark (car groups))
5727           (gnus-delete-line)
5728           (setq entry (gnus-gethash (pop groups) gnus-newsrc-hashtb))
5729           (push (cons (car entry) (nth 2 entry))
5730                 gnus-list-of-killed-groups)
5731           (setcdr (cdr entry) (cdr (cdr (cdr entry)))))
5732         (gnus-make-hashtable-from-newsrc-alist)))
5733     
5734     (gnus-group-position-point)
5735     (if (< (length out) 2) (car out) (nreverse out))))
5736
5737 (defun gnus-group-yank-group (&optional arg)
5738   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
5739 inserting it before the current newsgroup.  The numeric ARG specifies
5740 how many newsgroups are to be yanked.  The name of the newsgroup yanked
5741 is returned, or (if several groups are yanked) a list of yanked groups
5742 is returned."
5743   (interactive "p")
5744   (setq arg (or arg 1))
5745   (let (info group prev out)
5746     (while (>= (decf arg) 0)
5747       (if (not (setq info (pop gnus-list-of-killed-groups)))
5748           (error "No more newsgroups to yank"))
5749       (push (setq group (nth 1 info)) out)
5750       ;; Find which newsgroup to insert this one before - search
5751       ;; backward until something suitable is found.  If there are no
5752       ;; other newsgroups in this buffer, just make this newsgroup the
5753       ;; first newsgroup.
5754       (setq prev (gnus-group-group-name))
5755       (gnus-group-change-level 
5756        info (nth 2 info) gnus-level-killed 
5757        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
5758        t)
5759       (gnus-group-insert-group-line-info group))
5760     (forward-line -1)
5761     (gnus-group-position-point)
5762     (if (< (length out) 2) (car out) (nreverse out))))
5763
5764 (defun gnus-group-kill-level (level)
5765   "Kill all groups that is on a certain LEVEL."
5766   (interactive "nKill all groups on level: ")
5767   (cond 
5768    ((= level gnus-level-zombie)
5769     (setq gnus-killed-list
5770           (nconc gnus-zombie-list gnus-killed-list))
5771     (setq gnus-zombie-list nil))
5772    ((and (< level gnus-level-zombie)
5773          (> level 0)
5774          (or gnus-expert-user
5775              (gnus-yes-or-no-p
5776               (format 
5777                "Do you really want to kill all groups on level %d? "
5778                level))))
5779     (let* ((prev gnus-newsrc-alist)
5780            (alist (cdr prev)))
5781       (while alist
5782         (if (= (gnus-info-level level) level)
5783             (setcdr prev (cdr alist))
5784           (setq prev alist))
5785         (setq alist (cdr alist)))
5786       (gnus-make-hashtable-from-newsrc-alist)
5787       (gnus-group-list-groups)))
5788    (t
5789     (error "Can't kill; illegal level: %d" level))))
5790       
5791 (defun gnus-group-list-all-groups (&optional arg)
5792   "List all newsgroups with level ARG or lower.
5793 Default is gnus-level-unsubscribed, which lists all subscribed and most
5794 unsubscribed groups."
5795   (interactive "P")
5796   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
5797
5798 ;; Redefine this to list ALL killed groups if prefix arg used.
5799 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
5800 (defun gnus-group-list-killed (&optional arg)
5801   "List all killed newsgroups in the group buffer.
5802 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
5803 entail asking the server for the groups."
5804   (interactive "P")
5805   ;; Find all possible killed newsgroups if arg.
5806   (when arg
5807     ;; First make sure active file has been read.
5808     (unless gnus-have-read-active-file
5809       (let ((gnus-read-active-file t))
5810         (gnus-read-active-file)))
5811     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
5812     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
5813     (mapatoms
5814      (lambda (sym)
5815        (let ((groups 0)
5816              (group (symbol-name sym)))
5817          (if (or (null group)
5818                  (gnus-gethash group gnus-killed-hashtb)
5819                  (gnus-gethash group gnus-newsrc-hashtb))
5820              ()
5821            (let ((do-sub (gnus-matches-options-n group)))
5822              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
5823                  ()
5824                (setq groups (1+ groups))
5825                (setq gnus-killed-list 
5826                      (cons group gnus-killed-list))
5827                (gnus-sethash group group gnus-killed-hashtb))))))
5828      gnus-active-hashtb))
5829   (if (not gnus-killed-list)
5830       (gnus-message 6 "No killed groups")
5831     (let (gnus-group-list-mode)
5832       (funcall gnus-group-prepare-function 
5833                gnus-level-killed t gnus-level-killed))
5834     (goto-char (point-min)))
5835   (gnus-group-position-point))
5836
5837 (defun gnus-group-list-zombies ()
5838   "List all zombie newsgroups in the group buffer."
5839   (interactive)
5840   (if (not gnus-zombie-list)
5841       (gnus-message 6 "No zombie groups")
5842     (let (gnus-group-list-mode)
5843       (funcall gnus-group-prepare-function
5844                gnus-level-zombie t gnus-level-zombie))
5845     (goto-char (point-min)))
5846   (gnus-group-position-point))
5847
5848 (defun gnus-group-list-active ()
5849   "List all groups that are available from the server(s)."
5850   (interactive)
5851   ;; First we make sure that we have really read the active file. 
5852   (unless gnus-have-read-active-file
5853     (let ((gnus-read-active-file t))
5854       (gnus-read-active-file)))
5855   ;; Find all groups and sort them.
5856   (let ((groups 
5857          (sort 
5858           (let (list)
5859             (mapatoms
5860              (lambda (sym)
5861                (and (symbol-value sym)
5862                     (setq list (cons (symbol-name sym) list))))
5863              gnus-active-hashtb)
5864             list)
5865           'string<))
5866         (buffer-read-only nil))
5867     (erase-buffer)
5868     (while groups
5869       (gnus-group-insert-group-line-info (car groups))
5870       (setq groups (cdr groups)))
5871     (goto-char (point-min))))
5872
5873 (defun gnus-activate-all-groups (level)
5874   "Activate absolutely all groups."
5875   (interactive (list 7))
5876   (let ((gnus-activate-level level)
5877         (gnus-activate-foreign-newsgroups level))
5878     (gnus-group-get-new-news)))
5879
5880 (defun gnus-group-get-new-news (&optional arg)
5881   "Get newly arrived articles.
5882 If ARG is a number, it specifies which levels you are interested in
5883 re-scanning.  If ARG is non-nil and not a number, this will force
5884 \"hard\" re-reading of the active files from all servers."
5885   (interactive "P")
5886   (run-hooks 'gnus-get-new-news-hook)
5887   ;; We might read in new NoCeM messages here.
5888   (and gnus-use-nocem (gnus-nocem-scan-groups))
5889   ;; If ARG is not a number, then we read the active file.
5890   (and arg
5891        (not (numberp arg))
5892        (progn
5893          (let ((gnus-read-active-file t))
5894            (gnus-read-active-file))
5895          (setq arg nil)))
5896
5897   (setq arg (gnus-group-default-level arg t))
5898   (if (and gnus-read-active-file (not arg))
5899       (progn
5900         (gnus-read-active-file)
5901         (gnus-get-unread-articles arg))
5902     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
5903       (gnus-get-unread-articles arg)))
5904   (gnus-group-list-groups))
5905
5906 (defun gnus-group-get-new-news-this-group (&optional n)
5907   "Check for newly arrived news in the current group (and the N-1 next groups).
5908 The difference between N and the number of newsgroup checked is returned.
5909 If N is negative, this group and the N-1 previous groups will be checked."
5910   (interactive "P")
5911   (let* ((groups (gnus-group-process-prefix n))
5912          (ret (if (numberp n) (- n (length groups)) 0))
5913          group)
5914     (while groups
5915       (setq group (car groups)
5916             groups (cdr groups))
5917       (gnus-group-remove-mark group)
5918       (or (gnus-get-new-news-in-group group)
5919           (progn 
5920             (ding) 
5921             (message "%s error: %s" group (gnus-status-message group))
5922             (sit-for 2))))
5923     (gnus-group-next-unread-group 1 t)
5924     (gnus-summary-position-point)
5925     ret))
5926
5927 (defun gnus-get-new-news-in-group (group)
5928   (when (and group (gnus-activate-group group 'scan))
5929     (gnus-get-unread-articles-in-group 
5930      (gnus-get-info group) (gnus-active group))
5931     (when (gnus-group-goto-group group)
5932       (gnus-group-update-group-line))
5933     t))
5934
5935 (defun gnus-group-fetch-faq (group &optional faq-dir)
5936   "Fetch the FAQ for the current group."
5937   (interactive 
5938    (list
5939     (gnus-group-real-name (gnus-group-group-name))
5940     (cond (current-prefix-arg
5941            (completing-read 
5942             "Faq dir: " (and (listp gnus-group-faq-directory) 
5943                              gnus-group-faq-directory))))))
5944   (or faq-dir
5945       (setq faq-dir (if (listp gnus-group-faq-directory)
5946                         (car gnus-group-faq-directory)
5947                       gnus-group-faq-directory)))
5948   (or group (error "No group name given"))
5949   (let ((file (concat (file-name-as-directory faq-dir)
5950                       (gnus-group-real-name group))))
5951     (if (not (file-exists-p file))
5952         (error "No such file: %s" file)
5953       (find-file file))))
5954   
5955 (defun gnus-group-describe-group (force &optional group)
5956   "Display a description of the current newsgroup."
5957   (interactive (list current-prefix-arg (gnus-group-group-name)))
5958   (and force (setq gnus-description-hashtb nil))
5959   (let ((method (gnus-find-method-for-group group))
5960         desc)
5961     (or group (error "No group name given"))
5962     (and (or (and gnus-description-hashtb
5963                   ;; We check whether this group's method has been
5964                   ;; queried for a description file.  
5965                   (gnus-gethash 
5966                    (gnus-group-prefixed-name "" method) 
5967                    gnus-description-hashtb))
5968              (setq desc (gnus-group-get-description group))
5969              (gnus-read-descriptions-file method))
5970          (message
5971           (or desc (gnus-gethash group gnus-description-hashtb)
5972               "No description available")))))
5973
5974 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5975 (defun gnus-group-describe-all-groups (&optional force)
5976   "Pop up a buffer with descriptions of all newsgroups."
5977   (interactive "P")
5978   (and force (setq gnus-description-hashtb nil))
5979   (if (not (or gnus-description-hashtb
5980                (gnus-read-all-descriptions-files)))
5981       (error "Couldn't request descriptions file"))
5982   (let ((buffer-read-only nil)
5983         b)
5984     (erase-buffer)
5985     (mapatoms
5986      (lambda (group)
5987        (setq b (point))
5988        (insert (format "      *: %-20s %s\n" (symbol-name group)
5989                        (symbol-value group)))
5990        (add-text-properties 
5991         b (1+ b) (list 'gnus-group group
5992                        'gnus-unread t 'gnus-marked nil
5993                        'gnus-level (1+ gnus-level-subscribed))))
5994      gnus-description-hashtb)
5995     (goto-char (point-min))
5996     (gnus-group-position-point)))
5997
5998 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
5999 (defun gnus-group-apropos (regexp &optional search-description)
6000   "List all newsgroups that have names that match a regexp."
6001   (interactive "sGnus apropos (regexp): ")
6002   (let ((prev "")
6003         (obuf (current-buffer))
6004         groups des)
6005     ;; Go through all newsgroups that are known to Gnus.
6006     (mapatoms 
6007      (lambda (group)
6008        (and (symbol-name group)
6009             (string-match regexp (symbol-name group))
6010             (setq groups (cons (symbol-name group) groups))))
6011      gnus-active-hashtb)
6012     ;; Go through all descriptions that are known to Gnus. 
6013     (if search-description
6014         (mapatoms 
6015          (lambda (group)
6016            (and (string-match regexp (symbol-value group))
6017                 (gnus-active (symbol-name group))
6018                 (setq groups (cons (symbol-name group) groups))))
6019          gnus-description-hashtb))
6020     (if (not groups)
6021         (gnus-message 3 "No groups matched \"%s\"." regexp)
6022       ;; Print out all the groups.
6023       (save-excursion
6024         (pop-to-buffer "*Gnus Help*")
6025         (buffer-disable-undo (current-buffer))
6026         (erase-buffer)
6027         (setq groups (sort groups 'string<))
6028         (while groups
6029           ;; Groups may be entered twice into the list of groups.
6030           (if (not (string= (car groups) prev))
6031               (progn
6032                 (insert (setq prev (car groups)) "\n")
6033                 (if (and gnus-description-hashtb
6034                          (setq des (gnus-gethash (car groups) 
6035                                                  gnus-description-hashtb)))
6036                     (insert "  " des "\n"))))
6037           (setq groups (cdr groups)))
6038         (goto-char (point-min))))
6039     (pop-to-buffer obuf)))
6040
6041 (defun gnus-group-description-apropos (regexp)
6042   "List all newsgroups that have names or descriptions that match a regexp."
6043   (interactive "sGnus description apropos (regexp): ")
6044   (if (not (or gnus-description-hashtb
6045                (gnus-read-all-descriptions-files)))
6046       (error "Couldn't request descriptions file"))
6047   (gnus-group-apropos regexp t))
6048
6049 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6050 (defun gnus-group-list-matching (level regexp &optional all lowest) 
6051   "List all groups with unread articles that match REGEXP.
6052 If the prefix LEVEL is non-nil, it should be a number that says which
6053 level to cut off listing groups. 
6054 If ALL, also list groups with no unread articles.
6055 If LOWEST, don't list groups with level lower than LOWEST."
6056   (interactive "P\nsList newsgroups matching: ")
6057   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6058                            all (or lowest 1) regexp)
6059   (goto-char (point-min))
6060   (gnus-group-position-point))
6061
6062 (defun gnus-group-list-all-matching (level regexp &optional lowest) 
6063   "List all groups that match REGEXP.
6064 If the prefix LEVEL is non-nil, it should be a number that says which
6065 level to cut off listing groups. 
6066 If LOWEST, don't list groups with level lower than LOWEST."
6067   (interactive "P\nsList newsgroups matching: ")
6068   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6069
6070 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6071 (defun gnus-group-save-newsrc (&optional force)
6072   "Save the Gnus startup files.
6073 If FORCE, force saving whether it is necessary or not."
6074   (interactive "P")
6075   (gnus-save-newsrc-file force))
6076
6077 (defun gnus-group-restart (&optional arg)
6078   "Force Gnus to read the .newsrc file."
6079   (interactive "P")
6080   (gnus-save-newsrc-file)
6081   (gnus-setup-news 'force)
6082   (gnus-group-list-groups arg))
6083
6084 (defun gnus-group-read-init-file ()
6085   "Read the Gnus elisp init file."
6086   (interactive)
6087   (gnus-read-init-file))
6088
6089 (defun gnus-group-check-bogus-groups (&optional silent)
6090   "Check bogus newsgroups.
6091 If given a prefix, don't ask for confirmation before removing a bogus
6092 group."
6093   (interactive "P")
6094   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6095   (gnus-group-list-groups))
6096
6097 (defun gnus-group-edit-global-kill (&optional article group)
6098   "Edit the global kill file.
6099 If GROUP, edit that local kill file instead."
6100   (interactive "P")
6101   (setq gnus-current-kill-article article)
6102   (gnus-kill-file-edit-file group)
6103   (gnus-message 
6104    6
6105    (substitute-command-keys
6106     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6107             (if group "local" "global")))))
6108
6109 (defun gnus-group-edit-local-kill (article group)
6110   "Edit a local kill file."
6111   (interactive (list nil (gnus-group-group-name)))
6112   (gnus-group-edit-global-kill article group))
6113
6114 (defun gnus-group-force-update ()
6115   "Update `.newsrc' file."
6116   (interactive)
6117   (gnus-save-newsrc-file))
6118
6119 (defun gnus-group-suspend ()
6120   "Suspend the current Gnus session.
6121 In fact, cleanup buffers except for group mode buffer.
6122 The hook gnus-suspend-gnus-hook is called before actually suspending."
6123   (interactive)
6124   (run-hooks 'gnus-suspend-gnus-hook)
6125   ;; Kill Gnus buffers except for group mode buffer.
6126   (let ((group-buf (get-buffer gnus-group-buffer)))
6127     ;; Do this on a separate list in case the user does a ^G before we finish
6128     (let ((gnus-buffer-list
6129            (delq group-buf (delq gnus-dribble-buffer
6130                                  (append gnus-buffer-list nil)))))
6131       (while gnus-buffer-list
6132         (gnus-kill-buffer (car gnus-buffer-list))
6133         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6134     (if group-buf
6135         (progn
6136           (setq gnus-buffer-list (list group-buf))
6137           (bury-buffer group-buf)
6138           (delete-windows-on group-buf t)))))
6139
6140 (defun gnus-group-clear-dribble ()
6141   "Clear all information from the dribble buffer."
6142   (interactive)
6143   (gnus-dribble-clear)
6144   (gnus-message 7 "Cleared dribble buffer"))
6145
6146 (defun gnus-group-exit ()
6147   "Quit reading news after updating .newsrc.eld and .newsrc.
6148 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6149   (interactive)
6150   (if (or noninteractive                ;For gnus-batch-kill
6151           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6152           (not gnus-interactive-exit)   ;Without confirmation
6153           gnus-expert-user
6154           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6155       (progn
6156         (run-hooks 'gnus-exit-gnus-hook)
6157         ;; Offer to save data from non-quitted summary buffers.
6158         (gnus-offer-save-summaries)
6159         ;; Save the newsrc file(s).
6160         (gnus-save-newsrc-file)
6161         ;; Kill-em-all.
6162         (gnus-close-backends)
6163         ;; Shut down the cache.
6164         (when gnus-use-cache
6165           (gnus-cache-open))
6166         ;; Reset everything.
6167         (gnus-clear-system))))
6168
6169 (defun gnus-close-backends ()
6170   ;; Send a close request to all backends that support such a request. 
6171   (let ((methods gnus-valid-select-methods)
6172         func)
6173     (while methods
6174       (if (fboundp (setq func (intern (concat (car (car methods))
6175                                               "-request-close"))))
6176           (funcall func))
6177       (setq methods (cdr methods)))))
6178
6179 (defun gnus-group-quit ()
6180   "Quit reading news without updating .newsrc.eld or .newsrc.
6181 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6182   (interactive)
6183   (when (or noninteractive              ;For gnus-batch-kill
6184             (zerop (buffer-size))
6185             (not (gnus-server-opened gnus-select-method))
6186             gnus-expert-user
6187             (not gnus-current-startup-file)
6188             (gnus-yes-or-no-p
6189              (format "Quit reading news without saving %s? "
6190                      (file-name-nondirectory gnus-current-startup-file))))
6191     (run-hooks 'gnus-exit-gnus-hook)
6192     (if gnus-use-full-window
6193         (delete-other-windows)
6194       (gnus-remove-some-windows))
6195     (gnus-dribble-save)
6196     (gnus-close-backends)
6197     ;; Shut down the cache.
6198     (when gnus-use-cache
6199       (gnus-cache-open))
6200     (gnus-clear-system)))
6201
6202 (defun gnus-offer-save-summaries ()
6203   "Offer to save all active summary buffers."
6204   (save-excursion
6205     (let ((buflist (buffer-list)) 
6206           buffers bufname)
6207       ;; Go through all buffers and find all summaries.
6208       (while buflist
6209         (and (setq bufname (buffer-name (car buflist)))
6210              (string-match "Summary" bufname)
6211              (save-excursion
6212                (set-buffer bufname)
6213                ;; We check that this is, indeed, a summary buffer.
6214                (and (eq major-mode 'gnus-summary-mode)
6215                     ;; Also make sure this isn't bogus.
6216                     gnus-newsgroup-prepared))
6217              (push bufname buffers))
6218         (setq buflist (cdr buflist)))
6219       ;; Go through all these summary buffers and offer to save them.
6220       (when buffers
6221         (map-y-or-n-p 
6222          "Update summary buffer %s? "
6223          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6224          buffers)))))
6225
6226 (defun gnus-group-describe-briefly ()
6227   "Give a one line description of the group mode commands."
6228   (interactive)
6229   (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")))
6230
6231 (defun gnus-group-browse-foreign-server (method)
6232   "Browse a foreign news server.
6233 If called interactively, this function will ask for a select method
6234  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where). 
6235 If not, METHOD should be a list where the first element is the method
6236 and the second element is the address."
6237   (interactive
6238    (list (let ((how (completing-read 
6239                      "Which backend: "
6240                      (append gnus-valid-select-methods gnus-server-alist)
6241                      nil t "nntp")))
6242            ;; We either got a backend name or a virtual server name.
6243            ;; If the first, we also need an address.
6244            (if (assoc how gnus-valid-select-methods)
6245                (list (intern how)
6246                      ;; Suggested by mapjph@bath.ac.uk.
6247                      (completing-read 
6248                       "Address: " 
6249                       (mapcar (lambda (server) (list server))
6250                               gnus-secondary-servers)))
6251              ;; We got a server name, so we find the method.
6252              (gnus-server-to-method how)))))
6253   (gnus-browse-foreign-server method))
6254
6255 \f
6256 ;;;
6257 ;;; Browse Server Mode
6258 ;;;
6259
6260 (defvar gnus-browse-mode-hook nil)
6261 (defvar gnus-browse-mode-map nil)
6262 (put 'gnus-browse-mode 'mode-class 'special)
6263
6264 (if gnus-browse-mode-map
6265     nil
6266   (setq gnus-browse-mode-map (make-keymap))
6267   (suppress-keymap gnus-browse-mode-map)
6268   (define-key gnus-browse-mode-map " " 'gnus-browse-read-group)
6269   (define-key gnus-browse-mode-map "=" 'gnus-browse-select-group)
6270   (define-key gnus-browse-mode-map "n" 'gnus-browse-next-group)
6271   (define-key gnus-browse-mode-map "p" 'gnus-browse-prev-group)
6272   (define-key gnus-browse-mode-map "\177" 'gnus-browse-prev-group)
6273   (define-key gnus-browse-mode-map "N" 'gnus-browse-next-group)
6274   (define-key gnus-browse-mode-map "P" 'gnus-browse-prev-group)
6275   (define-key gnus-browse-mode-map "\M-n" 'gnus-browse-next-group)
6276   (define-key gnus-browse-mode-map "\M-p" 'gnus-browse-prev-group)
6277   (define-key gnus-browse-mode-map "\r" 'gnus-browse-select-group)
6278   (define-key gnus-browse-mode-map "u" 'gnus-browse-unsubscribe-current-group)
6279   (define-key gnus-browse-mode-map "l" 'gnus-browse-exit)
6280   (define-key gnus-browse-mode-map "L" 'gnus-browse-exit)
6281   (define-key gnus-browse-mode-map "q" 'gnus-browse-exit)
6282   (define-key gnus-browse-mode-map "Q" 'gnus-browse-exit)
6283   (define-key gnus-browse-mode-map "\C-c\C-c" 'gnus-browse-exit)
6284   (define-key gnus-browse-mode-map "?" 'gnus-browse-describe-briefly)
6285   (define-key gnus-browse-mode-map "\C-c\C-i" 'gnus-info-find-node)
6286   )
6287
6288 (defvar gnus-browse-current-method nil)
6289 (defvar gnus-browse-return-buffer nil)
6290
6291 (defvar gnus-browse-buffer "*Gnus Browse Server*")
6292
6293 (defun gnus-browse-foreign-server (method &optional return-buffer)
6294   "Browse the server METHOD."
6295   (setq gnus-browse-current-method method)
6296   (setq gnus-browse-return-buffer return-buffer)
6297   (let ((gnus-select-method method)
6298         groups group)
6299     (gnus-message 5 "Connecting to %s..." (nth 1 method))
6300     (cond 
6301      ((not (gnus-check-server method))
6302       (gnus-message 
6303        1 "Unable to contact server: %s" (gnus-status-message method))
6304       nil)
6305      ((not (gnus-request-list method))
6306       (gnus-message 
6307        1 "Couldn't request list: %s" (gnus-status-message method))
6308       nil)
6309      (t
6310       (get-buffer-create gnus-browse-buffer)
6311       (gnus-add-current-to-buffer-list)
6312       (and gnus-carpal (gnus-carpal-setup-buffer 'browse))
6313       (gnus-configure-windows 'browse)
6314       (buffer-disable-undo (current-buffer))
6315       (let ((buffer-read-only nil))
6316         (erase-buffer))
6317       (gnus-browse-mode)
6318       (setq mode-line-buffer-identification
6319             (list
6320              (format
6321               "Gnus: %%b {%s:%s}" (car method) (car (cdr method)))))
6322       (save-excursion
6323         (set-buffer nntp-server-buffer)
6324         (let ((cur (current-buffer)))
6325           (goto-char (point-min))
6326           (or (string= gnus-ignored-newsgroups "")
6327               (delete-matching-lines gnus-ignored-newsgroups))
6328           (while (re-search-forward 
6329                   "\\(^[^ \t]+\\)[ \t]+[0-9]+[ \t]+[0-9]+" nil t)
6330             (goto-char (match-end 1))
6331             (setq groups (cons (cons (match-string 1)
6332                                      (max 0 (- (1+ (read cur)) (read cur))))
6333                                groups)))))
6334       (setq groups (sort groups 
6335                          (lambda (l1 l2)
6336                            (string< (car l1) (car l2)))))
6337       (let ((buffer-read-only nil))
6338         (while groups
6339           (setq group (car groups))
6340           (insert 
6341            (format "K%7d: %s\n" (cdr group) (car group)))
6342           (setq groups (cdr groups))))
6343       (switch-to-buffer (current-buffer))
6344       (goto-char (point-min))
6345       (gnus-group-position-point)
6346       t))))
6347
6348 (defun gnus-browse-mode ()
6349   "Major mode for browsing a foreign server.
6350
6351 All normal editing commands are switched off.
6352
6353 \\<gnus-browse-mode-map>
6354 The only things you can do in this buffer is
6355
6356 1) `\\[gnus-browse-unsubscribe-current-group]' to subscribe to a group.
6357 The group will be inserted into the group buffer upon exit from this
6358 buffer.  
6359
6360 2) `\\[gnus-browse-read-group]' to read a group ephemerally.
6361
6362 3) `\\[gnus-browse-exit]' to return to the group buffer."
6363   (interactive)
6364   (kill-all-local-variables)
6365   (when (and menu-bar-mode
6366              (gnus-visual-p 'browse-menu 'menu))
6367     (gnus-browse-make-menu-bar))
6368   (gnus-simplify-mode-line)
6369   (setq major-mode 'gnus-browse-mode)
6370   (setq mode-name "Browse Server")
6371   (setq mode-line-process nil)
6372   (use-local-map gnus-browse-mode-map)
6373   (buffer-disable-undo (current-buffer))
6374   (setq truncate-lines t)
6375   (setq buffer-read-only t)
6376   (run-hooks 'gnus-browse-mode-hook))
6377
6378 (defun gnus-browse-read-group (&optional no-article)
6379   "Enter the group at the current line."
6380   (interactive)
6381   (let ((group (gnus-browse-group-name)))
6382     (or (gnus-group-read-ephemeral-group 
6383          group gnus-browse-current-method nil
6384          (cons (current-buffer) 'browse))
6385         (error "Couldn't enter %s" group))))
6386
6387 (defun gnus-browse-select-group ()
6388   "Select the current group."
6389   (interactive)
6390   (gnus-browse-read-group 'no))
6391
6392 (defun gnus-browse-next-group (n)
6393   "Go to the next group."
6394   (interactive "p")
6395   (prog1
6396       (forward-line n)
6397     (gnus-group-position-point)))
6398
6399 (defun gnus-browse-prev-group (n)
6400   "Go to the next group."
6401   (interactive "p")
6402   (gnus-browse-next-group (- n)))
6403
6404 (defun gnus-browse-unsubscribe-current-group (arg)
6405   "(Un)subscribe to the next ARG groups."
6406   (interactive "p")
6407   (and (eobp)
6408        (error "No group at current line."))
6409   (let ((ward (if (< arg 0) -1 1))
6410         (arg (abs arg)))
6411     (while (and (> arg 0)
6412                 (not (eobp))
6413                 (gnus-browse-unsubscribe-group)
6414                 (zerop (gnus-browse-next-group ward)))
6415       (setq arg (1- arg)))
6416     (gnus-group-position-point)
6417     (if (/= 0 arg) (gnus-message 7 "No more newsgroups"))
6418     arg))
6419
6420 (defun gnus-browse-group-name ()
6421   (save-excursion
6422     (beginning-of-line)
6423     (when (re-search-forward ": \\(.*\\)$" (gnus-point-at-eol) t)
6424       (gnus-group-prefixed-name (match-string 1) gnus-browse-current-method))))
6425   
6426 (defun gnus-browse-unsubscribe-group ()
6427   "Toggle subscription of the current group in the browse buffer."
6428   (let ((sub nil)
6429         (buffer-read-only nil)
6430         group)
6431     (save-excursion
6432       (beginning-of-line)
6433       ;; If this group it killed, then we want to subscribe it.
6434       (if (= (following-char) ?K) (setq sub t))
6435       (setq group (gnus-browse-group-name))
6436       (delete-char 1)
6437       (if sub
6438           (progn
6439             (gnus-group-change-level 
6440              (list t group gnus-level-default-subscribed
6441                    nil nil gnus-browse-current-method) 
6442              gnus-level-default-subscribed gnus-level-killed
6443              (and (car (nth 1 gnus-newsrc-alist))
6444                   (gnus-gethash (car (nth 1 gnus-newsrc-alist))
6445                                 gnus-newsrc-hashtb))
6446              t)
6447             (insert ? ))
6448         (gnus-group-change-level 
6449          group gnus-level-killed gnus-level-default-subscribed)
6450         (insert ?K)))
6451     t))
6452
6453 (defun gnus-browse-exit ()
6454   "Quit browsing and return to the group buffer."
6455   (interactive)
6456   (if (eq major-mode 'gnus-browse-mode)
6457       (kill-buffer (current-buffer)))
6458   (if gnus-browse-return-buffer
6459       (gnus-configure-windows 'server 'force)
6460     (gnus-configure-windows 'group 'force)
6461     (gnus-group-list-groups nil)))
6462
6463 (defun gnus-browse-describe-briefly ()
6464   "Give a one line description of the group mode commands."
6465   (interactive)
6466   (gnus-message 6
6467                 (substitute-command-keys "\\<gnus-browse-mode-map>\\[gnus-group-next-group]:Forward  \\[gnus-group-prev-group]:Backward  \\[gnus-browse-exit]:Exit  \\[gnus-info-find-node]:Run Info  \\[gnus-browse-describe-briefly]:This help")))
6468       
6469 \f
6470 ;;;
6471 ;;; Gnus summary mode
6472 ;;;
6473
6474 (defvar gnus-summary-mode-map nil)
6475 (defvar gnus-summary-mark-map nil)
6476 (defvar gnus-summary-mscore-map nil)
6477 (defvar gnus-summary-article-map nil)
6478 (defvar gnus-summary-thread-map nil)
6479 (defvar gnus-summary-goto-map nil)
6480 (defvar gnus-summary-exit-map nil)
6481 (defvar gnus-summary-interest-map nil)
6482 (defvar gnus-summary-sort-map nil)
6483 (defvar gnus-summary-backend-map nil)
6484 (defvar gnus-summary-save-map nil)
6485 (defvar gnus-summary-wash-map nil)
6486 (defvar gnus-summary-wash-hide-map nil)
6487 (defvar gnus-summary-wash-highlight-map nil)
6488 (defvar gnus-summary-wash-time-map nil)
6489 (defvar gnus-summary-help-map nil)
6490 (defvar gnus-summary-limit-map nil)
6491
6492 (put 'gnus-summary-mode 'mode-class 'special)
6493
6494 (if gnus-summary-mode-map
6495     nil
6496   (setq gnus-summary-mode-map (make-keymap))
6497   (suppress-keymap gnus-summary-mode-map)
6498
6499   ;; Non-orthogonal keys
6500
6501   (define-key gnus-summary-mode-map " " 'gnus-summary-next-page)
6502   (define-key gnus-summary-mode-map "\177" 'gnus-summary-prev-page)
6503   (define-key gnus-summary-mode-map "\r" 'gnus-summary-scroll-up)
6504   (define-key gnus-summary-mode-map "n" 'gnus-summary-next-unread-article)
6505   (define-key gnus-summary-mode-map "p" 'gnus-summary-prev-unread-article)
6506   (define-key gnus-summary-mode-map "N" 'gnus-summary-next-article)
6507   (define-key gnus-summary-mode-map "P" 'gnus-summary-prev-article)
6508   (define-key gnus-summary-mode-map "\M-\C-n" 'gnus-summary-next-same-subject)
6509   (define-key gnus-summary-mode-map "\M-\C-p" 'gnus-summary-prev-same-subject)
6510   (define-key gnus-summary-mode-map "\M-n" 'gnus-summary-next-unread-subject)
6511   (define-key gnus-summary-mode-map "\M-p" 'gnus-summary-prev-unread-subject)
6512   (define-key gnus-summary-mode-map "." 'gnus-summary-first-unread-article)
6513   (define-key gnus-summary-mode-map "," 'gnus-summary-best-unread-article)
6514   (define-key gnus-summary-mode-map 
6515     "\M-s" 'gnus-summary-search-article-forward)
6516   (define-key gnus-summary-mode-map 
6517     "\M-r" 'gnus-summary-search-article-backward)
6518   (define-key gnus-summary-mode-map "<" 'gnus-summary-beginning-of-article)
6519   (define-key gnus-summary-mode-map ">" 'gnus-summary-end-of-article)
6520   (define-key gnus-summary-mode-map "j" 'gnus-summary-goto-article)
6521   (define-key gnus-summary-mode-map "^" 'gnus-summary-refer-parent-article)
6522   (define-key gnus-summary-mode-map "\M-^" 'gnus-summary-refer-article)
6523   (define-key gnus-summary-mode-map "u" 'gnus-summary-tick-article-forward)
6524   (define-key gnus-summary-mode-map "!" 'gnus-summary-tick-article-forward)
6525   (define-key gnus-summary-mode-map "U" 'gnus-summary-tick-article-backward)
6526   (define-key gnus-summary-mode-map "d" 'gnus-summary-mark-as-read-forward)
6527   (define-key gnus-summary-mode-map "D" 'gnus-summary-mark-as-read-backward)
6528   (define-key gnus-summary-mode-map "E" 'gnus-summary-mark-as-expirable)
6529   (define-key gnus-summary-mode-map "\M-u" 'gnus-summary-clear-mark-forward)
6530   (define-key gnus-summary-mode-map "\M-U" 'gnus-summary-clear-mark-backward)
6531   (define-key gnus-summary-mode-map 
6532     "k" 'gnus-summary-kill-same-subject-and-select)
6533   (define-key gnus-summary-mode-map "\C-k" 'gnus-summary-kill-same-subject)
6534   (define-key gnus-summary-mode-map "\M-\C-k" 'gnus-summary-kill-thread)
6535   (define-key gnus-summary-mode-map "\M-\C-l" 'gnus-summary-lower-thread)
6536   (define-key gnus-summary-mode-map "e" 'gnus-summary-edit-article)
6537   (define-key gnus-summary-mode-map "#" 'gnus-summary-mark-as-processable)
6538   (define-key gnus-summary-mode-map "\M-#" 'gnus-summary-unmark-as-processable)
6539   (define-key gnus-summary-mode-map "\M-\C-t" 'gnus-summary-toggle-threads)
6540   (define-key gnus-summary-mode-map "\M-\C-s" 'gnus-summary-show-thread)
6541   (define-key gnus-summary-mode-map "\M-\C-h" 'gnus-summary-hide-thread)
6542   (define-key gnus-summary-mode-map "\M-\C-f" 'gnus-summary-next-thread)
6543   (define-key gnus-summary-mode-map "\M-\C-b" 'gnus-summary-prev-thread)
6544   (define-key gnus-summary-mode-map "\M-\C-u" 'gnus-summary-up-thread)
6545   (define-key gnus-summary-mode-map "\M-\C-d" 'gnus-summary-down-thread)
6546   (define-key gnus-summary-mode-map "&" 'gnus-summary-execute-command)
6547   (define-key gnus-summary-mode-map "c" 'gnus-summary-catchup-and-exit)
6548   (define-key gnus-summary-mode-map "\C-w" 'gnus-summary-mark-region-as-read)
6549   (define-key gnus-summary-mode-map "\C-t" 'gnus-summary-toggle-truncation)
6550   (define-key gnus-summary-mode-map "?" 'gnus-summary-mark-as-dormant)
6551   (define-key gnus-summary-mode-map 
6552     "\C-c\M-\C-s" 'gnus-summary-limit-include-expunged)
6553   (define-key gnus-summary-mode-map 
6554     "\C-c\C-s\C-n" 'gnus-summary-sort-by-number)
6555   (define-key gnus-summary-mode-map 
6556     "\C-c\C-s\C-a" 'gnus-summary-sort-by-author)
6557   (define-key gnus-summary-mode-map 
6558     "\C-c\C-s\C-s" 'gnus-summary-sort-by-subject)
6559   (define-key gnus-summary-mode-map "\C-c\C-s\C-d" 'gnus-summary-sort-by-date)
6560   (define-key gnus-summary-mode-map "\C-c\C-s\C-i" 'gnus-summary-sort-by-score)
6561   (define-key gnus-summary-mode-map "=" 'gnus-summary-expand-window)
6562   (define-key gnus-summary-mode-map 
6563     "\C-x\C-s" 'gnus-summary-reselect-current-group)
6564   (define-key gnus-summary-mode-map "\M-g" 'gnus-summary-rescan-group)
6565   (define-key gnus-summary-mode-map "w" 'gnus-summary-stop-page-breaking)
6566   (define-key gnus-summary-mode-map "\C-c\C-r" 'gnus-summary-caesar-message)
6567   (define-key gnus-summary-mode-map "\M-t" 'gnus-summary-toggle-mime)
6568   (define-key gnus-summary-mode-map "f" 'gnus-summary-followup)
6569   (define-key gnus-summary-mode-map "F" 'gnus-summary-followup-with-original)
6570   (define-key gnus-summary-mode-map "C" 'gnus-summary-cancel-article)
6571   (define-key gnus-summary-mode-map "r" 'gnus-summary-reply)
6572   (define-key gnus-summary-mode-map "R" 'gnus-summary-reply-with-original)
6573   (define-key gnus-summary-mode-map "\C-c\C-f" 'gnus-summary-mail-forward)
6574   (define-key gnus-summary-mode-map "o" 'gnus-summary-save-article)
6575   (define-key gnus-summary-mode-map "\C-o" 'gnus-summary-save-article-mail)
6576   (define-key gnus-summary-mode-map "|" 'gnus-summary-pipe-output)
6577   (define-key gnus-summary-mode-map "\M-k" 'gnus-summary-edit-local-kill)
6578   (define-key gnus-summary-mode-map "\M-K" 'gnus-summary-edit-global-kill)
6579   (define-key gnus-summary-mode-map "V" 'gnus-version)
6580   (define-key gnus-summary-mode-map "\C-c\C-d" 'gnus-summary-describe-group)
6581   (define-key gnus-summary-mode-map "q" 'gnus-summary-exit)
6582   (define-key gnus-summary-mode-map "Q" 'gnus-summary-exit-no-update)
6583   (define-key gnus-summary-mode-map "\C-c\C-i" 'gnus-info-find-node)
6584   (define-key gnus-summary-mode-map gnus-mouse-2 'gnus-mouse-pick-article)
6585   (define-key gnus-summary-mode-map "m" 'gnus-summary-mail-other-window)
6586   (define-key gnus-summary-mode-map "a" 'gnus-summary-post-news)
6587   (define-key gnus-summary-mode-map "x" 'gnus-summary-limit-to-unread)
6588   (define-key gnus-summary-mode-map "s" 'gnus-summary-isearch-article)
6589   (define-key gnus-summary-mode-map "t" 'gnus-summary-toggle-header)
6590   (define-key gnus-summary-mode-map "g" 'gnus-summary-show-article)
6591 ;  (define-key gnus-summary-mode-map "?" 'gnus-summary-describe-briefly)
6592   (define-key gnus-summary-mode-map "l" 'gnus-summary-goto-last-article)
6593   (define-key gnus-summary-mode-map "\C-c\C-v\C-v" 'gnus-uu-decode-uu-view)
6594   (define-key gnus-summary-mode-map "\C-d" 'gnus-summary-enter-digest-group)
6595   (define-key gnus-summary-mode-map "v" 'gnus-summary-verbose-headers)
6596   (define-key gnus-summary-mode-map "\C-c\C-b" 'gnus-bug)
6597
6598   (define-key gnus-summary-mode-map "*" 'gnus-cache-enter-article)
6599   (define-key gnus-summary-mode-map "\M-*" 'gnus-cache-remove-article)
6600
6601   ;; Sort of orthogonal keymap
6602   (define-prefix-command 'gnus-summary-mark-map)
6603   (define-key gnus-summary-mode-map "M" 'gnus-summary-mark-map)
6604   (define-key gnus-summary-mark-map "t" 'gnus-summary-tick-article-forward)
6605   (define-key gnus-summary-mark-map "!" 'gnus-summary-tick-article-forward)
6606   (define-key gnus-summary-mark-map "d" 'gnus-summary-mark-as-read-forward)
6607   (define-key gnus-summary-mark-map "r" 'gnus-summary-mark-as-read-forward)
6608   (define-key gnus-summary-mark-map "c" 'gnus-summary-clear-mark-forward)
6609   (define-key gnus-summary-mark-map " " 'gnus-summary-clear-mark-forward)
6610   (define-key gnus-summary-mark-map "e" 'gnus-summary-mark-as-expirable)
6611   (define-key gnus-summary-mark-map "x" 'gnus-summary-mark-as-expirable)
6612   (define-key gnus-summary-mark-map "?" 'gnus-summary-mark-as-dormant)
6613   (define-key gnus-summary-mark-map "b" 'gnus-summary-set-bookmark)
6614   (define-key gnus-summary-mark-map "B" 'gnus-summary-remove-bookmark)
6615   (define-key gnus-summary-mark-map "#" 'gnus-summary-mark-as-processable)
6616   (define-key gnus-summary-mark-map "\M-#" 'gnus-summary-unmark-as-processable)
6617   (define-key gnus-summary-mark-map "S" 'gnus-summary-limit-include-expunged)
6618   (define-key gnus-summary-mark-map "C" 'gnus-summary-catchup)
6619   (define-key gnus-summary-mark-map "H" 'gnus-summary-catchup-to-here)
6620   (define-key gnus-summary-mark-map "\C-c" 'gnus-summary-catchup-all)
6621   (define-key gnus-summary-mark-map 
6622     "k" 'gnus-summary-kill-same-subject-and-select)
6623   (define-key gnus-summary-mark-map "K" 'gnus-summary-kill-same-subject)
6624
6625   (define-prefix-command 'gnus-summary-mscore-map)
6626   (define-key gnus-summary-mark-map "V" 'gnus-summary-mscore-map)
6627   (define-key gnus-summary-mscore-map "c" 'gnus-summary-clear-above)
6628   (define-key gnus-summary-mscore-map "u" 'gnus-summary-tick-above)
6629   (define-key gnus-summary-mscore-map "m" 'gnus-summary-mark-above)
6630   (define-key gnus-summary-mscore-map "k" 'gnus-summary-kill-below)
6631
6632   (define-key gnus-summary-mark-map "P" 'gnus-uu-mark-map)
6633   
6634   (define-key gnus-summary-mode-map "S" 'gnus-summary-send-map)
6635
6636   (define-prefix-command 'gnus-summary-limit-map)
6637   (define-key gnus-summary-mode-map "/" 'gnus-summary-limit-map)
6638   (define-key gnus-summary-limit-map "/" 'gnus-summary-limit-to-subject)
6639   (define-key gnus-summary-limit-map "n" 'gnus-summary-limit-to-articles)
6640   (define-key gnus-summary-limit-map "w" 'gnus-summary-pop-limit)
6641   (define-key gnus-summary-limit-map "s" 'gnus-summary-limit-to-subject)
6642   (define-key gnus-summary-limit-map "u" 'gnus-summary-limit-to-unread)
6643   (define-key gnus-summary-limit-map "m" 'gnus-summary-limit-to-marks)
6644   (define-key gnus-summary-limit-map "v" 'gnus-summary-limit-to-score)
6645   (define-key gnus-summary-limit-map "D" 'gnus-summary-limit-include-dormant)
6646   (define-key gnus-summary-limit-map "d" 'gnus-summary-limit-exclude-dormant)
6647 ;  (define-key gnus-summary-limit-map "t" 'gnus-summary-limit-exclude-thread)
6648   (define-key gnus-summary-mark-map "E" 'gnus-summary-limit-include-expunged)
6649   (define-key gnus-summary-limit-map "c" 
6650     'gnus-summary-limit-exclude-childless-dormant)
6651
6652   (define-prefix-command 'gnus-summary-goto-map)
6653   (define-key gnus-summary-mode-map "G" 'gnus-summary-goto-map)
6654   (define-key gnus-summary-goto-map "n" 'gnus-summary-next-unread-article)
6655   (define-key gnus-summary-goto-map "p" 'gnus-summary-prev-unread-article)
6656   (define-key gnus-summary-goto-map "N" 'gnus-summary-next-article)
6657   (define-key gnus-summary-goto-map "P" 'gnus-summary-prev-article)
6658   (define-key gnus-summary-goto-map "\C-n" 'gnus-summary-next-same-subject)
6659   (define-key gnus-summary-goto-map "\C-p" 'gnus-summary-prev-same-subject)
6660   (define-key gnus-summary-goto-map "\M-n" 'gnus-summary-next-unread-subject)
6661   (define-key gnus-summary-goto-map "\M-p" 'gnus-summary-prev-unread-subject)
6662   (define-key gnus-summary-goto-map "f" 'gnus-summary-first-unread-article)
6663   (define-key gnus-summary-goto-map "b" 'gnus-summary-best-unread-article)
6664   (define-key gnus-summary-goto-map "g" 'gnus-summary-goto-subject)
6665   (define-key gnus-summary-goto-map "l" 'gnus-summary-goto-last-article)
6666   (define-key gnus-summary-goto-map "p" 'gnus-summary-pop-article)
6667
6668
6669   (define-prefix-command 'gnus-summary-thread-map)
6670   (define-key gnus-summary-mode-map "T" 'gnus-summary-thread-map)
6671   (define-key gnus-summary-thread-map "k" 'gnus-summary-kill-thread)
6672   (define-key gnus-summary-thread-map "l" 'gnus-summary-lower-thread)
6673   (define-key gnus-summary-thread-map "i" 'gnus-summary-raise-thread)
6674   (define-key gnus-summary-thread-map "T" 'gnus-summary-toggle-threads)
6675   (define-key gnus-summary-thread-map "s" 'gnus-summary-show-thread)
6676   (define-key gnus-summary-thread-map "S" 'gnus-summary-show-all-threads)
6677   (define-key gnus-summary-thread-map "h" 'gnus-summary-hide-thread)
6678   (define-key gnus-summary-thread-map "H" 'gnus-summary-hide-all-threads)
6679   (define-key gnus-summary-thread-map "n" 'gnus-summary-next-thread)
6680   (define-key gnus-summary-thread-map "p" 'gnus-summary-prev-thread)
6681   (define-key gnus-summary-thread-map "u" 'gnus-summary-up-thread)
6682   (define-key gnus-summary-thread-map "d" 'gnus-summary-down-thread)
6683   (define-key gnus-summary-thread-map "#" 'gnus-uu-mark-thread)
6684   (define-key gnus-summary-thread-map "\M-#" 'gnus-uu-unmark-thread)
6685
6686   
6687   (define-prefix-command 'gnus-summary-exit-map)
6688   (define-key gnus-summary-mode-map "Z" 'gnus-summary-exit-map)
6689   (define-key gnus-summary-exit-map "c" 'gnus-summary-catchup-and-exit)
6690   (define-key gnus-summary-exit-map "C" 'gnus-summary-catchup-all-and-exit)
6691   (define-key gnus-summary-exit-map "E" 'gnus-summary-exit-no-update)
6692   (define-key gnus-summary-exit-map "Q" 'gnus-summary-exit)
6693   (define-key gnus-summary-exit-map "Z" 'gnus-summary-exit)
6694   (define-key gnus-summary-exit-map 
6695     "n" 'gnus-summary-catchup-and-goto-next-group)
6696   (define-key gnus-summary-exit-map "R" 'gnus-summary-reselect-current-group)
6697   (define-key gnus-summary-exit-map "G" 'gnus-summary-rescan-group)
6698   (define-key gnus-summary-exit-map "N" 'gnus-summary-next-group)
6699   (define-key gnus-summary-exit-map "P" 'gnus-summary-prev-group)
6700
6701
6702   (define-prefix-command 'gnus-summary-article-map)
6703   (define-key gnus-summary-mode-map "A" 'gnus-summary-article-map)
6704   (define-key gnus-summary-article-map " " 'gnus-summary-next-page)
6705   (define-key gnus-summary-article-map "n" 'gnus-summary-next-page)
6706   (define-key gnus-summary-article-map "\177" 'gnus-summary-prev-page)
6707   (define-key gnus-summary-article-map "p" 'gnus-summary-prev-page)
6708   (define-key gnus-summary-article-map "\r" 'gnus-summary-scroll-up)
6709   (define-key gnus-summary-article-map "<" 'gnus-summary-beginning-of-article)
6710   (define-key gnus-summary-article-map ">" 'gnus-summary-end-of-article)
6711   (define-key gnus-summary-article-map "b" 'gnus-summary-beginning-of-article)
6712   (define-key gnus-summary-article-map "e" 'gnus-summary-end-of-article)
6713   (define-key gnus-summary-article-map "^" 'gnus-summary-refer-parent-article)
6714   (define-key gnus-summary-article-map "r" 'gnus-summary-refer-parent-article)
6715   (define-key gnus-summary-article-map "R" 'gnus-summary-refer-references)
6716   (define-key gnus-summary-article-map "g" 'gnus-summary-show-article)
6717   (define-key gnus-summary-article-map "s" 'gnus-summary-isearch-article)
6718
6719
6720
6721   (define-prefix-command 'gnus-summary-wash-map)
6722   (define-key gnus-summary-mode-map "W" 'gnus-summary-wash-map)
6723
6724   (define-prefix-command 'gnus-summary-wash-hide-map)
6725   (define-key gnus-summary-wash-map "W" 'gnus-summary-wash-hide-map)
6726   (define-key gnus-summary-wash-hide-map "a" 'gnus-article-hide)
6727   (define-key gnus-summary-wash-hide-map "h" 'gnus-article-hide-headers)
6728   (define-key gnus-summary-wash-hide-map "s" 'gnus-article-hide-signature)
6729   (define-key gnus-summary-wash-hide-map "c" 'gnus-article-hide-citation)
6730   (define-key gnus-summary-wash-hide-map "p" 'gnus-article-hide-pgp)
6731   (define-key gnus-summary-wash-hide-map 
6732     "\C-c" 'gnus-article-hide-citation-maybe)
6733
6734   (define-prefix-command 'gnus-summary-wash-highlight-map)
6735   (define-key gnus-summary-wash-map "H" 'gnus-summary-wash-highlight-map)
6736   (define-key gnus-summary-wash-highlight-map "a" 'gnus-article-highlight)
6737   (define-key gnus-summary-wash-highlight-map 
6738     "h" 'gnus-article-highlight-headers)
6739   (define-key gnus-summary-wash-highlight-map
6740     "c" 'gnus-article-highlight-citation)
6741   (define-key gnus-summary-wash-highlight-map
6742     "s" 'gnus-article-highlight-signature)
6743
6744   (define-prefix-command 'gnus-summary-wash-time-map)
6745   (define-key gnus-summary-wash-map "T" 'gnus-summary-wash-time-map)
6746   (define-key gnus-summary-wash-time-map "z" 'gnus-article-date-ut)
6747   (define-key gnus-summary-wash-time-map "u" 'gnus-article-date-ut)
6748   (define-key gnus-summary-wash-time-map "l" 'gnus-article-date-local)
6749   (define-key gnus-summary-wash-time-map "e" 'gnus-article-date-lapsed)
6750   (define-key gnus-summary-wash-time-map "o" 'gnus-article-date-original)
6751
6752   (define-key gnus-summary-wash-map "b" 'gnus-article-add-buttons)
6753   (define-key gnus-summary-wash-map "B" 'gnus-article-add-buttons-to-head)
6754   (define-key gnus-summary-wash-map "o" 'gnus-article-treat-overstrike)
6755   (define-key gnus-summary-wash-map "w" 'gnus-article-word-wrap)
6756   (define-key gnus-summary-wash-map "c" 'gnus-article-remove-cr)
6757   (define-key gnus-summary-wash-map "q" 'gnus-article-de-quoted-unreadable)
6758   (define-key gnus-summary-wash-map "f" 'gnus-article-display-x-face)
6759   (define-key gnus-summary-wash-map "l" 'gnus-summary-stop-page-breaking)
6760   (define-key gnus-summary-wash-map "r" 'gnus-summary-caesar-message)
6761   (define-key gnus-summary-wash-map "t" 'gnus-summary-toggle-header)
6762   (define-key gnus-summary-wash-map "m" 'gnus-summary-toggle-mime)
6763
6764
6765   (define-prefix-command 'gnus-summary-help-map)
6766   (define-key gnus-summary-mode-map "H" 'gnus-summary-help-map)
6767   (define-key gnus-summary-help-map "v" 'gnus-version)
6768   (define-key gnus-summary-help-map "f" 'gnus-summary-fetch-faq)
6769   (define-key gnus-summary-help-map "d" 'gnus-summary-describe-group)
6770   (define-key gnus-summary-help-map "h" 'gnus-summary-describe-briefly)
6771   (define-key gnus-summary-help-map "i" 'gnus-info-find-node)
6772
6773
6774   (define-prefix-command 'gnus-summary-backend-map)
6775   (define-key gnus-summary-mode-map "B" 'gnus-summary-backend-map)
6776   (define-key gnus-summary-backend-map "e" 'gnus-summary-expire-articles)
6777   (define-key gnus-summary-backend-map "\M-\C-e" 
6778     'gnus-summary-expire-articles-now)
6779   (define-key gnus-summary-backend-map "\177" 'gnus-summary-delete-article)
6780   (define-key gnus-summary-backend-map "m" 'gnus-summary-move-article)
6781   (define-key gnus-summary-backend-map "r" 'gnus-summary-respool-article)
6782   (define-key gnus-summary-backend-map "w" 'gnus-summary-edit-article)
6783   (define-key gnus-summary-backend-map "c" 'gnus-summary-copy-article)
6784   (define-key gnus-summary-backend-map "q" 'gnus-summary-respool-query)
6785   (define-key gnus-summary-backend-map "i" 'gnus-summary-import-article)
6786
6787
6788   (define-prefix-command 'gnus-summary-save-map)
6789   (define-key gnus-summary-mode-map "O" 'gnus-summary-save-map)
6790   (define-key gnus-summary-save-map "o" 'gnus-summary-save-article)
6791   (define-key gnus-summary-save-map "m" 'gnus-summary-save-article-mail)
6792   (define-key gnus-summary-save-map "r" 'gnus-summary-save-article-rmail)
6793   (define-key gnus-summary-save-map "f" 'gnus-summary-save-article-file)
6794   (define-key gnus-summary-save-map "b" 'gnus-summary-save-article-body-file)
6795   (define-key gnus-summary-save-map "h" 'gnus-summary-save-article-folder)
6796   (define-key gnus-summary-save-map "v" 'gnus-summary-save-article-vm)
6797   (define-key gnus-summary-save-map "p" 'gnus-summary-pipe-output)
6798   (define-key gnus-summary-save-map "s" 'gnus-soup-add-article)
6799
6800   (define-key gnus-summary-mode-map "X" 'gnus-uu-extract-map)
6801
6802   (define-key gnus-summary-mode-map "\M-&" 'gnus-summary-universal-argument)
6803   (define-key gnus-summary-article-map "D" 'gnus-summary-enter-digest-group)
6804
6805   (define-key gnus-summary-mode-map "V" 'gnus-summary-score-map)
6806
6807   (define-key gnus-summary-mode-map "I" 'gnus-summary-increase-score)
6808   (define-key gnus-summary-mode-map "L" 'gnus-summary-lower-score)
6809   )
6810
6811
6812 \f
6813
6814 (defun gnus-summary-mode (&optional group)
6815   "Major mode for reading articles.
6816
6817 All normal editing commands are switched off.
6818 \\<gnus-summary-mode-map>
6819 Each line in this buffer represents one article.  To read an
6820 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6821 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]', 
6822 respectively.
6823
6824 You can also post articles and send mail from this buffer.  To 
6825 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author 
6826 of an article, type `\\[gnus-summary-reply]'.
6827
6828 There are approx. one gazillion commands you can execute in this 
6829 buffer; read the info pages for more information (`\\[gnus-info-find-node]'). 
6830
6831 The following commands are available:
6832
6833 \\{gnus-summary-mode-map}"
6834   (interactive)
6835   (when (and menu-bar-mode
6836              (gnus-visual-p 'summary-menu 'menu))
6837     (gnus-summary-make-menu-bar))
6838   (kill-all-local-variables)
6839   (let ((locals gnus-summary-local-variables))
6840     (while locals
6841       (if (consp (car locals))
6842           (progn
6843             (make-local-variable (car (car locals)))
6844             (set (car (car locals)) (eval (cdr (car locals)))))
6845         (make-local-variable (car locals))
6846         (set (car locals) nil))
6847       (setq locals (cdr locals))))
6848   (gnus-make-thread-indent-array)
6849   (gnus-simplify-mode-line)
6850   (setq major-mode 'gnus-summary-mode)
6851   (setq mode-name "Summary")
6852   (make-local-variable 'minor-mode-alist)
6853   (use-local-map gnus-summary-mode-map)
6854   (buffer-disable-undo (current-buffer))
6855   (setq buffer-read-only t)             ;Disable modification
6856   (setq truncate-lines t)
6857   (setq selective-display t)
6858   (setq selective-display-ellipses t)   ;Display `...'
6859   (setq buffer-display-table gnus-summary-display-table)
6860   (setq gnus-newsgroup-name group)
6861   (run-hooks 'gnus-summary-mode-hook))
6862
6863 (defun gnus-summary-make-display-table ()
6864   ;; Change the display table.  Odd characters have a tendency to mess
6865   ;; up nicely formatted displays - we make all possible glyphs
6866   ;; display only a single character.
6867
6868   ;; We start from the standard display table, if any.
6869   (setq gnus-summary-display-table 
6870         (or (copy-sequence standard-display-table)
6871             (make-display-table)))
6872   ;; Nix out all the control chars...
6873   (let ((i 32))
6874     (while (>= (setq i (1- i)) 0)
6875       (aset gnus-summary-display-table i [??])))
6876   ;; ... but not newline and cr, of course. (cr is necessary for the
6877   ;; selective display).  
6878   (aset gnus-summary-display-table ?\n nil)
6879   (aset gnus-summary-display-table ?\r nil)
6880   ;; We nix out any glyphs over 126 that are not set already.  
6881   (let ((i 256))
6882     (while (>= (setq i (1- i)) 127)
6883       ;; Only modify if the entry is nil.
6884       (or (aref gnus-summary-display-table i) 
6885           (aset gnus-summary-display-table i [??])))))
6886
6887 (defun gnus-summary-clear-local-variables ()
6888   (let ((locals gnus-summary-local-variables))
6889     (while locals
6890       (if (consp (car locals))
6891           (and (vectorp (car (car locals)))
6892                (set (car (car locals)) nil))
6893         (and (vectorp (car locals))
6894              (set (car locals) nil)))
6895       (setq locals (cdr locals)))))
6896
6897 ;; Summary data functions.
6898
6899 (defmacro gnus-data-number (data)
6900   `(car ,data))
6901
6902 (defmacro gnus-data-mark (data)
6903   `(nth 1 ,data))
6904
6905 (defmacro gnus-data-set-mark (data mark)
6906   `(setcar (nthcdr 1 ,data) ,mark))
6907
6908 (defmacro gnus-data-pos (data)
6909   `(nth 2 ,data))
6910
6911 (defmacro gnus-data-set-pos (data pos)
6912   `(setcar (nthcdr 2 ,data) ,pos))
6913
6914 (defmacro gnus-data-header (data)
6915   `(nth 3 ,data))
6916
6917 (defmacro gnus-data-level (data)
6918   `(nth 4 ,data))
6919
6920 (defmacro gnus-data-unread-p (data)
6921   `(= (nth 1 ,data) gnus-unread-mark))
6922
6923 (defmacro gnus-data-pseudo-p (data)
6924   `(consp (nth 3 ,data)))
6925
6926 (defmacro gnus-data-find (number)
6927   `(assq ,number gnus-newsgroup-data))
6928
6929 (defmacro gnus-data-find-list (number &optional data)
6930   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
6931      (memq (assq ,number bdata)
6932            bdata)))
6933
6934 (defmacro gnus-data-make (number mark pos header level)
6935   `(list ,number ,mark ,pos ,header ,level))
6936
6937 (defun gnus-data-enter (after-article number mark pos header level offset)
6938   (let ((data (gnus-data-find-list after-article)))
6939     (or data (error "No such article: %d" after-article))
6940     (setcdr data (cons (gnus-data-make number mark pos header level)
6941                        (cdr data)))
6942     (setq gnus-newsgroup-data-reverse nil)
6943     (gnus-data-update-list (cdr (cdr data)) offset)))
6944
6945 (defun gnus-data-enter-list (after-article list &optional offset)
6946   (when list
6947     (let ((data (and after-article (gnus-data-find-list after-article)))
6948           (ilist list))
6949       (or data (not after-article) (error "No such article: %d" after-article))
6950       ;; Find the last element in the list to be spliced into the main
6951       ;; list.  
6952       (while (cdr list)
6953         (setq list (cdr list)))
6954       (if (not data)
6955           (progn
6956             (setcdr list gnus-newsgroup-data)
6957             (setq gnus-newsgroup-data ilist)
6958             (and offset (gnus-data-update-list (cdr list) offset)))
6959         (setcdr list (cdr data))
6960         (setcdr data ilist)
6961         (and offset (gnus-data-update-list (cdr data) offset)))
6962       (setq gnus-newsgroup-data-reverse nil))))
6963
6964 (defun gnus-data-remove (article &optional offset)
6965   (let ((data gnus-newsgroup-data))
6966     (if (= (gnus-data-number (car data)) article)
6967         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
6968               gnus-newsgroup-data-reverse nil)
6969       (while (cdr data)
6970         (and (= (gnus-data-number (car (cdr data))) article)
6971              (progn
6972                (setcdr data (cdr (cdr data)))
6973                (and offset (gnus-data-update-list (cdr data) offset))
6974                (setq data nil
6975                      gnus-newsgroup-data-reverse nil)))
6976         (setq data (cdr data))))))
6977
6978 (defmacro gnus-data-list (backward)
6979   `(if ,backward
6980        (or gnus-newsgroup-data-reverse
6981            (setq gnus-newsgroup-data-reverse
6982                  (reverse gnus-newsgroup-data)))
6983      gnus-newsgroup-data))
6984
6985 (defun gnus-data-update-list (data offset)
6986   "Add OFFSET to the POS of all data entries in DATA."
6987   (while data
6988     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
6989     (setq data (cdr data))))
6990
6991 (defun gnus-data-compute-positions ()
6992   "Compute the positions of all articles."
6993   (let ((data gnus-newsgroup-data)
6994         pos)
6995     (while data
6996       (when (setq pos (text-property-any 
6997                        (point-min) (point-max)
6998                        'gnus-number (gnus-data-number (car data))))
6999         (gnus-data-set-pos (car data) (+ pos 3)))
7000       (setq data (cdr data)))))
7001
7002 (defun gnus-summary-article-pseudo-p (article)
7003   "Say whether this article is a pseudo article or not."
7004   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7005
7006 (defun gnus-article-parent-p (number)
7007   "Say whether this article is a parent or not."
7008   (let* ((data (gnus-data-find-list number)))
7009     (and (cdr data)                     ; There has to be an article after...
7010          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7011             (gnus-data-level (nth 1 data))))))
7012     
7013 (defmacro gnus-summary-skip-intangible ()
7014   "If the current article is intangible, then jump to a different article."
7015   '(let ((to (get-text-property (point) 'gnus-intangible)))
7016     (when to
7017       (gnus-summary-goto-subject to))))
7018
7019 (defmacro gnus-summary-article-intangible-p ()
7020   "Say whether this article is intangible or not."
7021   '(get-text-property (point) 'gnus-intangible))
7022
7023 ;; Some summary mode macros.
7024
7025 (defmacro gnus-summary-article-number ()
7026   "The article number of the article on the current line.
7027 If there isn's an article number here, then we return the current
7028 article number."
7029   '(progn
7030      (gnus-summary-skip-intangible)
7031      (or (get-text-property (point) 'gnus-number) 
7032          (gnus-summary-last-subject))))
7033
7034 (defmacro gnus-summary-article-header (&optional number)
7035   `(gnus-data-header (gnus-data-find
7036                       ,(or number '(gnus-summary-article-number)))))
7037
7038 (defmacro gnus-summary-thread-level (&optional number)
7039   `(gnus-data-level (gnus-data-find
7040                      ,(or number '(gnus-summary-article-number)))))
7041
7042 (defmacro gnus-summary-article-mark (&optional number)
7043   `(gnus-data-mark (gnus-data-find
7044                     ,(or number '(gnus-summary-article-number)))))
7045
7046 (defmacro gnus-summary-article-pos (&optional number)
7047   `(gnus-data-pos (gnus-data-find
7048                    ,(or number '(gnus-summary-article-number)))))
7049
7050 (defmacro gnus-summary-article-subject (&optional number)
7051   "Return current subject string or nil if nothing."
7052   `(let ((headers 
7053           ,(if number
7054                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7055              '(gnus-data-header (assq (gnus-summary-article-number)
7056                                       gnus-newsgroup-data)))))
7057      (and headers
7058           (vectorp headers)
7059           (mail-header-subject headers))))
7060
7061 (defmacro gnus-summary-article-score (&optional number)
7062   "Return current article score."
7063   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7064                   gnus-newsgroup-scored))
7065        gnus-summary-default-score 0))
7066
7067 (defun gnus-summary-article-children (&optional number)
7068   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7069          (level (gnus-data-level (car data)))
7070          l children)
7071     (while (and (setq data (cdr data))
7072                 (> (setq l (gnus-data-level (car data))) level))
7073       (and (= (1+ level) l)
7074            (setq children (cons (gnus-data-number (car data))
7075                                 children))))
7076     (nreverse children)))
7077
7078 (defun gnus-summary-article-parent (&optional number)
7079   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7080                                     (gnus-data-list t)))
7081          (level (gnus-data-level (car data)))
7082          l)
7083     (if (zerop level)
7084         () ; This is a root.
7085       ;; We search until we find an article with a level less than
7086       ;; this one.  That function has to be the parent.
7087       (while (and (setq data (cdr data))
7088                   (not (< (gnus-data-level (car data)) level))))
7089       (and data (gnus-data-number (car data))))))
7090
7091
7092 ;; Various summary mode internalish functions.
7093
7094 (defun gnus-mouse-pick-article (e)
7095   (interactive "e")
7096   (mouse-set-point e)
7097   (gnus-summary-next-page nil t))
7098
7099 (defun gnus-summary-setup-buffer (group)
7100   "Initialize summary buffer."
7101   (let ((buffer (concat "*Summary " group "*")))
7102     (if (get-buffer buffer)
7103         (progn
7104           (set-buffer buffer)
7105           (setq gnus-summary-buffer (current-buffer))
7106           (not gnus-newsgroup-prepared))
7107       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7108       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7109       (gnus-add-current-to-buffer-list)
7110       (gnus-summary-mode group)
7111       (and gnus-carpal (gnus-carpal-setup-buffer 'summary))
7112       (setq gnus-newsgroup-name group)
7113       t)))
7114
7115 (defun gnus-set-global-variables ()
7116   ;; Set the global equivalents of the summary buffer-local variables
7117   ;; to the latest values they had.  These reflect the summary buffer
7118   ;; that was in action when the last article was fetched.
7119   (if (eq major-mode 'gnus-summary-mode) 
7120       (progn
7121         (setq gnus-summary-buffer (current-buffer))
7122         (let ((name gnus-newsgroup-name)
7123               (marked gnus-newsgroup-marked)
7124               (unread gnus-newsgroup-unreads)
7125               (headers gnus-current-headers)
7126               (data gnus-newsgroup-data)
7127               (score-file gnus-current-score-file))
7128           (save-excursion
7129             (set-buffer gnus-group-buffer)
7130             (setq gnus-newsgroup-name name)
7131             (setq gnus-newsgroup-marked marked)
7132             (setq gnus-newsgroup-unreads unread)
7133             (setq gnus-current-headers headers)
7134             (setq gnus-newsgroup-data data)
7135             (setq gnus-current-score-file score-file))))))
7136
7137 (defun gnus-summary-last-article-p (&optional article)
7138   "Return whether ARTICLE is the last article in the buffer."
7139   (if (not (setq article (or article (gnus-summary-article-number))))
7140       t ; All non-existant numbers are the last article. :-)
7141     (cdr (gnus-data-find-list article))))
7142     
7143 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7144   "Insert a dummy root in the summary buffer."
7145   (beginning-of-line)
7146   (add-text-properties
7147    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7148    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7149
7150 (defvar gnus-thread-indent-array nil)
7151 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7152 (defun gnus-make-thread-indent-array ()
7153   (let ((n 200))
7154     (if (and gnus-thread-indent-array
7155              (= gnus-thread-indent-level gnus-thread-indent-array-level))
7156         nil
7157       (setq gnus-thread-indent-array (make-vector 201 "")
7158             gnus-thread-indent-array-level gnus-thread-indent-level)
7159       (while (>= n 0)
7160         (aset gnus-thread-indent-array n
7161               (make-string (* n gnus-thread-indent-level) ? ))
7162         (setq n (1- n))))))
7163
7164 (defun gnus-summary-insert-line 
7165   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread 
7166                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7167                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7168   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7169          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7170          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7171          (gnus-tmp-score-char
7172           (if (or (null gnus-summary-default-score)
7173                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7174                       gnus-summary-zcore-fuzz)) ? 
7175             (if (< gnus-tmp-score gnus-summary-default-score)
7176                 gnus-score-below-mark gnus-score-over-mark)))
7177          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7178                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7179                                   gnus-cached-mark)
7180                                  (gnus-tmp-replied gnus-replied-mark)
7181                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7182                                   gnus-saved-mark)
7183                                  (t gnus-unread-mark)))
7184          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7185          (gnus-tmp-name 
7186           (cond 
7187            ((string-match "(.+)" gnus-tmp-from)
7188             (substring gnus-tmp-from 
7189                        (1+ (match-beginning 0)) (1- (match-end 0))))
7190            ((string-match "<[^>]+> *$" gnus-tmp-from)
7191             (let ((beg (match-beginning 0)))
7192               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7193                        (substring gnus-tmp-from (1+ (match-beginning 0))
7194                                   (1- (match-end 0))))
7195                   (substring gnus-tmp-from 0 beg))))
7196            (t gnus-tmp-from)))
7197          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7198          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7199          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7200          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7201          (buffer-read-only nil))
7202     (when (string= gnus-tmp-name "")
7203       (setq gnus-tmp-name gnus-tmp-from))
7204     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7205     (put-text-property
7206      (point)
7207      (progn (eval gnus-summary-line-format-spec) (point))
7208      'gnus-number gnus-tmp-number)
7209     (when (gnus-visual-p 'summary-highlight 'highlight)
7210       (forward-line -1)
7211       (run-hooks 'gnus-summary-update-hook)
7212       (forward-line 1))))
7213
7214 (defun gnus-summary-update-line (&optional dont-update)
7215   ;; Update summary line after change.
7216   (when (and gnus-summary-default-score
7217              (not gnus-summary-inhibit-highlight))
7218     (let ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7219           (article (gnus-summary-article-number)))
7220       (unless dont-update
7221         (if (and gnus-summary-mark-below
7222                  (< (gnus-summary-article-score)
7223                     gnus-summary-mark-below))
7224             ;; This article has a low score, so we mark it as read.
7225             (when (memq article gnus-newsgroup-unreads)
7226               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7227           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7228             ;; This article was previously marked as read on account
7229             ;; of a low score, but now it has risen, so we mark it as
7230             ;; unread. 
7231             (gnus-summary-mark-article-as-unread gnus-unread-mark))))
7232       ;; Do visual highlighting.
7233       (when (gnus-visual-p 'summary-highlight 'highlight)
7234         (run-hooks 'gnus-summary-update-hook)))))
7235
7236 (defvar gnus-tmp-new-adopts)
7237
7238 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7239   ;; Sum up all elements (and sub-elements) in a list.
7240   (let* ((number
7241           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7242           (cond ((and (consp thread) (cdr thread))
7243                  (apply
7244                   '+ 1 (mapcar
7245                         'gnus-summary-number-of-articles-in-thread 
7246                         (cdr thread))))
7247                 ((null thread)
7248                  1)
7249                 ((and level (zerop level) gnus-tmp-new-adopts)
7250                  (apply '+ 1 (mapcar 
7251                               'gnus-summary-number-of-articles-in-thread 
7252                               gnus-tmp-new-adopts)))
7253                 ((memq (mail-header-number (car thread))
7254                        gnus-newsgroup-limit)
7255                  1) 
7256                 (t 0))))
7257     (if char 
7258         (if (> number 1) gnus-not-empty-thread-mark
7259           gnus-empty-thread-mark)
7260       number)))
7261
7262 (defun gnus-summary-set-local-parameters (group)
7263  "Go through the local params of GROUP and set all variable specs in that list."
7264   (let ((params (gnus-info-params (gnus-get-info group)))
7265         elem)
7266     (while params
7267       (setq elem (car params)
7268             params (cdr params))
7269       (and (consp elem)                 ; Has to be a cons.
7270            (consp (cdr elem))           ; The cdr has to be a list.
7271            (symbolp (car elem))         ; Has to be a symbol in there.
7272            (progn                       ; So we set it.
7273              (make-local-variable (car elem))
7274              (set (car elem) (eval (nth 1 elem))))))))
7275
7276 (defun gnus-summary-read-group 
7277   (group &optional show-all no-article kill-buffer no-display)
7278   "Start reading news in newsgroup GROUP.
7279 If SHOW-ALL is non-nil, already read articles are also listed.
7280 If NO-ARTICLE is non-nil, no article is selected initially.
7281 If NO-DISPLAY, don't generate a summary buffer."
7282   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7283   (let* ((new-group (gnus-summary-setup-buffer group))
7284          (quit-config (gnus-group-quit-config group))
7285          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7286     (cond 
7287      ;; This summary buffer exists already, so we just select it. 
7288      ((not new-group)
7289       (gnus-set-global-variables)
7290       (gnus-kill-or-deaden-summary kill-buffer)
7291       (gnus-configure-windows 'summary 'force)
7292       (gnus-set-mode-line 'summary)
7293       (gnus-summary-position-point)
7294       (message "")
7295       t)
7296      ;; We couldn't select this group.
7297      ((null did-select) 
7298       (when (and (eq major-mode 'gnus-summary-mode)
7299                  (not (equal (current-buffer) kill-buffer)))
7300         (kill-buffer (current-buffer))
7301         (if (not quit-config)
7302             (progn
7303               (set-buffer gnus-group-buffer)
7304               (gnus-group-jump-to-group group)
7305               (gnus-group-next-unread-group 1))
7306           (if (not (buffer-name (car quit-config)))
7307               (gnus-configure-windows 'group 'force)
7308             (set-buffer (car quit-config))
7309             (and (eq major-mode 'gnus-summary-mode)
7310                  (gnus-set-global-variables))
7311             (gnus-configure-windows (cdr quit-config)))))
7312       (message "Can't select group")
7313       nil)
7314      ;; The user did a `C-g' while prompting for number of articles,
7315      ;; so we exit this group.
7316      ((eq did-select 'quit)
7317       (and (eq major-mode 'gnus-summary-mode)
7318            (not (equal (current-buffer) kill-buffer))
7319            (kill-buffer (current-buffer)))
7320       (gnus-kill-or-deaden-summary kill-buffer)
7321       (if (not quit-config)
7322           (progn
7323             (set-buffer gnus-group-buffer)
7324             (gnus-group-jump-to-group group)
7325             (gnus-group-next-unread-group 1)
7326             (gnus-configure-windows 'group 'force))
7327         (if (not (buffer-name (car quit-config)))
7328             (gnus-configure-windows 'group 'force)
7329           (set-buffer (car quit-config))
7330           (and (eq major-mode 'gnus-summary-mode)
7331                (gnus-set-global-variables))
7332           (gnus-configure-windows (cdr quit-config))))
7333       ;; Finallt signal the quit.
7334       (signal 'quit nil))
7335      ;; The group was successfully selected.
7336      (t
7337       (gnus-set-global-variables)
7338       ;; Save the active value in effect when the group was entered.
7339       (setq gnus-newsgroup-active 
7340             (gnus-copy-sequence
7341              (gnus-active gnus-newsgroup-name)))
7342       ;; You can change the summary buffer in some way with this hook.
7343       (run-hooks 'gnus-select-group-hook)
7344       ;; Set any local variables in the group parameters.
7345       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7346       ;; Do score processing.
7347       (when gnus-use-scoring
7348         (gnus-possibly-score-headers))
7349       (gnus-update-format-specifications)
7350       ;; Find the initial limit.
7351       (gnus-summary-initial-limit)
7352       ;; Generate the summary buffer.
7353       (unless no-display
7354         (gnus-summary-prepare))
7355       ;; If the summary buffer is empty, but there are some low-scored
7356       ;; articles or some excluded dormants, we include these in the
7357       ;; buffer. 
7358       (when (zerop (buffer-size))
7359         (cond (gnus-newsgroup-dormant
7360                (gnus-summary-limit-include-dormant))
7361               ((and gnus-newsgroup-scored show-all)
7362                (gnus-summary-limit-include-expunged))))
7363       ;; Function `gnus-apply-kill-file' must be called in this hook.
7364       (run-hooks 'gnus-apply-kill-hook)
7365       (if (zerop (buffer-size))
7366           (progn
7367             ;; This newsgroup is empty.
7368             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7369             (gnus-message 6 "No unread news")
7370             (gnus-kill-or-deaden-summary kill-buffer)
7371             ;; Return nil from this function.
7372             nil)
7373         ;; Hide conversation thread subtrees.  We cannot do this in
7374         ;; gnus-summary-prepare-hook since kill processing may not
7375         ;; work with hidden articles.
7376         (and gnus-show-threads
7377              gnus-thread-hide-subtree
7378              (gnus-summary-hide-all-threads))
7379         ;; Show first unread article if requested.
7380         (if (and (not no-article)
7381                  gnus-newsgroup-unreads
7382                  gnus-auto-select-first)
7383             (progn
7384               (if (eq gnus-auto-select-first 'best)
7385                   (gnus-summary-best-unread-article)
7386                 (gnus-summary-first-unread-article)))
7387           ;; Don't select any articles, just move point to the first
7388           ;; article in the group.
7389           (goto-char (point-min))
7390           (gnus-summary-position-point)
7391           (gnus-set-mode-line 'summary)
7392           (gnus-configure-windows 'summary 'force))
7393         ;; If we are in async mode, we send some info to the backend.
7394         (when gnus-newsgroup-async
7395           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7396         (gnus-kill-or-deaden-summary kill-buffer)
7397         (when (get-buffer-window gnus-group-buffer)
7398           ;; Gotta use windows, because recenter does wierd stuff if
7399           ;; the current buffer ain't the displayed window.
7400           (let ((owin (selected-window))) 
7401             (select-window (get-buffer-window gnus-group-buffer))
7402             (when (gnus-group-goto-group group)
7403               (recenter))
7404             (select-window owin))))
7405       ;; Mark this buffer as "prepared".
7406       (setq gnus-newsgroup-prepared t)
7407       t))))
7408
7409 (defun gnus-summary-prepare ()
7410   "Generate the summary buffer."
7411   (let ((buffer-read-only nil))
7412     (erase-buffer)
7413     (setq gnus-newsgroup-data nil
7414           gnus-newsgroup-data-reverse nil)
7415     (run-hooks 'gnus-summary-generate-hook)
7416     ;; Generate the buffer, either with threads or without.
7417     (gnus-summary-prepare-threads 
7418      (if gnus-show-threads
7419          (gnus-gather-threads (gnus-sort-threads (gnus-make-threads)))
7420        gnus-newsgroup-headers))
7421     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7422     ;; Call hooks for modifying summary buffer.
7423     (goto-char (point-min))
7424     (run-hooks 'gnus-summary-prepare-hook)))
7425
7426 (defun gnus-gather-threads (threads)
7427   "Gather threads that have lost their roots."
7428   (if (not gnus-summary-make-false-root)
7429       threads 
7430     (let ((hashtb (gnus-make-hashtable 1023))
7431           (prev threads)
7432           (result threads)
7433           subject hthread whole-subject)
7434       (while threads
7435         (setq whole-subject 
7436               (setq subject (mail-header-subject (car (car threads)))))
7437         (if (and gnus-summary-gather-exclude-subject
7438                  (string-match gnus-summary-gather-exclude-subject
7439                                subject))
7440             () ; We don't want to do anything with this.
7441           (if gnus-summary-gather-subject-limit
7442               (or (and (numberp gnus-summary-gather-subject-limit)
7443                        (> (length subject) gnus-summary-gather-subject-limit)
7444                        (setq subject
7445                              (substring subject 0 
7446                                         gnus-summary-gather-subject-limit)))
7447                   (and (eq 'fuzzy gnus-summary-gather-subject-limit)
7448                        (setq subject (gnus-simplify-subject-fuzzy subject))))
7449             (setq subject (gnus-simplify-subject-re subject)))
7450           (if (setq hthread 
7451                     (gnus-gethash subject hashtb))
7452               (progn
7453                 (or (stringp (car (car hthread)))
7454                     (setcar hthread (list whole-subject (car hthread))))
7455                 (setcdr (car hthread) (nconc (cdr (car hthread)) 
7456                                              (list (car threads))))
7457                 (setcdr prev (cdr threads))
7458                 (setq threads prev))
7459             (gnus-sethash subject threads hashtb)))
7460         (setq prev threads)
7461         (setq threads (cdr threads)))
7462       result)))
7463
7464 (defun gnus-make-threads ()
7465   "Go through the dependency hashtb and find the roots.  Return all threads."
7466   ;; Then we find all the roots and return all the threads.
7467   (let (threads)
7468     (mapatoms
7469      (lambda (refs)
7470        (or (car (symbol-value refs))
7471            (setq threads (append (cdr (symbol-value refs)) threads))))
7472      gnus-newsgroup-dependencies)
7473     threads))
7474   
7475 (defun gnus-build-old-threads ()
7476   ;; Look at all the articles that refer back to old articles, and
7477   ;; fetch the headers for the articles that aren't there.  This will
7478   ;; build complete threads - if the roots haven't been expired by the
7479   ;; server, that is.
7480   (let (id heads)
7481     (mapatoms
7482      (lambda (refs)
7483        (when (not (car (symbol-value refs)))
7484          (setq heads (cdr (symbol-value refs)))
7485          (while heads
7486            (if (memq (mail-header-number (car (car heads)))
7487                      gnus-newsgroup-dormant)
7488                (setq heads (cdr heads))
7489              (setq id (symbol-name refs))
7490              (while (and (setq id (gnus-build-get-header id))
7491                          (not (car (gnus-gethash 
7492                                     id gnus-newsgroup-dependencies)))))
7493              (setq heads nil)))))
7494      gnus-newsgroup-dependencies)))
7495
7496 (defun gnus-build-get-header (id)
7497   ;; Look through the buffer of NOV lines and find the header to
7498   ;; ID.  Enter this line into the dependencies hash table, and return
7499   ;; the id of the parent article (if any).
7500   (let ((deps gnus-newsgroup-dependencies)
7501         found header)
7502     (prog1
7503         (save-excursion
7504           (set-buffer nntp-server-buffer)
7505           (goto-char (point-min))
7506           (while (and (not found) (search-forward id nil t))
7507             (beginning-of-line)
7508             (setq found (looking-at 
7509                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7510                                  (regexp-quote id))))
7511             (or found (beginning-of-line 2)))
7512           (when found
7513             (let (ref)
7514               (beginning-of-line)
7515               (and
7516                (setq header (gnus-nov-parse-line 
7517                              (read (current-buffer)) deps))
7518                (gnus-parent-id (mail-header-references header))))))
7519       (when header
7520         (let ((number (mail-header-number header)))
7521           (push number gnus-newsgroup-limit)
7522           (push header gnus-newsgroup-headers)
7523           (push number gnus-newsgroup-ancient))))))
7524
7525 (defun gnus-rebuild-thread (id)
7526   "Rebuild the thread containing ID."
7527   (let ((dep gnus-newsgroup-dependencies)
7528         (buffer-read-only nil)
7529         current headers refs thread art data)
7530     (if (not gnus-show-threads)
7531         (setq thread (list (car (gnus-gethash (downcase id) dep))))
7532       ;; Get the thread this article is part of.
7533       (setq thread (gnus-remove-thread id)))
7534     (setq current (save-excursion
7535                     (and (zerop (forward-line -1))
7536                          (gnus-summary-article-number))))
7537     ;; If this is a gathered thread, we have to go some re-gathering.
7538     (when (stringp (car thread))
7539       (let ((subject (car thread))
7540             roots thr)
7541         (setq thread (cdr thread))
7542         (while thread
7543           (unless (memq (setq thr (gnus-id-to-thread 
7544                                       (gnus-root-id
7545                                        (mail-header-id (car (car thread))))))
7546                         roots)
7547             (push thr roots))
7548           (setq thread (cdr thread)))
7549         ;; We now have all (unique) roots.
7550         (if (= (length roots) 1)
7551             ;; All the loose roots are now one solid root.
7552             (setq thread (car roots))
7553           (setq thread (cons subject (gnus-sort-threads roots))))))
7554     (let ((beg (point)) 
7555           threads)
7556       ;; We then insert this thread into the summary buffer.
7557       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7558         (gnus-summary-prepare-threads (list thread))
7559         (setq data (nreverse gnus-newsgroup-data))
7560         (setq threads gnus-newsgroup-threads))
7561       ;; We splice the new data into the data structure.
7562       (gnus-data-enter-list current data)
7563       (gnus-data-compute-positions)
7564       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7565
7566 (defun gnus-id-to-thread (id)
7567   "Return the (sub-)thread where ID appears."
7568   (gnus-gethash (downcase id) gnus-newsgroup-dependencies))
7569
7570 (defun gnus-root-id (id)
7571   "Return the id of the root of the thread where ID appears."
7572   (let (last-id prev)
7573     (while (and id (setq prev (car (gnus-gethash 
7574                                     (downcase id)
7575                                     gnus-newsgroup-dependencies))))
7576       (setq last-id id
7577             id (gnus-parent-id (mail-header-references prev))))
7578     last-id))
7579
7580 (defun gnus-remove-thread (id)
7581   "Remove the thread that has ID in it."
7582   (let ((dep gnus-newsgroup-dependencies)
7583         headers thread prev last-id)
7584     ;; First go up in this thread until we find the root.
7585     (setq last-id (gnus-root-id id))
7586     (setq headers (list (car (gnus-id-to-thread last-id))
7587                         (car (car (cdr (gnus-id-to-thread last-id))))))
7588     ;; We have now found the real root of this thread.  It might have
7589     ;; been gathered into some loose thread, so we have to search
7590     ;; through the threads to find the thread we wanted.
7591     (let ((threads gnus-newsgroup-threads)
7592           sub)
7593       (while threads
7594         (setq sub (car threads))
7595         (if (stringp (car sub))
7596             ;; This is a gathered threads, so we look at the roots
7597             ;; below it to find whether this article in in this
7598             ;; gathered root.
7599             (progn
7600               (setq sub (cdr sub))
7601               (while sub
7602                 (when (member (car (car sub)) headers)
7603                   (setq thread (car threads)
7604                         threads nil
7605                         sub nil))
7606                 (setq sub (cdr sub))))
7607           ;; It's an ordinary thread, so we check it.
7608           (when (eq (car sub) (car headers))
7609             (setq thread sub
7610                   threads nil)))
7611         (setq threads (cdr threads)))
7612       ;; If this article is in no thread, then it's a root. 
7613       (if thread 
7614           (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads))
7615         (setq thread (gnus-gethash (downcase last-id) dep)))
7616       (when thread
7617         (prog1 
7618             thread ; We return this thread.
7619           (if (stringp (car thread))
7620               (progn
7621                 ;; If we use dummy roots, then we have to remove the
7622                 ;; dummy root as well.
7623                 (when (eq gnus-summary-make-false-root 'dummy)
7624                   ;; Uhm.
7625                   )
7626                 (setq thread (cdr thread))
7627                 (while thread
7628                   (gnus-remove-thread-1 (car thread))
7629                   (setq thread (cdr thread))))
7630             (gnus-remove-thread-1 thread)))))))
7631
7632 (defun gnus-remove-thread-1 (thread)
7633   "Remove the thread THREAD recursively."
7634   (let ((number (mail-header-number (car thread)))
7635         pos)
7636     (when (setq pos (text-property-any 
7637                      (point-min) (point-max) 'gnus-number number))
7638       (goto-char pos)
7639       (gnus-delete-line)
7640       (gnus-data-remove number))
7641     (setq thread (cdr thread))
7642     (while thread
7643       (gnus-remove-thread-1 (car thread))
7644       (setq thread (cdr thread)))))
7645
7646 (defun gnus-sort-threads (threads)
7647   "Sort THREADS as specified in `gnus-thread-sort-functions'."
7648   (let ((funs gnus-thread-sort-functions))
7649     (when funs
7650       (while funs
7651         (gnus-message 7 "Sorting with %S..." (car funs))
7652         (setq threads (sort threads (pop funs))))
7653       (gnus-message 7 "Sorting...done")))
7654   threads)
7655
7656 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
7657 (defmacro gnus-thread-header (thread)
7658   ;; Return header of first article in THREAD.
7659   ;; Note that THREAD must never, evr be anything else than a variable -
7660   ;; using some other form will lead to serious barfage.
7661   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
7662   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
7663   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ; 
7664         (vector thread) 2))
7665
7666 (defsubst gnus-article-sort-by-number (h1 h2)
7667   "Sort articles by article number."
7668   (< (mail-header-number h1)
7669      (mail-header-number h2)))
7670
7671 (defun gnus-thread-sort-by-number (h1 h2)
7672   "Sort threads by root article number."
7673   (gnus-article-sort-by-number 
7674    (gnus-thread-header h1) (gnus-thread-header h2)))
7675
7676 (defsubst gnus-article-sort-by-author (h1 h2)
7677   "Sort articles by root author."
7678   (string-lessp
7679    (let ((extract (funcall 
7680                    gnus-extract-address-components
7681                    (mail-header-from h1))))
7682      (or (car extract) (cdr extract)))
7683    (let ((extract (funcall
7684                    gnus-extract-address-components 
7685                    (mail-header-from h2))))
7686      (or (car extract) (cdr extract)))))
7687
7688 (defun gnus-thread-sort-by-author (h1 h2)
7689   "Sort threads by root author."
7690   (gnus-article-sort-by-author
7691    (gnus-thread-header h1)  (gnus-thread-header h2)))
7692
7693 (defsubst gnus-article-sort-by-subject (h1 h2)
7694   "Sort articles by root subject."
7695   (string-lessp
7696    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
7697    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
7698
7699 (defun gnus-thread-sort-by-subject (h1 h2)
7700   "Sort threads by root subject."
7701   (gnus-article-sort-by-subject 
7702    (gnus-thread-header h1) (gnus-thread-header h2)))
7703
7704 (defsubst gnus-article-sort-by-date (h1 h2)
7705   "Sort articles by root article date."
7706   (string-lessp
7707    (gnus-sortable-date (mail-header-date h1))
7708    (gnus-sortable-date (mail-header-date h2))))
7709
7710 (defun gnus-thread-sort-by-date (h1 h2)
7711   "Sort threads by root article date."
7712   (gnus-article-sort-by-date 
7713    (gnus-thread-header h1) (gnus-thread-header h2)))
7714
7715 (defsubst gnus-article-sort-by-score (h1 h2)
7716   "Sort articles by root article score.
7717 Unscored articles will be counted as having a score of zero."
7718   (> (or (cdr (assq (mail-header-number h1)
7719                     gnus-newsgroup-scored))
7720          gnus-summary-default-score 0)
7721      (or (cdr (assq (mail-header-number h2)
7722                     gnus-newsgroup-scored))
7723          gnus-summary-default-score 0)))
7724
7725 (defun gnus-thread-sort-by-score (h1 h2)
7726   "Sort threads by root article score."
7727   (gnus-article-sort-by-score 
7728    (gnus-thread-header h1) (gnus-thread-header h2)))
7729
7730 (defun gnus-thread-sort-by-total-score (h1 h2)
7731   "Sort threads by the sum of all scores in the thread.
7732 Unscored articles will be counted as having a score of zero."
7733   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
7734
7735 (defun gnus-thread-total-score (thread)
7736   ;;  This function find the total score of THREAD.
7737   (if (consp thread)
7738       (if (stringp (car thread))
7739           (apply gnus-thread-score-function 0
7740                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
7741         (gnus-thread-total-score-1 thread))
7742     (gnus-thread-total-score-1 (list thread))))
7743
7744 (defun gnus-thread-total-score-1 (root)
7745   ;; This function find the total score of the thread below ROOT.
7746   (setq root (car root))
7747   (apply gnus-thread-score-function
7748          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
7749              gnus-summary-default-score 0)
7750          (mapcar 'gnus-thread-total-score
7751                  (cdr (gnus-gethash (downcase (mail-header-id root))
7752                                     gnus-newsgroup-dependencies)))))
7753
7754 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
7755 (defvar gnus-tmp-prev-subject nil)
7756 (defvar gnus-tmp-false-parent nil)
7757 (defvar gnus-tmp-root-expunged nil)
7758 (defvar gnus-tmp-dummy-line nil)
7759
7760 (defun gnus-summary-prepare-threads (threads)
7761   "Prepare summary buffer from THREADS and indentation LEVEL.  
7762 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'  
7763 or a straight list of headers."
7764   (message "Generating summary...")
7765
7766   (setq gnus-newsgroup-threads threads)
7767   (beginning-of-line)
7768
7769   (let ((gnus-tmp-level 0)
7770         (default-score (or gnus-summary-default-score 0))
7771         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
7772         thread number subject stack state gnus-tmp-gathered beg-match
7773         new-roots gnus-tmp-new-adopts thread-end
7774         gnus-tmp-header gnus-tmp-unread
7775         gnus-tmp-replied gnus-tmp-subject-or-nil
7776         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
7777         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
7778         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
7779
7780     (setq gnus-tmp-prev-subject nil)
7781
7782     (if (vectorp (car threads))
7783         ;; If this is a straight (sic) list of headers, then a
7784         ;; threaded summary display isn't required, so we just create
7785         ;; an unthreaded one.
7786         (gnus-summary-prepare-unthreaded threads)
7787
7788       ;; Do the threaded display.
7789
7790       (while (or threads stack gnus-tmp-new-adopts new-roots)
7791
7792         (if (and (= gnus-tmp-level 0)
7793                  (not (setq gnus-tmp-dummy-line nil))
7794                  (or (not stack)
7795                      (= (car (car stack)) 0))
7796                  (not gnus-tmp-false-parent)
7797                  (or gnus-tmp-new-adopts new-roots))
7798             (if gnus-tmp-new-adopts
7799                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
7800                       thread (list (car gnus-tmp-new-adopts))
7801                       gnus-tmp-header (car (car thread))
7802                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
7803               (if new-roots
7804                   (setq thread (list (car new-roots))
7805                         gnus-tmp-header (car (car thread))
7806                         new-roots (cdr new-roots))))
7807
7808           (if threads
7809               ;; If there are some threads, we do them before the
7810               ;; threads on the stack.
7811               (setq thread threads
7812                     gnus-tmp-header (car (car thread)))
7813             ;; There were no current threads, so we pop something off
7814             ;; the stack. 
7815             (setq state (car stack)
7816                   gnus-tmp-level (car state)
7817                   thread (cdr state)
7818                   stack (cdr stack)
7819                   gnus-tmp-header (car (car thread)))))
7820
7821         (setq gnus-tmp-false-parent nil)
7822         (setq gnus-tmp-root-expunged nil)
7823         (setq thread-end nil)
7824
7825         (if (stringp gnus-tmp-header)
7826             ;; The header is a dummy root.
7827             (cond 
7828              ((eq gnus-summary-make-false-root 'adopt)
7829               ;; We let the first article adopt the rest.
7830               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
7831                                                (cdr (cdr (car thread)))))
7832               (setq gnus-tmp-gathered 
7833                     (nconc (mapcar
7834                             (lambda (h) (mail-header-number (car h)))
7835                             (cdr (cdr (car thread))))
7836                            gnus-tmp-gathered))
7837               (setq thread (cons (list (car (car thread))
7838                                        (car (cdr (car thread))))
7839                                  (cdr thread)))
7840               (setq gnus-tmp-level -1
7841                     gnus-tmp-false-parent t))
7842              ((eq gnus-summary-make-false-root 'empty)
7843               ;; We print adopted articles with empty subject fields.
7844               (setq gnus-tmp-gathered 
7845                     (nconc (mapcar
7846                             (lambda (h) (mail-header-number (car h)))
7847                             (cdr (cdr (car thread))))
7848                            gnus-tmp-gathered))
7849               (setq gnus-tmp-level -1))
7850              ((eq gnus-summary-make-false-root 'dummy)
7851               ;; We remember that we probably want to output a dummy
7852               ;; root.   
7853               (setq gnus-tmp-dummy-line gnus-tmp-header)
7854               (setq gnus-tmp-prev-subject gnus-tmp-header))
7855              (t
7856               ;; We do not make a root for the gathered
7857               ;; sub-threads at all.  
7858               (setq gnus-tmp-level -1)))
7859       
7860           (setq number (mail-header-number gnus-tmp-header)
7861                 subject (mail-header-subject gnus-tmp-header))
7862
7863           (cond 
7864            ;; If the thread has changed subject, we might want to make 
7865            ;; this subthread into a root.
7866            ((and (null gnus-thread-ignore-subject)
7867                  (not (zerop gnus-tmp-level))
7868                  gnus-tmp-prev-subject
7869                  (not (inline
7870                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
7871             (setq new-roots (nconc new-roots (list (car thread)))
7872                   thread-end t
7873                   gnus-tmp-header nil))
7874            ;; If the article lies outside the current limit,
7875            ;; then we do not display it.
7876            ((not (memq number gnus-newsgroup-limit))
7877             (setq gnus-tmp-gathered 
7878                   (nconc (mapcar
7879                           (lambda (h) (mail-header-number (car h)))
7880                           (cdr (car thread)))
7881                          gnus-tmp-gathered))
7882             (setq gnus-tmp-new-adopts (if (cdr (car thread))
7883                                           (append gnus-tmp-new-adopts 
7884                                                   (cdr (car thread)))
7885                                         gnus-tmp-new-adopts)
7886                   thread-end t
7887                   gnus-tmp-header nil)
7888             (when (zerop gnus-tmp-level)
7889               (setq gnus-tmp-root-expunged t)))
7890            ;; Perhaps this article is to be marked as read?
7891            ((and gnus-summary-mark-below
7892                  (< (or (cdr (assq number gnus-newsgroup-scored))
7893                         default-score)
7894                     gnus-summary-mark-below))
7895             (setq gnus-newsgroup-unreads 
7896                   (delq number gnus-newsgroup-unreads))
7897             (if gnus-newsgroup-auto-expire
7898                 (push number gnus-newsgroup-expirable)
7899               (push (cons number gnus-low-score-mark)
7900                     gnus-newsgroup-reads))))
7901           
7902           (when gnus-tmp-header
7903             ;; We may have an old dummy line to output before this
7904             ;; article.  
7905             (when gnus-tmp-dummy-line
7906               (gnus-summary-insert-dummy-line 
7907                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
7908               (setq gnus-tmp-dummy-line nil))
7909
7910             ;; Compute the mark.
7911             (setq 
7912              gnus-tmp-unread
7913              (cond 
7914               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
7915               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
7916               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
7917               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
7918               (t (or (cdr (assq number gnus-newsgroup-reads))
7919                      gnus-ancient-mark))))
7920
7921             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
7922                                   gnus-tmp-header gnus-tmp-level)
7923                   gnus-newsgroup-data)
7924
7925             ;; Actually insert the line.
7926             (setq 
7927              gnus-tmp-subject-or-nil
7928              (cond
7929               ((and gnus-thread-ignore-subject
7930                     gnus-tmp-prev-subject
7931                     (not (inline (gnus-subject-equal 
7932                                   gnus-tmp-prev-subject subject))))
7933                subject)
7934               ((zerop gnus-tmp-level)
7935                (if (and (eq gnus-summary-make-false-root 'empty)
7936                         (memq number gnus-tmp-gathered)
7937                         gnus-tmp-prev-subject
7938                         (inline (gnus-subject-equal
7939                                  gnus-tmp-prev-subject subject)))
7940                    gnus-summary-same-subject
7941                  subject))
7942               (t gnus-summary-same-subject)))
7943             (if (and (eq gnus-summary-make-false-root 'adopt)
7944                      (= gnus-tmp-level 1)
7945                      (memq number gnus-tmp-gathered))
7946                 (setq gnus-tmp-opening-bracket ?\<
7947                       gnus-tmp-closing-bracket ?\>)
7948               (setq gnus-tmp-opening-bracket ?\[
7949                     gnus-tmp-closing-bracket ?\]))
7950             (setq 
7951              gnus-tmp-indentation 
7952              (aref gnus-thread-indent-array gnus-tmp-level)
7953              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
7954              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
7955                                 gnus-summary-default-score 0)
7956              gnus-tmp-score-char
7957              (if (or (null gnus-summary-default-score)
7958                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7959                          gnus-summary-zcore-fuzz)) ? 
7960                (if (< gnus-tmp-score gnus-summary-default-score)
7961                    gnus-score-below-mark gnus-score-over-mark))
7962              gnus-tmp-replied
7963              (cond ((memq number gnus-newsgroup-processable)
7964                     gnus-process-mark)
7965                    ((memq number gnus-newsgroup-cached)
7966                     gnus-cached-mark)
7967                    ((memq number gnus-newsgroup-replied)
7968                     gnus-replied-mark)
7969                    (t gnus-unread-mark))
7970              gnus-tmp-from (mail-header-from gnus-tmp-header)
7971              gnus-tmp-name 
7972              (cond 
7973               ((string-match "(.+)" gnus-tmp-from)
7974                (substring gnus-tmp-from 
7975                           (1+ (match-beginning 0)) (1- (match-end 0))))
7976               ((string-match "<[^>]+> *$" gnus-tmp-from)
7977                (setq beg-match (match-beginning 0))
7978                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7979                         (substring gnus-tmp-from (1+ (match-beginning 0))
7980                                    (1- (match-end 0))))
7981                    (substring gnus-tmp-from 0 beg-match)))
7982               (t gnus-tmp-from)))
7983             (when (string= gnus-tmp-name "")
7984               (setq gnus-tmp-name gnus-tmp-from))
7985             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7986             (put-text-property
7987              (point)
7988              (progn (eval gnus-summary-line-format-spec) (point))
7989              'gnus-number number)
7990             (when gnus-visual-p
7991               (forward-line -1)
7992               (run-hooks 'gnus-summary-update-hook)
7993               (forward-line 1))
7994
7995             (setq gnus-tmp-prev-subject subject)))
7996
7997         (when (nth 1 thread) 
7998           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
7999         (incf gnus-tmp-level)
8000         (setq threads (if thread-end nil (cdr (car thread))))
8001         (unless threads
8002           (setq gnus-tmp-level 0)))))
8003   (message "Generating summary...done"))
8004
8005 (defun gnus-summary-prepare-unthreaded (headers)
8006   "Generate an unthreaded summary buffer based on HEADERS."
8007   (let (header number mark)
8008
8009     (while headers
8010       (setq header (car headers)
8011             headers (cdr headers)
8012             number (mail-header-number header))
8013
8014       ;; We may have to root out some bad articles...
8015       (when (memq number gnus-newsgroup-limit)
8016         (when (and gnus-summary-mark-below
8017                    (< (or (cdr (assq number gnus-newsgroup-scored))
8018                           gnus-summary-default-score 0)
8019                       gnus-summary-mark-below))
8020           (setq gnus-newsgroup-unreads 
8021                 (delq number gnus-newsgroup-unreads))
8022           (if gnus-newsgroup-auto-expire
8023               (push number gnus-newsgroup-expirable)
8024             (push (cons number gnus-low-score-mark)
8025                   gnus-newsgroup-reads)))
8026           
8027         (setq mark
8028               (cond 
8029                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8030                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8031                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8032                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8033                (t (or (cdr (assq number gnus-newsgroup-reads))
8034                       gnus-ancient-mark))))
8035         (setq gnus-newsgroup-data 
8036               (cons (gnus-data-make number mark (1+ (point)) header 0)
8037                     gnus-newsgroup-data))
8038         (gnus-summary-insert-line
8039          header 0 nil mark (memq number gnus-newsgroup-replied)
8040          (memq number gnus-newsgroup-expirable)
8041          (mail-header-subject header) nil
8042          (cdr (assq number gnus-newsgroup-scored))
8043          (memq number gnus-newsgroup-processable))))))
8044
8045 (defun gnus-select-newsgroup (group &optional read-all)
8046   "Select newsgroup GROUP.
8047 If READ-ALL is non-nil, all articles in the group are selected."
8048   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8049          (info (nth 2 entry))
8050          articles fetched-articles)
8051
8052     (or (gnus-check-server
8053          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8054         (error "Couldn't open server"))
8055     
8056     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8057         (gnus-activate-group group) ; Or we can activate it...
8058         (progn ; Or we bug out.
8059           (kill-buffer (current-buffer))
8060           (error "Couldn't request group %s: %s" 
8061                  group (gnus-status-message group))))
8062
8063     (setq gnus-newsgroup-name group)
8064     (setq gnus-newsgroup-unselected nil)
8065     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8066
8067     (and gnus-asynchronous
8068          (gnus-check-backend-function 
8069           'request-asynchronous gnus-newsgroup-name)
8070          (setq gnus-newsgroup-async
8071                (gnus-request-asynchronous gnus-newsgroup-name)))
8072
8073     ;; Adjust and set lists of article marks.
8074     (when info
8075       (gnus-adjust-marked-articles info))
8076
8077     (setq gnus-newsgroup-unreads 
8078           (gnus-set-difference
8079            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8080            gnus-newsgroup-dormant))
8081
8082     (setq gnus-newsgroup-processable nil)
8083     
8084     (setq articles (gnus-articles-to-read group read-all))
8085     
8086     (cond 
8087      ((null articles) 
8088       (gnus-message 3 "Couldn't select newsgroup")
8089       'quit)
8090      ((eq articles 0) nil)
8091      (t
8092       ;; Init the dependencies hash table.
8093       (setq gnus-newsgroup-dependencies 
8094             (gnus-make-hashtable (length articles)))
8095       ;; Retrieve the headers and read them in.
8096       (gnus-message 5 "Fetching headers...")
8097       (setq gnus-newsgroup-headers 
8098             (if (eq 'nov 
8099                     (setq gnus-headers-retrieved-by
8100                           (gnus-retrieve-headers 
8101                            articles gnus-newsgroup-name
8102                            ;; We might want to fetch old headers, but
8103                            ;; not if there is only 1 article.
8104                            (and gnus-fetch-old-headers
8105                                 (or (and 
8106                                      (not (eq gnus-fetch-old-headers 'some))
8107                                      (not (numberp gnus-fetch-old-headers)))
8108                                     (> (length articles) 1))))))
8109                 (gnus-get-newsgroup-headers-xover articles)
8110               (gnus-get-newsgroup-headers)))
8111       (gnus-message 5 "Fetching headers...done")      
8112       ;; Set the initial limit.
8113       (setq gnus-newsgroup-limit (copy-sequence articles))
8114       ;; Remove canceled articles from the list of unread articles.
8115       (setq gnus-newsgroup-unreads
8116             (gnus-set-sorted-intersection 
8117              gnus-newsgroup-unreads
8118              (setq fetched-articles
8119                    (mapcar (lambda (headers) (mail-header-number headers))
8120                            gnus-newsgroup-headers))))
8121       ;; Removed marked articles that do not exist.
8122       (gnus-update-missing-marks 
8123        (gnus-sorted-complement fetched-articles articles))
8124       ;; We might want to build some more threads first.
8125       (and gnus-fetch-old-headers
8126            (eq gnus-headers-retrieved-by 'nov)
8127            (gnus-build-old-threads))
8128       ;; Check whether auto-expire is to be done in this group.
8129       (setq gnus-newsgroup-auto-expire
8130             (gnus-group-auto-expirable-p group))
8131       ;; First and last article in this newsgroup.
8132       (and gnus-newsgroup-headers
8133            (setq gnus-newsgroup-begin 
8134                  (mail-header-number (car gnus-newsgroup-headers)))
8135            (setq gnus-newsgroup-end
8136                  (mail-header-number
8137                   (gnus-last-element gnus-newsgroup-headers))))
8138       (setq gnus-reffed-article-number -1)
8139       ;; GROUP is successfully selected.
8140       (or gnus-newsgroup-headers t)))))
8141
8142 (defun gnus-articles-to-read (group read-all)
8143   ;; Find out what articles the user wants to read.
8144   (let* ((articles
8145           ;; Select all articles if `read-all' is non-nil, or if there
8146           ;; are no unread articles.
8147           (if (or read-all
8148                   (and (zerop (length gnus-newsgroup-marked))
8149                        (zerop (length gnus-newsgroup-unreads))))
8150               (gnus-uncompress-range (gnus-active group))
8151             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked 
8152                           (copy-sequence gnus-newsgroup-unreads))
8153                   '<)))
8154          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8155          (scored (length scored-list))
8156          (number (length articles))
8157          (marked (+ (length gnus-newsgroup-marked)
8158                     (length gnus-newsgroup-dormant)))
8159          (select
8160           (cond 
8161            ((numberp read-all)
8162             read-all)
8163            (t
8164             (condition-case ()
8165                 (cond 
8166                  ((and (or (<= scored marked) (= scored number))
8167                        (numberp gnus-large-newsgroup)
8168                        (> number gnus-large-newsgroup))
8169                   (let ((input
8170                          (read-string
8171                           (format
8172                            "How many articles from %s (default %d): "
8173                            gnus-newsgroup-name number))))
8174                     (if (string-match "^[ \t]*$" input) number input)))
8175                  ((and (> scored marked) (< scored number))
8176                   (let ((input
8177                          (read-string
8178                           (format "%s %s (%d scored, %d total): "
8179                                   "How many articles from"
8180                                   group scored number))))
8181                     (if (string-match "^[ \t]*$" input)
8182                         number input)))
8183                  (t number))
8184               (quit nil))))))
8185     (setq select (if (stringp select) (string-to-number select) select))
8186     (if (or (null select) (zerop select))
8187         select
8188       (if (and (not (zerop scored)) (<= (abs select) scored))
8189           (progn
8190             (setq articles (sort scored-list '<))
8191             (setq number (length articles)))
8192         (setq articles (copy-sequence articles)))
8193
8194       (if (< (abs select) number)
8195           (if (< select 0) 
8196               ;; Select the N oldest articles.
8197               (setcdr (nthcdr (1- (abs select)) articles) nil)
8198             ;; Select the N most recent articles.
8199             (setq articles (nthcdr (- number select) articles))))
8200       (setq gnus-newsgroup-unselected
8201             (gnus-sorted-intersection
8202              gnus-newsgroup-unreads
8203              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8204       articles)))
8205
8206 (defun gnus-killed-articles (killed articles)
8207   (let (out)
8208     (while articles
8209       (if (inline (gnus-member-of-range (car articles) killed))
8210           (setq out (cons (car articles) out)))
8211       (setq articles (cdr articles)))
8212     out))
8213
8214 (defun gnus-adjust-marked-articles (info)
8215   "Set all article lists and remove all marks that are no longer legal."
8216   (let* ((marked-lists (gnus-info-marks info))
8217          (active (gnus-active (gnus-info-group info)))
8218          (min (car active))
8219          (max (cdr active))
8220          (types gnus-article-mark-lists)
8221          (uncompressed '(score bookmark))
8222          marks var articles article mark)
8223
8224     (while marked-lists
8225       (setq marks (pop marked-lists))
8226       (set (setq var (intern (format "gnus-newsgroup-%s" 
8227                                      (car (rassq (setq mark (car marks)) 
8228                                                  types)))))
8229            (if (memq (car marks) uncompressed) (cdr marks)
8230              (gnus-uncompress-range (cdr marks))))
8231
8232       (setq articles (symbol-value var))
8233
8234       ;; All articles have to be subsets of the active articles.  
8235       (cond 
8236        ;; Adjust "simple" lists.
8237        ((memq mark '(tick dormant expirable reply killed save))
8238         (while articles
8239           (when (or (< (setq article (pop articles)) min) (> article max))
8240             (set var (delq article (symbol-value var))))))
8241        ;; Adjust assocs.
8242        ((memq mark '(score bookmark))
8243         (while articles 
8244           (when (or (< (car (setq article (pop articles))) min) 
8245                     (> (car article) max))
8246             (set var (delq article (symbol-value var))))))))))
8247
8248 (defun gnus-update-missing-marks (missing)
8249   "Go through the list of MISSING articles and remove them mark lists."
8250   (when missing
8251     (let ((types gnus-article-mark-lists)
8252           var m)
8253       ;; Go through all types.
8254       (while types
8255         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8256         (when (symbol-value var)
8257           ;; This list has articles.  So we delete all missing articles 
8258           ;; from it.
8259           (setq m missing)
8260           (while m
8261             (set var (delq (pop m) (symbol-value var)))))))))
8262
8263 (defun gnus-update-marks ()
8264   "Enter the various lists of marked articles into the newsgroup info list."
8265   (let ((types gnus-article-mark-lists)
8266         (info (gnus-get-info gnus-newsgroup-name))
8267         (uncompressed '(score bookmark killed))
8268         var type list newmarked symbol)
8269     ;; Add all marks lists that are non-nil to the list of marks lists. 
8270     (while types
8271       (setq type (pop types))
8272       (when (setq list (symbol-value 
8273                         (setq symbol
8274                               (intern (format "gnus-newsgroup-%s" 
8275                                               (car type))))))
8276         (setq list (set symbol (sort list '<)))
8277         (push (cons (cdr type) 
8278                     (if (memq (cdr type) uncompressed) list
8279                       (gnus-compress-sequence list t)))
8280               newmarked)))
8281
8282     ;; Enter these new marks into the info of the group.
8283     (if (nthcdr 3 info)
8284         (setcar (nthcdr 3 info) newmarked)
8285       ;; Add the marks lists to the end of the info.
8286       (when newmarked
8287         (setcdr (nthcdr 2 info) (list newmarked))))
8288
8289     ;; Cut off the end of the info if there's nothing else there. 
8290     (let ((i 5))
8291       (while (and (> i 2)
8292                   (not (nth i info)))
8293         (when (nthcdr (decf i) info)
8294           (setcdr (nthcdr i info) nil))))))
8295
8296 (defun gnus-add-marked-articles (group type articles &optional info force)
8297   ;; Add ARTICLES of TYPE to the info of GROUP.
8298   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8299   ;; add, but replace marked articles of TYPE with ARTICLES.
8300   (let ((info (or info (gnus-get-info group)))
8301         (uncompressed '(score bookmark killed))
8302         marked m)
8303     (or (not info)
8304         (and (not (setq marked (nthcdr 3 info)))
8305              (setcdr (nthcdr 2 info)
8306                      (list (list (cons type (gnus-compress-sequence
8307                                              articles t))))))
8308         (and (not (setq m (assq type (car marked))))
8309              (setcar marked 
8310                      (cons (cons type (gnus-compress-sequence articles t) )
8311                            (car marked))))
8312         (if force
8313             (setcdr m (gnus-compress-sequence articles t))
8314           (setcdr m (gnus-compress-sequence
8315                      (sort (nconc (gnus-uncompress-range m) 
8316                                   (copy-sequence articles)) '<) t))))))
8317          
8318 (defun gnus-set-mode-line (where)
8319   "This function sets the mode line of the article or summary buffers.
8320 If WHERE is `summary', the summary mode line format will be used."
8321   ;; Is this mode line one we keep updated?
8322   (when (memq where gnus-updated-mode-lines)
8323     (let (mode-string)
8324       (save-excursion
8325         ;; We evaluate this in the summary buffer since these
8326         ;; variables are buffer-local to that buffer.
8327         (set-buffer gnus-summary-buffer)
8328         ;; We bind all these variables that are used in the `eval' form
8329         ;; below. 
8330         (let* ((mformat (if (eq where 'article) 
8331                             gnus-article-mode-line-format-spec
8332                           gnus-summary-mode-line-format-spec))
8333                (gnus-tmp-group-name gnus-newsgroup-name)
8334                (gnus-tmp-article-number (or gnus-current-article 0))
8335                (gnus-tmp-unread gnus-newsgroup-unreads)
8336                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8337                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8338                (gnus-tmp-unread-and-unselected
8339                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8340                             (zerop gnus-tmp-unselected)) "")
8341                       ((zerop gnus-tmp-unselected) 
8342                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8343                       (t (format "{%d(+%d) more}"
8344                                  gnus-tmp-unread-and-unticked
8345                                  gnus-tmp-unselected))))
8346                (gnus-tmp-subject
8347                 (if (and gnus-current-headers
8348                          (vectorp gnus-current-headers))
8349                     (mail-header-subject gnus-current-headers) ""))
8350                max-len 
8351                header);; passed as argument to any user-format-funcs
8352           (setq mode-string (eval mformat))
8353           (setq max-len (max 4 (if gnus-mode-non-string-length
8354                                    (- (frame-width) 
8355                                       gnus-mode-non-string-length)
8356                                  (length mode-string))))
8357           ;; We might have to chop a bit of the string off...
8358           (when (> (length mode-string) max-len)
8359             (setq mode-string 
8360                   (concat (gnus-truncate-string mode-string (- max-len 3))
8361                           "...")))
8362           ;; Pad the mode string a bit.
8363           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8364       ;; Update the mode line.
8365       (setq mode-line-buffer-identification (list mode-string))
8366       (set-buffer-modified-p t))))
8367
8368 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8369   "Go through the HEADERS list and add all Xrefs to a hash table.
8370 The resulting hash table is returned, or nil if no Xrefs were found."
8371   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
8372          (virtual (memq 'virtual 
8373                         (assoc (symbol-name (car (gnus-find-method-for-group 
8374                                                   from-newsgroup)))
8375                                gnus-valid-select-methods)))     
8376          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8377          (xref-hashtb (make-vector 63 0))
8378          start group entry number xrefs header)
8379     (while headers
8380       (setq header (pop headers))
8381       (when (and (setq xrefs (mail-header-xref header))
8382                  (not (memq (setq number (mail-header-number header))
8383                             unreads)))
8384         (setq start 0)
8385         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8386           (setq start (match-end 0))
8387           (setq group (concat prefix (substring xrefs (match-beginning 1) 
8388                                                 (match-end 1))))
8389           (setq number 
8390                 (string-to-int (substring xrefs (match-beginning 2) 
8391                                           (match-end 2))))
8392           (if (setq entry (gnus-gethash group xref-hashtb))
8393               (setcdr entry (cons number (cdr entry)))
8394             (gnus-sethash group (cons number nil) xref-hashtb)))))
8395     (and start xref-hashtb)))
8396
8397 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8398   "Look through all the headers and mark the Xrefs as read."
8399   (let ((virtual (memq 'virtual 
8400                        (assoc (symbol-name (car (gnus-find-method-for-group 
8401                                                  from-newsgroup)))
8402                               gnus-valid-select-methods)))
8403         name entry info xref-hashtb idlist method
8404         nth4)
8405     (save-excursion
8406       (set-buffer gnus-group-buffer)
8407       (when (setq xref-hashtb 
8408                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8409         (mapatoms 
8410          (lambda (group)
8411            (unless (string= from-newsgroup (setq name (symbol-name group)))
8412              (setq idlist (symbol-value group))
8413              ;; Dead groups are not updated.
8414              (and (prog1 
8415                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8416                             info (nth 2 entry))
8417                     (if (stringp (setq nth4 (gnus-info-method info)))
8418                         (setq nth4 (gnus-server-to-method nth4))))
8419                   ;; Only do the xrefs if the group has the same
8420                   ;; select method as the group we have just read.
8421                   (or (gnus-methods-equal-p 
8422                        nth4 (gnus-find-method-for-group from-newsgroup))
8423                       virtual
8424                       (equal nth4 (setq method (gnus-find-method-for-group 
8425                                                 from-newsgroup)))
8426                       (and (equal (car nth4) (car method))
8427                            (equal (nth 1 nth4) (nth 1 method))))
8428                   gnus-use-cross-reference
8429                   (or (not (eq gnus-use-cross-reference t))
8430                       virtual
8431                       ;; Only do cross-references on subscribed
8432                       ;; groups, if that is what is wanted.  
8433                       (<= (gnus-info-level info) gnus-level-subscribed))
8434                   (gnus-group-make-articles-read name idlist))))
8435          xref-hashtb)))))
8436
8437 (defun gnus-group-make-articles-read (group articles)
8438   (let* ((num 0)
8439          (entry (gnus-gethash group gnus-newsrc-hashtb))
8440          (info (nth 2 entry))
8441          (active (gnus-active group))
8442          range)
8443     ;; First peel off all illegal article numbers.
8444     (if active
8445         (let ((ids articles)
8446               id first)
8447           (while ids
8448             (setq id (car ids))
8449             (if (and first (> id (cdr active)))
8450                 (progn
8451                   ;; We'll end up in this situation in one particular
8452                   ;; obscure situation.  If you re-scan a group and get
8453                   ;; a new article that is cross-posted to a different
8454                   ;; group that has not been re-scanned, you might get
8455                   ;; crossposted article that has a higher number than
8456                   ;; Gnus believes possible.  So we re-activate this
8457                   ;; group as well.  This might mean doing the
8458                   ;; crossposting thingie will *increase* the number
8459                   ;; of articles in some groups.  Tsk, tsk.
8460                   (setq active (or (gnus-activate-group group) active))))
8461             (if (or (> id (cdr active))
8462                     (< id (car active)))
8463                 (setq articles (delq id articles)))
8464             (setq ids (cdr ids)))))
8465     ;; If the read list is nil, we init it.
8466     (and active
8467          (null (gnus-info-read info))
8468          (> (car active) 1)
8469          (gnus-info-set-read info (cons 1 (1- (car active)))))
8470     ;; Then we add the read articles to the range.
8471     (gnus-info-set-read
8472      info
8473      (setq range
8474            (gnus-add-to-range 
8475             (gnus-info-read info) (setq articles (sort articles '<)))))
8476     ;; Then we have to re-compute how many unread
8477     ;; articles there are in this group.
8478     (if active
8479         (progn
8480           (cond 
8481            ((not range)
8482             (setq num (- (1+ (cdr active)) (car active))))
8483            ((not (listp (cdr range)))
8484             (setq num (- (cdr active) (- (1+ (cdr range)) 
8485                                          (car range)))))
8486            (t
8487             (while range
8488               (if (numberp (car range))
8489                   (setq num (1+ num))
8490                 (setq num (+ num (- (1+ (cdr (car range)))
8491                                     (car (car range))))))
8492               (setq range (cdr range)))
8493             (setq num (- (cdr active) num))))
8494           ;; Update the number of unread articles.
8495           (setcar entry num)
8496           ;; Update the group buffer.
8497           (gnus-group-update-group group t)))))
8498
8499 (defun gnus-methods-equal-p (m1 m2)
8500   (let ((m1 (or m1 gnus-select-method))
8501         (m2 (or m2 gnus-select-method)))
8502     (or (equal m1 m2)
8503         (and (eq (car m1) (car m2))
8504              (or (not (memq 'address (assoc (symbol-name (car m1))
8505                                             gnus-valid-select-methods)))
8506                  (equal (nth 1 m1) (nth 1 m2)))))))
8507
8508 (defsubst gnus-header-value ()
8509   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8510
8511 (defvar gnus-newsgroup-none-id 0)
8512
8513 (defun gnus-get-newsgroup-headers (&optional dependencies)
8514   (let ((cur nntp-server-buffer)
8515         (dependencies 
8516          (or dependencies
8517              (save-excursion (set-buffer gnus-summary-buffer)
8518                              gnus-newsgroup-dependencies)))
8519         headers id id-dep ref-dep end ref)
8520     (save-excursion
8521       (set-buffer nntp-server-buffer)
8522       (let ((case-fold-search t)
8523             in-reply-to header number p lines)
8524         (goto-char (point-min))
8525         ;; Search to the beginning of the next header.  Error messages
8526         ;; do not begin with 2 or 3.
8527         (while (re-search-forward "^[23][0-9]+ " nil t)
8528           (setq id nil
8529                 ref nil)
8530           ;; This implementation of this function, with nine
8531           ;; search-forwards instead of the one re-search-forward and
8532           ;; a case (which basically was the old function) is actually
8533           ;; about twice as fast, even though it looks messier.  You
8534           ;; can't have everything, I guess.  Speed and elegance
8535           ;; doesn't always go hand in hand.
8536           (setq 
8537            header
8538            (vector
8539             ;; Number.
8540             (prog1
8541                 (read cur)
8542               (end-of-line)
8543               (setq p (point))
8544               (narrow-to-region (point) 
8545                                 (or (and (search-forward "\n.\n" nil t)
8546                                          (- (point) 2))
8547                                     (point))))
8548             ;; Subject.
8549             (progn
8550               (goto-char p)
8551               (if (search-forward "\nsubject: " nil t)
8552                   (gnus-header-value) "(none)"))
8553             ;; From.
8554             (progn
8555               (goto-char p)
8556               (if (search-forward "\nfrom: " nil t)
8557                   (gnus-header-value) "(nobody)"))
8558             ;; Date.
8559             (progn
8560               (goto-char p)
8561               (if (search-forward "\ndate: " nil t)
8562                   (gnus-header-value) ""))
8563             ;; Message-ID.
8564             (progn
8565               (goto-char p)
8566               (if (search-forward "\nmessage-id: " nil t)
8567                   (setq id (gnus-header-value))
8568                 ;; If there was no message-id, we just fake one to make
8569                 ;; subsequent routines simpler.
8570                 (setq id (concat "none+" 
8571                                  (int-to-string 
8572                                   (setq gnus-newsgroup-none-id 
8573                                         (1+ gnus-newsgroup-none-id)))))))
8574             ;; References.
8575             (progn
8576               (goto-char p)
8577               (if (search-forward "\nreferences: " nil t)
8578                   (prog1
8579                       (gnus-header-value)
8580                     (setq end (match-end 0))
8581                     (save-excursion
8582                       (setq ref 
8583                             (downcase
8584                              (buffer-substring
8585                               (progn 
8586                                 (end-of-line)
8587                                 (search-backward ">" end t)
8588                                 (1+ (point)))
8589                               (progn
8590                                 (search-backward "<" end t)
8591                                 (point)))))))
8592                 ;; Get the references from the in-reply-to header if there
8593                 ;; were no references and the in-reply-to header looks
8594                 ;; promising. 
8595                 (if (and (search-forward "\nin-reply-to: " nil t)
8596                          (setq in-reply-to (gnus-header-value))
8597                          (string-match "<[^>]+>" in-reply-to))
8598                     (prog1
8599                         (setq ref (substring in-reply-to (match-beginning 0)
8600                                              (match-end 0)))
8601                       (setq ref (downcase ref))))
8602                 (setq ref "")))
8603             ;; Chars.
8604             0
8605             ;; Lines.
8606             (progn
8607               (goto-char p)
8608               (if (search-forward "\nlines: " nil t)
8609                   (if (numberp (setq lines (read cur)))
8610                       lines 0)
8611                 0))
8612             ;; Xref.
8613             (progn
8614               (goto-char p)
8615               (and (search-forward "\nxref: " nil t)
8616                    (gnus-header-value)))))
8617           (if (and gnus-nocem-hashtb
8618                    (gnus-gethash id gnus-nocem-hashtb))
8619               ;; Banned article.
8620               (setq header nil)
8621             ;; We do the threading while we read the headers.  The
8622             ;; message-id and the last reference are both entered into
8623             ;; the same hash table.  Some tippy-toeing around has to be
8624             ;; done in case an article has arrived before the article
8625             ;; which it refers to.
8626             (if (boundp (setq id-dep (intern (downcase id) dependencies)))
8627                 (if (car (symbol-value id-dep))
8628                     ;; An article with this Message-ID has already
8629                     ;; been seen, so we ignore this one, except we add
8630                     ;; any additional Xrefs (in case the two articles
8631                     ;; came from different servers).
8632                     (progn
8633                       (mail-header-set-xref 
8634                        (car (symbol-value id-dep))
8635                        (concat (or (mail-header-xref 
8636                                     (car (symbol-value id-dep))) "")
8637                                (or (mail-header-xref header) "")))
8638                       (setq header nil))
8639                   (setcar (symbol-value id-dep) header))
8640               (set id-dep (list header))))
8641           (if header
8642               (progn
8643                 (if (boundp (setq ref-dep (intern ref dependencies)))
8644                     (setcdr (symbol-value ref-dep) 
8645                             (nconc (cdr (symbol-value ref-dep))
8646                                    (list (symbol-value id-dep))))
8647                   (set ref-dep (list nil (symbol-value id-dep))))
8648                 (setq headers (cons header headers))))
8649           (goto-char (point-max))
8650           (widen))
8651         (nreverse headers)))))
8652
8653 ;; The following macros and functions were written by Felix Lee
8654 ;; <flee@cse.psu.edu>. 
8655
8656 (defmacro gnus-nov-read-integer ()
8657   '(prog1
8658        (if (= (following-char) ?\t)
8659            0
8660          (let ((num (condition-case nil (read buffer) (error nil))))
8661            (if (numberp num) num 0)))
8662      (or (eobp) (forward-char 1))))
8663
8664 (defmacro gnus-nov-skip-field ()
8665   '(search-forward "\t" eol 'move))
8666
8667 (defmacro gnus-nov-field ()
8668   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
8669
8670 ;; Goes through the xover lines and returns a list of vectors
8671 (defun gnus-get-newsgroup-headers-xover (sequence)
8672   "Parse the news overview data in the server buffer, and return a
8673 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
8674   ;; Get the Xref when the users reads the articles since most/some
8675   ;; NNTP servers do not include Xrefs when using XOVER.
8676   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
8677   (let ((cur nntp-server-buffer)
8678         (dependencies gnus-newsgroup-dependencies)
8679         number headers header)
8680     (save-excursion
8681       (set-buffer nntp-server-buffer)
8682       ;; Allow the user to mangle the headers before parsing them.
8683       (run-hooks 'gnus-parse-headers-hook)
8684       ;; Allow the user to mangle the headers before parsing them.
8685       (run-hooks 'gnus-parse-headers-hook)
8686       (goto-char (point-min))
8687       (while (and sequence (not (eobp)))
8688         (setq number (read cur))
8689         (while (and sequence (< (car sequence) number))
8690           (setq sequence (cdr sequence)))
8691         (and sequence 
8692              (eq number (car sequence))
8693              (progn
8694                (setq sequence (cdr sequence))
8695                (if (setq header 
8696                          (inline (gnus-nov-parse-line number dependencies)))
8697                    (setq headers (cons header headers)))))
8698         (forward-line 1))
8699       (setq headers (nreverse headers)))
8700     headers))
8701
8702 ;; This function has to be called with point after the article number
8703 ;; on the beginning of the line.
8704 (defun gnus-nov-parse-line (number dependencies)
8705   (let ((none 0)
8706         (eol (gnus-point-at-eol)) 
8707         (buffer (current-buffer))
8708         header ref id id-dep ref-dep)
8709
8710     ;; overview: [num subject from date id refs chars lines misc]
8711     (narrow-to-region (point) eol)
8712     (or (eobp) (forward-char))
8713
8714     (condition-case nil
8715         (setq header
8716               (vector 
8717                number                   ; number
8718                (gnus-nov-field)         ; subject
8719                (gnus-nov-field)         ; from
8720                (gnus-nov-field)         ; date
8721                (setq id (or (gnus-nov-field)
8722                             (concat "none+"
8723                                     (int-to-string 
8724                                      (setq none (1+ none)))))) ; id
8725                (progn
8726                  (save-excursion
8727                    (let ((beg (point)))
8728                      (search-forward "\t" eol)
8729                      (if (search-backward ">" beg t)
8730                          (setq ref 
8731                                (downcase 
8732                                 (buffer-substring 
8733                                  (1+ (point))
8734                                  (progn
8735                                    (search-backward "<" beg t)
8736                                    (point)))))
8737                        (setq ref nil))))
8738                  (gnus-nov-field))      ; refs
8739                (gnus-nov-read-integer)  ; chars
8740                (gnus-nov-read-integer)  ; lines
8741                (if (= (following-char) ?\n)
8742                    nil
8743                  (gnus-nov-field))      ; misc
8744                ))
8745       (error (progn 
8746                (ding)
8747                (message "Strange nov line.")
8748                (setq header nil)
8749                (goto-char eol))))
8750
8751     (widen)
8752
8753     ;; We build the thread tree.
8754     (and header
8755          (if (and gnus-nocem-hashtb
8756                   (gnus-gethash id gnus-nocem-hashtb))
8757              ;; Banned article.
8758              (setq header nil)
8759            (if (boundp (setq id-dep (intern (downcase id) dependencies)))
8760                (if (car (symbol-value id-dep))
8761                    ;; An article with this Message-ID has already been seen,
8762                    ;; so we ignore this one, except we add any additional
8763                    ;; Xrefs (in case the two articles came from different
8764                    ;; servers.
8765                    (progn
8766                      (mail-header-set-xref 
8767                       (car (symbol-value id-dep))
8768                       (concat (or (mail-header-xref 
8769                                    (car (symbol-value id-dep))) "")
8770                               (or (mail-header-xref header) "")))
8771                      (setq header nil))
8772                  (setcar (symbol-value id-dep) header))
8773              (set id-dep (list header)))))
8774     (if header
8775         (progn
8776           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
8777               (setcdr (symbol-value ref-dep) 
8778                       (nconc (cdr (symbol-value ref-dep))
8779                              (list (symbol-value id-dep))))
8780             (set ref-dep (list nil (symbol-value id-dep))))))
8781     header))
8782
8783 (defun gnus-article-get-xrefs ()
8784   "Fill in the Xref value in `gnus-current-headers', if necessary.
8785 This is meant to be called in `gnus-article-internal-prepare-hook'."
8786   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
8787                                  gnus-current-headers)))
8788     (or (not gnus-use-cross-reference)
8789         (not headers)
8790         (and (mail-header-xref headers)
8791              (not (string= (mail-header-xref headers) "")))
8792         (let ((case-fold-search t)
8793               xref)
8794           (save-restriction
8795             (gnus-narrow-to-headers)
8796             (goto-char (point-min))
8797             (if (or (and (eq (downcase (following-char)) ?x)
8798                          (looking-at "Xref:"))
8799                     (search-forward "\nXref:" nil t))
8800                 (progn
8801                   (goto-char (1+ (match-end 0)))
8802                   (setq xref (buffer-substring (point) 
8803                                                (progn (end-of-line) (point))))
8804                   (mail-header-set-xref headers xref))))))))
8805
8806 (defun gnus-summary-insert-subject (id)
8807   "Find article ID and insert the summary line for that article."
8808   (let ((header (gnus-read-header id))
8809         number)
8810     (when header
8811       ;; Rebuild the thread that this article is part of and go to the
8812       ;; article we have fetched.
8813       (gnus-rebuild-thread (mail-header-id header))
8814       (gnus-summary-goto-subject (setq number (mail-header-number header)))
8815       (when (> number 0)
8816         ;; We have to update the boundaries, possibly.
8817         (and (> number gnus-newsgroup-end)
8818              (setq gnus-newsgroup-end number))
8819         (and (< number gnus-newsgroup-begin)
8820              (setq gnus-newsgroup-begin number))
8821         (setq gnus-newsgroup-unselected
8822               (delq number gnus-newsgroup-unselected)))
8823       ;; Report back a success.
8824       number)))
8825
8826 (defun gnus-summary-work-articles (n)
8827   "Return a list of articles to be worked upon.  The prefix argument,
8828 the list of process marked articles, and the current article will be
8829 taken into consideration."
8830   (cond
8831    ((and n (numberp n))
8832     ;; A numerical prefix has been given.
8833     (let ((backward (< n 0))
8834           (n (abs n))
8835           articles article)
8836       (save-excursion
8837         (while 
8838             (and (> n 0)
8839                  (push (setq article (gnus-summary-article-number))
8840                        articles)
8841                  (if backward
8842                      (gnus-summary-find-prev nil article)
8843                    (gnus-summary-find-next nil article)))
8844           (decf n)))
8845       (nreverse articles)))
8846    ((and (boundp 'transient-mark-mode)
8847          transient-mark-mode
8848          mark-active)
8849     ;; Work on the region between point and mark.
8850     (let ((max (max (point) (mark)))
8851           articles article)
8852       (save-excursion
8853         (goto-char (min (point) (mark)))
8854         (while 
8855             (and 
8856              (push (setq article (gnus-summary-article-number)) articles)
8857              (gnus-summary-find-next nil article)
8858              (< (point) max)))
8859         (nreverse articles))))
8860    (gnus-newsgroup-processable
8861     ;; There are process-marked articles present.
8862     (reverse gnus-newsgroup-processable))
8863    (t
8864     ;; Just return the current article.
8865     (list (gnus-summary-article-number)))))
8866
8867 (defun gnus-summary-search-group (&optional backward use-level)
8868   "Search for next unread newsgroup.
8869 If optional argument BACKWARD is non-nil, search backward instead."
8870   (save-excursion
8871     (set-buffer gnus-group-buffer)
8872     (if (gnus-group-search-forward 
8873          backward nil (if use-level (gnus-group-group-level) nil))
8874         (gnus-group-group-name))))
8875
8876 (defun gnus-summary-best-group (&optional exclude-group)
8877   "Find the name of the best unread group.
8878 If EXCLUDE-GROUP, do not go to this group."
8879   (save-excursion
8880     (set-buffer gnus-group-buffer)
8881     (save-excursion
8882       (gnus-group-best-unread-group exclude-group))))
8883
8884 (defun gnus-summary-find-next (&optional unread article backward)
8885   (if backward (gnus-summary-find-prev)
8886     (let* ((article (or article (gnus-summary-article-number)))
8887            (arts (gnus-data-find-list article))
8888            result)
8889       (when (or (not gnus-summary-check-current)
8890                 (not unread)
8891                 (not (gnus-data-unread-p (car arts))))
8892         (setq arts (cdr arts)))
8893       (when (setq result
8894                   (if unread
8895                       (progn
8896                         (while arts
8897                           (when (gnus-data-unread-p (car arts))
8898                             (setq result (car arts)
8899                                   arts nil))
8900                           (setq arts (cdr arts)))
8901                         result)
8902                     (car arts)))
8903         (goto-char (gnus-data-pos result))
8904         (gnus-data-number result)))))
8905
8906 (defun gnus-summary-find-prev (&optional unread article)
8907   (let* ((article (or article (gnus-summary-article-number)))
8908          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
8909          result)
8910     (when (or (not gnus-summary-check-current)
8911               (not unread)
8912               (not (gnus-data-unread-p (car arts))))
8913       (setq arts (cdr arts)))
8914     (if (setq result
8915               (if unread
8916                   (progn
8917                     (while arts
8918                       (and (gnus-data-unread-p (car arts))
8919                            (setq result (car arts)
8920                                  arts nil))
8921                       (setq arts (cdr arts)))
8922                     result)
8923                 (car arts)))
8924         (progn
8925           (goto-char (gnus-data-pos result))
8926           (gnus-data-number result)))))
8927
8928 (defun gnus-summary-find-subject (subject &optional unread backward article)
8929   (let* ((simp-subject (gnus-simplify-subject-fully subject))
8930          (article (or article (gnus-summary-article-number)))
8931          (articles (gnus-data-list backward))
8932          (arts (gnus-data-find-list article articles))
8933          result)
8934     (when (or (not gnus-summary-check-current)
8935               (not unread)
8936               (not (gnus-data-unread-p (car arts))))
8937       (setq arts (cdr arts)))
8938     (while arts
8939       (and (or (not unread)
8940                (gnus-data-unread-p (car arts)))
8941            (vectorp (gnus-data-header (car arts)))
8942            (gnus-subject-equal 
8943             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
8944            (setq result (car arts)
8945                  arts nil))
8946       (setq arts (cdr arts)))
8947     (and result
8948          (goto-char (gnus-data-pos result))
8949          (gnus-data-number result))))
8950
8951 (defun gnus-summary-search-forward (&optional unread subject backward)
8952   (cond (subject
8953          (gnus-summary-find-subject subject unread backward))
8954         (backward
8955          (gnus-summary-find-prev unread))
8956         (t
8957          (gnus-summary-find-next unread))))
8958
8959 (defun gnus-summary-recenter ()
8960   "Center point in the summary window.
8961 If `gnus-auto-center-summary' is nil, or the article buffer isn't
8962 displayed, no centering will be performed." 
8963   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
8964   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
8965   (let* ((top (cond ((< (window-height) 4) 0)
8966                     ((< (window-height) 7) 1)
8967                     (t 2)))
8968          (height (1- (window-height)))
8969          (bottom (save-excursion (goto-char (point-max))
8970                                  (forward-line (- height))
8971                                  (point)))
8972          (window (get-buffer-window (current-buffer))))
8973     (and 
8974      ;; The user has to want it,
8975      gnus-auto-center-summary 
8976      ;; the article buffer must be displayed,
8977      (get-buffer-window gnus-article-buffer)
8978      ;; Set the window start to either `bottom', which is the biggest
8979      ;; possible valid number, or the second line from the top,
8980      ;; whichever is the least.
8981      (set-window-start
8982       window (min bottom (save-excursion (forward-line (- top)) (point)))))))
8983
8984 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
8985 (defun gnus-short-group-name (group &optional levels)
8986   "Collapse GROUP name LEVELS."
8987   (let* ((name "") (foreign "") (depth -1) (skip 1)
8988          (levels (or levels
8989                      (progn
8990                        (while (string-match "\\." group skip)
8991                          (setq skip (match-end 0)
8992                                depth (+ depth 1)))
8993                        depth))))
8994     (if (string-match ":" group)
8995         (setq foreign (substring group 0 (match-end 0))
8996               group (substring group (match-end 0))))
8997     (while group
8998       (if (and (string-match "\\." group) (> levels 0))
8999           (setq name (concat name (substring group 0 1))
9000                 group (substring group (match-end 0))
9001                 levels (- levels 1)
9002                 name (concat name "."))
9003         (setq name (concat foreign name group)
9004               group nil)))
9005     name))
9006
9007 (defun gnus-summary-jump-to-group (newsgroup)
9008   "Move point to NEWSGROUP in group mode buffer."
9009   ;; Keep update point of group mode buffer if visible.
9010   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9011       (save-window-excursion
9012         ;; Take care of tree window mode.
9013         (if (get-buffer-window gnus-group-buffer)
9014             (pop-to-buffer gnus-group-buffer))
9015         (gnus-group-jump-to-group newsgroup))
9016     (save-excursion
9017       ;; Take care of tree window mode.
9018       (if (get-buffer-window gnus-group-buffer)
9019           (pop-to-buffer gnus-group-buffer)
9020         (set-buffer gnus-group-buffer))
9021       (gnus-group-jump-to-group newsgroup))))
9022
9023 ;; This function returns a list of article numbers based on the
9024 ;; difference between the ranges of read articles in this group and
9025 ;; the range of active articles.
9026 (defun gnus-list-of-unread-articles (group)
9027   (let* ((read (gnus-info-read (gnus-get-info group)))
9028          (active (gnus-active group))
9029          (last (cdr active))
9030          first nlast unread)
9031     ;; If none are read, then all are unread. 
9032     (if (not read)
9033         (setq first (car active))
9034       ;; If the range of read articles is a single range, then the
9035       ;; first unread article is the article after the last read
9036       ;; article.  Sounds logical, doesn't it?
9037       (if (not (listp (cdr read)))
9038           (setq first (1+ (cdr read)))
9039         ;; `read' is a list of ranges.
9040         (if (/= (setq nlast (or (and (numberp (car read)) (car read)) 
9041                                 (car (car read)))) 1)
9042             (setq first 1))
9043         (while read
9044           (if first 
9045               (while (< first nlast)
9046                 (setq unread (cons first unread))
9047                 (setq first (1+ first))))
9048           (setq first (1+ (if (atom (car read)) (car read) (cdr (car read)))))
9049           (setq nlast (if (atom (car (cdr read))) 
9050                           (car (cdr read))
9051                         (car (car (cdr read)))))
9052           (setq read (cdr read)))))
9053     ;; And add the last unread articles.
9054     (while (<= first last)
9055       (setq unread (cons first unread))
9056       (setq first (1+ first)))
9057     ;; Return the list of unread articles.
9058     (nreverse unread)))
9059
9060 (defun gnus-list-of-read-articles (group)
9061   "Return a list of unread, unticked and non-dormant articles."
9062   (let* ((info (gnus-get-info group))
9063          (marked (gnus-info-marks info))
9064          (active (gnus-active group)))
9065     (and info active
9066          (gnus-set-difference
9067           (gnus-sorted-complement 
9068            (gnus-uncompress-range active) 
9069            (gnus-list-of-unread-articles group))
9070           (append 
9071            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9072            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9073
9074 ;; Various summary commands
9075
9076 (defun gnus-summary-universal-argument ()
9077   "Perform any operation on all articles marked with the process mark."
9078   (interactive)
9079   (gnus-set-global-variables)
9080   (let ((articles (reverse gnus-newsgroup-processable))
9081         func)
9082     (or articles (error "No articles marked"))
9083     (or (setq func (key-binding (read-key-sequence "C-c C-u")))
9084         (error "Undefined key"))
9085     (while articles
9086       (gnus-summary-goto-subject (car articles))
9087       (command-execute func)
9088       (gnus-summary-remove-process-mark (car articles))
9089       (setq articles (cdr articles)))))
9090
9091 (defun gnus-summary-toggle-truncation (&optional arg)
9092   "Toggle truncation of summary lines.
9093 With arg, turn line truncation on iff arg is positive."
9094   (interactive "P")
9095   (setq truncate-lines
9096         (if (null arg) (not truncate-lines)
9097           (> (prefix-numeric-value arg) 0)))
9098   (redraw-display))
9099
9100 (defun gnus-summary-reselect-current-group (&optional all)
9101   "Once exit and then reselect the current newsgroup.
9102 The prefix argument ALL means to select all articles."
9103   (interactive "P")
9104   (gnus-set-global-variables)
9105   (let ((current-subject (gnus-summary-article-number))
9106         (group gnus-newsgroup-name))
9107     (setq gnus-newsgroup-begin nil)
9108     (gnus-summary-exit)
9109     ;; We have to adjust the point of group mode buffer because the
9110     ;; current point was moved to the next unread newsgroup by
9111     ;; exiting.
9112     (gnus-summary-jump-to-group group)
9113     (gnus-group-read-group all t)
9114     (gnus-summary-goto-subject current-subject)))
9115
9116 (defun gnus-summary-rescan-group (&optional all)
9117   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9118   (interactive "P")
9119   (gnus-set-global-variables)
9120   ;; Fix by Ilja Weis <kult@uni-paderborn.de>.
9121   (let ((group gnus-newsgroup-name))
9122     (gnus-summary-exit)
9123     (gnus-summary-jump-to-group group)
9124     (save-excursion
9125       (set-buffer gnus-group-buffer)
9126       (gnus-group-get-new-news-this-group 1))
9127     (gnus-summary-jump-to-group group)
9128     (gnus-group-read-group all)))
9129
9130 (defun gnus-summary-update-info ()
9131   (let* ((group gnus-newsgroup-name))
9132     (when gnus-newsgroup-kill-headers
9133       (setq gnus-newsgroup-killed
9134             (gnus-compress-sequence
9135              (nconc
9136               (gnus-set-sorted-intersection
9137                (gnus-uncompress-range gnus-newsgroup-killed)
9138                (setq gnus-newsgroup-unselected
9139                      (sort gnus-newsgroup-unselected '<)))
9140               (setq gnus-newsgroup-unreads
9141                     (sort gnus-newsgroup-unreads '<))) t)))
9142     (unless (listp (cdr gnus-newsgroup-killed))
9143       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9144     (let ((headers gnus-newsgroup-headers))
9145       (gnus-close-group group)
9146       (run-hooks 'gnus-exit-group-hook)
9147       (unless gnus-save-score
9148         (setq gnus-newsgroup-scored nil))
9149       ;; Set the new ranges of read articles.
9150       (gnus-update-read-articles
9151        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9152       ;; Set the current article marks.
9153       (gnus-update-marks)
9154       ;; Do the cross-ref thing.
9155       (when gnus-use-cross-reference
9156         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9157       ;; Do adaptive scoring, and possibly save score files.
9158       (when gnus-newsgroup-adaptive
9159         (gnus-score-adaptive))
9160       (when gnus-use-scoring 
9161         (gnus-score-save))
9162       ;; Do not switch windows but change the buffer to work.
9163       (set-buffer gnus-group-buffer)
9164       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9165           (gnus-group-update-group group)))))
9166   
9167 (defun gnus-summary-exit (&optional temporary)
9168   "Exit reading current newsgroup, and then return to group selection mode.
9169 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9170   (interactive)
9171   (gnus-set-global-variables)
9172   (gnus-kill-save-kill-buffer)
9173   (let* ((group gnus-newsgroup-name)
9174          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9175          (mode major-mode)
9176          (buf (current-buffer)))
9177     (run-hooks 'gnus-summary-prepare-exit-hook)
9178     ;; Make all changes in this group permanent.
9179     (gnus-summary-update-info)          
9180     (set-buffer buf)
9181     (when gnus-use-cache
9182       (gnus-cache-possibly-remove-articles)
9183       (gnus-cache-save-buffers))
9184     ;; Make sure where I was, and go to next newsgroup.
9185     (set-buffer gnus-group-buffer)
9186     (or quit-config
9187         (progn
9188           (gnus-group-jump-to-group group)
9189           (gnus-group-next-unread-group 1)))
9190     (if temporary
9191         nil                             ;Nothing to do.
9192       (if (not gnus-kill-summary-on-exit)
9193           (gnus-deaden-summary)
9194         ;; We set all buffer-local variables to nil.  It is unclear why
9195         ;; this is needed, but if we don't, buffer-local variables are
9196         ;; not garbage-collected, it seems.  This would the lead to en
9197         ;; ever-growing Emacs.
9198         (set-buffer buf)
9199         (gnus-summary-clear-local-variables)
9200         ;; We clear the global counterparts of the buffer-local
9201         ;; variables as well, just to be on the safe side.
9202         (gnus-configure-windows 'group 'force)
9203         (gnus-summary-clear-local-variables)
9204         ;; Return to group mode buffer. 
9205         (if (eq mode 'gnus-summary-mode)
9206             (gnus-kill-buffer buf)))
9207       (setq gnus-current-select-method gnus-select-method)
9208       (pop-to-buffer gnus-group-buffer)
9209       ;; Clear the current group name.
9210       (if (not quit-config)
9211           (progn
9212             (gnus-group-jump-to-group group)
9213             (gnus-group-next-unread-group 1))
9214         (if (not (buffer-name (car quit-config)))
9215             (gnus-configure-windows 'group 'force)
9216           (set-buffer (car quit-config))
9217           (and (eq major-mode 'gnus-summary-mode)
9218                (gnus-set-global-variables))
9219           (gnus-configure-windows (cdr quit-config))))
9220       (run-hooks 'gnus-summary-exit-hook)
9221       (unless quit-config
9222         (setq gnus-newsgroup-name nil)))))
9223
9224 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9225 (defun gnus-summary-exit-no-update (&optional no-questions)
9226   "Quit reading current newsgroup without updating read article info."
9227   (interactive)
9228   (gnus-set-global-variables)
9229   (let* ((group gnus-newsgroup-name)
9230          (quit-config (gnus-group-quit-config group)))
9231     (when (or no-questions
9232               gnus-expert-user
9233               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9234       (if (not gnus-kill-summary-on-exit)
9235           (gnus-deaden-summary)
9236         (gnus-close-group group)
9237         (gnus-summary-clear-local-variables)
9238         (set-buffer gnus-group-buffer)
9239         (gnus-summary-clear-local-variables)
9240         (when (get-buffer gnus-summary-buffer)
9241           (kill-buffer gnus-summary-buffer)))
9242       ;; Return to the group buffer.
9243       (gnus-configure-windows 'group 'force)
9244       ;; Clear the current group name.
9245       (setq gnus-newsgroup-name nil)
9246       (when (equal (gnus-group-group-name) group)
9247         (gnus-group-next-unread-group 1))
9248       (when quit-config
9249         (if (not (buffer-name (car quit-config)))
9250             (gnus-configure-windows 'group 'force)
9251           (set-buffer (car quit-config))
9252           (when (eq major-mode 'gnus-summary-mode)
9253             (gnus-set-global-variables))
9254           (gnus-configure-windows (cdr quit-config)))))))
9255
9256 ;;; Dead summaries.
9257
9258 (defvar gnus-dead-summary-mode-map nil)
9259
9260 (if gnus-dead-summary-mode-map
9261     nil
9262   (setq gnus-dead-summary-map (make-keymap))
9263   (suppress-keymap gnus-dead-summary-map)
9264   (substitute-key-definition
9265    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-map)
9266   (let ((keys '("\C-d" "\r" "\177")))
9267     (while keys
9268       (define-key gnus-dead-summary-map
9269         (pop keys) 'gnus-summary-wake-up-the-dead))))
9270  
9271 (defvar gnus-dead-summary-mode nil
9272   "Minor mode for Gnus summary buffers.")
9273
9274 (defun gnus-dead-summary-mode (&optional arg)
9275   "Minor mode for Gnus summary buffers."
9276   (interactive "P")
9277   (when (eq major-mode 'gnus-summary-mode)
9278     (make-local-variable 'gnus-dead-summary-mode)
9279     (setq gnus-dead-summary-mode 
9280           (if (null arg) (not gnus-dead-summary-mode)
9281             (> (prefix-numeric-value arg) 0)))
9282     (when gnus-dead-summary-mode
9283       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9284         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9285       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9286         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9287               minor-mode-map-alist)))))
9288
9289 (defun gnus-deaden-summary ()
9290   "Make the current summary buffer into a dead summary buffer."
9291   ;; Kill any previous dead summary buffer.
9292   (when (and gnus-dead-summary
9293              (buffer-name gnus-dead-summary))
9294     (save-excursion
9295       (set-buffer gnus-dead-summary)
9296       (when gnus-dead-summary-mode
9297         (kill-buffer (current-buffer)))))
9298   ;; Make this the current dead summary.
9299   (setq gnus-dead-summary (current-buffer))
9300   (gnus-dead-summary-mode 1)
9301   (let ((name (buffer-name)))
9302     (when (string-match "Summary" name)
9303       (rename-buffer
9304        (concat (substring name 0 (match-beginning 0)) "Dead "
9305                (substring name (match-beginning 0))) t))))
9306
9307 (defun gnus-kill-or-deaden-summary (buffer)
9308   "Kill or deaden the summary BUFFER."
9309   (cond (gnus-kill-summary-on-exit
9310          (gnus-kill-buffer buffer))
9311         ((and (get-buffer buffer)
9312               (buffer-name (get-buffer buffer)))
9313          (save-excursion
9314            (set-buffer buffer)
9315            (gnus-deaden-summary)))))
9316
9317 (defun gnus-summary-wake-up-the-dead (&rest args)
9318   "Wake up the dead summary buffer."
9319   (interactive)
9320   (gnus-dead-summary-mode -1)
9321   (let ((name (buffer-name)))
9322     (when (string-match "Dead " name)
9323       (rename-buffer
9324        (concat (substring name 0 (match-beginning 0))
9325                (substring name (match-end 0))) t)))
9326   (gnus-message 3 "This dead summary is now alive again"))
9327
9328 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9329 (defun gnus-summary-fetch-faq (&optional faq-dir)
9330   "Fetch the FAQ for the current group.
9331 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9332 in."
9333   (interactive 
9334    (list
9335     (if current-prefix-arg
9336         (completing-read 
9337          "Faq dir: " (and (listp gnus-group-faq-directory)
9338                           gnus-group-faq-directory)))))
9339   (let (gnus-faq-buffer)
9340     (and (setq gnus-faq-buffer 
9341                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9342          (gnus-configure-windows 'summary-faq))))
9343
9344 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9345 (defun gnus-summary-describe-group (&optional force)
9346   "Describe the current newsgroup."
9347   (interactive "P")
9348   (gnus-group-describe-group force gnus-newsgroup-name))
9349
9350 (defun gnus-summary-describe-briefly ()
9351   "Describe summary mode commands briefly."
9352   (interactive)
9353   (gnus-message 6
9354                 (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")))
9355
9356 ;; Walking around group mode buffer from summary mode.
9357
9358 (defun gnus-summary-next-group (&optional no-article target-group backward)
9359   "Exit current newsgroup and then select next unread newsgroup.
9360 If prefix argument NO-ARTICLE is non-nil, no article is selected
9361 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9362 previous group instead."
9363   (interactive "P")
9364   (gnus-set-global-variables)
9365   (let ((current-group gnus-newsgroup-name)
9366         (current-buffer (current-buffer))
9367         entered)
9368     ;; First we semi-exit this group to update Xrefs and all variables.
9369     ;; We can't do a real exit, because the window conf must remain
9370     ;; the same in case the user is prompted for info, and we don't
9371     ;; want the window conf to change before that...
9372     (gnus-summary-exit t)
9373     (while (not entered)
9374       ;; Then we find what group we are supposed to enter.
9375       (set-buffer gnus-group-buffer)
9376       (gnus-group-jump-to-group current-group)
9377       (setq target-group 
9378             (or target-group        
9379                 (if (eq gnus-keep-same-level 'best) 
9380                     (gnus-summary-best-group gnus-newsgroup-name)
9381                   (gnus-summary-search-group backward gnus-keep-same-level))))
9382       (if (not target-group)
9383           ;; There are no further groups, so we return to the group
9384           ;; buffer.
9385           (progn
9386             (gnus-message 5 "Returning to the group buffer")
9387             (setq entered t)
9388             (set-buffer current-buffer)
9389             (gnus-summary-exit))
9390         ;; We try to enter the target group.
9391         (gnus-group-jump-to-group target-group)
9392         (let ((unreads (gnus-group-group-unread)))
9393           (if (and (or (eq t unreads)
9394                        (and unreads (not (zerop unreads))))
9395                    (gnus-summary-read-group
9396                     target-group nil no-article current-buffer))
9397               (setq entered t)
9398             (setq current-group target-group
9399                   target-group nil)))))))
9400
9401 (defun gnus-summary-prev-group (&optional no-article)
9402   "Exit current newsgroup and then select previous unread newsgroup.
9403 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9404   (interactive "P")
9405   (gnus-summary-next-group no-article nil t))
9406
9407 ;; Walking around summary lines.
9408
9409 (defun gnus-summary-first-subject (&optional unread)
9410   "Go to the first unread subject.
9411 If UNREAD is non-nil, go to the first unread article.
9412 Returns the article selected or nil if there are no unread articles."
9413   (interactive "P")
9414   (prog1
9415       (cond 
9416        ;; Empty summary.
9417        ((null gnus-newsgroup-data)
9418         (gnus-message 3 "No articles in the group")
9419         nil)
9420        ;; Pick the first article.
9421        ((not unread)
9422         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9423         (gnus-data-number (car gnus-newsgroup-data)))
9424        ;; No unread articles.
9425        ((null gnus-newsgroup-unreads)
9426         (gnus-message 3 "No more unread articles")
9427         nil)
9428        ;; Find the first unread article.
9429        (t
9430         (let ((data gnus-newsgroup-data))
9431           (while (and data
9432                       (not (gnus-data-unread-p (car data))))
9433             (setq data (cdr data)))
9434           (if data
9435               (progn
9436                 (goto-char (gnus-data-pos (car data)))
9437                 (gnus-data-number (car data)))))))
9438     (gnus-summary-position-point)))
9439
9440 (defun gnus-summary-next-subject (n &optional unread dont-display)
9441   "Go to next N'th summary line.
9442 If N is negative, go to the previous N'th subject line.
9443 If UNREAD is non-nil, only unread articles are selected.
9444 The difference between N and the actual number of steps taken is
9445 returned."
9446   (interactive "p")
9447   (let ((backward (< n 0))
9448         (n (abs n)))
9449     (while (and (> n 0)
9450                 (if backward
9451                     (gnus-summary-find-prev unread)
9452                   (gnus-summary-find-next unread)))
9453       (setq n (1- n)))
9454     (if (/= 0 n) (gnus-message 7 "No more%s articles"
9455                                (if unread " unread" "")))
9456     (or dont-display
9457         (progn
9458           (gnus-summary-recenter)
9459           (gnus-summary-position-point)))
9460     n))
9461
9462 (defun gnus-summary-next-unread-subject (n)
9463   "Go to next N'th unread summary line."
9464   (interactive "p")
9465   (gnus-summary-next-subject n t))
9466
9467 (defun gnus-summary-prev-subject (n &optional unread)
9468   "Go to previous N'th summary line.
9469 If optional argument UNREAD is non-nil, only unread article is selected."
9470   (interactive "p")
9471   (gnus-summary-next-subject (- n) unread))
9472
9473 (defun gnus-summary-prev-unread-subject (n)
9474   "Go to previous N'th unread summary line."
9475   (interactive "p")
9476   (gnus-summary-next-subject (- n) t))
9477
9478 (defun gnus-summary-goto-subject (article &optional force silent)
9479   "Go the subject line of ARTICLE.
9480 If FORCE, also allow jumping to articles not currently shown."
9481   (let ((b (point))
9482         (data (gnus-data-find article)))
9483     ;; We read in the article if we have to.
9484     (and (not data) 
9485          force
9486          (gnus-summary-insert-subject article)
9487          (setq data (gnus-data-find article)))
9488     (goto-char b)
9489     (if (and (not silent) (not data))
9490         (progn
9491           (message "Can't find article %d" article)
9492           nil)
9493       (goto-char (gnus-data-pos data))
9494       article)))
9495
9496 ;; Walking around summary lines with displaying articles.
9497
9498 (defun gnus-summary-expand-window (&optional arg)
9499   "Make the summary buffer take up the entire Emacs frame.
9500 Given a prefix, will force an `article' buffer configuration."
9501   (interactive "P")
9502   (gnus-set-global-variables)
9503   (if arg
9504       (gnus-configure-windows 'article 'force)
9505     (gnus-configure-windows 'summary 'force)))
9506
9507 (defun gnus-summary-display-article (article &optional all-header)
9508   "Display ARTICLE in article buffer."
9509   (gnus-set-global-variables)
9510   (if (null article)
9511       nil
9512     (prog1
9513         (gnus-article-prepare article all-header)
9514       (gnus-summary-show-thread)
9515       (run-hooks 'gnus-select-article-hook)
9516       (gnus-summary-recenter)
9517       (gnus-summary-goto-subject article)
9518       ;; Successfully display article.
9519       (gnus-summary-update-line)
9520       (gnus-article-set-window-start 
9521        (cdr (assq article gnus-newsgroup-bookmarks)))
9522       t)))
9523
9524 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
9525   "Select the current article.
9526 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
9527 non-nil, the article will be re-fetched even if it already present in
9528 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
9529 be displayed."
9530   (let ((article (or article (gnus-summary-article-number)))
9531         (all-headers (not (not all-headers))) ;Must be T or NIL.
9532         did) 
9533     (and (not pseudo) 
9534          (gnus-summary-article-pseudo-p article)
9535          (error "This is a pseudo-article."))
9536     (prog1
9537         (save-excursion
9538           (set-buffer gnus-summary-buffer)
9539           (if (or (null gnus-current-article)
9540                   (null gnus-article-current)
9541                   (null (get-buffer gnus-article-buffer))
9542                   (not (eq article (cdr gnus-article-current)))
9543                   (not (equal (car gnus-article-current) gnus-newsgroup-name))
9544                   force)
9545               ;; The requested article is different from the current article.
9546               (progn
9547                 (gnus-summary-display-article article all-headers)
9548                 (setq did article))
9549             (if (or all-headers gnus-show-all-headers) 
9550                 (gnus-article-show-all-headers))
9551             nil))
9552       (if did 
9553           (gnus-article-set-window-start 
9554            (cdr (assq article gnus-newsgroup-bookmarks)))))))
9555
9556 (defun gnus-summary-set-current-mark (&optional current-mark)
9557   "Obsolete function."
9558   nil)
9559
9560 (defun gnus-summary-next-article (&optional unread subject backward)
9561   "Select the next article.
9562 If UNREAD, only unread articles are selected.
9563 If SUBJECT, only articles with SUBJECT are selected.
9564 If BACKWARD, the previous article is selected instead of the next."
9565   (interactive "P")
9566   (gnus-set-global-variables)
9567   (let (header)
9568     (cond
9569      ;; Is there such an article?
9570      ((and (gnus-summary-search-forward unread subject backward)
9571            (or (gnus-summary-display-article (gnus-summary-article-number))
9572                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
9573       (gnus-summary-position-point))
9574      ;; If not, we try the first unread, if that is wanted.
9575      ((and subject
9576            gnus-auto-select-same
9577            (or (gnus-summary-first-unread-article)
9578                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
9579       (gnus-summary-position-point)
9580       (gnus-message 6 "Wrapped"))
9581      ;; Try to get next/previous article not displayed in this group.
9582      ((and gnus-auto-extend-newsgroup
9583            (not unread) (not subject))
9584       (gnus-summary-goto-article 
9585        (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
9586        nil t))
9587      ;; Go to next/previous group.
9588      (t
9589       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9590           (gnus-summary-jump-to-group gnus-newsgroup-name))
9591       (let ((cmd last-command-char)
9592             (group 
9593              (if (eq gnus-keep-same-level 'best) 
9594                  (gnus-summary-best-group gnus-newsgroup-name)
9595                (gnus-summary-search-group backward gnus-keep-same-level))))
9596         ;; For some reason, the group window gets selected.  We change
9597         ;; it back.  
9598         (select-window (get-buffer-window (current-buffer)))
9599         ;; Select next unread newsgroup automagically.
9600         (cond 
9601          ((not gnus-auto-select-next)
9602           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
9603          ((or (eq gnus-auto-select-next 'quietly)
9604               (and (eq gnus-auto-select-next 'almost-quietly)
9605                    (gnus-summary-last-article-p)))
9606           ;; Select quietly.
9607           (if (gnus-ephemeral-group-p gnus-newsgroup-name)
9608               (gnus-summary-exit)
9609             (gnus-message 7 "No more%s articles (%s)..."
9610                           (if unread " unread" "") 
9611                           (if group (concat "selecting " group)
9612                             "exiting"))
9613             (gnus-summary-next-group nil group backward)))
9614          (t
9615           (gnus-summary-walk-group-buffer 
9616            gnus-newsgroup-name cmd unread backward))))))))
9617
9618 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
9619   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
9620                       (?\C-p (gnus-group-prev-unread-group 1))))
9621         keve key group ended)
9622     (while (not ended)
9623       (save-excursion
9624         (set-buffer gnus-group-buffer)
9625         (setq group (gnus-group-group-name)))
9626       (gnus-message 
9627        7 "No more%s articles%s" (if unread " unread" "")
9628        (if (and group 
9629                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
9630            (format " (Type %s for %s [%s])"
9631                    (single-key-description cmd) group
9632                    (car (gnus-gethash group gnus-newsrc-hashtb)))
9633          (format " (Type %s to exit %s)"
9634                  (single-key-description cmd)
9635                  gnus-newsgroup-name)))
9636       ;; Confirm auto selection.
9637       (setq key (car (setq keve (gnus-read-event-char))))
9638       (setq ended t)
9639       (cond 
9640        ((assq key keystrokes)
9641         (let ((obuf (current-buffer)))
9642           (switch-to-buffer gnus-group-buffer)
9643           (and group
9644                (gnus-group-jump-to-group group))
9645           (eval (car (cdr (assq key keystrokes))))
9646           (setq group (gnus-group-group-name))
9647           (switch-to-buffer obuf))
9648         (setq ended nil))
9649        ((equal key cmd)
9650         (if (or (not group)
9651                 (gnus-ephemeral-group-p gnus-newsgroup-name))
9652             (gnus-summary-exit)
9653           (gnus-summary-next-group nil group backward)))
9654        (t
9655         (push (cdr keve) unread-command-events))))))
9656
9657 (defun gnus-read-event-char ()
9658   "Get the next event."
9659   (let ((event (read-event)))
9660     (cons (and (numberp event) event) event)))
9661
9662 (defun gnus-summary-next-unread-article ()
9663   "Select unread article after current one."
9664   (interactive)
9665   (gnus-summary-next-article t (and gnus-auto-select-same
9666                                     (gnus-summary-article-subject))))
9667
9668 (defun gnus-summary-prev-article (&optional unread subject)
9669   "Select the article after the current one.
9670 If UNREAD is non-nil, only unread articles are selected."
9671   (interactive "P")
9672   (gnus-summary-next-article unread subject t))
9673
9674 (defun gnus-summary-prev-unread-article ()
9675   "Select unred article before current one."
9676   (interactive)
9677   (gnus-summary-prev-article t (and gnus-auto-select-same
9678                                     (gnus-summary-article-subject))))
9679
9680 (defun gnus-summary-next-page (&optional lines circular)
9681   "Show next page of selected article.
9682 If end of article, select next article.
9683 Argument LINES specifies lines to be scrolled up.
9684 If CIRCULAR is non-nil, go to the start of the article instead of 
9685 instead of selecting the next article when reaching the end of the
9686 current article." 
9687   (interactive "P")
9688   (setq gnus-summary-buffer (current-buffer))
9689   (gnus-set-global-variables)
9690   (let ((article (gnus-summary-article-number))
9691         (endp nil))
9692     (gnus-configure-windows 'article)
9693     (if (or (null gnus-current-article)
9694             (null gnus-article-current)
9695             (/= article (cdr gnus-article-current))
9696             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
9697         ;; Selected subject is different from current article's.
9698         (gnus-summary-display-article article)
9699       (gnus-eval-in-buffer-window
9700        gnus-article-buffer
9701        (setq endp (gnus-article-next-page lines)))
9702       (if endp
9703           (cond (circular
9704                  (gnus-summary-beginning-of-article))
9705                 (lines
9706                  (gnus-message 3 "End of message"))
9707                 ((null lines)
9708                  (gnus-summary-next-unread-article)))))
9709     (gnus-summary-recenter)
9710     (gnus-summary-position-point)))
9711
9712 (defun gnus-summary-prev-page (&optional lines)
9713   "Show previous page of selected article.
9714 Argument LINES specifies lines to be scrolled down."
9715   (interactive "P")
9716   (gnus-set-global-variables)
9717   (let ((article (gnus-summary-article-number)))
9718     (gnus-configure-windows 'article)
9719     (if (or (null gnus-current-article)
9720             (null gnus-article-current)
9721             (/= article (cdr gnus-article-current))
9722             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
9723         ;; Selected subject is different from current article's.
9724         (gnus-summary-display-article article)
9725       (gnus-summary-recenter)
9726       (gnus-eval-in-buffer-window gnus-article-buffer
9727                                   (gnus-article-prev-page lines))))
9728   (gnus-summary-position-point))
9729
9730 (defun gnus-summary-scroll-up (lines)
9731   "Scroll up (or down) one line current article.
9732 Argument LINES specifies lines to be scrolled up (or down if negative)."
9733   (interactive "p")
9734   (gnus-set-global-variables)
9735   (gnus-configure-windows 'article)
9736   (or (gnus-summary-select-article nil nil 'pseudo)
9737       (gnus-eval-in-buffer-window 
9738        gnus-article-buffer
9739        (cond ((> lines 0)
9740               (if (gnus-article-next-page lines)
9741                   (gnus-message 3 "End of message")))
9742              ((< lines 0)
9743               (gnus-article-prev-page (- lines))))))
9744   (gnus-summary-recenter)
9745   (gnus-summary-position-point))
9746
9747 (defun gnus-summary-next-same-subject ()
9748   "Select next article which has the same subject as current one."
9749   (interactive)
9750   (gnus-set-global-variables)
9751   (gnus-summary-next-article nil (gnus-summary-article-subject)))
9752
9753 (defun gnus-summary-prev-same-subject ()
9754   "Select previous article which has the same subject as current one."
9755   (interactive)
9756   (gnus-set-global-variables)
9757   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
9758
9759 (defun gnus-summary-next-unread-same-subject ()
9760   "Select next unread article which has the same subject as current one."
9761   (interactive)
9762   (gnus-set-global-variables)
9763   (gnus-summary-next-article t (gnus-summary-article-subject)))
9764
9765 (defun gnus-summary-prev-unread-same-subject ()
9766   "Select previous unread article which has the same subject as current one."
9767   (interactive)
9768   (gnus-set-global-variables)
9769   (gnus-summary-prev-article t (gnus-summary-article-subject)))
9770
9771 (defun gnus-summary-first-unread-article ()
9772   "Select the first unread article. 
9773 Return nil if there are no unread articles."
9774   (interactive)
9775   (gnus-set-global-variables)
9776   (prog1
9777       (if (gnus-summary-first-subject t)
9778           (progn
9779             (gnus-summary-show-thread)
9780             (gnus-summary-first-subject t)
9781             (gnus-summary-display-article (gnus-summary-article-number))))
9782     (gnus-summary-position-point)))
9783
9784 (defun gnus-summary-best-unread-article ()
9785   "Select the unread article with the highest score."
9786   (interactive)
9787   (gnus-set-global-variables)
9788   (let ((best -1000000)
9789         (data gnus-newsgroup-data)
9790         article score)
9791     (while data
9792       (and (gnus-data-unread-p (car data))
9793            (> (setq score 
9794                     (gnus-summary-article-score (gnus-data-number (car data))))
9795               best)
9796            (setq best score
9797                  article (gnus-data-number (car data))))
9798       (setq data (cdr data)))
9799     (if article
9800         (gnus-summary-goto-article article)
9801       (error "No unread articles"))
9802     (gnus-summary-position-point)))
9803
9804 (defun gnus-summary-last-subject ()
9805   "Go to the last displayed subject line in the group."
9806   (let ((article (gnus-data-number (car (gnus-data-list t)))))
9807     (when article
9808       (gnus-summary-goto-subject article))))
9809
9810 (defun gnus-summary-goto-article (article &optional all-headers force)
9811   "Fetch ARTICLE and display it if it exists.
9812 If ALL-HEADERS is non-nil, no header lines are hidden."
9813   (interactive
9814    (list
9815     (string-to-int
9816      (completing-read 
9817       "Article number: "
9818       (mapcar (lambda (number) (list (int-to-string number)))
9819               gnus-newsgroup-limit)))
9820     current-prefix-arg
9821     t))
9822   (prog1
9823       (if (gnus-summary-goto-subject article force)
9824           (gnus-summary-display-article article all-headers)
9825         (message "Couldn't go to article %s" article) nil)
9826     (gnus-summary-position-point)))
9827
9828 (defun gnus-summary-goto-last-article ()
9829   "Go to the previously read article."
9830   (interactive)
9831   (prog1
9832       (and gnus-last-article
9833            (gnus-summary-goto-article gnus-last-article))
9834     (gnus-summary-position-point)))
9835
9836 (defun gnus-summary-pop-article (number)
9837   "Pop one article off the history and go to the previous.
9838 NUMBER articles will be popped off."
9839   (interactive "p")
9840   (let (to)
9841     (setq gnus-newsgroup-history
9842           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
9843     (if to
9844         (gnus-summary-goto-article (car to))
9845       (error "Article history empty")))
9846   (gnus-summary-position-point))
9847
9848 ;; Summary commands and functions for limiting the summary buffer.
9849
9850 (defun gnus-summary-limit-to-articles (n)
9851   "Limit the summary buffer to the next N articles.
9852 If not given a prefix, use the process marked articles instead."
9853   (interactive "P")
9854   (gnus-set-global-variables)
9855   (prog1
9856       (let ((articles (gnus-summary-work-articles n)))
9857         (gnus-summary-limit articles))
9858     (gnus-summary-position-point)))
9859
9860 (defun gnus-summary-pop-limit (&optional total)
9861   "Restore the previous limit.
9862 If given a prefix, remove all limits."
9863   (interactive "P")
9864   (gnus-set-global-variables)
9865   (prog2
9866       (if total (setq gnus-newsgroup-limits 
9867                       (list (mapcar (lambda (h) (mail-header-number h))
9868                                     gnus-newsgroup-headers))))
9869       (gnus-summary-limit nil 'pop)
9870     (gnus-summary-position-point)))
9871
9872 (defun gnus-summary-limit-to-subject (subject)
9873   "Limit the summary buffer to articles that have subjects that match a regexp."
9874   (interactive "sRegexp: ")
9875   (when (not (equal "" subject))
9876     (prog1
9877         (let ((articles (gnus-summary-find-matching "subject" subject 'all)))
9878           (or articles (error "Found no matches for \"%s\"" subject))
9879           (gnus-summary-limit articles))
9880       (gnus-summary-position-point))))
9881
9882 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
9883 (make-obsolete 
9884  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
9885
9886 (defun gnus-summary-limit-to-unread (&optional all)
9887   "Limit the summary buffer to articles that are not marked as read.
9888 If ALL is non-nil, limit strictly to unread articles."
9889   (interactive "P")
9890   (if all
9891       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
9892     (gnus-summary-limit-to-marks
9893      ;; Concat all the marks that say that an article is read and have
9894      ;; those removed.  
9895      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
9896            gnus-killed-mark gnus-kill-file-mark
9897            gnus-low-score-mark gnus-expirable-mark
9898            gnus-canceled-mark gnus-catchup-mark)
9899      'reverse)))
9900
9901 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
9902 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
9903
9904 (defun gnus-summary-limit-to-marks (marks &optional reverse)
9905   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
9906 If REVERSE, limit the summary buffer to articles that are not marked
9907 with MARKS.  MARKS can either be a string of marks or a list of marks. 
9908 Returns how many articles were removed."
9909   (interactive "sMarks: ")
9910   (gnus-set-global-variables)
9911   (prog1
9912       (let ((data gnus-newsgroup-data)
9913             (marks (if (listp marks) marks
9914                      (append marks nil))) ; Transform to list.
9915             articles)
9916         (while data
9917           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
9918                  (memq (gnus-data-mark (car data)) marks))
9919                (setq articles (cons (gnus-data-number (car data)) articles)))
9920           (setq data (cdr data)))
9921         (gnus-summary-limit articles))
9922     (gnus-summary-position-point)))
9923
9924 (defun gnus-summary-limit-to-score (&optional score)
9925   "Limit to articles with score at or above SCORE."
9926   (interactive "P")
9927   (gnus-set-global-variables)
9928   (setq score (if score
9929                   (prefix-numeric-value score)
9930                 (or gnus-summary-default-score 0)))
9931   (let ((data gnus-newsgroup-data)
9932         articles)
9933     (while data
9934       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
9935                 score)
9936         (push (gnus-data-number (car data)) articles))
9937       (setq data (cdr data)))
9938     (prog1
9939         (gnus-summary-limit articles)
9940       (gnus-summary-position-point))))
9941
9942 (defun gnus-summary-limit-include-dormant ()
9943   "Display all the hidden articles that are marked as dormant."
9944   (interactive)
9945   (gnus-set-global-variables)
9946   (or gnus-newsgroup-dormant 
9947       (error "There are no dormant articles in this group"))
9948   (prog1
9949       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
9950     (gnus-summary-position-point)))
9951
9952 (defun gnus-summary-limit-exclude-dormant ()
9953   "Hide all dormant articles."
9954   (interactive)
9955   (gnus-set-global-variables)
9956   (prog1
9957       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
9958     (gnus-summary-position-point)))
9959
9960 (defun gnus-summary-limit-exclude-childless-dormant ()
9961   "Hide all dormant articles that have no children."
9962   (interactive)
9963   (gnus-set-global-variables)
9964   (let ((data gnus-newsgroup-data)
9965         articles)
9966     ;; Find all articles that are either not dormant or have
9967     ;; children. 
9968     (while data
9969       (and (or (not (= (gnus-data-mark (car data)) gnus-dormant-mark))
9970                (gnus-article-parent-p (gnus-data-number (car data))))
9971            (setq articles (cons (gnus-data-number (car data))
9972                                 articles)))
9973       (setq data (cdr data)))
9974     ;; Do the limiting.
9975     (prog1
9976         (gnus-summary-limit articles)
9977       (gnus-summary-position-point))))
9978  
9979 (defun gnus-summary-limit (articles &optional pop)
9980   (if pop
9981       ;; We pop the previous limit off the stack and use that.
9982       (setq articles (car gnus-newsgroup-limits)
9983             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
9984     ;; We use the new limit, so we push the old limit on the stack. 
9985     (setq gnus-newsgroup-limits 
9986           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
9987   ;; Set the limit.
9988   (setq gnus-newsgroup-limit articles)
9989   (let ((total (length gnus-newsgroup-data))
9990         (data (gnus-data-find-list (gnus-summary-article-number)))
9991         found)
9992     ;; This will do all the work of generating the new summary buffer
9993     ;; according to the new limit.
9994     (gnus-summary-prepare)
9995     ;; Try to return to the article you were at, or on in the
9996     ;; neighborhood.  
9997     (if data
9998         ;; We try to find some article after the current one.
9999         (while data
10000           (and (gnus-summary-goto-subject (gnus-data-number (car data)))
10001                (setq data nil
10002                      found t))
10003           (setq data (cdr data))))
10004     (or found
10005         ;; If there is no data, that means that we were after the last
10006         ;; article.  The same goes when we can't find any articles
10007         ;; after the current one.
10008         (progn
10009           (goto-char (point-max))
10010           (gnus-summary-find-prev)))
10011     ;; We return how many articles were removed from the summary
10012     ;; buffer as a result of the new limit.
10013     (- total (length gnus-newsgroup-data))))
10014
10015 (defun gnus-summary-initial-limit ()
10016   "Figure out what the initial limit is supposed to be on group entry.
10017 This entails weeding out unwanted dormants, low-scored articles,
10018 fetch-old-headers verbiage, and so on."
10019   ;; Most groups have nothing to remove.
10020   (if (or gnus-inhibit-limiting
10021           (and (null gnus-newsgroup-dormant)
10022                (not (eq gnus-fetch-old-headers 'some))
10023                (null gnus-summary-expunge-below)
10024                (null gnus-thread-expunge-below)))
10025       () ; Do nothing.
10026     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10027     (setq gnus-newsgroup-limit nil)
10028     (mapatoms
10029      (lambda (node)
10030        (unless (car (symbol-value node))
10031          ;; These threads have no parents -- they are roots.
10032          (let ((nodes (cdr (symbol-value node))))
10033            (while nodes
10034              (if (and gnus-thread-expunge-below
10035                       (< (gnus-thread-total-score (car nodes))
10036                          gnus-thread-expunge-below))
10037                  (gnus-expunge-thread (pop nodes))
10038                (gnus-summary-limit-children (pop nodes)))))))
10039      gnus-newsgroup-dependencies)
10040     (when (not gnus-newsgroup-limit)
10041       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10042     gnus-newsgroup-limit))
10043
10044 (defun gnus-summary-limit-children (thread)
10045   "Return 1 if this subthread is visible and 0 if it is not."
10046   ;; First we get the number of visible children to this thread.  This
10047   ;; is done by recursing down the thread using this function, so this
10048   ;; will really go down to a leaf article first, before slowly
10049   ;; working its way up towards the root.
10050   (let ((children 
10051          (if (cdr thread)
10052              (apply '+ (mapcar 'gnus-summary-limit-children 
10053                                (cdr thread)))
10054            0))
10055         (number (mail-header-number (car thread)))
10056         score)
10057     (if (or 
10058          ;; If this article is dormant and has absolutely no visible
10059          ;; children, then this article isn't visible.
10060          (and (memq number gnus-newsgroup-dormant)
10061               (= children 0))
10062          ;; If this is a "fetch-old-headered" and there is only one
10063          ;; visible child (or less), then we don't want this article. 
10064          (and (eq gnus-fetch-old-headers 'some)
10065               (memq number gnus-newsgroup-ancient)
10066               (<= children 1))
10067          ;; If we use expunging, and this article is really
10068          ;; low-scored, then we don't want this article.
10069          (when (and gnus-summary-expunge-below
10070                     (< (setq score 
10071                              (or (cdr (assq number gnus-newsgroup-scored)) 
10072                                  gnus-summary-default-score))
10073                        gnus-summary-expunge-below))
10074            ;; We increase the expunge-tally here, but that has
10075            ;; nothing to do with the limits, really.
10076            (incf gnus-newsgroup-expunged-tally)
10077            ;; We also mark as read here, if that's wanted.
10078            (when (and gnus-summary-mark-below
10079                       (< score gnus-summary-mark-below))
10080              (setq gnus-newsgroup-unreads 
10081                    (delq number gnus-newsgroup-unreads))
10082              (if gnus-newsgroup-auto-expire
10083                  (push number gnus-newsgroup-expirable)
10084                (push (cons number gnus-low-score-mark)
10085                      gnus-newsgroup-reads)))
10086            t))
10087         ;; Nope, invisible article.
10088         0
10089       ;; Ok, this article is to be visible, so we add it to the limit
10090       ;; and return 1.
10091       (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10092       1)))
10093
10094 (defun gnus-expunge-thread (thread)
10095   "Mark all articles in THREAD as read."
10096   (let* ((number (mail-header-number (car thread))))
10097     (incf gnus-newsgroup-expunged-tally)
10098     ;; We also mark as read here, if that's wanted.
10099     (setq gnus-newsgroup-unreads 
10100           (delq number gnus-newsgroup-unreads))
10101     (if gnus-newsgroup-auto-expire
10102         (push number gnus-newsgroup-expirable)
10103       (push (cons number gnus-low-score-mark)
10104             gnus-newsgroup-reads)))
10105   ;; Go recursively through all subthreads.
10106   (mapcar 'gnus-expunge-thread (cdr thread)))
10107
10108 ;; Summary article oriented commands
10109
10110 (defun gnus-summary-refer-parent-article (n)
10111   "Refer parent article N times.
10112 The difference between N and the number of articles fetched is returned."
10113   (interactive "p")
10114   (gnus-set-global-variables)
10115   (while 
10116       (and 
10117        (> n 0)
10118        (let* ((header (gnus-summary-article-header))
10119               (ref 
10120                ;; If we try to find the parent of the currently
10121                ;; displayed article, then we take a look at the actual
10122                ;; References header, since this is slightly more
10123                ;; reliable than the References field we got from the
10124                ;; server. 
10125                (if (and (eq (mail-header-number header) 
10126                             (cdr gnus-article-current))
10127                         (equal gnus-newsgroup-name 
10128                                (car gnus-article-current)))
10129                    (save-excursion
10130                      (set-buffer gnus-original-article-buffer)
10131                      (gnus-narrow-to-headers)
10132                      (prog1
10133                          (mail-fetch-field "references")
10134                        (widen)))
10135                  ;; It's not the current article, so we take a bet on
10136                  ;; the value we got from the server. 
10137                  (mail-header-references header))))
10138          (if ref
10139              (or (gnus-summary-refer-article (gnus-parent-id ref))
10140                  (gnus-message 1 "Couldn't find parent"))
10141            (gnus-message 1 "No references in article %d"
10142                          (gnus-summary-article-number))
10143            nil)))
10144     (setq n (1- n)))
10145   (gnus-summary-position-point)
10146   n)
10147
10148 (defun gnus-summary-refer-references ()
10149   "Fetch all articles mentioned in the References header.
10150 Return how many articles were fetched."
10151   (interactive)
10152   (gnus-set-global-variables)
10153   (let ((ref (mail-header-references (gnus-summary-article-header)))
10154         (current (gnus-summary-article-number))
10155         (n 0))
10156     ;; For each Message-ID in the References header...
10157     (while (string-match "<[^>]*>" ref)
10158       (incf n)
10159       ;; ... fetch that article.
10160       (gnus-summary-refer-article 
10161        (prog1 (match-string 0 ref)
10162          (setq ref (substring ref (match-end 0))))))
10163     (gnus-summary-goto-subject current)
10164     (gnus-summary-position-point)
10165     n))
10166     
10167 (defun gnus-summary-refer-article (message-id)
10168   "Fetch an article specified by MESSAGE-ID."
10169   (interactive "sMessage-ID: ")
10170   (when (and (stringp message-id)
10171              (not (zerop (length message-id))))
10172     ;; Construct the correct Message-ID if necessary.
10173     ;; Suggested by tale@pawl.rpi.edu.
10174     (unless (string-match "^<" message-id)
10175       (setq message-id (concat "<" message-id)))
10176     (unless (string-match ">$" message-id)
10177       (setq message-id (concat message-id ">")))
10178     (let ((header (car (gnus-gethash (downcase message-id)
10179                                      gnus-newsgroup-dependencies))))
10180       (if header
10181           ;; The article is present in the buffer, to we just go to it.
10182           (gnus-summary-goto-article (mail-header-number header) nil t)
10183         ;; We fetch the article
10184         (let ((gnus-override-method gnus-refer-article-method)
10185               number)
10186           ;; Start the special refer-article method, if necessary.
10187           (when gnus-refer-article-method
10188             (gnus-check-server gnus-refer-article-method))
10189           ;; Fetch the header, and display the article.
10190           (when (setq number (gnus-summary-insert-subject message-id))
10191             (gnus-summary-select-article nil nil nil number)))))))
10192
10193 (defun gnus-summary-enter-digest-group (&optional force)
10194   "Enter a digest group based on the current article."
10195   (interactive "P")
10196   (gnus-set-global-variables)
10197   (gnus-summary-select-article)
10198   (let ((name (format "%s-%d" 
10199                       (gnus-group-prefixed-name 
10200                        gnus-newsgroup-name (list 'nndoc "")) 
10201                       gnus-current-article))
10202         (ogroup gnus-newsgroup-name)
10203         (buf (current-buffer)))
10204     (save-excursion
10205       (set-buffer gnus-original-article-buffer)
10206       (goto-char (point-min))
10207       (search-forward "\n\n" nil t)
10208       (narrow-to-region (point) (point-max)))
10209     (unwind-protect
10210         (if (gnus-group-read-ephemeral-group 
10211              name `(nndoc ,name (nndoc-address 
10212                                  ,(get-buffer gnus-original-article-buffer))
10213                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10214             ;; Make all postings to this group go to the parent group.
10215             (setcdr (nthcdr 4 (gnus-get-info name))
10216                     (list (list (cons 'to-group ogroup))))
10217           ;; Couldn't select this doc group.
10218           (switch-to-buffer buf)
10219           (gnus-set-global-variables)
10220           (gnus-configure-windows 'summary)
10221           (gnus-message 3 "Article couldn't be entered?"))
10222       (save-excursion
10223         (set-buffer gnus-original-article-buffer)
10224         (widen)))))
10225
10226 (defun gnus-summary-isearch-article (&optional regexp-p)
10227   "Do incremental search forward on the current article.
10228 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10229   (interactive "P")
10230   (gnus-set-global-variables)
10231   (gnus-summary-select-article)
10232   (gnus-eval-in-buffer-window 
10233    gnus-article-buffer
10234    (goto-char (point-min))
10235    (isearch-forward regexp-p)))
10236
10237 (defun gnus-summary-search-article-forward (regexp &optional backward)
10238   "Search for an article containing REGEXP forward.
10239 If BACKWARD, search backward instead."
10240   (interactive
10241    (list (read-string
10242           (format "Search article %s (regexp%s): "
10243                   (if current-prefix-arg "backward" "forward")
10244                   (if gnus-last-search-regexp
10245                       (concat ", default " gnus-last-search-regexp)
10246                     "")))
10247          current-prefix-arg))
10248   (gnus-set-global-variables)
10249   (if (string-equal regexp "")
10250       (setq regexp (or gnus-last-search-regexp ""))
10251     (setq gnus-last-search-regexp regexp))
10252   (if (gnus-summary-search-article regexp backward)
10253       (gnus-article-set-window-start 
10254        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10255     (error "Search failed: \"%s\"" regexp)))
10256
10257 (defun gnus-summary-search-article-backward (regexp)
10258   "Search for an article containing REGEXP backward."
10259   (interactive
10260    (list (read-string
10261           (format "Search article backward (regexp%s): "
10262                   (if gnus-last-search-regexp
10263                       (concat ", default " gnus-last-search-regexp)
10264                     "")))))
10265   (gnus-summary-search-article-forward regexp 'backward))
10266
10267 (defun gnus-summary-search-article (regexp &optional backward)
10268   "Search for an article containing REGEXP.
10269 Optional argument BACKWARD means do search for backward.
10270 gnus-select-article-hook is not called during the search."
10271   (let ((gnus-select-article-hook nil)  ;Disable hook.
10272         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10273         (re-search
10274          (if backward
10275              (function re-search-backward) (function re-search-forward)))
10276         (found nil)
10277         (last nil))
10278     ;; Hidden thread subtrees must be searched for ,too.
10279     (gnus-summary-show-all-threads)
10280     ;; First of all, search current article.
10281     ;; We don't want to read article again from NNTP server nor reset
10282     ;; current point.
10283     (gnus-summary-select-article)
10284     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10285     (setq last gnus-current-article)
10286     (gnus-eval-in-buffer-window
10287      gnus-article-buffer
10288      (save-restriction
10289        (widen)
10290        ;; Begin search from current point.
10291        (setq found (funcall re-search regexp nil t))))
10292     ;; Then search next articles.
10293     (while (and (not found)
10294                 (gnus-summary-display-article 
10295                  (if backward (gnus-summary-find-prev)
10296                    (gnus-summary-find-next))))
10297       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10298       (gnus-eval-in-buffer-window
10299        gnus-article-buffer
10300        (save-restriction
10301          (widen)
10302          (goto-char (if backward (point-max) (point-min)))
10303          (setq found (funcall re-search regexp nil t)))))
10304     (message "")
10305     ;; Adjust article pointer.
10306     (or (eq last gnus-current-article)
10307         (setq gnus-last-article last))
10308     ;; Return T if found such article.
10309     found))
10310
10311 (defun gnus-summary-find-matching (header regexp &optional backward unread
10312                                           not-case-fold)
10313   "Return a list of all articles that match REGEXP on HEADER.
10314 The search stars on the current article and goes forwards unless
10315 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10316 If UNREAD is non-nil, only unread articles will
10317 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10318 in the comparisons."
10319   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10320                 (gnus-data-find-list 
10321                  (gnus-summary-article-number) (gnus-data-list backward))))
10322         (func (intern (concat "gnus-header-" header)))
10323         (case-fold-search (not not-case-fold))
10324         articles d)
10325     (or (fboundp func) (error "%s is not a valid header" header))
10326     (while data
10327       (setq d (car data))
10328       (and (or (not unread)             ; We want all articles...
10329                (gnus-data-unread-p d))  ; Or just unreads.
10330            (vectorp (gnus-data-header d)) ; It's not a pseudo.
10331            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
10332            (setq articles (cons (gnus-data-number d) articles))) ; Success!
10333       (setq data (cdr data)))
10334     (nreverse articles)))
10335     
10336 (defun gnus-summary-execute-command (header regexp command &optional backward)
10337   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
10338 If HEADER is an empty string (or nil), the match is done on the entire
10339 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
10340   (interactive
10341    (list (let ((completion-ignore-case t))
10342            (completing-read 
10343             "Header name: "
10344             (mapcar (lambda (string) (list string))
10345                     '("Number" "Subject" "From" "Lines" "Date"
10346                       "Message-ID" "Xref" "References"))
10347             nil 'require-match))
10348          (read-string "Regexp: ")
10349          (read-key-sequence "Command: ")
10350          current-prefix-arg))
10351   (gnus-set-global-variables)
10352   ;; Hidden thread subtrees must be searched as well.
10353   (gnus-summary-show-all-threads)
10354   ;; We don't want to change current point nor window configuration.
10355   (save-excursion
10356     (save-window-excursion
10357       (gnus-message 6 "Executing %s..." (key-description command))
10358       ;; We'd like to execute COMMAND interactively so as to give arguments.
10359       (gnus-execute header regexp
10360                     `(lambda () (call-interactively ',(key-binding command)))
10361                     backward)
10362       (gnus-message 6 "Executing %s...done" (key-description command)))))
10363
10364 (defun gnus-summary-beginning-of-article ()
10365   "Scroll the article back to the beginning."
10366   (interactive)
10367   (gnus-set-global-variables)
10368   (gnus-summary-select-article)
10369   (gnus-configure-windows 'article)
10370   (gnus-eval-in-buffer-window
10371    gnus-article-buffer
10372    (widen)
10373    (goto-char (point-min))
10374    (and gnus-break-pages (gnus-narrow-to-page))))
10375
10376 (defun gnus-summary-end-of-article ()
10377   "Scroll to the end of the article."
10378   (interactive)
10379   (gnus-set-global-variables)
10380   (gnus-summary-select-article)
10381   (gnus-configure-windows 'article)
10382   (gnus-eval-in-buffer-window 
10383    gnus-article-buffer
10384    (widen)
10385    (goto-char (point-max))
10386    (recenter -3)
10387    (and gnus-break-pages (gnus-narrow-to-page))))
10388
10389 (defun gnus-summary-show-article (&optional arg)
10390   "Force re-fetching of the current article.
10391 If ARG (the prefix) is non-nil, show the raw article without any
10392 article massaging functions being run."
10393   (interactive "P")
10394   (gnus-set-global-variables)
10395   (if (not arg)
10396       ;; Select the article the normal way.
10397       (gnus-summary-select-article nil 'force)
10398     ;; Bind the article treatment functions to nil.
10399     (let ((gnus-have-all-headers t)
10400           gnus-article-display-hook
10401           gnus-article-prepare-hook
10402           gnus-visual)
10403       (gnus-summary-select-article nil 'force)))
10404   (gnus-configure-windows 'article)
10405   (gnus-summary-position-point))
10406
10407 (defun gnus-summary-verbose-headers (&optional arg)
10408   "Toggle permanent full header display.
10409 If ARG is a positive number, turn header display on.
10410 If ARG is a negative number, turn header display off."
10411   (interactive "P")
10412   (gnus-set-global-variables)
10413   (gnus-summary-toggle-header arg)
10414   (setq gnus-show-all-headers
10415         (cond ((or (not (numberp arg))
10416                    (zerop arg))
10417                (not gnus-show-all-headers))
10418               ((natnump arg)
10419                t))))
10420
10421 (defun gnus-summary-toggle-header (&optional arg)
10422   "Show the headers if they are hidden, or hide them if they are shown.
10423 If ARG is a positive number, show the entire header.
10424 If ARG is a negative number, hide the unwanted header lines."
10425   (interactive "P")
10426   (gnus-set-global-variables)
10427   (save-excursion
10428     (set-buffer gnus-article-buffer)
10429     (let* ((buffer-read-only nil)
10430            (inhibit-point-motion-hooks t) 
10431            (hidden (text-property-any 
10432                     (goto-char (point-min)) (search-forward "\n\n")
10433                     'invisible t))
10434            e)
10435       (goto-char (point-min))
10436       (when (search-forward "\n\n" nil t)
10437         (delete-region (point-min) (1- (point))))
10438       (goto-char (point-min))
10439       (save-excursion 
10440         (set-buffer gnus-original-article-buffer)
10441         (goto-char (point-min))
10442         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
10443       (insert-buffer-substring gnus-original-article-buffer 1 e)
10444       (let ((gnus-inhibit-hiding t))
10445         (run-hooks 'gnus-article-display-hook))
10446       (if (or (not hidden) (and (numberp arg) (< arg 0)))
10447           (gnus-article-hide-headers)))))
10448
10449 (defun gnus-summary-show-all-headers ()
10450   "Make all header lines visible."
10451   (interactive)
10452   (gnus-set-global-variables)
10453   (gnus-article-show-all-headers))
10454
10455 (defun gnus-summary-toggle-mime (&optional arg)
10456   "Toggle MIME processing.
10457 If ARG is a positive number, turn MIME processing on."
10458   (interactive "P")
10459   (gnus-set-global-variables)
10460   (setq gnus-show-mime
10461         (if (null arg) (not gnus-show-mime)
10462           (> (prefix-numeric-value arg) 0)))
10463   (gnus-summary-select-article t 'force))
10464
10465 (defun gnus-summary-caesar-message (&optional arg)
10466   "Caesar rotate the current article by 13.
10467 The numerical prefix specifies how manu places to rotate each letter
10468 forward."
10469   (interactive "P")
10470   (gnus-set-global-variables)
10471   (gnus-summary-select-article)
10472   (let ((mail-header-separator ""))
10473     (gnus-eval-in-buffer-window 
10474      gnus-article-buffer
10475      (save-restriction
10476        (widen)
10477        (let ((start (window-start)))
10478          (news-caesar-buffer-body arg)
10479          (set-window-start (get-buffer-window (current-buffer)) start))))))
10480
10481 (defun gnus-summary-stop-page-breaking ()
10482   "Stop page breaking in the current article."
10483   (interactive)
10484   (gnus-set-global-variables)
10485   (gnus-summary-select-article)
10486   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
10487
10488 ;; Suggested by Brian Edmonds <bedmonds@prodigy.bc.ca>.
10489
10490 (defun gnus-summary-move-article (&optional n to-newsgroup select-method)
10491   "Move the current article to a different newsgroup.
10492 If N is a positive number, move the N next articles.
10493 If N is a negative number, move the N previous articles.
10494 If N is nil and any articles have been marked with the process mark,
10495 move those articles instead.
10496 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
10497 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
10498 re-spool using this method.
10499 For this function to work, both the current newsgroup and the
10500 newsgroup that you want to move to have to support the `request-move'
10501 and `request-accept' functions. (Ie. mail newsgroups at present.)"
10502   (interactive "P")
10503   (gnus-set-global-variables)
10504   (or (gnus-check-backend-function 'request-move-article gnus-newsgroup-name)
10505       (error "The current newsgroup does not support article moving"))
10506   (let ((articles (gnus-summary-work-articles n))
10507         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
10508         art-group to-method sel-met)
10509     (if (and (not to-newsgroup) (not select-method))
10510         (setq to-newsgroup
10511               (completing-read 
10512                (format "Where do you want to move %s? %s"
10513                        (if (> (length articles) 1)
10514                            (format "these %d articles" (length articles))
10515                          "this article")
10516                        (if gnus-current-move-group
10517                            (format "(default %s) " gnus-current-move-group)
10518                          ""))
10519                gnus-active-hashtb nil nil prefix)))
10520     (if to-newsgroup
10521         (progn
10522           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
10523               (setq to-newsgroup (or gnus-current-move-group "")))
10524           (or (gnus-active to-newsgroup)
10525               (gnus-activate-group to-newsgroup)
10526               (error "No such group: %s" to-newsgroup))
10527           (setq gnus-current-move-group to-newsgroup)))
10528     (setq to-method (if select-method (list select-method "")
10529                       (gnus-find-method-for-group to-newsgroup)))
10530     (or (gnus-check-backend-function 'request-accept-article (car to-method))
10531         (error "%s does not support article copying" (car to-method)))
10532     (or (gnus-check-server to-method)
10533         (error "Can't open server %s" (car to-method)))
10534     (gnus-message 6 "Moving to %s: %s..." 
10535                   (or select-method to-newsgroup) articles)
10536     (while articles
10537       (if (setq art-group
10538                 (gnus-request-move-article 
10539                  (car articles)         ; Article to move
10540                  gnus-newsgroup-name    ; From newsgrouo
10541                  (nth 1 (gnus-find-method-for-group 
10542                          gnus-newsgroup-name)) ; Server
10543                  (list 'gnus-request-accept-article 
10544                        (if select-method
10545                            (list 'quote select-method)
10546                          to-newsgroup)
10547                        (not (cdr articles))) ; Accept form
10548                  (not (cdr articles)))) ; Only save nov last time
10549           (let* ((buffer-read-only nil)
10550                  (entry 
10551                   (or
10552                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
10553                    (gnus-gethash 
10554                     (gnus-group-prefixed-name 
10555                      (car art-group) 
10556                      (if select-method (list select-method "")
10557                        (gnus-find-method-for-group to-newsgroup)))
10558                     gnus-newsrc-hashtb)))
10559                  (info (nth 2 entry))
10560                  (article (car articles)))
10561             ;; Update the group that has been moved to.
10562             (if (not info)
10563                 ()                      ; This group does not exist yet.
10564               (unless (memq article gnus-newsgroup-unreads)
10565                 (gnus-info-set-read 
10566                  info (gnus-add-to-range (gnus-info-read info) 
10567                                          (list (cdr art-group)))))
10568
10569               ;; Copy any marks over to the new group.
10570               (let ((marks '((tick . gnus-newsgroup-marked)
10571                              (dormant . gnus-newsgroup-dormant)
10572                              (expire . gnus-newsgroup-expirable)
10573                              (bookmark . gnus-newsgroup-bookmarks)
10574                              (save . gnus-newsgroup-saved)
10575                              (reply . gnus-newsgroup-replied)))
10576                     (to-article (cdr art-group)))
10577
10578                 ;; See whether the article is to be put in the cache.
10579                 (when gnus-use-cache
10580                   (gnus-cache-possibly-enter-article 
10581                    (gnus-info-group info) to-article
10582                    (let ((header (copy-sequence
10583                                   (gnus-summary-article-header article))))
10584                      (mail-header-set-number header to-article)
10585                      header)
10586                    (memq article gnus-newsgroup-marked)
10587                    (memq article gnus-newsgroup-dormant)
10588                    (memq article gnus-newsgroup-unreads)))
10589
10590                 (while marks
10591                   (if (memq article (symbol-value (cdr (car marks))))
10592                       (gnus-add-marked-articles 
10593                        (gnus-info-group info) (car (car marks))
10594                        (list to-article) info))
10595                   (setq marks (cdr marks)))))
10596             (gnus-summary-goto-subject article)
10597             (gnus-summary-mark-article article gnus-canceled-mark))
10598         (gnus-message 1 "Couldn't move article %s" (car articles)))
10599       (gnus-summary-remove-process-mark (car articles))
10600       (setq articles (cdr articles)))
10601     (gnus-set-mode-line 'summary)))
10602
10603 (defun gnus-summary-respool-article (&optional n respool-method)
10604   "Respool the current article.
10605 The article will be squeezed through the mail spooling process again,
10606 which means that it will be put in some mail newsgroup or other
10607 depending on `nnmail-split-methods'.
10608 If N is a positive number, respool the N next articles.
10609 If N is a negative number, respool the N previous articles.
10610 If N is nil and any articles have been marked with the process mark,
10611 respool those articles instead.
10612
10613 Respooling can be done both from mail groups and \"real\" newsgroups.
10614 In the former case, the articles in question will be moved from the
10615 current group into whatever groups they are destined to.  In the
10616 latter case, they will be copied into the relevant groups."
10617   (interactive "P")
10618   (gnus-set-global-variables)
10619   (let ((respool-methods (gnus-methods-using 'respool))
10620         (methname 
10621          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
10622     (or respool-method
10623         (setq respool-method
10624               (completing-read
10625                "What method do you want to use when respooling? "
10626                respool-methods nil t methname)))
10627     (or (string= respool-method "")
10628         (if (assoc (symbol-name
10629                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
10630                    respool-methods)
10631             (gnus-summary-move-article n nil (intern respool-method))
10632           (gnus-summary-copy-article n nil (intern respool-method))))))
10633
10634 ;; Suggested by gregj@unidata.com (Gregory J. Grubbs).
10635 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
10636   "Move the current article to a different newsgroup.
10637 If N is a positive number, move the N next articles.
10638 If N is a negative number, move the N previous articles.
10639 If N is nil and any articles have been marked with the process mark,
10640 move those articles instead.
10641 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to. 
10642 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
10643 re-spool using this method.
10644 For this function to work, the newsgroup that you want to move to have
10645 to support the `request-move' and `request-accept'
10646 functions. (Ie. mail newsgroups at present.)"
10647   (interactive "P")
10648   (gnus-set-global-variables)
10649   (let ((articles (gnus-summary-work-articles n))
10650         (copy-buf (get-buffer-create "*copy work*"))
10651         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
10652         art-group to-method)
10653     (buffer-disable-undo copy-buf)
10654     (if (and (not to-newsgroup) (not select-method))
10655         (setq to-newsgroup
10656               (completing-read 
10657                (format "Where do you want to copy %s? %s"
10658                        (if (> (length articles) 1)
10659                            (format "these %d articles" (length articles))
10660                          "this article")
10661                        (if gnus-current-move-group
10662                            (format "(default %s) " gnus-current-move-group)
10663                          ""))
10664                gnus-active-hashtb nil nil prefix)))
10665     (if to-newsgroup
10666         (progn
10667           (if (or (string= to-newsgroup "") (string= to-newsgroup prefix))
10668               (setq to-newsgroup (or gnus-current-move-group "")))
10669           (or (gnus-active to-newsgroup)
10670               (gnus-activate-group to-newsgroup)
10671               (error "No such group: %s" to-newsgroup))
10672           (setq gnus-current-move-group to-newsgroup)))
10673     (setq to-method (if select-method (list select-method "")
10674                       (gnus-find-method-for-group to-newsgroup)))
10675     (or (gnus-check-backend-function 'request-accept-article (car to-method))
10676         (error "%s does not support article copying" (car to-method)))
10677     (or (gnus-check-server to-method)
10678         (error "Can't open server %s" (car to-method)))
10679     (while articles
10680       (gnus-message 6 "Copying to %s: %s..." 
10681                     (or select-method to-newsgroup) articles)
10682       (if (setq art-group
10683                 (save-excursion
10684                   (set-buffer copy-buf)
10685                   (gnus-request-article-this-buffer
10686                    (car articles) gnus-newsgroup-name)
10687                   (gnus-request-accept-article
10688                    (if select-method select-method to-newsgroup)
10689                    (not (cdr articles)))))
10690           (let* ((entry 
10691                   (or
10692                    (gnus-gethash (car art-group) gnus-newsrc-hashtb)
10693                    (gnus-gethash 
10694                     (gnus-group-prefixed-name 
10695                      (car art-group) 
10696                      (if select-method (list select-method "")
10697                        (gnus-find-method-for-group to-newsgroup)))
10698                     gnus-newsrc-hashtb)))
10699                  (info (nth 2 entry))
10700                  (article (car articles)))
10701             ;; We copy the info over to the new group.
10702             (if (not info)
10703                 ()                      ; This group does not exist (yet).
10704               (if (not (memq article gnus-newsgroup-unreads))
10705                   (gnus-info-set-read 
10706                    info (gnus-add-to-range (gnus-info-read info) 
10707                                            (list (cdr art-group)))))
10708
10709               ;; Copy any marks over to the new group.
10710               (let ((marks '((tick . gnus-newsgroup-marked)
10711                              (dormant . gnus-newsgroup-dormant)
10712                              (expire . gnus-newsgroup-expirable)
10713                              (bookmark . gnus-newsgroup-bookmarks)
10714                              (save . gnus-newsgroup-saved)
10715                              (reply . gnus-newsgroup-replied)))
10716                     (to-article (cdr art-group)))
10717
10718               ;; See whether the article is to be put in the cache.
10719               (when gnus-use-cache
10720                 (gnus-cache-possibly-enter-article 
10721                  (gnus-info-group info) to-article 
10722                  (let ((header (copy-sequence
10723                                 (gnus-summary-article-header article))))
10724                    (mail-header-set-number header to-article)
10725                    header)
10726                  (memq article gnus-newsgroup-marked)
10727                  (memq article gnus-newsgroup-dormant)
10728                  (memq article gnus-newsgroup-unreads)))
10729
10730               (while marks
10731                 (if (memq article (symbol-value (cdr (car marks))))
10732                     (gnus-add-marked-articles 
10733                      (gnus-info-group info) (car (car marks)) 
10734                      (list to-article) info))
10735                 (setq marks (cdr marks))))))
10736         (gnus-message 1 "Couldn't copy article %s" (car articles)))
10737       (gnus-summary-remove-process-mark (car articles))
10738       (setq articles (cdr articles)))
10739     (kill-buffer copy-buf)))
10740
10741 (defun gnus-summary-import-article (file)
10742   "Import a random file into a mail newsgroup."
10743   (interactive "fImport file: ")
10744   (gnus-set-global-variables)
10745   (let ((group gnus-newsgroup-name)
10746         atts lines)
10747     (or (gnus-check-backend-function 'request-accept-article group)
10748         (error "%s does not support article importing" group))
10749     (or (file-readable-p file)
10750         (not (file-regular-p file))
10751         (error "Can't read %s" file))
10752     (save-excursion
10753       (set-buffer (get-buffer-create " *import file*"))
10754       (buffer-disable-undo (current-buffer))
10755       (erase-buffer)
10756       (insert-file-contents file)
10757       (goto-char (point-min))
10758       (if (nnheader-article-p)
10759           ()
10760         (setq atts (file-attributes file)
10761               lines (count-lines (point-min) (point-max)))
10762         (insert "From: " (read-string "From: ") "\n"
10763                 "Subject: " (read-string "Subject: ") "\n"
10764                 "Date: " (current-time-string (nth 5 atts)) "\n"
10765                 "Message-ID: " (gnus-inews-message-id) "\n"
10766                 "Lines: " (int-to-string lines) "\n"
10767                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
10768       (gnus-request-accept-article group t)
10769       (kill-buffer (current-buffer)))))
10770
10771 (defun gnus-summary-expire-articles ()
10772   "Expire all articles that are marked as expirable in the current group."
10773   (interactive)
10774   (gnus-set-global-variables)
10775   (when (gnus-check-backend-function 
10776          'request-expire-articles gnus-newsgroup-name)
10777     ;; This backend supports expiry.
10778     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
10779            (expirable (if total
10780                           (gnus-list-of-read-articles gnus-newsgroup-name)
10781                         (setq gnus-newsgroup-expirable
10782                               (sort gnus-newsgroup-expirable '<))))
10783            (expiry-wait (gnus-group-get-parameter 
10784                          gnus-newsgroup-name 'expiry-wait))
10785            es)
10786       (when expirable
10787         ;; There are expirable articles in this group, so we run them
10788         ;; through the expiry process.
10789         (gnus-message 6 "Expiring articles...")
10790         ;; The list of articles that weren't expired is returned.
10791         (if expiry-wait
10792             (let ((nnmail-expiry-wait-function nil)
10793                   (nnmail-expiry-wait expiry-wait))
10794               (setq es (gnus-request-expire-articles
10795                         expirable gnus-newsgroup-name)))
10796           (setq es (gnus-request-expire-articles
10797                     expirable gnus-newsgroup-name)))
10798         (or total (setq gnus-newsgroup-expirable es))
10799         ;; We go through the old list of expirable, and mark all
10800         ;; really expired articles as non-existant.
10801         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
10802           (let ((gnus-use-cache nil))
10803             (while expirable
10804               (unless (memq (car expirable) es)
10805                 (when (gnus-data-find (car expirable))
10806                   (gnus-summary-mark-article
10807                    (car expirable) gnus-canceled-mark)))
10808               (setq expirable (cdr expirable)))))
10809         (gnus-message 6 "Expiring articles...done")))))
10810
10811 (defun gnus-summary-expire-articles-now ()
10812   "Expunge all expirable articles in the current group.
10813 This means that *all* articles that are marked as expirable will be
10814 deleted forever, right now."
10815   (interactive)
10816   (gnus-set-global-variables)
10817   (or gnus-expert-user
10818       (gnus-y-or-n-p
10819        "Are you really, really, really sure you want to expunge? ")
10820       (error "Phew!"))
10821   (let ((nnmail-expiry-wait 'immediate)
10822         (nnmail-expiry-wait-function nil))
10823     (gnus-summary-expire-articles)))
10824
10825 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
10826 (defun gnus-summary-delete-article (&optional n)
10827   "Delete the N next (mail) articles.
10828 This command actually deletes articles.  This is not a marking
10829 command.  The article will disappear forever from your life, never to
10830 return. 
10831 If N is negative, delete backwards.
10832 If N is nil and articles have been marked with the process mark,
10833 delete these instead."
10834   (interactive "P")
10835   (gnus-set-global-variables)
10836   (or (gnus-check-backend-function 'request-expire-articles 
10837                                    gnus-newsgroup-name)
10838       (error "The current newsgroup does not support article deletion."))
10839   ;; Compute the list of articles to delete.
10840   (let ((articles (gnus-summary-work-articles n))
10841         not-deleted)
10842     (if (and gnus-novice-user
10843              (not (gnus-y-or-n-p 
10844                    (format "Do you really want to delete %s forever? "
10845                            (if (> (length articles) 1) "these articles"
10846                              "this article")))))
10847         ()
10848       ;; Delete the articles.
10849       (setq not-deleted (gnus-request-expire-articles 
10850                          articles gnus-newsgroup-name 'force))
10851       (while articles
10852         (gnus-summary-remove-process-mark (car articles))       
10853         ;; The backend might not have been able to delete the article
10854         ;; after all.  
10855         (or (memq (car articles) not-deleted)
10856             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
10857         (setq articles (cdr articles))))
10858     (gnus-summary-position-point)
10859     (gnus-set-mode-line 'summary)
10860     not-deleted))
10861
10862 (defun gnus-summary-edit-article (&optional force)
10863   "Enter into a buffer and edit the current article.
10864 This will have permanent effect only in mail groups.
10865 If FORCE is non-nil, allow editing of articles even in read-only
10866 groups."
10867   (interactive "P")
10868   (gnus-set-global-variables)
10869   (when (and (not force)
10870              (gnus-group-read-only-p))
10871     (error "The current newsgroup does not support article editing."))
10872   (gnus-summary-select-article t nil t)
10873   (gnus-configure-windows 'article)
10874   (select-window (get-buffer-window gnus-article-buffer))
10875   (gnus-message 6 "C-c C-c to end edits")
10876   (setq buffer-read-only nil)
10877   (text-mode)
10878   (use-local-map (copy-keymap (current-local-map)))
10879   (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
10880   (buffer-enable-undo)
10881   (widen)
10882   (goto-char (point-min))
10883   (search-forward "\n\n" nil t))
10884
10885 (defun gnus-summary-edit-article-done ()
10886   "Make edits to the current article permanent."
10887   (interactive)
10888   (if (gnus-group-read-only-p)
10889       (progn
10890         (gnus-summary-edit-article-postpone)
10891         (message "The current newsgroup does not support article editing.")
10892         (ding))
10893     (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
10894       (erase-buffer)
10895       (insert buf)
10896       (if (not (gnus-request-replace-article 
10897                 (cdr gnus-article-current) (car gnus-article-current) 
10898                 (current-buffer)))
10899           (error "Couldn't replace article.")
10900         (gnus-article-mode)
10901         (use-local-map gnus-article-mode-map)
10902         (setq buffer-read-only t)
10903         (buffer-disable-undo (current-buffer))
10904         (gnus-configure-windows 'summary))
10905       (and (gnus-visual-p 'summary-highlight 'highlight)
10906            (run-hooks 'gnus-visual-mark-article-hook)))))
10907
10908 (defun gnus-summary-edit-article-postpone ()
10909   "Postpone changes to the current article."
10910   (interactive)
10911   (gnus-article-mode)
10912   (use-local-map gnus-article-mode-map)
10913   (setq buffer-read-only t)
10914   (buffer-disable-undo (current-buffer))
10915   (gnus-configure-windows 'summary)
10916   (and (gnus-visual-p 'summary-highlight 'highlight)
10917        (run-hooks 'gnus-visual-mark-article-hook)))
10918
10919 (defun gnus-summary-respool-query ()
10920   "Query where the respool algorithm would put this article."
10921   (interactive)
10922   (gnus-set-global-variables)
10923   (gnus-summary-select-article)
10924   (save-excursion
10925     (set-buffer gnus-article-buffer)
10926     (save-restriction
10927       (goto-char (point-min))
10928       (search-forward "\n\n")
10929       (narrow-to-region (point-min) (point))
10930       (pp-eval-expression
10931        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
10932
10933 ;; Summary score commands.
10934
10935 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
10936
10937 (defun gnus-summary-raise-score (n)
10938   "Raise the score of the current article by N."
10939   (interactive "p")
10940   (gnus-set-global-variables)
10941   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
10942
10943 (defun gnus-summary-set-score (n)
10944   "Set the score of the current article to N."
10945   (interactive "p")
10946   (gnus-set-global-variables)
10947   (save-excursion
10948     (gnus-summary-show-thread)
10949     (let ((buffer-read-only nil))
10950       ;; Set score.
10951       (gnus-summary-update-mark
10952        (if (= n (or gnus-summary-default-score 0)) ? 
10953          (if (< n (or gnus-summary-default-score 0)) 
10954              gnus-score-below-mark gnus-score-over-mark)) 'score))
10955     (let* ((article (gnus-summary-article-number))
10956            (score (assq article gnus-newsgroup-scored)))
10957       (if score (setcdr score n)
10958         (setq gnus-newsgroup-scored 
10959               (cons (cons article n) gnus-newsgroup-scored))))
10960     (gnus-summary-update-line)))
10961
10962 (defun gnus-summary-current-score ()
10963   "Return the score of the current article."
10964   (interactive)
10965   (gnus-set-global-variables)
10966   (message "%s" (gnus-summary-article-score)))
10967
10968 ;; Summary marking commands.
10969
10970 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
10971   "Mark articles which has the same subject as read, and then select the next.
10972 If UNMARK is positive, remove any kind of mark.
10973 If UNMARK is negative, tick articles."
10974   (interactive "P")
10975   (gnus-set-global-variables)
10976   (if unmark
10977       (setq unmark (prefix-numeric-value unmark)))
10978   (let ((count
10979          (gnus-summary-mark-same-subject
10980           (gnus-summary-article-subject) unmark)))
10981     ;; Select next unread article.  If auto-select-same mode, should
10982     ;; select the first unread article.
10983     (gnus-summary-next-article t (and gnus-auto-select-same
10984                                       (gnus-summary-article-subject)))
10985     (gnus-message 7 "%d article%s marked as %s"
10986                   count (if (= count 1) " is" "s are")
10987                   (if unmark "unread" "read"))))
10988
10989 (defun gnus-summary-kill-same-subject (&optional unmark)
10990   "Mark articles which has the same subject as read. 
10991 If UNMARK is positive, remove any kind of mark.
10992 If UNMARK is negative, tick articles."
10993   (interactive "P")
10994   (gnus-set-global-variables)
10995   (if unmark
10996       (setq unmark (prefix-numeric-value unmark)))
10997   (let ((count
10998          (gnus-summary-mark-same-subject
10999           (gnus-summary-article-subject) unmark)))
11000     ;; If marked as read, go to next unread subject.
11001     (if (null unmark)
11002         ;; Go to next unread subject.
11003         (gnus-summary-next-subject 1 t))
11004     (gnus-message 7 "%d articles are marked as %s"
11005                   count (if unmark "unread" "read"))))
11006
11007 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11008   "Mark articles with same SUBJECT as read, and return marked number.
11009 If optional argument UNMARK is positive, remove any kinds of marks.
11010 If optional argument UNMARK is negative, mark articles as unread instead."
11011   (let ((count 1))
11012     (save-excursion
11013       (cond 
11014        ((null unmark)                   ; Mark as read.
11015         (while (and 
11016                 (progn
11017                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11018                   (gnus-summary-show-thread) t)
11019                 (gnus-summary-find-subject subject))
11020           (setq count (1+ count))))
11021        ((> unmark 0)                    ; Tick.
11022         (while (and
11023                 (progn
11024                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11025                   (gnus-summary-show-thread) t)
11026                 (gnus-summary-find-subject subject))
11027           (setq count (1+ count))))
11028        (t                               ; Mark as unread.
11029         (while (and
11030                 (progn
11031                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11032                   (gnus-summary-show-thread) t)
11033                 (gnus-summary-find-subject subject))
11034           (setq count (1+ count)))))
11035       (gnus-set-mode-line 'summary)
11036       ;; Return the number of marked articles.
11037       count)))
11038
11039 (defun gnus-summary-mark-as-processable (n &optional unmark)
11040   "Set the process mark on the next N articles.
11041 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11042 the process mark instead.  The difference between N and the actual
11043 number of articles marked is returned."
11044   (interactive "p")
11045   (gnus-set-global-variables)
11046   (let ((backward (< n 0))
11047         (n (abs n)))
11048     (while (and 
11049             (> n 0)
11050             (if unmark
11051                 (gnus-summary-remove-process-mark
11052                  (gnus-summary-article-number))
11053               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11054             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11055       (setq n (1- n)))
11056     (if (/= 0 n) (gnus-message 7 "No more articles"))
11057     (gnus-summary-recenter)
11058     (gnus-summary-position-point)
11059     n))
11060
11061 (defun gnus-summary-unmark-as-processable (n)
11062   "Remove the process mark from the next N articles.
11063 If N is negative, mark backward instead.  The difference between N and
11064 the actual number of articles marked is returned."
11065   (interactive "p")
11066   (gnus-set-global-variables)
11067   (gnus-summary-mark-as-processable n t))
11068
11069 (defun gnus-summary-unmark-all-processable ()
11070   "Remove the process mark from all articles."
11071   (interactive)
11072   (gnus-set-global-variables)
11073   (save-excursion
11074     (while gnus-newsgroup-processable
11075       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11076   (gnus-summary-position-point))
11077
11078 (defun gnus-summary-mark-as-expirable (n)
11079   "Mark N articles forward as expirable.
11080 If N is negative, mark backward instead.  The difference between N and
11081 the actual number of articles marked is returned."
11082   (interactive "p")
11083   (gnus-set-global-variables)
11084   (gnus-summary-mark-forward n gnus-expirable-mark))
11085
11086 (defun gnus-summary-mark-article-as-replied (article)
11087   "Mark ARTICLE replied and update the summary line."
11088   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11089   (let ((buffer-read-only nil))
11090     (when (gnus-summary-goto-subject article)
11091       (gnus-summary-update-secondary-mark article))))
11092
11093 (defun gnus-summary-set-bookmark (article)
11094   "Set a bookmark in current article."
11095   (interactive (list (gnus-summary-article-number)))
11096   (gnus-set-global-variables)
11097   (if (or (not (get-buffer gnus-article-buffer))
11098           (not gnus-current-article)
11099           (not gnus-article-current)
11100           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11101       (error "No current article selected"))
11102   ;; Remove old bookmark, if one exists.
11103   (let ((old (assq article gnus-newsgroup-bookmarks)))
11104     (if old (setq gnus-newsgroup-bookmarks 
11105                   (delq old gnus-newsgroup-bookmarks))))
11106   ;; Set the new bookmark, which is on the form 
11107   ;; (article-number . line-number-in-body).
11108   (setq gnus-newsgroup-bookmarks 
11109         (cons 
11110          (cons article 
11111                (save-excursion
11112                  (set-buffer gnus-article-buffer)
11113                  (count-lines
11114                   (min (point)
11115                        (save-excursion
11116                          (goto-char (point-min))
11117                          (search-forward "\n\n" nil t)
11118                          (point)))
11119                   (point))))
11120          gnus-newsgroup-bookmarks))
11121   (gnus-message 6 "A bookmark has been added to the current article."))
11122
11123 (defun gnus-summary-remove-bookmark (article)
11124   "Remove the bookmark from the current article."
11125   (interactive (list (gnus-summary-article-number)))
11126   (gnus-set-global-variables)
11127   ;; Remove old bookmark, if one exists.
11128   (let ((old (assq article gnus-newsgroup-bookmarks)))
11129     (if old 
11130         (progn
11131           (setq gnus-newsgroup-bookmarks 
11132                 (delq old gnus-newsgroup-bookmarks))
11133           (gnus-message 6 "Removed bookmark."))
11134       (gnus-message 6 "No bookmark in current article."))))
11135
11136 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11137 (defun gnus-summary-mark-as-dormant (n)
11138   "Mark N articles forward as dormant.
11139 If N is negative, mark backward instead.  The difference between N and
11140 the actual number of articles marked is returned."
11141   (interactive "p")
11142   (gnus-set-global-variables)
11143   (gnus-summary-mark-forward n gnus-dormant-mark))
11144
11145 (defun gnus-summary-set-process-mark (article)
11146   "Set the process mark on ARTICLE and update the summary line."
11147   (setq gnus-newsgroup-processable 
11148         (cons article 
11149               (delq article gnus-newsgroup-processable)))
11150   (when (gnus-summary-goto-subject article)
11151     (gnus-summary-show-thread)
11152     (gnus-summary-update-secondary-mark article)))
11153
11154 (defun gnus-summary-remove-process-mark (article)
11155   "Remove the process mark from ARTICLE and update the summary line."
11156   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11157   (when (gnus-summary-goto-subject article)
11158     (gnus-summary-show-thread)
11159     (gnus-summary-update-secondary-mark article)))
11160
11161 (defun gnus-summary-set-saved-mark (article)
11162   "Set the process mark on ARTICLE and update the summary line."
11163   (push article gnus-newsgroup-saved)
11164   (when (gnus-summary-goto-subject article)
11165     (gnus-summary-update-secondary-mark article)))
11166
11167 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11168   "Mark N articles as read forwards.
11169 If N is negative, mark backwards instead.
11170 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11171 marked as unread. 
11172 The difference between N and the actual number of articles marked is
11173 returned."
11174   (interactive "p")
11175   (gnus-set-global-variables)
11176   (let ((backward (< n 0))
11177         (gnus-summary-goto-unread
11178          (and gnus-summary-goto-unread
11179               (not (memq mark (list gnus-unread-mark
11180                                     gnus-ticked-mark gnus-dormant-mark)))))
11181         (n (abs n))
11182         (mark (or mark gnus-del-mark)))
11183     (while (and (> n 0)
11184                 (gnus-summary-mark-article nil mark no-expire)
11185                 (zerop (gnus-summary-next-subject 
11186                         (if backward -1 1) gnus-summary-goto-unread t)))
11187       (setq n (1- n)))
11188     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11189     (gnus-summary-recenter)
11190     (gnus-summary-position-point)
11191     (gnus-set-mode-line 'summary)
11192     n))
11193
11194 (defun gnus-summary-mark-article-as-read (mark)
11195   "Mark the current article quickly as read with MARK."
11196   (let ((article (gnus-summary-article-number)))
11197     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11198     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11199     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11200     (setq gnus-newsgroup-reads
11201           (cons (cons article mark) gnus-newsgroup-reads))
11202     ;; Possibly remove from cache, if that is used. 
11203     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11204     (and gnus-newsgroup-auto-expire 
11205          (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11206              (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11207              (= mark gnus-read-mark) (= mark gnus-souped-mark))
11208          (progn
11209            (setq mark gnus-expirable-mark)
11210            (setq gnus-newsgroup-expirable 
11211                  (cons article gnus-newsgroup-expirable))))
11212     ;; Fix the mark.
11213     (gnus-summary-update-mark mark 'unread)
11214     t))
11215
11216 (defun gnus-summary-mark-article-as-unread (mark)
11217   "Mark the current article quickly as unread with MARK."
11218   (let ((article (gnus-summary-article-number)))
11219     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11220     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11221     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11222     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11223     (cond ((= mark gnus-ticked-mark)
11224            (push article gnus-newsgroup-marked))
11225           ((= mark gnus-dormant-mark)
11226            (push article gnus-newsgroup-dormant))
11227           (t     
11228            (push article gnus-newsgroup-unreads)))
11229     (setq gnus-newsgroup-reads
11230           (delq (assq article gnus-newsgroup-reads)
11231                 gnus-newsgroup-reads))
11232
11233     ;; See whether the article is to be put in the cache.
11234     (and gnus-use-cache
11235          (vectorp (gnus-summary-article-header article))
11236          (save-excursion
11237            (gnus-cache-possibly-enter-article 
11238             gnus-newsgroup-name article 
11239             (gnus-summary-article-header article)
11240             (= mark gnus-ticked-mark)
11241             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11242
11243     ;; Fix the mark.
11244     (gnus-summary-update-mark mark 'unread)
11245     t))
11246
11247 (defun gnus-summary-mark-article (&optional article mark no-expire)
11248   "Mark ARTICLE with MARK.  MARK can be any character.
11249 Four MARK strings are reserved: `? ' (unread), `?!' (ticked), 
11250 `??' (dormant) and `?E' (expirable).
11251 If MARK is nil, then the default character `?D' is used.
11252 If ARTICLE is nil, then the article on the current line will be
11253 marked." 
11254   ;; The mark might be a string.
11255   (and (stringp mark)
11256        (setq mark (aref mark 0)))
11257   ;; If no mark is given, then we check auto-expiring.
11258   (and (not no-expire)
11259        gnus-newsgroup-auto-expire 
11260        (or (not mark)
11261            (and (numberp mark) 
11262                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11263                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11264                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11265        (setq mark gnus-expirable-mark))
11266   (let* ((mark (or mark gnus-del-mark))
11267          (article (or article (gnus-summary-article-number))))
11268     (or article (error "No article on current line"))
11269     (if (or (= mark gnus-unread-mark) 
11270             (= mark gnus-ticked-mark) 
11271             (= mark gnus-dormant-mark))
11272         (gnus-mark-article-as-unread article mark)
11273       (gnus-mark-article-as-read article mark))
11274
11275     ;; See whether the article is to be put in the cache.
11276     (and gnus-use-cache
11277          (not (= mark gnus-canceled-mark))
11278          (vectorp (gnus-summary-article-header article))
11279          (save-excursion
11280            (gnus-cache-possibly-enter-article 
11281             gnus-newsgroup-name article 
11282             (gnus-summary-article-header article)
11283             (= mark gnus-ticked-mark)
11284             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11285
11286     (if (gnus-summary-goto-subject article nil t)
11287         (let ((buffer-read-only nil))
11288           (gnus-summary-show-thread)
11289           ;; Fix the mark.
11290           (gnus-summary-update-mark mark 'unread)
11291           t))))
11292
11293 (defun gnus-summary-update-secondary-mark (article)
11294   "Update the secondary (read, process, cache) mark."
11295   (gnus-summary-update-mark
11296    (cond ((memq article gnus-newsgroup-processable)
11297           gnus-process-mark)
11298          ((memq article gnus-newsgroup-cached)
11299           gnus-cached-mark)
11300          ((memq article gnus-newsgroup-replied)
11301           gnus-replied-mark)
11302          ((memq article gnus-newsgroup-saved)
11303           gnus-saved-mark)
11304          (t gnus-unread-mark))
11305    'replied)
11306   (when (gnus-visual-p 'summary-highlight 'highlight)
11307     (run-hooks 'gnus-summary-update-hook))
11308   t)
11309
11310 (defun gnus-summary-update-mark (mark type)
11311   (beginning-of-line)
11312   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11313         (buffer-read-only nil))
11314     (when forward
11315       ;; Go to the right position on the line.
11316       (forward-char forward)
11317       ;; Replace the old mark with the new mark.
11318       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11319       ;; Optionally update the marks by some user rule.
11320       (when (eq type 'unread)
11321         (gnus-data-set-mark 
11322          (gnus-data-find (gnus-summary-article-number)) mark)
11323         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11324   
11325 (defun gnus-mark-article-as-read (article &optional mark)
11326   "Enter ARTICLE in the pertinent lists and remove it from others."
11327   ;; Make the article expirable.
11328   (let ((mark (or mark gnus-del-mark)))
11329     (if (= mark gnus-expirable-mark)
11330         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
11331       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
11332     ;; Remove from unread and marked lists.
11333     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11334     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11335     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11336     (push (cons article mark) gnus-newsgroup-reads)
11337     ;; Possibly remove from cache, if that is used. 
11338     (when gnus-use-cache 
11339       (gnus-cache-enter-remove-article article))))
11340
11341 (defun gnus-mark-article-as-unread (article &optional mark)
11342   "Enter ARTICLE in the pertinent lists and remove it from others."
11343   (let ((mark (or mark gnus-ticked-mark)))
11344     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11345     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11346     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11347     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11348     (cond ((= mark gnus-ticked-mark)
11349            (push article gnus-newsgroup-marked))
11350           ((= mark gnus-dormant-mark)
11351            (push article gnus-newsgroup-dormant))
11352           (t     
11353            (push article gnus-newsgroup-unreads)))
11354     (setq gnus-newsgroup-reads
11355           (delq (assq article gnus-newsgroup-reads)
11356                 gnus-newsgroup-reads))))
11357
11358 (defalias 'gnus-summary-mark-as-unread-forward 
11359   'gnus-summary-tick-article-forward)
11360 (make-obsolete 'gnus-summary-mark-as-unread-forward 
11361                'gnus-summary-tick-article-forward)
11362 (defun gnus-summary-tick-article-forward (n)
11363   "Tick N articles forwards.
11364 If N is negative, tick backwards instead.
11365 The difference between N and the number of articles ticked is returned."
11366   (interactive "p")
11367   (gnus-summary-mark-forward n gnus-ticked-mark))
11368
11369 (defalias 'gnus-summary-mark-as-unread-backward 
11370   'gnus-summary-tick-article-backward)
11371 (make-obsolete 'gnus-summary-mark-as-unread-backward 
11372                'gnus-summary-tick-article-backward)
11373 (defun gnus-summary-tick-article-backward (n)
11374   "Tick N articles backwards.
11375 The difference between N and the number of articles ticked is returned."
11376   (interactive "p")
11377   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11378
11379 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11380 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11381 (defun gnus-summary-tick-article (&optional article clear-mark)
11382   "Mark current article as unread.
11383 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11384 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11385   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11386                                        gnus-ticked-mark)))
11387
11388 (defun gnus-summary-mark-as-read-forward (n)
11389   "Mark N articles as read forwards.
11390 If N is negative, mark backwards instead.
11391 The difference between N and the actual number of articles marked is
11392 returned."
11393   (interactive "p")
11394   (gnus-summary-mark-forward n gnus-del-mark t))
11395
11396 (defun gnus-summary-mark-as-read-backward (n)
11397   "Mark the N articles as read backwards.
11398 The difference between N and the actual number of articles marked is
11399 returned."
11400   (interactive "p")
11401   (gnus-summary-mark-forward (- n) gnus-del-mark t))
11402
11403 (defun gnus-summary-mark-as-read (&optional article mark)
11404   "Mark current article as read.
11405 ARTICLE specifies the article to be marked as read.
11406 MARK specifies a string to be inserted at the beginning of the line."
11407   (gnus-summary-mark-article article mark))
11408
11409 (defun gnus-summary-clear-mark-forward (n)
11410   "Clear marks from N articles forward.
11411 If N is negative, clear backward instead.
11412 The difference between N and the number of marks cleared is returned."
11413   (interactive "p")
11414   (gnus-summary-mark-forward n gnus-unread-mark))
11415
11416 (defun gnus-summary-clear-mark-backward (n)
11417   "Clear marks from N articles backward.
11418 The difference between N and the number of marks cleared is returned."
11419   (interactive "p")
11420   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11421
11422 (defun gnus-summary-mark-unread-as-read ()
11423   "Intended to be used by `gnus-summary-mark-article-hook'."
11424   (when (memq gnus-current-article gnus-newsgroup-unreads)
11425     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11426
11427 (defun gnus-summary-mark-region-as-read (point mark all)
11428   "Mark all unread articles between point and mark as read.
11429 If given a prefix, mark all articles between point and mark as read,
11430 even ticked and dormant ones."
11431   (interactive "r\nP")
11432   (save-excursion
11433     (let (article)
11434       (goto-char point)
11435       (beginning-of-line)
11436       (while (and 
11437               (< (point) mark)
11438               (progn
11439                 (when (or all 
11440                           (memq (setq article (gnus-summary-article-number))
11441                                 gnus-newsgroup-unreads))
11442                   (gnus-summary-mark-article article gnus-del-mark))
11443                 t)
11444               (gnus-summary-find-next))))))
11445
11446 (defun gnus-summary-mark-below (score mark)
11447   "Mark articles with score less than SCORE with MARK."
11448   (interactive "P\ncMark: ")
11449   (gnus-set-global-variables)
11450   (setq score (if score
11451                   (prefix-numeric-value score)
11452                 (or gnus-summary-default-score 0)))
11453   (save-excursion
11454     (set-buffer gnus-summary-buffer)
11455     (goto-char (point-min))
11456     (while (not (eobp))
11457       (and (< (gnus-summary-article-score) score)
11458            (gnus-summary-mark-article nil mark))
11459       (gnus-summary-find-next))))
11460
11461 (defun gnus-summary-kill-below (&optional score)
11462   "Mark articles with score below SCORE as read."
11463   (interactive "P")
11464   (gnus-set-global-variables)
11465   (gnus-summary-mark-below score gnus-killed-mark))
11466
11467 (defun gnus-summary-clear-above (&optional score)
11468   "Clear all marks from articles with score above SCORE."
11469   (interactive "P")
11470   (gnus-set-global-variables)
11471   (gnus-summary-mark-above score gnus-unread-mark))
11472
11473 (defun gnus-summary-tick-above (&optional score)
11474   "Tick all articles with score above SCORE."
11475   (interactive "P")
11476   (gnus-set-global-variables)
11477   (gnus-summary-mark-above score gnus-ticked-mark))
11478
11479 (defun gnus-summary-mark-above (score mark)
11480   "Mark articles with score over SCORE with MARK."
11481   (interactive "P\ncMark: ")
11482   (gnus-set-global-variables)
11483   (setq score (if score
11484                   (prefix-numeric-value score)
11485                 (or gnus-summary-default-score 0)))
11486   (save-excursion
11487     (set-buffer gnus-summary-buffer)
11488     (goto-char (point-min))
11489     (while (and (progn
11490                   (if (> (gnus-summary-article-score) score)
11491                       (gnus-summary-mark-article nil mark))
11492                   t)
11493                 (gnus-summary-find-next)))))
11494
11495 ;; Suggested by Daniel Quinlan <quinlan@best.com>.  
11496 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
11497 (defun gnus-summary-limit-include-expunged ()
11498   "Display all the hidden articles that were expunged for low scores."
11499   (interactive)
11500   (gnus-set-global-variables)
11501   (let ((buffer-read-only nil))
11502     (let ((scored gnus-newsgroup-scored)
11503           headers h)
11504       (while scored
11505         (or (gnus-summary-goto-subject (car (car scored)))
11506             (and (setq h (gnus-summary-article-header (car (car scored))))
11507                  (< (cdr (car scored)) gnus-summary-expunge-below)
11508                  (setq headers (cons h headers))))
11509         (setq scored (cdr scored)))
11510       (or headers (error "No expunged articles hidden."))
11511       (goto-char (point-min))
11512       (gnus-summary-prepare-unthreaded (nreverse headers)))
11513     (goto-char (point-min))
11514     (gnus-summary-position-point)))
11515
11516 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
11517   "Mark all articles not marked as unread in this newsgroup as read.
11518 If prefix argument ALL is non-nil, all articles are marked as read.
11519 If QUIETLY is non-nil, no questions will be asked.
11520 If TO-HERE is non-nil, it should be a point in the buffer.  All
11521 articles before this point will be marked as read.
11522 The number of articles marked as read is returned."
11523   (interactive "P")
11524   (gnus-set-global-variables)
11525   (prog1
11526       (if (or quietly
11527               (not gnus-interactive-catchup) ;Without confirmation?
11528               gnus-expert-user
11529               (gnus-y-or-n-p
11530                (if all
11531                    "Mark absolutely all articles as read? "
11532                  "Mark all unread articles as read? ")))
11533           (if (and not-mark 
11534                    (not gnus-newsgroup-adaptive)
11535                    (not gnus-newsgroup-auto-expire))
11536               (progn
11537                 (when all
11538                   (setq gnus-newsgroup-marked nil
11539                         gnus-newsgroup-dormant nil))
11540                 (setq gnus-newsgroup-unreads nil))
11541             ;; We actually mark all articles as canceled, which we
11542             ;; have to do when using auto-expiry or adaptive scoring. 
11543             (gnus-summary-show-all-threads)
11544             (if (gnus-summary-first-subject (not all))
11545                 (while (and 
11546                         (if to-here (< (point) to-here) t)
11547                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
11548                         (gnus-summary-find-next (not all)))))
11549             (unless to-here
11550               (setq gnus-newsgroup-unreads nil))
11551             (gnus-set-mode-line 'summary)))
11552     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
11553       (if (and (not to-here) (eq 'nnvirtual (car method)))
11554           (nnvirtual-catchup-group
11555            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
11556     (gnus-summary-position-point)))
11557
11558 (defun gnus-summary-catchup-to-here (&optional all)
11559   "Mark all unticked articles before the current one as read.
11560 If ALL is non-nil, also mark ticked and dormant articles as read."
11561   (interactive "P")
11562   (gnus-set-global-variables)
11563   (save-excursion
11564     (let ((beg (point)))
11565       ;; We check that there are unread articles.
11566       (when (or all (gnus-summary-find-prev))
11567         (gnus-summary-catchup all t beg))))
11568   (gnus-summary-position-point))
11569
11570 (defun gnus-summary-catchup-all (&optional quietly)
11571   "Mark all articles in this newsgroup as read."
11572   (interactive "P")
11573   (gnus-set-global-variables)
11574   (gnus-summary-catchup t quietly))
11575
11576 (defun gnus-summary-catchup-and-exit (&optional all quietly)
11577   "Mark all articles not marked as unread in this newsgroup as read, then exit.
11578 If prefix argument ALL is non-nil, all articles are marked as read."
11579   (interactive "P")
11580   (gnus-set-global-variables)
11581   (gnus-summary-catchup all quietly nil 'fast)
11582   ;; Select next newsgroup or exit.
11583   (if (eq gnus-auto-select-next 'quietly)
11584       (gnus-summary-next-group nil)
11585     (gnus-summary-exit)))
11586
11587 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
11588   "Mark all articles in this newsgroup as read, and then exit."
11589   (interactive "P")
11590   (gnus-set-global-variables)
11591   (gnus-summary-catchup-and-exit t quietly))
11592
11593 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
11594 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
11595   "Mark all articles in this group as read and select the next group.
11596 If given a prefix, mark all articles, unread as well as ticked, as
11597 read." 
11598   (interactive "P")
11599   (gnus-set-global-variables)
11600   (gnus-summary-catchup all)
11601   (gnus-summary-next-article t))
11602
11603 ;; Thread-based commands.
11604
11605 (defun gnus-summary-articles-in-thread (&optional article)
11606   "Return a list of all articles in the current thread.
11607 If ARTICLE is non-nil, return all articles in the thread that starts
11608 with that article."
11609   (let* ((article (or article (gnus-summary-article-number)))
11610          (data (gnus-data-find-list article))
11611          (top-level (gnus-data-level (car data)))
11612          (top-subject 
11613           (cond ((null gnus-thread-operation-ignore-subject)
11614                  (gnus-simplify-subject-re
11615                   (mail-header-subject (gnus-data-header (car data)))))
11616                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
11617                  (gnus-simplify-subject-fuzzy
11618                   (mail-header-subject (gnus-data-header (car data)))))
11619                 (t nil)))
11620          articles)
11621     (if (not data)
11622         ()                              ; This article doesn't exist.
11623       (while data
11624         (and (or (not top-subject)
11625                  (string= top-subject
11626                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
11627                               (gnus-simplify-subject-fuzzy
11628                                (mail-header-subject 
11629                                 (gnus-data-header (car data))))
11630                             (gnus-simplify-subject-re
11631                              (mail-header-subject 
11632                               (gnus-data-header (car data)))))))
11633              (setq articles (cons (gnus-data-number (car data)) articles)))
11634         (if (and (setq data (cdr data))
11635                  (> (gnus-data-level (car data)) top-level))
11636             ()
11637           (setq data nil)))
11638       ;; Return the list of articles.
11639       (nreverse articles))))
11640
11641 (defun gnus-summary-toggle-threads (&optional arg)
11642   "Toggle showing conversation threads.
11643 If ARG is positive number, turn showing conversation threads on."
11644   (interactive "P")
11645   (gnus-set-global-variables)
11646   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
11647     (setq gnus-show-threads
11648           (if (null arg) (not gnus-show-threads)
11649             (> (prefix-numeric-value arg) 0)))
11650     (gnus-summary-prepare)
11651     (gnus-summary-goto-subject current)
11652     (gnus-summary-position-point)))
11653
11654 (defun gnus-summary-show-all-threads ()
11655   "Show all threads."
11656   (interactive)
11657   (gnus-set-global-variables)
11658   (save-excursion
11659     (let ((buffer-read-only nil))
11660       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
11661   (gnus-summary-position-point))
11662
11663 (defun gnus-summary-show-thread ()
11664   "Show thread subtrees.
11665 Returns nil if no thread was there to be shown."
11666   (interactive)
11667   (gnus-set-global-variables)
11668   (let ((buffer-read-only nil)
11669         (orig (point))
11670         ;; first goto end then to beg, to have point at beg after let
11671         (end (progn (end-of-line) (point)))
11672         (beg (progn (beginning-of-line) (point))))
11673     (prog1
11674         ;; Any hidden lines here?
11675         (search-forward "\r" end t)
11676       (subst-char-in-region beg end ?\^M ?\n t)
11677       (goto-char orig)
11678       (gnus-summary-position-point))))
11679
11680 (defun gnus-summary-hide-all-threads ()
11681   "Hide all thread subtrees."
11682   (interactive)
11683   (gnus-set-global-variables)
11684   (save-excursion
11685     (goto-char (point-min))
11686     (gnus-summary-hide-thread)
11687     (while (zerop (gnus-summary-next-thread 1 t))
11688       (gnus-summary-hide-thread)))
11689   (gnus-summary-position-point))
11690
11691 (defun gnus-summary-hide-thread ()
11692   "Hide thread subtrees.
11693 Returns nil if no threads were there to be hidden."
11694   (interactive)
11695   (gnus-set-global-variables)
11696   (let ((buffer-read-only nil)
11697         (start (point))
11698         (article (gnus-summary-article-number))
11699         end)
11700     ;; Go forward until either the buffer ends or the subthread
11701     ;; ends. 
11702     (when (and (not (eobp))
11703                (or (and (zerop (gnus-summary-next-thread 1 t))
11704                         (gnus-summary-find-prev))
11705                    (goto-char (gnus-data-pos (car (gnus-data-list 'rev))))))
11706       (setq end (point))
11707       (prog1
11708           (if (and (> (point) start)
11709                    (search-backward "\n" start t))
11710               (progn
11711                 (subst-char-in-region start end ?\n ?\^M)
11712                 (gnus-summary-goto-subject article))
11713             (goto-char start)
11714             nil)
11715         (gnus-summary-position-point)))))
11716
11717 (defun gnus-summary-go-to-next-thread (&optional previous)
11718   "Go to the same level (or less) next thread.
11719 If PREVIOUS is non-nil, go to previous thread instead.
11720 Return the article number moved to, or nil if moving was impossible."
11721   (let* ((level (gnus-summary-thread-level))
11722          (article (gnus-summary-article-number))
11723          (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
11724          oart)
11725     (while data
11726       (if (<= (gnus-data-level (car data)) level)
11727           (setq oart (gnus-data-number (car data))
11728                 data nil)
11729         (setq data (cdr data))))
11730     (and oart 
11731          (gnus-summary-goto-subject oart))))
11732
11733 (defun gnus-summary-next-thread (n &optional silent)
11734   "Go to the same level next N'th thread.
11735 If N is negative, search backward instead.
11736 Returns the difference between N and the number of skips actually
11737 done.
11738
11739 If SILENT, don't output messages."
11740   (interactive "p")
11741   (gnus-set-global-variables)
11742   (let ((backward (< n 0))
11743         (n (abs n)))
11744     (while (and (> n 0)
11745                 (gnus-summary-go-to-next-thread backward))
11746       (decf n))
11747     (gnus-summary-position-point)
11748     (when (and (not silent) (/= 0 n))
11749       (gnus-message 7 "No more threads"))
11750     n))
11751
11752 (defun gnus-summary-prev-thread (n)
11753   "Go to the same level previous N'th thread.
11754 Returns the difference between N and the number of skips actually
11755 done."
11756   (interactive "p")
11757   (gnus-set-global-variables)
11758   (gnus-summary-next-thread (- n)))
11759
11760 (defun gnus-summary-go-down-thread ()
11761   "Go down one level in the current thread."
11762   (let ((children (gnus-summary-article-children)))
11763     (and children
11764          (gnus-summary-goto-subject (car children)))))
11765
11766 (defun gnus-summary-go-up-thread ()
11767   "Go up one level in the current thread."
11768   (let ((parent (gnus-summary-article-parent)))
11769     (and parent
11770          (gnus-summary-goto-subject parent))))
11771
11772 (defun gnus-summary-down-thread (n)
11773   "Go down thread N steps.
11774 If N is negative, go up instead.
11775 Returns the difference between N and how many steps down that were
11776 taken."
11777   (interactive "p")
11778   (gnus-set-global-variables)
11779   (let ((up (< n 0))
11780         (n (abs n)))
11781     (while (and (> n 0)
11782                 (if up (gnus-summary-go-up-thread)
11783                   (gnus-summary-go-down-thread)))
11784       (setq n (1- n)))
11785     (gnus-summary-position-point)
11786     (if (/= 0 n) (gnus-message 7 "Can't go further"))
11787     n))
11788
11789 (defun gnus-summary-up-thread (n)
11790   "Go up thread N steps.
11791 If N is negative, go up instead.
11792 Returns the difference between N and how many steps down that were
11793 taken."
11794   (interactive "p")
11795   (gnus-set-global-variables)
11796   (gnus-summary-down-thread (- n)))
11797
11798 (defun gnus-summary-kill-thread (&optional unmark)
11799   "Mark articles under current thread as read.
11800 If the prefix argument is positive, remove any kinds of marks.
11801 If the prefix argument is negative, tick articles instead."
11802   (interactive "P")
11803   (gnus-set-global-variables)
11804   (if unmark
11805       (setq unmark (prefix-numeric-value unmark)))
11806   (let ((articles (gnus-summary-articles-in-thread)))
11807     (save-excursion
11808       ;; Expand the thread.
11809       (gnus-summary-show-thread)
11810       ;; Mark all the articles.
11811       (while articles
11812         (gnus-summary-goto-subject (car articles))
11813         (cond ((null unmark) 
11814                (gnus-summary-mark-article-as-read gnus-killed-mark))
11815               ((> unmark 0) 
11816                (gnus-summary-mark-article-as-unread gnus-unread-mark))
11817               (t 
11818                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
11819         (setq articles (cdr articles))))
11820     ;; Hide killed subtrees.
11821     (and (null unmark)
11822          gnus-thread-hide-killed
11823          (gnus-summary-hide-thread))
11824     ;; If marked as read, go to next unread subject.
11825     (if (null unmark)
11826         ;; Go to next unread subject.
11827         (gnus-summary-next-subject 1 t)))
11828   (gnus-set-mode-line 'summary))
11829
11830 ;; Summary sorting commands
11831
11832 (defun gnus-summary-sort-by-number (&optional reverse)
11833   "Sort summary buffer by article number.
11834 Argument REVERSE means reverse order."
11835   (interactive "P")
11836   (gnus-summary-sort 'number reverse))
11837
11838 (defun gnus-summary-sort-by-author (&optional reverse)
11839   "Sort summary buffer by author name alphabetically.
11840 If case-fold-search is non-nil, case of letters is ignored.
11841 Argument REVERSE means reverse order."
11842   (interactive "P")
11843   (gnus-summary-sort 'author reverse))
11844
11845 (defun gnus-summary-sort-by-subject (&optional reverse)
11846   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
11847 If case-fold-search is non-nil, case of letters is ignored.
11848 Argument REVERSE means reverse order."
11849   (interactive "P")
11850   (gnus-summary-sort 'subject reverse))
11851
11852 (defun gnus-summary-sort-by-date (&optional reverse)
11853   "Sort summary buffer by date.
11854 Argument REVERSE means reverse order."
11855   (interactive "P")
11856   (gnus-summary-sort 'date reverse))
11857
11858 (defun gnus-summary-sort-by-score (&optional reverse)
11859   "Sort summary buffer by score.
11860 Argument REVERSE means reverse order."
11861   (interactive "P")
11862   (gnus-summary-sort 'score reverse))
11863
11864 (defun gnus-summary-sort (predicate reverse)
11865   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
11866   (gnus-set-global-variables)
11867   (let* ((gnus-thread-sort-functions 
11868           (list (intern (format "gnus-thread-sort-by-%s" predicate))))
11869          (gnus-article-sort-functions
11870           (list (intern (format "gnus-article-sort-by-%s" predicate))))
11871          (buffer-read-only)
11872          (gnus-summary-prepare-hook nil))
11873     ;; We do the sorting by regenerating the threads.
11874     (gnus-summary-prepare)
11875     ;; Hide subthreads if needed.
11876     (when (and gnus-show-threads gnus-thread-hide-subtree)
11877       (gnus-summary-hide-all-threads)))
11878   ;; If in async mode, we send some info to the backend.
11879   (when gnus-newsgroup-async
11880     (gnus-request-asynchronous 
11881      gnus-newsgroup-name gnus-newsgroup-data)))
11882   
11883 (defun gnus-sortable-date (date)
11884   "Make sortable string by string-lessp from DATE.
11885 Timezone package is used."
11886   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
11887          (year (aref date 0))
11888          (month (aref date 1))
11889          (day (aref date 2)))
11890     (timezone-make-sortable-date 
11891      year month day 
11892      (timezone-make-time-string
11893       (aref date 3) (aref date 4) (aref date 5)))))
11894
11895
11896 ;; Summary saving commands.
11897
11898 (defun gnus-summary-save-article (&optional n not-saved)
11899   "Save the current article using the default saver function.
11900 If N is a positive number, save the N next articles.
11901 If N is a negative number, save the N previous articles.
11902 If N is nil and any articles have been marked with the process mark,
11903 save those articles instead.
11904 The variable `gnus-default-article-saver' specifies the saver function."
11905   (interactive "P")
11906   (gnus-set-global-variables)
11907   (let ((articles (gnus-summary-work-articles n))
11908         file header article)
11909     (while articles
11910       (setq header (gnus-summary-article-header
11911                     (setq article (pop articles))))
11912       (if (not (vectorp header))
11913           ;; This is a pseudo-article.
11914           (if (assq 'name header)
11915               (gnus-copy-file (cdr (assq 'name header)))
11916             (gnus-message 1 "Article %d is unsaveable" article))
11917         ;; This is a real article.
11918         (save-window-excursion
11919           (gnus-summary-select-article t nil nil article))
11920         (unless gnus-save-all-headers
11921           ;; Remove headers accoring to `gnus-saved-headers'.
11922           (let ((gnus-visible-headers 
11923                  (or gnus-saved-headers gnus-visible-headers)))
11924             (gnus-article-hide-headers t)))
11925         ;; Remove any X-Gnus lines.
11926         (save-excursion
11927           (set-buffer gnus-article-buffer)
11928           (save-restriction
11929             (let ((buffer-read-only nil))
11930               (gnus-narrow-to-headers)
11931               (while (re-search-forward "^X-Gnus" nil t)
11932                 (gnus-delete-line)))))
11933         (save-window-excursion
11934           (if (not gnus-default-article-saver)
11935               (error "No default saver is defined.")
11936             (setq file (funcall
11937                         gnus-default-article-saver
11938                         (cond
11939                          ((not gnus-prompt-before-saving)
11940                           'default)
11941                          ((eq gnus-prompt-before-saving 'always)
11942                           nil)
11943                          (t file))))))
11944         (gnus-summary-remove-process-mark article)
11945         (unless not-saved
11946           (gnus-summary-set-saved-mark article))))
11947     (gnus-summary-position-point)
11948     n))
11949
11950 (defun gnus-summary-pipe-output (&optional arg)
11951   "Pipe the current article to a subprocess.
11952 If N is a positive number, pipe the N next articles.
11953 If N is a negative number, pipe the N previous articles.
11954 If N is nil and any articles have been marked with the process mark,
11955 pipe those articles instead."
11956   (interactive "P")
11957   (gnus-set-global-variables)
11958   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
11959     (gnus-summary-save-article arg t))
11960   (gnus-configure-windows 'pipe))
11961
11962 (defun gnus-summary-save-article-mail (&optional arg)
11963   "Append the current article to an mail file.
11964 If N is a positive number, save the N next articles.
11965 If N is a negative number, save the N previous articles.
11966 If N is nil and any articles have been marked with the process mark,
11967 save those articles instead."
11968   (interactive "P")
11969   (gnus-set-global-variables)
11970   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
11971     (gnus-summary-save-article arg)))
11972
11973 (defun gnus-summary-save-article-rmail (&optional arg)
11974   "Append the current article to an rmail file.
11975 If N is a positive number, save the N next articles.
11976 If N is a negative number, save the N previous articles.
11977 If N is nil and any articles have been marked with the process mark,
11978 save those articles instead."
11979   (interactive "P")
11980   (gnus-set-global-variables)
11981   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
11982     (gnus-summary-save-article arg)))
11983
11984 (defun gnus-summary-save-article-file (&optional arg)
11985   "Append the current article to a file.
11986 If N is a positive number, save the N next articles.
11987 If N is a negative number, save the N previous articles.
11988 If N is nil and any articles have been marked with the process mark,
11989 save those articles instead."
11990   (interactive "P")
11991   (gnus-set-global-variables)
11992   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
11993     (gnus-summary-save-article arg)))
11994
11995 (defun gnus-summary-save-article-body-file (&optional arg)
11996   "Append the current article body to a file.
11997 If N is a positive number, save the N next articles.
11998 If N is a negative number, save the N previous articles.
11999 If N is nil and any articles have been marked with the process mark,
12000 save those articles instead."
12001   (interactive "P")
12002   (gnus-set-global-variables)
12003   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12004     (gnus-summary-save-article arg)))
12005
12006 (defun gnus-read-save-file-name (prompt default-name)
12007   (let ((methods gnus-split-methods)
12008         split-name method result match)
12009     ;; Let the split methods have their say.
12010     (when gnus-split-methods
12011       (save-excursion
12012         (set-buffer gnus-original-article-buffer)
12013         (save-restriction
12014           (gnus-narrow-to-headers)
12015           (while methods
12016             (goto-char (point-min))
12017             (setq method (pop methods))
12018             (setq match (pop method))
12019             (when (cond
12020                    ((stringp match)
12021                     ;; Regular expression.
12022                     (condition-case () 
12023                         (re-search-forward match nil t)
12024                       (error nil)))
12025                    ((gnus-functionp match)
12026                     (save-restriction
12027                       (widen)
12028                       (setq result (funcall match gnus-newsgroup-name))))
12029                    ((consp match)
12030                     (save-restriction
12031                       (widen)
12032                       (setq result (eval match)))))
12033               (setq split-name (append (cdr methods) split-name))
12034               (cond ((stringp result)
12035                      (push result split-name))
12036                     ((consp result)
12037                      (setq split-name (append result split-name)))))))))
12038     (cond
12039      ;; No split name was found.
12040      ((null split-name)
12041       (read-file-name
12042        (concat prompt " (default " (file-name-nondirectory default-name) ") ")
12043        (file-name-directory default-name)
12044        default-name))
12045      ;; A single split name was found
12046      ((= 1 (length split-name))
12047       (read-file-name
12048        (concat prompt " (default " (car split-name) ") ")
12049        gnus-article-save-directory
12050        (concat gnus-article-save-directory (car split-name))))
12051      ;; A list of splits was found.
12052      (t
12053       (setq split-name (mapcar (lambda (el) (list el)) (nreverse split-name)))
12054       (let ((result (completing-read (concat prompt " ") split-name nil nil)))
12055         (concat gnus-article-save-directory
12056                 (if (string= result "")
12057                     (car (car split-name))
12058                   result)))))))
12059
12060 (defun gnus-article-archive-name (group)
12061   "Return the first instance of an \"Archive-name\" in the current buffer."
12062   (let ((case-fold-search t))
12063     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12064       (match-string 1))))
12065
12066 (defun gnus-summary-save-in-rmail (&optional filename)
12067   "Append this article to Rmail file.
12068 Optional argument FILENAME specifies file name.
12069 Directory to save to is default to `gnus-article-save-directory' which
12070 is initialized from the SAVEDIR environment variable."
12071   (interactive)
12072   (gnus-set-global-variables)
12073   (let ((default-name
12074           (funcall gnus-rmail-save-name gnus-newsgroup-name
12075                    gnus-current-headers gnus-newsgroup-last-rmail)))
12076     (setq filename
12077           (cond ((eq filename 'default)
12078                  default-name)
12079                 (filename filename)
12080                 (t (gnus-read-save-file-name 
12081                     "Save in rmail file:" default-name))))
12082     (gnus-make-directory (file-name-directory filename))
12083     (gnus-eval-in-buffer-window 
12084      gnus-original-article-buffer
12085      (save-excursion
12086        (save-restriction
12087          (widen)
12088          (gnus-output-to-rmail filename))))
12089     ;; Remember the directory name to save articles
12090     (setq gnus-newsgroup-last-rmail filename)))
12091
12092 (defun gnus-summary-save-in-mail (&optional filename)
12093   "Append this article to Unix mail file.
12094 Optional argument FILENAME specifies file name.
12095 Directory to save to is default to `gnus-article-save-directory' which
12096 is initialized from the SAVEDIR environment variable."
12097   (interactive)
12098   (gnus-set-global-variables)
12099   (let ((default-name
12100           (funcall gnus-mail-save-name gnus-newsgroup-name
12101                    gnus-current-headers gnus-newsgroup-last-mail)))
12102     (setq filename
12103           (cond ((eq filename 'default)
12104                  default-name)
12105                 (filename filename)
12106                 (t (gnus-read-save-file-name 
12107                     "Save in Unix mail file:" default-name))))
12108     (setq filename
12109           (expand-file-name filename
12110                             (and default-name
12111                                  (file-name-directory default-name))))
12112     (gnus-make-directory (file-name-directory filename))
12113     (gnus-eval-in-buffer-window 
12114      gnus-original-article-buffer
12115      (save-excursion
12116        (save-restriction
12117          (widen)
12118          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12119              (gnus-output-to-rmail filename)
12120            (let ((mail-use-rfc822 t))
12121              (rmail-output filename 1 t t))))))
12122     ;; Remember the directory name to save articles.
12123     (setq gnus-newsgroup-last-mail filename)))
12124
12125 (defun gnus-summary-save-in-file (&optional filename)
12126   "Append this article to file.
12127 Optional argument FILENAME specifies file name.
12128 Directory to save to is default to `gnus-article-save-directory' which
12129 is initialized from the SAVEDIR environment variable."
12130   (interactive)
12131   (gnus-set-global-variables)
12132   (let ((default-name
12133           (funcall gnus-file-save-name gnus-newsgroup-name
12134                    gnus-current-headers gnus-newsgroup-last-file)))
12135     (setq filename
12136           (cond ((eq filename 'default)
12137                  default-name)
12138                 (filename filename)
12139                 (t (gnus-read-save-file-name 
12140                     "Save in file:" default-name))))
12141     (gnus-make-directory (file-name-directory filename))
12142     (gnus-eval-in-buffer-window 
12143      gnus-article-buffer
12144      (save-excursion
12145        (save-restriction
12146          (widen)
12147          (gnus-output-to-file filename))))
12148     ;; Remember the directory name to save articles.
12149     (setq gnus-newsgroup-last-file filename)))
12150
12151 (defun gnus-summary-save-body-in-file (&optional filename)
12152   "Append this article body to a file.
12153 Optional argument FILENAME specifies file name.
12154 The directory to save in defaults to `gnus-article-save-directory' which
12155 is initialized from the SAVEDIR environment variable."
12156   (interactive)
12157   (gnus-set-global-variables)
12158   (let ((default-name
12159           (funcall gnus-file-save-name gnus-newsgroup-name
12160                    gnus-current-headers gnus-newsgroup-last-file)))
12161     (setq filename
12162           (cond ((eq filename 'default)
12163                  default-name)
12164                 (filename filename)
12165                 (t (gnus-read-save-file-name 
12166                     "Save body in file:" default-name))))
12167     (gnus-make-directory (file-name-directory filename))
12168     (gnus-eval-in-buffer-window 
12169      gnus-article-buffer
12170      (save-excursion
12171        (save-restriction
12172          (widen)
12173          (goto-char (point-min))
12174          (and (search-forward "\n\n" nil t)
12175               (narrow-to-region (point) (point-max)))
12176          (gnus-output-to-file filename))))
12177     ;; Remember the directory name to save articles.
12178     (setq gnus-newsgroup-last-file filename)))
12179
12180 (defun gnus-summary-save-in-pipe (&optional command)
12181   "Pipe this article to subprocess."
12182   (interactive)
12183   (gnus-set-global-variables)
12184   (setq command
12185         (cond ((eq command 'default)
12186                gnus-last-shell-command)
12187               (command command)
12188               (t (read-string "Shell command on article: "
12189                               gnus-last-shell-command))))
12190   (if (string-equal command "")
12191       (setq command gnus-last-shell-command))
12192   (gnus-eval-in-buffer-window 
12193    gnus-article-buffer
12194    (save-restriction
12195      (widen)
12196      (shell-command-on-region (point-min) (point-max) command nil)))
12197   (setq gnus-last-shell-command command))
12198
12199 ;; Summary extract commands
12200
12201 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12202   (let ((buffer-read-only nil)
12203         (article (gnus-summary-article-number))
12204         after-article b e)
12205     (or (gnus-summary-goto-subject article)
12206         (error (format "No such article: %d" article)))
12207     (gnus-summary-position-point)
12208     ;; If all commands are to be bunched up on one line, we collect
12209     ;; them here.  
12210     (if gnus-view-pseudos-separately
12211         ()
12212       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12213             files action)
12214         (while ps
12215           (setq action (cdr (assq 'action (car ps))))
12216           (setq files (list (cdr (assq 'name (car ps)))))
12217           (while (and ps (cdr ps)
12218                       (string= (or action "1")
12219                                (or (cdr (assq 'action (car (cdr ps)))) "2")))
12220             (setq files (cons (cdr (assq 'name (car (cdr ps)))) files))
12221             (setcdr ps (cdr (cdr ps))))
12222           (if (not files)
12223               ()
12224             (if (not (string-match "%s" action))
12225                 (setq files (cons " " files)))
12226             (setq files (cons " " files))
12227             (and (assq 'execute (car ps))
12228                  (setcdr (assq 'execute (car ps))
12229                          (funcall (if (string-match "%s" action)
12230                                       'format 'concat)
12231                                   action 
12232                                   (mapconcat (lambda (f) f) files " ")))))
12233           (setq ps (cdr ps)))))
12234     (if (and gnus-view-pseudos (not not-view))
12235         (while pslist
12236           (and (assq 'execute (car pslist))
12237                (gnus-execute-command (cdr (assq 'execute (car pslist)))
12238                                      (eq gnus-view-pseudos 'not-confirm)))
12239           (setq pslist (cdr pslist)))
12240       (save-excursion
12241         (while pslist
12242           (setq after-article (or (cdr (assq 'article (car pslist)))
12243                                   (gnus-summary-article-number)))
12244           (gnus-summary-goto-subject after-article)
12245           (forward-line 1)
12246           (setq b (point))
12247           (insert "          " (file-name-nondirectory
12248                                 (cdr (assq 'name (car pslist))))
12249                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12250           (setq e (point))
12251           (forward-line -1)             ; back to `b'
12252           (put-text-property b e 'gnus-number gnus-reffed-article-number)
12253           (gnus-data-enter after-article
12254                            gnus-reffed-article-number
12255                            gnus-unread-mark 
12256                            b
12257                            (car pslist) 
12258                            0 
12259                            (- e b))
12260           (setq gnus-newsgroup-unreads
12261                 (cons gnus-reffed-article-number gnus-newsgroup-unreads))
12262           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12263           (setq pslist (cdr pslist)))))))
12264
12265 (defun gnus-pseudos< (p1 p2)
12266   (let ((c1 (cdr (assq 'action p1)))
12267         (c2 (cdr (assq 'action p2))))
12268     (and c1 c2 (string< c1 c2))))
12269
12270 (defun gnus-request-pseudo-article (props)
12271   (cond ((assq 'execute props)
12272          (gnus-execute-command (cdr (assq 'execute props)))))
12273   (let ((gnus-current-article (gnus-summary-article-number)))
12274     (run-hooks 'gnus-mark-article-hook)))
12275
12276 (defun gnus-execute-command (command &optional automatic)
12277   (save-excursion
12278     (gnus-article-setup-buffer)
12279     (set-buffer gnus-article-buffer)
12280     (let ((command (if automatic command (read-string "Command: " command)))
12281           (buffer-read-only nil))
12282       (erase-buffer)
12283       (insert "$ " command "\n\n")
12284       (if gnus-view-pseudo-asynchronously
12285           (start-process "gnus-execute" nil "sh" "-c" command)
12286         (call-process "sh" nil t nil "-c" command)))))
12287
12288 (defun gnus-copy-file (file &optional to)
12289   "Copy FILE to TO."
12290   (interactive
12291    (list (read-file-name "Copy file: " default-directory)
12292          (read-file-name "Copy file to: " default-directory)))
12293   (gnus-set-global-variables)
12294   (or to (setq to (read-file-name "Copy file to: " default-directory)))
12295   (and (file-directory-p to) 
12296        (setq to (concat (file-name-as-directory to)
12297                         (file-name-nondirectory file))))
12298   (copy-file file to))
12299
12300 ;; Summary kill commands.
12301
12302 (defun gnus-summary-edit-global-kill (article)
12303   "Edit the \"global\" kill file."
12304   (interactive (list (gnus-summary-article-number)))
12305   (gnus-set-global-variables)
12306   (gnus-group-edit-global-kill article))
12307
12308 (defun gnus-summary-edit-local-kill ()
12309   "Edit a local kill file applied to the current newsgroup."
12310   (interactive)
12311   (gnus-set-global-variables)
12312   (setq gnus-current-headers (gnus-summary-article-header))
12313   (gnus-set-global-variables)
12314   (gnus-group-edit-local-kill 
12315    (gnus-summary-article-number) gnus-newsgroup-name))
12316
12317 \f
12318 ;;;
12319 ;;; Gnus article mode
12320 ;;;
12321
12322 (put 'gnus-article-mode 'mode-class 'special)
12323
12324 (if gnus-article-mode-map
12325     nil
12326   (setq gnus-article-mode-map (make-keymap))
12327   (suppress-keymap gnus-article-mode-map)
12328   (define-key gnus-article-mode-map " " 'gnus-article-next-page)
12329   (define-key gnus-article-mode-map "\177" 'gnus-article-prev-page)
12330   (define-key gnus-article-mode-map "\C-c^" 'gnus-article-refer-article)
12331   (define-key gnus-article-mode-map "h" 'gnus-article-show-summary)
12332   (define-key gnus-article-mode-map "s" 'gnus-article-show-summary)
12333   (define-key gnus-article-mode-map "\C-c\C-m" 'gnus-article-mail)
12334   (define-key gnus-article-mode-map "?" 'gnus-article-describe-briefly)
12335   (define-key gnus-article-mode-map gnus-mouse-2 'gnus-article-push-button)
12336   (define-key gnus-article-mode-map "\r" 'gnus-article-press-button)
12337   (define-key gnus-article-mode-map "\t" 'gnus-article-next-button)
12338   (define-key gnus-article-mode-map "\M-\t" 'gnus-article-prev-button)
12339   (define-key gnus-article-mode-map "\C-c\C-b" 'gnus-bug)
12340   
12341   ;; Duplicate almost all summary keystrokes in the article mode map.
12342   (let ((commands 
12343          (list 
12344           "p" "N" "P" "\M-\C-n" "\M-\C-p"
12345           "\M-n" "\M-p" "." "," "\M-s" "\M-r" "<" ">" "j"
12346           "u" "!" "U" "d" "D" "E" "\M-u" "\M-U" "k" "\C-k" "\M-\C-k"
12347           "\M-\C-l" "e" "#" "\M-#" "\M-\C-t" "\M-\C-s" "\M-\C-h"
12348           "\M-\C-f" "\M-\C-b" "\M-\C-u" "\M-\C-d" "&" "\C-w"
12349           "\C-t" "?" "\C-c\M-\C-s" "\C-c\C-s\C-n" "\C-c\C-s\C-a"
12350           "\C-c\C-s\C-s" "\C-c\C-s\C-d" "\C-c\C-s\C-i" "\C-x\C-s"
12351           "\M-g" "w" "\C-c\C-r" "\M-t" "C"
12352           "o" "\C-o" "|" "\M-k" "\M-K" "V" "\C-c\C-d"
12353           "\C-c\C-i" "x" "X" "t" "g" "?" "l"
12354           "\C-c\C-v\C-v" "\C-d" "v" 
12355 ;;        "Mt" "M!" "Md" "Mr"
12356 ;;        "Mc" "M " "Me" "Mx" "M?" "Mb" "MB" "M#" "M\M-#" "M\M-r"
12357 ;;        "M\M-\C-r" "MD" "M\M-D" "MS" "MC" "MH" "M\C-c" "Mk" "MK"
12358 ;;        "Ms" "Mc" "Mu" "Mm" "Mk" "Gn" "Gp" "GN" "GP" "G\C-n" "G\C-p"
12359 ;;        "G\M-n" "G\M-p" "Gf" "Gb" "Gg" "Gl" "Gp" "Tk" "Tl" "Ti" "TT"
12360 ;;        "Ts" "TS" "Th" "TH" "Tn" "Tp" "Tu" "Td" "T#" "A " "An" "A\177" "Ap"
12361 ;;        "A\r" "A<" "A>" "Ab" "Ae" "A^" "Ar" "Aw" "Ac" "Ag" "At" "Am"
12362 ;;        "As" "Wh" "Ws" "Wc" "Wo" "Ww" "Wd" "Wq" "Wf" "Wt" "W\C-t"
12363 ;;        "WT" "WA" "Wa" "WH" "WC" "WS" "Wb" "Hv" "Hf" "Hd" "Hh" "Hi"
12364 ;;        "Be" "B\177" "Bm" "Br" "Bw" "Bc" "Bq" "Bi" "Oo" "Om" "Or"
12365 ;;        "Of" "Oh" "Ov" "Op" "Vu" "V\C-s" "V\C-r" "Vr" "V&" "VT" "Ve"
12366 ;;        "VD" "Vk" "VK" "Vsn" "Vsa" "Vss" "Vsd" "Vsi"
12367           )))
12368     (while commands
12369       (define-key gnus-article-mode-map (car commands) 
12370         'gnus-article-summary-command)
12371       (setq commands (cdr commands))))
12372
12373   (let ((commands (list "q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
12374 ;;                      "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP" 
12375                          "=" "n"  "^" "\M-^")))
12376     (while commands
12377       (define-key gnus-article-mode-map (car commands) 
12378         'gnus-article-summary-command-nosave)
12379       (setq commands (cdr commands)))))
12380
12381
12382 (defun gnus-article-mode ()
12383   "Major mode for displaying an article.
12384
12385 All normal editing commands are switched off.
12386
12387 The following commands are available:
12388
12389 \\<gnus-article-mode-map>
12390 \\[gnus-article-next-page]\t Scroll the article one page forwards
12391 \\[gnus-article-prev-page]\t Scroll the article one page backwards
12392 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
12393 \\[gnus-article-show-summary]\t Display the summary buffer
12394 \\[gnus-article-mail]\t Send a reply to the address near point
12395 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
12396 \\[gnus-info-find-node]\t Go to the Gnus info node"
12397   (interactive)
12398   (when (and menu-bar-mode
12399              (gnus-visual-p 'article-menu 'menu))
12400     (gnus-article-make-menu-bar))
12401   (kill-all-local-variables)
12402   (gnus-simplify-mode-line)
12403   (setq mode-name "Article")
12404   (setq major-mode 'gnus-article-mode)
12405   (make-local-variable 'minor-mode-alist)
12406   (or (assq 'gnus-show-mime minor-mode-alist)
12407       (setq minor-mode-alist
12408             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
12409   (use-local-map gnus-article-mode-map)
12410   (make-local-variable 'page-delimiter)
12411   (setq page-delimiter gnus-page-delimiter)
12412   (buffer-disable-undo (current-buffer))
12413   (setq buffer-read-only t)             ;Disable modification
12414   (run-hooks 'gnus-article-mode-hook))
12415
12416 (defun gnus-article-setup-buffer ()
12417   "Initialize article mode buffer."
12418   ;; Returns the article buffer.
12419   (if (get-buffer gnus-article-buffer)
12420       (save-excursion
12421         (set-buffer gnus-article-buffer)
12422         (buffer-disable-undo (current-buffer))
12423         (setq buffer-read-only t)
12424         (gnus-add-current-to-buffer-list)
12425         (or (eq major-mode 'gnus-article-mode)
12426             (gnus-article-mode))
12427         (current-buffer))
12428     (save-excursion
12429       (set-buffer (get-buffer-create gnus-article-buffer))
12430       (gnus-add-current-to-buffer-list)
12431       (gnus-article-mode)
12432       (current-buffer))))
12433
12434 ;; Set article window start at LINE, where LINE is the number of lines
12435 ;; from the head of the article.
12436 (defun gnus-article-set-window-start (&optional line)
12437   (set-window-start 
12438    (get-buffer-window gnus-article-buffer)
12439    (save-excursion
12440      (set-buffer gnus-article-buffer)
12441      (goto-char (point-min))
12442      (if (not line)
12443          (point-min)
12444        (gnus-message 6 "Moved to bookmark")
12445        (search-forward "\n\n" nil t)
12446        (forward-line line)
12447        (point)))))
12448
12449 (defun gnus-kill-all-overlays ()
12450   "Delete all overlays in the current buffer."
12451   (when (fboundp 'overlay-lists)
12452     (let* ((overlayss (overlay-lists))
12453            (buffer-read-only nil)
12454            (overlays (nconc (car overlayss) (cdr overlayss))))
12455       (while overlays
12456         (delete-overlay (pop overlays))))))
12457
12458 (defun gnus-request-article-this-buffer (article group)
12459   "Get an article and insert it into this buffer."
12460   (prog1
12461       (save-excursion
12462         (if (get-buffer gnus-original-article-buffer)
12463             (set-buffer (get-buffer gnus-original-article-buffer))
12464           (set-buffer (get-buffer-create gnus-original-article-buffer))
12465           (buffer-disable-undo (current-buffer))
12466           (setq major-mode 'gnus-original-article-mode)
12467           (setq buffer-read-only t)
12468           (gnus-add-current-to-buffer-list))
12469
12470         (setq group (or group gnus-newsgroup-name))
12471
12472         ;; Open server if it has closed.
12473         (gnus-check-server (gnus-find-method-for-group group))
12474
12475         ;; Using `gnus-request-article' directly will insert the article into
12476         ;; `nntp-server-buffer' - so we'll save some time by not having to
12477         ;; copy it from the server buffer into the article buffer.
12478
12479         ;; We only request an article by message-id when we do not have the
12480         ;; headers for it, so we'll have to get those.
12481         (and (stringp article) 
12482              (let ((gnus-override-method gnus-refer-article-method))
12483                (gnus-read-header article)))
12484
12485         ;; If the article number is negative, that means that this article
12486         ;; doesn't belong in this newsgroup (possibly), so we find its
12487         ;; message-id and request it by id instead of number.
12488         (if (not (numberp article))
12489             ()
12490           (save-excursion
12491             (set-buffer gnus-summary-buffer)
12492             (let ((header (gnus-summary-article-header article)))
12493               (if (< article 0)
12494                   (if (vectorp header)
12495                       ;; It's a real article.
12496                       (setq article (mail-header-id header))
12497                     ;; It is an extracted pseudo-article.
12498                     (setq article 'pseudo)
12499                     (gnus-request-pseudo-article header)))
12500
12501               (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12502                 (if (not (eq (car method) 'nneething))
12503                     ()
12504                   (let ((dir (concat (file-name-as-directory (nth 1 method))
12505                                      (mail-header-subject header))))
12506                     (if (file-directory-p dir)
12507                         (progn
12508                           (setq article 'nneething)
12509                           (gnus-group-enter-directory dir)))))))))
12510
12511         (cond 
12512          ;; We first check `gnus-original-article-buffer'.
12513          ((and (equal (car gnus-original-article) group)
12514                (eq (cdr gnus-original-article) article))
12515           ;; We don't have to do anything, since it's already where we
12516           ;; want it.  
12517           'article)
12518          ;; Check the backlog.
12519          ((and gnus-keep-backlog
12520                (gnus-backlog-request-article group article (current-buffer)))
12521           'article)
12522          ;; Check the cache.
12523          ((and gnus-use-cache
12524                (numberp article)
12525                (gnus-cache-request-article article group))
12526           'article)
12527          ;; Get the article and put into the article buffer.
12528          ((or (stringp article) (numberp article))
12529           (let ((gnus-override-method 
12530                  (and (stringp article) gnus-refer-article-method))
12531                 (buffer-read-only nil))
12532             (erase-buffer)
12533             (gnus-kill-all-overlays)
12534             (if (gnus-request-article article group (current-buffer))
12535                 (progn
12536                   (and gnus-keep-backlog 
12537                        (gnus-backlog-enter-article 
12538                         group article (current-buffer)))
12539                   'article))))
12540          ;; It was a pseudo.
12541          (t article)))
12542     (setq gnus-original-article (cons group article))
12543     (let (buffer-read-only)
12544       (erase-buffer)
12545       (gnus-kill-all-overlays)
12546       (insert-buffer-substring gnus-original-article-buffer))))
12547
12548 (defun gnus-read-header (id)
12549   "Read the headers of article ID and enter them into the Gnus system."
12550   (let ((group gnus-newsgroup-name)
12551         (headers gnus-newsgroup-headers)
12552         header where)
12553     ;; First we check to see whether the header in question is already
12554     ;; fetched. 
12555     (if (stringp id)
12556         ;; This is a Message-ID.
12557         (while headers
12558           (if (string= id (mail-header-id (car headers)))
12559               (setq header (car headers)
12560                     headers nil)
12561             (setq headers (cdr headers))))
12562       ;; This is an article number.
12563       (while headers
12564         (if (= id (mail-header-number (car headers)))
12565             (setq header (car headers)
12566                   headers nil)
12567           (setq headers (cdr headers)))))
12568     (if header
12569         ;; We have found the header.
12570         header
12571       ;; We have to really fetch the header to this article.
12572       (when (setq where
12573                   (if (gnus-check-backend-function 'request-head group)
12574                       (gnus-request-head id group)
12575                     (gnus-request-article id group)))
12576         (save-excursion
12577           (set-buffer nntp-server-buffer)
12578           (and (search-forward "\n\n" nil t)
12579                (delete-region (1- (point)) (point-max)))
12580           (goto-char (point-max))
12581           (insert ".\n")
12582           (goto-char (point-min))
12583           (insert "211 "
12584                   (int-to-string
12585                    (cond
12586                     ((numberp id)
12587                      id)
12588                     ((cdr where)
12589                      (cdr where))
12590                     (t
12591                      gnus-reffed-article-number)))
12592                   " Article retrieved.\n"))
12593         (if (not (setq header (car (gnus-get-newsgroup-headers))))
12594             () ; Malformed head.
12595           (if (and (stringp id)
12596                    (not (string= (gnus-group-real-name group)
12597                                  (car where))))
12598               ;; If we fetched by Message-ID and the article came
12599               ;; from a different group, we fudge some bogus article
12600               ;; numbers for this article.
12601               (mail-header-set-number header gnus-reffed-article-number))
12602           (decf gnus-reffed-article-number)
12603           (push header gnus-newsgroup-headers)
12604           (setq gnus-current-headers header)
12605           (push (mail-header-number header) gnus-newsgroup-limit)
12606           header)))))
12607
12608 (defun gnus-article-prepare (article &optional all-headers header)
12609   "Prepare ARTICLE in article mode buffer.
12610 ARTICLE should either be an article number or a Message-ID.
12611 If ARTICLE is an id, HEADER should be the article headers.
12612 If ALL-HEADERS is non-nil, no headers are hidden."
12613   (save-excursion
12614     ;; Make sure we start in a summary buffer.
12615     (unless (eq major-mode 'gnus-summary-mode)
12616       (set-buffer gnus-summary-buffer))
12617     (setq gnus-summary-buffer (current-buffer))
12618     ;; Make sure the connection to the server is alive.
12619     (unless (gnus-server-opened
12620              (gnus-find-method-for-group gnus-newsgroup-name))
12621       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
12622       (gnus-request-group gnus-newsgroup-name t))
12623     (let* ((article (if header (mail-header-number header) article))
12624            (summary-buffer (current-buffer))
12625            (internal-hook gnus-article-internal-prepare-hook)
12626            (group gnus-newsgroup-name)
12627            result)
12628       (save-excursion
12629         (gnus-article-setup-buffer)
12630         (set-buffer gnus-article-buffer)
12631         ;; Deactivate active regions.
12632         (when (and (boundp 'transient-mark-mode)
12633                    transient-mark-mode)
12634           (setq mark-active nil))
12635         (if (not (setq result (let ((buffer-read-only nil))
12636                                 (gnus-request-article-this-buffer 
12637                                  article group))))
12638             ;; There is no such article.
12639             (save-excursion
12640               (if (not (numberp article))
12641                   ()
12642                 (setq gnus-article-current 
12643                       (cons gnus-newsgroup-name article))
12644                 (set-buffer gnus-summary-buffer)
12645                 (setq gnus-current-article article)
12646                 (gnus-summary-mark-article article gnus-canceled-mark))
12647               (gnus-message 
12648                1 "No such article (may have expired or been canceled)")
12649               (ding)
12650               nil)
12651           (if (or (eq result 'pseudo) (eq result 'nneething))
12652               (progn
12653                 (save-excursion
12654                   (set-buffer summary-buffer)
12655                   (setq gnus-last-article gnus-current-article
12656                         gnus-newsgroup-history (cons gnus-current-article
12657                                                      gnus-newsgroup-history)
12658                         gnus-current-article 0
12659                         gnus-current-headers nil
12660                         gnus-article-current nil)
12661                   (if (eq result 'nneething)
12662                       (gnus-configure-windows 'summary)
12663                     (gnus-configure-windows 'article))
12664                   (gnus-set-global-variables))
12665                 (gnus-set-mode-line 'article))
12666             ;; The result from the `request' was an actual article -
12667             ;; or at least some text that is now displayed in the
12668             ;; article buffer.
12669             (if (and (numberp article)
12670                      (not (eq article gnus-current-article)))
12671                 ;; Seems like a new article has been selected.
12672                 ;; `gnus-current-article' must be an article number.
12673                 (save-excursion
12674                   (set-buffer summary-buffer)
12675                   (setq gnus-last-article gnus-current-article
12676                         gnus-newsgroup-history (cons gnus-current-article
12677                                                      gnus-newsgroup-history)
12678                         gnus-current-article article
12679                         gnus-current-headers 
12680                         (gnus-summary-article-header gnus-current-article)
12681                         gnus-article-current 
12682                         (cons gnus-newsgroup-name gnus-current-article))
12683                   (unless (vectorp gnus-current-headers)
12684                     (setq gnus-current-headers nil))
12685                   (gnus-summary-show-thread)
12686                   (run-hooks 'gnus-mark-article-hook)
12687                   (gnus-set-mode-line 'summary)
12688                   (and (gnus-visual-p 'article-highlight 'highlight)
12689                        (run-hooks 'gnus-visual-mark-article-hook))
12690                   ;; Set the global newsgroup variables here.
12691                   ;; Suggested by Jim Sisolak
12692                   ;; <sisolak@trans4.neep.wisc.edu>.
12693                   (gnus-set-global-variables)
12694                   (setq gnus-have-all-headers 
12695                         (or all-headers gnus-show-all-headers))
12696                   (and gnus-use-cache 
12697                        (vectorp (gnus-summary-article-header article))
12698                        (gnus-cache-possibly-enter-article
12699                         group article
12700                         (gnus-summary-article-header article)
12701                         (memq article gnus-newsgroup-marked)
12702                         (memq article gnus-newsgroup-dormant)
12703                         (memq article gnus-newsgroup-unreads)))))
12704             ;; Hooks for getting information from the article.
12705             ;; This hook must be called before being narrowed.
12706             (let (buffer-read-only)
12707               (run-hooks 'internal-hook)
12708               (run-hooks 'gnus-article-prepare-hook)
12709               ;; Decode MIME message.
12710               (if gnus-show-mime
12711                   (if (or (not gnus-strict-mime)
12712                           (gnus-fetch-field "Mime-Version"))
12713                       (funcall gnus-show-mime-method)
12714                     (funcall gnus-decode-encoded-word-method)))
12715               ;; Perform the article display hooks.
12716               (run-hooks 'gnus-article-display-hook))
12717             ;; Do page break.
12718             (goto-char (point-min))
12719             (and gnus-break-pages (gnus-narrow-to-page))
12720             (gnus-set-mode-line 'article)
12721             (gnus-configure-windows 'article)
12722             (goto-char (point-min))
12723             t))))))
12724
12725 (defun gnus-article-show-all-headers ()
12726   "Show all article headers in article mode buffer."
12727   (save-excursion 
12728     (gnus-article-setup-buffer)
12729     (set-buffer gnus-article-buffer)
12730     (let ((buffer-read-only nil))
12731       (remove-text-properties (point-min) (point-max) 
12732                               gnus-hidden-properties))))
12733
12734 (defun gnus-article-hide-headers-if-wanted ()
12735   "Hide unwanted headers if `gnus-have-all-headers' is nil.
12736 Provided for backwards compatability."
12737   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
12738       gnus-inhibit-hiding
12739       (gnus-article-hide-headers)))
12740
12741 (defun gnus-article-hide-headers (&optional delete)
12742   "Hide unwanted headers and possibly sort them as well."
12743   (interactive "P")
12744   ;; This function might be inhibited.
12745   (unless gnus-inhibit-hiding
12746     (save-excursion
12747       (set-buffer gnus-article-buffer)
12748       (save-restriction
12749         (let ((buffer-read-only nil)
12750               (ignored (when (not (stringp gnus-visible-headers))
12751                          (cond ((stringp gnus-ignored-headers)
12752                                 gnus-ignored-headers)
12753                                ((listp gnus-ignored-headers)
12754                                 (mapconcat 'identity gnus-ignored-headers
12755                                            "\\|")))))
12756               (visible (cond ((stringp gnus-visible-headers)
12757                               gnus-visible-headers)
12758                              ((listp gnus-visible-headers)
12759                               (mapconcat 'identity gnus-visible-headers
12760                                          "\\|"))))
12761               want-list beg want-l)
12762           ;; First we narrow to just the headers.
12763           (widen)
12764           (goto-char (point-min))
12765           ;; Hide any "From " lines at the beginning of (mail) articles. 
12766           (while (looking-at "From ")
12767             (forward-line 1))
12768           (unless (bobp) 
12769             (add-text-properties (point-min) (point) gnus-hidden-properties))
12770           ;; Then treat the rest of the header lines.
12771           (narrow-to-region 
12772            (point) 
12773            (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
12774           ;; Then we use the two regular expressions
12775           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
12776           ;; select which header lines is to remain visible in the
12777           ;; article buffer.
12778           (goto-char (point-min))
12779           (while (re-search-forward "^[^ \t]*:" nil t)
12780             (beginning-of-line)
12781             ;; We add the headers we want to keep to a list and delete
12782             ;; them from the buffer.
12783             (if (or (and visible (looking-at visible))
12784                     (and ignored (not (looking-at ignored))))
12785                 (progn
12786                   (push (buffer-substring
12787                          (setq beg (point))
12788                          (progn 
12789                            (forward-line 1)
12790                            ;; Be sure to get multi-line headers...
12791                            (re-search-forward "^[^ \t]*:" nil t)
12792                            (beginning-of-line) 
12793                            (point)))
12794                         want-list)
12795                   (delete-region beg (point)))
12796               (forward-line 1)))
12797           ;; Sort the headers that we want to display.
12798           (setq want-list (sort want-list 'gnus-article-header-less))
12799           (goto-char (point-min))
12800           (while want-list
12801             (insert (pop want-list)))
12802           ;; We make the unwanted headers invisible.
12803           (if delete
12804               (delete-region (point-min) (point-max))
12805             ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
12806             (add-text-properties 
12807              (point) (point-max) gnus-hidden-properties)))))))
12808
12809 (defsubst gnus-article-header-rank (header)
12810   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
12811   (let ((list gnus-sorted-header-list)
12812         (i 0))
12813     (while list
12814       (when (string-match (car list) header)
12815         (setq list nil))
12816       (setq list (cdr list))
12817       (incf i))
12818     i))
12819
12820 (defun gnus-article-header-less (h1 h2)
12821   "Say whether string H1 is \"less\" than string H2."
12822   (< (gnus-article-header-rank h1)
12823      (gnus-article-header-rank h2)))
12824
12825 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
12826 (defun gnus-article-treat-overstrike ()
12827   "Translate overstrikes into bold text."
12828   (interactive)
12829   (save-excursion
12830     (set-buffer gnus-article-buffer)
12831     (let ((buffer-read-only nil))
12832       (while (search-forward "\b" nil t)
12833         (let ((next (following-char))
12834               (previous (char-after (- (point) 2))))
12835           (cond ((eq next previous)
12836                  (put-text-property (- (point) 2) (point) 'invisible t)
12837                  (put-text-property (point) (1+ (point)) 'face 'bold))
12838                 ((eq next ?_)
12839                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
12840                  (put-text-property
12841                   (- (point) 2) (1- (point)) 'face 'underline))
12842                 ((eq previous ?_)
12843                  (put-text-property (- (point) 2) (point) 'invisible t)
12844                  (put-text-property 
12845                   (point) (1+ (point))  'face 'underline))))))))
12846
12847 (defun gnus-article-word-wrap ()
12848   "Format too long lines."
12849   (interactive)
12850   (save-excursion
12851     (set-buffer gnus-article-buffer)
12852     (let ((buffer-read-only nil)
12853           p)
12854       (widen)
12855       (goto-char (point-min))
12856       (search-forward "\n\n" nil t)
12857       (end-of-line 1)
12858       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
12859             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
12860             (adaptive-fill-mode t))
12861         (while (not (eobp))
12862           (and (>= (current-column) (min fill-column (window-width)))
12863                (/= (preceding-char) ?:)
12864                (fill-paragraph nil))
12865           (end-of-line 2))))))
12866
12867 (defun gnus-article-remove-cr ()
12868   "Remove carriage returns from an article."
12869   (interactive)
12870   (save-excursion
12871     (set-buffer gnus-article-buffer)
12872     (let ((buffer-read-only nil))
12873       (goto-char (point-min))
12874       (while (search-forward "\r" nil t)
12875         (replace-match "" t t)))))
12876
12877 (defun gnus-article-display-x-face (&optional force)
12878   "Look for an X-Face header and display it if present."
12879   (interactive (list 'force))
12880   (save-excursion
12881     (set-buffer gnus-article-buffer)
12882     ;; Delete the old process, if any.
12883     (when (process-status "gnus-x-face")
12884       (delete-process "gnus-x-face"))
12885     (let ((inhibit-point-motion-hooks t)
12886           (case-fold-search nil)
12887           from)
12888       (save-restriction
12889         (gnus-narrow-to-headers)
12890         (setq from (mail-fetch-field "from"))
12891         (goto-char (point-min))
12892         (when (and gnus-article-x-face-command 
12893                    (or force
12894                        ;; Check whether this face is censored.
12895                        (not gnus-article-x-face-too-ugly)
12896                        (and gnus-article-x-face-too-ugly from
12897                             (not (string-match gnus-article-x-face-too-ugly 
12898                                                from))))
12899                    ;; Has to be present.
12900                    (re-search-forward "^X-Face: " nil t))
12901           ;; We now have the area of the buffer where the X-Face is stored.
12902           (let ((beg (point))
12903                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
12904             ;; We display the face.
12905             (if (symbolp gnus-article-x-face-command)
12906                 ;; The command is a lisp function, so we call it.
12907                 (if (gnus-functionp gnus-article-x-face-command)
12908                     (funcall gnus-article-x-face-command beg end)
12909                   (error "%s is not a function" gnus-article-x-face-command))
12910               ;; The command is a string, so we interpret the command
12911               ;; as a, well, command, and fork it off.
12912               (let ((process-connection-type nil))
12913                 (process-kill-without-query
12914                  (start-process 
12915                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
12916                 (process-send-region "gnus-x-face" beg end)
12917                 (process-send-eof "gnus-x-face")))))))))
12918
12919 (defun gnus-headers-decode-quoted-printable ()
12920   "Hack to remove QP encoding from headers."
12921   (let ((case-fold-search t)
12922         (inhibit-point-motion-hooks t)
12923         string)
12924     (goto-char (point-min))
12925     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
12926       (setq string (match-string 1))
12927       (narrow-to-region (match-beginning 0) (match-end 0))
12928       (delete-region (point-min) (point-max))
12929       (insert string)
12930       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
12931       (subst-char-in-region (point-min) (point-max) ?_ ? )
12932       (widen)
12933       (goto-char (point-min)))))
12934        
12935 (defun gnus-article-de-quoted-unreadable (&optional force)
12936   "Do a naive translation of a quoted-printable-encoded article.
12937 This is in no way, shape or form meant as a replacement for real MIME
12938 processing, but is simply a stop-gap measure until MIME support is
12939 written.
12940 If FORCE, decode the article whether it is marked as quoted-printable
12941 or not." 
12942   (interactive (list 'force))
12943   (save-excursion
12944     (set-buffer gnus-article-buffer)
12945     (let ((case-fold-search t)
12946           (buffer-read-only nil)
12947           (type (gnus-fetch-field "content-transfer-encoding")))
12948       (when (or force
12949                 (and type (string-match "quoted-printable" type)))
12950         (goto-char (point-min))
12951         (search-forward "\n\n" nil 'move)
12952         (gnus-mime-decode-quoted-printable (point) (point-max))
12953         (gnus-headers-decode-quoted-printable)))))
12954
12955 (defun gnus-mime-decode-quoted-printable (from to)
12956   "Decode Quoted-Printable in the region between FROM and TO."
12957   (goto-char from)
12958   (while (search-forward "=" to t)
12959     (cond ((eq (following-char) ?\n)
12960            (delete-char -1)
12961            (delete-char 1))
12962           ((looking-at "[0-9A-F][0-9A-F]")
12963            (delete-char -1)
12964            (insert (hexl-hex-string-to-integer
12965                     (buffer-substring (point) (+ 2 (point)))))
12966            (delete-char 2))
12967           ((looking-at "=")
12968            (delete-char 1))
12969           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
12970
12971 (defun gnus-article-hide-pgp ()
12972   "Hide any PGP headers and signatures in the current article."
12973   (interactive)
12974   (save-excursion
12975     (set-buffer gnus-article-buffer)
12976     (let (buffer-read-only beg end)
12977       (widen)
12978       (goto-char (point-min))
12979       ;; Hide the "header".
12980       (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
12981            (add-text-properties (match-beginning 0) (match-end 0)
12982                                 gnus-hidden-properties))
12983       (setq beg (point))
12984       ;; Hide the actual signature.
12985       (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
12986            (setq end (match-beginning 0))
12987            (add-text-properties 
12988             (match-beginning 0)
12989             (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
12990                 (match-end 0)
12991               ;; Perhaps we shouldn't hide to the end of the buffer
12992               ;; if there is no end to the signature?
12993               (point-max))
12994             gnus-hidden-properties))
12995       ;; Hide "- " PGP quotation markers.
12996       (when (and beg end)
12997         (narrow-to-region beg end)
12998         (goto-char (point-min))
12999         (while (re-search-forward "^- " nil t)
13000           (add-text-properties (match-beginning 0) (match-end 0)
13001                                gnus-hidden-properties))
13002         (widen)))))
13003
13004 (defvar gnus-article-time-units
13005   `((year . ,(* 365.25 24 60 60))
13006     (week . ,(* 7 24 60 60))
13007     (day . ,(* 24 60 60))
13008     (hour . ,(* 60 60))
13009     (minute . 60)
13010     (second . 1))
13011   "Mapping from time units to seconds.")
13012
13013 (defun gnus-article-date-ut (&optional type highlight)
13014   "Convert DATE date to universal time in the current article.
13015 If TYPE is `local', convert to local time; if it is `lapsed', output
13016 how much time has lapsed since DATE."
13017   (interactive (list 'ut t))
13018   (let* ((header (or gnus-current-headers 
13019                      (gnus-summary-article-header) ""))
13020          (date (and (vectorp header) (mail-header-date header)))
13021          (date-regexp "^Date: \\|^X-Sent: ")
13022          (inhibit-point-motion-hooks t))
13023     (when (and date (not (string= date "")))
13024       (save-excursion
13025         (set-buffer gnus-article-buffer)
13026         (save-restriction
13027           (gnus-narrow-to-headers)
13028           (let ((buffer-read-only nil))
13029             ;; Delete any old Date headers.
13030             (if (zerop (nnheader-remove-header date-regexp t))
13031                 (beginning-of-line)
13032               (goto-char (point-max)))
13033             (insert
13034              (cond 
13035               ;; Convert to the local timezone.  We have to slap a
13036               ;; `condition-case' round the calls to the timezone
13037               ;; functions since they aren't particularly resistant to
13038               ;; buggy dates.
13039               ((eq type 'local)
13040                (concat "Date: " (condition-case ()
13041                                     (timezone-make-date-arpa-standard date)
13042                                   (error date))
13043                        "\n"))
13044               ;; Convert to Universal Time.
13045               ((eq type 'ut)
13046                (concat "Date: "
13047                        (condition-case ()
13048                            (timezone-make-date-arpa-standard date nil "UT")
13049                          (error date))
13050                        "\n"))
13051               ;; Get the original date from the article.
13052               ((eq type 'original)
13053                (concat "Date: " date "\n"))
13054               ;; Do an X-Sent lapsed format.
13055               ((eq type 'lapsed)
13056                ;; If the date is seriously mangled, the timezone
13057                ;; functions are liable to bug out, so we condition-case
13058                ;; the entire thing.  
13059                (let* ((real-sec (condition-case ()
13060                                     (- (gnus-seconds-since-epoch 
13061                                         (timezone-make-date-arpa-standard
13062                                          (current-time-string) 
13063                                          (current-time-zone) "UT"))
13064                                        (gnus-seconds-since-epoch 
13065                                         (timezone-make-date-arpa-standard 
13066                                          date nil "UT")))
13067                                   (error 0)))
13068                       (sec (abs real-sec))
13069                       num prev)
13070                  (if (zerop sec)
13071                      "X-Sent: Now\n"
13072                    (concat
13073                     "X-Sent: "
13074                     ;; This is a bit convoluted, but basically we go
13075                     ;; through the time units for years, weeks, etc,
13076                     ;; and divide things to see whether that results
13077                     ;; in positive answers.
13078                     (mapconcat 
13079                      (lambda (unit)
13080                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
13081                            ;; The (remaining) seconds are too few to
13082                            ;; be divided into this time unit.
13083                            "" 
13084                          ;; It's big enough, so we output it.
13085                          (setq sec (- sec (* num (cdr unit))))
13086                          (prog1
13087                              (concat (if prev ", " "") (int-to-string 
13088                                                         (floor num))
13089                                      " " (symbol-name (car unit))
13090                                      (if (> num 1) "s" ""))
13091                            (setq prev t))))
13092                      gnus-article-time-units "")
13093                     ;; If dates are odd, then it might appear like the
13094                     ;; article was sent in the future.
13095                     (if (> real-sec 0)
13096                         " ago\n"
13097                       " in the future\n")))))
13098               (t
13099                (error "Unknown conversion type: %s" type)))))
13100           ;; Do highlighting.
13101           (when (and highlight (gnus-visual-p 'article-highlight 'highlight))
13102             (gnus-article-highlight-headers)))))))
13103
13104 (defun gnus-article-date-local (&optional highlight)
13105   "Convert the current article date to the local timezone."
13106   (interactive (list t))
13107   (gnus-article-date-ut 'local highlight))
13108
13109 (defun gnus-article-date-original (&optional highlight)
13110   "Convert the current article date to what it was originally.
13111 This is only useful if you have used some other date conversion
13112 function and want to see what the date was before converting."
13113   (interactive (list t))
13114   (gnus-article-date-ut 'original highlight))
13115
13116 (defun gnus-article-date-lapsed (&optional highlight)
13117   "Convert the current article date to time lapsed since it was sent."
13118   (interactive (list t))
13119   (gnus-article-date-ut 'lapsed highlight))
13120
13121 (defun gnus-article-maybe-highlight ()
13122   "Do some article highlighting if `gnus-visual' is non-nil."
13123   (if (gnus-visual-p 'article-highlight 'highlight)
13124       (gnus-article-highlight-some)))
13125
13126 ;; Article savers.
13127
13128 (defun gnus-output-to-rmail (file-name)
13129   "Append the current article to an Rmail file named FILE-NAME."
13130   (require 'rmail)
13131   ;; Most of these codes are borrowed from rmailout.el.
13132   (setq file-name (expand-file-name file-name))
13133   (setq rmail-default-rmail-file file-name)
13134   (let ((artbuf (current-buffer))
13135         (tmpbuf (get-buffer-create " *Gnus-output*")))
13136     (save-excursion
13137       (or (get-file-buffer file-name)
13138           (file-exists-p file-name)
13139           (if (gnus-yes-or-no-p
13140                (concat "\"" file-name "\" does not exist, create it? "))
13141               (let ((file-buffer (create-file-buffer file-name)))
13142                 (save-excursion
13143                   (set-buffer file-buffer)
13144                   (rmail-insert-rmail-file-header)
13145                   (let ((require-final-newline nil))
13146                     (write-region (point-min) (point-max) file-name t 1)))
13147                 (kill-buffer file-buffer))
13148             (error "Output file does not exist")))
13149       (set-buffer tmpbuf)
13150       (buffer-disable-undo (current-buffer))
13151       (erase-buffer)
13152       (insert-buffer-substring artbuf)
13153       (gnus-convert-article-to-rmail)
13154       ;; Decide whether to append to a file or to an Emacs buffer.
13155       (let ((outbuf (get-file-buffer file-name)))
13156         (if (not outbuf)
13157             (append-to-file (point-min) (point-max) file-name)
13158           ;; File has been visited, in buffer OUTBUF.
13159           (set-buffer outbuf)
13160           (let ((buffer-read-only nil)
13161                 (msg (and (boundp 'rmail-current-message)
13162                           (symbol-value 'rmail-current-message))))
13163             ;; If MSG is non-nil, buffer is in RMAIL mode.
13164             (if msg
13165                 (progn (widen)
13166                        (narrow-to-region (point-max) (point-max))))
13167             (insert-buffer-substring tmpbuf)
13168             (if msg
13169                 (progn
13170                   (goto-char (point-min))
13171                   (widen)
13172                   (search-backward "\^_")
13173                   (narrow-to-region (point) (point-max))
13174                   (goto-char (1+ (point-min)))
13175                   (rmail-count-new-messages t)
13176                   (rmail-show-message msg)))))))
13177     (kill-buffer tmpbuf)))
13178
13179 (defun gnus-output-to-file (file-name)
13180   "Append the current article to a file named FILE-NAME."
13181   (setq file-name (expand-file-name file-name))
13182   (let ((artbuf (current-buffer))
13183         (tmpbuf (get-buffer-create " *Gnus-output*")))
13184     (save-excursion
13185       (set-buffer tmpbuf)
13186       (buffer-disable-undo (current-buffer))
13187       (erase-buffer)
13188       (insert-buffer-substring artbuf)
13189       ;; Append newline at end of the buffer as separator, and then
13190       ;; save it to file.
13191       (goto-char (point-max))
13192       (insert "\n")
13193       (append-to-file (point-min) (point-max) file-name))
13194     (kill-buffer tmpbuf)))
13195
13196 (defun gnus-convert-article-to-rmail ()
13197   "Convert article in current buffer to Rmail message format."
13198   (let ((buffer-read-only nil))
13199     ;; Convert article directly into Babyl format.
13200     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
13201     (goto-char (point-min))
13202     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
13203     (while (search-forward "\n\^_" nil t) ;single char
13204       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
13205     (goto-char (point-max))
13206     (insert "\^_")))
13207
13208 (defun gnus-narrow-to-page (&optional arg)
13209   "Narrow the article buffer to a page.
13210 If given a numerical ARG, move forward ARG pages."
13211   (interactive "P")
13212   (setq arg (if arg (prefix-numeric-value arg) 0))
13213   (save-excursion
13214     (set-buffer gnus-article-buffer)
13215     (goto-char (point-min))
13216     (widen)
13217     (when 
13218         (cond ((< arg 0)
13219                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
13220               ((> arg 0)
13221                (re-search-forward page-delimiter nil 'move arg)))
13222       (goto-char (match-end 0)))
13223     (when (and (gnus-visual-p 'page-marker)
13224                (not (bolp)))
13225       (gnus-insert-prev-page-button))
13226     (narrow-to-region
13227      (point)
13228      (if (re-search-forward page-delimiter nil 'move)
13229          (prog1 (match-beginning 0) 
13230            (when (and (gnus-visual-p 'page-marker)
13231                       (not (bolp)))
13232              (gnus-insert-next-page-button)))
13233        (point)))))
13234
13235 ;; Article mode commands
13236
13237 (defun gnus-article-next-page (&optional lines)
13238   "Show next page of current article.
13239 If end of article, return non-nil.  Otherwise return nil.
13240 Argument LINES specifies lines to be scrolled up."
13241   (interactive "P")
13242   (move-to-window-line -1)
13243   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
13244   (if (save-excursion
13245         (end-of-line)
13246         (and (pos-visible-in-window-p)  ;Not continuation line.
13247              (eobp)))
13248       ;; Nothing in this page.
13249       (if (or (not gnus-break-pages)
13250               (save-excursion
13251                 (save-restriction
13252                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
13253           t                             ;Nothing more.
13254         (gnus-narrow-to-page 1)         ;Go to next page.
13255         nil)
13256     ;; More in this page.
13257     (condition-case ()
13258         (scroll-up lines)
13259       (end-of-buffer
13260        ;; Long lines may cause an end-of-buffer error.
13261        (goto-char (point-max))))
13262     nil))
13263
13264 (defun gnus-article-prev-page (&optional lines)
13265   "Show previous page of current article.
13266 Argument LINES specifies lines to be scrolled down."
13267   (interactive "P")
13268   (move-to-window-line 0)
13269   (if (and gnus-break-pages
13270            (bobp)
13271            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
13272       (progn
13273         (gnus-narrow-to-page -1)        ;Go to previous page.
13274         (goto-char (point-max))
13275         (recenter -1))
13276     (condition-case ()
13277         (scroll-down lines)
13278       (error nil))))
13279
13280 (defun gnus-article-refer-article ()
13281   "Read article specified by message-id around point."
13282   (interactive)
13283   (search-forward ">" nil t)            ;Move point to end of "<....>".
13284   (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
13285       (let ((message-id (match-string 1)))
13286         (set-buffer gnus-summary-buffer)
13287         (gnus-summary-refer-article message-id))
13288     (error "No references around point")))
13289
13290 (defun gnus-article-show-summary ()
13291   "Reconfigure windows to show summary buffer."
13292   (interactive)
13293   (gnus-configure-windows 'article)
13294   (gnus-summary-goto-subject gnus-current-article))
13295
13296 (defun gnus-article-describe-briefly ()
13297   "Describe article mode commands briefly."
13298   (interactive)
13299   (gnus-message 6
13300                 (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")))
13301
13302 (defun gnus-article-summary-command ()
13303   "Execute the last keystroke in the summary buffer."
13304   (interactive)
13305   (let ((obuf (current-buffer))
13306         (owin (current-window-configuration))
13307         func)
13308     (switch-to-buffer gnus-summary-buffer 'norecord)
13309     (setq func (lookup-key (current-local-map) (this-command-keys)))
13310     (call-interactively func)
13311     (set-buffer obuf)
13312     (set-window-configuration owin)
13313     (set-window-point (get-buffer-window (current-buffer)) (point))))
13314
13315 (defun gnus-article-summary-command-nosave ()
13316   "Execute the last keystroke in the summary buffer."
13317   (interactive)
13318   (let (func)
13319     (pop-to-buffer gnus-summary-buffer 'norecord)
13320     (setq func (lookup-key (current-local-map) (this-command-keys)))
13321     (call-interactively func)))
13322
13323 \f
13324 ;; Basic ideas by emv@math.lsa.umich.edu (Edward Vielmetti)
13325
13326 ;;;###autoload
13327 (defalias 'gnus-batch-kill 'gnus-batch-score)
13328 ;;;###autoload
13329 (defun gnus-batch-score ()
13330   "Run batched scoring.
13331 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
13332 Newsgroups is a list of strings in Bnews format.  If you want to score
13333 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
13334 score the alt hierarchy, you'd say \"!alt.all\"."
13335   (interactive)
13336   (let* ((yes-and-no
13337           (gnus-newsrc-parse-options
13338            (apply (function concat)
13339                   (mapcar (lambda (g) (concat g " "))
13340                           command-line-args-left))))
13341          (gnus-expert-user t)
13342          (nnmail-spool-file nil)
13343          (gnus-use-dribble-file nil)
13344          (yes (car yes-and-no))
13345          (no (cdr yes-and-no))
13346          group newsrc entry
13347          ;; Disable verbose message.
13348          gnus-novice-user gnus-large-newsgroup)
13349     ;; Eat all arguments.
13350     (setq command-line-args-left nil)
13351     ;; Start Gnus.
13352     (gnus)
13353     ;; Apply kills to specified newsgroups in command line arguments.
13354     (setq newsrc (cdr gnus-newsrc-alist))
13355     (while newsrc
13356       (setq group (car (car newsrc)))
13357       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
13358       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
13359                (and (car entry)
13360                     (or (eq (car entry) t)
13361                         (not (zerop (car entry)))))
13362                (if yes (string-match yes group) t)
13363                (or (null no) (not (string-match no group))))
13364           (progn
13365             (gnus-summary-read-group group nil t nil t)
13366             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
13367                  (gnus-summary-exit))))
13368       (setq newsrc (cdr newsrc)))
13369     ;; Exit Emacs.
13370     (switch-to-buffer gnus-group-buffer)
13371     (gnus-group-save-newsrc)))
13372
13373 (defun gnus-apply-kill-file ()
13374   "Apply a kill file to the current newsgroup.
13375 Returns the number of articles marked as read."
13376   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
13377           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
13378       (gnus-apply-kill-file-internal)
13379     0))
13380
13381 (defun gnus-kill-save-kill-buffer ()
13382   (save-excursion
13383     (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
13384       (if (get-file-buffer file)
13385           (progn
13386             (set-buffer (get-file-buffer file))
13387             (and (buffer-modified-p) (save-buffer))
13388             (kill-buffer (current-buffer)))))))
13389
13390 (defvar gnus-kill-file-name "KILL"
13391   "Suffix of the kill files.")
13392
13393 (defun gnus-newsgroup-kill-file (newsgroup)
13394   "Return the name of a kill file name for NEWSGROUP.
13395 If NEWSGROUP is nil, return the global kill file name instead."
13396   (cond ((or (null newsgroup)
13397              (string-equal newsgroup ""))
13398          ;; The global KILL file is placed at top of the directory.
13399          (expand-file-name gnus-kill-file-name
13400                            (or gnus-kill-files-directory "~/News")))
13401         ((gnus-use-long-file-name 'not-kill)
13402          ;; Append ".KILL" to newsgroup name.
13403          (expand-file-name (concat (gnus-newsgroup-saveable-name newsgroup)
13404                                    "." gnus-kill-file-name)
13405                            (or gnus-kill-files-directory "~/News")))
13406         (t
13407          ;; Place "KILL" under the hierarchical directory.
13408          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
13409                                    "/" gnus-kill-file-name)
13410                            (or gnus-kill-files-directory "~/News")))))
13411
13412 \f
13413 ;;;
13414 ;;; Dribble file
13415 ;;;
13416
13417 (defvar gnus-dribble-ignore nil)
13418 (defvar gnus-dribble-eval-file nil)
13419
13420 (defun gnus-dribble-file-name ()
13421   (concat 
13422    (if gnus-dribble-directory
13423        (concat (file-name-as-directory gnus-dribble-directory)
13424                (file-name-nondirectory gnus-current-startup-file))
13425      gnus-current-startup-file)
13426    "-dribble"))
13427
13428 (defun gnus-dribble-enter (string)
13429   (if (and (not gnus-dribble-ignore)
13430            (or gnus-dribble-buffer
13431                gnus-slave)
13432            (buffer-name gnus-dribble-buffer))
13433       (let ((obuf (current-buffer)))
13434         (set-buffer gnus-dribble-buffer)
13435         (insert string "\n")
13436         (set-window-point (get-buffer-window (current-buffer)) (point-max))
13437         (set-buffer obuf))))
13438
13439 (defun gnus-dribble-read-file ()
13440   (let ((dribble-file (gnus-dribble-file-name)))
13441     (save-excursion 
13442       (set-buffer (setq gnus-dribble-buffer 
13443                         (get-buffer-create 
13444                          (file-name-nondirectory dribble-file))))
13445       (gnus-add-current-to-buffer-list)
13446       (erase-buffer)
13447       (setq buffer-file-name dribble-file)
13448       (auto-save-mode t)
13449       (buffer-disable-undo (current-buffer))
13450       (bury-buffer (current-buffer))
13451       (set-buffer-modified-p nil)
13452       (let ((auto (make-auto-save-file-name))
13453             (gnus-dribble-ignore t))
13454         (if (or (file-exists-p auto) (file-exists-p dribble-file))
13455             (progn
13456               (if (file-newer-than-file-p auto dribble-file)
13457                   (setq dribble-file auto))
13458               (insert-file-contents dribble-file)
13459               (if (not (zerop (buffer-size)))
13460                   (set-buffer-modified-p t))
13461               (if (gnus-y-or-n-p 
13462                    "Auto-save file exists.  Do you want to read it? ")
13463                   (setq gnus-dribble-eval-file t))))))))
13464
13465 (defun gnus-dribble-eval-file ()
13466   (if (not gnus-dribble-eval-file)
13467       ()
13468     (setq gnus-dribble-eval-file nil)
13469     (save-excursion
13470       (let ((gnus-dribble-ignore t))
13471         (set-buffer gnus-dribble-buffer)
13472         (eval-buffer (current-buffer))))))
13473
13474 (defun gnus-dribble-delete-file ()
13475   (if (file-exists-p (gnus-dribble-file-name))
13476       (delete-file (gnus-dribble-file-name)))
13477   (if gnus-dribble-buffer
13478       (save-excursion
13479         (set-buffer gnus-dribble-buffer)
13480         (let ((auto (make-auto-save-file-name)))
13481           (if (file-exists-p auto)
13482               (delete-file auto))
13483           (erase-buffer)
13484           (set-buffer-modified-p nil)))))
13485
13486 (defun gnus-dribble-save ()
13487   (if (and gnus-dribble-buffer
13488            (buffer-name gnus-dribble-buffer))
13489       (save-excursion
13490         (set-buffer gnus-dribble-buffer)
13491         (save-buffer))))
13492
13493 (defun gnus-dribble-clear ()
13494   (save-excursion
13495     (if (gnus-buffer-exists-p gnus-dribble-buffer)
13496         (progn
13497           (set-buffer gnus-dribble-buffer)
13498           (erase-buffer)
13499           (set-buffer-modified-p nil)
13500           (setq buffer-saved-size (buffer-size))))))
13501
13502 ;;;
13503 ;;; Server Communication
13504 ;;;
13505
13506 (defun gnus-start-news-server (&optional confirm)
13507   "Open a method for getting news.
13508 If CONFIRM is non-nil, the user will be asked for an NNTP server."
13509   (let (how)
13510     (if gnus-current-select-method
13511         ;; Stream is already opened.
13512         nil
13513       ;; Open NNTP server.
13514       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
13515       (if confirm
13516           (progn
13517             ;; Read server name with completion.
13518             (setq gnus-nntp-server
13519                   (completing-read "NNTP server: "
13520                                    (mapcar (lambda (server) (list server))
13521                                            (cons (list gnus-nntp-server)
13522                                                  gnus-secondary-servers))
13523                                    nil nil gnus-nntp-server))))
13524
13525       (if (and gnus-nntp-server 
13526                (stringp gnus-nntp-server)
13527                (not (string= gnus-nntp-server "")))
13528           (setq gnus-select-method
13529                 (cond ((or (string= gnus-nntp-server "")
13530                            (string= gnus-nntp-server "::"))
13531                        (list 'nnspool (system-name)))
13532                       ((string-match "^:" gnus-nntp-server)
13533                        (list 'nnmh gnus-nntp-server 
13534                              (list 'nnmh-directory 
13535                                    (file-name-as-directory
13536                                     (expand-file-name
13537                                      (concat "~/" (substring
13538                                                    gnus-nntp-server 1)))))
13539                              (list 'nnmh-get-new-mail nil)))
13540                       (t
13541                        (list 'nntp gnus-nntp-server)))))
13542
13543       (setq how (car gnus-select-method))
13544       (cond ((eq how 'nnspool)
13545              (require 'nnspool)
13546              (gnus-message 5 "Looking up local news spool..."))
13547             ((eq how 'nnmh)
13548              (require 'nnmh)
13549              (gnus-message 5 "Looking up mh spool..."))
13550             (t
13551              (require 'nntp)))
13552       (setq gnus-current-select-method gnus-select-method)
13553       (run-hooks 'gnus-open-server-hook)
13554       (or 
13555        ;; gnus-open-server-hook might have opened it
13556        (gnus-server-opened gnus-select-method)  
13557        (gnus-open-server gnus-select-method)
13558        (gnus-y-or-n-p
13559         (format
13560          "%s open error: '%s'.  Continue? "
13561          (nth 1 gnus-select-method)
13562          (gnus-status-message gnus-select-method)))
13563        (progn
13564          (gnus-message 1 "Couldn't open server on %s" 
13565                        (nth 1 gnus-select-method))
13566          (ding)
13567          nil)))))
13568
13569 (defun gnus-check-server (&optional method)
13570   "Check whether the connection to METHOD is down.
13571 If METHOD is nil, use `gnus-select-method'.
13572 If it is down, start it up (again)."
13573   (let ((method (or method gnus-select-method)))
13574     ;; Transform virtual server names into select methods.
13575     (when (stringp method)
13576       (setq method (gnus-server-to-method method)))
13577     (if (gnus-server-opened method)
13578         ;; The stream is already opened.
13579         t
13580       ;; Open the server.
13581       (gnus-message 5 "Opening %s server on %s..." (car method) (nth 1 method))
13582       (run-hooks 'gnus-open-server-hook)
13583       (prog1
13584           (gnus-open-server method)
13585         (message "")))))
13586
13587 (defun gnus-get-function (method function)
13588   "Return a function symbol based on METHOD and FUNCTION."
13589   ;; Translate server names into methods.
13590   (unless method
13591     (error "Attempted use of a nil select method"))
13592   (when (stringp method)
13593     (setq method (gnus-server-to-method method)))
13594   (let ((func (intern (format "%s-%s" (car method) function))))
13595     ;; If the functions isn't bound, we require the backend in
13596     ;; question.  
13597     (unless (fboundp func)
13598       (require (car method))
13599       (unless (fboundp func)
13600         ;; This backend doesn't implement this function.
13601         (error "No such function: %s" func)))
13602     func))
13603
13604 ;;; Interface functions to the backends.
13605
13606 (defun gnus-open-server (method)
13607   "Open a connection to METHOD."
13608   (let ((elem (assoc method gnus-opened-servers)))
13609     ;; If this method was previously denied, we just return nil.
13610     (if (eq (nth 1 elem) 'denied)
13611         (progn
13612           (gnus-message 1 "Denied server")
13613           nil)
13614       ;; Open the server.
13615       (let ((result
13616              (funcall (gnus-get-function method 'open-server)
13617                       (nth 1 method) (nthcdr 2 method))))
13618         ;; If this hasn't been opened before, we add it to the list.
13619         (unless elem 
13620           (setq elem (list method nil)
13621                 gnus-opened-servers (cons elem gnus-opened-servers)))
13622         ;; Set the status of this server.
13623         (setcar (cdr elem) (if result 'ok 'denied))
13624         ;; Return the result from the "open" call.
13625         result))))
13626
13627 (defun gnus-close-server (method)
13628   "Close the connection to METHOD."
13629   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
13630
13631 (defun gnus-request-list (method)
13632   "Request the active file from METHOD."
13633   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
13634
13635 (defun gnus-request-list-newsgroups (method)
13636   "Request the newsgroups file from METHOD."
13637   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
13638
13639 (defun gnus-request-newgroups (date method)
13640   "Request all new groups since DATE from METHOD."
13641   (funcall (gnus-get-function method 'request-newgroups) 
13642            date (nth 1 method)))
13643
13644 (defun gnus-server-opened (method)
13645   "Check whether a connection to METHOD has been opened."
13646   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
13647
13648 (defun gnus-status-message (method)
13649   "Return the status message from METHOD.
13650 If METHOD is a string, it is interpreted as a group name.   The method
13651 this group uses will be queried."
13652   (let ((method (if (stringp method) (gnus-find-method-for-group method)
13653                   method)))
13654     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
13655
13656 (defun gnus-request-group (group &optional dont-check)
13657   "Request GROUP.  If DONT-CHECK, no information is required."
13658   (let ((method (gnus-find-method-for-group group)))
13659     (funcall (gnus-get-function method 'request-group) 
13660              (gnus-group-real-name group) (nth 1 method) dont-check)))
13661
13662 (defun gnus-request-asynchronous (group &optional articles)
13663   "Request that GROUP behave asynchronously.
13664 ARTICLES is the `data' of the group."
13665   (let ((method (gnus-find-method-for-group group)))
13666     (funcall (gnus-get-function method 'request-asynchronous) 
13667              (gnus-group-real-name group) (nth 1 method) articles)))
13668
13669 (defun gnus-list-active-group (group)
13670   "Request active information on GROUP."
13671   (let ((method (gnus-find-method-for-group group))
13672         (func 'list-active-group))
13673     (when (gnus-check-backend-function func group)
13674       (funcall (gnus-get-function method func) 
13675                (gnus-group-real-name group) (nth 1 method)))))
13676
13677 (defun gnus-request-group-description (group)
13678   "Request a description of GROUP."
13679   (let ((method (gnus-find-method-for-group group))
13680         (func 'request-group-description))
13681     (when (gnus-check-backend-function func group)
13682       (funcall (gnus-get-function method func) 
13683                (gnus-group-real-name group) (nth 1 method)))))
13684
13685 (defun gnus-close-group (group)
13686   "Request the GROUP be closed."
13687   (let ((method (gnus-find-method-for-group group)))
13688     (funcall (gnus-get-function method 'close-group) 
13689              (gnus-group-real-name group) (nth 1 method))))
13690
13691 (defun gnus-retrieve-headers (articles group &optional fetch-old)
13692   "Request headers for ARTICLES in GROUP.
13693 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
13694   (let ((method (gnus-find-method-for-group group)))
13695     (if (and gnus-use-cache (numberp (car articles)))
13696         (gnus-cache-retrieve-headers articles group fetch-old)
13697       (funcall (gnus-get-function method 'retrieve-headers) 
13698                articles (gnus-group-real-name group) (nth 1 method)
13699                fetch-old))))
13700
13701 (defun gnus-retrieve-groups (groups method)
13702   "Request active information on GROUPS from METHOD."
13703   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
13704
13705 (defun gnus-request-type (group &optional article)
13706   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
13707   (let ((method (gnus-find-method-for-group group)))
13708     (if (not (gnus-check-backend-function 'request-type (car method)))
13709         'unknown
13710       (funcall (gnus-get-function method 'request-type)
13711                (gnus-group-real-name group) article))))
13712
13713 (defun gnus-request-article (article group &optional buffer)
13714   "Request the ARTICLE in GROUP.
13715 ARTICLE can either be an article number or an article Message-ID.
13716 If BUFFER, insert the article in that group."
13717   (let ((method (gnus-find-method-for-group group)))
13718     (funcall (gnus-get-function method 'request-article) 
13719              article (gnus-group-real-name group) (nth 1 method) buffer)))
13720
13721 (defun gnus-request-head (article group)
13722   "Request the head of ARTICLE in GROUP."
13723   (let ((method (gnus-find-method-for-group group)))
13724     (funcall (gnus-get-function method 'request-head) 
13725              article (gnus-group-real-name group) (nth 1 method))))
13726
13727 (defun gnus-request-body (article group)
13728   "Request the body of ARTICLE in GROUP."
13729   (let ((method (gnus-find-method-for-group group)))
13730     (funcall (gnus-get-function method 'request-body) 
13731              article (gnus-group-real-name group) (nth 1 method))))
13732
13733 (defun gnus-request-post (method)
13734   "Post the current buffer using METHOD."
13735   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
13736
13737 (defun gnus-request-scan (group method)
13738   "Request a SCAN being performed in GROUP from METHOD.
13739 If GROUP is nil, all groups on METHOD are scanned."
13740   (let ((method (if group (gnus-find-method-for-group group) method)))
13741     (funcall (gnus-get-function method 'request-scan) 
13742              (and group (gnus-group-real-name group)) (nth 1 method))))
13743
13744 (defun gnus-request-update-info (info method)
13745   "Request that METHOD update INFO."
13746   (when (gnus-check-backend-function 'request-update-info method)
13747     (funcall (gnus-get-function method 'request-update-info) 
13748              (gnus-group-real-name (gnus-info-group info)) 
13749              info (nth 1 method))))
13750
13751 (defun gnus-request-expire-articles (articles group &optional force)
13752   (let ((method (gnus-find-method-for-group group)))
13753     (funcall (gnus-get-function method 'request-expire-articles) 
13754              articles (gnus-group-real-name group) (nth 1 method)
13755              force)))
13756
13757 (defun gnus-request-move-article 
13758   (article group server accept-function &optional last)
13759   (let ((method (gnus-find-method-for-group group)))
13760     (funcall (gnus-get-function method 'request-move-article) 
13761              article (gnus-group-real-name group) 
13762              (nth 1 method) accept-function last)))
13763
13764 (defun gnus-request-accept-article (group &optional last)
13765   (let ((func (if (symbolp group) group
13766                 (car (gnus-find-method-for-group group)))))
13767     (funcall (intern (format "%s-request-accept-article" func))
13768              (if (stringp group) (gnus-group-real-name group) group)
13769              last)))
13770
13771 (defun gnus-request-replace-article (article group buffer)
13772   (let ((func (car (gnus-find-method-for-group group))))
13773     (funcall (intern (format "%s-request-replace-article" func))
13774              article (gnus-group-real-name group) buffer)))
13775
13776 (defun gnus-request-create-group (group)
13777   (let ((method (gnus-find-method-for-group group)))
13778     (funcall (gnus-get-function method 'request-create-group) 
13779              (gnus-group-real-name group) (nth 1 method))))
13780
13781 (defun gnus-request-delete-group (group &optional force)
13782   (let ((method (gnus-find-method-for-group group)))
13783     (funcall (gnus-get-function method 'request-delete-group) 
13784              (gnus-group-real-name group) force (nth 1 method))))
13785
13786 (defun gnus-request-rename-group (group new-name)
13787   (let ((method (gnus-find-method-for-group group)))
13788     (funcall (gnus-get-function method 'request-rename-group) 
13789              (gnus-group-real-name group) 
13790              (gnus-group-real-name new-name) (nth 1 method))))
13791
13792 (defun gnus-post-method (group force-group-method)
13793   "Return the posting method based on GROUP and FORCE."
13794   (let ((group-method (if (stringp group)
13795                           (gnus-find-method-for-group group)
13796                         group)))
13797     (cond 
13798      ;; If the group-method is nil (which shouldn't happen) we use 
13799      ;; the default method.
13800      ((null group-method)
13801       gnus-select-method)
13802      ;; We want this group's method.
13803      (force-group-method group-method)
13804      ;; Override normal method.
13805      ((and gnus-post-method
13806            (or (gnus-method-option-p group-method 'post)
13807                (gnus-method-option-p group-method 'post-mail)))
13808       gnus-post-method)
13809      ;; Perhaps this is a mail group?
13810      ((and (not (gnus-member-of-valid 'post group))
13811            (not (gnus-method-option-p group-method 'post-mail)))
13812       group-method)
13813      ;; Use the normal select method.
13814      (t gnus-select-method))))
13815
13816 (defun gnus-member-of-valid (symbol group)
13817   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
13818   (memq symbol (assoc
13819                 (symbol-name (car (gnus-find-method-for-group group)))
13820                 gnus-valid-select-methods)))
13821
13822 (defun gnus-method-option-p (method option)
13823   "Return non-nil if select METHOD has OPTION as a parameter."
13824   (memq option (assoc (format "%s" (car method))
13825                       gnus-valid-select-methods)))
13826
13827 (defmacro gnus-server-equal (ss1 ss2)
13828   "Say whether two servers are equal."
13829   `(let ((s1 ,ss1)
13830          (s2 ,ss2))
13831      (or (equal s1 s2)
13832          (and (= (length s1) (length s2))
13833               (progn
13834                 (while (and s1 (member (car s1) s2))
13835                   (setq s1 (cdr s1)))
13836                 (null s1))))))
13837
13838 (defun gnus-server-extend-method (group method)
13839   ;; This function "extends" a virtual server.  If the server is
13840   ;; "hello", and the select method is ("hello" (my-var "something")) 
13841   ;; in the group "alt.alt", this will result in a new virtual server
13842   ;; called "helly+alt.alt".
13843   (let ((entry
13844          (gnus-copy-sequence 
13845           (if (equal (car method) "native") gnus-select-method
13846             (cdr (assoc (car method) gnus-server-alist))))))
13847     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
13848     (nconc entry (cdr method))))
13849
13850 (defun gnus-find-method-for-group (group &optional info)
13851   "Find the select method that GROUP uses."
13852   (or gnus-override-method
13853       (and (not group)
13854            gnus-select-method)
13855       (let ((info (or info (gnus-get-info group)))
13856             method)
13857         (if (or (not info)
13858                 (not (setq method (gnus-info-method info))))
13859             (setq method gnus-select-method)
13860           (setq method
13861                 (cond ((stringp method)
13862                        (gnus-server-to-method method))
13863                       ((stringp (car method))
13864                        (gnus-server-extend-method group method))
13865                       (t
13866                        method))))
13867         (gnus-server-add-address method))))
13868
13869 (defun gnus-check-backend-function (func group)
13870   "Check whether GROUP supports function FUNC."
13871   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
13872                   group)))
13873     (fboundp (intern (format "%s-%s" method func)))))
13874
13875 (defun gnus-methods-using (feature)
13876   "Find all methods that have FEATURE."
13877   (let ((valids gnus-valid-select-methods)
13878         outs)
13879     (while valids
13880       (if (memq feature (car valids)) 
13881           (setq outs (cons (car valids) outs)))
13882       (setq valids (cdr valids)))
13883     outs))
13884
13885 ;;; 
13886 ;;; Active & Newsrc File Handling
13887 ;;;
13888
13889 ;; Newsrc related functions.
13890 ;; Gnus internal format of gnus-newsrc-alist:
13891 ;; (("alt.general" 3 (1 . 1))
13892 ;;  ("alt.misc"    3 ((1 . 10) (12 . 15)))
13893 ;;  ("alt.test"    7 (1 . 99) (45 57 93)) ...)
13894 ;; The first item is the group name; the second is the subscription
13895 ;; level; the third is either a range of a list of ranges of read
13896 ;; articles, the optional fourth element is a list of marked articles,
13897 ;; the optional fifth element is the select method.
13898 ;;
13899 ;; Gnus internal format of gnus-newsrc-hashtb:
13900 ;; (95 ("alt.general" 3 (1 . 1)) ("alt.misc" 3 ((1 . 10) (12 . 15))) ...)
13901 ;; This is the entry for "alt.misc". The first element is the number
13902 ;; of unread articles in "alt.misc". The cdr of this entry is the
13903 ;; element *before* "alt.misc" in gnus-newsrc-alist, which makes is
13904 ;; trivial to remove or add new elements into gnus-newsrc-alist
13905 ;; without scanning the entire list.  So, to get the actual information
13906 ;; of "alt.misc", you'd say something like 
13907 ;; (nth 2 (gnus-gethash "alt.misc" gnus-newsrc-hashtb))
13908 ;;
13909 ;; Gnus internal format of gnus-active-hashtb:
13910 ;; ((1 . 1))
13911 ;;  (5 . 10))
13912 ;;  (67 . 99)) ...)
13913 ;; The only element in each entry in this hash table is a range of
13914 ;; (possibly) available articles. (Articles in this range may have
13915 ;; been expired or canceled.)
13916 ;;
13917 ;; Gnus internal format of gnus-killed-list and gnus-zombie-list:
13918 ;; ("alt.misc" "alt.test" "alt.general" ...)
13919
13920 (defun gnus-setup-news (&optional rawfile level)
13921   "Setup news information.
13922 If RAWFILE is non-nil, the .newsrc file will also be read.
13923 If LEVEL is non-nil, the news will be set up at level LEVEL."
13924   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
13925     ;; Clear some variables to re-initialize news information.
13926     (if init (setq gnus-newsrc-alist nil 
13927                    gnus-active-hashtb nil))
13928
13929     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
13930     (if init (gnus-read-newsrc-file rawfile))
13931
13932     ;; If we don't read the complete active file, we fill in the
13933     ;; hashtb here. 
13934     (if (or (null gnus-read-active-file)
13935             (eq gnus-read-active-file 'some))
13936         (gnus-update-active-hashtb-from-killed))
13937
13938     ;; Read the active file and create `gnus-active-hashtb'.
13939     ;; If `gnus-read-active-file' is nil, then we just create an empty
13940     ;; hash table.  The partial filling out of the hash table will be
13941     ;; done in `gnus-get-unread-articles'.
13942     (and gnus-read-active-file 
13943          (not level)
13944          (gnus-read-active-file))
13945
13946     (or gnus-active-hashtb
13947         (setq gnus-active-hashtb (make-vector 4095 0)))
13948
13949     ;; Initialize the cache.
13950     (when gnus-use-cache
13951       (gnus-cache-open))
13952
13953     ;; Possibly eval the dribble file.
13954     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
13955
13956     (gnus-update-format-specifications)
13957
13958     ;; Find new newsgroups and treat them.
13959     (if (and init gnus-check-new-newsgroups gnus-read-active-file (not level)
13960              (gnus-check-server gnus-select-method))
13961         (gnus-find-new-newsgroups))
13962
13963     ;; Find the number of unread articles in each non-dead group.
13964     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
13965       (gnus-get-unread-articles level))
13966
13967     (if (and init gnus-check-bogus-newsgroups 
13968              gnus-read-active-file (not level)
13969              (gnus-server-opened gnus-select-method))
13970         (gnus-check-bogus-newsgroups))))
13971
13972 (defun gnus-find-new-newsgroups ()
13973   "Search for new newsgroups and add them.
13974 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
13975 The `-n' option line from .newsrc is respected."
13976   (interactive)
13977   (or (gnus-check-first-time-used)
13978       (if (or (consp gnus-check-new-newsgroups)
13979               (eq gnus-check-new-newsgroups 'ask-server))
13980           (gnus-ask-server-for-new-groups)
13981         (let ((groups 0)
13982               group new-newsgroups)
13983           (gnus-message 5 "Looking for new newsgroups...")
13984           (or gnus-have-read-active-file (gnus-read-active-file))
13985           (setq gnus-newsrc-last-checked-date (current-time-string))
13986           (if (not gnus-killed-hashtb) (gnus-make-hashtable-from-killed))
13987           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
13988           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
13989           (mapatoms
13990            (lambda (sym)
13991              (if (or (null (setq group (symbol-name sym)))
13992                      (not (boundp sym))
13993                      (null (symbol-value sym))
13994                      (gnus-gethash group gnus-killed-hashtb)
13995                      (gnus-gethash group gnus-newsrc-hashtb))
13996                  ()
13997                (let ((do-sub (gnus-matches-options-n group)))
13998                  (cond 
13999                   ((eq do-sub 'subscribe)
14000                    (setq groups (1+ groups))
14001                    (gnus-sethash group group gnus-killed-hashtb)
14002                    (funcall gnus-subscribe-options-newsgroup-method group))
14003                   ((eq do-sub 'ignore)
14004                    nil)
14005                   (t
14006                    (setq groups (1+ groups))
14007                    (gnus-sethash group group gnus-killed-hashtb)
14008                    (if gnus-subscribe-hierarchical-interactive
14009                        (setq new-newsgroups (cons group new-newsgroups))
14010                      (funcall gnus-subscribe-newsgroup-method group)))))))
14011            gnus-active-hashtb)
14012           (if new-newsgroups 
14013               (gnus-subscribe-hierarchical-interactive new-newsgroups))
14014           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14015           (if (> groups 0)
14016               (gnus-message 6 "%d new newsgroup%s arrived." 
14017                             groups (if (> groups 1) "s have" " has"))
14018             (gnus-message 6 "No new newsgroups."))))))
14019
14020 (defun gnus-matches-options-n (group)
14021   ;; Returns `subscribe' if the group is to be uncoditionally
14022   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
14023   ;; no match for the group.
14024
14025   ;; First we check the two user variables.
14026   (cond
14027    ((and gnus-options-subscribe
14028          (string-match gnus-options-subscribe group))
14029     'subscribe)
14030    ((and gnus-auto-subscribed-groups 
14031          (string-match gnus-auto-subscribed-groups group))
14032     'subscribe)
14033    ((and gnus-options-not-subscribe
14034          (string-match gnus-options-not-subscribe group))
14035     'ignore)
14036    ;; Then we go through the list that was retrieved from the .newsrc
14037    ;; file.  This list has elements on the form 
14038    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
14039    ;; is in the reverse order of the options line) is returned.
14040    (t
14041     (let ((regs gnus-newsrc-options-n))
14042       (while (and regs
14043                   (not (string-match (car (car regs)) group)))
14044         (setq regs (cdr regs)))
14045       (and regs (cdr (car regs)))))))
14046
14047 (defun gnus-ask-server-for-new-groups ()
14048   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
14049          (methods (cons gnus-select-method 
14050                         (append
14051                          (and (consp gnus-check-new-newsgroups)
14052                               gnus-check-new-newsgroups)
14053                          gnus-secondary-select-methods)))
14054          (groups 0)
14055          (new-date (current-time-string))
14056          (hashtb (gnus-make-hashtable 100))
14057          group new-newsgroups got-new method)
14058     ;; Go through both primary and secondary select methods and
14059     ;; request new newsgroups.  
14060     (while methods
14061       (setq method (gnus-server-get-method nil (car methods)))
14062       (and (gnus-check-server method)
14063            (gnus-request-newgroups date method)
14064            (save-excursion
14065              (setq got-new t)
14066              (set-buffer nntp-server-buffer)
14067              ;; Enter all the new groups in a hashtable.
14068              (gnus-active-to-gnus-format method hashtb 'ignore)))
14069       (setq methods (cdr methods)))
14070     (and got-new (setq gnus-newsrc-last-checked-date new-date))
14071     ;; Now all new groups from all select methods are in `hashtb'.
14072     (mapatoms
14073      (lambda (group-sym)
14074        (setq group (symbol-name group-sym))
14075        (if (or (null group)
14076                (null (symbol-value group-sym))
14077                (gnus-gethash group gnus-newsrc-hashtb)
14078                (member group gnus-zombie-list)
14079                (member group gnus-killed-list))
14080            ;; The group is already known.
14081            ()
14082          (and (symbol-value group-sym)
14083               (gnus-set-active group (symbol-value group-sym)))
14084          (let ((do-sub (gnus-matches-options-n group)))
14085            (cond ((eq do-sub 'subscribe)
14086                   (setq groups (1+ groups))
14087                   (gnus-sethash group group gnus-killed-hashtb)
14088                   (funcall 
14089                    gnus-subscribe-options-newsgroup-method group))
14090                  ((eq do-sub 'ignore)
14091                   nil)
14092                  (t
14093                   (setq groups (1+ groups))
14094                   (gnus-sethash group group gnus-killed-hashtb)
14095                   (if gnus-subscribe-hierarchical-interactive
14096                       (setq new-newsgroups (cons group new-newsgroups))
14097                     (funcall gnus-subscribe-newsgroup-method group)))))))
14098      hashtb)
14099     (if new-newsgroups 
14100         (gnus-subscribe-hierarchical-interactive new-newsgroups))
14101     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14102     (if (> groups 0)
14103         (gnus-message 6 "%d new newsgroup%s arrived." 
14104                       groups (if (> groups 1) "s have" " has")))
14105     got-new))
14106
14107 (defun gnus-check-first-time-used ()
14108   (if (or (> (length gnus-newsrc-alist) 1)
14109           (file-exists-p gnus-startup-file)
14110           (file-exists-p (concat gnus-startup-file ".el"))
14111           (file-exists-p (concat gnus-startup-file ".eld")))
14112       nil
14113     (gnus-message 6 "First time user; subscribing you to default groups")
14114     (or gnus-have-read-active-file (gnus-read-active-file))
14115     (setq gnus-newsrc-last-checked-date (current-time-string))
14116     (let ((groups gnus-default-subscribed-newsgroups)
14117           group)
14118       (if (eq groups t)
14119           nil
14120         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
14121         (mapatoms
14122          (lambda (sym)
14123            (if (null (setq group (symbol-name sym)))
14124                ()
14125              (let ((do-sub (gnus-matches-options-n group)))
14126                (cond 
14127                 ((eq do-sub 'subscribe)
14128                  (gnus-sethash group group gnus-killed-hashtb)
14129                  (funcall gnus-subscribe-options-newsgroup-method group))
14130                 ((eq do-sub 'ignore)
14131                  nil)
14132                 (t
14133                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
14134          gnus-active-hashtb)
14135         (while groups
14136           (if (gnus-active (car groups))
14137               (gnus-group-change-level 
14138                (car groups) gnus-level-default-subscribed gnus-level-killed))
14139           (setq groups (cdr groups)))
14140         (gnus-group-make-help-group)
14141         (and gnus-novice-user
14142              (gnus-message 7 "`A k' to list killed groups"))))))
14143
14144 (defun gnus-subscribe-group (group previous &optional method)
14145   (gnus-group-change-level 
14146    (if method
14147        (list t group gnus-level-default-subscribed nil nil method)
14148      group) 
14149    gnus-level-default-subscribed gnus-level-killed previous t))
14150
14151 ;; `gnus-group-change-level' is the fundamental function for changing
14152 ;; subscription levels of newsgroups.  This might mean just changing
14153 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
14154 ;; again, which subscribes/unsubscribes a group, which is equally
14155 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
14156 ;; from 8-9 to 1-7 means that you remove the group from the list of
14157 ;; killed (or zombie) groups and add them to the (kinda) subscribed
14158 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
14159 ;; which is trivial.
14160 ;; ENTRY can either be a string (newsgroup name) or a list (if
14161 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
14162 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
14163 ;; entries. 
14164 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
14165 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
14166 ;; after. 
14167 (defun gnus-group-change-level (entry level &optional oldlevel
14168                                       previous fromkilled)
14169   (let (group info active num)
14170     ;; Glean what info we can from the arguments
14171     (if (consp entry)
14172         (if fromkilled (setq group (nth 1 entry))
14173           (setq group (car (nth 2 entry))))
14174       (setq group entry))
14175     (if (and (stringp entry)
14176              oldlevel 
14177              (< oldlevel gnus-level-zombie))
14178         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
14179     (if (and (not oldlevel)
14180              (consp entry))
14181         (setq oldlevel (car (cdr (nth 2 entry)))))
14182     (if (stringp previous)
14183         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
14184
14185     (if (and (>= oldlevel gnus-level-zombie)
14186              (gnus-gethash group gnus-newsrc-hashtb))
14187         ;; We are trying to subscribe a group that is already
14188         ;; subscribed. 
14189         ()                              ; Do nothing. 
14190
14191       (or (gnus-ephemeral-group-p group)
14192           (gnus-dribble-enter
14193            (format "(gnus-group-change-level %S %S %S %S %S)" 
14194                    group level oldlevel (car (nth 2 previous)) fromkilled)))
14195     
14196       ;; Then we remove the newgroup from any old structures, if needed.
14197       ;; If the group was killed, we remove it from the killed or zombie
14198       ;; list.  If not, and it is in fact going to be killed, we remove
14199       ;; it from the newsrc hash table and assoc.
14200       (cond ((>= oldlevel gnus-level-zombie)
14201              (if (= oldlevel gnus-level-zombie)
14202                  (setq gnus-zombie-list (delete group gnus-zombie-list))
14203                (setq gnus-killed-list (delete group gnus-killed-list))))
14204             (t
14205              (if (and (>= level gnus-level-zombie)
14206                       entry)
14207                  (progn
14208                    (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
14209                    (if (nth 3 entry)
14210                        (setcdr (gnus-gethash (car (nth 3 entry))
14211                                              gnus-newsrc-hashtb)
14212                                (cdr entry)))
14213                    (setcdr (cdr entry) (cdr (cdr (cdr entry))))))))
14214
14215       ;; Finally we enter (if needed) the list where it is supposed to
14216       ;; go, and change the subscription level.  If it is to be killed,
14217       ;; we enter it into the killed or zombie list.
14218       (cond ((>= level gnus-level-zombie)
14219              ;; Remove from the hash table.
14220              (gnus-sethash group nil gnus-newsrc-hashtb)
14221              ;; We do not enter foreign groups into the list of dead
14222              ;; groups.  
14223              (unless (gnus-group-foreign-p group)
14224                (if (= level gnus-level-zombie)
14225                    (setq gnus-zombie-list (cons group gnus-zombie-list))
14226                  (setq gnus-killed-list (cons group gnus-killed-list)))))
14227             (t
14228              ;; If the list is to be entered into the newsrc assoc, and
14229              ;; it was killed, we have to create an entry in the newsrc
14230              ;; hashtb format and fix the pointers in the newsrc assoc.
14231              (if (>= oldlevel gnus-level-zombie)
14232                  (progn
14233                    (if (listp entry)
14234                        (progn
14235                          (setq info (cdr entry))
14236                          (setq num (car entry)))
14237                      (setq active (gnus-active group))
14238                      (setq num 
14239                            (if active (- (1+ (cdr active)) (car active)) t))
14240                      ;; Check whether the group is foreign.  If so, the
14241                      ;; foreign select method has to be entered into the
14242                      ;; info. 
14243                      (let ((method (gnus-group-method-name group)))
14244                        (if (eq method gnus-select-method)
14245                            (setq info (list group level nil))
14246                          (setq info (list group level nil nil method)))))
14247                    (or previous 
14248                        (setq previous 
14249                              (let ((p gnus-newsrc-alist))
14250                                (while (cdr (cdr p))
14251                                  (setq p (cdr p)))
14252                                p)))
14253                    (setq entry (cons info (cdr (cdr previous))))
14254                    (if (cdr previous)
14255                        (progn
14256                          (setcdr (cdr previous) entry)
14257                          (gnus-sethash group (cons num (cdr previous)) 
14258                                        gnus-newsrc-hashtb))
14259                      (setcdr previous entry)
14260                      (gnus-sethash group (cons num previous)
14261                                    gnus-newsrc-hashtb))
14262                    (if (cdr entry)
14263                        (setcdr (gnus-gethash (car (car (cdr entry)))
14264                                              gnus-newsrc-hashtb)
14265                                entry)))
14266                ;; It was alive, and it is going to stay alive, so we
14267                ;; just change the level and don't change any pointers or
14268                ;; hash table entries.
14269                (setcar (cdr (car (cdr (cdr entry)))) level)))))))
14270
14271 (defun gnus-kill-newsgroup (newsgroup)
14272   "Obsolete function.  Kills a newsgroup."
14273   (gnus-group-change-level
14274    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
14275
14276 (defun gnus-check-bogus-newsgroups (&optional confirm)
14277   "Remove bogus newsgroups.
14278 If CONFIRM is non-nil, the user has to confirm the deletion of every
14279 newsgroup." 
14280   (let ((newsrc (cdr gnus-newsrc-alist))
14281         bogus group entry info)
14282     (gnus-message 5 "Checking bogus newsgroups...")
14283     (unless gnus-have-read-active-file 
14284       (gnus-read-active-file))
14285     (when (member gnus-select-method gnus-have-read-active-file)
14286       ;; Find all bogus newsgroup that are subscribed.
14287       (while newsrc
14288         (setq info (pop newsrc)
14289               group (gnus-info-group info))
14290         (unless (or (gnus-active group) ; Active
14291                     (gnus-info-method info) ; Foreign
14292                     (and confirm
14293                          (not (gnus-y-or-n-p
14294                                (format "Remove bogus newsgroup: %s " group)))))
14295           ;; Found a bogus newsgroup.
14296           (push group bogus)))
14297       ;; Remove all bogus subscribed groups by first killing them, and
14298       ;; then removing them from the list of killed groups.
14299       (while bogus
14300         (when (setq entry (gnus-gethash (setq group (pop bogus))
14301                                         gnus-newsrc-hashtb))
14302           (gnus-group-change-level entry gnus-level-killed)
14303           (setq gnus-killed-list (delete group gnus-killed-list))))
14304       ;; Then we remove all bogus groups from the list of killed and
14305       ;; zombie groups.  They are are removed without confirmation.
14306       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
14307             killed)
14308         (while dead-lists
14309           (setq killed (symbol-value (car dead-lists)))
14310           (while killed
14311             (unless (gnus-active (setq group (pop killed)))
14312               ;; The group is bogus.
14313               ;; !!!Slow as hell.
14314               (set (car dead-lists)
14315                    (delete group (symbol-value (car dead-lists))))))
14316           (setq dead-lists (cdr dead-lists))))
14317       (gnus-message 5 "Checking bogus newsgroups...done"))))
14318
14319 (defun gnus-check-duplicate-killed-groups ()
14320   "Remove duplicates from the list of killed groups."
14321   (interactive)
14322   (let ((killed gnus-killed-list))
14323     (while killed
14324       (gnus-message 9 "%d" (length killed))
14325       (setcdr killed (delete (car killed) (cdr killed)))
14326       (setq killed (cdr killed)))))
14327
14328 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
14329 ;; and compute how many unread articles there are in each group.
14330 (defun gnus-get-unread-articles (&optional level) 
14331   (let* ((newsrc (cdr gnus-newsrc-alist))
14332          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
14333          (foreign-level
14334           (min 
14335            (cond ((and gnus-activate-foreign-newsgroups 
14336                        (not (numberp gnus-activate-foreign-newsgroups)))
14337                   (1+ gnus-level-subscribed))
14338                  ((numberp gnus-activate-foreign-newsgroups)
14339                   gnus-activate-foreign-newsgroups)
14340                  (t 0))
14341            level))
14342          (update
14343           (fboundp (intern (format "%s-request-update-info"
14344                                    (car gnus-select-method)))))
14345          info group active virtuals method fmethod)
14346     (gnus-message 5 "Checking new news...")
14347
14348     (while newsrc
14349       (setq info (car newsrc)
14350             group (gnus-info-group info)
14351             active (gnus-active group))
14352
14353       ;; Check newsgroups.  If the user doesn't want to check them, or
14354       ;; they can't be checked (for instance, if the news server can't
14355       ;; be reached) we just set the number of unread articles in this
14356       ;; newsgroup to t.  This means that Gnus thinks that there are
14357       ;; unread articles, but it has no idea how many.
14358       (if (and (setq method (gnus-info-method info))
14359                (not (gnus-server-equal
14360                      gnus-select-method
14361                      (prog1
14362                          (setq fmethod (gnus-server-get-method nil method))
14363                        ;; We do this here because it would be awkward
14364                        ;; to do it anywhere else.  Hell, it's pretty
14365                        ;; awkward here as well, but at least it's
14366                        ;; reasonably efficient. 
14367                        (and (<= (gnus-info-level info) foreign-level)
14368                             (gnus-request-update-info info method)))))
14369                (not (gnus-secondary-method-p method)))
14370           ;; These groups are foreign.  Check the level.
14371           (if (<= (gnus-info-level info) foreign-level)
14372               (setq active (gnus-activate-group (gnus-info-group info) 'scan)))
14373
14374         ;; These groups are native or secondary. 
14375         (if (<= (gnus-info-level info) level)
14376             (progn
14377               (if (and update (not method))
14378                   (progn
14379                     ;; Allow updating of native groups as well, even
14380                     ;; though that's pretty unlikely.
14381                     (gnus-request-update-info info gnus-select-method)
14382                     (setq active (gnus-activate-group 
14383                                   (gnus-info-group info) 'scan)))
14384                 (or gnus-read-active-file
14385                     (setq active (gnus-activate-group 
14386                                   (gnus-info-group info) 'scan)))))))
14387       
14388       (if active
14389           (gnus-get-unread-articles-in-group info active)
14390         ;; The group couldn't be reached, so we nix out the number of
14391         ;; unread articles and stuff.
14392         (gnus-set-active group nil)
14393         (setcar (gnus-gethash group gnus-newsrc-hashtb) t))
14394       
14395       (setq newsrc (cdr newsrc)))
14396
14397     (gnus-message 5 "Checking new news...done")))
14398
14399 ;; Create a hash table out of the newsrc alist.  The `car's of the
14400 ;; alist elements are used as keys.
14401 (defun gnus-make-hashtable-from-newsrc-alist ()
14402   (let ((alist gnus-newsrc-alist)
14403         (ohashtb gnus-newsrc-hashtb)
14404         prev)
14405     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
14406     (setq alist 
14407           (setq prev (setq gnus-newsrc-alist 
14408                            (if (equal (car (car gnus-newsrc-alist))
14409                                       "dummy.group")
14410                                gnus-newsrc-alist
14411                              (cons (list "dummy.group" 0 nil) alist)))))
14412     (while alist
14413       (gnus-sethash 
14414        (car (car alist)) 
14415        (cons (and ohashtb (car (gnus-gethash (car (car alist)) ohashtb))) 
14416              prev)
14417        gnus-newsrc-hashtb)
14418       (setq prev alist
14419             alist (cdr alist)))))
14420
14421 (defun gnus-make-hashtable-from-killed ()
14422   "Create a hash table from the killed and zombie lists."
14423   (let ((lists '(gnus-killed-list gnus-zombie-list))
14424         list)
14425     (setq gnus-killed-hashtb 
14426           (gnus-make-hashtable 
14427            (+ (length gnus-killed-list) (length gnus-zombie-list))))
14428     (while lists
14429       (setq list (symbol-value (car lists)))
14430       (setq lists (cdr lists))
14431       (while list
14432         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
14433         (setq list (cdr list))))))
14434
14435 (defun gnus-get-unread-articles-in-group (info active)
14436   (let* ((range (gnus-info-read info))
14437          (num 0)
14438          (marked (gnus-info-marks info)))
14439     ;; If a cache is present, we may have to alter the active info.
14440     (and gnus-use-cache
14441          (gnus-cache-possibly-alter-active (gnus-info-group info) active))
14442     ;; Modify the list of read articles according to what articles 
14443     ;; are available; then tally the unread articles and add the
14444     ;; number to the group hash table entry.
14445     (cond 
14446      ((zerop (cdr active))
14447       (setq num 0))
14448      ((not range)
14449       (setq num (- (1+ (cdr active)) (car active))))
14450      ((not (listp (cdr range)))
14451       ;; Fix a single (num . num) range according to the
14452       ;; active hash table.
14453       ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
14454       (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
14455       (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
14456       ;; Compute number of unread articles.
14457       (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
14458      (t
14459       ;; The read list is a list of ranges.  Fix them according to
14460       ;; the active hash table.
14461       ;; First peel off any elements that are below the lower
14462       ;; active limit. 
14463       (while (and (cdr range) 
14464                   (>= (car active) 
14465                       (or (and (atom (car (cdr range))) (car (cdr range)))
14466                           (car (car (cdr range))))))
14467         (if (numberp (car range))
14468             (setcar range 
14469                     (cons (car range) 
14470                           (or (and (numberp (car (cdr range)))
14471                                    (car (cdr range))) 
14472                               (cdr (car (cdr range))))))
14473           (setcdr (car range) 
14474                   (or (and (numberp (nth 1 range)) (nth 1 range))
14475                       (cdr (car (cdr range))))))
14476         (setcdr range (cdr (cdr range))))
14477       ;; Adjust the first element to be the same as the lower limit. 
14478       (if (and (not (atom (car range))) 
14479                (< (cdr (car range)) (car active)))
14480           (setcdr (car range) (1- (car active))))
14481       ;; Then we want to peel off any elements that are higher
14482       ;; than the upper active limit.  
14483       (let ((srange range))
14484         ;; Go past all legal elements.
14485         (while (and (cdr srange) 
14486                     (<= (or (and (atom (car (cdr srange)))
14487                                  (car (cdr srange)))
14488                             (car (car (cdr srange)))) (cdr active)))
14489           (setq srange (cdr srange)))
14490         (if (cdr srange)
14491             ;; Nuke all remaining illegal elements.
14492             (setcdr srange nil))
14493
14494         ;; Adjust the final element.
14495         (if (and (not (atom (car srange)))
14496                  (> (cdr (car srange)) (cdr active)))
14497             (setcdr (car srange) (cdr active))))
14498       ;; Compute the number of unread articles.
14499       (while range
14500         (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
14501                                     (cdr (car range))))
14502                             (or (and (atom (car range)) (car range))
14503                                 (car (car range))))))
14504         (setq range (cdr range)))
14505       (setq num (max 0 (- (cdr active) num)))))
14506     ;; Set the number of unread articles.
14507     (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num)
14508     num))
14509
14510 (defun gnus-activate-group (group &optional scan)
14511   ;; Check whether a group has been activated or not.
14512   ;; If SCAN, request a scan of that group as well.
14513   (let ((method (gnus-find-method-for-group group))
14514         active)
14515     (and (gnus-check-server method)
14516          ;; We escape all bugs and quit here to make it possible to
14517          ;; continue if a group is so out-there that it reports bugs
14518          ;; and stuff.
14519          (progn
14520            (and scan
14521                 (gnus-check-backend-function 'request-scan (car method))
14522                 (gnus-request-scan group method))
14523            t)
14524          (condition-case ()
14525              (gnus-request-group group)
14526         ;   (error nil)
14527            (quit nil))
14528          (save-excursion
14529            (set-buffer nntp-server-buffer)
14530            (goto-char (point-min))
14531            ;; Parse the result we got from `gnus-request-group'.
14532            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
14533                 (progn
14534                   (goto-char (match-beginning 1))
14535                   (gnus-set-active 
14536                    group (setq active (cons (read (current-buffer))
14537                                             (read (current-buffer)))))
14538                   ;; Return the new active info.
14539                   active))))))
14540
14541 (defun gnus-update-read-articles (group unread)
14542   "Update the list of read and ticked articles in GROUP using the
14543 UNREAD and TICKED lists.
14544 Note: UNSELECTED has to be sorted over `<'.
14545 Returns whether the updating was successful."
14546   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
14547          (entry (gnus-gethash group gnus-newsrc-hashtb))
14548          (info (nth 2 entry))
14549          (marked (gnus-info-marks info))
14550          (prev 1)
14551          (unread (sort (copy-sequence unread) '<))
14552          read)
14553     (if (or (not info) (not active))
14554         ;; There is no info on this group if it was, in fact,
14555         ;; killed.  Gnus stores no information on killed groups, so
14556         ;; there's nothing to be done. 
14557         ;; One could store the information somewhere temporarily,
14558         ;; perhaps...  Hmmm... 
14559         ()
14560       ;; Remove any negative articles numbers.
14561       (while (and unread (< (car unread) 0))
14562         (setq unread (cdr unread)))
14563       ;; Remove any expired article numbers
14564       (while (and unread (< (car unread) (car active)))
14565         (setq unread (cdr unread)))
14566       ;; Compute the ranges of read articles by looking at the list of
14567       ;; unread articles.  
14568       (while unread
14569         (if (/= (car unread) prev)
14570             (setq read (cons (if (= prev (1- (car unread))) prev
14571                                (cons prev (1- (car unread)))) read)))
14572         (setq prev (1+ (car unread)))
14573         (setq unread (cdr unread)))
14574       (when (<= prev (cdr active))
14575         (setq read (cons (cons prev (cdr active)) read)))
14576       ;; Enter this list into the group info.
14577       (gnus-info-set-read 
14578        info (if (> (length read) 1) (nreverse read) read))
14579       ;; Set the number of unread articles in gnus-newsrc-hashtb.
14580       (gnus-get-unread-articles-in-group info (gnus-active group))
14581       t)))
14582
14583 (defun gnus-make-articles-unread (group articles)
14584   "Mark ARTICLES in GROUP as unread."
14585   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
14586                           (gnus-gethash (gnus-group-real-name group)
14587                                         gnus-newsrc-hashtb))))
14588          (ranges (gnus-info-read info))
14589          news article)
14590     (while articles
14591       (when (gnus-member-of-range 
14592              (setq article (pop articles)) ranges)
14593         (setq news (cons article news))))
14594     (when news
14595       (gnus-info-set-read 
14596        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
14597       (gnus-group-update-group group t))))
14598
14599 ;; Enter all dead groups into the hashtb.
14600 (defun gnus-update-active-hashtb-from-killed ()
14601   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
14602         (lists (list gnus-killed-list gnus-zombie-list))
14603         killed)
14604     (while lists
14605       (setq killed (car lists))
14606       (while killed
14607         (gnus-sethash (car killed) nil hashtb)
14608         (setq killed (cdr killed)))
14609       (setq lists (cdr lists)))))
14610
14611 ;; Get the active file(s) from the backend(s).
14612 (defun gnus-read-active-file ()
14613   (gnus-group-set-mode-line)
14614   (let ((methods (if (gnus-check-server gnus-select-method)
14615                      ;; The native server is available.
14616                      (cons gnus-select-method gnus-secondary-select-methods)
14617                    ;; The native server is down, so we just do the
14618                    ;; secondary ones.   
14619                    gnus-secondary-select-methods))
14620         list-type)
14621     (setq gnus-have-read-active-file nil)
14622     (save-excursion
14623       (set-buffer nntp-server-buffer)
14624       (while methods
14625         (let* ((method (gnus-server-get-method nil (car methods)))
14626                (where (nth 1 method))
14627                (mesg (format "Reading active file%s via %s..."
14628                              (if (and where (not (zerop (length where))))
14629                                  (concat " from " where) "")
14630                              (car method))))
14631           (gnus-message 5 mesg)
14632           (if (not (gnus-check-server method))
14633               ()
14634             ;; Request that the backend scan its incoming messages.
14635             (and (gnus-check-backend-function 'request-scan (car method))
14636                  (gnus-request-scan nil method))
14637             (cond 
14638              ((and (eq gnus-read-active-file 'some)
14639                    (gnus-check-backend-function 'retrieve-groups (car method)))
14640               (let ((newsrc (cdr gnus-newsrc-alist))
14641                     (gmethod (gnus-server-get-method nil method))
14642                     groups)
14643                 (while newsrc
14644                   (and (gnus-server-equal 
14645                         (gnus-find-method-for-group 
14646                          (car (car newsrc)) (car newsrc))
14647                         gmethod)
14648                        (setq groups (cons (gnus-group-real-name 
14649                                            (car (car newsrc))) groups)))
14650                   (setq newsrc (cdr newsrc)))
14651                 (gnus-check-server method)
14652                 (setq list-type (gnus-retrieve-groups groups method))
14653                 (cond 
14654                  ((not list-type)
14655                   (gnus-message 
14656                    1 "Cannot read partial active file from %s server." 
14657                    (car method))
14658                   (ding)
14659                   (sit-for 2))
14660                  ((eq list-type 'active)
14661                   (gnus-active-to-gnus-format method gnus-active-hashtb))
14662                  (t
14663                   (gnus-groups-to-gnus-format method gnus-active-hashtb)))))
14664              (t
14665               (if (not (gnus-request-list method))
14666                   (progn
14667                     (gnus-message 1 "Cannot read active file from %s server." 
14668                                   (car method))
14669                     (ding))
14670                 (gnus-active-to-gnus-format method)
14671                 ;; We mark this active file as read.
14672                 (setq gnus-have-read-active-file
14673                       (cons method gnus-have-read-active-file))
14674                 (gnus-message 5 "%sdone" mesg))))))
14675         (setq methods (cdr methods))))))
14676
14677 ;; Read an active file and place the results in `gnus-active-hashtb'.
14678 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
14679   (unless method
14680     (setq method gnus-select-method))
14681   (let ((cur (current-buffer))
14682         (hashtb (or hashtb 
14683                     (if (and gnus-active-hashtb 
14684                              (not (equal method gnus-select-method)))
14685                         gnus-active-hashtb
14686                       (setq gnus-active-hashtb
14687                             (if (equal method gnus-select-method)
14688                                 (gnus-make-hashtable 
14689                                  (count-lines (point-min) (point-max)))
14690                               (gnus-make-hashtable 4096))))))
14691         (flag-hashtb (gnus-make-hashtable 60)))
14692     ;; Delete unnecessary lines.
14693     (goto-char (point-min))
14694     (while (search-forward "\nto." nil t)
14695       (delete-region (1+ (match-beginning 0)) 
14696                      (progn (forward-line 1) (point))))
14697     (or (string= gnus-ignored-newsgroups "")
14698         (progn
14699           (goto-char (point-min))
14700           (delete-matching-lines gnus-ignored-newsgroups)))
14701     ;; Make the group names readable as a lisp expression even if they
14702     ;; contain special characters.
14703     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
14704     (goto-char (point-max))
14705     (while (re-search-backward "[][';?()#]" nil t)
14706       (insert ?\\))
14707     ;; If these are groups from a foreign select method, we insert the
14708     ;; group prefix in front of the group names. 
14709     (and method (not (gnus-server-equal
14710                       (gnus-server-get-method nil method)
14711                       (gnus-server-get-method nil gnus-select-method)))
14712          (let ((prefix (gnus-group-prefixed-name "" method)))
14713            (goto-char (point-min))
14714            (while (and (not (eobp))
14715                        (progn (insert prefix)
14716                               (zerop (forward-line 1)))))))
14717     ;; Store the active file in a hash table.
14718     (goto-char (point-min))
14719     (if (string-match "%[oO]" gnus-group-line-format)
14720         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
14721         ;; If we want information on moderated groups, we use this
14722         ;; loop...   
14723         (let* ((mod-hashtb (make-vector 7 0))
14724                (m (intern "m" mod-hashtb))
14725                group max min)
14726           (while (not (eobp))
14727             (condition-case nil
14728                 (progn
14729                   (narrow-to-region (point) (gnus-point-at-eol))
14730                   (setq group (let ((obarray hashtb)) (read cur)))
14731                   (if (and (numberp (setq max (read cur)))
14732                            (numberp (setq min (read cur)))
14733                            (progn 
14734                              (skip-chars-forward " \t")
14735                              (not
14736                               (or (= (following-char) ?=)
14737                                   (= (following-char) ?x)
14738                                   (= (following-char) ?j)))))
14739                       (set group (cons min max))
14740                     (set group nil))
14741                   ;; Enter moderated groups into a list.
14742                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
14743                       (setq gnus-moderated-list 
14744                             (cons (symbol-name group) gnus-moderated-list))))
14745               (error 
14746                (and group
14747                     (symbolp group)
14748                     (set group nil))))
14749             (widen)
14750             (forward-line 1)))
14751       ;; And if we do not care about moderation, we use this loop,
14752       ;; which is faster.
14753       (let (group max min)
14754         (while (not (eobp))
14755           (condition-case ()
14756               (progn
14757                 (narrow-to-region (point) (gnus-point-at-eol))
14758                 ;; group gets set to a symbol interned in the hash table
14759                 ;; (what a hack!!) - jwz
14760                 (setq group (let ((obarray hashtb)) (read cur)))
14761                 (if (and (numberp (setq max (read cur)))
14762                          (numberp (setq min (read cur)))
14763                          (progn 
14764                            (skip-chars-forward " \t")
14765                            (not
14766                             (or (= (following-char) ?=)
14767                                 (= (following-char) ?x)
14768                                 (= (following-char) ?j)))))
14769                     (set group (cons min max))
14770                   (set group nil)))
14771             (error 
14772              (progn 
14773                (and group
14774                     (symbolp group)
14775                     (set group nil))
14776                (or ignore-errors
14777                    (gnus-message 3 "Warning - illegal active: %s"
14778                                  (buffer-substring 
14779                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
14780           (widen)
14781           (forward-line 1))))))
14782
14783 (defun gnus-groups-to-gnus-format (method &optional hashtb)
14784   ;; Parse a "groups" active file.
14785   (let ((cur (current-buffer))
14786         (hashtb (or hashtb 
14787                     (if (and method gnus-active-hashtb)
14788                         gnus-active-hashtb
14789                       (setq gnus-active-hashtb
14790                             (gnus-make-hashtable 
14791                              (count-lines (point-min) (point-max)))))))
14792         (prefix (and method 
14793                      (not (gnus-server-equal
14794                            (gnus-server-get-method nil method)
14795                            (gnus-server-get-method nil gnus-select-method)))
14796                      (gnus-group-prefixed-name "" method))))
14797
14798     (goto-char (point-min))
14799     ;; We split this into to separate loops, one with the prefix
14800     ;; and one without to speed the reading up somewhat.
14801     (if prefix
14802         (let (min max opoint group)
14803           (while (not (eobp))
14804             (condition-case ()
14805                 (progn
14806                   (read cur) (read cur)
14807                   (setq min (read cur)
14808                         max (read cur)
14809                         opoint (point))
14810                   (skip-chars-forward " \t")
14811                   (insert prefix)
14812                   (goto-char opoint)
14813                   (set (let ((obarray hashtb)) (read cur)) 
14814                        (cons min max)))
14815               (error (and group (symbolp group) (set group nil))))
14816             (forward-line 1)))
14817       (let (min max group)
14818         (while (not (eobp))
14819           (condition-case ()
14820               (if (= (following-char) ?2)
14821                   (progn
14822                     (read cur) (read cur)
14823                     (setq min (read cur)
14824                           max (read cur))
14825                     (set (setq group (let ((obarray hashtb)) (read cur)))
14826                          (cons min max))))
14827             (error (and group (symbolp group) (set group nil))))
14828           (forward-line 1))))))
14829
14830 (defun gnus-read-newsrc-file (&optional force)
14831   "Read startup file.
14832 If FORCE is non-nil, the .newsrc file is read."
14833   ;; Reset variables that might be defined in the .newsrc.eld file.
14834   (let ((variables gnus-variable-list))
14835     (while variables
14836       (set (car variables) nil)
14837       (setq variables (cdr variables))))
14838   (let* ((newsrc-file gnus-current-startup-file)
14839          (quick-file (concat newsrc-file ".el")))
14840     (save-excursion
14841       ;; We always load the .newsrc.eld file.  If always contains
14842       ;; much information that can not be gotten from the .newsrc
14843       ;; file (ticked articles, killed groups, foreign methods, etc.)
14844       (gnus-read-newsrc-el-file quick-file)
14845  
14846       (if (or force
14847               (and (file-newer-than-file-p newsrc-file quick-file)
14848                    (file-newer-than-file-p newsrc-file 
14849                                            (concat quick-file "d")))
14850               (not gnus-newsrc-alist))
14851           ;; We read the .newsrc file.  Note that if there if a
14852           ;; .newsrc.eld file exists, it has already been read, and
14853           ;; the `gnus-newsrc-hashtb' has been created.  While reading
14854           ;; the .newsrc file, Gnus will only use the information it
14855           ;; can find there for changing the data already read -
14856           ;; ie. reading the .newsrc file will not trash the data
14857           ;; already read (except for read articles).
14858           (save-excursion
14859             (gnus-message 5 "Reading %s..." newsrc-file)
14860             (set-buffer (find-file-noselect newsrc-file))
14861             (buffer-disable-undo (current-buffer))
14862             (gnus-newsrc-to-gnus-format)
14863             (kill-buffer (current-buffer))
14864             (gnus-message 5 "Reading %s...done" newsrc-file)))
14865
14866       ;; Read any slave files.
14867       (or gnus-slave
14868           (gnus-master-read-slave-newsrc)))))
14869
14870 (defun gnus-read-newsrc-el-file (file)
14871   (let ((ding-file (concat file "d")))
14872     ;; We always, always read the .eld file.
14873     (gnus-message 5 "Reading %s..." ding-file)
14874     (let (gnus-newsrc-assoc)
14875       (condition-case nil
14876           (load ding-file t t t)
14877         (error
14878          (gnus-message 1 "Error in %s" ding-file)
14879          (ding)))
14880       (when gnus-newsrc-assoc 
14881         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
14882     (gnus-make-hashtable-from-newsrc-alist)
14883     (when (file-newer-than-file-p file ding-file)
14884       ;; Old format quick file
14885       (gnus-message 5 "Reading %s..." file)
14886       ;; The .el file is newer than the .eld file, so we read that one
14887       ;; as well. 
14888       (gnus-read-old-newsrc-el-file file))))
14889
14890 ;; Parse the old-style quick startup file
14891 (defun gnus-read-old-newsrc-el-file (file)
14892   (let (newsrc killed marked group m)
14893     (prog1
14894         (let ((gnus-killed-assoc nil)
14895               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
14896           (prog1
14897               (condition-case nil
14898                   (load file t t t)
14899                 (error nil))
14900             (setq newsrc gnus-newsrc-assoc
14901                   killed gnus-killed-assoc
14902                   marked gnus-marked-assoc)))
14903       (setq gnus-newsrc-alist nil)
14904       (while newsrc
14905         (setq group (car newsrc))
14906         (let ((info (gnus-get-info (car group))))
14907           (if info
14908               (progn
14909                 (gnus-info-set-read info (cdr (cdr group)))
14910                 (gnus-info-set-level
14911                  info (if (nth 1 group) gnus-level-default-subscribed 
14912                         gnus-level-default-unsubscribed))
14913                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
14914             (setq gnus-newsrc-alist
14915                   (cons 
14916                    (setq info
14917                          (list (car group)
14918                                (if (nth 1 group) gnus-level-default-subscribed
14919                                  gnus-level-default-unsubscribed) 
14920                                (cdr (cdr group))))
14921                    gnus-newsrc-alist)))
14922           (if (setq m (assoc (car group) marked))
14923               (gnus-info-set-marks 
14924                info (cons (list (cons 'tick (gnus-compress-sequence
14925                                              (sort (cdr m) '<) t)))
14926                           nil))))
14927         (setq newsrc (cdr newsrc)))
14928       (setq newsrc killed)
14929       (while newsrc
14930         (setcar newsrc (car (car newsrc)))
14931         (setq newsrc (cdr newsrc)))
14932       (setq gnus-killed-list killed))
14933     ;; The .el file version of this variable does not begin with
14934     ;; "options", while the .eld version does, so we just add it if it
14935     ;; isn't there.
14936     (and
14937      gnus-newsrc-options 
14938      (progn
14939        (and (not (string-match "^ *options" gnus-newsrc-options))
14940             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
14941        (and (not (string-match "\n$" gnus-newsrc-options))
14942             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
14943        ;; Finally, if we read some options lines, we parse them.
14944        (or (string= gnus-newsrc-options "")
14945            (gnus-newsrc-parse-options gnus-newsrc-options))))
14946
14947     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
14948     (gnus-make-hashtable-from-newsrc-alist)))
14949       
14950 (defun gnus-make-newsrc-file (file)
14951   "Make server dependent file name by catenating FILE and server host name."
14952   (let* ((file (expand-file-name file nil))
14953          (real-file (concat file "-" (nth 1 gnus-select-method))))
14954     (if (or (file-exists-p real-file)
14955             (file-exists-p (concat real-file ".el"))
14956             (file-exists-p (concat real-file ".eld")))
14957         real-file file)))
14958
14959 (defun gnus-newsrc-to-gnus-format ()
14960   (setq gnus-newsrc-options "")
14961   (setq gnus-newsrc-options-n nil)
14962
14963   (or gnus-active-hashtb
14964       (setq gnus-active-hashtb (make-vector 4095 0)))
14965   (let ((buf (current-buffer))
14966         (already-read (> (length gnus-newsrc-alist) 1))
14967         group subscribed options-symbol newsrc Options-symbol
14968         symbol reads num1)
14969     (goto-char (point-min))
14970     ;; We intern the symbol `options' in the active hashtb so that we
14971     ;; can `eq' against it later.
14972     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
14973     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
14974   
14975     (while (not (eobp))
14976       ;; We first read the first word on the line by narrowing and
14977       ;; then reading into `gnus-active-hashtb'.  Most groups will
14978       ;; already exist in that hashtb, so this will save some string
14979       ;; space.
14980       (narrow-to-region
14981        (point)
14982        (progn (skip-chars-forward "^ \t!:\n") (point)))
14983       (goto-char (point-min))
14984       (setq symbol 
14985             (and (/= (point-min) (point-max))
14986                  (let ((obarray gnus-active-hashtb)) (read buf))))
14987       (widen)
14988       ;; Now, the symbol we have read is either `options' or a group
14989       ;; name.  If it is an options line, we just add it to a string. 
14990       (cond 
14991        ((or (eq symbol options-symbol)
14992             (eq symbol Options-symbol))
14993         (setq gnus-newsrc-options
14994               ;; This concatting is quite inefficient, but since our
14995               ;; thorough studies show that approx 99.37% of all
14996               ;; .newsrc files only contain a single options line, we
14997               ;; don't give a damn, frankly, my dear.
14998               (concat gnus-newsrc-options
14999                       (buffer-substring 
15000                        (gnus-point-at-bol)
15001                        ;; Options may continue on the next line.
15002                        (or (and (re-search-forward "^[^ \t]" nil 'move)
15003                                 (progn (beginning-of-line) (point)))
15004                            (point)))))
15005         (forward-line -1))
15006        (symbol
15007         (or (boundp symbol) (set symbol nil))
15008         ;; It was a group name.
15009         (setq subscribed (= (following-char) ?:)
15010               group (symbol-name symbol)
15011               reads nil)
15012         (if (eolp)
15013             ;; If the line ends here, this is clearly a buggy line, so
15014             ;; we put point a the beginning of line and let the cond
15015             ;; below do the error handling.
15016             (beginning-of-line)
15017           ;; We skip to the beginning of the ranges.
15018           (skip-chars-forward "!: \t"))
15019         ;; We are now at the beginning of the list of read articles.
15020         ;; We read them range by range.
15021         (while
15022             (cond 
15023              ((looking-at "[0-9]+")
15024               ;; We narrow and read a number instead of buffer-substring/
15025               ;; string-to-int because it's faster.  narrow/widen is
15026               ;; faster than save-restriction/narrow, and save-restriction
15027               ;; produces a garbage object.
15028               (setq num1 (progn
15029                            (narrow-to-region (match-beginning 0) (match-end 0))
15030                            (read buf)))
15031               (widen)
15032               ;; If the next character is a dash, then this is a range.
15033               (if (= (following-char) ?-)
15034                   (progn
15035                     ;; We read the upper bound of the range.
15036                     (forward-char 1)
15037                     (if (not (looking-at "[0-9]+"))
15038                         ;; This is a buggy line, by we pretend that
15039                         ;; it's kinda OK.  Perhaps the user should be
15040                         ;; dinged? 
15041                         (setq reads (cons num1 reads))
15042                       (setq reads 
15043                             (cons 
15044                              (cons num1
15045                                    (progn
15046                                      (narrow-to-region (match-beginning 0) 
15047                                                        (match-end 0))
15048                                      (read buf)))
15049                              reads))
15050                       (widen)))
15051                 ;; It was just a simple number, so we add it to the
15052                 ;; list of ranges.
15053                 (setq reads (cons num1 reads)))
15054               ;; If the next char in ?\n, then we have reached the end
15055               ;; of the line and return nil.
15056               (/= (following-char) ?\n))
15057              ((= (following-char) ?\n)
15058               ;; End of line, so we end.
15059               nil)
15060              (t
15061               ;; Not numbers and not eol, so this might be a buggy
15062               ;; line... 
15063               (or (eobp)                
15064                   ;; If it was eob instead of ?\n, we allow it.
15065                   (progn
15066                     ;; The line was buggy.
15067                     (setq group nil)
15068                     (gnus-message 3 "Mangled line: %s" 
15069                                   (buffer-substring (gnus-point-at-bol) 
15070                                                     (gnus-point-at-eol)))
15071                     (ding)
15072                     (sit-for 1)))
15073               nil))
15074           ;; Skip past ", ".  Spaces are illegal in these ranges, but
15075           ;; we allow them, because it's a common mistake to put a
15076           ;; space after the comma.
15077           (skip-chars-forward ", "))
15078
15079         ;; We have already read .newsrc.eld, so we gently update the
15080         ;; data in the hash table with the information we have just
15081         ;; read. 
15082         (when group
15083           (let ((info (gnus-get-info group))
15084                 level)
15085             (if info
15086                 ;; There is an entry for this file in the alist.
15087                 (progn
15088                   (gnus-info-set-read info (nreverse reads))
15089                   ;; We update the level very gently.  In fact, we
15090                   ;; only change it if there's been a status change
15091                   ;; from subscribed to unsubscribed, or vice versa.
15092                   (setq level (gnus-info-level info))
15093                   (cond ((and (<= level gnus-level-subscribed)
15094                               (not subscribed))
15095                          (setq level (if reads
15096                                          gnus-level-default-unsubscribed 
15097                                        (1+ gnus-level-default-unsubscribed))))
15098                         ((and (> level gnus-level-subscribed) subscribed)
15099                          (setq level gnus-level-default-subscribed)))
15100                   (gnus-info-set-level info level))
15101               ;; This is a new group.
15102               (setq info (list group 
15103                                (if subscribed
15104                                    gnus-level-default-subscribed 
15105                                  (if reads
15106                                      (1+ gnus-level-subscribed)
15107                                    gnus-level-default-unsubscribed))
15108                                (nreverse reads))))
15109             (setq newsrc (cons info newsrc))))))
15110       (forward-line 1))
15111     
15112     (setq newsrc (nreverse newsrc))
15113
15114     (if (not already-read)
15115         ()
15116       ;; We now have two newsrc lists - `newsrc', which is what we
15117       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
15118       ;; what we've read from .newsrc.eld.  We have to merge these
15119       ;; lists.  We do this by "attaching" any (foreign) groups in the
15120       ;; gnus-newsrc-alist to the (native) group that precedes them. 
15121       (let ((rc (cdr gnus-newsrc-alist))
15122             (prev gnus-newsrc-alist)
15123             entry mentry)
15124         (while rc
15125           (or (null (nth 4 (car rc)))   ; It's a native group.
15126               (assoc (car (car rc)) newsrc) ; It's already in the alist.
15127               (if (setq entry (assoc (car (car prev)) newsrc))
15128                   (setcdr (setq mentry (memq entry newsrc))
15129                           (cons (car rc) (cdr mentry)))
15130                 (setq newsrc (cons (car rc) newsrc))))
15131           (setq prev rc
15132                 rc (cdr rc)))))
15133
15134     (setq gnus-newsrc-alist newsrc)
15135     ;; We make the newsrc hashtb.
15136     (gnus-make-hashtable-from-newsrc-alist)
15137
15138     ;; Finally, if we read some options lines, we parse them.
15139     (or (string= gnus-newsrc-options "")
15140         (gnus-newsrc-parse-options gnus-newsrc-options))))
15141
15142 ;; Parse options lines to find "options -n !all rec.all" and stuff.
15143 ;; The return value will be a list on the form
15144 ;; ((regexp1 . ignore)
15145 ;;  (regexp2 . subscribe)...)
15146 ;; When handling new newsgroups, groups that match a `ignore' regexp
15147 ;; will be ignored, and groups that match a `subscribe' regexp will be
15148 ;; subscribed.  A line like
15149 ;; options -n !all rec.all
15150 ;; will lead to a list that looks like
15151 ;; (("^rec\\..+" . subscribe) 
15152 ;;  ("^.+" . ignore))
15153 ;; So all "rec.*" groups will be subscribed, while all the other
15154 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
15155 ;; different from "options -n rec.all !all". 
15156 (defun gnus-newsrc-parse-options (options)
15157   (let (out eol)
15158     (save-excursion
15159       (gnus-set-work-buffer)
15160       (insert (regexp-quote options))
15161       ;; First we treat all continuation lines.
15162       (goto-char (point-min))
15163       (while (re-search-forward "\n[ \t]+" nil t)
15164         (replace-match " " t t))
15165       ;; Then we transform all "all"s into ".+"s.
15166       (goto-char (point-min))
15167       (while (re-search-forward "\\ball\\b" nil t)
15168         (replace-match ".+" t t))
15169       (goto-char (point-min))
15170       ;; We remove all other options than the "-n" ones.
15171       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
15172         (replace-match " ")
15173         (forward-char -1))
15174       (goto-char (point-min))
15175
15176       ;; We are only interested in "options -n" lines - we
15177       ;; ignore the other option lines.
15178       (while (re-search-forward "[ \t]-n" nil t)
15179         (setq eol 
15180               (or (save-excursion
15181                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
15182                          (- (point) 2)))
15183                   (gnus-point-at-eol)))
15184         ;; Search for all "words"...
15185         (while (re-search-forward "[^ \t,\n]+" eol t)
15186           (if (= (char-after (match-beginning 0)) ?!)
15187               ;; If the word begins with a bang (!), this is a "not"
15188               ;; spec.  We put this spec (minus the bang) and the
15189               ;; symbol `ignore' into the list.
15190               (setq out (cons (cons (concat 
15191                                      "^" (buffer-substring 
15192                                           (1+ (match-beginning 0))
15193                                           (match-end 0)))
15194                                     'ignore) out))
15195             ;; There was no bang, so this is a "yes" spec.
15196             (setq out (cons (cons (concat "^" (match-string 0))
15197                                   'subscribe) out)))))
15198     
15199       (setq gnus-newsrc-options-n out))))
15200
15201 (defun gnus-save-newsrc-file (&optional force)
15202   "Save .newsrc file."
15203   ;; Note: We cannot save .newsrc file if all newsgroups are removed
15204   ;; from the variable gnus-newsrc-alist.
15205   (when (and (or gnus-newsrc-alist gnus-killed-list)
15206              gnus-current-startup-file)
15207     (save-excursion
15208       (if (and (or gnus-use-dribble-file gnus-slave)
15209                (not force)
15210                (or (not gnus-dribble-buffer)
15211                    (not (buffer-name gnus-dribble-buffer))
15212                    (zerop (save-excursion
15213                             (set-buffer gnus-dribble-buffer)
15214                             (buffer-size)))))
15215           (gnus-message 4 "(No changes need to be saved)")
15216         (run-hooks 'gnus-save-newsrc-hook)
15217         (if gnus-slave
15218             (gnus-slave-save-newsrc)
15219           ;; Save .newsrc.
15220           (when gnus-save-newsrc-file
15221             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
15222             (gnus-gnus-to-newsrc-format)
15223             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
15224           ;; Save .newsrc.eld.
15225           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
15226           (make-local-variable 'version-control)
15227           (setq version-control 'never)
15228           (setq buffer-file-name 
15229                 (concat gnus-current-startup-file ".eld"))
15230           (gnus-add-current-to-buffer-list)
15231           (buffer-disable-undo (current-buffer))
15232           (erase-buffer)
15233           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
15234           (gnus-gnus-to-quick-newsrc-format)
15235           (run-hooks 'gnus-save-quick-newsrc-hook)
15236           (save-buffer)
15237           (kill-buffer (current-buffer))
15238           (gnus-message 
15239            5 "Saving %s.eld...done" gnus-current-startup-file))
15240         (gnus-dribble-delete-file)))))
15241
15242 (defun gnus-gnus-to-quick-newsrc-format ()
15243   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
15244   (insert ";; Gnus startup file.\n")
15245   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
15246   (insert ";; to read .newsrc.\n")
15247   (insert "(setq gnus-newsrc-file-version "
15248           (prin1-to-string gnus-version) ")\n")
15249   (let ((variables 
15250          (if gnus-save-killed-list gnus-variable-list
15251            ;; Remove the `gnus-killed-list' from the list of variables
15252            ;; to be saved, if required.
15253            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
15254         ;; Peel off the "dummy" group.
15255         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
15256         variable)
15257     ;; Insert the variables into the file.
15258     (while variables
15259       (when (and (boundp (setq variable (pop variables)))
15260                  (symbol-value variable))
15261         (insert "(setq " (symbol-name variable) " '"
15262                 (prin1-to-string (symbol-value variable)) ")\n")))))
15263
15264 (defun gnus-gnus-to-newsrc-format ()
15265   ;; Generate and save the .newsrc file.
15266   (let ((newsrc (cdr gnus-newsrc-alist))
15267         info ranges range)
15268     (save-excursion
15269       (set-buffer (create-file-buffer gnus-current-startup-file))
15270       (setq buffer-file-name gnus-current-startup-file)
15271       (buffer-disable-undo (current-buffer))
15272       (erase-buffer)
15273       ;; Write options.
15274       (if gnus-newsrc-options (insert gnus-newsrc-options))
15275       ;; Write subscribed and unsubscribed.
15276       (while newsrc
15277         (setq info (car newsrc))
15278         (if (not (gnus-info-method info))
15279             ;; Don't write foreign groups to .newsrc.
15280             (progn
15281               (insert (gnus-info-group info)
15282                       (if (> (gnus-info-level info) gnus-level-subscribed)
15283                           "!" ":"))
15284               (if (setq ranges (gnus-info-read info))
15285                   (progn
15286                     (insert " ")
15287                     (if (not (listp (cdr ranges)))
15288                         (if (= (car ranges) (cdr ranges))
15289                             (insert (int-to-string (car ranges)))
15290                           (insert (int-to-string (car ranges)) "-" 
15291                                   (int-to-string (cdr ranges))))
15292                       (while ranges
15293                         (setq range (car ranges)
15294                               ranges (cdr ranges))
15295                         (if (or (atom range) (= (car range) (cdr range)))
15296                             (insert (int-to-string 
15297                                      (or (and (atom range) range) 
15298                                          (car range))))
15299                           (insert (int-to-string (car range)) "-"
15300                                   (int-to-string (cdr range))))
15301                         (if ranges (insert ","))))))
15302               (insert "\n")))
15303         (setq newsrc (cdr newsrc)))
15304       (make-local-variable 'version-control)
15305       (setq version-control 'never)
15306       ;; It has been reported that sometime the modtime on the .newsrc
15307       ;; file seems to be off.  We really do want to overwrite it, so
15308       ;; we clear the modtime here before saving.  It's a bit odd,
15309       ;; though... 
15310       ;; sometimes the modtime clear isn't sufficient.  most brute force:
15311       ;; delete the silly thing entirely first.  but this fails to provide
15312       ;; such niceties as .newsrc~ creation.
15313       (if gnus-modtime-botch
15314           (delete-file gnus-startup-file)
15315         (clear-visited-file-modtime))
15316       (run-hooks 'gnus-save-standard-newsrc-hook)
15317       (save-buffer)
15318       (kill-buffer (current-buffer)))))
15319
15320
15321 ;;; Slave functions.
15322
15323 (defun gnus-slave-save-newsrc ()
15324   (save-excursion
15325     (set-buffer gnus-dribble-buffer)
15326     (let ((slave-name 
15327            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
15328       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
15329
15330 (defun gnus-master-read-slave-newsrc ()
15331   (let ((slave-files 
15332          (directory-files 
15333           (file-name-directory gnus-current-startup-file)
15334           t (concat 
15335              "^" (regexp-quote
15336                   (concat
15337                    (file-name-nondirectory gnus-current-startup-file)
15338                    "-slave-")))
15339           t))
15340         file)
15341     (if (not slave-files)
15342         ()                              ; There are no slave files to read.
15343       (gnus-message 7 "Reading slave newsrcs...")
15344       (save-excursion
15345         (set-buffer (get-buffer-create " *gnus slave*"))
15346         (buffer-disable-undo (current-buffer))
15347         (setq slave-files 
15348               (sort (mapcar (lambda (file) 
15349                               (list (nth 5 (file-attributes file)) file))
15350                             slave-files)
15351                     (lambda (f1 f2)
15352                       (or (< (car (car f1)) (car (car f2)))
15353                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
15354         (while slave-files
15355           (erase-buffer)
15356           (setq file (nth 1 (car slave-files)))
15357           (insert-file-contents file)
15358           (if (condition-case ()
15359                   (progn
15360                     (eval-buffer (current-buffer))
15361                     t)
15362                 (error 
15363                  (message "Possible error in %s" file)
15364                  (ding)
15365                  (sit-for 2)
15366                  nil))
15367               (or gnus-slave ; Slaves shouldn't delete these files.
15368                   (condition-case ()
15369                       (delete-file file)
15370                     (error nil))))
15371           (setq slave-files (cdr slave-files))))
15372       (gnus-message 7 "Reading slave newsrcs...done"))))
15373
15374
15375 ;;; Group description.
15376
15377 (defun gnus-read-all-descriptions-files ()
15378   (let ((methods (cons gnus-select-method gnus-secondary-select-methods)))
15379     (while methods
15380       (gnus-read-descriptions-file (car methods))
15381       (setq methods (cdr methods)))
15382     t))
15383
15384 (defun gnus-read-descriptions-file (&optional method)
15385   (let ((method (or method gnus-select-method)))
15386     ;; We create the hashtable whether we manage to read the desc file
15387     ;; to avoid trying to re-read after a failed read.
15388     (or gnus-description-hashtb
15389         (setq gnus-description-hashtb 
15390               (gnus-make-hashtable (length gnus-active-hashtb))))
15391     ;; Mark this method's desc file as read.
15392     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
15393                   gnus-description-hashtb)
15394
15395     (gnus-message 5 "Reading descriptions file via %s..." (car method))
15396     (cond 
15397      ((not (gnus-check-server method))
15398       (gnus-message 1 "Couldn't open server")
15399       nil)
15400      ((not (gnus-request-list-newsgroups method))
15401       (gnus-message 1 "Couldn't read newsgroups descriptions")
15402       nil)
15403      (t
15404       (let (group)
15405         (save-excursion
15406           (save-restriction
15407             (set-buffer nntp-server-buffer)
15408             (goto-char (point-min))
15409             (if (or (search-forward "\n.\n" nil t)
15410                     (goto-char (point-max)))
15411                 (progn
15412                   (beginning-of-line)
15413                   (narrow-to-region (point-min) (point))))
15414             (goto-char (point-min))
15415             (while (not (eobp))
15416               ;; If we get an error, we set group to 0, which is not a
15417               ;; symbol... 
15418               (setq group 
15419                     (condition-case ()
15420                         (let ((obarray gnus-description-hashtb))
15421                           ;; Group is set to a symbol interned in this
15422                           ;; hash table.
15423                           (read nntp-server-buffer))
15424                       (error 0)))
15425               (skip-chars-forward " \t")
15426               ;; ...  which leads to this line being effectively ignored.
15427               (and (symbolp group)
15428                    (set group (buffer-substring 
15429                                (point) (progn (end-of-line) (point)))))
15430               (forward-line 1))))
15431         (gnus-message 5 "Reading descriptions file...done")
15432         t)))))
15433
15434 (defun gnus-group-get-description (group)
15435   "Get the description of a group by sending XGTITLE to the server."
15436   (when (gnus-request-group-description group)
15437     (save-excursion
15438       (set-buffer nntp-server-buffer)
15439       (goto-char (point-min))
15440       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
15441         (match-string 1)))))
15442
15443 ;;;
15444 ;;; Buffering of read articles.
15445 ;;;
15446
15447 (defvar gnus-backlog-buffer " *Gnus Backlog*")
15448 (defvar gnus-backlog-articles nil)
15449 (defvar gnus-backlog-hashtb nil)
15450
15451 (defun gnus-backlog-buffer ()
15452   "Return the backlog buffer."
15453   (or (get-buffer gnus-backlog-buffer)
15454       (save-excursion
15455         (set-buffer (get-buffer-create gnus-backlog-buffer))
15456         (buffer-disable-undo (current-buffer))
15457         (setq buffer-read-only t)
15458         (gnus-add-current-to-buffer-list)
15459         (get-buffer gnus-backlog-buffer))))
15460
15461 (defun gnus-backlog-setup ()
15462   "Initialize backlog variables."
15463   (unless gnus-backlog-hashtb
15464     (setq gnus-backlog-hashtb (make-vector 1023 0))))
15465
15466 (defun gnus-backlog-shutdown ()
15467   "Clear all backlog variables and buffers."
15468   (when (get-buffer gnus-backlog-buffer)
15469     (kill-buffer gnus-backlog-buffer))
15470   (setq gnus-backlog-hashtb nil
15471         gnus-backlog-articles nil))
15472
15473 (defun gnus-backlog-enter-article (group number buffer)
15474   (gnus-backlog-setup)
15475   (let ((ident (intern (concat group ":" (int-to-string number))
15476                        gnus-backlog-hashtb))
15477         b)
15478     (if (memq ident gnus-backlog-articles)
15479         () ; It's already kept.
15480       ;; Remove the oldest article, if necessary.
15481       (and (numberp gnus-keep-backlog)
15482            (>= (length gnus-backlog-articles) gnus-keep-backlog)
15483            (gnus-backlog-remove-oldest-article))
15484       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
15485       ;; Insert the new article.
15486       (save-excursion
15487         (set-buffer (gnus-backlog-buffer))
15488         (let (buffer-read-only)
15489           (goto-char (point-max))
15490           (or (bolp) (insert "\n"))
15491           (setq b (point))
15492           (insert-buffer-substring buffer)
15493           ;; Tag the beginning of the article with the ident.
15494           (put-text-property b (1+ b) 'gnus-backlog ident))))))
15495
15496 (defun gnus-backlog-remove-oldest-article ()
15497   (save-excursion
15498     (set-buffer (gnus-backlog-buffer))
15499     (goto-char (point-min))
15500     (if (zerop (buffer-size))
15501         () ; The buffer is empty.
15502       (let ((ident (get-text-property (point) 'gnus-backlog))
15503             buffer-read-only)
15504         ;; Remove the ident from the list of articles.
15505         (when ident
15506           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
15507         ;; Delete the article itself.
15508         (delete-region 
15509          (point) (next-single-property-change
15510                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
15511
15512 (defun gnus-backlog-request-article (group number buffer)
15513   (gnus-backlog-setup)
15514   (let ((ident (intern (concat group ":" (int-to-string number))
15515                        gnus-backlog-hashtb))
15516         beg end)
15517     (when (memq ident gnus-backlog-articles)
15518       ;; It was in the backlog.
15519       (save-excursion
15520         (set-buffer (gnus-backlog-buffer))
15521         (if (not (setq beg (text-property-any 
15522                             (point-min) (point-max) 'gnus-backlog
15523                             ident)))
15524             ;; It wasn't in the backlog after all.
15525             (progn
15526               (setq gnus-backlog-articles (delq ident gnus-backlog-articles))
15527               nil)
15528           ;; Find the end (i. e., the beginning of the next article).
15529           (setq end
15530                 (next-single-property-change 
15531                  (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
15532       (let ((buffer-read-only nil))
15533         (erase-buffer)
15534         (insert-buffer-substring gnus-backlog-buffer beg end)
15535         t))))
15536
15537 ;; Allow redefinition of Gnus functions.
15538
15539 (gnus-ems-redefine)
15540
15541 (provide 'gnus)
15542
15543 ;;; gnus.el ends here