*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval '(run-hooks 'gnus-load-hook))
30
31 (require 'mail-utils)
32 (require 'timezone)
33 (require 'nnheader)
34 (require 'message)
35
36 (eval-when-compile (require 'cl))
37
38 ;; Site dependent variables.  These variables should be defined in
39 ;; paths.el.
40
41 (defvar gnus-default-nntp-server nil
42   "Specify a default NNTP server.
43 This variable should be defined in paths.el, and should never be set
44 by the user.
45 If you want to change servers, you should use `gnus-select-method'.
46 See the documentation to that variable.")
47
48 (defvar gnus-backup-default-subscribed-newsgroups
49   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
50   "Default default new newsgroups the first time Gnus is run.
51 Should be set in paths.el, and shouldn't be touched by the user.")
52
53 (defvar gnus-local-domain nil
54   "Local domain name without a host name.
55 The DOMAINNAME environment variable is used instead if it is defined.
56 If the `system-name' function returns the full Internet name, there is
57 no need to set this variable.")
58
59 (defvar gnus-local-organization nil
60   "String with a description of what organization (if any) the user belongs to.
61 The ORGANIZATION environment variable is used instead if it is defined.
62 If this variable contains a function, this function will be called
63 with the current newsgroup name as the argument.  The function should
64 return a string.
65
66 In any case, if the string (either in the variable, in the environment
67 variable, or returned by the function) is a file name, the contents of
68 this file will be used as the organization.")
69
70 ;; Customization variables
71
72 ;; Don't touch this variable.
73 (defvar gnus-nntp-service "nntp"
74   "*NNTP service name (\"nntp\" or 119).
75 This is an obsolete variable, which is scarcely used.  If you use an
76 nntp server for your newsgroup and want to change the port number
77 used to 899, you would say something along these lines:
78
79  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
80
81 (defvar gnus-nntpserver-file "/etc/nntpserver"
82   "*A file with only the name of the nntp server in it.")
83
84 ;; This function is used to check both the environment variable
85 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
86 ;; an nntp server name default.
87 (defun gnus-getenv-nntpserver ()
88   (or (getenv "NNTPSERVER")
89       (and (file-readable-p gnus-nntpserver-file)
90            (save-excursion
91              (set-buffer (get-buffer-create " *gnus nntp*"))
92              (buffer-disable-undo (current-buffer))
93              (insert-file-contents gnus-nntpserver-file)
94              (let ((name (buffer-string)))
95                (prog1
96                    (if (string-match "^[ \t\n]*$" name)
97                        nil
98                      name)
99                  (kill-buffer (current-buffer))))))))
100
101 (defvar gnus-select-method
102   (nconc
103    (list 'nntp (or (condition-case ()
104                        (gnus-getenv-nntpserver)
105                      (error nil))
106                    (if (and gnus-default-nntp-server
107                             (not (string= gnus-default-nntp-server "")))
108                        gnus-default-nntp-server)
109                    (system-name)))
110    (if (or (null gnus-nntp-service)
111            (equal gnus-nntp-service "nntp"))
112        nil
113      (list gnus-nntp-service)))
114   "*Default method for selecting a newsgroup.
115 This variable should be a list, where the first element is how the
116 news is to be fetched, the second is the address.
117
118 For instance, if you want to get your news via NNTP from
119 \"flab.flab.edu\", you could say:
120
121 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
122
123 If you want to use your local spool, say:
124
125 (setq gnus-select-method (list 'nnspool (system-name)))
126
127 If you use this variable, you must set `gnus-nntp-server' to nil.
128
129 There is a lot more to know about select methods and virtual servers -
130 see the manual for details.")
131
132 (defvar gnus-message-archive-method 
133   '(nnfolder "archive" (nnfolder-directory "~/Mail/archive/")
134              (nnfolder-active-file "~/Mail/archive/active")
135              (nnfolder-get-new-mail nil)
136              (nnfolder-inhibit-expiry t))
137   "*Method used for archiving messages you've sent.
138 This should be a mail method.")
139
140 (defvar gnus-refer-article-method nil
141   "*Preferred method for fetching an article by Message-ID.
142 If you are reading news from the local spool (with nnspool), fetching
143 articles by Message-ID is painfully slow.  By setting this method to an
144 nntp method, you might get acceptable results.
145
146 The value of this variable must be a valid select method as discussed
147 in the documentation of `gnus-select-method'.")
148
149 (defvar gnus-secondary-select-methods nil
150   "*A list of secondary methods that will be used for reading news.
151 This is a list where each element is a complete select method (see
152 `gnus-select-method').
153
154 If, for instance, you want to read your mail with the nnml backend,
155 you could set this variable:
156
157 (setq gnus-secondary-select-methods '((nnml \"\")))")
158
159 (defvar gnus-secondary-servers nil
160   "*List of NNTP servers that the user can choose between interactively.
161 To make Gnus query you for a server, you have to give `gnus' a
162 non-numeric prefix - `C-u M-x gnus', in short.")
163
164 (defvar gnus-nntp-server nil
165   "*The name of the host running the NNTP server.
166 This variable is semi-obsolete.  Use the `gnus-select-method'
167 variable instead.")
168
169 (defvar gnus-startup-file "~/.newsrc"
170   "*Your `.newsrc' file.
171 `.newsrc-SERVER' will be used instead if that exists.")
172
173 (defvar gnus-init-file "~/.gnus"
174   "*Your Gnus elisp startup file.
175 If a file with the .el or .elc suffixes exist, it will be read
176 instead.")
177
178 (defvar gnus-group-faq-directory
179   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
180     "/ftp@sunsite.auc.dk:/pub/usenet/"
181     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
182     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
183     "/ftp@rtfm.mit.edu:/pub/usenet/"
184     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
185     "/ftp@ftp.sunet.se:/pub/usenet/"
186     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
187     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
188     "/ftp@ftp.hk.super.net:/mirror/faqs/")
189   "*Directory where the group FAQs are stored.
190 This will most commonly be on a remote machine, and the file will be
191 fetched by ange-ftp.
192
193 This variable can also be a list of directories.  In that case, the
194 first element in the list will be used by default, and the others will
195 be used as backup sites.
196
197 Note that Gnus uses an aol machine as the default directory.  If this
198 feels fundamentally unclean, just think of it as a way to finally get
199 something of value back from them.
200
201 If the default site is too slow, try one of these:
202
203    North America: mirrors.aol.com                /pub/rtfm/usenet
204                   ftp.seas.gwu.edu               /pub/rtfm
205                   rtfm.mit.edu                   /pub/usenet
206    Europe:        ftp.uni-paderborn.de           /pub/FAQ
207                   src.doc.ic.ac.uk               /usenet/news-FAQS
208                   ftp.sunet.se                   /pub/usenet
209                   sunsite.auc.dk                 /pub/usenet
210    Asia:          nctuccca.edu.tw                /USENET/FAQ
211                   hwarang.postech.ac.kr          /pub/usenet
212                   ftp.hk.super.net               /mirror/faqs")
213
214 (defvar gnus-group-archive-directory
215   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
216   "*The address of the (ding) archives.")
217
218 (defvar gnus-group-recent-archive-directory
219   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
220   "*The address of the most recent (ding) articles.")
221
222 (defvar gnus-default-subscribed-newsgroups nil
223   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
224 It should be a list of strings.
225 If it is `t', Gnus will not do anything special the first time it is
226 started; it'll just use the normal newsgroups subscription methods.")
227
228 (defvar gnus-use-cross-reference t
229   "*Non-nil means that cross referenced articles will be marked as read.
230 If nil, ignore cross references.  If t, mark articles as read in
231 subscribed newsgroups.  If neither t nor nil, mark as read in all
232 newsgroups.")
233
234 (defvar gnus-single-article-buffer t
235   "*If non-nil, display all articles in the same buffer.
236 If nil, each group will get its own article buffer.")
237
238 (defvar gnus-use-dribble-file t
239   "*Non-nil means that Gnus will use a dribble file to store user updates.
240 If Emacs should crash without saving the .newsrc files, complete
241 information can be restored from the dribble file.")
242
243 (defvar gnus-dribble-directory nil
244   "*The directory where dribble files will be saved.
245 If this variable is nil, the directory where the .newsrc files are
246 saved will be used.")
247
248 (defvar gnus-asynchronous nil
249   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
250
251 (defvar gnus-kill-summary-on-exit t
252   "*If non-nil, kill the summary buffer when you exit from it.
253 If nil, the summary will become a \"*Dead Summary*\" buffer, and
254 it will be killed sometime later.")
255
256 (defvar gnus-large-newsgroup 200
257   "*The number of articles which indicates a large newsgroup.
258 If the number of articles in a newsgroup is greater than this value,
259 confirmation is required for selecting the newsgroup.")
260
261 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
262 (defvar gnus-no-groups-message "No news is horrible news"
263   "*Message displayed by Gnus when no groups are available.")
264
265 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
266   "*Non-nil means that the default name of a file to save articles in is the group name.
267 If it's nil, the directory form of the group name is used instead.
268
269 If this variable is a list, and the list contains the element
270 `not-score', long file names will not be used for score files; if it
271 contains the element `not-save', long file names will not be used for
272 saving; and if it contains the element `not-kill', long file names
273 will not be used for kill files.")
274
275 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
276   "*Name of the directory articles will be saved in (default \"~/News\").
277 Initialized from the SAVEDIR environment variable.")
278
279 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
280   "*Name of the directory where kill files will be stored (default \"~/News\").
281 Initialized from the SAVEDIR environment variable.")
282
283 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
284   "*A function to save articles in your favorite format.
285 The function must be interactively callable (in other words, it must
286 be an Emacs command).
287
288 Gnus provides the following functions:
289
290 * gnus-summary-save-in-rmail (Rmail format)
291 * gnus-summary-save-in-mail (Unix mail format)
292 * gnus-summary-save-in-folder (MH folder)
293 * gnus-summary-save-in-file (article format).
294 * gnus-summary-save-in-vm (use VM's folder format).")
295
296 (defvar gnus-prompt-before-saving 'always
297   "*This variable says how much prompting is to be done when saving articles.
298 If it is nil, no prompting will be done, and the articles will be
299 saved to the default files.  If this variable is `always', each and
300 every article that is saved will be preceded by a prompt, even when
301 saving large batches of articles.  If this variable is neither nil not
302 `always', there the user will be prompted once for a file name for
303 each invocation of the saving commands.")
304
305 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
306   "*A function generating a file name to save articles in Rmail format.
307 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
308
309 (defvar gnus-mail-save-name (function gnus-plain-save-name)
310   "*A function generating a file name to save articles in Unix mail format.
311 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
312
313 (defvar gnus-folder-save-name (function gnus-folder-save-name)
314   "*A function generating a file name to save articles in MH folder.
315 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
316
317 (defvar gnus-file-save-name (function gnus-numeric-save-name)
318   "*A function generating a file name to save articles in article format.
319 The function is called with NEWSGROUP, HEADERS, and optional
320 LAST-FILE.")
321
322 (defvar gnus-split-methods
323   '((gnus-article-archive-name))
324   "*Variable used to suggest where articles are to be saved.
325 For instance, if you would like to save articles related to Gnus in
326 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
327 you could set this variable to something like:
328
329  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
330    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
331
332 This variable is an alist where the where the key is the match and the
333 value is a list of possible files to save in if the match is non-nil.
334
335 If the match is a string, it is used as a regexp match on the
336 article.  If the match is a symbol, that symbol will be funcalled
337 from the buffer of the article to be saved with the newsgroup as the
338 parameter.  If it is a list, it will be evaled in the same buffer.
339
340 If this form or function returns a string, this string will be used as
341 a possible file name; and if it returns a non-nil list, that list will
342 be used as possible file names.")
343
344 (defvar gnus-move-split-methods nil
345   "*Variable used to suggest where articles are to be moved to.
346 It uses the same syntax as the `gnus-split-methods' variable.")
347
348 (defvar gnus-save-score nil
349   "*If non-nil, save group scoring info.")
350
351 (defvar gnus-use-adaptive-scoring nil
352   "*If non-nil, use some adaptive scoring scheme.")
353
354 (defvar gnus-use-cache nil
355   "*If nil, Gnus will ignore the article cache.
356 If `passive', it will allow entering (and reading) articles
357 explicitly entered into the cache.  If anything else, use the
358 cache to the full extent of the law.")
359
360 (defvar gnus-use-trees nil
361   "*If non-nil, display a thread tree buffer.")
362
363 (defvar gnus-use-grouplens nil
364   "*If non-nil, use GroupLens ratings.")
365
366 (defvar gnus-keep-backlog nil
367   "*If non-nil, Gnus will keep read articles for later re-retrieval.
368 If it is a number N, then Gnus will only keep the last N articles
369 read.  If it is neither nil nor a number, Gnus will keep all read
370 articles.  This is not a good idea.")
371
372 (defvar gnus-use-nocem nil
373   "*If non-nil, Gnus will read NoCeM cancel messages.")
374
375 (defvar gnus-use-demon nil
376   "If non-nil, Gnus might use some demons.")
377
378 (defvar gnus-use-scoring t
379   "*If non-nil, enable scoring.")
380
381 (defvar gnus-use-picons nil
382   "*If non-nil, display picons.")
383
384 (defvar gnus-fetch-old-headers nil
385   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
386 If an unread article in the group refers to an older, already read (or
387 just marked as read) article, the old article will not normally be
388 displayed in the Summary buffer.  If this variable is non-nil, Gnus
389 will attempt to grab the headers to the old articles, and thereby
390 build complete threads.  If it has the value `some', only enough
391 headers to connect otherwise loose threads will be displayed.
392 This variable can also be a number.  In that case, no more than that
393 number of old headers will be fetched.
394
395 The server has to support NOV for any of this to work.")
396
397 ;see gnus-cus.el
398 ;(defvar gnus-visual t
399 ;  "*If non-nil, will do various highlighting.
400 ;If nil, no mouse highlights (or any other highlights) will be
401 ;performed.  This might speed up Gnus some when generating large group
402 ;and summary buffers.")
403
404 (defvar gnus-novice-user t
405   "*Non-nil means that you are a usenet novice.
406 If non-nil, verbose messages may be displayed and confirmations may be
407 required.")
408
409 (defvar gnus-expert-user nil
410   "*Non-nil means that you will never be asked for confirmation about anything.
411 And that means *anything*.")
412
413 (defvar gnus-verbose 7
414   "*Integer that says how verbose Gnus should be.
415 The higher the number, the more messages Gnus will flash to say what
416 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
417 display most important messages; and at ten, Gnus will keep on
418 jabbering all the time.")
419
420 (defvar gnus-keep-same-level nil
421   "*Non-nil means that the next newsgroup after the current will be on the same level.
422 When you type, for instance, `n' after reading the last article in the
423 current newsgroup, you will go to the next newsgroup.  If this variable
424 is nil, the next newsgroup will be the next from the group
425 buffer.
426 If this variable is non-nil, Gnus will either put you in the
427 next newsgroup with the same level, or, if no such newsgroup is
428 available, the next newsgroup with the lowest possible level higher
429 than the current level.
430 If this variable is `best', Gnus will make the next newsgroup the one
431 with the best level.")
432
433 (defvar gnus-summary-make-false-root 'adopt
434   "*nil means that Gnus won't gather loose threads.
435 If the root of a thread has expired or been read in a previous
436 session, the information necessary to build a complete thread has been
437 lost.  Instead of having many small sub-threads from this original thread
438 scattered all over the summary buffer, Gnus can gather them.
439
440 If non-nil, Gnus will try to gather all loose sub-threads from an
441 original thread into one large thread.
442
443 If this variable is non-nil, it should be one of `none', `adopt',
444 `dummy' or `empty'.
445
446 If this variable is `none', Gnus will not make a false root, but just
447 present the sub-threads after another.
448 If this variable is `dummy', Gnus will create a dummy root that will
449 have all the sub-threads as children.
450 If this variable is `adopt', Gnus will make one of the \"children\"
451 the parent and mark all the step-children as such.
452 If this variable is `empty', the \"children\" are printed with empty
453 subject fields.  (Or rather, they will be printed with a string
454 given by the `gnus-summary-same-subject' variable.)")
455
456 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
457   "*A regexp to match subjects to be excluded from loose thread gathering.
458 As loose thread gathering is done on subjects only, that means that
459 there can be many false gatherings performed.  By rooting out certain
460 common subjects, gathering might become saner.")
461
462 (defvar gnus-summary-gather-subject-limit nil
463   "*Maximum length of subject comparisons when gathering loose threads.
464 Use nil to compare full subjects.  Setting this variable to a low
465 number will help gather threads that have been corrupted by
466 newsreaders chopping off subject lines, but it might also mean that
467 unrelated articles that have subject that happen to begin with the
468 same few characters will be incorrectly gathered.
469
470 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
471 comparing subjects.")
472
473 (defvar gnus-simplify-ignored-prefixes nil
474   "*Regexp, matches for which are removed from subject lines when simplifying.")
475
476 (defvar gnus-build-sparse-threads nil
477   "*If non-nil, fill in the gaps in threads.
478 If `some', only fill in the gaps that are needed to tie loose threads
479 together.  If `more', fill in all leaf nodes that Gnus can find.  If
480 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
481
482 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
483   "Function used for gathering loose threads.
484 There are two pre-defined functions: `gnus-gather-threads-by-subject',
485 which only takes Subjects into consideration; and
486 `gnus-gather-threads-by-references', which compared the References
487 headers of the articles to find matches.")
488
489 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
490 (defvar gnus-summary-same-subject ""
491   "*String indicating that the current article has the same subject as the previous.
492 This variable will only be used if the value of
493 `gnus-summary-make-false-root' is `empty'.")
494
495 (defvar gnus-summary-goto-unread t
496   "*If non-nil, marking commands will go to the next unread article.
497 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
498 whether it is read or not.")
499
500 (defvar gnus-group-goto-unread t
501   "*If non-nil, movement commands will go to the next unread and subscribed group.")
502
503 (defvar gnus-goto-next-group-when-activating t
504   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
505
506 (defvar gnus-check-new-newsgroups t
507   "*Non-nil means that Gnus will add new newsgroups at startup.
508 If this variable is `ask-server', Gnus will ask the server for new
509 groups since the last time it checked.  This means that the killed list
510 is no longer necessary, so you could set `gnus-save-killed-list' to
511 nil.
512
513 A variant is to have this variable be a list of select methods.  Gnus
514 will then use the `ask-server' method on all these select methods to
515 query for new groups from all those servers.
516
517 Eg.
518   (setq gnus-check-new-newsgroups
519         '((nntp \"some.server\") (nntp \"other.server\")))
520
521 If this variable is nil, then you have to tell Gnus explicitly to
522 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
523
524 (defvar gnus-check-bogus-newsgroups nil
525   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
526 If this variable is nil, then you have to tell Gnus explicitly to
527 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
528
529 (defvar gnus-read-active-file t
530   "*Non-nil means that Gnus will read the entire active file at startup.
531 If this variable is nil, Gnus will only know about the groups in your
532 `.newsrc' file.
533
534 If this variable is `some', Gnus will try to only read the relevant
535 parts of the active file from the server.  Not all servers support
536 this, and it might be quite slow with other servers, but this should
537 generally be faster than both the t and nil value.
538
539 If you set this variable to nil or `some', you probably still want to
540 be told about new newsgroups that arrive.  To do that, set
541 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
542 properly with all servers.")
543
544 (defvar gnus-level-subscribed 5
545   "*Groups with levels less than or equal to this variable are subscribed.")
546
547 (defvar gnus-level-unsubscribed 7
548   "*Groups with levels less than or equal to this variable are unsubscribed.
549 Groups with levels less than `gnus-level-subscribed', which should be
550 less than this variable, are subscribed.")
551
552 (defvar gnus-level-zombie 8
553   "*Groups with this level are zombie groups.")
554
555 (defvar gnus-level-killed 9
556   "*Groups with this level are killed.")
557
558 (defvar gnus-level-default-subscribed 3
559   "*New subscribed groups will be subscribed at this level.")
560
561 (defvar gnus-level-default-unsubscribed 6
562   "*New unsubscribed groups will be unsubscribed at this level.")
563
564 (defvar gnus-activate-level (1+ gnus-level-subscribed)
565   "*Groups higher than this level won't be activated on startup.
566 Setting this variable to something log might save lots of time when
567 you have many groups that you aren't interested in.")
568
569 (defvar gnus-activate-foreign-newsgroups 4
570   "*If nil, Gnus will not check foreign newsgroups at startup.
571 If it is non-nil, it should be a number between one and nine.  Foreign
572 newsgroups that have a level lower or equal to this number will be
573 activated on startup.  For instance, if you want to active all
574 subscribed newsgroups, but not the rest, you'd set this variable to
575 `gnus-level-subscribed'.
576
577 If you subscribe to lots of newsgroups from different servers, startup
578 might take a while.  By setting this variable to nil, you'll save time,
579 but you won't be told how many unread articles there are in the
580 groups.")
581
582 (defvar gnus-save-newsrc-file t
583   "*Non-nil means that Gnus will save the `.newsrc' file.
584 Gnus always saves its own startup file, which is called
585 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
586 be readily understood by other newsreaders.  If you don't plan on
587 using other newsreaders, set this variable to nil to save some time on
588 exit.")
589
590 (defvar gnus-save-killed-list t
591   "*If non-nil, save the list of killed groups to the startup file.
592 If you set this variable to nil, you'll save both time (when starting
593 and quitting) and space (both memory and disk), but it will also mean
594 that Gnus has no record of which groups are new and which are old, so
595 the automatic new newsgroups subscription methods become meaningless.
596
597 You should always set `gnus-check-new-newsgroups' to `ask-server' or
598 nil if you set this variable to nil.")
599
600 (defvar gnus-interactive-catchup t
601   "*If non-nil, require your confirmation when catching up a group.")
602
603 (defvar gnus-interactive-post t
604   "*If non-nil, group name will be asked for when posting.")
605
606 (defvar gnus-interactive-exit t
607   "*If non-nil, require your confirmation when exiting Gnus.")
608
609 (defvar gnus-kill-killed t
610   "*If non-nil, Gnus will apply kill files to already killed articles.
611 If it is nil, Gnus will never apply kill files to articles that have
612 already been through the scoring process, which might very well save lots
613 of time.")
614
615 (defvar gnus-extract-address-components 'gnus-extract-address-components
616   "*Function for extracting address components from a From header.
617 Two pre-defined function exist: `gnus-extract-address-components',
618 which is the default, quite fast, and too simplistic solution, and
619 `mail-extract-address-components', which works much better, but is
620 slower.")
621
622 (defvar gnus-summary-default-score 0
623   "*Default article score level.
624 If this variable is nil, scoring will be disabled.")
625
626 (defvar gnus-summary-zcore-fuzz 0
627   "*Fuzziness factor for the zcore in the summary buffer.
628 Articles with scores closer than this to `gnus-summary-default-score'
629 will not be marked.")
630
631 (defvar gnus-simplify-subject-fuzzy-regexp nil
632   "*Strings to be removed when doing fuzzy matches.
633 This can either be a regular expression or list of regular expressions
634 that will be removed from subject strings if fuzzy subject
635 simplification is selected.")
636
637 (defvar gnus-permanently-visible-groups nil
638   "*Regexp to match groups that should always be listed in the group buffer.
639 This means that they will still be listed when there are no unread
640 articles in the groups.")
641
642 (defvar gnus-list-groups-with-ticked-articles t
643   "*If non-nil, list groups that have only ticked articles.
644 If nil, only list groups that have unread articles.")
645
646 (defvar gnus-group-default-list-level gnus-level-subscribed
647   "*Default listing level.
648 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
649
650 (defvar gnus-group-use-permanent-levels nil
651   "*If non-nil, once you set a level, Gnus will use this level.")
652
653 (defvar gnus-group-list-inactive-groups t
654   "*If non-nil, inactive groups will be listed.")
655
656 (defvar gnus-show-mime nil
657   "*If non-nil, do mime processing of articles.
658 The articles will simply be fed to the function given by
659 `gnus-show-mime-method'.")
660
661 (defvar gnus-strict-mime t
662   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
663
664 (defvar gnus-show-mime-method 'metamail-buffer
665   "*Function to process a MIME message.
666 The function is called from the article buffer.")
667
668 (defvar gnus-decode-encoded-word-method (lambda ())
669   "*Function to decode a MIME encoded-words.
670 The function is called from the article buffer.")
671
672 (defvar gnus-show-threads t
673   "*If non-nil, display threads in summary mode.")
674
675 (defvar gnus-thread-hide-subtree nil
676   "*If non-nil, hide all threads initially.
677 If threads are hidden, you have to run the command
678 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
679 to expose hidden threads.")
680
681 (defvar gnus-thread-hide-killed t
682   "*If non-nil, hide killed threads automatically.")
683
684 (defvar gnus-thread-ignore-subject nil
685   "*If non-nil, ignore subjects and do all threading based on the Reference header.
686 If nil, which is the default, articles that have different subjects
687 from their parents will start separate threads.")
688
689 (defvar gnus-thread-operation-ignore-subject t
690   "*If non-nil, subjects will be ignored when doing thread commands.
691 This affects commands like `gnus-summary-kill-thread' and
692 `gnus-summary-lower-thread'.
693
694 If this variable is nil, articles in the same thread with different
695 subjects will not be included in the operation in question.  If this
696 variable is `fuzzy', only articles that have subjects that are fuzzily
697 equal will be included.")
698
699 (defvar gnus-thread-indent-level 4
700   "*Number that says how much each sub-thread should be indented.")
701
702 (defvar gnus-ignored-newsgroups
703   (purecopy (mapconcat 'identity
704                        '("^to\\."       ; not "real" groups
705                          "^[0-9. \t]+ " ; all digits in name
706                          "[][\"#'()]"   ; bogus characters
707                          )
708                        "\\|"))
709   "*A regexp to match uninteresting newsgroups in the active file.
710 Any lines in the active file matching this regular expression are
711 removed from the newsgroup list before anything else is done to it,
712 thus making them effectively non-existent.")
713
714 (defvar gnus-ignored-headers
715   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
716   "*All headers that match this regexp will be hidden.
717 This variable can also be a list of regexps of headers to be ignored.
718 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
719
720 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-"
721   "*All headers that do not match this regexp will be hidden.
722 This variable can also be a list of regexp of headers to remain visible.
723 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
724
725 (defvar gnus-sorted-header-list
726   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
727     "^Cc:" "^Date:" "^Organization:")
728   "*This variable is a list of regular expressions.
729 If it is non-nil, headers that match the regular expressions will
730 be placed first in the article buffer in the sequence specified by
731 this list.")
732
733 (defvar gnus-boring-article-headers
734   '(empty followup-to reply-to)
735   "*Headers that are only to be displayed if they have interesting data.
736 Possible values in this list are `empty', `newsgroups', `followup-to',
737 `reply-to', and `date'.")
738
739 (defvar gnus-show-all-headers nil
740   "*If non-nil, don't hide any headers.")
741
742 (defvar gnus-save-all-headers t
743   "*If non-nil, don't remove any headers before saving.")
744
745 (defvar gnus-saved-headers gnus-visible-headers
746   "*Headers to keep if `gnus-save-all-headers' is nil.
747 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
748 If that variable is nil, however, all headers that match this regexp
749 will be kept while the rest will be deleted before saving.")
750
751 (defvar gnus-inhibit-startup-message nil
752   "*If non-nil, the startup message will not be displayed.")
753
754 (defvar gnus-signature-separator "^-- *$"
755   "Regexp matching signature separator.")
756
757 (defvar gnus-signature-limit nil
758   "Provide a limit to what is considered a signature.
759 If it is a number, no signature may not be longer (in characters) than
760 that number.  If it is a function, the function will be called without
761 any parameters, and if it returns nil, there is no signature in the
762 buffer.  If it is a string, it will be used as a regexp.  If it
763 matches, the text in question is not a signature.")
764
765 (defvar gnus-auto-extend-newsgroup t
766   "*If non-nil, extend newsgroup forward and backward when requested.")
767
768 (defvar gnus-auto-select-first t
769   "*If nil, don't select the first unread article when entering a group.
770 If this variable is `best', select the highest-scored unread article
771 in the group.  If neither nil nor `best', select the first unread
772 article.
773
774 If you want to prevent automatic selection of the first unread article
775 in some newsgroups, set the variable to nil in
776 `gnus-select-group-hook'.")
777
778 (defvar gnus-auto-select-next t
779   "*If non-nil, offer to go to the next group from the end of the previous.
780 If the value is t and the next newsgroup is empty, Gnus will exit
781 summary mode and go back to group mode.  If the value is neither nil
782 nor t, Gnus will select the following unread newsgroup.  In
783 particular, if the value is the symbol `quietly', the next unread
784 newsgroup will be selected without any confirmation, and if it is
785 `almost-quietly', the next group will be selected without any
786 confirmation if you are located on the last article in the group.
787 Finally, if this variable is `slightly-quietly', the `Z n' command
788 will go to the next group without confirmation.")
789
790 (defvar gnus-auto-select-same nil
791   "*If non-nil, select the next article with the same subject.")
792
793 (defvar gnus-summary-check-current nil
794   "*If non-nil, consider the current article when moving.
795 The \"unread\" movement commands will stay on the same line if the
796 current article is unread.")
797
798 (defvar gnus-auto-center-summary t
799   "*If non-nil, always center the current summary buffer.
800 In particular, if `vertical' do only vertical recentering.  If non-nil
801 and non-`vertical', do both horizontal and vertical recentering.")
802
803 (defvar gnus-break-pages t
804   "*If non-nil, do page breaking on articles.
805 The page delimiter is specified by the `gnus-page-delimiter'
806 variable.")
807
808 (defvar gnus-page-delimiter "^\^L"
809   "*Regexp describing what to use as article page delimiters.
810 The default value is \"^\^L\", which is a form linefeed at the
811 beginning of a line.")
812
813 (defvar gnus-use-full-window t
814   "*If non-nil, use the entire Emacs screen.")
815
816 (defvar gnus-window-configuration nil
817   "Obsolete variable.  See `gnus-buffer-configuration'.")
818
819 (defvar gnus-window-min-width 2
820   "*Minimum width of Gnus buffers.")
821
822 (defvar gnus-window-min-height 1
823   "*Minimum height of Gnus buffers.")
824
825 (defvar gnus-buffer-configuration
826   '((group
827      (vertical 1.0
828                (group 1.0 point)
829                (if gnus-carpal '(group-carpal 4))))
830     (summary
831      (vertical 1.0
832                (summary 1.0 point)
833                (if gnus-carpal '(summary-carpal 4))))
834     (article
835      (cond 
836       (gnus-use-picons
837        '(frame 1.0
838                (vertical 1.0
839                          (summary 0.25 point)
840                          (if gnus-carpal '(summary-carpal 4))
841                          (article 1.0))
842                (vertical ((height . 5) (width . 15)
843                           (user-position . t)
844                           (left . -1) (top . 1))
845                          (picons 1.0))))
846       (gnus-use-trees
847        '(vertical 1.0
848                   (summary 0.25 point)
849                   (tree 0.25)
850                   (article 1.0)))
851       (t
852        '(vertical 1.0
853                  (summary 0.25 point)
854                  (if gnus-carpal '(summary-carpal 4))
855                  (if gnus-use-trees '(tree 0.25))
856                  (article 1.0)))))
857     (server
858      (vertical 1.0
859                (server 1.0 point)
860                (if gnus-carpal '(server-carpal 2))))
861     (browse
862      (vertical 1.0
863                (browse 1.0 point)
864                (if gnus-carpal '(browse-carpal 2))))
865     (message
866      (vertical 1.0
867                (message 1.0 point)))
868     (pick
869      (vertical 1.0
870                (article 1.0 point)))
871     (info
872      (vertical 1.0
873                (info 1.0 point)))
874     (summary-faq
875      (vertical 1.0
876                (summary 0.25)
877                (faq 1.0 point)))
878     (edit-group
879      (vertical 1.0
880                (group 0.5)
881                (edit-group 1.0 point)))
882     (edit-server
883      (vertical 1.0
884                (server 0.5)
885                (edit-server 1.0 point)))
886     (edit-score
887      (vertical 1.0
888                (summary 0.25)
889                (edit-score 1.0 point)))
890     (post
891      (vertical 1.0
892                (post 1.0 point)))
893     (reply
894      (vertical 1.0
895                (article-copy 0.5)
896                (message 1.0 point)))
897     (forward
898      (vertical 1.0
899                (message 1.0 point)))
900     (reply-yank
901      (vertical 1.0
902                (message 1.0 point)))
903     (mail-bounce
904      (vertical 1.0
905                (article 0.5)
906                (message 1.0 point)))
907     (draft
908      (vertical 1.0
909                (draft 1.0 point)))
910     (pipe
911      (vertical 1.0
912                (summary 0.25 point)
913                (if gnus-carpal '(summary-carpal 4))
914                ("*Shell Command Output*" 1.0)))
915     (bug
916      (vertical 1.0
917                ("*Gnus Help Bug*" 0.5)
918                ("*Gnus Bug*" 1.0 point)))
919     (compose-bounce
920      (vertical 1.0
921                (article 0.5)
922                (message 1.0 point))))
923   "Window configuration for all possible Gnus buffers.
924 This variable is a list of lists.  Each of these lists has a NAME and
925 a RULE.  The NAMEs are commonsense names like `group', which names a
926 rule used when displaying the group buffer; `summary', which names a
927 rule for what happens when you enter a group and do not display an
928 article buffer; and so on.  See the value of this variable for a
929 complete list of NAMEs.
930
931 Each RULE is a list of vectors.  The first element in this vector is
932 the name of the buffer to be displayed; the second element is the
933 percentage of the screen this buffer is to occupy (a number in the
934 0.0-0.99 range); the optional third element is `point', which should
935 be present to denote which buffer point is to go to after making this
936 buffer configuration.")
937
938 (defvar gnus-window-to-buffer
939   '((group . gnus-group-buffer)
940     (summary . gnus-summary-buffer)
941     (article . gnus-article-buffer)
942     (server . gnus-server-buffer)
943     (browse . "*Gnus Browse Server*")
944     (edit-group . gnus-group-edit-buffer)
945     (edit-server . gnus-server-edit-buffer)
946     (group-carpal . gnus-carpal-group-buffer)
947     (summary-carpal . gnus-carpal-summary-buffer)
948     (server-carpal . gnus-carpal-server-buffer)
949     (browse-carpal . gnus-carpal-browse-buffer)
950     (edit-score . gnus-score-edit-buffer)
951     (message . gnus-message-buffer)
952     (faq . gnus-faq-buffer)
953     (picons . "*Picons*")
954     (tree . gnus-tree-buffer)
955     (info . gnus-info-buffer)
956     (article-copy . gnus-article-copy)
957     (draft . gnus-draft-buffer))
958   "Mapping from short symbols to buffer names or buffer variables.")
959
960 (defvar gnus-carpal nil
961   "*If non-nil, display clickable icons.")
962
963 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
964   "*Function called with a group name when new group is detected.
965 A few pre-made functions are supplied: `gnus-subscribe-randomly'
966 inserts new groups at the beginning of the list of groups;
967 `gnus-subscribe-alphabetically' inserts new groups in strict
968 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
969 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
970 for your decision; `gnus-subscribe-killed' kills all new groups.")
971
972 ;; Suggested by a bug report by Hallvard B Furuseth.
973 ;; <h.b.furuseth@usit.uio.no>.
974 (defvar gnus-subscribe-options-newsgroup-method
975   (function gnus-subscribe-alphabetically)
976   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
977 If, for instance, you want to subscribe to all newsgroups in the
978 \"no\" and \"alt\" hierarchies, you'd put the following in your
979 .newsrc file:
980
981 options -n no.all alt.all
982
983 Gnus will the subscribe all new newsgroups in these hierarchies with
984 the subscription method in this variable.")
985
986 (defvar gnus-subscribe-hierarchical-interactive nil
987   "*If non-nil, Gnus will offer to subscribe hierarchically.
988 When a new hierarchy appears, Gnus will ask the user:
989
990 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
991
992 If the user pressed `d', Gnus will descend the hierarchy, `y' will
993 subscribe to all newsgroups in the hierarchy and `s' will skip this
994 hierarchy in its entirety.")
995
996 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
997   "*Function used for sorting the group buffer.
998 This function will be called with group info entries as the arguments
999 for the groups to be sorted.  Pre-made functions include
1000 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1001 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1002 `gnus-group-sort-by-rank'.
1003
1004 This variable can also be a list of sorting functions.  In that case,
1005 the most significant sort function should be the last function in the
1006 list.")
1007
1008 ;; Mark variables suggested by Thomas Michanek
1009 ;; <Thomas.Michanek@telelogic.se>.
1010 (defvar gnus-unread-mark ? 
1011   "*Mark used for unread articles.")
1012 (defvar gnus-ticked-mark ?!
1013   "*Mark used for ticked articles.")
1014 (defvar gnus-dormant-mark ??
1015   "*Mark used for dormant articles.")
1016 (defvar gnus-del-mark ?r
1017   "*Mark used for del'd articles.")
1018 (defvar gnus-read-mark ?R
1019   "*Mark used for read articles.")
1020 (defvar gnus-expirable-mark ?E
1021   "*Mark used for expirable articles.")
1022 (defvar gnus-killed-mark ?K
1023   "*Mark used for killed articles.")
1024 (defvar gnus-souped-mark ?F
1025   "*Mark used for killed articles.")
1026 (defvar gnus-kill-file-mark ?X
1027   "*Mark used for articles killed by kill files.")
1028 (defvar gnus-low-score-mark ?Y
1029   "*Mark used for articles with a low score.")
1030 (defvar gnus-catchup-mark ?C
1031   "*Mark used for articles that are caught up.")
1032 (defvar gnus-replied-mark ?A
1033   "*Mark used for articles that have been replied to.")
1034 (defvar gnus-cached-mark ?*
1035   "*Mark used for articles that are in the cache.")
1036 (defvar gnus-saved-mark ?S
1037   "*Mark used for articles that have been saved to.")
1038 (defvar gnus-process-mark ?#
1039   "*Process mark.")
1040 (defvar gnus-ancient-mark ?O
1041   "*Mark used for ancient articles.")
1042 (defvar gnus-sparse-mark ?Q
1043   "*Mark used for sparsely reffed articles.")
1044 (defvar gnus-canceled-mark ?G
1045   "*Mark used for canceled articles.")
1046 (defvar gnus-score-over-mark ?+
1047   "*Score mark used for articles with high scores.")
1048 (defvar gnus-score-below-mark ?-
1049   "*Score mark used for articles with low scores.")
1050 (defvar gnus-empty-thread-mark ? 
1051   "*There is no thread under the article.")
1052 (defvar gnus-not-empty-thread-mark ?=
1053   "*There is a thread under the article.")
1054
1055 (defvar gnus-view-pseudo-asynchronously nil
1056   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1057
1058 (defvar gnus-view-pseudos nil
1059   "*If `automatic', pseudo-articles will be viewed automatically.
1060 If `not-confirm', pseudos will be viewed automatically, and the user
1061 will not be asked to confirm the command.")
1062
1063 (defvar gnus-view-pseudos-separately t
1064   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1065 If nil, all files that use the same viewing command will be given as a
1066 list of parameters to that command.")
1067
1068 (defvar gnus-insert-pseudo-articles t
1069   "*If non-nil, insert pseudo-articles when decoding articles.")
1070
1071 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1072   "*Format of group lines.
1073 It works along the same lines as a normal formatting string,
1074 with some simple extensions.
1075
1076 %M    Only marked articles (character, \"*\" or \" \")
1077 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1078 %L    Level of subscribedness (integer)
1079 %N    Number of unread articles (integer)
1080 %I    Number of dormant articles (integer)
1081 %i    Number of ticked and dormant (integer)
1082 %T    Number of ticked articles (integer)
1083 %R    Number of read articles (integer)
1084 %t    Total number of articles (integer)
1085 %y    Number of unread, unticked articles (integer)
1086 %G    Group name (string)
1087 %g    Qualified group name (string)
1088 %D    Group description (string)
1089 %s    Select method (string)
1090 %o    Moderated group (char, \"m\")
1091 %p    Process mark (char)
1092 %O    Moderated group (string, \"(m)\" or \"\")
1093 %P    Topic indentation (string)
1094 %l    Whether there are GroupLens predictions for this group (string)
1095 %n    Select from where (string)
1096 %z    A string that look like `<%s:%n>' if a foreign select method is used
1097 %u    User defined specifier.  The next character in the format string should
1098       be a letter.  Gnus will call the function gnus-user-format-function-X,
1099       where X is the letter following %u.  The function will be passed the
1100       current header as argument.  The function should return a string, which
1101       will be inserted into the buffer just like information from any other
1102       group specifier.
1103
1104 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1105 the mouse point move inside the area.  There can only be one such area.
1106
1107 Note that this format specification is not always respected.  For
1108 reasons of efficiency, when listing killed groups, this specification
1109 is ignored altogether.  If the spec is changed considerably, your
1110 output may end up looking strange when listing both alive and killed
1111 groups.
1112
1113 If you use %o or %O, reading the active file will be slower and quite
1114 a bit of extra memory will be used. %D will also worsen performance.
1115 Also note that if you change the format specification to include any
1116 of these specs, you must probably re-start Gnus to see them go into
1117 effect.")
1118
1119 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1120   "*The format specification of the lines in the summary buffer.
1121
1122 It works along the same lines as a normal formatting string,
1123 with some simple extensions.
1124
1125 %N   Article number, left padded with spaces (string)
1126 %S   Subject (string)
1127 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1128 %n   Name of the poster (string)
1129 %a   Extracted name of the poster (string)
1130 %A   Extracted address of the poster (string)
1131 %F   Contents of the From: header (string)
1132 %x   Contents of the Xref: header (string)
1133 %D   Date of the article (string)
1134 %d   Date of the article (string) in DD-MMM format
1135 %M   Message-id of the article (string)
1136 %r   References of the article (string)
1137 %c   Number of characters in the article (integer)
1138 %L   Number of lines in the article (integer)
1139 %I   Indentation based on thread level (a string of spaces)
1140 %T   A string with two possible values: 80 spaces if the article
1141      is on thread level two or larger and 0 spaces on level one
1142 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1143 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1144 %[   Opening bracket (character, \"[\" or \"<\")
1145 %]   Closing bracket (character, \"]\" or \">\")
1146 %>   Spaces of length thread-level (string)
1147 %<   Spaces of length (- 20 thread-level) (string)
1148 %i   Article score (number)
1149 %z   Article zcore (character)
1150 %t   Number of articles under the current thread (number).
1151 %e   Whether the thread is empty or not (character).
1152 %l   GroupLens score (string).
1153 %u   User defined specifier.  The next character in the format string should
1154      be a letter.  Gnus will call the function gnus-user-format-function-X,
1155      where X is the letter following %u.  The function will be passed the
1156      current header as argument.  The function should return a string, which
1157      will be inserted into the summary just like information from any other
1158      summary specifier.
1159
1160 Text between %( and %) will be highlighted with `gnus-mouse-face'
1161 when the mouse point is placed inside the area.  There can only be one
1162 such area.
1163
1164 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1165 with care.  For reasons of efficiency, Gnus will compute what column
1166 these characters will end up in, and \"hard-code\" that.  This means that
1167 it is illegal to have these specs after a variable-length spec.  Well,
1168 you might not be arrested, but your summary buffer will look strange,
1169 which is bad enough.
1170
1171 The smart choice is to have these specs as for to the left as
1172 possible.
1173
1174 This restriction may disappear in later versions of Gnus.")
1175
1176 (defvar gnus-summary-dummy-line-format
1177   "*  %(:                          :%) %S\n"
1178   "*The format specification for the dummy roots in the summary buffer.
1179 It works along the same lines as a normal formatting string,
1180 with some simple extensions.
1181
1182 %S  The subject")
1183
1184 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1185   "*The format specification for the summary mode line.
1186 It works along the same lines as a normal formatting string,
1187 with some simple extensions:
1188
1189 %G  Group name
1190 %p  Unprefixed group name
1191 %A  Current article number
1192 %V  Gnus version
1193 %U  Number of unread articles in the group
1194 %e  Number of unselected articles in the group
1195 %Z  A string with unread/unselected article counts
1196 %g  Shortish group name
1197 %S  Subject of the current article
1198 %u  User-defined spec
1199 %s  Current score file name
1200 %d  Number of dormant articles
1201 %r  Number of articles that have been marked as read in this session
1202 %E  Number of articles expunged by the score files")
1203
1204 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1205   "*The format specification for the article mode line.
1206 See `gnus-summary-mode-line-format' for a closer description.")
1207
1208 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1209   "*The format specification for the group mode line.
1210 It works along the same lines as a normal formatting string,
1211 with some simple extensions:
1212
1213 %S   The native news server.
1214 %M   The native select method.")
1215
1216 (defvar gnus-valid-select-methods
1217   '(("nntp" post address prompt-address)
1218     ("nnspool" post address)
1219     ("nnvirtual" post-mail virtual prompt-address)
1220     ("nnmbox" mail respool address)
1221     ("nnml" mail respool address)
1222     ("nnmh" mail respool address)
1223     ("nndir" post-mail prompt-address address)
1224     ("nneething" none address prompt-address)
1225     ("nndoc" none address prompt-address)
1226     ("nnbabyl" mail address respool)
1227     ("nnkiboze" post address virtual)
1228     ("nnsoup" post-mail address)
1229     ("nndraft" post-mail)
1230     ("nnfolder" mail respool address))
1231   "An alist of valid select methods.
1232 The first element of each list lists should be a string with the name
1233 of the select method.  The other elements may be be the category of
1234 this method (ie. `post', `mail', `none' or whatever) or other
1235 properties that this method has (like being respoolable).
1236 If you implement a new select method, all you should have to change is
1237 this variable.  I think.")
1238
1239 (defvar gnus-updated-mode-lines '(group article summary tree)
1240   "*List of buffers that should update their mode lines.
1241 The list may contain the symbols `group', `article' and `summary'.  If
1242 the corresponding symbol is present, Gnus will keep that mode line
1243 updated with information that may be pertinent.
1244 If this variable is nil, screen refresh may be quicker.")
1245
1246 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1247 (defvar gnus-mode-non-string-length nil
1248   "*Max length of mode-line non-string contents.
1249 If this is nil, Gnus will take space as is needed, leaving the rest
1250 of the modeline intact.")
1251
1252 ;see gnus-cus.el
1253 ;(defvar gnus-mouse-face 'highlight
1254 ;  "*Face used for mouse highlighting in Gnus.
1255 ;No mouse highlights will be done if `gnus-visual' is nil.")
1256
1257 (defvar gnus-summary-mark-below nil
1258   "*Mark all articles with a score below this variable as read.
1259 This variable is local to each summary buffer and usually set by the
1260 score file.")
1261
1262 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1263   "*List of functions used for sorting articles in the summary buffer.
1264 This variable is only used when not using a threaded display.")
1265
1266 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1267   "*List of functions used for sorting threads in the summary buffer.
1268 By default, threads are sorted by article number.
1269
1270 Each function takes two threads and return non-nil if the first thread
1271 should be sorted before the other.  If you use more than one function,
1272 the primary sort function should be the last.  You should probably
1273 always include `gnus-thread-sort-by-number' in the list of sorting
1274 functions -- preferably first.
1275
1276 Ready-mady functions include `gnus-thread-sort-by-number',
1277 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1278 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1279 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1280
1281 (defvar gnus-thread-score-function '+
1282   "*Function used for calculating the total score of a thread.
1283
1284 The function is called with the scores of the article and each
1285 subthread and should then return the score of the thread.
1286
1287 Some functions you can use are `+', `max', or `min'.")
1288
1289 (defvar gnus-summary-expunge-below nil
1290   "All articles that have a score less than this variable will be expunged.")
1291
1292 (defvar gnus-thread-expunge-below nil
1293   "All threads that have a total score less than this variable will be expunged.
1294 See `gnus-thread-score-function' for en explanation of what a
1295 \"thread score\" is.")
1296
1297 (defvar gnus-auto-subscribed-groups
1298   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1299   "*All new groups that match this regexp will be subscribed automatically.
1300 Note that this variable only deals with new groups.  It has no effect
1301 whatsoever on old groups.")
1302
1303 (defvar gnus-options-subscribe nil
1304   "*All new groups matching this regexp will be subscribed unconditionally.
1305 Note that this variable deals only with new newsgroups.  This variable
1306 does not affect old newsgroups.")
1307
1308 (defvar gnus-options-not-subscribe nil
1309   "*All new groups matching this regexp will be ignored.
1310 Note that this variable deals only with new newsgroups.  This variable
1311 does not affect old (already subscribed) newsgroups.")
1312
1313 (defvar gnus-auto-expirable-newsgroups nil
1314   "*Groups in which to automatically mark read articles as expirable.
1315 If non-nil, this should be a regexp that should match all groups in
1316 which to perform auto-expiry.  This only makes sense for mail groups.")
1317
1318 (defvar gnus-total-expirable-newsgroups nil
1319   "*Groups in which to perform expiry of all read articles.
1320 Use with extreme caution.  All groups that match this regexp will be
1321 expiring - which means that all read articles will be deleted after
1322 (say) one week.  (This only goes for mail groups and the like, of
1323 course.)")
1324
1325 (defvar gnus-group-uncollapsed-levels 1
1326   "Number of group name elements to leave alone when making a short group name.")
1327
1328 (defvar gnus-hidden-properties '(invisible t intangible t)
1329   "Property list to use for hiding text.")
1330
1331 (defvar gnus-modtime-botch nil
1332   "*Non-nil means .newsrc should be deleted prior to save.  
1333 Its use is due to the bogus appearance that .newsrc was modified on
1334 disc.")
1335
1336 ;; Hooks.
1337
1338 (defvar gnus-group-mode-hook nil
1339   "*A hook for Gnus group mode.")
1340
1341 (defvar gnus-summary-mode-hook nil
1342   "*A hook for Gnus summary mode.
1343 This hook is run before any variables are set in the summary buffer.")
1344
1345 (defvar gnus-article-mode-hook nil
1346   "*A hook for Gnus article mode.")
1347
1348 (defvar gnus-summary-prepare-exit-hook nil
1349   "*A hook called when preparing to exit from the summary buffer.
1350 It calls `gnus-summary-expire-articles' by default.")
1351 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1352
1353 (defvar gnus-summary-exit-hook nil
1354   "*A hook called on exit from the summary buffer.")
1355
1356 (defvar gnus-group-catchup-group-hook nil
1357   "*A hook run when catching up a group from the group buffer.")
1358
1359 (defvar gnus-open-server-hook nil
1360   "*A hook called just before opening connection to the news server.")
1361
1362 (defvar gnus-load-hook nil
1363   "*A hook run while Gnus is loaded.")
1364
1365 (defvar gnus-startup-hook nil
1366   "*A hook called at startup.
1367 This hook is called after Gnus is connected to the NNTP server.")
1368
1369 (defvar gnus-get-new-news-hook nil
1370   "*A hook run just before Gnus checks for new news.")
1371
1372 (defvar gnus-after-getting-new-news-hook nil
1373   "*A hook run after Gnus checks for new news.")
1374
1375 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1376   "*A function that is called to generate the group buffer.
1377 The function is called with three arguments: The first is a number;
1378 all group with a level less or equal to that number should be listed,
1379 if the second is non-nil, empty groups should also be displayed.  If
1380 the third is non-nil, it is a number.  No groups with a level lower
1381 than this number should be displayed.
1382
1383 The only current function implemented is `gnus-group-prepare-flat'.")
1384
1385 (defvar gnus-group-prepare-hook nil
1386   "*A hook called after the group buffer has been generated.
1387 If you want to modify the group buffer, you can use this hook.")
1388
1389 (defvar gnus-summary-prepare-hook nil
1390   "*A hook called after the summary buffer has been generated.
1391 If you want to modify the summary buffer, you can use this hook.")
1392
1393 (defvar gnus-summary-generate-hook nil
1394   "*A hook run just before generating the summary buffer.
1395 This hook is commonly used to customize threading variables and the
1396 like.")
1397
1398 (defvar gnus-article-prepare-hook nil
1399   "*A hook called after an article has been prepared in the article buffer.
1400 If you want to run a special decoding program like nkf, use this hook.")
1401
1402 ;(defvar gnus-article-display-hook nil
1403 ;  "*A hook called after the article is displayed in the article buffer.
1404 ;The hook is designed to change the contents of the article
1405 ;buffer.  Typical functions that this hook may contain are
1406 ;`gnus-article-hide-headers' (hide selected headers),
1407 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1408 ;`gnus-article-hide-signature' (hide signature) and
1409 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1410 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1411 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1412 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1413
1414 (defvar gnus-article-x-face-command
1415   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1416   "String or function to be executed to display an X-Face header.
1417 If it is a string, the command will be executed in a sub-shell
1418 asynchronously.  The compressed face will be piped to this command.")
1419
1420 (defvar gnus-article-x-face-too-ugly nil
1421   "Regexp matching posters whose face shouldn't be shown automatically.")
1422
1423 (defvar gnus-select-group-hook nil
1424   "*A hook called when a newsgroup is selected.
1425
1426 If you'd like to simplify subjects like the
1427 `gnus-summary-next-same-subject' command does, you can use the
1428 following hook:
1429
1430  (setq gnus-select-group-hook
1431       (list
1432         (lambda ()
1433           (mapcar (lambda (header)
1434                      (mail-header-set-subject
1435                       header
1436                       (gnus-simplify-subject
1437                        (mail-header-subject header) 're-only)))
1438                   gnus-newsgroup-headers))))")
1439
1440 (defvar gnus-select-article-hook nil
1441   "*A hook called when an article is selected.")
1442
1443 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1444   "*A hook called to apply kill files to a group.
1445 This hook is intended to apply a kill file to the selected newsgroup.
1446 The function `gnus-apply-kill-file' is called by default.
1447
1448 Since a general kill file is too heavy to use only for a few
1449 newsgroups, I recommend you to use a lighter hook function.  For
1450 example, if you'd like to apply a kill file to articles which contains
1451 a string `rmgroup' in subject in newsgroup `control', you can use the
1452 following hook:
1453
1454  (setq gnus-apply-kill-hook
1455       (list
1456         (lambda ()
1457           (cond ((string-match \"control\" gnus-newsgroup-name)
1458                  (gnus-kill \"Subject\" \"rmgroup\")
1459                  (gnus-expunge \"X\"))))))")
1460
1461 (defvar gnus-visual-mark-article-hook
1462   (list 'gnus-highlight-selected-summary)
1463   "*Hook run after selecting an article in the summary buffer.
1464 It is meant to be used for highlighting the article in some way.  It
1465 is not run if `gnus-visual' is nil.")
1466
1467 (defvar gnus-parse-headers-hook nil
1468   "*A hook called before parsing the headers.")
1469
1470 (defvar gnus-exit-group-hook nil
1471   "*A hook called when exiting (not quitting) summary mode.")
1472
1473 (defvar gnus-suspend-gnus-hook nil
1474   "*A hook called when suspending (not exiting) Gnus.")
1475
1476 (defvar gnus-exit-gnus-hook nil
1477   "*A hook called when exiting Gnus.")
1478
1479 (defvar gnus-after-exiting-gnus-hook nil
1480   "*A hook called after exiting Gnus.")
1481
1482 (defvar gnus-save-newsrc-hook nil
1483   "*A hook called before saving any of the newsrc files.")
1484
1485 (defvar gnus-save-quick-newsrc-hook nil
1486   "*A hook called just before saving the quick newsrc file.
1487 Can be used to turn version control on or off.")
1488
1489 (defvar gnus-save-standard-newsrc-hook nil
1490   "*A hook called just before saving the standard newsrc file.
1491 Can be used to turn version control on or off.")
1492
1493 (defvar gnus-summary-update-hook
1494   (list 'gnus-summary-highlight-line)
1495   "*A hook called when a summary line is changed.
1496 The hook will not be called if `gnus-visual' is nil.
1497
1498 The default function `gnus-summary-highlight-line' will
1499 highlight the line according to the `gnus-summary-highlight'
1500 variable.")
1501
1502 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1503   "*A hook called when a group line is changed.
1504 The hook will not be called if `gnus-visual' is nil.
1505
1506 The default function `gnus-group-highlight-line' will
1507 highlight the line according to the `gnus-group-highlight'
1508 variable.")
1509
1510 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1511   "*A hook called when an article is selected for the first time.
1512 The hook is intended to mark an article as read (or unread)
1513 automatically when it is selected.")
1514
1515 (defvar gnus-group-change-level-function nil
1516   "Function run when a group level is changed.
1517 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1518
1519 ;; Remove any hilit infestation.
1520 (add-hook 'gnus-startup-hook
1521           (lambda ()
1522             (remove-hook 'gnus-summary-prepare-hook
1523                          'hilit-rehighlight-buffer-quietly)
1524             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1525             (setq gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read))
1526             (remove-hook 'gnus-article-prepare-hook
1527                          'hilit-rehighlight-buffer-quietly)))
1528
1529 \f
1530 ;; Internal variables
1531
1532 (defvar gnus-server-alist nil
1533   "List of available servers.")
1534
1535 (defvar gnus-group-indentation-function nil)
1536
1537 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1538
1539 (defvar gnus-goto-missing-group-function nil)
1540
1541 (defvar gnus-override-subscribe-method nil)
1542
1543 (defvar gnus-group-goto-next-group-function nil
1544   "Function to override finding the next group after listing groups.")
1545
1546 (defconst gnus-article-mark-lists
1547   '((marked . tick) (replied . reply)
1548     (expirable . expire) (killed . killed)
1549     (bookmarks . bookmark) (dormant . dormant)
1550     (scored . score) (saved . save)
1551     (cached . cache)
1552     ))
1553
1554 ;; Avoid highlighting in kill files.
1555 (defvar gnus-summary-inhibit-highlight nil)
1556 (defvar gnus-newsgroup-selected-overlay nil)
1557
1558 (defvar gnus-inhibit-hiding nil)
1559 (defvar gnus-group-indentation "")
1560 (defvar gnus-inhibit-limiting nil)
1561 (defvar gnus-created-frames nil)
1562
1563 (defvar gnus-article-mode-map nil)
1564 (defvar gnus-dribble-buffer nil)
1565 (defvar gnus-headers-retrieved-by nil)
1566 (defvar gnus-article-reply nil)
1567 (defvar gnus-override-method nil)
1568 (defvar gnus-article-check-size nil)
1569
1570 (defvar gnus-current-score-file nil)
1571 (defvar gnus-newsgroup-adaptive-score-file nil)
1572 (defvar gnus-scores-exclude-files nil)
1573
1574 (defvar gnus-opened-servers nil)
1575
1576 (defvar gnus-current-move-group nil)
1577
1578 (defvar gnus-newsgroup-dependencies nil)
1579 (defvar gnus-newsgroup-async nil)
1580 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1581
1582 (defvar gnus-newsgroup-adaptive nil)
1583
1584 (defvar gnus-summary-display-table nil)
1585 (defvar gnus-summary-display-article-function nil)
1586
1587 (defvar gnus-summary-highlight-line-function nil
1588   "Function called after highlighting a summary line.")
1589
1590 (defvar gnus-group-line-format-alist
1591   `((?M gnus-tmp-marked-mark ?c)
1592     (?S gnus-tmp-subscribed ?c)
1593     (?L gnus-tmp-level ?d)
1594     (?N (cond ((eq number t) "*" )
1595               ((numberp number) 
1596                (int-to-string
1597                 (+ number
1598                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1599                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1600               (t number)) ?s)
1601     (?R gnus-tmp-number-of-read ?s)
1602     (?t gnus-tmp-number-total ?d)
1603     (?y gnus-tmp-number-of-unread ?s)
1604     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1605     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1606     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1607            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1608     (?g gnus-tmp-group ?s)
1609     (?G gnus-tmp-qualified-group ?s)
1610     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1611     (?D gnus-tmp-newsgroup-description ?s)
1612     (?o gnus-tmp-moderated ?c)
1613     (?O gnus-tmp-moderated-string ?s)
1614     (?p gnus-tmp-process-marked ?c)
1615     (?s gnus-tmp-news-server ?s)
1616     (?n gnus-tmp-news-method ?s)
1617     (?P gnus-group-indentation ?s)
1618     (?l gnus-tmp-grouplens ?s)
1619     (?z gnus-tmp-news-method-string ?s)
1620     (?u gnus-tmp-user-defined ?s)))
1621
1622 (defvar gnus-summary-line-format-alist
1623   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1624     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1625     (?s gnus-tmp-subject-or-nil ?s)
1626     (?n gnus-tmp-name ?s)
1627     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1628         ?s)
1629     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1630             gnus-tmp-from) ?s)
1631     (?F gnus-tmp-from ?s)
1632     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1633     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1634     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1635     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1636     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1637     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1638     (?L gnus-tmp-lines ?d)
1639     (?I gnus-tmp-indentation ?s)
1640     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1641     (?R gnus-tmp-replied ?c)
1642     (?\[ gnus-tmp-opening-bracket ?c)
1643     (?\] gnus-tmp-closing-bracket ?c)
1644     (?\> (make-string gnus-tmp-level ? ) ?s)
1645     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1646     (?i gnus-tmp-score ?d)
1647     (?z gnus-tmp-score-char ?c)
1648     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1649     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1650     (?U gnus-tmp-unread ?c)
1651     (?t (gnus-summary-number-of-articles-in-thread
1652          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1653         ?d)
1654     (?e (gnus-summary-number-of-articles-in-thread
1655          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1656         ?c)
1657     (?u gnus-tmp-user-defined ?s))
1658   "An alist of format specifications that can appear in summary lines,
1659 and what variables they correspond with, along with the type of the
1660 variable (string, integer, character, etc).")
1661
1662 (defvar gnus-summary-dummy-line-format-alist
1663   `((?S gnus-tmp-subject ?s)
1664     (?N gnus-tmp-number ?d)
1665     (?u gnus-tmp-user-defined ?s)))
1666
1667 (defvar gnus-summary-mode-line-format-alist
1668   `((?G gnus-tmp-group-name ?s)
1669     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1670     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1671     (?A gnus-tmp-article-number ?d)
1672     (?Z gnus-tmp-unread-and-unselected ?s)
1673     (?V gnus-version ?s)
1674     (?U gnus-tmp-unread ?d)
1675     (?S gnus-tmp-subject ?s)
1676     (?e gnus-tmp-unselected ?d)
1677     (?u gnus-tmp-user-defined ?s)
1678     (?d (length gnus-newsgroup-dormant) ?d)
1679     (?t (length gnus-newsgroup-marked) ?d)
1680     (?r (length gnus-newsgroup-reads) ?d)
1681     (?E gnus-newsgroup-expunged-tally ?d)
1682     (?s (gnus-current-score-file-nondirectory) ?s)))
1683
1684 (defvar gnus-article-mode-line-format-alist
1685   gnus-summary-mode-line-format-alist)
1686
1687 (defvar gnus-group-mode-line-format-alist
1688   `((?S gnus-tmp-news-server ?s)
1689     (?M gnus-tmp-news-method ?s)
1690     (?u gnus-tmp-user-defined ?s)))
1691
1692 (defvar gnus-have-read-active-file nil)
1693
1694 (defconst gnus-maintainer
1695   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1696   "The mail address of the Gnus maintainers.")
1697
1698 (defconst gnus-version "September Gnus v0.69"
1699   "Version number for this version of Gnus.")
1700
1701 (defvar gnus-info-nodes
1702   '((gnus-group-mode            "(gnus)The Group Buffer")
1703     (gnus-summary-mode          "(gnus)The Summary Buffer")
1704     (gnus-article-mode          "(gnus)The Article Buffer"))
1705   "Assoc list of major modes and related Info nodes.")
1706
1707 (defvar gnus-group-buffer "*Group*")
1708 (defvar gnus-summary-buffer "*Summary*")
1709 (defvar gnus-article-buffer "*Article*")
1710 (defvar gnus-server-buffer "*Server*")
1711
1712 (defvar gnus-work-buffer " *gnus work*")
1713
1714 (defvar gnus-original-article-buffer " *Original Article*")
1715 (defvar gnus-original-article nil)
1716
1717 (defvar gnus-buffer-list nil
1718   "Gnus buffers that should be killed on exit.")
1719
1720 (defvar gnus-slave nil
1721   "Whether this Gnus is a slave or not.")
1722
1723 (defvar gnus-variable-list
1724   '(gnus-newsrc-options gnus-newsrc-options-n
1725     gnus-newsrc-last-checked-date
1726     gnus-newsrc-alist gnus-server-alist
1727     gnus-killed-list gnus-zombie-list
1728     gnus-topic-topology gnus-topic-alist
1729     gnus-format-specs)
1730   "Gnus variables saved in the quick startup file.")
1731
1732 (defvar gnus-newsrc-options nil
1733   "Options line in the .newsrc file.")
1734
1735 (defvar gnus-newsrc-options-n nil
1736   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1737
1738 (defvar gnus-newsrc-last-checked-date nil
1739   "Date Gnus last asked server for new newsgroups.")
1740
1741 (defvar gnus-topic-topology nil
1742   "The complete topic hierarchy.")
1743
1744 (defvar gnus-topic-alist nil
1745   "The complete topic-group alist.")
1746
1747 (defvar gnus-newsrc-alist nil
1748   "Assoc list of read articles.
1749 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1750
1751 (defvar gnus-newsrc-hashtb nil
1752   "Hashtable of gnus-newsrc-alist.")
1753
1754 (defvar gnus-killed-list nil
1755   "List of killed newsgroups.")
1756
1757 (defvar gnus-killed-hashtb nil
1758   "Hash table equivalent of gnus-killed-list.")
1759
1760 (defvar gnus-zombie-list nil
1761   "List of almost dead newsgroups.")
1762
1763 (defvar gnus-description-hashtb nil
1764   "Descriptions of newsgroups.")
1765
1766 (defvar gnus-list-of-killed-groups nil
1767   "List of newsgroups that have recently been killed by the user.")
1768
1769 (defvar gnus-active-hashtb nil
1770   "Hashtable of active articles.")
1771
1772 (defvar gnus-moderated-list nil
1773   "List of moderated newsgroups.")
1774
1775 (defvar gnus-group-marked nil)
1776
1777 (defvar gnus-current-startup-file nil
1778   "Startup file for the current host.")
1779
1780 (defvar gnus-last-search-regexp nil
1781   "Default regexp for article search command.")
1782
1783 (defvar gnus-last-shell-command nil
1784   "Default shell command on article.")
1785
1786 (defvar gnus-current-select-method nil
1787   "The current method for selecting a newsgroup.")
1788
1789 (defvar gnus-group-list-mode nil)
1790
1791 (defvar gnus-article-internal-prepare-hook nil)
1792
1793 (defvar gnus-newsgroup-name nil)
1794 (defvar gnus-newsgroup-begin nil)
1795 (defvar gnus-newsgroup-end nil)
1796 (defvar gnus-newsgroup-last-rmail nil)
1797 (defvar gnus-newsgroup-last-mail nil)
1798 (defvar gnus-newsgroup-last-folder nil)
1799 (defvar gnus-newsgroup-last-file nil)
1800 (defvar gnus-newsgroup-auto-expire nil)
1801 (defvar gnus-newsgroup-active nil)
1802
1803 (defvar gnus-newsgroup-data nil)
1804 (defvar gnus-newsgroup-data-reverse nil)
1805 (defvar gnus-newsgroup-limit nil)
1806 (defvar gnus-newsgroup-limits nil)
1807
1808 (defvar gnus-newsgroup-unreads nil
1809   "List of unread articles in the current newsgroup.")
1810
1811 (defvar gnus-newsgroup-unselected nil
1812   "List of unselected unread articles in the current newsgroup.")
1813
1814 (defvar gnus-newsgroup-reads nil
1815   "Alist of read articles and article marks in the current newsgroup.")
1816
1817 (defvar gnus-newsgroup-expunged-tally nil)
1818
1819 (defvar gnus-newsgroup-marked nil
1820   "List of ticked articles in the current newsgroup (a subset of unread art).")
1821
1822 (defvar gnus-newsgroup-killed nil
1823   "List of ranges of articles that have been through the scoring process.")
1824
1825 (defvar gnus-newsgroup-cached nil
1826   "List of articles that come from the article cache.")
1827
1828 (defvar gnus-newsgroup-saved nil
1829   "List of articles that have been saved.")
1830
1831 (defvar gnus-newsgroup-kill-headers nil)
1832
1833 (defvar gnus-newsgroup-replied nil
1834   "List of articles that have been replied to in the current newsgroup.")
1835
1836 (defvar gnus-newsgroup-expirable nil
1837   "List of articles in the current newsgroup that can be expired.")
1838
1839 (defvar gnus-newsgroup-processable nil
1840   "List of articles in the current newsgroup that can be processed.")
1841
1842 (defvar gnus-newsgroup-bookmarks nil
1843   "List of articles in the current newsgroup that have bookmarks.")
1844
1845 (defvar gnus-newsgroup-dormant nil
1846   "List of dormant articles in the current newsgroup.")
1847
1848 (defvar gnus-newsgroup-scored nil
1849   "List of scored articles in the current newsgroup.")
1850
1851 (defvar gnus-newsgroup-headers nil
1852   "List of article headers in the current newsgroup.")
1853
1854 (defvar gnus-newsgroup-threads nil)
1855
1856 (defvar gnus-newsgroup-prepared nil
1857   "Whether the current group has been prepared properly.")
1858
1859 (defvar gnus-newsgroup-ancient nil
1860   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1861
1862 (defvar gnus-newsgroup-sparse nil)
1863
1864 (defvar gnus-current-article nil)
1865 (defvar gnus-article-current nil)
1866 (defvar gnus-current-headers nil)
1867 (defvar gnus-have-all-headers nil)
1868 (defvar gnus-last-article nil)
1869 (defvar gnus-newsgroup-history nil)
1870 (defvar gnus-current-kill-article nil)
1871
1872 ;; Save window configuration.
1873 (defvar gnus-prev-winconf nil)
1874
1875 (defvar gnus-summary-mark-positions nil)
1876 (defvar gnus-group-mark-positions nil)
1877
1878 (defvar gnus-reffed-article-number nil)
1879
1880 ;;; Let the byte-compiler know that we know about this variable.
1881 (defvar rmail-default-rmail-file)
1882
1883 (defvar gnus-cache-removable-articles nil)
1884
1885 (defvar gnus-dead-summary nil)
1886
1887 (defconst gnus-summary-local-variables
1888   '(gnus-newsgroup-name
1889     gnus-newsgroup-begin gnus-newsgroup-end
1890     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1891     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1892     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1893     gnus-newsgroup-unselected gnus-newsgroup-marked
1894     gnus-newsgroup-reads gnus-newsgroup-saved
1895     gnus-newsgroup-replied gnus-newsgroup-expirable
1896     gnus-newsgroup-processable gnus-newsgroup-killed
1897     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1898     gnus-newsgroup-headers gnus-newsgroup-threads
1899     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1900     gnus-current-article gnus-current-headers gnus-have-all-headers
1901     gnus-last-article gnus-article-internal-prepare-hook
1902     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1903     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1904     gnus-newsgroup-async 
1905     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1906     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1907     gnus-newsgroup-history gnus-newsgroup-ancient
1908     gnus-newsgroup-sparse
1909     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1910     gnus-newsgroup-adaptive-score-file
1911     (gnus-newsgroup-expunged-tally . 0)
1912     gnus-cache-removable-articles gnus-newsgroup-cached
1913     gnus-newsgroup-data gnus-newsgroup-data-reverse
1914     gnus-newsgroup-limit gnus-newsgroup-limits)
1915   "Variables that are buffer-local to the summary buffers.")
1916
1917 (defconst gnus-bug-message
1918   "Sending a bug report to the Gnus Towers.
1919 ========================================
1920
1921 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1922 be sent to the Gnus Bug Exterminators.
1923
1924 At the bottom of the buffer you'll see lots of variable settings.
1925 Please do not delete those.  They will tell the Bug People what your
1926 environment is, so that it will be easier to locate the bugs.
1927
1928 If you have found a bug that makes Emacs go \"beep\", set
1929 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1930 and include the backtrace in your bug report.
1931
1932 Please describe the bug in annoying, painstaking detail.
1933
1934 Thank you for your help in stamping out bugs.
1935 ")
1936
1937 ;;; End of variables.
1938
1939 ;; Define some autoload functions Gnus might use.
1940 (eval-and-compile
1941
1942   ;; This little mapcar goes through the list below and marks the
1943   ;; symbols in question as autoloaded functions.
1944   (mapcar
1945    (lambda (package)
1946      (let ((interactive (nth 1 (memq ':interactive package))))
1947        (mapcar
1948         (lambda (function)
1949           (let (keymap)
1950             (when (consp function)
1951               (setq keymap (car (memq 'keymap function)))
1952               (setq function (car function)))
1953             (autoload function (car package) nil interactive keymap)))
1954         (if (eq (nth 1 package) ':interactive)
1955             (cdddr package)
1956           (cdr package)))))
1957    '(("metamail" metamail-buffer)
1958      ("info" Info-goto-node)
1959      ("hexl" hexl-hex-string-to-integer)
1960      ("pp" pp pp-to-string pp-eval-expression)
1961      ("mail-extr" mail-extract-address-components)
1962      ("nnmail" nnmail-split-fancy nnmail-article-group)
1963      ("nnvirtual" nnvirtual-catchup-group)
1964      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1965       timezone-make-sortable-date timezone-make-time-string)
1966      ("sendmail" mail-position-on-field mail-setup)
1967      ("rmailout" rmail-output)
1968      ("rnewspost" news-mail-other-window news-reply-yank-original
1969       news-caesar-buffer-body)
1970      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1971       rmail-show-message)
1972      ("gnus-soup" :interactive t
1973       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1974       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1975      ("nnsoup" nnsoup-pack-replies)
1976      ("gnus-scomo" :interactive t gnus-score-mode)
1977      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1978       gnus-Folder-save-name gnus-folder-save-name)
1979      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1980      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
1981       gnus-server-make-menu-bar gnus-article-make-menu-bar
1982       gnus-browse-make-menu-bar gnus-highlight-selected-summary
1983       gnus-summary-highlight-line gnus-carpal-setup-buffer
1984       gnus-group-highlight-line
1985       gnus-article-add-button gnus-insert-next-page-button
1986       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
1987      ("gnus-vis" :interactive t
1988       gnus-article-push-button gnus-article-press-button
1989       gnus-article-highlight gnus-article-highlight-some
1990       gnus-article-highlight-headers gnus-article-highlight-signature
1991       gnus-article-add-buttons gnus-article-add-buttons-to-head
1992       gnus-article-next-button gnus-article-prev-button)
1993      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
1994       gnus-demon-add-disconnection gnus-demon-add-handler
1995       gnus-demon-remove-handler)
1996      ("gnus-demon" :interactive t
1997       gnus-demon-init gnus-demon-cancel)
1998      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
1999       gnus-tree-open gnus-tree-close)
2000      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2001       gnus-nocem-unwanted-article-p)
2002      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2003      ("gnus-srvr" gnus-browse-foreign-server)
2004      ("gnus-cite" :interactive t
2005       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2006       gnus-article-hide-citation gnus-article-fill-cited-article)
2007      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2008       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2009       gnus-execute gnus-expunge)
2010      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2011       gnus-cache-possibly-remove-articles gnus-cache-request-article
2012       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2013       gnus-cache-enter-remove-article gnus-cached-article-p
2014       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2015      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2016       gnus-cache-remove-article)
2017      ("gnus-score" :interactive t
2018       gnus-summary-increase-score gnus-summary-lower-score
2019       gnus-score-flush-cache gnus-score-close
2020       gnus-score-raise-same-subject-and-select
2021       gnus-score-raise-same-subject gnus-score-default
2022       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2023       gnus-score-lower-same-subject gnus-score-lower-thread
2024       gnus-possibly-score-headers)
2025      ("gnus-score"
2026       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2027       gnus-current-score-file-nondirectory gnus-score-adaptive
2028       gnus-score-find-trace gnus-score-file-name)
2029      ("gnus-edit" :interactive t gnus-score-customize)
2030      ("gnus-topic" :interactive t gnus-topic-mode)
2031      ("gnus-topic" gnus-topic-remove-group)
2032      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2033      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2034      ("gnus-uu" :interactive t
2035       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2036       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2037       gnus-uu-mark-by-regexp gnus-uu-mark-all
2038       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2039       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2040       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2041       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2042       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2043       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2044       gnus-uu-decode-binhex-view)
2045      ("gnus-msg" (gnus-summary-send-map keymap)
2046       gnus-mail-yank-original gnus-mail-send-and-exit
2047       gnus-sendmail-setup-mail gnus-article-mail
2048       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2049      ("gnus-msg" :interactive t
2050       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2051       gnus-summary-followup gnus-summary-followup-with-original
2052       gnus-summary-followup-and-reply
2053       gnus-summary-followup-and-reply-with-original
2054       gnus-summary-cancel-article gnus-summary-supersede-article
2055       gnus-post-news gnus-inews-news gnus-cancel-news
2056       gnus-summary-reply gnus-summary-reply-with-original
2057       gnus-summary-mail-forward gnus-summary-mail-other-window
2058       gnus-bug)
2059      ("gnus-picon" :interactive t gnus-article-display-picons
2060       gnus-group-display-picons gnus-picons-article-display-x-face)
2061      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2062       gnus-grouplens-mode)
2063      ("gnus-vm" gnus-vm-mail-setup)
2064      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2065       gnus-summary-save-article-vm gnus-yank-article))))
2066
2067 \f
2068
2069 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2070 ;; If you want the cursor to go somewhere else, set these two
2071 ;; functions in some startup hook to whatever you want.
2072 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2073 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2074
2075 ;;; Various macros and substs.
2076
2077 (defun gnus-header-from (header)
2078   (mail-header-from header))
2079
2080 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2081   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2082   (let ((tempvar (make-symbol "GnusStartBufferWindow")))
2083     `(let ((,tempvar (selected-window)))
2084        (unwind-protect
2085            (progn
2086              (pop-to-buffer ,buffer)
2087              ,@forms)
2088          (select-window ,tempvar)))))
2089
2090 (defmacro gnus-gethash (string hashtable)
2091   "Get hash value of STRING in HASHTABLE."
2092   `(symbol-value (intern-soft ,string ,hashtable)))
2093
2094 (defmacro gnus-sethash (string value hashtable)
2095   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2096   `(set (intern ,string ,hashtable) ,value))
2097
2098 (defmacro gnus-intern-safe (string hashtable)
2099   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2100   `(let ((symbol (intern ,string ,hashtable)))
2101      (or (boundp symbol)
2102          (set symbol nil))
2103      symbol))
2104
2105 (defmacro gnus-group-unread (group)
2106   "Get the currently computed number of unread articles in GROUP."
2107   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2108
2109 (defmacro gnus-group-entry (group)
2110   "Get the newsrc entry for GROUP."
2111   `(gnus-gethash ,group gnus-newsrc-hashtb))
2112
2113 (defmacro gnus-active (group)
2114   "Get active info on GROUP."
2115   `(gnus-gethash ,group gnus-active-hashtb))
2116
2117 (defmacro gnus-set-active (group active)
2118   "Set GROUP's active info."
2119   `(gnus-sethash ,group ,active gnus-active-hashtb))
2120
2121 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2122 ;;   function `substring' might cut on a middle of multi-octet
2123 ;;   character.
2124 (defun gnus-truncate-string (str width)
2125   (substring str 0 width))
2126
2127 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2128 ;; to limit the length of a string.  This function is necessary since
2129 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2130 (defsubst gnus-limit-string (str width)
2131   (if (> (length str) width)
2132       (substring str 0 width)
2133     str))
2134
2135 (defsubst gnus-simplify-subject-re (subject)
2136   "Remove \"Re:\" from subject lines."
2137   (if (string-match "^[Rr][Ee]: *" subject)
2138       (substring subject (match-end 0))
2139     subject))
2140
2141 (defsubst gnus-functionp (form)
2142   "Return non-nil if FORM is funcallable."
2143   (or (and (symbolp form) (fboundp form))
2144       (and (listp form) (eq (car form) 'lambda))))
2145
2146 (defsubst gnus-goto-char (point)
2147   (and point (goto-char point)))
2148
2149 (defmacro gnus-buffer-exists-p (buffer)
2150   `(let ((buffer ,buffer))
2151      (and buffer
2152           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2153                    buffer))))
2154
2155 (defmacro gnus-kill-buffer (buffer)
2156   `(let ((buf ,buffer))
2157      (if (gnus-buffer-exists-p buf)
2158          (kill-buffer buf))))
2159
2160 (defsubst gnus-point-at-bol ()
2161   "Return point at the beginning of the line."
2162   (let ((p (point)))
2163     (beginning-of-line)
2164     (prog1
2165         (point)
2166       (goto-char p))))
2167
2168 (defsubst gnus-point-at-eol ()
2169   "Return point at the end of the line."
2170   (let ((p (point)))
2171     (end-of-line)
2172     (prog1
2173         (point)
2174       (goto-char p))))
2175
2176 (defun gnus-alive-p ()
2177   "Say whether Gnus is running or not."
2178   (and gnus-group-buffer
2179        (get-buffer gnus-group-buffer)))
2180
2181 ;; Delete the current line (and the next N lines.);
2182 (defmacro gnus-delete-line (&optional n)
2183   `(delete-region (progn (beginning-of-line) (point))
2184                   (progn (forward-line ,(or n 1)) (point))))
2185
2186 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2187 (defvar gnus-init-inhibit nil)
2188 (defun gnus-read-init-file (&optional inhibit-next)
2189   (if gnus-init-inhibit
2190       (setq gnus-init-inhibit nil)
2191     (setq gnus-init-inhibit inhibit-next)
2192     (and gnus-init-file
2193          (or (and (file-exists-p gnus-init-file)
2194                   ;; Don't try to load a directory.
2195                   (not (file-directory-p gnus-init-file)))
2196              (file-exists-p (concat gnus-init-file ".el"))
2197              (file-exists-p (concat gnus-init-file ".elc")))
2198          (condition-case var
2199              (load gnus-init-file nil t)
2200            (error
2201             (error "Error in %s: %s" gnus-init-file var))))))
2202
2203 ;; Info access macros.
2204
2205 (defmacro gnus-info-group (info)
2206   `(nth 0 ,info))
2207 (defmacro gnus-info-rank (info)
2208   `(nth 1 ,info))
2209 (defmacro gnus-info-read (info)
2210   `(nth 2 ,info))
2211 (defmacro gnus-info-marks (info)
2212   `(nth 3 ,info))
2213 (defmacro gnus-info-method (info)
2214   `(nth 4 ,info))
2215 (defmacro gnus-info-params (info)
2216   `(nth 5 ,info))
2217
2218 (defmacro gnus-info-level (info)
2219   `(let ((rank (gnus-info-rank ,info)))
2220      (if (consp rank)
2221          (car rank)
2222        rank)))
2223 (defmacro gnus-info-score (info)
2224   `(let ((rank (gnus-info-rank ,info)))
2225      (or (and (consp rank) (cdr rank)) 0)))
2226
2227 (defmacro gnus-info-set-group (info group)
2228   `(setcar ,info ,group))
2229 (defmacro gnus-info-set-rank (info rank)
2230   `(setcar (nthcdr 1 ,info) ,rank))
2231 (defmacro gnus-info-set-read (info read)
2232   `(setcar (nthcdr 2 ,info) ,read))
2233 (defmacro gnus-info-set-marks (info marks)
2234   `(setcar (nthcdr 3 ,info) ,marks))
2235 (defmacro gnus-info-set-method (info method)
2236   `(setcar (nthcdr 4 ,info) ,method))
2237 (defmacro gnus-info-set-params (info params)
2238   `(setcar (nthcdr 5 ,info) ,params))
2239
2240 (defmacro gnus-info-set-level (info level)
2241   `(let ((rank (cdr ,info)))
2242      (if (consp (car rank))
2243          (setcar (car rank) ,level)
2244        (setcar rank ,level))))
2245 (defmacro gnus-info-set-score (info score)
2246   `(let ((rank (cdr ,info)))
2247      (if (consp (car rank))
2248          (setcdr (car rank) ,score)
2249        (setcar rank (cons (car rank) ,score)))))
2250
2251 (defmacro gnus-get-info (group)
2252   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2253
2254 (defun gnus-byte-code (func)
2255   "Return a form that can be `eval'ed based on FUNC."
2256   (let ((fval (symbol-function func)))
2257     (if (byte-code-function-p fval)
2258         (let ((flist (append fval nil)))
2259           (setcar flist 'byte-code)
2260           flist)
2261       (cons 'progn (cddr fval)))))
2262
2263 ;;; Load the compatability functions.
2264
2265 (require 'gnus-cus)
2266 (require 'gnus-ems)
2267
2268 \f
2269 ;;;
2270 ;;; Shutdown
2271 ;;;
2272
2273 (defvar gnus-shutdown-alist nil)
2274
2275 (defun gnus-add-shutdown (function &rest symbols)
2276   "Run FUNCTION whenever one of SYMBOLS is shut down."
2277   (push (cons function symbols) gnus-shutdown-alist))
2278
2279 (defun gnus-shutdown (symbol)
2280   "Shut down everything that waits for SYMBOL."
2281   (let ((alist gnus-shutdown-alist)
2282         entry)
2283     (while (setq entry (pop alist))
2284       (when (memq symbol (cdr entry))
2285         (funcall (car entry))))))
2286
2287 \f
2288
2289 ;; Format specs.  The chunks below are the machine-generated forms
2290 ;; that are to be evaled as the result of the default format strings.
2291 ;; We write them in here to get them byte-compiled.  That way the
2292 ;; default actions will be quite fast, while still retaining the full
2293 ;; flexibility of the user-defined format specs.
2294
2295 ;; First we have lots of dummy defvars to let the compiler know these
2296 ;; are really dynamic variables.
2297
2298 (defvar gnus-tmp-unread)
2299 (defvar gnus-tmp-replied)
2300 (defvar gnus-tmp-score-char)
2301 (defvar gnus-tmp-indentation)
2302 (defvar gnus-tmp-opening-bracket)
2303 (defvar gnus-tmp-lines)
2304 (defvar gnus-tmp-name)
2305 (defvar gnus-tmp-closing-bracket)
2306 (defvar gnus-tmp-subject-or-nil)
2307 (defvar gnus-tmp-subject)
2308 (defvar gnus-tmp-marked)
2309 (defvar gnus-tmp-marked-mark)
2310 (defvar gnus-tmp-subscribed)
2311 (defvar gnus-tmp-process-marked)
2312 (defvar gnus-tmp-number-of-unread)
2313 (defvar gnus-tmp-group-name)
2314 (defvar gnus-tmp-group)
2315 (defvar gnus-tmp-article-number)
2316 (defvar gnus-tmp-unread-and-unselected)
2317 (defvar gnus-tmp-news-method)
2318 (defvar gnus-tmp-news-server)
2319 (defvar gnus-tmp-article-number)
2320 (defvar gnus-mouse-face)
2321 (defvar gnus-mouse-face-prop)
2322
2323 (defun gnus-summary-line-format-spec ()
2324   (insert gnus-tmp-unread gnus-tmp-replied
2325           gnus-tmp-score-char gnus-tmp-indentation)
2326   (put-text-property
2327    (point)
2328    (progn
2329      (insert
2330       gnus-tmp-opening-bracket
2331       (format "%4d: %-20s"
2332               gnus-tmp-lines
2333               (if (> (length gnus-tmp-name) 20)
2334                   (substring gnus-tmp-name 0 20)
2335                 gnus-tmp-name))
2336       gnus-tmp-closing-bracket)
2337      (point))
2338    gnus-mouse-face-prop gnus-mouse-face)
2339   (insert " " gnus-tmp-subject-or-nil "\n"))
2340
2341 (defvar gnus-summary-line-format-spec
2342   (gnus-byte-code 'gnus-summary-line-format-spec))
2343
2344 (defun gnus-summary-dummy-line-format-spec ()
2345   (insert "*  ")
2346   (put-text-property
2347    (point)
2348    (progn
2349      (insert ":                          :")
2350      (point))
2351    gnus-mouse-face-prop gnus-mouse-face)
2352   (insert " " gnus-tmp-subject "\n"))
2353
2354 (defvar gnus-summary-dummy-line-format-spec
2355   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2356
2357 (defun gnus-group-line-format-spec ()
2358   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2359           gnus-tmp-process-marked
2360           gnus-group-indentation
2361           (format "%5s: " gnus-tmp-number-of-unread))
2362   (put-text-property
2363    (point)
2364    (progn
2365      (insert gnus-tmp-group "\n")
2366      (1- (point)))
2367    gnus-mouse-face-prop gnus-mouse-face))
2368 (defvar gnus-group-line-format-spec
2369   (gnus-byte-code 'gnus-group-line-format-spec))
2370
2371 (defvar gnus-format-specs
2372   `((version . ,emacs-version)
2373     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2374     (summary-dummy ,gnus-summary-dummy-line-format
2375                    ,gnus-summary-dummy-line-format-spec)
2376     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2377
2378 (defvar gnus-article-mode-line-format-spec nil)
2379 (defvar gnus-summary-mode-line-format-spec nil)
2380 (defvar gnus-group-mode-line-format-spec nil)
2381
2382 ;;; Phew.  All that gruft is over, fortunately.
2383
2384 \f
2385 ;;;
2386 ;;; Gnus Utility Functions
2387 ;;;
2388
2389 (defun gnus-extract-address-components (from)
2390   (let (name address)
2391     ;; First find the address - the thing with the @ in it.  This may
2392     ;; not be accurate in mail addresses, but does the trick most of
2393     ;; the time in news messages.
2394     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2395         (setq address (substring from (match-beginning 0) (match-end 0))))
2396     ;; Then we check whether the "name <address>" format is used.
2397     (and address
2398          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2399          ;; Linear white space is not required.
2400          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2401          (and (setq name (substring from 0 (match-beginning 0)))
2402               ;; Strip any quotes from the name.
2403               (string-match "\".*\"" name)
2404               (setq name (substring name 1 (1- (match-end 0))))))
2405     ;; If not, then "address (name)" is used.
2406     (or name
2407         (and (string-match "(.+)" from)
2408              (setq name (substring from (1+ (match-beginning 0))
2409                                    (1- (match-end 0)))))
2410         (and (string-match "()" from)
2411              (setq name address))
2412         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2413         ;; XOVER might not support folded From headers.
2414         (and (string-match "(.*" from)
2415              (setq name (substring from (1+ (match-beginning 0))
2416                                    (match-end 0)))))
2417     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2418     (list (or name from) (or address from))))
2419
2420 (defun gnus-fetch-field (field)
2421   "Return the value of the header FIELD of current article."
2422   (save-excursion
2423     (save-restriction
2424       (let ((case-fold-search t)
2425             (inhibit-point-motion-hooks t))
2426         (nnheader-narrow-to-headers)
2427         (mail-fetch-field field)))))
2428
2429 (defun gnus-goto-colon ()
2430   (beginning-of-line)
2431   (search-forward ":" (gnus-point-at-eol) t))
2432
2433 ;;;###autoload
2434 (defun gnus-update-format (var)
2435   "Update the format specification near point."
2436   (interactive
2437    (list
2438     (save-excursion
2439       (eval-defun nil)
2440       ;; Find the end of the current word.
2441       (re-search-forward "[ \t\n]" nil t)
2442       ;; Search backward.
2443       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2444         (match-string 1)))))
2445   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2446                               (match-string 1 var))))
2447          (entry (assq type gnus-format-specs))
2448          value spec)
2449     (when entry
2450       (setq gnus-format-specs (delq entry gnus-format-specs)))
2451     (set
2452      (intern (format "%s-spec" var))
2453      (gnus-parse-format (setq value (symbol-value (intern var)))
2454                         (symbol-value (intern (format "%s-alist" var)))
2455                         (not (string-match "mode" var))))
2456     (setq spec (symbol-value (intern (format "%s-spec" var))))
2457     (push (list type value spec) gnus-format-specs)
2458
2459     (pop-to-buffer "*Gnus Format*")
2460     (erase-buffer)
2461     (lisp-interaction-mode)
2462     (insert (pp-to-string spec))))
2463
2464 (defun gnus-update-format-specifications (&optional force)
2465   "Update all (necessary) format specifications."
2466   ;; Make the indentation array.
2467   (gnus-make-thread-indent-array)
2468
2469   ;; See whether all the stored info needs to be flushed.
2470   (when (or force
2471             (not (equal emacs-version
2472                         (cdr (assq 'version gnus-format-specs)))))
2473     (setq gnus-format-specs nil))
2474
2475   ;; Go through all the formats and see whether they need updating.
2476   (let ((types '(summary summary-dummy group
2477                          summary-mode group-mode article-mode))
2478         new-format entry type val)
2479     (while (setq type (pop types))
2480       ;; Jump to the proper buffer to find out the value of
2481       ;; the variable, if possible.  (It may be buffer-local.)
2482       (save-excursion
2483         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2484               val)
2485           (when (and (boundp buffer)
2486                      (setq val (symbol-value buffer))
2487                      (get-buffer val)
2488                      (buffer-name (get-buffer val)))
2489             (set-buffer (get-buffer val)))
2490           (setq new-format (symbol-value
2491                             (intern (format "gnus-%s-line-format" type))))))
2492       (setq entry (cdr (assq type gnus-format-specs)))
2493       (if (and entry
2494                (equal (car entry) new-format))
2495           ;; Use the old format.
2496           (set (intern (format "gnus-%s-line-format-spec" type))
2497                (cadr entry))
2498         ;; This is a new format.
2499         (setq val
2500               (if (not (stringp new-format))
2501                   ;; This is a function call or something.
2502                   new-format
2503                 ;; This is a "real" format.
2504                 (gnus-parse-format
2505                  new-format
2506                  (symbol-value
2507                   (intern (format "gnus-%s-line-format-alist"
2508                                   (if (eq type 'article-mode)
2509                                       'summary-mode type))))
2510                  (not (string-match "mode$" (symbol-name type))))))
2511         ;; Enter the new format spec into the list.
2512         (if entry
2513             (progn
2514               (setcar (cdr entry) val)
2515               (setcar entry new-format))
2516           (push (list type new-format val) gnus-format-specs))
2517         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2518
2519   (unless (assq 'version gnus-format-specs)
2520     (push (cons 'version emacs-version) gnus-format-specs))
2521
2522   (gnus-update-group-mark-positions)
2523   (gnus-update-summary-mark-positions))
2524
2525 (defun gnus-update-summary-mark-positions ()
2526   "Compute where the summary marks are to go."
2527   (save-excursion
2528     (let ((gnus-replied-mark 129)
2529           (gnus-score-below-mark 130)
2530           (gnus-score-over-mark 130)
2531           (thread nil)
2532           (gnus-visual nil)
2533           pos)
2534       (gnus-set-work-buffer)
2535       (gnus-summary-insert-line
2536        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2537       (goto-char (point-min))
2538       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2539                                          (- (point) 2)))))
2540       (goto-char (point-min))
2541       (push (cons 'replied (and (search-forward "\201" nil t) (- (point) 2)))
2542             pos)
2543       (goto-char (point-min))
2544       (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2545             pos)
2546       (setq gnus-summary-mark-positions pos))))
2547
2548 (defun gnus-update-group-mark-positions ()
2549   (save-excursion
2550     (let ((gnus-process-mark 128)
2551           (gnus-group-marked '("dummy.group")))
2552       (gnus-set-active "dummy.group" '(0 . 0))
2553       (gnus-set-work-buffer)
2554       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2555       (goto-char (point-min))
2556       (setq gnus-group-mark-positions
2557             (list (cons 'process (and (search-forward "\200" nil t)
2558                                       (- (point) 2))))))))
2559
2560 (defvar gnus-mouse-face-0 'highlight)
2561 (defvar gnus-mouse-face-1 'highlight)
2562 (defvar gnus-mouse-face-2 'highlight)
2563 (defvar gnus-mouse-face-3 'highlight)
2564 (defvar gnus-mouse-face-4 'highlight)
2565
2566 (defun gnus-mouse-face-function (form type)
2567   `(put-text-property
2568     (point) (progn ,@form (point))
2569     gnus-mouse-face-prop
2570     ,(if (equal type 0)
2571          'gnus-mouse-face
2572        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2573
2574 (defvar gnus-face-0 'bold)
2575 (defvar gnus-face-1 'italic)
2576 (defvar gnus-face-2 'bold-italic)
2577 (defvar gnus-face-3 'bold)
2578 (defvar gnus-face-4 'bold)
2579
2580 (defun gnus-face-face-function (form type)
2581   `(put-text-property
2582     (point) (progn ,@form (point))
2583     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2584
2585 (defun gnus-max-width-function (el max-width)
2586   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2587   (if (symbolp el)
2588       `(if (> (length ,el) ,max-width)
2589            (substring ,el 0 ,max-width)
2590          ,el)
2591     `(let ((val (eval ,el)))
2592        (if (numberp val)
2593            (setq val (int-to-string val)))
2594        (if (> (length val) ,max-width)
2595            (substring val 0 ,max-width)
2596          val))))
2597
2598 (defun gnus-parse-format (format spec-alist &optional insert)
2599   ;; This function parses the FORMAT string with the help of the
2600   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2601   ;; string.  If the FORMAT string contains the specifiers %( and %)
2602   ;; the text between them will have the mouse-face text property.
2603   (if (string-match
2604        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2605        format)
2606       (gnus-parse-complex-format format spec-alist)
2607     ;; This is a simple format.
2608     (gnus-parse-simple-format format spec-alist insert)))
2609
2610 (defun gnus-parse-complex-format (format spec-alist)
2611   (save-excursion
2612     (gnus-set-work-buffer)
2613     (insert format)
2614     (goto-char (point-min))
2615     (while (re-search-forward "\"" nil t)
2616       (replace-match "\\\"" nil t))
2617     (goto-char (point-min))
2618     (insert "(\"")
2619     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2620       (let ((number (if (match-beginning 1)
2621                         (match-string 1) "0"))
2622             (delim (aref (match-string 2) 0)))
2623         (if (or (= delim ?\() (= delim ?\{))
2624             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2625                                    " " number " \""))
2626           (replace-match "\")\""))))
2627     (goto-char (point-max))
2628     (insert "\")")
2629     (goto-char (point-min))
2630     (let ((form (read (current-buffer))))
2631       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2632
2633 (defun gnus-complex-form-to-spec (form spec-alist)
2634   (delq nil
2635         (mapcar
2636          (lambda (sform)
2637            (if (stringp sform)
2638                (gnus-parse-simple-format sform spec-alist t)
2639              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2640                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2641                       (nth 1 sform))))
2642          form)))
2643
2644 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2645   ;; This function parses the FORMAT string with the help of the
2646   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2647   ;; string.
2648   (let ((max-width 0)
2649         spec flist fstring newspec elem beg result dontinsert)
2650     (save-excursion
2651       (gnus-set-work-buffer)
2652       (insert format)
2653       (goto-char (point-min))
2654       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2655                                 nil t)
2656         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2657               (setq newspec "%"
2658                     beg (1+ (match-beginning 0)))
2659           ;; First check if there are any specs that look anything like
2660           ;; "%12,12A", ie. with a "max width specification".  These have
2661           ;; to be treated specially.
2662           (if (setq beg (match-beginning 1))
2663               (setq max-width
2664                     (string-to-int
2665                      (buffer-substring
2666                       (1+ (match-beginning 1)) (match-end 1))))
2667             (setq max-width 0)
2668             (setq beg (match-beginning 2)))
2669           ;; Find the specification from `spec-alist'.
2670           (unless (setq elem (cdr (assq spec spec-alist)))
2671             (setq elem '("*" ?s)))
2672           ;; Treat user defined format specifiers specially.
2673           (when (eq (car elem) 'gnus-tmp-user-defined)
2674             (setq elem
2675                   (list
2676                    (list (intern (concat "gnus-user-format-function-"
2677                                          (match-string 3)))
2678                          'gnus-tmp-header) ?s))
2679             (delete-region (match-beginning 3) (match-end 3)))
2680           (if (not (zerop max-width))
2681               (let ((el (car elem)))
2682                 (cond ((= (cadr elem) ?c)
2683                        (setq el (list 'char-to-string el)))
2684                       ((= (cadr elem) ?d)
2685                        (setq el (list 'int-to-string el))))
2686                 (setq flist (cons (gnus-max-width-function el max-width)
2687                                   flist))
2688                 (setq newspec ?s))
2689             (progn
2690               (setq flist (cons (car elem) flist))
2691               (setq newspec (cadr elem)))))
2692         ;; Remove the old specification (and possibly a ",12" string).
2693         (delete-region beg (match-end 2))
2694         ;; Insert the new specification.
2695         (goto-char beg)
2696         (insert newspec))
2697       (setq fstring (buffer-substring 1 (point-max))))
2698     ;; Do some postprocessing to increase efficiency.
2699     (setq
2700      result
2701      (cond
2702       ;; Emptyness.
2703       ((string= fstring "")
2704        nil)
2705       ;; Not a format string.
2706       ((not (string-match "%" fstring))
2707        (list fstring))
2708       ;; A format string with just a single string spec.
2709       ((string= fstring "%s")
2710        (list (car flist)))
2711       ;; A single character.
2712       ((string= fstring "%c")
2713        (list (car flist)))
2714       ;; A single number.
2715       ((string= fstring "%d")
2716        (setq dontinsert)
2717        (if insert
2718            (list `(princ ,(car flist)))
2719          (list `(int-to-string ,(car flist)))))
2720       ;; Just lots of chars and strings.
2721       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2722        (nreverse flist))
2723       ;; A single string spec at the beginning of the spec.
2724       ((string-match "\\`%[sc][^%]+\\'" fstring)
2725        (list (car flist) (substring fstring 2)))
2726       ;; A single string spec in the middle of the spec.
2727       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2728        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2729       ;; A single string spec in the end of the spec.
2730       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2731        (list (match-string 1 fstring) (car flist)))
2732       ;; A more complex spec.
2733       (t
2734        (list (cons 'format (cons fstring (nreverse flist)))))))
2735
2736     (if insert
2737         (when result
2738           (if dontinsert
2739               result
2740             (cons 'insert result)))
2741       (cond ((stringp result)
2742              result)
2743             ((consp result)
2744              (cons 'concat result))
2745             (t "")))))
2746
2747 (defun gnus-eval-format (format &optional alist props)
2748   "Eval the format variable FORMAT, using ALIST.
2749 If PROPS, insert the result."
2750   (let ((form (gnus-parse-format format alist props)))
2751     (if props
2752         (add-text-properties (point) (progn (eval form) (point)) props)
2753       (eval form))))
2754
2755 (defun gnus-remove-text-with-property (prop)
2756   "Delete all text in the current buffer with text property PROP."
2757   (save-excursion
2758     (goto-char (point-min))
2759     (while (not (eobp))
2760       (while (get-text-property (point) prop)
2761         (delete-char 1))
2762       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2763
2764 (defun gnus-set-work-buffer ()
2765   (if (get-buffer gnus-work-buffer)
2766       (progn
2767         (set-buffer gnus-work-buffer)
2768         (erase-buffer))
2769     (set-buffer (get-buffer-create gnus-work-buffer))
2770     (kill-all-local-variables)
2771     (buffer-disable-undo (current-buffer))
2772     (gnus-add-current-to-buffer-list)))
2773
2774 ;; Article file names when saving.
2775
2776 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2777   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2778 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2779 Otherwise, it is like ~/News/news/group/num."
2780   (let ((default
2781           (expand-file-name
2782            (concat (if (gnus-use-long-file-name 'not-save)
2783                        (gnus-capitalize-newsgroup newsgroup)
2784                      (gnus-newsgroup-directory-form newsgroup))
2785                    "/" (int-to-string (mail-header-number headers)))
2786            (or gnus-article-save-directory "~/News"))))
2787     (if (and last-file
2788              (string-equal (file-name-directory default)
2789                            (file-name-directory last-file))
2790              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2791         default
2792       (or last-file default))))
2793
2794 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2795   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2796 If variable `gnus-use-long-file-name' is non-nil, it is
2797 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2798   (let ((default
2799           (expand-file-name
2800            (concat (if (gnus-use-long-file-name 'not-save)
2801                        newsgroup
2802                      (gnus-newsgroup-directory-form newsgroup))
2803                    "/" (int-to-string (mail-header-number headers)))
2804            (or gnus-article-save-directory "~/News"))))
2805     (if (and last-file
2806              (string-equal (file-name-directory default)
2807                            (file-name-directory last-file))
2808              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2809         default
2810       (or last-file default))))
2811
2812 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2813   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2814 If variable `gnus-use-long-file-name' is non-nil, it is
2815 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2816   (or last-file
2817       (expand-file-name
2818        (if (gnus-use-long-file-name 'not-save)
2819            (gnus-capitalize-newsgroup newsgroup)
2820          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2821        (or gnus-article-save-directory "~/News"))))
2822
2823 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2824   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2825 If variable `gnus-use-long-file-name' is non-nil, it is
2826 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2827   (or last-file
2828       (expand-file-name
2829        (if (gnus-use-long-file-name 'not-save)
2830            newsgroup
2831          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2832        (or gnus-article-save-directory "~/News"))))
2833
2834 ;; For subscribing new newsgroup
2835
2836 (defun gnus-subscribe-hierarchical-interactive (groups)
2837   (let ((groups (sort groups 'string<))
2838         prefixes prefix start ans group starts)
2839     (while groups
2840       (setq prefixes (list "^"))
2841       (while (and groups prefixes)
2842         (while (not (string-match (car prefixes) (car groups)))
2843           (setq prefixes (cdr prefixes)))
2844         (setq prefix (car prefixes))
2845         (setq start (1- (length prefix)))
2846         (if (and (string-match "[^\\.]\\." (car groups) start)
2847                  (cdr groups)
2848                  (setq prefix
2849                        (concat "^" (substring (car groups) 0 (match-end 0))))
2850                  (string-match prefix (cadr groups)))
2851             (progn
2852               (setq prefixes (cons prefix prefixes))
2853               (message "Descend hierarchy %s? ([y]nsq): "
2854                        (substring prefix 1 (1- (length prefix))))
2855               (setq ans (read-char))
2856               (cond ((= ans ?n)
2857                      (while (and groups
2858                                  (string-match prefix
2859                                                (setq group (car groups))))
2860                        (setq gnus-killed-list
2861                              (cons group gnus-killed-list))
2862                        (gnus-sethash group group gnus-killed-hashtb)
2863                        (setq groups (cdr groups)))
2864                      (setq starts (cdr starts)))
2865                     ((= ans ?s)
2866                      (while (and groups
2867                                  (string-match prefix
2868                                                (setq group (car groups))))
2869                        (gnus-sethash group group gnus-killed-hashtb)
2870                        (gnus-subscribe-alphabetically (car groups))
2871                        (setq groups (cdr groups)))
2872                      (setq starts (cdr starts)))
2873                     ((= ans ?q)
2874                      (while groups
2875                        (setq group (car groups))
2876                        (setq gnus-killed-list (cons group gnus-killed-list))
2877                        (gnus-sethash group group gnus-killed-hashtb)
2878                        (setq groups (cdr groups))))
2879                     (t nil)))
2880           (message "Subscribe %s? ([n]yq)" (car groups))
2881           (setq ans (read-char))
2882           (setq group (car groups))
2883           (cond ((= ans ?y)
2884                  (gnus-subscribe-alphabetically (car groups))
2885                  (gnus-sethash group group gnus-killed-hashtb))
2886                 ((= ans ?q)
2887                  (while groups
2888                    (setq group (car groups))
2889                    (setq gnus-killed-list (cons group gnus-killed-list))
2890                    (gnus-sethash group group gnus-killed-hashtb)
2891                    (setq groups (cdr groups))))
2892                 (t
2893                  (setq gnus-killed-list (cons group gnus-killed-list))
2894                  (gnus-sethash group group gnus-killed-hashtb)))
2895           (setq groups (cdr groups)))))))
2896
2897 (defun gnus-subscribe-randomly (newsgroup)
2898   "Subscribe new NEWSGROUP by making it the first newsgroup."
2899   (gnus-subscribe-newsgroup newsgroup))
2900
2901 (defun gnus-subscribe-alphabetically (newgroup)
2902   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2903   (let ((groups (cdr gnus-newsrc-alist))
2904         before)
2905     (while (and (not before) groups)
2906       (if (string< newgroup (caar groups))
2907           (setq before (caar groups))
2908         (setq groups (cdr groups))))
2909     (gnus-subscribe-newsgroup newgroup before)))
2910
2911 (defun gnus-subscribe-hierarchically (newgroup)
2912   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2913   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2914   (save-excursion
2915     (set-buffer (find-file-noselect gnus-current-startup-file))
2916     (let ((groupkey newgroup)
2917           before)
2918       (while (and (not before) groupkey)
2919         (goto-char (point-min))
2920         (let ((groupkey-re
2921                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2922           (while (and (re-search-forward groupkey-re nil t)
2923                       (progn
2924                         (setq before (match-string 1))
2925                         (string< before newgroup)))))
2926         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2927         (setq groupkey
2928               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2929                   (substring groupkey (match-beginning 1) (match-end 1)))))
2930       (gnus-subscribe-newsgroup newgroup before))))
2931
2932 (defun gnus-subscribe-interactively (group)
2933   "Subscribe the new GROUP interactively.
2934 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2935 it is killed."
2936   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2937       (gnus-subscribe-hierarchically group)
2938     (push group gnus-killed-list)))
2939
2940 (defun gnus-subscribe-zombies (group)
2941   "Make the new GROUP into a zombie group."
2942   (push group gnus-zombie-list))
2943
2944 (defun gnus-subscribe-killed (group)
2945   "Make the new GROUP a killed group."
2946   (push group gnus-killed-list))
2947
2948 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2949   "Subscribe new NEWSGROUP.
2950 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2951 the first newsgroup."
2952   ;; We subscribe the group by changing its level to `subscribed'.
2953   (gnus-group-change-level
2954    newsgroup gnus-level-default-subscribed
2955    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2956   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2957
2958 ;; For directories
2959
2960 (defun gnus-newsgroup-directory-form (newsgroup)
2961   "Make hierarchical directory name from NEWSGROUP name."
2962   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2963         (len (length newsgroup))
2964         idx)
2965     ;; If this is a foreign group, we don't want to translate the
2966     ;; entire name.
2967     (if (setq idx (string-match ":" newsgroup))
2968         (aset newsgroup idx ?/)
2969       (setq idx 0))
2970     ;; Replace all occurrences of `.' with `/'.
2971     (while (< idx len)
2972       (if (= (aref newsgroup idx) ?.)
2973           (aset newsgroup idx ?/))
2974       (setq idx (1+ idx)))
2975     newsgroup))
2976
2977 (defun gnus-newsgroup-savable-name (group)
2978   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2979   ;; with dots.
2980   (nnheader-replace-chars-in-string group ?/ ?.))
2981
2982 (defun gnus-make-directory (dir)
2983   "Make DIRECTORY recursively."
2984   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
2985   ;; of the many mysteries of the universe.
2986   (let* ((dir (expand-file-name dir default-directory))
2987          dirs err)
2988     (if (string-match "/$" dir)
2989         (setq dir (substring dir 0 (match-beginning 0))))
2990     ;; First go down the path until we find a directory that exists.
2991     (while (not (file-exists-p dir))
2992       (setq dirs (cons dir dirs))
2993       (string-match "/[^/]+$" dir)
2994       (setq dir (substring dir 0 (match-beginning 0))))
2995     ;; Then create all the subdirs.
2996     (while (and dirs (not err))
2997       (condition-case ()
2998           (make-directory (car dirs))
2999         (error (setq err t)))
3000       (setq dirs (cdr dirs)))
3001     ;; We return whether we were successful or not.
3002     (not dirs)))
3003
3004 (defun gnus-capitalize-newsgroup (newsgroup)
3005   "Capitalize NEWSGROUP name."
3006   (and (not (zerop (length newsgroup)))
3007        (concat (char-to-string (upcase (aref newsgroup 0)))
3008                (substring newsgroup 1))))
3009
3010 ;; Various... things.
3011
3012 (defun gnus-simplify-subject (subject &optional re-only)
3013   "Remove `Re:' and words in parentheses.
3014 If RE-ONLY is non-nil, strip leading `Re:'s only."
3015   (let ((case-fold-search t))           ;Ignore case.
3016     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3017     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3018       (setq subject (substring subject (match-end 0))))
3019     ;; Remove uninteresting prefixes.
3020     (if (and (not re-only)
3021              gnus-simplify-ignored-prefixes
3022              (string-match gnus-simplify-ignored-prefixes subject))
3023         (setq subject (substring subject (match-end 0))))
3024     ;; Remove words in parentheses from end.
3025     (unless re-only
3026       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3027         (setq subject (substring subject 0 (match-beginning 0)))))
3028     ;; Return subject string.
3029     subject))
3030
3031 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3032 ;; all whitespace.
3033 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3034 (defun gnus-simplify-buffer-fuzzy ()
3035   (goto-char (point-min))
3036   (while (search-forward "\t" nil t)
3037     (replace-match " " t t))
3038   (goto-char (point-min))
3039   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3040   (goto-char (match-beginning 0))
3041   (while (or
3042           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3043           (looking-at "^[[].*: .*[]]$"))
3044     (goto-char (point-min))
3045     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3046                               nil t)
3047       (replace-match "" t t))
3048     (goto-char (point-min))
3049     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3050       (goto-char (match-end 0))
3051       (delete-char -1)
3052       (delete-region
3053        (progn (goto-char (match-beginning 0)))
3054        (re-search-forward ":"))))
3055   (goto-char (point-min))
3056   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3057     (replace-match "" t t))
3058   (goto-char (point-min))
3059   (while (re-search-forward "  +" nil t)
3060     (replace-match " " t t))
3061   (goto-char (point-min))
3062   (while (re-search-forward " $" nil t)
3063     (replace-match "" t t))
3064   (goto-char (point-min))
3065   (while (re-search-forward "^ +" nil t)
3066     (replace-match "" t t))
3067   (goto-char (point-min))
3068   (when gnus-simplify-subject-fuzzy-regexp
3069     (if (listp gnus-simplify-subject-fuzzy-regexp)
3070         (let ((list gnus-simplify-subject-fuzzy-regexp))
3071           (while list
3072             (goto-char (point-min))
3073             (while (re-search-forward (car list) nil t)
3074               (replace-match "" t t))
3075             (setq list (cdr list))))
3076       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3077         (replace-match "" t t)))))
3078
3079 (defun gnus-simplify-subject-fuzzy (subject)
3080   "Siplify a subject string fuzzily."
3081   (save-excursion
3082     (gnus-set-work-buffer)
3083     (let ((case-fold-search t))
3084       (insert subject)
3085       (inline (gnus-simplify-buffer-fuzzy))
3086       (buffer-string))))
3087
3088 ;; Add the current buffer to the list of buffers to be killed on exit.
3089 (defun gnus-add-current-to-buffer-list ()
3090   (or (memq (current-buffer) gnus-buffer-list)
3091       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3092
3093 (defun gnus-string> (s1 s2)
3094   (not (or (string< s1 s2)
3095            (string= s1 s2))))
3096
3097 ;;; General various misc type functions.
3098
3099 (defun gnus-clear-system ()
3100   "Clear all variables and buffers."
3101   ;; Clear Gnus variables.
3102   (let ((variables gnus-variable-list))
3103     (while variables
3104       (set (car variables) nil)
3105       (setq variables (cdr variables))))
3106   ;; Clear other internal variables.
3107   (setq gnus-list-of-killed-groups nil
3108         gnus-have-read-active-file nil
3109         gnus-newsrc-alist nil
3110         gnus-newsrc-hashtb nil
3111         gnus-killed-list nil
3112         gnus-zombie-list nil
3113         gnus-killed-hashtb nil
3114         gnus-active-hashtb nil
3115         gnus-moderated-list nil
3116         gnus-description-hashtb nil
3117         gnus-current-headers nil
3118         gnus-thread-indent-array nil
3119         gnus-newsgroup-headers nil
3120         gnus-newsgroup-name nil
3121         gnus-server-alist nil
3122         gnus-group-list-mode nil
3123         gnus-opened-servers nil
3124         gnus-current-select-method nil)
3125   (gnus-shutdown 'gnus)
3126   ;; Kill the startup file.
3127   (and gnus-current-startup-file
3128        (get-file-buffer gnus-current-startup-file)
3129        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3130   ;; Clear the dribble buffer.
3131   (gnus-dribble-clear)
3132   ;; Kill global KILL file buffer.
3133   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3134     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3135   (gnus-kill-buffer nntp-server-buffer)
3136   ;; Kill Gnus buffers.
3137   (while gnus-buffer-list
3138     (gnus-kill-buffer (pop gnus-buffer-list)))
3139   ;; Remove Gnus frames.
3140   (while gnus-created-frames
3141     (when (frame-live-p (car gnus-created-frames))
3142       ;; We slap a condition-case around this `delete-frame' to ensure 
3143       ;; agains errors if we try do delete the single frame that's left.
3144       (condition-case ()
3145           (delete-frame (car gnus-created-frames))
3146         (error nil)))
3147     (pop gnus-created-frames)))
3148
3149 (defun gnus-windows-old-to-new (setting)
3150   ;; First we take care of the really, really old Gnus 3 actions.
3151   (when (symbolp setting)
3152     (setq setting
3153           ;; Take care of ooold GNUS 3.x values.
3154           (cond ((eq setting 'SelectArticle) 'article)
3155                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3156                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3157                 (t setting))))
3158   (if (or (listp setting)
3159           (not (and gnus-window-configuration
3160                     (memq setting '(group summary article)))))
3161       setting
3162     (let* ((setting (if (eq setting 'group)
3163                         (if (assq 'newsgroup gnus-window-configuration)
3164                             'newsgroup
3165                           'newsgroups) setting))
3166            (elem (cadr (assq setting gnus-window-configuration)))
3167            (total (apply '+ elem))
3168            (types '(group summary article))
3169            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3170            (i 0)
3171            perc
3172            out)
3173       (while (< i 3)
3174         (or (not (numberp (nth i elem)))
3175             (zerop (nth i elem))
3176             (progn
3177               (setq perc  (/ (float (nth 0 elem)) total))
3178               (setq out (cons (if (eq pbuf (nth i types))
3179                                   (vector (nth i types) perc 'point)
3180                                 (vector (nth i types) perc))
3181                               out))))
3182         (setq i (1+ i)))
3183       (list (nreverse out)))))
3184
3185 ;;;###autoload
3186 (defun gnus-add-configuration (conf)
3187   "Add the window configuration CONF to `gnus-buffer-configuration'."
3188   (setq gnus-buffer-configuration
3189         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3190                          gnus-buffer-configuration))))
3191
3192 (defvar gnus-frame-list nil)
3193
3194 (defun gnus-configure-frame (split &optional window)
3195   "Split WINDOW according to SPLIT."
3196   (unless window
3197     (setq window (get-buffer-window (current-buffer))))
3198   (select-window window)
3199   ;; This might be an old-stylee buffer config.
3200   (when (vectorp split)
3201     (setq split (append split nil)))
3202   (when (or (consp (car split))
3203             (vectorp (car split)))
3204     (push 1.0 split)
3205     (push 'vertical split))
3206   ;; The SPLIT might be something that is to be evaled to
3207   ;; return a new SPLIT.
3208   (while (and (not (assq (car split) gnus-window-to-buffer))
3209               (gnus-functionp (car split)))
3210     (setq split (eval split)))
3211   (let* ((type (car split))
3212          (subs (cddr split))
3213          (len (if (eq type 'horizontal) (window-width) (window-height)))
3214          (total 0)
3215          (window-min-width (or gnus-window-min-width window-min-width))
3216          (window-min-height (or gnus-window-min-height window-min-height))
3217          s result new-win rest comp-subs size sub)
3218     (cond
3219      ;; Nothing to do here.
3220      ((null split))
3221      ;; Don't switch buffers.
3222      ((null type)
3223       (and (memq 'point split) window))
3224      ;; This is a buffer to be selected.
3225      ((not (memq type '(frame horizontal vertical)))
3226       (let ((buffer (cond ((stringp type) type)
3227                           (t (cdr (assq type gnus-window-to-buffer)))))
3228             buf)
3229         (unless buffer
3230           (error "Illegal buffer type: %s" type))
3231         (unless (setq buf (get-buffer (if (symbolp buffer)
3232                                           (symbol-value buffer) buffer)))
3233           (setq buf (get-buffer-create (if (symbolp buffer)
3234                                            (symbol-value buffer) buffer))))
3235         (switch-to-buffer buf)
3236         ;; We return the window if it has the `point' spec.
3237         (and (memq 'point split) window)))
3238      ;; This is a frame split.
3239      ((eq type 'frame)
3240       (unless gnus-frame-list
3241         (setq gnus-frame-list (list (window-frame
3242                                      (get-buffer-window (current-buffer))))))
3243       (let ((i 0)
3244             params frame fresult)
3245         (while (< i (length subs))
3246           ;; Frame parameter is gotten from the sub-split.
3247           (setq params (cadr (elt subs i)))
3248           ;; It should be a list.
3249           (unless (listp params)
3250             (setq params nil))
3251           ;; Create a new frame?
3252           (unless (setq frame (elt gnus-frame-list i))
3253             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3254             (push frame gnus-created-frames))
3255           ;; Is the old frame still alive?
3256           (unless (frame-live-p frame)
3257             (setcar (nthcdr i gnus-frame-list)
3258                     (setq frame (make-frame params))))
3259           ;; Select the frame in question and do more splits there.
3260           (select-frame frame)
3261           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3262           (incf i))
3263         ;; Select the frame that has the selected buffer.
3264         (when fresult
3265           (select-frame (window-frame fresult)))))
3266      ;; This is a normal split.
3267      (t
3268       (when (> (length subs) 0)
3269         ;; First we have to compute the sizes of all new windows.
3270         (while subs
3271           (setq sub (append (pop subs) nil))
3272           (while (and (not (assq (car sub) gnus-window-to-buffer))
3273                       (gnus-functionp (car sub)))
3274             (setq sub (eval sub)))
3275           (when sub
3276             (push sub comp-subs)
3277             (setq size (cadar comp-subs))
3278             (cond ((equal size 1.0)
3279                    (setq rest (car comp-subs))
3280                    (setq s 0))
3281                   ((floatp size)
3282                    (setq s (floor (* size len))))
3283                   ((integerp size)
3284                    (setq s size))
3285                   (t
3286                    (error "Illegal size: %s" size)))
3287             ;; Try to make sure that we are inside the safe limits.
3288             (cond ((zerop s))
3289                   ((eq type 'horizontal)
3290                    (setq s (max s window-min-width)))
3291                   ((eq type 'vertical)
3292                    (setq s (max s window-min-height))))
3293             (setcar (cdar comp-subs) s)
3294             (incf total s)))
3295         ;; Take care of the "1.0" spec.
3296         (if rest
3297             (setcar (cdr rest) (- len total))
3298           (error "No 1.0 specs in %s" split))
3299         ;; The we do the actual splitting in a nice recursive
3300         ;; fashion.
3301         (setq comp-subs (nreverse comp-subs))
3302         (while comp-subs
3303           (if (null (cdr comp-subs))
3304               (setq new-win window)
3305             (setq new-win
3306                   (split-window window (cadar comp-subs)
3307                                 (eq type 'horizontal))))
3308           (setq result (or (gnus-configure-frame
3309                             (car comp-subs) window) result))
3310           (select-window new-win)
3311           (setq window new-win)
3312           (setq comp-subs (cdr comp-subs))))
3313       ;; Return the proper window, if any.
3314       (when result
3315         (select-window result))))))
3316
3317 (defvar gnus-frame-split-p nil)
3318
3319 (defun gnus-configure-windows (setting &optional force)
3320   (setq setting (gnus-windows-old-to-new setting))
3321   (let ((split (if (symbolp setting)
3322                    (cadr (assq setting gnus-buffer-configuration))
3323                  setting))
3324         all-visible)
3325
3326     (setq gnus-frame-split-p nil)
3327
3328     (unless split
3329       (error "No such setting: %s" setting))
3330
3331     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3332              (not force))
3333         ;; All the windows mentioned are already visible, so we just
3334         ;; put point in the assigned buffer, and do not touch the
3335         ;; winconf.
3336         (select-window all-visible)
3337
3338       ;; Either remove all windows or just remove all Gnus windows.
3339       (let ((frame (selected-frame)))
3340         (unwind-protect
3341             (if gnus-use-full-window
3342                 ;; We want to remove all other windows.
3343                 (if (not gnus-frame-split-p)
3344                     ;; This is not a `frame' split, so we ignore the
3345                     ;; other frames.  
3346                     (delete-other-windows)
3347                   ;; This is a `frame' split, so we delete all windows
3348                   ;; on all frames.
3349                   (mapcar 
3350                    (lambda (frame)
3351                      (unless (eq (cdr (assq 'minibuffer
3352                                             (frame-parameters frame)))
3353                                  'only)
3354                        (select-frame frame)
3355                        (delete-other-windows)))
3356                    (frame-list)))
3357               ;; Just remove some windows.
3358               (gnus-remove-some-windows)
3359               (switch-to-buffer nntp-server-buffer))
3360           (select-frame frame)))
3361
3362       (switch-to-buffer nntp-server-buffer)
3363       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3364
3365 (defun gnus-all-windows-visible-p (split)
3366   "Say whether all buffers in SPLIT are currently visible.
3367 In particular, the value returned will be the window that
3368 should have point."
3369   (let ((stack (list split))
3370         (all-visible t)
3371         type buffer win buf)
3372     (while (and (setq split (pop stack))
3373                 all-visible)
3374       ;; Be backwards compatible.
3375       (when (vectorp split)
3376         (setq split (append split nil)))
3377       (when (or (consp (car split))
3378                 (vectorp (car split)))
3379         (push 1.0 split)
3380         (push 'vertical split))
3381       ;; The SPLIT might be something that is to be evaled to
3382       ;; return a new SPLIT.
3383       (while (and (not (assq (car split) gnus-window-to-buffer))
3384                   (gnus-functionp (car split)))
3385         (setq split (eval split)))
3386
3387       (setq type (elt split 0))
3388       (cond
3389        ;; Nothing here.
3390        ((null split) t)
3391        ;; A buffer.
3392        ((not (memq type '(horizontal vertical frame)))
3393         (setq buffer (cond ((stringp type) type)
3394                            (t (cdr (assq type gnus-window-to-buffer)))))
3395         (unless buffer
3396           (error "Illegal buffer type: %s" type))
3397         (when (setq buf (get-buffer (if (symbolp buffer)
3398                                         (symbol-value buffer)
3399                                       buffer)))
3400           (setq win (get-buffer-window buf t)))
3401         (if win
3402             (when (memq 'point split)
3403                 (setq all-visible win))
3404           (setq all-visible nil)))
3405        (t
3406         (when (eq type 'frame)
3407           (setq gnus-frame-split-p t))
3408         (setq stack (append (cddr split) stack)))))
3409     (unless (eq all-visible t)
3410       all-visible)))
3411
3412 (defun gnus-window-top-edge (&optional window)
3413   (nth 1 (window-edges window)))
3414
3415 (defun gnus-remove-some-windows ()
3416   (let ((buffers gnus-window-to-buffer)
3417         buf bufs lowest-buf lowest)
3418     (save-excursion
3419       ;; Remove windows on all known Gnus buffers.
3420       (while buffers
3421         (setq buf (cdar buffers))
3422         (if (symbolp buf)
3423             (setq buf (and (boundp buf) (symbol-value buf))))
3424         (and buf
3425              (get-buffer-window buf)
3426              (progn
3427                (setq bufs (cons buf bufs))
3428                (pop-to-buffer buf)
3429                (if (or (not lowest)
3430                        (< (gnus-window-top-edge) lowest))
3431                    (progn
3432                      (setq lowest (gnus-window-top-edge))
3433                      (setq lowest-buf buf)))))
3434         (setq buffers (cdr buffers)))
3435       ;; Remove windows on *all* summary buffers.
3436       (walk-windows
3437        (lambda (win)
3438          (let ((buf (window-buffer win)))
3439            (if (string-match    "^\\*Summary" (buffer-name buf))
3440                (progn
3441                  (setq bufs (cons buf bufs))
3442                  (pop-to-buffer buf)
3443                  (if (or (not lowest)
3444                          (< (gnus-window-top-edge) lowest))
3445                      (progn
3446                        (setq lowest-buf buf)
3447                        (setq lowest (gnus-window-top-edge)))))))))
3448       (and lowest-buf
3449            (progn
3450              (pop-to-buffer lowest-buf)
3451              (switch-to-buffer nntp-server-buffer)))
3452       (while bufs
3453         (and (not (eq (car bufs) lowest-buf))
3454              (delete-windows-on (car bufs)))
3455         (setq bufs (cdr bufs))))))
3456
3457 (defun gnus-version ()
3458   "Version numbers of this version of Gnus."
3459   (interactive)
3460   (let ((methods gnus-valid-select-methods)
3461         (mess gnus-version)
3462         meth)
3463     ;; Go through all the legal select methods and add their version
3464     ;; numbers to the total version string.  Only the backends that are
3465     ;; currently in use will have their message numbers taken into
3466     ;; consideration.
3467     (while methods
3468       (setq meth (intern (concat (caar methods) "-version")))
3469       (and (boundp meth)
3470            (stringp (symbol-value meth))
3471            (setq mess (concat mess "; " (symbol-value meth))))
3472       (setq methods (cdr methods)))
3473     (gnus-message 2 mess)))
3474
3475 (defun gnus-info-find-node ()
3476   "Find Info documentation of Gnus."
3477   (interactive)
3478   ;; Enlarge info window if needed.
3479   (let ((mode major-mode)
3480         gnus-info-buffer)
3481     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3482     (setq gnus-info-buffer (current-buffer))
3483     (gnus-configure-windows 'info)))
3484
3485 (defun gnus-days-between (date1 date2)
3486   ;; Return the number of days between date1 and date2.
3487   (- (gnus-day-number date1) (gnus-day-number date2)))
3488
3489 (defun gnus-day-number (date)
3490   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3491                      (timezone-parse-date date))))
3492     (timezone-absolute-from-gregorian
3493      (nth 1 dat) (nth 2 dat) (car dat))))
3494
3495 (defun gnus-encode-date (date)
3496   "Convert DATE to internal time."
3497   (let* ((parse (timezone-parse-date date))
3498          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3499          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3500     (encode-time (caddr time) (cadr time) (car time)
3501                  (caddr date) (cadr date) (car date) (nth 4 date))))
3502
3503 (defun gnus-time-minus (t1 t2)
3504   "Subtract two internal times."
3505   (let ((borrow (< (cadr t1) (cadr t2))))
3506     (list (- (car t1) (car t2) (if borrow 1 0))
3507           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3508
3509 (defun gnus-file-newer-than (file date)
3510   (let ((fdate (nth 5 (file-attributes file))))
3511     (or (> (car fdate) (car date))
3512         (and (= (car fdate) (car date))
3513              (> (nth 1 fdate) (nth 1 date))))))
3514
3515 (defmacro gnus-local-set-keys (&rest plist)
3516   "Set the keys in PLIST in the current keymap."
3517   `(gnus-define-keys-1 (current-local-map) ',plist))
3518
3519 (defmacro gnus-define-keys (keymap &rest plist)
3520   "Define all keys in PLIST in KEYMAP."
3521   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3522
3523 (put 'gnus-define-keys 'lisp-indent-function 1)
3524 (put 'gnus-define-keys 'lisp-indent-hook 1)
3525 (put 'gnus-define-keymap 'lisp-indent-function 1)
3526 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3527
3528 (defmacro gnus-define-keymap (keymap &rest plist)
3529   "Define all keys in PLIST in KEYMAP."
3530   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3531
3532 (defun gnus-define-keys-1 (keymap plist)
3533   (when (null keymap)
3534     (error "Can't set keys in a null keymap"))
3535   (cond ((symbolp keymap)
3536          (setq keymap (symbol-value keymap)))
3537         ((keymapp keymap))
3538         ((listp keymap)
3539          (set (car keymap) nil)
3540          (define-prefix-command (car keymap))
3541          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3542          (setq keymap (symbol-value (car keymap)))))
3543   (let (key)
3544     (while plist
3545       (when (symbolp (setq key (pop plist)))
3546         (setq key (symbol-value key)))
3547       (define-key keymap key (pop plist)))))
3548
3549 (defun gnus-group-read-only-p (&optional group)
3550   "Check whether GROUP supports editing or not.
3551 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3552 that that variable is buffer-local to the summary buffers."
3553   (let ((group (or group gnus-newsgroup-name)))
3554     (not (gnus-check-backend-function 'request-replace-article group))))
3555
3556 (defun gnus-group-total-expirable-p (group)
3557   "Check whether GROUP is total-expirable or not."
3558   (let ((params (gnus-info-params (gnus-get-info group))))
3559     (or (memq 'total-expire params)
3560         (cdr (assq 'total-expire params)) ; (total-expire . t)
3561         (and gnus-total-expirable-newsgroups ; Check var.
3562              (string-match gnus-total-expirable-newsgroups group)))))
3563
3564 (defun gnus-group-auto-expirable-p (group)
3565   "Check whether GROUP is total-expirable or not."
3566   (let ((params (gnus-info-params (gnus-get-info group))))
3567     (or (memq 'auto-expire params)
3568         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3569         (and gnus-auto-expirable-newsgroups ; Check var.
3570              (string-match gnus-auto-expirable-newsgroups group)))))
3571
3572 (defun gnus-virtual-group-p (group)
3573   "Say whether GROUP is virtual or not."
3574   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3575                         gnus-valid-select-methods)))
3576
3577 (defun gnus-news-group-p (group &optional article)
3578   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3579   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3580       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3581            (eq (gnus-request-type group article) 'news))))
3582
3583 (defsubst gnus-simplify-subject-fully (subject)
3584   "Simplify a subject string according to the user's wishes."
3585   (cond
3586    ((null gnus-summary-gather-subject-limit)
3587     (gnus-simplify-subject-re subject))
3588    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3589     (gnus-simplify-subject-fuzzy subject))
3590    ((numberp gnus-summary-gather-subject-limit)
3591     (gnus-limit-string (gnus-simplify-subject-re subject)
3592                        gnus-summary-gather-subject-limit))
3593    (t
3594     subject)))
3595
3596 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3597   "Check whether two subjects are equal.  If optional argument
3598 simple-first is t, first argument is already simplified."
3599   (cond
3600    ((null simple-first)
3601     (equal (gnus-simplify-subject-fully s1)
3602            (gnus-simplify-subject-fully s2)))
3603    (t
3604     (equal s1
3605            (gnus-simplify-subject-fully s2)))))
3606
3607 ;; Returns a list of writable groups.
3608 (defun gnus-writable-groups ()
3609   (let ((alist gnus-newsrc-alist)
3610         groups group)
3611     (while (setq group (car (pop alist)))
3612       (unless (gnus-group-read-only-p group)
3613         (push group groups)))
3614     (nreverse groups)))
3615
3616 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3617 ;; the echo area.
3618 (defun gnus-y-or-n-p (prompt)
3619   (prog1
3620       (y-or-n-p prompt)
3621     (message "")))
3622
3623 (defun gnus-yes-or-no-p (prompt)
3624   (prog1
3625       (yes-or-no-p prompt)
3626     (message "")))
3627
3628 ;; Check whether to use long file names.
3629 (defun gnus-use-long-file-name (symbol)
3630   ;; The variable has to be set...
3631   (and gnus-use-long-file-name
3632        ;; If it isn't a list, then we return t.
3633        (or (not (listp gnus-use-long-file-name))
3634            ;; If it is a list, and the list contains `symbol', we
3635            ;; return nil.
3636            (not (memq symbol gnus-use-long-file-name)))))
3637
3638 ;; I suspect there's a better way, but I haven't taken the time to do
3639 ;; it yet. -erik selberg@cs.washington.edu
3640 (defun gnus-dd-mmm (messy-date)
3641   "Return a string like DD-MMM from a big messy string"
3642   (let ((datevec (timezone-parse-date messy-date)))
3643     (format "%2s-%s"
3644             (condition-case ()
3645                 ;; Make sure leading zeroes are stripped.
3646                 (number-to-string (string-to-number (aref datevec 2)))
3647               (error "??"))
3648             (capitalize
3649              (or (car
3650                   (nth (1- (string-to-number (aref datevec 1)))
3651                        timezone-months-assoc))
3652                  "???")))))
3653
3654 ;; Make a hash table (default and minimum size is 255).
3655 ;; Optional argument HASHSIZE specifies the table size.
3656 (defun gnus-make-hashtable (&optional hashsize)
3657   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3658
3659 ;; Make a number that is suitable for hashing; bigger than MIN and one
3660 ;; less than 2^x.
3661 (defun gnus-create-hash-size (min)
3662   (let ((i 1))
3663     (while (< i min)
3664       (setq i (* 2 i)))
3665     (1- i)))
3666
3667 ;; Show message if message has a lower level than `gnus-verbose'.
3668 ;; Guideline for numbers:
3669 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3670 ;; for things that take a long time, 7 - not very important messages
3671 ;; on stuff, 9 - messages inside loops.
3672 (defun gnus-message (level &rest args)
3673   (if (<= level gnus-verbose)
3674       (apply 'message args)
3675     ;; We have to do this format thingy here even if the result isn't
3676     ;; shown - the return value has to be the same as the return value
3677     ;; from `message'.
3678     (apply 'format args)))
3679
3680 ;; Generate a unique new group name.
3681 (defun gnus-generate-new-group-name (leaf)
3682   (let ((name leaf)
3683         (num 0))
3684     (while (gnus-gethash name gnus-newsrc-hashtb)
3685       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3686     name))
3687
3688 (defsubst gnus-hide-text (b e props)
3689   "Set text PROPS on the B to E region, extending `intangble' 1 past B."
3690   (add-text-properties b e props)
3691   (when (memq 'intangible props)
3692     (put-text-property (max (1- b) (point-min))
3693                        b 'intangible (cddr (memq 'intangible props)))))
3694
3695 (defsubst gnus-unhide-text (b e)
3696   "Remove hidden text properties from region between B and E."
3697   (remove-text-properties b e gnus-hidden-properties)
3698   (when (memq 'intangible gnus-hidden-properties)
3699     (put-text-property (max (1- b) (point-min))
3700                        b 'intangible nil)))
3701
3702 (defun gnus-hide-text-type (b e type)
3703   "Hide text of TYPE between B and E."
3704   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3705
3706 ;; Find out whether the gnus-visual TYPE is wanted.
3707 (defun gnus-visual-p (&optional type class)
3708   (and gnus-visual                      ; Has to be non-nil, at least.
3709        (if (not type)                   ; We don't care about type.
3710            gnus-visual
3711          (if (listp gnus-visual)        ; It's a list, so we check it.
3712              (or (memq type gnus-visual)
3713                  (memq class gnus-visual))
3714            t))))
3715
3716 (defun gnus-parent-id (references)
3717   "Return the last Message-ID in REFERENCES."
3718   (when (and references
3719              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3720     (substring references (match-beginning 1) (match-end 1))))
3721
3722 (defun gnus-split-references (references)
3723   "Return a list of Message-IDs in REFERENCES."
3724   (let ((beg 0)
3725         ids)
3726     (while (string-match "<[^>]+>" references beg)
3727       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3728             ids))
3729     (nreverse ids)))
3730
3731 (defun gnus-buffer-live-p (buffer)
3732   "Say whether BUFFER is alive or not."
3733   (and buffer
3734        (get-buffer buffer)
3735        (buffer-name (get-buffer buffer))))
3736
3737 (defun gnus-ephemeral-group-p (group)
3738   "Say whether GROUP is ephemeral or not."
3739   (gnus-group-get-parameter group 'quit-config))
3740
3741 (defun gnus-group-quit-config (group)
3742   "Return the quit-config of GROUP."
3743   (gnus-group-get-parameter group 'quit-config))
3744
3745 (defun gnus-simplify-mode-line ()
3746   "Make mode lines a bit simpler."
3747   (setq mode-line-modified "-- ")
3748   (when (listp mode-line-format)
3749     (make-local-variable 'mode-line-format)
3750     (setq mode-line-format (copy-sequence mode-line-format))
3751     (when (equal (nth 3 mode-line-format) "   ")
3752       (setcar (nthcdr 3 mode-line-format) " "))))
3753
3754 ;;; List and range functions
3755
3756 (defun gnus-last-element (list)
3757   "Return last element of LIST."
3758   (while (cdr list)
3759     (setq list (cdr list)))
3760   (car list))
3761
3762 (defun gnus-copy-sequence (list)
3763   "Do a complete, total copy of a list."
3764   (if (and (consp list) (not (consp (cdr list))))
3765       (cons (car list) (cdr list))
3766     (mapcar (lambda (elem) (if (consp elem)
3767                                (if (consp (cdr elem))
3768                                    (gnus-copy-sequence elem)
3769                                  (cons (car elem) (cdr elem)))
3770                              elem))
3771             list)))
3772
3773 (defun gnus-set-difference (list1 list2)
3774   "Return a list of elements of LIST1 that do not appear in LIST2."
3775   (let ((list1 (copy-sequence list1)))
3776     (while list2
3777       (setq list1 (delq (car list2) list1))
3778       (setq list2 (cdr list2)))
3779     list1))
3780
3781 (defun gnus-sorted-complement (list1 list2)
3782   "Return a list of elements of LIST1 that do not appear in LIST2.
3783 Both lists have to be sorted over <."
3784   (let (out)
3785     (if (or (null list1) (null list2))
3786         (or list1 list2)
3787       (while (and list1 list2)
3788         (cond ((= (car list1) (car list2))
3789                (setq list1 (cdr list1)
3790                      list2 (cdr list2)))
3791               ((< (car list1) (car list2))
3792                (setq out (cons (car list1) out))
3793                (setq list1 (cdr list1)))
3794               (t
3795                (setq out (cons (car list2) out))
3796                (setq list2 (cdr list2)))))
3797       (nconc (nreverse out) (or list1 list2)))))
3798
3799 (defun gnus-intersection (list1 list2)
3800   (let ((result nil))
3801     (while list2
3802       (if (memq (car list2) list1)
3803           (setq result (cons (car list2) result)))
3804       (setq list2 (cdr list2)))
3805     result))
3806
3807 (defun gnus-sorted-intersection (list1 list2)
3808   ;; LIST1 and LIST2 have to be sorted over <.
3809   (let (out)
3810     (while (and list1 list2)
3811       (cond ((= (car list1) (car list2))
3812              (setq out (cons (car list1) out)
3813                    list1 (cdr list1)
3814                    list2 (cdr list2)))
3815             ((< (car list1) (car list2))
3816              (setq list1 (cdr list1)))
3817             (t
3818              (setq list2 (cdr list2)))))
3819     (nreverse out)))
3820
3821 (defun gnus-set-sorted-intersection (list1 list2)
3822   ;; LIST1 and LIST2 have to be sorted over <.
3823   ;; This function modifies LIST1.
3824   (let* ((top (cons nil list1))
3825          (prev top))
3826     (while (and list1 list2)
3827       (cond ((= (car list1) (car list2))
3828              (setq prev list1
3829                    list1 (cdr list1)
3830                    list2 (cdr list2)))
3831             ((< (car list1) (car list2))
3832              (setcdr prev (cdr list1))
3833              (setq list1 (cdr list1)))
3834             (t
3835              (setq list2 (cdr list2)))))
3836     (setcdr prev nil)
3837     (cdr top)))
3838
3839 (defun gnus-compress-sequence (numbers &optional always-list)
3840   "Convert list of numbers to a list of ranges or a single range.
3841 If ALWAYS-LIST is non-nil, this function will always release a list of
3842 ranges."
3843   (let* ((first (car numbers))
3844          (last (car numbers))
3845          result)
3846     (if (null numbers)
3847         nil
3848       (if (not (listp (cdr numbers)))
3849           numbers
3850         (while numbers
3851           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3852                 ((= (1+ last) (car numbers)) ;Still in sequence
3853                  (setq last (car numbers)))
3854                 (t                      ;End of one sequence
3855                  (setq result
3856                        (cons (if (= first last) first
3857                                (cons first last)) result))
3858                  (setq first (car numbers))
3859                  (setq last  (car numbers))))
3860           (setq numbers (cdr numbers)))
3861         (if (and (not always-list) (null result))
3862             (if (= first last) (list first) (cons first last))
3863           (nreverse (cons (if (= first last) first (cons first last))
3864                           result)))))))
3865
3866 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3867 (defun gnus-uncompress-range (ranges)
3868   "Expand a list of ranges into a list of numbers.
3869 RANGES is either a single range on the form `(num . num)' or a list of
3870 these ranges."
3871   (let (first last result)
3872     (cond
3873      ((null ranges)
3874       nil)
3875      ((not (listp (cdr ranges)))
3876       (setq first (car ranges))
3877       (setq last (cdr ranges))
3878       (while (<= first last)
3879         (setq result (cons first result))
3880         (setq first (1+ first)))
3881       (nreverse result))
3882      (t
3883       (while ranges
3884         (if (atom (car ranges))
3885             (if (numberp (car ranges))
3886                 (setq result (cons (car ranges) result)))
3887           (setq first (caar ranges))
3888           (setq last  (cdar ranges))
3889           (while (<= first last)
3890             (setq result (cons first result))
3891             (setq first (1+ first))))
3892         (setq ranges (cdr ranges)))
3893       (nreverse result)))))
3894
3895 (defun gnus-add-to-range (ranges list)
3896   "Return a list of ranges that has all articles from both RANGES and LIST.
3897 Note: LIST has to be sorted over `<'."
3898   (if (not ranges)
3899       (gnus-compress-sequence list t)
3900     (setq list (copy-sequence list))
3901     (or (listp (cdr ranges))
3902         (setq ranges (list ranges)))
3903     (let ((out ranges)
3904           ilist lowest highest temp)
3905       (while (and ranges list)
3906         (setq ilist list)
3907         (setq lowest (or (and (atom (car ranges)) (car ranges))
3908                          (caar ranges)))
3909         (while (and list (cdr list) (< (cadr list) lowest))
3910           (setq list (cdr list)))
3911         (if (< (car ilist) lowest)
3912             (progn
3913               (setq temp list)
3914               (setq list (cdr list))
3915               (setcdr temp nil)
3916               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3917         (setq highest (or (and (atom (car ranges)) (car ranges))
3918                           (cdar ranges)))
3919         (while (and list (<= (car list) highest))
3920           (setq list (cdr list)))
3921         (setq ranges (cdr ranges)))
3922       (if list
3923           (setq out (nconc (gnus-compress-sequence list t) out)))
3924       (setq out (sort out (lambda (r1 r2)
3925                             (< (or (and (atom r1) r1) (car r1))
3926                                (or (and (atom r2) r2) (car r2))))))
3927       (setq ranges out)
3928       (while ranges
3929         (if (atom (car ranges))
3930             (if (cdr ranges)
3931                 (if (atom (cadr ranges))
3932                     (if (= (1+ (car ranges)) (cadr ranges))
3933                         (progn
3934                           (setcar ranges (cons (car ranges)
3935                                                (cadr ranges)))
3936                           (setcdr ranges (cddr ranges))))
3937                   (if (= (1+ (car ranges)) (caadr ranges))
3938                       (progn
3939                         (setcar (cadr ranges) (car ranges))
3940                         (setcar ranges (cadr ranges))
3941                         (setcdr ranges (cddr ranges))))))
3942           (if (cdr ranges)
3943               (if (atom (cadr ranges))
3944                   (if (= (1+ (cdar ranges)) (cadr ranges))
3945                       (progn
3946                         (setcdr (car ranges) (cadr ranges))
3947                         (setcdr ranges (cddr ranges))))
3948                 (if (= (1+ (cdar ranges)) (caadr ranges))
3949                     (progn
3950                       (setcdr (car ranges) (cdadr ranges))
3951                       (setcdr ranges (cddr ranges)))))))
3952         (setq ranges (cdr ranges)))
3953       out)))
3954
3955 (defun gnus-remove-from-range (ranges list)
3956   "Return a list of ranges that has all articles from LIST removed from RANGES.
3957 Note: LIST has to be sorted over `<'."
3958   ;; !!! This function shouldn't look like this, but I've got a headache.
3959   (gnus-compress-sequence
3960    (gnus-sorted-complement
3961     (gnus-uncompress-range ranges) list)))
3962
3963 (defun gnus-member-of-range (number ranges)
3964   (if (not (listp (cdr ranges)))
3965       (and (>= number (car ranges))
3966            (<= number (cdr ranges)))
3967     (let ((not-stop t))
3968       (while (and ranges
3969                   (if (numberp (car ranges))
3970                       (>= number (car ranges))
3971                     (>= number (caar ranges)))
3972                   not-stop)
3973         (if (if (numberp (car ranges))
3974                 (= number (car ranges))
3975               (and (>= number (caar ranges))
3976                    (<= number (cdar ranges))))
3977             (setq not-stop nil))
3978         (setq ranges (cdr ranges)))
3979       (not not-stop))))
3980
3981 (defun gnus-range-length (range)
3982   "Return the length RANGE would have if uncompressed."
3983   (length (gnus-uncompress-range range)))
3984
3985 (defun gnus-sublist-p (list sublist)
3986   "Test whether all elements in SUBLIST are members of LIST."
3987   (let ((sublistp t))
3988     (while sublist
3989       (unless (memq (pop sublist) list)
3990         (setq sublistp nil
3991               sublist nil)))
3992     sublistp))
3993
3994 \f
3995 ;;;
3996 ;;; Gnus group mode
3997 ;;;
3998
3999 (defvar gnus-group-mode-map nil)
4000 (put 'gnus-group-mode 'mode-class 'special)
4001
4002 (unless gnus-group-mode-map
4003   (setq gnus-group-mode-map (make-keymap))
4004   (suppress-keymap gnus-group-mode-map)
4005
4006   (gnus-define-keys gnus-group-mode-map
4007     " " gnus-group-read-group
4008     "=" gnus-group-select-group
4009     "\r" gnus-group-select-group
4010     "\M-\r" gnus-group-quick-select-group
4011     "j" gnus-group-jump-to-group
4012     "n" gnus-group-next-unread-group
4013     "p" gnus-group-prev-unread-group
4014     "\177" gnus-group-prev-unread-group
4015     [delete] gnus-group-prev-unread-group
4016     "N" gnus-group-next-group
4017     "P" gnus-group-prev-group
4018     "\M-n" gnus-group-next-unread-group-same-level
4019     "\M-p" gnus-group-prev-unread-group-same-level
4020     "," gnus-group-best-unread-group
4021     "." gnus-group-first-unread-group
4022     "u" gnus-group-unsubscribe-current-group
4023     "U" gnus-group-unsubscribe-group
4024     "c" gnus-group-catchup-current
4025     "C" gnus-group-catchup-current-all
4026     "l" gnus-group-list-groups
4027     "L" gnus-group-list-all-groups
4028     "m" gnus-group-mail
4029     "g" gnus-group-get-new-news
4030     "\M-g" gnus-group-get-new-news-this-group
4031     "R" gnus-group-restart
4032     "r" gnus-group-read-init-file
4033     "B" gnus-group-browse-foreign-server
4034     "b" gnus-group-check-bogus-groups
4035     "F" gnus-find-new-newsgroups
4036     "\C-c\C-d" gnus-group-describe-group
4037     "\M-d" gnus-group-describe-all-groups
4038     "\C-c\C-a" gnus-group-apropos
4039     "\C-c\M-\C-a" gnus-group-description-apropos
4040     "a" gnus-group-post-news
4041     "\ek" gnus-group-edit-local-kill
4042     "\eK" gnus-group-edit-global-kill
4043     "\C-k" gnus-group-kill-group
4044     "\C-y" gnus-group-yank-group
4045     "\C-w" gnus-group-kill-region
4046     "\C-x\C-t" gnus-group-transpose-groups
4047     "\C-c\C-l" gnus-group-list-killed
4048     "\C-c\C-x" gnus-group-expire-articles
4049     "\C-c\M-\C-x" gnus-group-expire-all-groups
4050     "V" gnus-version
4051     "s" gnus-group-save-newsrc
4052     "z" gnus-group-suspend
4053     "Z" gnus-group-clear-dribble
4054     "q" gnus-group-exit
4055     "Q" gnus-group-quit
4056     "?" gnus-group-describe-briefly
4057     "\C-c\C-i" gnus-info-find-node
4058     "\M-e" gnus-group-edit-group-method
4059     "^" gnus-group-enter-server-mode
4060     gnus-mouse-2 gnus-mouse-pick-group
4061     "<" beginning-of-buffer
4062     ">" end-of-buffer
4063     "\C-c\C-b" gnus-bug
4064     "\C-c\C-s" gnus-group-sort-groups
4065     "t" gnus-topic-mode
4066     "\C-c\M-g" gnus-activate-all-groups
4067     "\M-&" gnus-group-universal-argument
4068     "#" gnus-group-mark-group
4069     "\M-#" gnus-group-unmark-group)
4070
4071   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4072     "m" gnus-group-mark-group
4073     "u" gnus-group-unmark-group
4074     "w" gnus-group-mark-region
4075     "m" gnus-group-mark-buffer
4076     "r" gnus-group-mark-regexp
4077     "U" gnus-group-unmark-all-groups)
4078
4079   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4080     "d" gnus-group-make-directory-group
4081     "h" gnus-group-make-help-group
4082     "a" gnus-group-make-archive-group
4083     "k" gnus-group-make-kiboze-group
4084     "m" gnus-group-make-group
4085     "E" gnus-group-edit-group
4086     "e" gnus-group-edit-group-method
4087     "p" gnus-group-edit-group-parameters
4088     "v" gnus-group-add-to-virtual
4089     "V" gnus-group-make-empty-virtual
4090     "D" gnus-group-enter-directory
4091     "f" gnus-group-make-doc-group
4092     "r" gnus-group-rename-group
4093     "\177" gnus-group-delete-group
4094     [delete] gnus-group-delete-group)
4095
4096    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4097      "b" gnus-group-brew-soup
4098      "w" gnus-soup-save-areas
4099      "s" gnus-soup-send-replies
4100      "p" gnus-soup-pack-packet
4101      "r" nnsoup-pack-replies)
4102
4103    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4104      "s" gnus-group-sort-groups
4105      "a" gnus-group-sort-groups-by-alphabet
4106      "u" gnus-group-sort-groups-by-unread
4107      "l" gnus-group-sort-groups-by-level
4108      "v" gnus-group-sort-groups-by-score
4109      "r" gnus-group-sort-groups-by-rank
4110      "m" gnus-group-sort-groups-by-method)
4111
4112    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4113      "k" gnus-group-list-killed
4114      "z" gnus-group-list-zombies
4115      "s" gnus-group-list-groups
4116      "u" gnus-group-list-all-groups
4117      "A" gnus-group-list-active
4118      "a" gnus-group-apropos
4119      "d" gnus-group-description-apropos
4120      "m" gnus-group-list-matching
4121      "M" gnus-group-list-all-matching
4122      "l" gnus-group-list-level)
4123
4124    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4125      "f" gnus-score-flush-cache)
4126
4127    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4128      "f" gnus-group-fetch-faq)
4129
4130    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4131      "l" gnus-group-set-current-level
4132      "t" gnus-group-unsubscribe-current-group
4133      "s" gnus-group-unsubscribe-group
4134      "k" gnus-group-kill-group
4135      "y" gnus-group-yank-group
4136      "w" gnus-group-kill-region
4137      "\C-k" gnus-group-kill-level
4138      "z" gnus-group-kill-all-zombies))
4139
4140 (defun gnus-group-mode ()
4141   "Major mode for reading news.
4142
4143 All normal editing commands are switched off.
4144 \\<gnus-group-mode-map>
4145 The group buffer lists (some of) the groups available.  For instance,
4146 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4147 lists all zombie groups.
4148
4149 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4150 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4151
4152 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4153
4154 The following commands are available:
4155
4156 \\{gnus-group-mode-map}"
4157   (interactive)
4158   (when (and menu-bar-mode
4159              (gnus-visual-p 'group-menu 'menu))
4160     (gnus-group-make-menu-bar))
4161   (kill-all-local-variables)
4162   (gnus-simplify-mode-line)
4163   (setq major-mode 'gnus-group-mode)
4164   (setq mode-name "Group")
4165   (gnus-group-set-mode-line)
4166   (setq mode-line-process nil)
4167   (use-local-map gnus-group-mode-map)
4168   (buffer-disable-undo (current-buffer))
4169   (setq truncate-lines t)
4170   (setq buffer-read-only t)
4171   (run-hooks 'gnus-group-mode-hook))
4172
4173 (defun gnus-mouse-pick-group (e)
4174   "Enter the group under the mouse pointer."
4175   (interactive "e")
4176   (mouse-set-point e)
4177   (gnus-group-read-group nil))
4178
4179 ;; Look at LEVEL and find out what the level is really supposed to be.
4180 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4181 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4182 (defun gnus-group-default-level (&optional level number-or-nil)
4183   (cond
4184    (gnus-group-use-permanent-levels
4185 ;    (setq gnus-group-default-list-level
4186 ;         (or level gnus-group-default-list-level))
4187     (or level gnus-group-default-list-level gnus-level-subscribed))
4188    (number-or-nil
4189     level)
4190    (t
4191     (or level gnus-group-default-list-level gnus-level-subscribed))))
4192
4193 ;;;###autoload
4194 (defun gnus-slave-no-server (&optional arg)
4195   "Read network news as a slave, without connecting to local server"
4196   (interactive "P")
4197   (gnus-no-server arg t))
4198
4199 ;;;###autoload
4200 (defun gnus-no-server (&optional arg slave)
4201   "Read network news.
4202 If ARG is a positive number, Gnus will use that as the
4203 startup level.  If ARG is nil, Gnus will be started at level 2.
4204 If ARG is non-nil and not a positive number, Gnus will
4205 prompt the user for the name of an NNTP server to use.
4206 As opposed to `gnus', this command will not connect to the local server."
4207   (interactive "P")
4208   (let ((gnus-group-use-permanent-levels t))
4209     (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4210   (make-local-variable 'gnus-group-use-permanent-levels)
4211   (setq gnus-group-use-permanent-levels t))
4212
4213 ;;;###autoload
4214 (defun gnus-slave (&optional arg)
4215   "Read news as a slave."
4216   (interactive "P")
4217   (gnus arg nil 'slave))
4218
4219 ;;;###autoload
4220 (defun gnus-other-frame (&optional arg)
4221   "Pop up a frame to read news."
4222   (interactive "P")
4223   (if (get-buffer gnus-group-buffer)
4224       (let ((pop-up-frames t))
4225         (gnus arg))
4226     (select-frame (make-frame))
4227     (gnus arg)))
4228
4229 ;;;###autoload
4230 (defun gnus (&optional arg dont-connect slave)
4231   "Read network news.
4232 If ARG is non-nil and a positive number, Gnus will use that as the
4233 startup level.  If ARG is non-nil and not a positive number, Gnus will
4234 prompt the user for the name of an NNTP server to use."
4235   (interactive "P")
4236
4237   (if (get-buffer gnus-group-buffer)
4238       (progn
4239         (switch-to-buffer gnus-group-buffer)
4240         (gnus-group-get-new-news))
4241
4242     (gnus-clear-system)
4243     (nnheader-init-server-buffer)
4244     (gnus-read-init-file)
4245     (setq gnus-slave slave)
4246
4247     (gnus-group-setup-buffer)
4248     (let ((buffer-read-only nil))
4249       (erase-buffer)
4250       (if (not gnus-inhibit-startup-message)
4251           (progn
4252             (gnus-group-startup-message)
4253             (sit-for 0))))
4254
4255     (let ((level (and (numberp arg) (> arg 0) arg))
4256           did-connect)
4257       (unwind-protect
4258           (progn
4259             (or dont-connect
4260                 (setq did-connect
4261                       (gnus-start-news-server (and arg (not level))))))
4262         (if (and (not dont-connect)
4263                  (not did-connect))
4264             (gnus-group-quit)
4265           (run-hooks 'gnus-startup-hook)
4266           ;; NNTP server is successfully open.
4267
4268           ;; Find the current startup file name.
4269           (setq gnus-current-startup-file
4270                 (gnus-make-newsrc-file gnus-startup-file))
4271
4272           ;; Read the dribble file.
4273           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4274
4275           ;; Allow using GroupLens predictions.
4276           (when gnus-use-grouplens
4277             (bbb-login)
4278             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4279
4280           (gnus-summary-make-display-table)
4281           ;; Do the actual startup.
4282           (gnus-setup-news nil level dont-connect)
4283           ;; Generate the group buffer.
4284           (gnus-group-list-groups level)
4285           (gnus-group-first-unread-group)
4286           (gnus-configure-windows 'group)
4287           (gnus-group-set-mode-line))))))
4288
4289 (defun gnus-unload ()
4290   "Unload all Gnus features."
4291   (interactive)
4292   (or (boundp 'load-history)
4293       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4294   (let ((history load-history)
4295         feature)
4296     (while history
4297       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4298            (setq feature (cdr (assq 'provide (car history))))
4299            (unload-feature feature 'force))
4300       (setq history (cdr history)))))
4301
4302 (defun gnus-compile ()
4303   "Byte-compile the user-defined format specs."
4304   (interactive)
4305   (let ((entries gnus-format-specs)
4306         entry gnus-tmp-func)
4307     (save-excursion
4308       (gnus-message 7 "Compiling format specs...")
4309
4310       (while entries
4311         (setq entry (pop entries))
4312         (if (eq (car entry) 'version)
4313             (setq gnus-format-specs (delq entry gnus-format-specs))
4314           (when (and (listp (caddr entry))
4315                      (not (eq 'byte-code (caaddr entry))))
4316             (fset 'gnus-tmp-func
4317                   `(lambda () ,(caddr entry)))
4318             (byte-compile 'gnus-tmp-func)
4319             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4320
4321       (push (cons 'version emacs-version) gnus-format-specs)
4322
4323       (gnus-message 7 "Compiling user specs...done"))))
4324
4325 (defun gnus-indent-rigidly (start end arg)
4326   "Indent rigidly using only spaces and no tabs."
4327   (save-excursion
4328     (save-restriction
4329       (narrow-to-region start end)
4330       (indent-rigidly start end arg)
4331       (goto-char (point-min))
4332       (while (search-forward "\t" nil t)
4333         (replace-match "        " t t)))))
4334
4335 (defun gnus-group-startup-message (&optional x y)
4336   "Insert startup message in current buffer."
4337   ;; Insert the message.
4338   (erase-buffer)
4339   (insert
4340    (format "              %s
4341           _    ___ _             _
4342           _ ___ __ ___  __    _ ___
4343           __   _     ___    __  ___
4344               _           ___     _
4345              _  _ __             _
4346              ___   __            _
4347                    __           _
4348                     _      _   _
4349                    _      _    _
4350                       _  _    _
4351                   __  ___
4352                  _   _ _     _
4353                 _   _
4354               _    _
4355              _    _
4356             _
4357           __
4358
4359 "
4360            ""))
4361   ;; And then hack it.
4362   (gnus-indent-rigidly (point-min) (point-max)
4363                        (/ (max (- (window-width) (or x 46)) 0) 2))
4364   (goto-char (point-min))
4365   (forward-line 1)
4366   (let* ((pheight (count-lines (point-min) (point-max)))
4367          (wheight (window-height))
4368          (rest (- wheight pheight)))
4369     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4370   ;; Fontify some.
4371   (goto-char (point-min))
4372   (and (search-forward "Praxis" nil t)
4373        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4374   (goto-char (point-min))
4375   (let* ((mode-string (gnus-group-set-mode-line)))
4376     (setq mode-line-buffer-identification
4377           (list (concat gnus-version (substring (car mode-string) 4))))
4378     (set-buffer-modified-p t)))
4379
4380 (defun gnus-group-setup-buffer ()
4381   (or (get-buffer gnus-group-buffer)
4382       (progn
4383         (switch-to-buffer gnus-group-buffer)
4384         (gnus-add-current-to-buffer-list)
4385         (gnus-group-mode)
4386         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4387
4388 (defun gnus-group-list-groups (&optional level unread lowest)
4389   "List newsgroups with level LEVEL or lower that have unread articles.
4390 Default is all subscribed groups.
4391 If argument UNREAD is non-nil, groups with no unread articles are also
4392 listed."
4393   (interactive (list (if current-prefix-arg
4394                          (prefix-numeric-value current-prefix-arg)
4395                        (or
4396                         (gnus-group-default-level nil t)
4397                         gnus-group-default-list-level
4398                         gnus-level-subscribed))))
4399   (or level
4400       (setq level (car gnus-group-list-mode)
4401             unread (cdr gnus-group-list-mode)))
4402   (setq level (gnus-group-default-level level))
4403   (gnus-group-setup-buffer)             ;May call from out of group buffer
4404   (gnus-update-format-specifications)
4405   (let ((case-fold-search nil)
4406         (props (text-properties-at (gnus-point-at-bol)))
4407         (group (gnus-group-group-name)))
4408     (set-buffer gnus-group-buffer)
4409     (funcall gnus-group-prepare-function level unread lowest)
4410     (if (zerop (buffer-size))
4411         (gnus-message 5 gnus-no-groups-message)
4412       (goto-char (point-max))
4413       (when (or (not gnus-group-goto-next-group-function)
4414                 (not (funcall gnus-group-goto-next-group-function 
4415                               group props)))
4416         (if (not group)
4417             ;; Go to the first group with unread articles.
4418             (gnus-group-search-forward t)
4419           ;; Find the right group to put point on.  If the current group
4420           ;; has disappeared in the new listing, try to find the next
4421           ;; one.        If no next one can be found, just leave point at the
4422           ;; first newsgroup in the buffer.
4423           (if (not (gnus-goto-char
4424                     (text-property-any
4425                      (point-min) (point-max)
4426                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4427               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4428                 (while (and newsrc
4429                             (not (gnus-goto-char
4430                                   (text-property-any
4431                                    (point-min) (point-max) 'gnus-group
4432                                    (gnus-intern-safe
4433                                     (caar newsrc) gnus-active-hashtb)))))
4434                   (setq newsrc (cdr newsrc)))
4435                 (or newsrc (progn (goto-char (point-max))
4436                                   (forward-line -1)))))))
4437       ;; Adjust cursor point.
4438       (gnus-group-position-point))))
4439
4440 (defun gnus-group-list-level (level &optional all)
4441   "List groups on LEVEL.
4442 If ALL (the prefix), also list groups that have no unread articles."
4443   (interactive "nList groups on level: \nP")
4444   (gnus-group-list-groups level all level))
4445
4446 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4447   "List all newsgroups with unread articles of level LEVEL or lower.
4448 If ALL is non-nil, list groups that have no unread articles.
4449 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4450 If REGEXP, only list groups matching REGEXP."
4451   (set-buffer gnus-group-buffer)
4452   (let ((buffer-read-only nil)
4453         (newsrc (cdr gnus-newsrc-alist))
4454         (lowest (or lowest 1))
4455         info clevel unread group params)
4456     (erase-buffer)
4457     (if (< lowest gnus-level-zombie)
4458         ;; List living groups.
4459         (while newsrc
4460           (setq info (car newsrc)
4461                 group (gnus-info-group info)
4462                 params (gnus-info-params info)
4463                 newsrc (cdr newsrc)
4464                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4465           (and unread                   ; This group might be bogus
4466                (or (not regexp)
4467                    (string-match regexp group))
4468                (<= (setq clevel (gnus-info-level info)) level)
4469                (>= clevel lowest)
4470                (or all                  ; We list all groups?
4471                    (if (eq unread t)    ; Unactivated?
4472                        gnus-group-list-inactive-groups ; We list unactivated 
4473                      (> unread 0))      ; We list groups with unread articles
4474                    (and gnus-list-groups-with-ticked-articles
4475                         (cdr (assq 'tick (gnus-info-marks info))))
4476                                         ; And groups with tickeds
4477                    ;; Check for permanent visibility.
4478                    (and gnus-permanently-visible-groups
4479                         (string-match gnus-permanently-visible-groups
4480                                       group))
4481                    (memq 'visible params)
4482                    (cdr (assq 'visible params)))
4483                (gnus-group-insert-group-line
4484                 group (gnus-info-level info)
4485                 (gnus-info-marks info) unread (gnus-info-method info)))))
4486
4487     ;; List dead groups.
4488     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4489          (gnus-group-prepare-flat-list-dead
4490           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4491           gnus-level-zombie ?Z
4492           regexp))
4493     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4494          (gnus-group-prepare-flat-list-dead
4495           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4496           gnus-level-killed ?K regexp))
4497
4498     (gnus-group-set-mode-line)
4499     (setq gnus-group-list-mode (cons level all))
4500     (run-hooks 'gnus-group-prepare-hook)))
4501
4502 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4503   ;; List zombies and killed lists somewhat faster, which was
4504   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4505   ;; this by ignoring the group format specification altogether.
4506   (let (group)
4507     (if regexp
4508         ;; This loop is used when listing groups that match some
4509         ;; regexp.
4510         (while groups
4511           (setq group (pop groups))
4512           (when (string-match regexp group)
4513             (add-text-properties
4514              (point) (prog1 (1+ (point))
4515                        (insert " " mark "     *: " group "\n"))
4516              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4517                    'gnus-unread t
4518                    'gnus-level level))))
4519       ;; This loop is used when listing all groups.
4520       (while groups
4521         (add-text-properties
4522          (point) (prog1 (1+ (point))
4523                    (insert " " mark "     *: "
4524                            (setq group (pop groups)) "\n"))
4525          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4526                'gnus-unread t
4527                'gnus-level level))))))
4528
4529 (defmacro gnus-group-real-name (group)
4530   "Find the real name of a foreign newsgroup."
4531   `(let ((gname ,group))
4532      (if (string-match ":[^:]+$" gname)
4533          (substring gname (1+ (match-beginning 0)))
4534        gname)))
4535
4536 (defsubst gnus-server-add-address (method)
4537   (let ((method-name (symbol-name (car method))))
4538     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4539              (not (assq (intern (concat method-name "-address")) method)))
4540         (append method (list (list (intern (concat method-name "-address"))
4541                                    (nth 1 method))))
4542       method)))
4543
4544 (defsubst gnus-server-get-method (group method)
4545   ;; Input either a server name, and extended server name, or a
4546   ;; select method, and return a select method.
4547   (cond ((stringp method)
4548          (gnus-server-to-method method))
4549         ((equal method gnus-select-method)
4550          gnus-select-method)
4551         ((and (stringp (car method)) group)
4552          (gnus-server-extend-method group method))
4553         ((and method (not group)
4554               (equal (cadr method) ""))
4555          method)
4556         (t
4557          (gnus-server-add-address method))))
4558
4559 (defun gnus-server-to-method (server)
4560   "Map virtual server names to select methods."
4561   (or 
4562    ;; Perhaps this is the native server?
4563    (and (equal server "native") gnus-select-method)
4564    ;; It should be in the server alist.
4565    (cdr (assoc server gnus-server-alist))
4566    ;; If not, we look through all the opened server
4567    ;; to see whether we can find it there.
4568    (let ((opened gnus-opened-servers))
4569      (while (and opened
4570                  (not (equal server (format "%s:%s" (caaar opened)
4571                                             (cadaar opened)))))
4572        (pop opened))
4573      (caar opened))))
4574
4575 (defmacro gnus-method-equal (ss1 ss2)
4576   "Say whether two servers are equal."
4577   `(let ((s1 ,ss1)
4578          (s2 ,ss2))
4579      (or (equal s1 s2)
4580          (and (= (length s1) (length s2))
4581               (progn
4582                 (while (and s1 (member (car s1) s2))
4583                   (setq s1 (cdr s1)))
4584                 (null s1))))))
4585
4586 (defun gnus-server-equal (m1 m2)
4587   "Say whether two methods are equal."
4588   (let ((m1 (cond ((null m1) gnus-select-method)
4589                   ((stringp m1) (gnus-server-to-method m1))
4590                   (t m1)))
4591         (m2 (cond ((null m2) gnus-select-method)
4592                   ((stringp m2) (gnus-server-to-method m2))
4593                   (t m2))))
4594     (gnus-method-equal m1 m2)))
4595
4596 (defun gnus-servers-using-backend (backend)
4597   "Return a list of known servers using BACKEND."
4598   (let ((opened gnus-opened-servers)
4599         out)
4600     (while opened
4601       (when (eq backend (caaar opened))
4602         (push (caar opened) out))
4603       (pop opened))
4604     out))
4605
4606 (defun gnus-group-prefixed-name (group method)
4607   "Return the whole name from GROUP and METHOD."
4608   (and (stringp method) (setq method (gnus-server-to-method method)))
4609   (concat (format "%s" (car method))
4610           (if (and
4611                (or (assoc (format "%s" (car method)) 
4612                           (gnus-methods-using 'address))
4613                    (gnus-server-equal method gnus-message-archive-method))
4614                (nth 1 method)
4615                (not (string= (nth 1 method) "")))
4616               (concat "+" (nth 1 method)))
4617           ":" group))
4618
4619 (defun gnus-group-real-prefix (group)
4620   "Return the prefix of the current group name."
4621   (if (string-match "^[^:]+:" group)
4622       (substring group 0 (match-end 0))
4623     ""))
4624
4625 (defun gnus-group-method (group)
4626   "Return the server or method used for selecting GROUP."
4627   (let ((prefix (gnus-group-real-prefix group)))
4628     (if (equal prefix "")
4629         gnus-select-method
4630       (let ((servers gnus-opened-servers)
4631             (server "")
4632             backend possible found)
4633         (if (string-match "^[^\\+]+\\+" prefix)
4634             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4635                   server (substring prefix (match-end 0) (1- (length prefix))))
4636           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4637         (while servers
4638           (when (eq (caaar servers) backend)
4639             (setq possible (caar servers))
4640             (when (equal (cadaar servers) server)
4641               (setq found (caar servers))))
4642           (pop servers))
4643         (or (car (rassoc found gnus-server-alist))
4644             found
4645             (car (rassoc possible gnus-server-alist))
4646             possible
4647             (list backend server))))))
4648
4649 (defsubst gnus-secondary-method-p (method)
4650   "Return whether METHOD is a secondary select method."
4651   (let ((methods gnus-secondary-select-methods)
4652         (gmethod (gnus-server-get-method nil method)))
4653     (while (and methods
4654                 (not (equal (gnus-server-get-method nil (car methods))
4655                             gmethod)))
4656       (setq methods (cdr methods)))
4657     methods))
4658
4659 (defun gnus-group-foreign-p (group)
4660   "Say whether a group is foreign or not."
4661   (and (not (gnus-group-native-p group))
4662        (not (gnus-group-secondary-p group))))
4663
4664 (defun gnus-group-native-p (group)
4665   "Say whether the group is native or not."
4666   (not (string-match ":" group)))
4667
4668 (defun gnus-group-secondary-p (group)
4669   "Say whether the group is secondary or not."
4670   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4671
4672 (defun gnus-group-get-parameter (group &optional symbol)
4673   "Returns the group parameters for GROUP.
4674 If SYMBOL, return the value of that symbol in the group parameters."
4675   (let ((params (gnus-info-params (gnus-get-info group))))
4676     (if symbol
4677         (gnus-group-parameter-value params symbol)
4678       params)))
4679
4680 (defun gnus-group-parameter-value (params symbol)
4681   "Return the value of SYMBOL in group PARAMS."
4682   (or (car (memq symbol params))        ; It's either a simple symbol
4683       (cdr (assq symbol params))))      ; or a cons.
4684
4685 (defun gnus-group-add-parameter (group param)
4686   "Add parameter PARAM to GROUP."
4687   (let ((info (gnus-get-info group)))
4688     (if (not info)
4689         () ; This is a dead group.  We just ignore it.
4690       ;; Cons the new param to the old one and update.
4691       (gnus-group-set-info (cons param (gnus-info-params info))
4692                            group 'params))))
4693
4694 (defun gnus-group-set-parameter (group name value)
4695   "Set parameter NAME to VALUE in GROUP."
4696   (let ((info (gnus-get-info group)))
4697     (if (not info)
4698         () ; This is a dead group.  We just ignore it.
4699       (let ((old-params (gnus-info-params info))
4700             (new-params (list (cons name value))))
4701         (while old-params
4702           (if (or (not (listp (car old-params)))
4703                   (not (eq (caar old-params) name)))
4704               (setq new-params (append new-params (list (car old-params)))))
4705           (setq old-params (cdr old-params)))
4706         (gnus-group-set-info new-params group 'params)))))
4707
4708 (defun gnus-group-add-score (group &optional score)
4709   "Add SCORE to the GROUP score.
4710 If SCORE is nil, add 1 to the score of GROUP."
4711   (let ((info (gnus-get-info group)))
4712     (when info
4713       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4714
4715 (defun gnus-summary-bubble-group ()
4716   "Increase the score of the current group.
4717 This is a handy function to add to `gnus-summary-exit-hook' to
4718 increase the score of each group you read."
4719   (gnus-group-add-score gnus-newsgroup-name))
4720
4721 (defun gnus-group-set-info (info &optional method-only-group part)
4722   (let* ((entry (gnus-gethash
4723                  (or method-only-group (gnus-info-group info))
4724                  gnus-newsrc-hashtb))
4725          (part-info info)
4726          (info (if method-only-group (nth 2 entry) info))
4727          method)
4728     (when method-only-group
4729       (unless entry
4730         (error "Trying to change non-existent group %s" method-only-group))
4731       ;; We have received parts of the actual group info - either the
4732       ;; select method or the group parameters.  We first check
4733       ;; whether we have to extend the info, and if so, do that.
4734       (let ((len (length info))
4735             (total (if (eq part 'method) 5 6)))
4736         (when (< len total)
4737           (setcdr (nthcdr (1- len) info)
4738                   (make-list (- total len) nil)))
4739         ;; Then we enter the new info.
4740         (setcar (nthcdr (1- total) info) part-info)))
4741     (unless entry
4742       ;; This is a new group, so we just create it.
4743       (save-excursion
4744         (set-buffer gnus-group-buffer)
4745         (setq method (gnus-info-method info))
4746         (when (gnus-server-equal method "native")
4747           (setq method nil))
4748         (if method
4749             ;; It's a foreign group...
4750             (gnus-group-make-group
4751              (gnus-group-real-name (gnus-info-group info))
4752              (if (stringp method) method
4753                (prin1-to-string (car method)))
4754              (and (consp method)
4755                   (nth 1 (gnus-info-method info))))
4756           ;; It's a native group.
4757           (gnus-group-make-group (gnus-info-group info)))
4758         (gnus-message 6 "Note: New group created")
4759         (setq entry
4760               (gnus-gethash (gnus-group-prefixed-name
4761                              (gnus-group-real-name (gnus-info-group info))
4762                              (or (gnus-info-method info) gnus-select-method))
4763                             gnus-newsrc-hashtb))))
4764     ;; Whether it was a new group or not, we now have the entry, so we
4765     ;; can do the update.
4766     (if entry
4767         (progn
4768           (setcar (nthcdr 2 entry) info)
4769           (when (and (not (eq (car entry) t))
4770                      (gnus-active (gnus-info-group info)))
4771             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4772       (error "No such group: %s" (gnus-info-group info)))))
4773
4774 (defun gnus-group-set-method-info (group select-method)
4775   (gnus-group-set-info select-method group 'method))
4776
4777 (defun gnus-group-set-params-info (group params)
4778   (gnus-group-set-info params group 'params))
4779
4780 (defun gnus-group-update-group-line ()
4781   "Update the current line in the group buffer."
4782   (let* ((buffer-read-only nil)
4783          (group (gnus-group-group-name))
4784          (gnus-group-indentation (gnus-group-group-indentation))
4785          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4786     (and entry
4787          (not (gnus-ephemeral-group-p group))
4788          (gnus-dribble-enter
4789           (concat "(gnus-group-set-info '"
4790                   (prin1-to-string (nth 2 entry)) ")")))
4791     (gnus-delete-line)
4792     (gnus-group-insert-group-line-info group)
4793     (forward-line -1)
4794     (gnus-group-position-point)))
4795
4796 (defun gnus-group-insert-group-line-info (group)
4797   "Insert GROUP on the current line."
4798   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4799         active info)
4800     (if entry
4801         (progn
4802           ;; (Un)subscribed group.
4803           (setq info (nth 2 entry))
4804           (gnus-group-insert-group-line
4805            group (gnus-info-level info) (gnus-info-marks info)
4806            (or (car entry) t) (gnus-info-method info)))
4807       ;; This group is dead.
4808       (gnus-group-insert-group-line
4809        group
4810        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4811        nil
4812        (if (setq active (gnus-active group))
4813            (- (1+ (cdr active)) (car active)) 0)
4814        nil))))
4815
4816 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4817                                                     gnus-tmp-marked number
4818                                                     gnus-tmp-method)
4819   "Insert a group line in the group buffer."
4820   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4821          (gnus-tmp-number-total
4822           (if gnus-tmp-active
4823               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4824             0))
4825          (gnus-tmp-number-of-unread
4826           (if (numberp number) (int-to-string (max 0 number))
4827             "*"))
4828          (gnus-tmp-number-of-read
4829           (if (numberp number)
4830               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4831             "*"))
4832          (gnus-tmp-subscribed
4833           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4834                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4835                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4836                 (t ?K)))
4837          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4838          (gnus-tmp-newsgroup-description
4839           (if gnus-description-hashtb
4840               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4841             ""))
4842          (gnus-tmp-moderated
4843           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4844          (gnus-tmp-moderated-string
4845           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4846          (gnus-tmp-method
4847           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4848          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4849          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4850          (gnus-tmp-news-method-string
4851           (if gnus-tmp-method
4852               (format "(%s:%s)" (car gnus-tmp-method)
4853                       (cadr gnus-tmp-method)) ""))
4854          (gnus-tmp-marked-mark
4855           (if (and (numberp number)
4856                    (zerop number)
4857                    (cdr (assq 'tick gnus-tmp-marked)))
4858               ?* ? ))
4859          (gnus-tmp-process-marked
4860           (if (member gnus-tmp-group gnus-group-marked)
4861               gnus-process-mark ? ))
4862          (gnus-tmp-grouplens
4863           (or (and gnus-use-grouplens
4864                    (bbb-grouplens-group-p gnus-tmp-group))
4865               ""))
4866          (buffer-read-only nil)
4867          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4868     (beginning-of-line)
4869     (add-text-properties
4870      (point)
4871      (prog1 (1+ (point))
4872        ;; Insert the text.
4873        (eval gnus-group-line-format-spec))
4874      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4875        gnus-unread ,(if (numberp number)
4876                         (string-to-int gnus-tmp-number-of-unread)
4877                       t)
4878        gnus-marked ,gnus-tmp-marked-mark
4879        gnus-indentation ,gnus-group-indentation
4880        gnus-level ,gnus-tmp-level))
4881     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4882       (forward-line -1)
4883       (run-hooks 'gnus-group-update-hook)
4884       (forward-line))
4885     ;; Allow XEmacs to remove front-sticky text properties.
4886     (gnus-group-remove-excess-properties)))
4887
4888 (defun gnus-group-update-group (group &optional visible-only)
4889   "Update all lines where GROUP appear.
4890 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4891 already."
4892   (save-excursion
4893     (set-buffer gnus-group-buffer)
4894     ;; The buffer may be narrowed.
4895     (save-restriction
4896       (widen)
4897       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4898             (loc (point-min))
4899             found buffer-read-only)
4900         ;; Enter the current status into the dribble buffer.
4901         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4902           (if (and entry (not (gnus-ephemeral-group-p group)))
4903               (gnus-dribble-enter
4904                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4905                        ")"))))
4906         ;; Find all group instances.  If topics are in use, each group
4907         ;; may be listed in more than once.
4908         (while (setq loc (text-property-any
4909                           loc (point-max) 'gnus-group ident))
4910           (setq found t)
4911           (goto-char loc)
4912           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4913             (gnus-delete-line)
4914             (gnus-group-insert-group-line-info group))
4915           (setq loc (1+ loc)))
4916         (unless (or found visible-only)
4917           ;; No such line in the buffer, find out where it's supposed to
4918           ;; go, and insert it there (or at the end of the buffer).
4919           (if gnus-goto-missing-group-function
4920               (funcall gnus-goto-missing-group-function group)
4921             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4922               (while (and entry (car entry)
4923                           (not
4924                            (gnus-goto-char
4925                             (text-property-any
4926                              (point-min) (point-max)
4927                              'gnus-group (gnus-intern-safe
4928                                           (caar entry) gnus-active-hashtb)))))
4929                 (setq entry (cdr entry)))
4930               (or entry (goto-char (point-max)))))
4931           ;; Finally insert the line.
4932           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4933             (gnus-group-insert-group-line-info group)))
4934         (gnus-group-set-mode-line)))))
4935
4936 (defun gnus-group-set-mode-line ()
4937   (when (memq 'group gnus-updated-mode-lines)
4938     (let* ((gformat (or gnus-group-mode-line-format-spec
4939                         (setq gnus-group-mode-line-format-spec
4940                               (gnus-parse-format
4941                                gnus-group-mode-line-format
4942                                gnus-group-mode-line-format-alist))))
4943            (gnus-tmp-news-server (cadr gnus-select-method))
4944            (gnus-tmp-news-method (car gnus-select-method))
4945            (max-len 60)
4946            gnus-tmp-header                      ;Dummy binding for user-defined formats
4947            ;; Get the resulting string.
4948            (mode-string (eval gformat)))
4949       ;; If the line is too long, we chop it off.
4950       (when (> (length mode-string) max-len)
4951         (setq mode-string (substring mode-string 0 (- max-len 4))))
4952       (prog1
4953           (setq mode-line-buffer-identification (list mode-string))
4954         (set-buffer-modified-p t)))))
4955
4956 (defun gnus-group-group-name ()
4957   "Get the name of the newsgroup on the current line."
4958   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4959     (and group (symbol-name group))))
4960
4961 (defun gnus-group-group-level ()
4962   "Get the level of the newsgroup on the current line."
4963   (get-text-property (gnus-point-at-bol) 'gnus-level))
4964
4965 (defun gnus-group-group-indentation ()
4966   "Get the indentation of the newsgroup on the current line."
4967   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
4968       (and gnus-group-indentation-function
4969            (funcall gnus-group-indentation-function))
4970       ""))
4971
4972 (defun gnus-group-group-unread ()
4973   "Get the number of unread articles of the newsgroup on the current line."
4974   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4975
4976 (defun gnus-group-search-forward (&optional backward all level first-too)
4977   "Find the next newsgroup with unread articles.
4978 If BACKWARD is non-nil, find the previous newsgroup instead.
4979 If ALL is non-nil, just find any newsgroup.
4980 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4981 group exists.
4982 If FIRST-TOO, the current line is also eligible as a target."
4983   (let ((way (if backward -1 1))
4984         (low gnus-level-killed)
4985         (beg (point))
4986         pos found lev)
4987     (if (and backward (progn (beginning-of-line)) (bobp))
4988         nil
4989       (or first-too (forward-line way))
4990       (while (and
4991               (not (eobp))
4992               (not (setq
4993                     found
4994                     (and (or all
4995                              (and
4996                               (let ((unread
4997                                      (get-text-property (point) 'gnus-unread)))
4998                                 (and (numberp unread) (> unread 0)))
4999                               (setq lev (get-text-property (point)
5000                                                            'gnus-level))
5001                               (<= lev gnus-level-subscribed)))
5002                          (or (not level)
5003                              (and (setq lev (get-text-property (point)
5004                                                                'gnus-level))
5005                                   (or (= lev level)
5006                                       (and (< lev low)
5007                                            (< level lev)
5008                                            (progn
5009                                              (setq low lev)
5010                                              (setq pos (point))
5011                                              nil))))))))
5012               (zerop (forward-line way)))))
5013     (if found
5014         (progn (gnus-group-position-point) t)
5015       (goto-char (or pos beg))
5016       (and pos t))))
5017
5018 ;;; Gnus group mode commands
5019
5020 ;; Group marking.
5021
5022 (defun gnus-group-mark-group (n &optional unmark no-advance)
5023   "Mark the current group."
5024   (interactive "p")
5025   (let ((buffer-read-only nil)
5026         group)
5027     (while
5028         (and (> n 0)
5029              (setq group (gnus-group-group-name))
5030              (progn
5031                (beginning-of-line)
5032                (forward-char
5033                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5034                (delete-char 1)
5035                (if unmark
5036                    (progn
5037                      (insert " ")
5038                      (setq gnus-group-marked (delete group gnus-group-marked)))
5039                  (insert "#")
5040                  (setq gnus-group-marked
5041                        (cons group (delete group gnus-group-marked))))
5042                t)
5043              (or no-advance (zerop (gnus-group-next-group 1))))
5044       (setq n (1- n)))
5045     (gnus-summary-position-point)
5046     n))
5047
5048 (defun gnus-group-unmark-group (n)
5049   "Remove the mark from the current group."
5050   (interactive "p")
5051   (gnus-group-mark-group n 'unmark)
5052   (gnus-group-position-point))
5053
5054 (defun gnus-group-unmark-all-groups ()
5055   "Unmark all groups."
5056   (interactive)
5057   (let ((groups gnus-group-marked))
5058     (save-excursion
5059       (while groups
5060         (gnus-group-remove-mark (pop groups)))))
5061   (gnus-group-position-point))
5062
5063 (defun gnus-group-mark-region (unmark beg end)
5064   "Mark all groups between point and mark.
5065 If UNMARK, remove the mark instead."
5066   (interactive "P\nr")
5067   (let ((num (count-lines beg end)))
5068     (save-excursion
5069       (goto-char beg)
5070       (- num (gnus-group-mark-group num unmark)))))
5071
5072 (defun gnus-group-mark-buffer (&optional unmark)
5073   "Mark all groups in the buffer.
5074 If UNMARK, remove the mark instead."
5075   (interactive "P")
5076   (gnus-group-mark-region unmark (point-min) (point-max)))
5077
5078 (defun gnus-group-mark-regexp (regexp)
5079   "Mark all groups that match some regexp."
5080   (interactive "sMark (regexp): ")
5081   (let ((alist (cdr gnus-newsrc-alist))
5082         group)
5083     (while alist
5084       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5085         (gnus-group-set-mark group))))
5086   (gnus-group-position-point))
5087
5088 (defun gnus-group-remove-mark (group)
5089   "Remove the process mark from GROUP and move point there.
5090 Return nil if the group isn't displayed."
5091   (if (gnus-group-goto-group group)
5092       (save-excursion
5093         (gnus-group-mark-group 1 'unmark t)
5094         t)
5095     (setq gnus-group-marked
5096           (delete group gnus-group-marked))
5097     nil))
5098
5099 (defun gnus-group-set-mark (group)
5100   "Set the process mark on GROUP."
5101   (if (gnus-group-goto-group group) 
5102       (save-excursion
5103         (gnus-group-mark-group 1 nil t))
5104     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5105
5106 (defun gnus-group-universal-argument (arg &optional groups func)
5107   "Perform any command on all groups accoring to the process/prefix convention."
5108   (interactive "P")
5109   (let ((groups (or groups (gnus-group-process-prefix arg)))
5110         group func)
5111     (if (eq (setq func (or func
5112                            (key-binding
5113                             (read-key-sequence
5114                              (substitute-command-keys
5115                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5116             'undefined)
5117         (progn
5118           (message "Undefined key")
5119           (ding))
5120       (while groups
5121         (gnus-group-remove-mark (setq group (pop groups)))
5122         (command-execute func))))
5123   (gnus-group-position-point))
5124
5125 (defun gnus-group-process-prefix (n)
5126   "Return a list of groups to work on.
5127 Take into consideration N (the prefix) and the list of marked groups."
5128   (cond
5129    (n
5130     (setq n (prefix-numeric-value n))
5131     ;; There is a prefix, so we return a list of the N next
5132     ;; groups.
5133     (let ((way (if (< n 0) -1 1))
5134           (n (abs n))
5135           group groups)
5136       (save-excursion
5137         (while (and (> n 0)
5138                     (setq group (gnus-group-group-name)))
5139           (setq groups (cons group groups))
5140           (setq n (1- n))
5141           (gnus-group-next-group way)))
5142       (nreverse groups)))
5143    ((and (boundp 'transient-mark-mode)
5144          transient-mark-mode
5145          mark-active)
5146     ;; Work on the region between point and mark.
5147     (let ((max (max (point) (mark)))
5148           groups)
5149       (save-excursion
5150         (goto-char (min (point) (mark)))
5151         (while
5152             (and
5153              (push (gnus-group-group-name) groups)
5154              (zerop (gnus-group-next-group 1))
5155              (< (point) max)))
5156         (nreverse groups))))
5157    (gnus-group-marked
5158     ;; No prefix, but a list of marked articles.
5159     (reverse gnus-group-marked))
5160    (t
5161     ;; Neither marked articles or a prefix, so we return the
5162     ;; current group.
5163     (let ((group (gnus-group-group-name)))
5164       (and group (list group))))))
5165
5166 ;; Selecting groups.
5167
5168 (defun gnus-group-read-group (&optional all no-article group)
5169   "Read news in this newsgroup.
5170 If the prefix argument ALL is non-nil, already read articles become
5171 readable.  IF ALL is a number, fetch this number of articles.  If the
5172 optional argument NO-ARTICLE is non-nil, no article will be
5173 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5174 group."
5175   (interactive "P")
5176   (let ((group (or group (gnus-group-group-name)))
5177         number active marked entry)
5178     (or group (error "No group on current line"))
5179     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5180                                             group gnus-newsrc-hashtb)))))
5181     ;; This group might be a dead group.  In that case we have to get
5182     ;; the number of unread articles from `gnus-active-hashtb'.
5183     (setq number
5184           (cond ((numberp all) all)
5185                 (entry (car entry))
5186                 ((setq active (gnus-active group))
5187                  (- (1+ (cdr active)) (car active)))))
5188     (gnus-summary-read-group
5189      group (or all (and (numberp number)
5190                         (zerop (+ number (length (cdr (assq 'tick marked)))
5191                                   (length (cdr (assq 'dormant marked)))))))
5192      no-article)))
5193
5194 (defun gnus-group-select-group (&optional all)
5195   "Select this newsgroup.
5196 No article is selected automatically.
5197 If ALL is non-nil, already read articles become readable.
5198 If ALL is a number, fetch this number of articles."
5199   (interactive "P")
5200   (gnus-group-read-group all t))
5201
5202 (defun gnus-group-quick-select-group (&optional all)
5203   "Select the current group \"quickly\".
5204 This means that no highlighting or scoring will be performed."
5205   (interactive "P")
5206   (let (gnus-visual
5207         gnus-score-find-score-files-function
5208         gnus-apply-kill-hook
5209         gnus-summary-expunge-below)
5210     (gnus-group-read-group all t)))
5211
5212 (defun gnus-group-visible-select-group (&optional all)
5213   "Select the current group without hiding any articles."
5214   (interactive "P")
5215   (let ((gnus-inhibit-limiting t))
5216     (gnus-group-read-group all t)))
5217
5218 ;;;###autoload
5219 (defun gnus-fetch-group (group)
5220   "Start Gnus if necessary and enter GROUP.
5221 Returns whether the fetching was successful or not."
5222   (interactive "sGroup name: ")
5223   (or (get-buffer gnus-group-buffer)
5224       (gnus))
5225   (gnus-group-read-group nil nil group))
5226
5227 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5228 ;; if selection was successful.
5229 (defun gnus-group-read-ephemeral-group
5230   (group method &optional activate quit-config)
5231   (let ((group (if (gnus-group-foreign-p group) group
5232                  (gnus-group-prefixed-name group method))))
5233     (gnus-sethash
5234      group
5235      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5236                      ((quit-config . ,(if quit-config quit-config
5237                                         (cons (current-buffer) 'summary))))))
5238      gnus-newsrc-hashtb)
5239     (set-buffer gnus-group-buffer)
5240     (or (gnus-check-server method)
5241         (error "Unable to contact server: %s" (gnus-status-message method)))
5242     (if activate (or (gnus-request-group group)
5243                      (error "Couldn't request group")))
5244     (condition-case ()
5245         (gnus-group-read-group t t group)
5246       (error nil)
5247       (quit nil))))
5248
5249 (defun gnus-group-jump-to-group (group)
5250   "Jump to newsgroup GROUP."
5251   (interactive
5252    (list (completing-read
5253           "Group: " gnus-active-hashtb nil
5254           (memq gnus-select-method gnus-have-read-active-file))))
5255
5256   (if (equal group "")
5257       (error "Empty group name"))
5258
5259   (let ((b (text-property-any
5260             (point-min) (point-max)
5261             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5262     (unless (gnus-ephemeral-group-p group)
5263       (if b
5264           ;; Either go to the line in the group buffer...
5265           (goto-char b)
5266         ;; ... or insert the line.
5267         (or
5268          (gnus-active group)
5269          (gnus-activate-group group)
5270          (error "%s error: %s" group (gnus-status-message group)))
5271
5272         (gnus-group-update-group group)
5273         (goto-char (text-property-any
5274                     (point-min) (point-max)
5275                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5276     ;; Adjust cursor point.
5277     (gnus-group-position-point)))
5278
5279 (defun gnus-group-goto-group (group)
5280   "Goto to newsgroup GROUP."
5281   (when group
5282     (let ((b (text-property-any (point-min) (point-max)
5283                                 'gnus-group (gnus-intern-safe
5284                                              group gnus-active-hashtb))))
5285       (and b (goto-char b)))))
5286
5287 (defun gnus-group-next-group (n)
5288   "Go to next N'th newsgroup.
5289 If N is negative, search backward instead.
5290 Returns the difference between N and the number of skips actually
5291 done."
5292   (interactive "p")
5293   (gnus-group-next-unread-group n t))
5294
5295 (defun gnus-group-next-unread-group (n &optional all level)
5296   "Go to next N'th unread newsgroup.
5297 If N is negative, search backward instead.
5298 If ALL is non-nil, choose any newsgroup, unread or not.
5299 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5300 such group can be found, the next group with a level higher than
5301 LEVEL.
5302 Returns the difference between N and the number of skips actually
5303 made."
5304   (interactive "p")
5305   (let ((backward (< n 0))
5306         (n (abs n)))
5307     (while (and (> n 0)
5308                 (gnus-group-search-forward
5309                  backward (or (not gnus-group-goto-unread) all) level))
5310       (setq n (1- n)))
5311     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5312                                (if level " on this level or higher" "")))
5313     n))
5314
5315 (defun gnus-group-prev-group (n)
5316   "Go to previous N'th newsgroup.
5317 Returns the difference between N and the number of skips actually
5318 done."
5319   (interactive "p")
5320   (gnus-group-next-unread-group (- n) t))
5321
5322 (defun gnus-group-prev-unread-group (n)
5323   "Go to previous N'th unread newsgroup.
5324 Returns the difference between N and the number of skips actually
5325 done."
5326   (interactive "p")
5327   (gnus-group-next-unread-group (- n)))
5328
5329 (defun gnus-group-next-unread-group-same-level (n)
5330   "Go to next N'th unread newsgroup on the same level.
5331 If N is negative, search backward instead.
5332 Returns the difference between N and the number of skips actually
5333 done."
5334   (interactive "p")
5335   (gnus-group-next-unread-group n t (gnus-group-group-level))
5336   (gnus-group-position-point))
5337
5338 (defun gnus-group-prev-unread-group-same-level (n)
5339   "Go to next N'th unread newsgroup on the same level.
5340 Returns the difference between N and the number of skips actually
5341 done."
5342   (interactive "p")
5343   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5344   (gnus-group-position-point))
5345
5346 (defun gnus-group-best-unread-group (&optional exclude-group)
5347   "Go to the group with the highest level.
5348 If EXCLUDE-GROUP, do not go to that group."
5349   (interactive)
5350   (goto-char (point-min))
5351   (let ((best 100000)
5352         unread best-point)
5353     (while (not (eobp))
5354       (setq unread (get-text-property (point) 'gnus-unread))
5355       (if (and (numberp unread) (> unread 0))
5356           (progn
5357             (if (and (get-text-property (point) 'gnus-level)
5358                      (< (get-text-property (point) 'gnus-level) best)
5359                      (or (not exclude-group)
5360                          (not (equal exclude-group (gnus-group-group-name)))))
5361                 (progn
5362                   (setq best (get-text-property (point) 'gnus-level))
5363                   (setq best-point (point))))))
5364       (forward-line 1))
5365     (if best-point (goto-char best-point))
5366     (gnus-summary-position-point)
5367     (and best-point (gnus-group-group-name))))
5368
5369 (defun gnus-group-first-unread-group ()
5370   "Go to the first group with unread articles."
5371   (interactive)
5372   (prog1
5373       (let ((opoint (point))
5374             unread)
5375         (goto-char (point-min))
5376         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5377                 (and (numberp unread)   ; Not a topic.
5378                      (not (zerop unread))) ; Has unread articles.
5379                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5380             (point)                     ; Success.
5381           (goto-char opoint)
5382           nil))                         ; Not success.
5383     (gnus-group-position-point)))
5384
5385 (defun gnus-group-enter-server-mode ()
5386   "Jump to the server buffer."
5387   (interactive)
5388   (gnus-enter-server-buffer))
5389
5390 (defun gnus-group-make-group (name &optional method address)
5391   "Add a new newsgroup.
5392 The user will be prompted for a NAME, for a select METHOD, and an
5393 ADDRESS."
5394   (interactive
5395    (cons
5396     (read-string "Group name: ")
5397     (let ((method
5398            (completing-read
5399             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5400             nil t)))
5401       (cond ((assoc method gnus-valid-select-methods)
5402              (list method
5403                    (if (memq 'prompt-address
5404                              (assoc method gnus-valid-select-methods))
5405                        (read-string "Address: ")
5406                      "")))
5407             ((assoc method gnus-server-alist)
5408              (list method))
5409             (t
5410              (list method ""))))))
5411
5412   (save-excursion
5413     (set-buffer gnus-group-buffer)
5414     (let* ((meth (and method (if address (list (intern method) address)
5415                                method)))
5416            (nname (if method (gnus-group-prefixed-name name meth) name))
5417            backend info)
5418       (and (gnus-gethash nname gnus-newsrc-hashtb)
5419            (error "Group %s already exists" nname))
5420       (gnus-group-change-level
5421        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5422        gnus-level-default-subscribed gnus-level-killed
5423        (and (gnus-group-group-name)
5424             (gnus-gethash (gnus-group-group-name)
5425                           gnus-newsrc-hashtb))
5426        t)
5427       (gnus-set-active nname (cons 1 0))
5428       (or (gnus-ephemeral-group-p name)
5429           (gnus-dribble-enter
5430            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5431       (gnus-group-insert-group-line-info nname)
5432
5433       (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5434                                                     nil meth))))
5435                    gnus-valid-select-methods)
5436         (require backend))
5437       (gnus-check-server meth)
5438       (and (gnus-check-backend-function 'request-create-group nname)
5439            (gnus-request-create-group nname))
5440       t)))
5441
5442 (defun gnus-group-delete-group (group &optional force)
5443   "Delete the current group.
5444 If FORCE (the prefix) is non-nil, all the articles in the group will
5445 be deleted.  This is \"deleted\" as in \"removed forever from the face
5446 of the Earth\".  There is no undo."
5447   (interactive
5448    (list (gnus-group-group-name)
5449          current-prefix-arg))
5450   (or group (error "No group to rename"))
5451   (or (gnus-check-backend-function 'request-delete-group group)
5452       (error "This backend does not support group deletion"))
5453   (prog1
5454       (if (not (gnus-yes-or-no-p
5455                 (format
5456                  "Do you really want to delete %s%s? "
5457                  group (if force " and all its contents" ""))))
5458           () ; Whew!
5459         (gnus-message 6 "Deleting group %s..." group)
5460         (if (not (gnus-request-delete-group group force))
5461             (progn
5462               (gnus-message 3 "Couldn't delete group %s" group)
5463               (ding))
5464           (gnus-message 6 "Deleting group %s...done" group)
5465           (gnus-group-goto-group group)
5466           (gnus-group-kill-group 1 t)
5467           (gnus-sethash group nil gnus-active-hashtb)
5468           t))
5469     (gnus-group-position-point)))
5470
5471 (defun gnus-group-rename-group (group new-name)
5472   (interactive
5473    (list
5474     (gnus-group-group-name)
5475     (progn
5476       (or (gnus-check-backend-function
5477            'request-rename-group (gnus-group-group-name))
5478           (error "This backend does not support renaming groups"))
5479       (read-string "New group name: "))))
5480
5481   (or (gnus-check-backend-function 'request-rename-group group)
5482       (error "This backend does not support renaming groups"))
5483
5484   (or group (error "No group to rename"))
5485   (and (string-match "^[ \t]*$" new-name)
5486        (error "Not a valid group name"))
5487
5488   ;; We find the proper prefixed name.
5489   (setq new-name
5490         (gnus-group-prefixed-name
5491          (gnus-group-real-name new-name)
5492          (gnus-info-method (gnus-get-info group))))
5493
5494   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5495   (prog1
5496       (if (not (gnus-request-rename-group group new-name))
5497           (progn
5498             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
5499             (ding))
5500         ;; We rename the group internally by killing it...
5501         (gnus-group-goto-group group)
5502         (gnus-group-kill-group)
5503         ;; ... changing its name ...
5504         (setcar (cdar gnus-list-of-killed-groups) new-name)
5505         ;; ... and then yanking it.  Magic!
5506         (gnus-group-yank-group)
5507         (gnus-set-active new-name (gnus-active group))
5508         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5509         new-name)
5510     (gnus-group-position-point)))
5511
5512 (defun gnus-group-edit-group (group &optional part)
5513   "Edit the group on the current line."
5514   (interactive (list (gnus-group-group-name)))
5515   (let* ((part (or part 'info))
5516          (done-func `(lambda ()
5517                        "Exit editing mode and update the information."
5518                        (interactive)
5519                        (gnus-group-edit-group-done ',part ,group)))
5520          (winconf (current-window-configuration))
5521          info)
5522     (or group (error "No group on current line"))
5523     (or (setq info (gnus-get-info group))
5524         (error "Killed group; can't be edited"))
5525     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5526     (gnus-configure-windows 'edit-group)
5527     (gnus-add-current-to-buffer-list)
5528     (emacs-lisp-mode)
5529     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5530     (use-local-map (copy-keymap emacs-lisp-mode-map))
5531     (local-set-key "\C-c\C-c" done-func)
5532     (make-local-variable 'gnus-prev-winconf)
5533     (setq gnus-prev-winconf winconf)
5534     (erase-buffer)
5535     (insert
5536      (cond
5537       ((eq part 'method)
5538        ";; Type `C-c C-c' after editing the select method.\n\n")
5539       ((eq part 'params)
5540        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5541       ((eq part 'info)
5542        ";; Type `C-c C-c' after editing the group info.\n\n")))
5543     (insert
5544      (pp-to-string
5545       (cond ((eq part 'method)
5546              (or (gnus-info-method info) "native"))
5547             ((eq part 'params)
5548              (gnus-info-params info))
5549             (t info)))
5550      "\n")))
5551
5552 (defun gnus-group-edit-group-method (group)
5553   "Edit the select method of GROUP."
5554   (interactive (list (gnus-group-group-name)))
5555   (gnus-group-edit-group group 'method))
5556
5557 (defun gnus-group-edit-group-parameters (group)
5558   "Edit the group parameters of GROUP."
5559   (interactive (list (gnus-group-group-name)))
5560   (gnus-group-edit-group group 'params))
5561
5562 (defun gnus-group-edit-group-done (part group)
5563   "Get info from buffer, update variables and jump to the group buffer."
5564   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5565   (goto-char (point-min))
5566   (let* ((form (read (current-buffer)))
5567          (winconf gnus-prev-winconf)
5568          (method (cond ((eq part 'info) (nth 4 form))
5569                        ((eq part 'method) form)
5570                        (t nil)))
5571          (info (cond ((eq part 'info) form)
5572                      ((eq part 'method) (gnus-get-info group))
5573                      (t nil)))
5574          (new-group (if info
5575                       (if (or (not method)
5576                               (gnus-server-equal
5577                                gnus-select-method method))
5578                           (gnus-group-real-name (car info))
5579                         (gnus-group-prefixed-name
5580                          (gnus-group-real-name (car info)) method))
5581                       nil)))
5582     (when (and new-group
5583                (not (equal new-group group)))
5584       (when (gnus-group-goto-group group)
5585         (gnus-group-kill-group 1))
5586       (gnus-activate-group new-group))
5587     ;; Set the info.
5588     (if (and info new-group)
5589         (progn
5590           (setq info (gnus-copy-sequence info))
5591           (setcar info new-group)
5592           (unless (gnus-server-equal method "native")
5593             (unless (nthcdr 3 info)
5594               (nconc info (list nil nil)))
5595             (unless (nthcdr 4 info)
5596               (nconc info (list nil)))
5597             (gnus-info-set-method info method))
5598           (gnus-group-set-info info))
5599       (gnus-group-set-info form (or new-group group) part))
5600     (kill-buffer (current-buffer))
5601     (and winconf (set-window-configuration winconf))
5602     (set-buffer gnus-group-buffer)
5603     (gnus-group-update-group (or new-group group))
5604     (gnus-group-position-point)))
5605
5606 (defun gnus-group-make-help-group ()
5607   "Create the Gnus documentation group."
5608   (interactive)
5609   (let ((path load-path)
5610         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5611         file dir)
5612     (and (gnus-gethash name gnus-newsrc-hashtb)
5613          (error "Documentation group already exists"))
5614     (while path
5615       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5616             file nil)
5617       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5618                 (file-exists-p
5619                  (setq file (concat (file-name-directory
5620                                      (directory-file-name dir))
5621                                     "etc/gnus-tut.txt"))))
5622         (setq path nil)))
5623     (if (not file)
5624         (message "Couldn't find doc group")
5625       (gnus-group-make-group
5626        (gnus-group-real-name name)
5627        (list 'nndoc "gnus-help"
5628              (list 'nndoc-address file)
5629              (list 'nndoc-article-type 'mbox)))))
5630   (gnus-group-position-point))
5631
5632 (defun gnus-group-make-doc-group (file type)
5633   "Create a group that uses a single file as the source."
5634   (interactive
5635    (list (read-file-name "File name: ")
5636          (and current-prefix-arg 'ask)))
5637   (when (eq type 'ask)
5638     (let ((err "")
5639           char found)
5640       (while (not found)
5641         (message
5642          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5643          err)
5644         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5645                           ((= char ?b) 'babyl)
5646                           ((= char ?d) 'digest)
5647                           ((= char ?f) 'forward)
5648                           ((= char ?a) 'mmfd)
5649                           (t (setq err (format "%c unknown. " char))
5650                              nil))))
5651       (setq type found)))
5652   (let* ((file (expand-file-name file))
5653          (name (gnus-generate-new-group-name
5654                 (gnus-group-prefixed-name
5655                  (file-name-nondirectory file) '(nndoc "")))))
5656     (gnus-group-make-group
5657      (gnus-group-real-name name)
5658      (list 'nndoc (file-name-nondirectory file)
5659            (list 'nndoc-address file)
5660            (list 'nndoc-article-type (or type 'guess))))
5661     (forward-line -1)
5662     (gnus-group-position-point)))
5663
5664 (defun gnus-group-make-archive-group (&optional all)
5665   "Create the (ding) Gnus archive group of the most recent articles.
5666 Given a prefix, create a full group."
5667   (interactive "P")
5668   (let ((group (gnus-group-prefixed-name
5669                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5670     (and (gnus-gethash group gnus-newsrc-hashtb)
5671          (error "Archive group already exists"))
5672     (gnus-group-make-group
5673      (gnus-group-real-name group)
5674      (list 'nndir (if all "hpc" "edu")
5675            (list 'nndir-directory
5676                  (if all gnus-group-archive-directory
5677                    gnus-group-recent-archive-directory)))))
5678   (forward-line -1)
5679   (gnus-group-position-point))
5680
5681 (defun gnus-group-make-directory-group (dir)
5682   "Create an nndir group.
5683 The user will be prompted for a directory.  The contents of this
5684 directory will be used as a newsgroup.  The directory should contain
5685 mail messages or news articles in files that have numeric names."
5686   (interactive
5687    (list (read-file-name "Create group from directory: ")))
5688   (or (file-exists-p dir) (error "No such directory"))
5689   (or (file-directory-p dir) (error "Not a directory"))
5690   (let ((ext "")
5691         (i 0)
5692         group)
5693     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5694       (setq group
5695             (gnus-group-prefixed-name
5696              (concat (file-name-as-directory (directory-file-name dir))
5697                      ext)
5698              '(nndir "")))
5699       (setq ext (format "<%d>" (setq i (1+ i)))))
5700     (gnus-group-make-group
5701      (gnus-group-real-name group)
5702      (list 'nndir group (list 'nndir-directory dir))))
5703   (forward-line -1)
5704   (gnus-group-position-point))
5705
5706 (defun gnus-group-make-kiboze-group (group address scores)
5707   "Create an nnkiboze group.
5708 The user will be prompted for a name, a regexp to match groups, and
5709 score file entries for articles to include in the group."
5710   (interactive
5711    (list
5712     (read-string "nnkiboze group name: ")
5713     (read-string "Source groups (regexp): ")
5714     (let ((headers (mapcar (lambda (group) (list group))
5715                            '("subject" "from" "number" "date" "message-id"
5716                              "references" "chars" "lines" "xref"
5717                              "followup" "all" "body" "head")))
5718           scores header regexp regexps)
5719       (while (not (equal "" (setq header (completing-read
5720                                           "Match on header: " headers nil t))))
5721         (setq regexps nil)
5722         (while (not (equal "" (setq regexp (read-string
5723                                             (format "Match on %s (string): "
5724                                                     header)))))
5725           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5726         (setq scores (cons (cons header regexps) scores)))
5727       scores)))
5728   (gnus-group-make-group group "nnkiboze" address)
5729   (save-excursion
5730     (gnus-set-work-buffer)
5731     (let (emacs-lisp-mode-hook)
5732       (pp scores (current-buffer)))
5733     (write-region (point-min) (point-max)
5734                   (gnus-score-file-name (concat "nnkiboze:" group))))
5735   (forward-line -1)
5736   (gnus-group-position-point))
5737
5738 (defun gnus-group-add-to-virtual (n vgroup)
5739   "Add the current group to a virtual group."
5740   (interactive
5741    (list current-prefix-arg
5742          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5743                           "nnvirtual:")))
5744   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5745       (error "%s is not an nnvirtual group" vgroup))
5746   (let* ((groups (gnus-group-process-prefix n))
5747          (method (gnus-info-method (gnus-get-info vgroup))))
5748     (setcar (cdr method)
5749             (concat
5750              (nth 1 method) "\\|"
5751              (mapconcat
5752               (lambda (s)
5753                 (gnus-group-remove-mark s)
5754                 (concat "\\(^" (regexp-quote s) "$\\)"))
5755               groups "\\|"))))
5756   (gnus-group-position-point))
5757
5758 (defun gnus-group-make-empty-virtual (group)
5759   "Create a new, fresh, empty virtual group."
5760   (interactive "sCreate new, empty virtual group: ")
5761   (let* ((method (list 'nnvirtual "^$"))
5762          (pgroup (gnus-group-prefixed-name group method)))
5763     ;; Check whether it exists already.
5764     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5765          (error "Group %s already exists." pgroup))
5766     ;; Subscribe the new group after the group on the current line.
5767     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5768     (gnus-group-update-group pgroup)
5769     (forward-line -1)
5770     (gnus-group-position-point)))
5771
5772 (defun gnus-group-enter-directory (dir)
5773   "Enter an ephemeral nneething group."
5774   (interactive "DDirectory to read: ")
5775   (let* ((method (list 'nneething dir))
5776          (leaf (gnus-group-prefixed-name
5777                 (file-name-nondirectory (directory-file-name dir))
5778                 method))
5779          (name (gnus-generate-new-group-name leaf)))
5780     (let ((nneething-read-only t))
5781       (or (gnus-group-read-ephemeral-group
5782            name method t
5783            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5784                                       'summary 'group)))
5785           (error "Couldn't enter %s" dir)))))
5786
5787 ;; Group sorting commands
5788 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5789
5790 (defun gnus-group-sort-groups (func &optional reverse)
5791   "Sort the group buffer according to FUNC.
5792 If REVERSE, reverse the sorting order."
5793   (interactive (list gnus-group-sort-function
5794                      current-prefix-arg))
5795   (let ((func (cond 
5796                ((not (listp func)) func)
5797                ((null func) func)
5798                ((= 1 (length func)) (car func))
5799                (t `(lambda (t1 t2)
5800                      ,(gnus-make-sort-function 
5801                        (reverse func)))))))
5802     ;; We peel off the dummy group from the alist.
5803     (when func
5804       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5805         (pop gnus-newsrc-alist))
5806       ;; Do the sorting.
5807       (setq gnus-newsrc-alist
5808             (sort gnus-newsrc-alist func))
5809       (when reverse
5810         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5811       ;; Regenerate the hash table.
5812       (gnus-make-hashtable-from-newsrc-alist)
5813       (gnus-group-list-groups))))
5814
5815 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5816   "Sort the group buffer alphabetically by group name.
5817 If REVERSE, sort in reverse order."
5818   (interactive "P")
5819   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5820
5821 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5822   "Sort the group buffer by number of unread articles.
5823 If REVERSE, sort in reverse order."
5824   (interactive "P")
5825   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5826
5827 (defun gnus-group-sort-groups-by-level (&optional reverse)
5828   "Sort the group buffer by group level.
5829 If REVERSE, sort in reverse order."
5830   (interactive "P")
5831   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5832
5833 (defun gnus-group-sort-groups-by-score (&optional reverse)
5834   "Sort the group buffer by group score.
5835 If REVERSE, sort in reverse order."
5836   (interactive "P")
5837   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5838
5839 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5840   "Sort the group buffer by group rank.
5841 If REVERSE, sort in reverse order."
5842   (interactive "P")
5843   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5844
5845 (defun gnus-group-sort-groups-by-method (&optional reverse)
5846   "Sort the group buffer alphabetically by backend name.
5847 If REVERSE, sort in reverse order."
5848   (interactive "P")
5849   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5850
5851 (defun gnus-group-sort-by-alphabet (info1 info2)
5852   "Sort alphabetically."
5853   (string< (gnus-info-group info1) (gnus-info-group info2)))
5854
5855 (defun gnus-group-sort-by-unread (info1 info2)
5856   "Sort by number of unread articles."
5857   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5858         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5859     (< (or (and (numberp n1) n1) 0)
5860        (or (and (numberp n2) n2) 0))))
5861
5862 (defun gnus-group-sort-by-level (info1 info2)
5863   "Sort by level."
5864   (< (gnus-info-level info1) (gnus-info-level info2)))
5865
5866 (defun gnus-group-sort-by-method (info1 info2)
5867   "Sort alphabetically by backend name."
5868   (string< (symbol-name (car (gnus-find-method-for-group
5869                               (gnus-info-group info1) info1)))
5870            (symbol-name (car (gnus-find-method-for-group
5871                               (gnus-info-group info2) info2)))))
5872
5873 (defun gnus-group-sort-by-score (info1 info2)
5874   "Sort by group score."
5875   (< (gnus-info-score info1) (gnus-info-score info2)))
5876
5877 (defun gnus-group-sort-by-rank (info1 info2)
5878   "Sort by level and score."
5879   (let ((level1 (gnus-info-level info1))
5880         (level2 (gnus-info-level info2)))
5881     (or (< level1 level2)
5882         (and (= level1 level2)
5883              (< (gnus-info-score info1) (gnus-info-score info2))))))
5884
5885 ;; Group catching up.
5886
5887 (defun gnus-group-clear-data (n)
5888   "Clear all marks and read ranges from the current group."
5889   (interactive "P")
5890   (let ((groups (gnus-group-process-prefix n))
5891         group info)
5892     (while (setq group (pop groups))
5893       (setq info (gnus-get-info group))
5894       (gnus-info-set-read info nil)
5895       (when (gnus-info-marks info)
5896         (gnus-info-set-marks info nil))
5897       (gnus-get-unread-articles-in-group info (gnus-active group) t)
5898       (when (gnus-group-goto-group group)
5899         (gnus-group-remove-mark group)
5900         (gnus-group-update-group-line)))))
5901
5902 (defun gnus-group-catchup-current (&optional n all)
5903   "Mark all articles not marked as unread in current newsgroup as read.
5904 If prefix argument N is numeric, the ARG next newsgroups will be
5905 caught up.  If ALL is non-nil, marked articles will also be marked as
5906 read.  Cross references (Xref: header) of articles are ignored.
5907 The difference between N and actual number of newsgroups that were
5908 caught up is returned."
5909   (interactive "P")
5910   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5911                gnus-expert-user
5912                (gnus-y-or-n-p
5913                 (if all
5914                     "Do you really want to mark all articles as read? "
5915                   "Mark all unread articles as read? "))))
5916       n
5917     (let ((groups (gnus-group-process-prefix n))
5918           (ret 0))
5919       (while groups
5920         ;; Virtual groups have to be given special treatment.
5921         (let ((method (gnus-find-method-for-group (car groups))))
5922           (if (eq 'nnvirtual (car method))
5923               (nnvirtual-catchup-group
5924                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5925         (gnus-group-remove-mark (car groups))
5926         (if (prog1
5927                 (gnus-group-goto-group (car groups))
5928               (gnus-group-catchup (car groups) all))
5929             (gnus-group-update-group-line)
5930           (setq ret (1+ ret)))
5931         (setq groups (cdr groups)))
5932       (gnus-group-next-unread-group 1)
5933       ret)))
5934
5935 (defun gnus-group-catchup-current-all (&optional n)
5936   "Mark all articles in current newsgroup as read.
5937 Cross references (Xref: header) of articles are ignored."
5938   (interactive "P")
5939   (gnus-group-catchup-current n 'all))
5940
5941 (defun gnus-group-catchup (group &optional all)
5942   "Mark all articles in GROUP as read.
5943 If ALL is non-nil, all articles are marked as read.
5944 The return value is the number of articles that were marked as read,
5945 or nil if no action could be taken."
5946   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5947          (num (car entry)))
5948     ;; Do the updating only if the newsgroup isn't killed.
5949     (if (not (numberp (car entry)))
5950         (gnus-message 1 "Can't catch up; non-active group")
5951       ;; Do auto-expirable marks if that's required.
5952       (when (gnus-group-auto-expirable-p group)
5953         (gnus-add-marked-articles
5954          group 'expire (gnus-list-of-unread-articles group))
5955         (when all
5956           (let ((marks (nth 3 (nth 2 entry))))
5957             (gnus-add-marked-articles
5958              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5959             (gnus-add-marked-articles
5960              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5961       (when entry
5962         (gnus-update-read-articles group nil)
5963         ;; Also nix out the lists of marks and dormants.
5964         (when all
5965           (gnus-add-marked-articles group 'tick nil nil 'force)
5966           (gnus-add-marked-articles group 'dormant nil nil 'force))
5967         (run-hooks 'gnus-group-catchup-group-hook)
5968         num))))
5969
5970 (defun gnus-group-expire-articles (&optional n)
5971   "Expire all expirable articles in the current newsgroup."
5972   (interactive "P")
5973   (let ((groups (gnus-group-process-prefix n))
5974         group)
5975     (unless groups
5976       (error "No groups to expire"))
5977     (while (setq group (pop groups))
5978       (gnus-group-remove-mark group)
5979       (when (gnus-check-backend-function 'request-expire-articles group)
5980         (gnus-message 6 "Expiring articles in %s..." group)
5981         (let* ((info (gnus-get-info group))
5982                (expirable (if (gnus-group-total-expirable-p group)
5983                               (cons nil (gnus-list-of-read-articles group))
5984                             (assq 'expire (gnus-info-marks info))))
5985                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
5986           (when expirable
5987             (setcdr
5988              expirable
5989              (gnus-compress-sequence
5990               (if expiry-wait
5991                   ;; We set the expiry variables to the groupp
5992                   ;; parameter. 
5993                   (let ((nnmail-expiry-wait-function nil)
5994                         (nnmail-expiry-wait expiry-wait))
5995                     (gnus-request-expire-articles
5996                      (gnus-uncompress-sequence (cdr expirable)) group))
5997                 ;; Just expire using the normal expiry values.
5998                 (gnus-request-expire-articles
5999                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6000           (gnus-message 6 "Expiring articles in %s...done" group)))
6001       (gnus-group-position-point))))
6002
6003 (defun gnus-group-expire-all-groups ()
6004   "Expire all expirable articles in all newsgroups."
6005   (interactive)
6006   (save-excursion
6007     (gnus-message 5 "Expiring...")
6008     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6009                                      (cdr gnus-newsrc-alist))))
6010       (gnus-group-expire-articles nil)))
6011   (gnus-group-position-point)
6012   (gnus-message 5 "Expiring...done"))
6013
6014 (defun gnus-group-set-current-level (n level)
6015   "Set the level of the next N groups to LEVEL."
6016   (interactive
6017    (list
6018     current-prefix-arg
6019     (string-to-int
6020      (let ((s (read-string
6021                (format "Level (default %s): "
6022                        (or (gnus-group-group-level) 
6023                            gnus-level-default-subscribed)))))
6024        (if (string-match "^\\s-*$" s)
6025            (int-to-string (or (gnus-group-group-level) 
6026                               gnus-level-default-subscribed))
6027          s)))))
6028   (or (and (>= level 1) (<= level gnus-level-killed))
6029       (error "Illegal level: %d" level))
6030   (let ((groups (gnus-group-process-prefix n))
6031         group)
6032     (while (setq group (pop groups))
6033       (gnus-group-remove-mark group)
6034       (gnus-message 6 "Changed level of %s from %d to %d"
6035                     group (or (gnus-group-group-level) gnus-level-killed)
6036                     level)
6037       (gnus-group-change-level
6038        group level (or (gnus-group-group-level) gnus-level-killed))
6039       (gnus-group-update-group-line)))
6040   (gnus-group-position-point))
6041
6042 (defun gnus-group-unsubscribe-current-group (&optional n)
6043   "Toggle subscription of the current group.
6044 If given numerical prefix, toggle the N next groups."
6045   (interactive "P")
6046   (let ((groups (gnus-group-process-prefix n))
6047         group)
6048     (while groups
6049       (setq group (car groups)
6050             groups (cdr groups))
6051       (gnus-group-remove-mark group)
6052       (gnus-group-unsubscribe-group
6053        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6054                  gnus-level-default-unsubscribed
6055                gnus-level-default-subscribed) t)
6056       (gnus-group-update-group-line))
6057     (gnus-group-next-group 1)))
6058
6059 (defun gnus-group-unsubscribe-group (group &optional level silent)
6060   "Toggle subscription to GROUP.
6061 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6062 group line."
6063   (interactive
6064    (list (completing-read
6065           "Group: " gnus-active-hashtb nil
6066           (memq gnus-select-method gnus-have-read-active-file))))
6067   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6068     (cond
6069      ((string-match "^[ \t]$" group)
6070       (error "Empty group name"))
6071      (newsrc
6072       ;; Toggle subscription flag.
6073       (gnus-group-change-level
6074        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6075                                       gnus-level-subscribed)
6076                                   (1+ gnus-level-subscribed)
6077                                 gnus-level-default-subscribed)))
6078       (unless silent
6079         (gnus-group-update-group group)))
6080      ((and (stringp group)
6081            (or (not (memq gnus-select-method gnus-have-read-active-file))
6082                (gnus-active group)))
6083       ;; Add new newsgroup.
6084       (gnus-group-change-level
6085        group
6086        (if level level gnus-level-default-subscribed)
6087        (or (and (member group gnus-zombie-list)
6088                 gnus-level-zombie)
6089            gnus-level-killed)
6090        (and (gnus-group-group-name)
6091             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6092       (unless silent
6093         (gnus-group-update-group group)))
6094      (t (error "No such newsgroup: %s" group)))
6095     (gnus-group-position-point)))
6096
6097 (defun gnus-group-transpose-groups (n)
6098   "Move the current newsgroup up N places.
6099 If given a negative prefix, move down instead.  The difference between
6100 N and the number of steps taken is returned."
6101   (interactive "p")
6102   (or (gnus-group-group-name)
6103       (error "No group on current line"))
6104   (gnus-group-kill-group 1)
6105   (prog1
6106       (forward-line (- n))
6107     (gnus-group-yank-group)
6108     (gnus-group-position-point)))
6109
6110 (defun gnus-group-kill-all-zombies ()
6111   "Kill all zombie newsgroups."
6112   (interactive)
6113   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6114   (setq gnus-zombie-list nil)
6115   (gnus-group-list-groups))
6116
6117 (defun gnus-group-kill-region (begin end)
6118   "Kill newsgroups in current region (excluding current point).
6119 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6120   (interactive "r")
6121   (let ((lines
6122          ;; Count lines.
6123          (save-excursion
6124            (count-lines
6125             (progn
6126               (goto-char begin)
6127               (beginning-of-line)
6128               (point))
6129             (progn
6130               (goto-char end)
6131               (beginning-of-line)
6132               (point))))))
6133     (goto-char begin)
6134     (beginning-of-line)                 ;Important when LINES < 1
6135     (gnus-group-kill-group lines)))
6136
6137 (defun gnus-group-kill-group (&optional n discard)
6138   "Kill the next N groups.
6139 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6140 However, only groups that were alive can be yanked; already killed
6141 groups or zombie groups can't be yanked.
6142 The return value is the name of the group that was killed, or a list
6143 of groups killed."
6144   (interactive "P")
6145   (let ((buffer-read-only nil)
6146         (groups (gnus-group-process-prefix n))
6147         group entry level out)
6148     (if (< (length groups) 10)
6149         ;; This is faster when there are few groups.
6150         (while groups
6151           (push (setq group (pop groups)) out)
6152           (gnus-group-remove-mark group)
6153           (setq level (gnus-group-group-level))
6154           (gnus-delete-line)
6155           (when (and (not discard)
6156                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6157             (push (cons (car entry) (nth 2 entry))
6158                   gnus-list-of-killed-groups))
6159           (gnus-group-change-level
6160            (if entry entry group) gnus-level-killed (if entry nil level)))
6161       ;; If there are lots and lots of groups to be killed, we use
6162       ;; this thing instead.
6163       (let (entry)
6164         (setq groups (nreverse groups))
6165         (while groups
6166           (gnus-group-remove-mark (setq group (pop groups)))
6167           (gnus-delete-line)
6168           (cond
6169            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6170             (push (cons (car entry) (nth 2 entry))
6171                   gnus-list-of-killed-groups)
6172             (setcdr (cdr entry) (cdddr entry)))
6173            ((member group gnus-zombie-list)
6174             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6175         (gnus-make-hashtable-from-newsrc-alist)))
6176
6177     (gnus-group-position-point)
6178     (if (< (length out) 2) (car out) (nreverse out))))
6179
6180 (defun gnus-group-yank-group (&optional arg)
6181   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6182 inserting it before the current newsgroup.  The numeric ARG specifies
6183 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6184 is returned, or (if several groups are yanked) a list of yanked groups
6185 is returned."
6186   (interactive "p")
6187   (setq arg (or arg 1))
6188   (let (info group prev out)
6189     (while (>= (decf arg) 0)
6190       (if (not (setq info (pop gnus-list-of-killed-groups)))
6191           (error "No more newsgroups to yank"))
6192       (push (setq group (nth 1 info)) out)
6193       ;; Find which newsgroup to insert this one before - search
6194       ;; backward until something suitable is found.  If there are no
6195       ;; other newsgroups in this buffer, just make this newsgroup the
6196       ;; first newsgroup.
6197       (setq prev (gnus-group-group-name))
6198       (gnus-group-change-level
6199        info (gnus-info-level (cdr info)) gnus-level-killed
6200        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6201        t)
6202       (gnus-group-insert-group-line-info group))
6203     (forward-line -1)
6204     (gnus-group-position-point)
6205     (if (< (length out) 2) (car out) (nreverse out))))
6206
6207 (defun gnus-group-kill-level (level)
6208   "Kill all groups that is on a certain LEVEL."
6209   (interactive "nKill all groups on level: ")
6210   (cond
6211    ((= level gnus-level-zombie)
6212     (setq gnus-killed-list
6213           (nconc gnus-zombie-list gnus-killed-list))
6214     (setq gnus-zombie-list nil))
6215    ((and (< level gnus-level-zombie)
6216          (> level 0)
6217          (or gnus-expert-user
6218              (gnus-yes-or-no-p
6219               (format
6220                "Do you really want to kill all groups on level %d? "
6221                level))))
6222     (let* ((prev gnus-newsrc-alist)
6223            (alist (cdr prev)))
6224       (while alist
6225         (if (= (gnus-info-level level) level)
6226             (setcdr prev (cdr alist))
6227           (setq prev alist))
6228         (setq alist (cdr alist)))
6229       (gnus-make-hashtable-from-newsrc-alist)
6230       (gnus-group-list-groups)))
6231    (t
6232     (error "Can't kill; illegal level: %d" level))))
6233
6234 (defun gnus-group-list-all-groups (&optional arg)
6235   "List all newsgroups with level ARG or lower.
6236 Default is gnus-level-unsubscribed, which lists all subscribed and most
6237 unsubscribed groups."
6238   (interactive "P")
6239   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6240
6241 ;; Redefine this to list ALL killed groups if prefix arg used.
6242 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6243 (defun gnus-group-list-killed (&optional arg)
6244   "List all killed newsgroups in the group buffer.
6245 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6246 entail asking the server for the groups."
6247   (interactive "P")
6248   ;; Find all possible killed newsgroups if arg.
6249   (when arg
6250     ;; First make sure active file has been read.
6251     (unless gnus-have-read-active-file
6252       (let ((gnus-read-active-file t))
6253         (gnus-read-active-file)))
6254     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6255     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6256     (mapatoms
6257      (lambda (sym)
6258        (let ((groups 0)
6259              (group (symbol-name sym)))
6260          (if (or (null group)
6261                  (gnus-gethash group gnus-killed-hashtb)
6262                  (gnus-gethash group gnus-newsrc-hashtb))
6263              ()
6264            (let ((do-sub (gnus-matches-options-n group)))
6265              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6266                  ()
6267                (setq groups (1+ groups))
6268                (setq gnus-killed-list
6269                      (cons group gnus-killed-list))
6270                (gnus-sethash group group gnus-killed-hashtb))))))
6271      gnus-active-hashtb))
6272   (if (not gnus-killed-list)
6273       (gnus-message 6 "No killed groups")
6274     (let (gnus-group-list-mode)
6275       (funcall gnus-group-prepare-function
6276                gnus-level-killed t gnus-level-killed))
6277     (goto-char (point-min)))
6278   (gnus-group-position-point))
6279
6280 (defun gnus-group-list-zombies ()
6281   "List all zombie newsgroups in the group buffer."
6282   (interactive)
6283   (if (not gnus-zombie-list)
6284       (gnus-message 6 "No zombie groups")
6285     (let (gnus-group-list-mode)
6286       (funcall gnus-group-prepare-function
6287                gnus-level-zombie t gnus-level-zombie))
6288     (goto-char (point-min)))
6289   (gnus-group-position-point))
6290
6291 (defun gnus-group-list-active ()
6292   "List all groups that are available from the server(s)."
6293   (interactive)
6294   ;; First we make sure that we have really read the active file.
6295   (unless gnus-have-read-active-file
6296     (let ((gnus-read-active-file t))
6297       (gnus-read-active-file)))
6298   ;; Find all groups and sort them.
6299   (let ((groups
6300          (sort
6301           (let (list)
6302             (mapatoms
6303              (lambda (sym)
6304                (and (symbol-value sym)
6305                     (setq list (cons (symbol-name sym) list))))
6306              gnus-active-hashtb)
6307             list)
6308           'string<))
6309         (buffer-read-only nil))
6310     (erase-buffer)
6311     (while groups
6312       (gnus-group-insert-group-line-info (pop groups)))
6313     (goto-char (point-min))))
6314
6315 (defun gnus-activate-all-groups (level)
6316   "Activate absolutely all groups."
6317   (interactive (list 7))
6318   (let ((gnus-activate-level level)
6319         (gnus-activate-foreign-newsgroups level))
6320     (gnus-group-get-new-news)))
6321
6322 (defun gnus-group-get-new-news (&optional arg)
6323   "Get newly arrived articles.
6324 If ARG is a number, it specifies which levels you are interested in
6325 re-scanning.  If ARG is non-nil and not a number, this will force
6326 \"hard\" re-reading of the active files from all servers."
6327   (interactive "P")
6328   (run-hooks 'gnus-get-new-news-hook)
6329   ;; We might read in new NoCeM messages here.
6330   (when (and gnus-use-nocem 
6331              (null arg))
6332     (gnus-nocem-scan-groups))
6333   ;; If ARG is not a number, then we read the active file.
6334   (when (and arg (not (numberp arg)))
6335     (let ((gnus-read-active-file t))
6336       (gnus-read-active-file))
6337     (setq arg nil))
6338
6339   (setq arg (gnus-group-default-level arg t))
6340   (if (and gnus-read-active-file (not arg))
6341       (progn
6342         (gnus-read-active-file)
6343         (gnus-get-unread-articles arg))
6344     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6345       (gnus-get-unread-articles arg)))
6346   (run-hooks 'gnus-after-getting-new-news-hook)
6347   (gnus-group-list-groups))
6348
6349 (defun gnus-group-get-new-news-this-group (&optional n)
6350   "Check for newly arrived news in the current group (and the N-1 next groups).
6351 The difference between N and the number of newsgroup checked is returned.
6352 If N is negative, this group and the N-1 previous groups will be checked."
6353   (interactive "P")
6354   (let* ((groups (gnus-group-process-prefix n))
6355          (ret (if (numberp n) (- n (length groups)) 0))
6356          group)
6357     (while groups
6358       (setq group (car groups)
6359             groups (cdr groups))
6360       (gnus-group-remove-mark group)
6361       (if (and group (gnus-activate-group group 'scan))
6362           (progn
6363             (gnus-get-unread-articles-in-group
6364              (gnus-get-info group) (gnus-active group) t)
6365             (gnus-close-group group)
6366             (gnus-group-update-group group))
6367         (ding)
6368         (gnus-message 3 "%s error: %s" group (gnus-status-message group))))
6369     (when gnus-goto-next-group-when-activating
6370       (gnus-group-next-unread-group 1 t))
6371     (gnus-summary-position-point)
6372     ret))
6373
6374 (defun gnus-group-fetch-faq (group &optional faq-dir)
6375   "Fetch the FAQ for the current group."
6376   (interactive
6377    (list
6378     (gnus-group-real-name (gnus-group-group-name))
6379     (cond (current-prefix-arg
6380            (completing-read
6381             "Faq dir: " (and (listp gnus-group-faq-directory)
6382                              gnus-group-faq-directory))))))
6383   (or faq-dir
6384       (setq faq-dir (if (listp gnus-group-faq-directory)
6385                         (car gnus-group-faq-directory)
6386                       gnus-group-faq-directory)))
6387   (or group (error "No group name given"))
6388   (let ((file (concat (file-name-as-directory faq-dir)
6389                       (gnus-group-real-name group))))
6390     (if (not (file-exists-p file))
6391         (error "No such file: %s" file)
6392       (find-file file))))
6393
6394 (defun gnus-group-describe-group (force &optional group)
6395   "Display a description of the current newsgroup."
6396   (interactive (list current-prefix-arg (gnus-group-group-name)))
6397   (and force (setq gnus-description-hashtb nil))
6398   (let ((method (gnus-find-method-for-group group))
6399         desc)
6400     (or group (error "No group name given"))
6401     (and (or (and gnus-description-hashtb
6402                   ;; We check whether this group's method has been
6403                   ;; queried for a description file.
6404                   (gnus-gethash
6405                    (gnus-group-prefixed-name "" method)
6406                    gnus-description-hashtb))
6407              (setq desc (gnus-group-get-description group))
6408              (gnus-read-descriptions-file method))
6409          (message
6410           (or desc (gnus-gethash group gnus-description-hashtb)
6411               "No description available")))))
6412
6413 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6414 (defun gnus-group-describe-all-groups (&optional force)
6415   "Pop up a buffer with descriptions of all newsgroups."
6416   (interactive "P")
6417   (and force (setq gnus-description-hashtb nil))
6418   (if (not (or gnus-description-hashtb
6419                (gnus-read-all-descriptions-files)))
6420       (error "Couldn't request descriptions file"))
6421   (let ((buffer-read-only nil)
6422         b)
6423     (erase-buffer)
6424     (mapatoms
6425      (lambda (group)
6426        (setq b (point))
6427        (insert (format "      *: %-20s %s\n" (symbol-name group)
6428                        (symbol-value group)))
6429        (add-text-properties
6430         b (1+ b) (list 'gnus-group group
6431                        'gnus-unread t 'gnus-marked nil
6432                        'gnus-level (1+ gnus-level-subscribed))))
6433      gnus-description-hashtb)
6434     (goto-char (point-min))
6435     (gnus-group-position-point)))
6436
6437 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6438 (defun gnus-group-apropos (regexp &optional search-description)
6439   "List all newsgroups that have names that match a regexp."
6440   (interactive "sGnus apropos (regexp): ")
6441   (let ((prev "")
6442         (obuf (current-buffer))
6443         groups des)
6444     ;; Go through all newsgroups that are known to Gnus.
6445     (mapatoms
6446      (lambda (group)
6447        (and (symbol-name group)
6448             (string-match regexp (symbol-name group))
6449             (setq groups (cons (symbol-name group) groups))))
6450      gnus-active-hashtb)
6451     ;; Also go through all descriptions that are known to Gnus.
6452     (when search-description
6453       (mapatoms
6454        (lambda (group)
6455          (and (string-match regexp (symbol-value group))
6456               (gnus-active (symbol-name group))
6457               (setq groups (cons (symbol-name group) groups))))
6458        gnus-description-hashtb))
6459     (if (not groups)
6460         (gnus-message 3 "No groups matched \"%s\"." regexp)
6461       ;; Print out all the groups.
6462       (save-excursion
6463         (pop-to-buffer "*Gnus Help*")
6464         (buffer-disable-undo (current-buffer))
6465         (erase-buffer)
6466         (setq groups (sort groups 'string<))
6467         (while groups
6468           ;; Groups may be entered twice into the list of groups.
6469           (if (not (string= (car groups) prev))
6470               (progn
6471                 (insert (setq prev (car groups)) "\n")
6472                 (if (and gnus-description-hashtb
6473                          (setq des (gnus-gethash (car groups)
6474                                                  gnus-description-hashtb)))
6475                     (insert "  " des "\n"))))
6476           (setq groups (cdr groups)))
6477         (goto-char (point-min))))
6478     (pop-to-buffer obuf)))
6479
6480 (defun gnus-group-description-apropos (regexp)
6481   "List all newsgroups that have names or descriptions that match a regexp."
6482   (interactive "sGnus description apropos (regexp): ")
6483   (if (not (or gnus-description-hashtb
6484                (gnus-read-all-descriptions-files)))
6485       (error "Couldn't request descriptions file"))
6486   (gnus-group-apropos regexp t))
6487
6488 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6489 (defun gnus-group-list-matching (level regexp &optional all lowest)
6490   "List all groups with unread articles that match REGEXP.
6491 If the prefix LEVEL is non-nil, it should be a number that says which
6492 level to cut off listing groups.
6493 If ALL, also list groups with no unread articles.
6494 If LOWEST, don't list groups with level lower than LOWEST."
6495   (interactive "P\nsList newsgroups matching: ")
6496   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6497                            all (or lowest 1) regexp)
6498   (goto-char (point-min))
6499   (gnus-group-position-point))
6500
6501 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6502   "List all groups that match REGEXP.
6503 If the prefix LEVEL is non-nil, it should be a number that says which
6504 level to cut off listing groups.
6505 If LOWEST, don't list groups with level lower than LOWEST."
6506   (interactive "P\nsList newsgroups matching: ")
6507   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6508
6509 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6510 (defun gnus-group-save-newsrc (&optional force)
6511   "Save the Gnus startup files.
6512 If FORCE, force saving whether it is necessary or not."
6513   (interactive "P")
6514   (gnus-save-newsrc-file force))
6515
6516 (defun gnus-group-restart (&optional arg)
6517   "Force Gnus to read the .newsrc file."
6518   (interactive "P")
6519   (when (gnus-yes-or-no-p
6520          (format "Are you sure you want to read %s? "
6521                  gnus-current-startup-file))
6522     (gnus-save-newsrc-file)
6523     (gnus-setup-news 'force)
6524     (gnus-group-list-groups arg)))
6525
6526 (defun gnus-group-read-init-file ()
6527   "Read the Gnus elisp init file."
6528   (interactive)
6529   (gnus-read-init-file))
6530
6531 (defun gnus-group-check-bogus-groups (&optional silent)
6532   "Check bogus newsgroups.
6533 If given a prefix, don't ask for confirmation before removing a bogus
6534 group."
6535   (interactive "P")
6536   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6537   (gnus-group-list-groups))
6538
6539 (defun gnus-group-edit-global-kill (&optional article group)
6540   "Edit the global kill file.
6541 If GROUP, edit that local kill file instead."
6542   (interactive "P")
6543   (setq gnus-current-kill-article article)
6544   (gnus-kill-file-edit-file group)
6545   (gnus-message
6546    6
6547    (substitute-command-keys
6548     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6549             (if group "local" "global")))))
6550
6551 (defun gnus-group-edit-local-kill (article group)
6552   "Edit a local kill file."
6553   (interactive (list nil (gnus-group-group-name)))
6554   (gnus-group-edit-global-kill article group))
6555
6556 (defun gnus-group-force-update ()
6557   "Update `.newsrc' file."
6558   (interactive)
6559   (gnus-save-newsrc-file))
6560
6561 (defun gnus-group-suspend ()
6562   "Suspend the current Gnus session.
6563 In fact, cleanup buffers except for group mode buffer.
6564 The hook gnus-suspend-gnus-hook is called before actually suspending."
6565   (interactive)
6566   (run-hooks 'gnus-suspend-gnus-hook)
6567   ;; Kill Gnus buffers except for group mode buffer.
6568   (let ((group-buf (get-buffer gnus-group-buffer)))
6569     ;; Do this on a separate list in case the user does a ^G before we finish
6570     (let ((gnus-buffer-list
6571            (delq group-buf (delq gnus-dribble-buffer
6572                                  (append gnus-buffer-list nil)))))
6573       (while gnus-buffer-list
6574         (gnus-kill-buffer (car gnus-buffer-list))
6575         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6576     (if group-buf
6577         (progn
6578           (setq gnus-buffer-list (list group-buf))
6579           (bury-buffer group-buf)
6580           (delete-windows-on group-buf t)))))
6581
6582 (defun gnus-group-clear-dribble ()
6583   "Clear all information from the dribble buffer."
6584   (interactive)
6585   (gnus-dribble-clear)
6586   (gnus-message 7 "Cleared dribble buffer"))
6587
6588 (defun gnus-group-exit ()
6589   "Quit reading news after updating .newsrc.eld and .newsrc.
6590 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6591   (interactive)
6592   (when 
6593       (or noninteractive                ;For gnus-batch-kill
6594           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6595           (not gnus-interactive-exit)   ;Without confirmation
6596           gnus-expert-user
6597           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6598     (run-hooks 'gnus-exit-gnus-hook)
6599     ;; Offer to save data from non-quitted summary buffers.
6600     (gnus-offer-save-summaries)
6601     ;; Save the newsrc file(s).
6602     (gnus-save-newsrc-file)
6603     ;; Kill-em-all.
6604     (gnus-close-backends)
6605     ;; Reset everything.
6606     (gnus-clear-system)
6607     ;; Allow the user to do things after cleaning up.
6608     (run-hooks 'gnus-after-exiting-gnus-hook)))
6609
6610 (defun gnus-close-backends ()
6611   ;; Send a close request to all backends that support such a request.
6612   (let ((methods gnus-valid-select-methods)
6613         func)
6614     (while methods
6615       (if (fboundp (setq func (intern (concat (caar methods)
6616                                               "-request-close"))))
6617           (funcall func))
6618       (setq methods (cdr methods)))))
6619
6620 (defun gnus-group-quit ()
6621   "Quit reading news without updating .newsrc.eld or .newsrc.
6622 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6623   (interactive)
6624   (when (or noninteractive              ;For gnus-batch-kill
6625             (zerop (buffer-size))
6626             (not (gnus-server-opened gnus-select-method))
6627             gnus-expert-user
6628             (not gnus-current-startup-file)
6629             (gnus-yes-or-no-p
6630              (format "Quit reading news without saving %s? "
6631                      (file-name-nondirectory gnus-current-startup-file))))
6632     (run-hooks 'gnus-exit-gnus-hook)
6633     (if gnus-use-full-window
6634         (delete-other-windows)
6635       (gnus-remove-some-windows))
6636     (gnus-dribble-save)
6637     (gnus-close-backends)
6638     (gnus-clear-system)
6639     ;; Allow the user to do things after cleaning up.
6640     (run-hooks 'gnus-after-exiting-gnus-hook)))
6641
6642 (defun gnus-offer-save-summaries ()
6643   "Offer to save all active summary buffers."
6644   (save-excursion
6645     (let ((buflist (buffer-list))
6646           buffers bufname)
6647       ;; Go through all buffers and find all summaries.
6648       (while buflist
6649         (and (setq bufname (buffer-name (car buflist)))
6650              (string-match "Summary" bufname)
6651              (save-excursion
6652                (set-buffer bufname)
6653                ;; We check that this is, indeed, a summary buffer.
6654                (and (eq major-mode 'gnus-summary-mode)
6655                     ;; Also make sure this isn't bogus.
6656                     gnus-newsgroup-prepared))
6657              (push bufname buffers))
6658         (setq buflist (cdr buflist)))
6659       ;; Go through all these summary buffers and offer to save them.
6660       (when buffers
6661         (map-y-or-n-p
6662          "Update summary buffer %s? "
6663          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6664          buffers)))))
6665
6666 (defun gnus-group-describe-briefly ()
6667   "Give a one line description of the group mode commands."
6668   (interactive)
6669   (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")))
6670
6671 (defun gnus-group-browse-foreign-server (method)
6672   "Browse a foreign news server.
6673 If called interactively, this function will ask for a select method
6674  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6675 If not, METHOD should be a list where the first element is the method
6676 and the second element is the address."
6677   (interactive
6678    (list (let ((how (completing-read
6679                      "Which backend: "
6680                      (append gnus-valid-select-methods gnus-server-alist)
6681                      nil t (cons "nntp" 0))))
6682            ;; We either got a backend name or a virtual server name.
6683            ;; If the first, we also need an address.
6684            (if (assoc how gnus-valid-select-methods)
6685                (list (intern how)
6686                      ;; Suggested by mapjph@bath.ac.uk.
6687                      (completing-read
6688                       "Address: "
6689                       (mapcar (lambda (server) (list server))
6690                               gnus-secondary-servers)))
6691              ;; We got a server name, so we find the method.
6692              (gnus-server-to-method how)))))
6693   (gnus-browse-foreign-server method))
6694
6695 \f
6696 ;;;
6697 ;;; Gnus summary mode
6698 ;;;
6699
6700 (defvar gnus-summary-mode-map nil)
6701
6702 (put 'gnus-summary-mode 'mode-class 'special)
6703
6704 (unless gnus-summary-mode-map
6705   (setq gnus-summary-mode-map (make-keymap))
6706   (suppress-keymap gnus-summary-mode-map)
6707
6708   ;; Non-orthogonal keys
6709
6710   (gnus-define-keys gnus-summary-mode-map
6711     " " gnus-summary-next-page
6712     "\177" gnus-summary-prev-page
6713     [delete] gnus-summary-prev-page
6714     "\r" gnus-summary-scroll-up
6715     "n" gnus-summary-next-unread-article
6716     "p" gnus-summary-prev-unread-article
6717     "N" gnus-summary-next-article
6718     "P" gnus-summary-prev-article
6719     "\M-\C-n" gnus-summary-next-same-subject
6720     "\M-\C-p" gnus-summary-prev-same-subject
6721     "\M-n" gnus-summary-next-unread-subject
6722     "\M-p" gnus-summary-prev-unread-subject
6723     "." gnus-summary-first-unread-article
6724     "," gnus-summary-best-unread-article
6725     "\M-s" gnus-summary-search-article-forward
6726     "\M-r" gnus-summary-search-article-backward
6727     "<" gnus-summary-beginning-of-article
6728     ">" gnus-summary-end-of-article
6729     "j" gnus-summary-goto-article
6730     "^" gnus-summary-refer-parent-article
6731     "\M-^" gnus-summary-refer-article
6732     "u" gnus-summary-tick-article-forward
6733     "!" gnus-summary-tick-article-forward
6734     "U" gnus-summary-tick-article-backward
6735     "d" gnus-summary-mark-as-read-forward
6736     "D" gnus-summary-mark-as-read-backward
6737     "E" gnus-summary-mark-as-expirable
6738     "\M-u" gnus-summary-clear-mark-forward
6739     "\M-U" gnus-summary-clear-mark-backward
6740     "k" gnus-summary-kill-same-subject-and-select
6741     "\C-k" gnus-summary-kill-same-subject
6742     "\M-\C-k" gnus-summary-kill-thread
6743     "\M-\C-l" gnus-summary-lower-thread
6744     "e" gnus-summary-edit-article
6745     "#" gnus-summary-mark-as-processable
6746     "\M-#" gnus-summary-unmark-as-processable
6747     "\M-\C-t" gnus-summary-toggle-threads
6748     "\M-\C-s" gnus-summary-show-thread
6749     "\M-\C-h" gnus-summary-hide-thread
6750     "\M-\C-f" gnus-summary-next-thread
6751     "\M-\C-b" gnus-summary-prev-thread
6752     "\M-\C-u" gnus-summary-up-thread
6753     "\M-\C-d" gnus-summary-down-thread
6754     "&" gnus-summary-execute-command
6755     "c" gnus-summary-catchup-and-exit
6756     "\C-w" gnus-summary-mark-region-as-read
6757     "\C-t" gnus-summary-toggle-truncation
6758     "?" gnus-summary-mark-as-dormant
6759     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6760     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6761     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6762     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6763     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6764     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6765     "=" gnus-summary-expand-window
6766     "\C-x\C-s" gnus-summary-reselect-current-group
6767     "\M-g" gnus-summary-rescan-group
6768     "w" gnus-summary-stop-page-breaking
6769     "\C-c\C-r" gnus-summary-caesar-message
6770     "\M-t" gnus-summary-toggle-mime
6771     "f" gnus-summary-followup
6772     "F" gnus-summary-followup-with-original
6773     "C" gnus-summary-cancel-article
6774     "r" gnus-summary-reply
6775     "R" gnus-summary-reply-with-original
6776     "\C-c\C-f" gnus-summary-mail-forward
6777     "o" gnus-summary-save-article
6778     "\C-o" gnus-summary-save-article-mail
6779     "|" gnus-summary-pipe-output
6780     "\M-k" gnus-summary-edit-local-kill
6781     "\M-K" gnus-summary-edit-global-kill
6782     "V" gnus-version
6783     "\C-c\C-d" gnus-summary-describe-group
6784     "q" gnus-summary-exit
6785     "Q" gnus-summary-exit-no-update
6786     "\C-c\C-i" gnus-info-find-node
6787     gnus-mouse-2 gnus-mouse-pick-article
6788     "m" gnus-summary-mail-other-window
6789     "a" gnus-summary-post-news
6790     "x" gnus-summary-limit-to-unread
6791     "s" gnus-summary-isearch-article
6792     "t" gnus-article-hide-headers
6793     "g" gnus-summary-show-article
6794     "l" gnus-summary-goto-last-article
6795     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6796     "\C-d" gnus-summary-enter-digest-group
6797     "\C-c\C-b" gnus-bug
6798     "*" gnus-cache-enter-article
6799     "\M-*" gnus-cache-remove-article
6800     "\M-&" gnus-summary-universal-argument
6801     "\C-l" gnus-recenter
6802     "I" gnus-summary-increase-score
6803     "L" gnus-summary-lower-score
6804
6805     "V" gnus-summary-score-map
6806     "X" gnus-uu-extract-map
6807     "S" gnus-summary-send-map)
6808
6809   ;; Sort of orthogonal keymap
6810   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6811     "t" gnus-summary-tick-article-forward
6812     "!" gnus-summary-tick-article-forward
6813     "d" gnus-summary-mark-as-read-forward
6814     "r" gnus-summary-mark-as-read-forward
6815     "c" gnus-summary-clear-mark-forward
6816     " " gnus-summary-clear-mark-forward
6817     "e" gnus-summary-mark-as-expirable
6818     "x" gnus-summary-mark-as-expirable
6819     "?" gnus-summary-mark-as-dormant
6820     "b" gnus-summary-set-bookmark
6821     "B" gnus-summary-remove-bookmark
6822     "#" gnus-summary-mark-as-processable
6823     "\M-#" gnus-summary-unmark-as-processable
6824     "S" gnus-summary-limit-include-expunged
6825     "C" gnus-summary-catchup
6826     "H" gnus-summary-catchup-to-here
6827     "\C-c" gnus-summary-catchup-all
6828     "k" gnus-summary-kill-same-subject-and-select
6829     "K" gnus-summary-kill-same-subject
6830     "P" gnus-uu-mark-map)
6831
6832   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6833     "c" gnus-summary-clear-above
6834     "u" gnus-summary-tick-above
6835     "m" gnus-summary-mark-above
6836     "k" gnus-summary-kill-below)
6837
6838   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6839     "/" gnus-summary-limit-to-subject
6840     "n" gnus-summary-limit-to-articles
6841     "w" gnus-summary-pop-limit
6842     "s" gnus-summary-limit-to-subject
6843     "a" gnus-summary-limit-to-author
6844     "u" gnus-summary-limit-to-unread
6845     "m" gnus-summary-limit-to-marks
6846     "v" gnus-summary-limit-to-score
6847     "D" gnus-summary-limit-include-dormant
6848     "d" gnus-summary-limit-exclude-dormant
6849     ;;  "t" gnus-summary-limit-exclude-thread
6850     "E" gnus-summary-limit-include-expunged
6851     "c" gnus-summary-limit-exclude-childless-dormant
6852     "C" gnus-summary-limit-mark-excluded-as-read)
6853
6854   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6855     "n" gnus-summary-next-unread-article
6856     "p" gnus-summary-prev-unread-article
6857     "N" gnus-summary-next-article
6858     "P" gnus-summary-prev-article
6859     "\C-n" gnus-summary-next-same-subject
6860     "\C-p" gnus-summary-prev-same-subject
6861     "\M-n" gnus-summary-next-unread-subject
6862     "\M-p" gnus-summary-prev-unread-subject
6863     "f" gnus-summary-first-unread-article
6864     "b" gnus-summary-best-unread-article
6865     "j" gnus-summary-goto-article
6866     "g" gnus-summary-goto-subject
6867     "l" gnus-summary-goto-last-article
6868     "p" gnus-summary-pop-article)
6869
6870   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6871     "k" gnus-summary-kill-thread
6872     "l" gnus-summary-lower-thread
6873     "i" gnus-summary-raise-thread
6874     "T" gnus-summary-toggle-threads
6875     "t" gnus-summary-rethread-current
6876     "^" gnus-summary-reparent-thread
6877     "s" gnus-summary-show-thread
6878     "S" gnus-summary-show-all-threads
6879     "h" gnus-summary-hide-thread
6880     "H" gnus-summary-hide-all-threads
6881     "n" gnus-summary-next-thread
6882     "p" gnus-summary-prev-thread
6883     "u" gnus-summary-up-thread
6884     "o" gnus-summary-top-thread
6885     "d" gnus-summary-down-thread
6886     "#" gnus-uu-mark-thread
6887     "\M-#" gnus-uu-unmark-thread)
6888
6889   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6890     "c" gnus-summary-catchup-and-exit
6891     "C" gnus-summary-catchup-all-and-exit
6892     "E" gnus-summary-exit-no-update
6893     "Q" gnus-summary-exit
6894     "Z" gnus-summary-exit
6895     "n" gnus-summary-catchup-and-goto-next-group
6896     "R" gnus-summary-reselect-current-group
6897     "G" gnus-summary-rescan-group
6898     "N" gnus-summary-next-group
6899     "P" gnus-summary-prev-group)
6900
6901   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6902     " " gnus-summary-next-page
6903     "n" gnus-summary-next-page
6904     "\177" gnus-summary-prev-page
6905     [delete] gnus-summary-prev-page
6906     "p" gnus-summary-prev-page
6907     "\r" gnus-summary-scroll-up
6908     "<" gnus-summary-beginning-of-article
6909     ">" gnus-summary-end-of-article
6910     "b" gnus-summary-beginning-of-article
6911     "e" gnus-summary-end-of-article
6912     "^" gnus-summary-refer-parent-article
6913     "r" gnus-summary-refer-parent-article
6914     "R" gnus-summary-refer-references
6915     "g" gnus-summary-show-article
6916     "s" gnus-summary-isearch-article)
6917
6918   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6919     "b" gnus-article-add-buttons
6920     "B" gnus-article-add-buttons-to-head
6921     "o" gnus-article-treat-overstrike
6922     ;;  "w" gnus-article-word-wrap
6923     "w" gnus-article-fill-cited-article
6924     "c" gnus-article-remove-cr
6925     "L" gnus-article-remove-trailing-blank-lines
6926     "q" gnus-article-de-quoted-unreadable
6927     "f" gnus-article-display-x-face
6928     "l" gnus-summary-stop-page-breaking
6929     "r" gnus-summary-caesar-message
6930     "t" gnus-article-hide-headers
6931     "v" gnus-summary-verbose-headers
6932     "m" gnus-summary-toggle-mime)
6933
6934   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6935     "a" gnus-article-hide
6936     "h" gnus-article-hide-headers
6937     "b" gnus-article-hide-boring-headers
6938     "s" gnus-article-hide-signature
6939     "c" gnus-article-hide-citation
6940     "p" gnus-article-hide-pgp
6941     "\C-c" gnus-article-hide-citation-maybe)
6942
6943   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6944     "a" gnus-article-highlight
6945     "h" gnus-article-highlight-headers
6946     "c" gnus-article-highlight-citation
6947     "s" gnus-article-highlight-signature)
6948
6949   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6950     "z" gnus-article-date-ut
6951     "u" gnus-article-date-ut
6952     "l" gnus-article-date-local
6953     "e" gnus-article-date-lapsed
6954     "o" gnus-article-date-original)
6955
6956   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
6957     "v" gnus-version
6958     "f" gnus-summary-fetch-faq
6959     "d" gnus-summary-describe-group
6960     "h" gnus-summary-describe-briefly
6961     "i" gnus-info-find-node)
6962
6963   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
6964     "e" gnus-summary-expire-articles
6965     "\M-\C-e" gnus-summary-expire-articles-now
6966     "\177" gnus-summary-delete-article
6967     [delete] gnus-summary-delete-article
6968     "m" gnus-summary-move-article
6969     "r" gnus-summary-respool-article
6970     "w" gnus-summary-edit-article
6971     "c" gnus-summary-copy-article
6972     "B" gnus-summary-crosspost-article
6973     "q" gnus-summary-respool-query
6974     "i" gnus-summary-import-article)
6975
6976   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
6977     "o" gnus-summary-save-article
6978     "m" gnus-summary-save-article-mail
6979     "r" gnus-summary-save-article-rmail
6980     "f" gnus-summary-save-article-file
6981     "b" gnus-summary-save-article-body-file
6982     "h" gnus-summary-save-article-folder
6983     "v" gnus-summary-save-article-vm
6984     "p" gnus-summary-pipe-output
6985     "s" gnus-soup-add-article)
6986   )
6987
6988 \f
6989
6990 (defun gnus-summary-mode (&optional group)
6991   "Major mode for reading articles.
6992
6993 All normal editing commands are switched off.
6994 \\<gnus-summary-mode-map>
6995 Each line in this buffer represents one article.  To read an
6996 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6997 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
6998 respectively.
6999
7000 You can also post articles and send mail from this buffer.  To
7001 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7002 of an article, type `\\[gnus-summary-reply]'.
7003
7004 There are approx. one gazillion commands you can execute in this
7005 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7006
7007 The following commands are available:
7008
7009 \\{gnus-summary-mode-map}"
7010   (interactive)
7011   (when (and menu-bar-mode
7012              (gnus-visual-p 'summary-menu 'menu))
7013     (gnus-summary-make-menu-bar))
7014   (kill-all-local-variables)
7015   (let ((locals gnus-summary-local-variables))
7016     (while locals
7017       (if (consp (car locals))
7018           (progn
7019             (make-local-variable (caar locals))
7020             (set (caar locals) (eval (cdar locals))))
7021         (make-local-variable (car locals))
7022         (set (car locals) nil))
7023       (setq locals (cdr locals))))
7024   (gnus-make-thread-indent-array)
7025   (gnus-simplify-mode-line)
7026   (setq major-mode 'gnus-summary-mode)
7027   (setq mode-name "Summary")
7028   (make-local-variable 'minor-mode-alist)
7029   (use-local-map gnus-summary-mode-map)
7030   (buffer-disable-undo (current-buffer))
7031   (setq buffer-read-only t)             ;Disable modification
7032   (setq truncate-lines t)
7033   (setq selective-display t)
7034   (setq selective-display-ellipses t)   ;Display `...'
7035   (setq buffer-display-table gnus-summary-display-table)
7036   (setq gnus-newsgroup-name group)
7037   (run-hooks 'gnus-summary-mode-hook))
7038
7039 (defun gnus-summary-make-display-table ()
7040   ;; Change the display table.  Odd characters have a tendency to mess
7041   ;; up nicely formatted displays - we make all possible glyphs
7042   ;; display only a single character.
7043
7044   ;; We start from the standard display table, if any.
7045   (setq gnus-summary-display-table
7046         (or (copy-sequence standard-display-table)
7047             (make-display-table)))
7048   ;; Nix out all the control chars...
7049   (let ((i 32))
7050     (while (>= (setq i (1- i)) 0)
7051       (aset gnus-summary-display-table i [??])))
7052   ;; ... but not newline and cr, of course. (cr is necessary for the
7053   ;; selective display).
7054   (aset gnus-summary-display-table ?\n nil)
7055   (aset gnus-summary-display-table ?\r nil)
7056   ;; We nix out any glyphs over 126 that are not set already.
7057   (let ((i 256))
7058     (while (>= (setq i (1- i)) 127)
7059       ;; Only modify if the entry is nil.
7060       (or (aref gnus-summary-display-table i)
7061           (aset gnus-summary-display-table i [??])))))
7062
7063 (defun gnus-summary-clear-local-variables ()
7064   (let ((locals gnus-summary-local-variables))
7065     (while locals
7066       (if (consp (car locals))
7067           (and (vectorp (caar locals))
7068                (set (caar locals) nil))
7069         (and (vectorp (car locals))
7070              (set (car locals) nil)))
7071       (setq locals (cdr locals)))))
7072
7073 ;; Summary data functions.
7074
7075 (defmacro gnus-data-number (data)
7076   `(car ,data))
7077
7078 (defmacro gnus-data-set-number (data number)
7079   `(setcar ,data ,number))
7080
7081 (defmacro gnus-data-mark (data)
7082   `(nth 1 ,data))
7083
7084 (defmacro gnus-data-set-mark (data mark)
7085   `(setcar (nthcdr 1 ,data) ,mark))
7086
7087 (defmacro gnus-data-pos (data)
7088   `(nth 2 ,data))
7089
7090 (defmacro gnus-data-set-pos (data pos)
7091   `(setcar (nthcdr 2 ,data) ,pos))
7092
7093 (defmacro gnus-data-header (data)
7094   `(nth 3 ,data))
7095
7096 (defmacro gnus-data-level (data)
7097   `(nth 4 ,data))
7098
7099 (defmacro gnus-data-unread-p (data)
7100   `(= (nth 1 ,data) gnus-unread-mark))
7101
7102 (defmacro gnus-data-pseudo-p (data)
7103   `(consp (nth 3 ,data)))
7104
7105 (defmacro gnus-data-find (number)
7106   `(assq ,number gnus-newsgroup-data))
7107
7108 (defmacro gnus-data-find-list (number &optional data)
7109   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7110      (memq (assq ,number bdata)
7111            bdata)))
7112
7113 (defmacro gnus-data-make (number mark pos header level)
7114   `(list ,number ,mark ,pos ,header ,level))
7115
7116 (defun gnus-data-enter (after-article number mark pos header level offset)
7117   (let ((data (gnus-data-find-list after-article)))
7118     (or data (error "No such article: %d" after-article))
7119     (setcdr data (cons (gnus-data-make number mark pos header level)
7120                        (cdr data)))
7121     (setq gnus-newsgroup-data-reverse nil)
7122     (gnus-data-update-list (cddr data) offset)))
7123
7124 (defun gnus-data-enter-list (after-article list &optional offset)
7125   (when list
7126     (let ((data (and after-article (gnus-data-find-list after-article)))
7127           (ilist list))
7128       (or data (not after-article) (error "No such article: %d" after-article))
7129       ;; Find the last element in the list to be spliced into the main
7130       ;; list.
7131       (while (cdr list)
7132         (setq list (cdr list)))
7133       (if (not data)
7134           (progn
7135             (setcdr list gnus-newsgroup-data)
7136             (setq gnus-newsgroup-data ilist)
7137             (and offset (gnus-data-update-list (cdr list) offset)))
7138         (setcdr list (cdr data))
7139         (setcdr data ilist)
7140         (and offset (gnus-data-update-list (cdr data) offset)))
7141       (setq gnus-newsgroup-data-reverse nil))))
7142
7143 (defun gnus-data-remove (article &optional offset)
7144   (let ((data gnus-newsgroup-data))
7145     (if (= (gnus-data-number (car data)) article)
7146         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7147               gnus-newsgroup-data-reverse nil)
7148       (while (cdr data)
7149         (and (= (gnus-data-number (cadr data)) article)
7150              (progn
7151                (setcdr data (cddr data))
7152                (and offset (gnus-data-update-list (cdr data) offset))
7153                (setq data nil
7154                      gnus-newsgroup-data-reverse nil)))
7155         (setq data (cdr data))))))
7156
7157 (defmacro gnus-data-list (backward)
7158   `(if ,backward
7159        (or gnus-newsgroup-data-reverse
7160            (setq gnus-newsgroup-data-reverse
7161                  (reverse gnus-newsgroup-data)))
7162      gnus-newsgroup-data))
7163
7164 (defun gnus-data-update-list (data offset)
7165   "Add OFFSET to the POS of all data entries in DATA."
7166   (while data
7167     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7168     (setq data (cdr data))))
7169
7170 (defun gnus-data-compute-positions ()
7171   "Compute the positions of all articles."
7172   (let ((data gnus-newsgroup-data)
7173         pos)
7174     (while data
7175       (when (setq pos (text-property-any
7176                        (point-min) (point-max)
7177                        'gnus-number (gnus-data-number (car data))))
7178         (gnus-data-set-pos (car data) (+ pos 3)))
7179       (setq data (cdr data)))))
7180
7181 (defun gnus-summary-article-pseudo-p (article)
7182   "Say whether this article is a pseudo article or not."
7183   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7184
7185 (defun gnus-article-parent-p (number)
7186   "Say whether this article is a parent or not."
7187   (let ((data (gnus-data-find-list number)))
7188     (and (cdr data)                     ; There has to be an article after...
7189          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7190             (gnus-data-level (nth 1 data))))))
7191
7192 (defun gnus-article-children (number)
7193   "Return a list of all children to NUMBER."
7194   (let* ((data (gnus-data-find-list number))
7195          (level (gnus-data-level (car data)))
7196          children)
7197     (setq data (cdr data))
7198     (while (and data            
7199                 (= (gnus-data-level (car data)) (1+ level)))
7200       (push (gnus-data-number (car data)) children)
7201       (setq data (cdr data)))
7202     children))
7203
7204 (defmacro gnus-summary-skip-intangible ()
7205   "If the current article is intangible, then jump to a different article."
7206   '(let ((to (get-text-property (point) 'gnus-intangible)))
7207     (and to (gnus-summary-goto-subject to))))
7208
7209 (defmacro gnus-summary-article-intangible-p ()
7210   "Say whether this article is intangible or not."
7211   '(get-text-property (point) 'gnus-intangible))
7212
7213 ;; Some summary mode macros.
7214
7215 (defmacro gnus-summary-article-number ()
7216   "The article number of the article on the current line.
7217 If there isn's an article number here, then we return the current
7218 article number."
7219   '(progn
7220      (gnus-summary-skip-intangible)
7221      (or (get-text-property (point) 'gnus-number)
7222          (gnus-summary-last-subject))))
7223
7224 (defmacro gnus-summary-article-header (&optional number)
7225   `(gnus-data-header (gnus-data-find
7226                       ,(or number '(gnus-summary-article-number)))))
7227
7228 (defmacro gnus-summary-thread-level (&optional number)
7229   `(if (and (eq gnus-summary-make-false-root 'dummy)
7230             (get-text-property (point) 'gnus-intangible))
7231        0
7232      (gnus-data-level (gnus-data-find
7233                        ,(or number '(gnus-summary-article-number))))))
7234
7235 (defmacro gnus-summary-article-mark (&optional number)
7236   `(gnus-data-mark (gnus-data-find
7237                     ,(or number '(gnus-summary-article-number)))))
7238
7239 (defmacro gnus-summary-article-pos (&optional number)
7240   `(gnus-data-pos (gnus-data-find
7241                    ,(or number '(gnus-summary-article-number)))))
7242
7243 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7244 (defmacro gnus-summary-article-subject (&optional number)
7245   "Return current subject string or nil if nothing."
7246   `(let ((headers
7247           ,(if number
7248                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7249              '(gnus-data-header (assq (gnus-summary-article-number)
7250                                       gnus-newsgroup-data)))))
7251      (and headers
7252           (vectorp headers)
7253           (mail-header-subject headers))))
7254
7255 (defmacro gnus-summary-article-score (&optional number)
7256   "Return current article score."
7257   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7258                   gnus-newsgroup-scored))
7259        gnus-summary-default-score 0))
7260
7261 (defun gnus-summary-article-children (&optional number)
7262   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7263          (level (gnus-data-level (car data)))
7264          l children)
7265     (while (and (setq data (cdr data))
7266                 (> (setq l (gnus-data-level (car data))) level))
7267       (and (= (1+ level) l)
7268            (setq children (cons (gnus-data-number (car data))
7269                                 children))))
7270     (nreverse children)))
7271
7272 (defun gnus-summary-article-parent (&optional number)
7273   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7274                                     (gnus-data-list t)))
7275          (level (gnus-data-level (car data))))
7276     (if (zerop level)
7277         () ; This is a root.
7278       ;; We search until we find an article with a level less than
7279       ;; this one.  That function has to be the parent.
7280       (while (and (setq data (cdr data))
7281                   (not (< (gnus-data-level (car data)) level))))
7282       (and data (gnus-data-number (car data))))))
7283
7284 (defun gnus-unread-mark-p (mark)
7285   "Say whether MARK is the unread mark."
7286   (= mark gnus-unread-mark))
7287
7288 (defun gnus-read-mark-p (mark)
7289   "Say whether MARK is one of the marks that mark as read.
7290 This is all marks except unread, ticked, dormant, and expirable."
7291   (not (or (= mark gnus-unread-mark)
7292            (= mark gnus-ticked-mark)
7293            (= mark gnus-dormant-mark)
7294            (= mark gnus-expirable-mark))))
7295
7296 ;; Various summary mode internalish functions.
7297
7298 (defun gnus-mouse-pick-article (e)
7299   (interactive "e")
7300   (mouse-set-point e)
7301   (gnus-summary-next-page nil t))
7302
7303 (defun gnus-summary-setup-buffer (group)
7304   "Initialize summary buffer."
7305   (let ((buffer (concat "*Summary " group "*")))
7306     (if (get-buffer buffer)
7307         (progn
7308           (set-buffer buffer)
7309           (setq gnus-summary-buffer (current-buffer))
7310           (not gnus-newsgroup-prepared))
7311       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7312       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7313       (gnus-add-current-to-buffer-list)
7314       (gnus-summary-mode group)
7315       (when gnus-carpal
7316         (gnus-carpal-setup-buffer 'summary))
7317       (unless gnus-single-article-buffer
7318         (make-local-variable 'gnus-article-buffer)
7319         (make-local-variable 'gnus-article-current)
7320         (make-local-variable 'gnus-original-article-buffer))
7321       (setq gnus-newsgroup-name group)
7322       t)))
7323
7324 (defun gnus-set-global-variables ()
7325   ;; Set the global equivalents of the summary buffer-local variables
7326   ;; to the latest values they had.  These reflect the summary buffer
7327   ;; that was in action when the last article was fetched.
7328   (when (eq major-mode 'gnus-summary-mode)
7329     (setq gnus-summary-buffer (current-buffer))
7330     (let ((name gnus-newsgroup-name)
7331           (marked gnus-newsgroup-marked)
7332           (unread gnus-newsgroup-unreads)
7333           (headers gnus-current-headers)
7334           (data gnus-newsgroup-data)
7335           (summary gnus-summary-buffer)
7336           (article-buffer gnus-article-buffer)
7337           (original gnus-original-article-buffer)
7338           (gac gnus-article-current)
7339           (score-file gnus-current-score-file))
7340       (save-excursion
7341         (set-buffer gnus-group-buffer)
7342         (setq gnus-newsgroup-name name)
7343         (setq gnus-newsgroup-marked marked)
7344         (setq gnus-newsgroup-unreads unread)
7345         (setq gnus-current-headers headers)
7346         (setq gnus-newsgroup-data data)
7347         (setq gnus-article-current gac)
7348         (setq gnus-summary-buffer summary)
7349         (setq gnus-article-buffer article-buffer)
7350         (setq gnus-original-article-buffer original)
7351         (setq gnus-current-score-file score-file)))))
7352
7353 (defun gnus-summary-last-article-p (&optional article)
7354   "Return whether ARTICLE is the last article in the buffer."
7355   (if (not (setq article (or article (gnus-summary-article-number))))
7356       t ; All non-existant numbers are the last article. :-)
7357     (not (cdr (gnus-data-find-list article)))))
7358
7359 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7360   "Insert a dummy root in the summary buffer."
7361   (beginning-of-line)
7362   (add-text-properties
7363    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7364    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7365
7366 (defvar gnus-thread-indent-array nil)
7367 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7368 (defun gnus-make-thread-indent-array ()
7369   (let ((n 200))
7370     (unless (and gnus-thread-indent-array
7371                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7372       (setq gnus-thread-indent-array (make-vector 201 "")
7373             gnus-thread-indent-array-level gnus-thread-indent-level)
7374       (while (>= n 0)
7375         (aset gnus-thread-indent-array n
7376               (make-string (* n gnus-thread-indent-level) ? ))
7377         (setq n (1- n))))))
7378
7379 (defun gnus-summary-insert-line
7380   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7381                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7382                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7383   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7384          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7385          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7386          (gnus-tmp-score-char
7387           (if (or (null gnus-summary-default-score)
7388                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7389                       gnus-summary-zcore-fuzz)) ? 
7390             (if (< gnus-tmp-score gnus-summary-default-score)
7391                 gnus-score-below-mark gnus-score-over-mark)))
7392          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7393                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7394                                   gnus-cached-mark)
7395                                  (gnus-tmp-replied gnus-replied-mark)
7396                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7397                                   gnus-saved-mark)
7398                                  (t gnus-unread-mark)))
7399          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7400          (gnus-tmp-name
7401           (cond
7402            ((string-match "(.+)" gnus-tmp-from)
7403             (substring gnus-tmp-from
7404                        (1+ (match-beginning 0)) (1- (match-end 0))))
7405            ((string-match "<[^>]+> *$" gnus-tmp-from)
7406             (let ((beg (match-beginning 0)))
7407               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7408                        (substring gnus-tmp-from (1+ (match-beginning 0))
7409                                   (1- (match-end 0))))
7410                   (substring gnus-tmp-from 0 beg))))
7411            (t gnus-tmp-from)))
7412          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7413          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7414          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7415          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7416          (buffer-read-only nil))
7417     (when (string= gnus-tmp-name "")
7418       (setq gnus-tmp-name gnus-tmp-from))
7419     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7420     (put-text-property
7421      (point)
7422      (progn (eval gnus-summary-line-format-spec) (point))
7423      'gnus-number gnus-tmp-number)
7424     (when (gnus-visual-p 'summary-highlight 'highlight)
7425       (forward-line -1)
7426       (run-hooks 'gnus-summary-update-hook)
7427       (forward-line 1))))
7428
7429 (defun gnus-summary-update-line (&optional dont-update)
7430   ;; Update summary line after change.
7431   (when (and gnus-summary-default-score
7432              (not gnus-summary-inhibit-highlight))
7433     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7434            (article (gnus-summary-article-number))
7435            (score (gnus-summary-article-score article)))
7436       (unless dont-update
7437         (if (and gnus-summary-mark-below
7438                  (< (gnus-summary-article-score)
7439                     gnus-summary-mark-below))
7440             ;; This article has a low score, so we mark it as read.
7441             (when (memq article gnus-newsgroup-unreads)
7442               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7443           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7444             ;; This article was previously marked as read on account
7445             ;; of a low score, but now it has risen, so we mark it as
7446             ;; unread.
7447             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7448         (gnus-summary-update-mark
7449          (if (or (null gnus-summary-default-score)
7450                  (<= (abs (- score gnus-summary-default-score))
7451                      gnus-summary-zcore-fuzz)) ? 
7452            (if (< score gnus-summary-default-score)
7453                gnus-score-below-mark gnus-score-over-mark)) 'score))
7454       ;; Do visual highlighting.
7455       (when (gnus-visual-p 'summary-highlight 'highlight)
7456         (run-hooks 'gnus-summary-update-hook)))))
7457
7458 (defvar gnus-tmp-new-adopts nil)
7459
7460 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7461   ;; Sum up all elements (and sub-elements) in a list.
7462   (let* ((number
7463           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7464           (cond
7465            ((and (consp thread) (cdr thread))
7466             (apply
7467              '+ 1 (mapcar
7468                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7469            ((null thread)
7470             1)
7471            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7472             1)
7473            (t 1))))
7474     (when (and level (zerop level) gnus-tmp-new-adopts)
7475       (incf number
7476             (apply '+ (mapcar
7477                        'gnus-summary-number-of-articles-in-thread
7478                        gnus-tmp-new-adopts))))
7479     (if char
7480         (if (> number 1) gnus-not-empty-thread-mark
7481           gnus-empty-thread-mark)
7482       number)))
7483
7484 (defun gnus-summary-set-local-parameters (group)
7485  "Go through the local params of GROUP and set all variable specs in that list."
7486   (let ((params (gnus-info-params (gnus-get-info group)))
7487         elem)
7488     (while params
7489       (setq elem (car params)
7490             params (cdr params))
7491       (and (consp elem)                 ; Has to be a cons.
7492            (consp (cdr elem))           ; The cdr has to be a list.
7493            (symbolp (car elem))         ; Has to be a symbol in there.
7494            (not (memq (car elem) 
7495                       '(quit-config to-address to-list to-group)))
7496            (progn                       ; So we set it.
7497              (make-local-variable (car elem))
7498              (set (car elem) (eval (nth 1 elem))))))))
7499
7500 (defun gnus-summary-read-group (group &optional show-all no-article
7501                                       kill-buffer no-display)
7502   "Start reading news in newsgroup GROUP.
7503 If SHOW-ALL is non-nil, already read articles are also listed.
7504 If NO-ARTICLE is non-nil, no article is selected initially.
7505 If NO-DISPLAY, don't generate a summary buffer."
7506   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7507   (let* ((new-group (gnus-summary-setup-buffer group))
7508          (quit-config (gnus-group-quit-config group))
7509          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7510     (cond
7511      ;; This summary buffer exists already, so we just select it.
7512      ((not new-group)
7513       (gnus-set-global-variables)
7514       (when kill-buffer
7515         (gnus-kill-or-deaden-summary kill-buffer))
7516       (gnus-configure-windows 'summary 'force)
7517       (gnus-set-mode-line 'summary)
7518       (gnus-summary-position-point)
7519       (message "")
7520       t)
7521      ;; We couldn't select this group.
7522      ((null did-select)
7523       (when (and (eq major-mode 'gnus-summary-mode)
7524                  (not (equal (current-buffer) kill-buffer)))
7525         (kill-buffer (current-buffer))
7526         (if (not quit-config)
7527             (progn
7528               (set-buffer gnus-group-buffer)
7529               (gnus-group-jump-to-group group)
7530               (gnus-group-next-unread-group 1))
7531           (if (not (buffer-name (car quit-config)))
7532               (gnus-configure-windows 'group 'force)
7533             (set-buffer (car quit-config))
7534             (and (eq major-mode 'gnus-summary-mode)
7535                  (gnus-set-global-variables))
7536             (gnus-configure-windows (cdr quit-config)))))
7537       (gnus-message 3 "Can't select group")
7538       nil)
7539      ;; The user did a `C-g' while prompting for number of articles,
7540      ;; so we exit this group.
7541      ((eq did-select 'quit)
7542       (and (eq major-mode 'gnus-summary-mode)
7543            (not (equal (current-buffer) kill-buffer))
7544            (kill-buffer (current-buffer)))
7545       (when kill-buffer
7546         (gnus-kill-or-deaden-summary kill-buffer))
7547       (if (not quit-config)
7548           (progn
7549             (set-buffer gnus-group-buffer)
7550             (gnus-group-jump-to-group group)
7551             (gnus-group-next-unread-group 1)
7552             (gnus-configure-windows 'group 'force))
7553         (if (not (buffer-name (car quit-config)))
7554             (gnus-configure-windows 'group 'force)
7555           (set-buffer (car quit-config))
7556           (and (eq major-mode 'gnus-summary-mode)
7557                (gnus-set-global-variables))
7558           (gnus-configure-windows (cdr quit-config))))
7559       ;; Finally signal the quit.
7560       (signal 'quit nil))
7561      ;; The group was successfully selected.
7562      (t
7563       (gnus-set-global-variables)
7564       ;; Save the active value in effect when the group was entered.
7565       (setq gnus-newsgroup-active
7566             (gnus-copy-sequence
7567              (gnus-active gnus-newsgroup-name)))
7568       ;; You can change the summary buffer in some way with this hook.
7569       (run-hooks 'gnus-select-group-hook)
7570       ;; Set any local variables in the group parameters.
7571       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7572       (gnus-update-format-specifications)
7573       ;; Do score processing.
7574       (when gnus-use-scoring
7575         (gnus-possibly-score-headers))
7576       ;; Check whether to fill in the gaps in the threads.
7577       (when gnus-build-sparse-threads
7578         (gnus-build-sparse-threads))
7579       ;; Find the initial limit.
7580       (if show-all
7581           (let ((gnus-newsgroup-dormant nil))
7582             (gnus-summary-initial-limit show-all))
7583         (gnus-summary-initial-limit show-all))
7584       ;; Generate the summary buffer.
7585       (unless no-display
7586         (gnus-summary-prepare))
7587       (when gnus-use-trees
7588         (gnus-tree-open group)
7589         (setq gnus-summary-highlight-line-function
7590               'gnus-tree-highlight-article))
7591       ;; If the summary buffer is empty, but there are some low-scored
7592       ;; articles or some excluded dormants, we include these in the
7593       ;; buffer.
7594       (when (and (zerop (buffer-size))
7595                  (not no-display))
7596         (cond (gnus-newsgroup-dormant
7597                (gnus-summary-limit-include-dormant))
7598               ((and gnus-newsgroup-scored show-all)
7599                (gnus-summary-limit-include-expunged))))
7600       ;; Function `gnus-apply-kill-file' must be called in this hook.
7601       (run-hooks 'gnus-apply-kill-hook)
7602       (if (and (zerop (buffer-size))
7603                (not no-display))
7604           (progn
7605             ;; This newsgroup is empty.
7606             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7607             (gnus-message 6 "No unread news")
7608             (when kill-buffer
7609               (gnus-kill-or-deaden-summary kill-buffer))
7610             ;; Return nil from this function.
7611             nil)
7612         ;; Hide conversation thread subtrees.  We cannot do this in
7613         ;; gnus-summary-prepare-hook since kill processing may not
7614         ;; work with hidden articles.
7615         (and gnus-show-threads
7616              gnus-thread-hide-subtree
7617              (gnus-summary-hide-all-threads))
7618         ;; Show first unread article if requested.
7619         (if (and (not no-article)
7620                  (not no-display)
7621                  gnus-newsgroup-unreads
7622                  gnus-auto-select-first)
7623             (unless (if (eq gnus-auto-select-first 'best)
7624                         (gnus-summary-best-unread-article)
7625                       (gnus-summary-first-unread-article))
7626               (gnus-configure-windows 'summary))
7627           ;; Don't select any articles, just move point to the first
7628           ;; article in the group.
7629           (goto-char (point-min))
7630           (gnus-summary-position-point)
7631           (gnus-set-mode-line 'summary)
7632           (gnus-configure-windows 'summary 'force))
7633         ;; If we are in async mode, we send some info to the backend.
7634         (when gnus-newsgroup-async
7635           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7636         (when kill-buffer
7637           (gnus-kill-or-deaden-summary kill-buffer))
7638         (when (get-buffer-window gnus-group-buffer t)
7639           ;; Gotta use windows, because recenter does wierd stuff if
7640           ;; the current buffer ain't the displayed window.
7641           (let ((owin (selected-window)))
7642             (select-window (get-buffer-window gnus-group-buffer t))
7643             (when (gnus-group-goto-group group)
7644               (recenter))
7645             (select-window owin))))
7646       ;; Mark this buffer as "prepared".
7647       (setq gnus-newsgroup-prepared t)
7648       t))))
7649
7650 (defun gnus-summary-prepare ()
7651   "Generate the summary buffer."
7652   (let ((buffer-read-only nil))
7653     (erase-buffer)
7654     (setq gnus-newsgroup-data nil
7655           gnus-newsgroup-data-reverse nil)
7656     (run-hooks 'gnus-summary-generate-hook)
7657     ;; Generate the buffer, either with threads or without.
7658     (when gnus-newsgroup-headers
7659       (gnus-summary-prepare-threads
7660        (if gnus-show-threads
7661            (gnus-sort-gathered-threads
7662             (funcall gnus-summary-thread-gathering-function
7663                      (gnus-sort-threads
7664                       (gnus-cut-threads (gnus-make-threads)))))
7665          ;; Unthreaded display.
7666          (gnus-sort-articles gnus-newsgroup-headers))))
7667     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7668     ;; Call hooks for modifying summary buffer.
7669     (goto-char (point-min))
7670     (run-hooks 'gnus-summary-prepare-hook)))
7671
7672 (defun gnus-gather-threads-by-subject (threads)
7673   "Gather threads by looking at Subject headers."
7674   (if (not gnus-summary-make-false-root)
7675       threads
7676     (let ((hashtb (gnus-make-hashtable 1023))
7677           (prev threads)
7678           (result threads)
7679           subject hthread whole-subject)
7680       (while threads
7681         (setq whole-subject (mail-header-subject (caar threads)))
7682         (setq subject
7683               (cond
7684                ;; Truncate the subject.
7685                ((numberp gnus-summary-gather-subject-limit)
7686                 (setq subject (gnus-simplify-subject-re whole-subject))
7687                 (if (> (length subject) gnus-summary-gather-subject-limit)
7688                     (substring subject 0 gnus-summary-gather-subject-limit)
7689                   subject))
7690                ;; Fuzzily simplify it.
7691                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7692                 (gnus-simplify-subject-fuzzy whole-subject))
7693                ;; Just remove the leading "Re:".
7694                (t
7695                 (gnus-simplify-subject-re whole-subject))))
7696
7697         (if (and gnus-summary-gather-exclude-subject
7698                  (string-match gnus-summary-gather-exclude-subject
7699                                subject))
7700             ()          ; We don't want to do anything with this article.
7701           ;; We simplify the subject before looking it up in the
7702           ;; hash table.
7703
7704           (if (setq hthread (gnus-gethash subject hashtb))
7705               (progn
7706                 ;; We enter a dummy root into the thread, if we
7707                 ;; haven't done that already.
7708                 (unless (stringp (caar hthread))
7709                   (setcar hthread (list whole-subject (car hthread))))
7710                 ;; We add this new gathered thread to this gathered
7711                 ;; thread.
7712                 (setcdr (car hthread)
7713                         (nconc (cdar hthread) (list (car threads))))
7714                 ;; Remove it from the list of threads.
7715                 (setcdr prev (cdr threads))
7716                 (setq threads prev))
7717             ;; Enter this thread into the hash table.
7718             (gnus-sethash subject threads hashtb)))
7719         (setq prev threads)
7720         (setq threads (cdr threads)))
7721       result)))
7722
7723 (defun gnus-gather-threads-by-references (threads)
7724   "Gather threads by looking at References headers."
7725   (let ((idhashtb (gnus-make-hashtable 1023))
7726         (thhashtb (gnus-make-hashtable 1023))
7727         (prev threads)
7728         (result threads)
7729         ids references id gthread gid entered)
7730     (while threads
7731       (when (setq references (mail-header-references (caar threads)))
7732         (setq id (mail-header-id (caar threads)))
7733         (setq ids (gnus-split-references references))
7734         (setq entered nil)
7735         (while ids
7736           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7737               (progn
7738                 (gnus-sethash (car ids) id idhashtb)
7739                 (gnus-sethash id threads thhashtb))
7740             (setq gthread (gnus-gethash gid thhashtb))
7741             (unless entered
7742               ;; We enter a dummy root into the thread, if we
7743               ;; haven't done that already.
7744               (unless (stringp (caar gthread))
7745                 (setcar gthread (list (mail-header-subject (caar gthread))
7746                                       (car gthread))))
7747               ;; We add this new gathered thread to this gathered
7748               ;; thread.
7749               (setcdr (car gthread)
7750                       (nconc (cdar gthread) (list (car threads)))))
7751             ;; Add it into the thread hash table.
7752             (gnus-sethash id gthread thhashtb)
7753             (setq entered t)
7754             ;; Remove it from the list of threads.
7755             (setcdr prev (cdr threads))
7756             (setq threads prev))
7757           (setq ids (cdr ids))))
7758       (setq prev threads)
7759       (setq threads (cdr threads)))
7760     result))
7761
7762 (defun gnus-sort-gathered-threads (threads)
7763   "Sort subtreads inside each gathered thread by article number."
7764   (let ((result threads))
7765     (while threads
7766       (when (stringp (caar threads))
7767         (setcdr (car threads)
7768                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7769       (setq threads (cdr threads)))
7770     result))
7771
7772 (defun gnus-make-threads ()
7773   "Go through the dependency hashtb and find the roots.  Return all threads."
7774   (let (threads)
7775     (mapatoms
7776      (lambda (refs)
7777        (unless (car (symbol-value refs))
7778          ;; These threads do not refer back to any other articles,
7779          ;; so they're roots.
7780          (setq threads (append (cdr (symbol-value refs)) threads))))
7781      gnus-newsgroup-dependencies)
7782     threads))
7783
7784 (defun gnus-build-sparse-threads ()
7785   (let ((headers gnus-newsgroup-headers)
7786         (deps gnus-newsgroup-dependencies)
7787         header references generation relations 
7788         cthread subject child end pthread relation)
7789     ;; First we create an alist of generations/relations, where 
7790     ;; generations is how much we trust the ralation, and the relation
7791     ;; is parent/child.
7792     (gnus-message 7 "Making sparse threads...")
7793     (save-excursion
7794       (nnheader-set-temp-buffer " *gnus sparse threads*")
7795       (while (setq header (pop headers))
7796         (when (and (setq references (mail-header-references header))
7797                    (not (string= references "")))
7798           (insert references)
7799           (setq child (mail-header-id header)
7800                 subject (mail-header-subject header))
7801           (setq generation 0)
7802           (while (search-backward ">" nil t)
7803             (setq end (1+ (point)))
7804             (when (search-backward "<" nil t)
7805               (push (list (incf generation) 
7806                           child (setq child (buffer-substring (point) end))
7807                           subject)
7808                     relations)))
7809           (push (list (1+ generation) child nil subject) relations)
7810           (erase-buffer)))
7811       (kill-buffer (current-buffer)))
7812     ;; Sort over trustworthiness.
7813     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7814     (while (setq relation (pop relations))
7815       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7816                 (unless (car (symbol-value cthread))
7817                   ;; Make this article the parent of these threads.
7818                   (setcar (symbol-value cthread)
7819                           (vector gnus-reffed-article-number 
7820                                   (cadddr relation) 
7821                                   "" ""
7822                                   (cadr relation) 
7823                                   (or (caddr relation) "") 0 0 "")))
7824               (set cthread (list (vector gnus-reffed-article-number
7825                                          (cadddr relation) 
7826                                          "" "" (cadr relation) 
7827                                          (or (caddr relation) "") 0 0 ""))))
7828         (push gnus-reffed-article-number gnus-newsgroup-limit)
7829         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7830         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7831               gnus-newsgroup-reads)
7832         (decf gnus-reffed-article-number)
7833         ;; Make this new thread the child of its parent.
7834         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7835             (setcdr (symbol-value pthread)
7836                     (nconc (cdr (symbol-value pthread))
7837                            (list (symbol-value cthread))))
7838           (set pthread (list nil (symbol-value cthread))))))
7839     (gnus-message 7 "Making sparse threads...done")))
7840
7841 (defun gnus-build-old-threads ()
7842   ;; Look at all the articles that refer back to old articles, and
7843   ;; fetch the headers for the articles that aren't there.  This will
7844   ;; build complete threads - if the roots haven't been expired by the
7845   ;; server, that is.
7846   (let (id heads)
7847     (mapatoms
7848      (lambda (refs)
7849        (when (not (car (symbol-value refs)))
7850          (setq heads (cdr (symbol-value refs)))
7851          (while heads
7852            (if (memq (mail-header-number (caar heads))
7853                      gnus-newsgroup-dormant)
7854                (setq heads (cdr heads))
7855              (setq id (symbol-name refs))
7856              (while (and (setq id (gnus-build-get-header id))
7857                          (not (car (gnus-gethash
7858                                     id gnus-newsgroup-dependencies)))))
7859              (setq heads nil)))))
7860      gnus-newsgroup-dependencies)))
7861
7862 (defun gnus-build-get-header (id)
7863   ;; Look through the buffer of NOV lines and find the header to
7864   ;; ID.  Enter this line into the dependencies hash table, and return
7865   ;; the id of the parent article (if any).
7866   (let ((deps gnus-newsgroup-dependencies)
7867         found header)
7868     (prog1
7869         (save-excursion
7870           (set-buffer nntp-server-buffer)
7871           (goto-char (point-min))
7872           (while (and (not found) (search-forward id nil t))
7873             (beginning-of-line)
7874             (setq found (looking-at
7875                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7876                                  (regexp-quote id))))
7877             (or found (beginning-of-line 2)))
7878           (when found
7879             (beginning-of-line)
7880             (and
7881              (setq header (gnus-nov-parse-line
7882                            (read (current-buffer)) deps))
7883              (gnus-parent-id (mail-header-references header)))))
7884       (when header
7885         (let ((number (mail-header-number header)))
7886           (push number gnus-newsgroup-limit)
7887           (push header gnus-newsgroup-headers)
7888           (if (memq number gnus-newsgroup-unselected)
7889               (progn
7890                 (push number gnus-newsgroup-unreads)
7891                 (setq gnus-newsgroup-unselected
7892                       (delq number gnus-newsgroup-unselected)))
7893             (push number gnus-newsgroup-ancient)))))))
7894
7895 (defun gnus-summary-update-article (article &optional header)
7896   "Update ARTICLE in the summary buffer."
7897   (set-buffer gnus-summary-buffer)
7898   (let* ((header (or header (gnus-summary-article-header article)))
7899          (id (mail-header-id header))
7900          (data (gnus-data-find article))
7901          (thread (gnus-id-to-thread id))
7902          (parent
7903           (gnus-id-to-thread (or (gnus-parent-id 
7904                                   (mail-header-references header))
7905                                  "tull")))
7906          (buffer-read-only nil)
7907          (old (car thread))
7908          (number (mail-header-number header))
7909          pos)
7910     (when thread
7911       (setcar thread nil)
7912       (when parent
7913         (delq thread parent))
7914       (if (gnus-summary-insert-subject id header)
7915           ;; Set the (possibly) new article number in the data structure.
7916           (gnus-data-set-number data (gnus-id-to-article id))
7917         (setcar thread old)
7918         nil))))
7919
7920 (defun gnus-rebuild-thread (id)
7921   "Rebuild the thread containing ID."
7922   (let ((buffer-read-only nil)
7923         current thread data)
7924     (if (not gnus-show-threads)
7925         (setq thread (list (car (gnus-id-to-thread id))))
7926       ;; Get the thread this article is part of.
7927       (setq thread (gnus-remove-thread id)))
7928     (setq current (save-excursion
7929                     (and (zerop (forward-line -1))
7930                          (gnus-summary-article-number))))
7931     ;; If this is a gathered thread, we have to go some re-gathering.
7932     (when (stringp (car thread))
7933       (let ((subject (car thread))
7934             roots thr)
7935         (setq thread (cdr thread))
7936         (while thread
7937           (unless (memq (setq thr (gnus-id-to-thread
7938                                       (gnus-root-id
7939                                        (mail-header-id (caar thread)))))
7940                         roots)
7941             (push thr roots))
7942           (setq thread (cdr thread)))
7943         ;; We now have all (unique) roots.
7944         (if (= (length roots) 1)
7945             ;; All the loose roots are now one solid root.
7946             (setq thread (car roots))
7947           (setq thread (cons subject (gnus-sort-threads roots))))))
7948     (let (threads)
7949       ;; We then insert this thread into the summary buffer.
7950       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7951         (gnus-summary-prepare-threads (list thread))
7952         (setq data (nreverse gnus-newsgroup-data))
7953         (setq threads gnus-newsgroup-threads))
7954       ;; We splice the new data into the data structure.
7955       (gnus-data-enter-list current data)
7956       (gnus-data-compute-positions)
7957       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7958
7959 (defun gnus-id-to-thread (id)
7960   "Return the (sub-)thread where ID appears."
7961   (gnus-gethash id gnus-newsgroup-dependencies))
7962
7963 (defun gnus-id-to-article (id)
7964   "Return the article number of ID."
7965   (let ((thread (gnus-id-to-thread id)))
7966     (when thread
7967       (mail-header-number (car thread)))))
7968
7969 (defun gnus-id-to-header (id)
7970   "Return the article headers of ID."
7971   (car (gnus-id-to-thread id)))
7972
7973 (defun gnus-article-displayed-root-p (article)
7974   "Say whether ARTICLE is a root(ish) article."
7975   (let ((level (gnus-summary-thread-level article))
7976         particle)
7977     (cond 
7978      ((null level) nil)
7979      ((zerop level) t)
7980      ((and (= 1 level)
7981            (null (setq particle (gnus-id-to-article
7982                                  (gnus-parent-id 
7983                                   (mail-header-references 
7984                                    (gnus-summary-article-header article))))))
7985            (null (gnus-summary-thread-level particle)))))))
7986
7987 (defun gnus-root-id (id)
7988   "Return the id of the root of the thread where ID appears."
7989   (let (last-id prev)
7990     (while (and id (setq prev (car (gnus-gethash 
7991                                     id gnus-newsgroup-dependencies))))
7992       (setq last-id id
7993             id (gnus-parent-id (mail-header-references prev))))
7994     last-id))
7995
7996 (defun gnus-remove-thread (id &optional dont-remove)
7997   "Remove the thread that has ID in it."
7998   (let ((dep gnus-newsgroup-dependencies)
7999         headers thread last-id)
8000     ;; First go up in this thread until we find the root.
8001     (setq last-id (gnus-root-id id))
8002     (setq headers (list (car (gnus-id-to-thread last-id))
8003                         (caadr (gnus-id-to-thread last-id))))
8004     ;; We have now found the real root of this thread.  It might have
8005     ;; been gathered into some loose thread, so we have to search
8006     ;; through the threads to find the thread we wanted.
8007     (let ((threads gnus-newsgroup-threads)
8008           sub)
8009       (while threads
8010         (setq sub (car threads))
8011         (if (stringp (car sub))
8012             ;; This is a gathered threads, so we look at the roots
8013             ;; below it to find whether this article in in this
8014             ;; gathered root.
8015             (progn
8016               (setq sub (cdr sub))
8017               (while sub
8018                 (when (member (caar sub) headers)
8019                   (setq thread (car threads)
8020                         threads nil
8021                         sub nil))
8022                 (setq sub (cdr sub))))
8023           ;; It's an ordinary thread, so we check it.
8024           (when (eq (car sub) (car headers))
8025             (setq thread sub
8026                   threads nil)))
8027         (setq threads (cdr threads)))
8028       ;; If this article is in no thread, then it's a root.
8029       (if thread
8030           (unless dont-remove
8031             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8032         (setq thread (gnus-gethash last-id dep)))
8033       (when thread
8034         (prog1
8035             thread ; We return this thread.
8036           (unless dont-remove
8037             (if (stringp (car thread))
8038                 (progn
8039                   ;; If we use dummy roots, then we have to remove the
8040                   ;; dummy root as well.
8041                   (when (eq gnus-summary-make-false-root 'dummy)
8042                     ;; Uhm.
8043                     )
8044                   (setq thread (cdr thread))
8045                   (while thread
8046                     (gnus-remove-thread-1 (car thread))
8047                     (setq thread (cdr thread))))
8048               (gnus-remove-thread-1 thread))))))))
8049
8050 (defun gnus-remove-thread-1 (thread)
8051   "Remove the thread THREAD recursively."
8052   (let ((number (mail-header-number (car thread)))
8053         pos)
8054     (when (setq pos (text-property-any
8055                      (point-min) (point-max) 'gnus-number number))
8056       (goto-char pos)
8057       (gnus-delete-line)
8058       (gnus-data-remove number))
8059     (setq thread (cdr thread))
8060     (while thread
8061       (gnus-remove-thread-1 (pop thread)))))
8062
8063 (defun gnus-sort-threads (threads)
8064   "Sort THREADS."
8065   (if (not gnus-thread-sort-functions)
8066       threads
8067     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8068                     (car gnus-thread-sort-functions)
8069                   `(lambda (t1 t2)
8070                      ,(gnus-make-sort-function 
8071                        (reverse gnus-thread-sort-functions))))))
8072       (gnus-message 7 "Sorting threads...")
8073       (prog1
8074           (sort threads func)
8075         (gnus-message 7 "Sorting threads...done")))))
8076
8077 (defun gnus-sort-articles (articles)
8078   "Sort ARTICLES."
8079   (when gnus-article-sort-functions
8080     (let ((func (if (= 1 (length gnus-article-sort-functions))
8081                     (car gnus-article-sort-functions)
8082                   `(lambda (t1 t2)
8083                      ,(gnus-make-sort-function 
8084                        (reverse gnus-article-sort-functions))))))
8085       (gnus-message 7 "Sorting articles...")
8086       (prog1
8087           (setq gnus-newsgroup-headers (sort articles func))
8088         (gnus-message 7 "Sorting articles...done")))))
8089
8090 (defun gnus-make-sort-function (funs)
8091   "Return a composite sort condition based on the functions in FUNC."
8092   (if (cdr funs)
8093       `(or (,(car funs) t1 t2)
8094            (and (not (,(car funs) t2 t1))
8095                 ,(gnus-make-sort-function (cdr funs))))
8096     `(,(car funs) t1 t2)))
8097                  
8098 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8099 (defmacro gnus-thread-header (thread)
8100   ;; Return header of first article in THREAD.
8101   ;; Note that THREAD must never, ever be anything else than a variable -
8102   ;; using some other form will lead to serious barfage.
8103   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8104   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8105   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8106         (vector thread) 2))
8107
8108 (defsubst gnus-article-sort-by-number (h1 h2)
8109   "Sort articles by article number."
8110   (< (mail-header-number h1)
8111      (mail-header-number h2)))
8112
8113 (defun gnus-thread-sort-by-number (h1 h2)
8114   "Sort threads by root article number."
8115   (gnus-article-sort-by-number
8116    (gnus-thread-header h1) (gnus-thread-header h2)))
8117
8118 (defsubst gnus-article-sort-by-author (h1 h2)
8119   "Sort articles by root author."
8120   (string-lessp
8121    (let ((extract (funcall
8122                    gnus-extract-address-components
8123                    (mail-header-from h1))))
8124      (or (car extract) (cdr extract)))
8125    (let ((extract (funcall
8126                    gnus-extract-address-components
8127                    (mail-header-from h2))))
8128      (or (car extract) (cdr extract)))))
8129
8130 (defun gnus-thread-sort-by-author (h1 h2)
8131   "Sort threads by root author."
8132   (gnus-article-sort-by-author
8133    (gnus-thread-header h1)  (gnus-thread-header h2)))
8134
8135 (defsubst gnus-article-sort-by-subject (h1 h2)
8136   "Sort articles by root subject."
8137   (string-lessp
8138    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8139    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8140
8141 (defun gnus-thread-sort-by-subject (h1 h2)
8142   "Sort threads by root subject."
8143   (gnus-article-sort-by-subject
8144    (gnus-thread-header h1) (gnus-thread-header h2)))
8145
8146 (defsubst gnus-article-sort-by-date (h1 h2)
8147   "Sort articles by root article date."
8148   (string-lessp
8149    (gnus-sortable-date (mail-header-date h1))
8150    (gnus-sortable-date (mail-header-date h2))))
8151
8152 (defun gnus-thread-sort-by-date (h1 h2)
8153   "Sort threads by root article date."
8154   (gnus-article-sort-by-date
8155    (gnus-thread-header h1) (gnus-thread-header h2)))
8156
8157 (defsubst gnus-article-sort-by-score (h1 h2)
8158   "Sort articles by root article score.
8159 Unscored articles will be counted as having a score of zero."
8160   (> (or (cdr (assq (mail-header-number h1)
8161                     gnus-newsgroup-scored))
8162          gnus-summary-default-score 0)
8163      (or (cdr (assq (mail-header-number h2)
8164                     gnus-newsgroup-scored))
8165          gnus-summary-default-score 0)))
8166
8167 (defun gnus-thread-sort-by-score (h1 h2)
8168   "Sort threads by root article score."
8169   (gnus-article-sort-by-score
8170    (gnus-thread-header h1) (gnus-thread-header h2)))
8171
8172 (defun gnus-thread-sort-by-total-score (h1 h2)
8173   "Sort threads by the sum of all scores in the thread.
8174 Unscored articles will be counted as having a score of zero."
8175   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8176
8177 (defun gnus-thread-total-score (thread)
8178   ;;  This function find the total score of THREAD.
8179   (if (consp thread)
8180       (if (stringp (car thread))
8181           (apply gnus-thread-score-function 0
8182                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8183         (gnus-thread-total-score-1 thread))
8184     (gnus-thread-total-score-1 (list thread))))
8185
8186 (defun gnus-thread-total-score-1 (root)
8187   ;; This function find the total score of the thread below ROOT.
8188   (setq root (car root))
8189   (apply gnus-thread-score-function
8190          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8191              gnus-summary-default-score 0)
8192          (mapcar 'gnus-thread-total-score
8193                  (cdr (gnus-gethash (mail-header-id root)
8194                                     gnus-newsgroup-dependencies)))))
8195
8196 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8197 (defvar gnus-tmp-prev-subject nil)
8198 (defvar gnus-tmp-false-parent nil)
8199 (defvar gnus-tmp-root-expunged nil)
8200 (defvar gnus-tmp-dummy-line nil)
8201
8202 (defun gnus-summary-prepare-threads (threads)
8203   "Prepare summary buffer from THREADS and indentation LEVEL.
8204 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8205 or a straight list of headers."
8206   (gnus-message 7 "Generating summary...")
8207
8208   (setq gnus-newsgroup-threads threads)
8209   (beginning-of-line)
8210
8211   (let ((gnus-tmp-level 0)
8212         (default-score (or gnus-summary-default-score 0))
8213         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8214         thread number subject stack state gnus-tmp-gathered beg-match
8215         new-roots gnus-tmp-new-adopts thread-end
8216         gnus-tmp-header gnus-tmp-unread
8217         gnus-tmp-replied gnus-tmp-subject-or-nil
8218         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8219         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8220         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8221
8222     (setq gnus-tmp-prev-subject nil)
8223
8224     (if (vectorp (car threads))
8225         ;; If this is a straight (sic) list of headers, then a
8226         ;; threaded summary display isn't required, so we just create
8227         ;; an unthreaded one.
8228         (gnus-summary-prepare-unthreaded threads)
8229
8230       ;; Do the threaded display.
8231
8232       (while (or threads stack gnus-tmp-new-adopts new-roots)
8233
8234         (if (and (= gnus-tmp-level 0)
8235                  (not (setq gnus-tmp-dummy-line nil))
8236                  (or (not stack)
8237                      (= (caar stack) 0))
8238                  (not gnus-tmp-false-parent)
8239                  (or gnus-tmp-new-adopts new-roots))
8240             (if gnus-tmp-new-adopts
8241                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8242                       thread (list (car gnus-tmp-new-adopts))
8243                       gnus-tmp-header (caar thread)
8244                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8245               (if new-roots
8246                   (setq thread (list (car new-roots))
8247                         gnus-tmp-header (caar thread)
8248                         new-roots (cdr new-roots))))
8249
8250           (if threads
8251               ;; If there are some threads, we do them before the
8252               ;; threads on the stack.
8253               (setq thread threads
8254                     gnus-tmp-header (caar thread))
8255             ;; There were no current threads, so we pop something off
8256             ;; the stack.
8257             (setq state (car stack)
8258                   gnus-tmp-level (car state)
8259                   thread (cdr state)
8260                   stack (cdr stack)
8261                   gnus-tmp-header (caar thread))))
8262
8263         (setq gnus-tmp-false-parent nil)
8264         (setq gnus-tmp-root-expunged nil)
8265         (setq thread-end nil)
8266
8267         (if (stringp gnus-tmp-header)
8268             ;; The header is a dummy root.
8269             (cond
8270              ((eq gnus-summary-make-false-root 'adopt)
8271               ;; We let the first article adopt the rest.
8272               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8273                                                (cddar thread)))
8274               (setq gnus-tmp-gathered
8275                     (nconc (mapcar
8276                             (lambda (h) (mail-header-number (car h)))
8277                             (cddar thread))
8278                            gnus-tmp-gathered))
8279               (setq thread (cons (list (caar thread)
8280                                        (cadar thread))
8281                                  (cdr thread)))
8282               (setq gnus-tmp-level -1
8283                     gnus-tmp-false-parent t))
8284              ((eq gnus-summary-make-false-root 'empty)
8285               ;; We print adopted articles with empty subject fields.
8286               (setq gnus-tmp-gathered
8287                     (nconc (mapcar
8288                             (lambda (h) (mail-header-number (car h)))
8289                             (cddar thread))
8290                            gnus-tmp-gathered))
8291               (setq gnus-tmp-level -1))
8292              ((eq gnus-summary-make-false-root 'dummy)
8293               ;; We remember that we probably want to output a dummy
8294               ;; root.
8295               (setq gnus-tmp-dummy-line gnus-tmp-header)
8296               (setq gnus-tmp-prev-subject gnus-tmp-header))
8297              (t
8298               ;; We do not make a root for the gathered
8299               ;; sub-threads at all.
8300               (setq gnus-tmp-level -1)))
8301
8302           (setq number (mail-header-number gnus-tmp-header)
8303                 subject (mail-header-subject gnus-tmp-header))
8304
8305           (cond
8306            ;; If the thread has changed subject, we might want to make
8307            ;; this subthread into a root.
8308            ((and (null gnus-thread-ignore-subject)
8309                  (not (zerop gnus-tmp-level))
8310                  gnus-tmp-prev-subject
8311                  (not (inline
8312                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8313             (setq new-roots (nconc new-roots (list (car thread)))
8314                   thread-end t
8315                   gnus-tmp-header nil))
8316            ;; If the article lies outside the current limit,
8317            ;; then we do not display it.
8318            ((and (not (memq number gnus-newsgroup-limit))
8319                  (not gnus-tmp-dummy-line))
8320             (setq gnus-tmp-gathered
8321                   (nconc (mapcar
8322                           (lambda (h) (mail-header-number (car h)))
8323                           (cdar thread))
8324                          gnus-tmp-gathered))
8325             (setq gnus-tmp-new-adopts (if (cdar thread)
8326                                           (append gnus-tmp-new-adopts
8327                                                   (cdar thread))
8328                                         gnus-tmp-new-adopts)
8329                   thread-end t
8330                   gnus-tmp-header nil)
8331             (when (zerop gnus-tmp-level)
8332               (setq gnus-tmp-root-expunged t)))
8333            ;; Perhaps this article is to be marked as read?
8334            ((and gnus-summary-mark-below
8335                  (< (or (cdr (assq number gnus-newsgroup-scored))
8336                         default-score)
8337                     gnus-summary-mark-below)
8338                  ;; Don't touch sparse articles.
8339                  (not (memq number gnus-newsgroup-sparse)))
8340             (setq gnus-newsgroup-unreads
8341                   (delq number gnus-newsgroup-unreads))
8342             (if gnus-newsgroup-auto-expire
8343                 (push number gnus-newsgroup-expirable)
8344               (push (cons number gnus-low-score-mark)
8345                     gnus-newsgroup-reads))))
8346
8347           (when gnus-tmp-header
8348             ;; We may have an old dummy line to output before this
8349             ;; article.
8350             (when gnus-tmp-dummy-line
8351               (gnus-summary-insert-dummy-line
8352                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8353               (setq gnus-tmp-dummy-line nil))
8354
8355             ;; Compute the mark.
8356             (setq
8357              gnus-tmp-unread
8358              (cond
8359               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8360               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8361               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8362               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8363               (t (or (cdr (assq number gnus-newsgroup-reads))
8364                      gnus-ancient-mark))))
8365
8366             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8367                                   gnus-tmp-header gnus-tmp-level)
8368                   gnus-newsgroup-data)
8369
8370             ;; Actually insert the line.
8371             (setq
8372              gnus-tmp-subject-or-nil
8373              (cond
8374               ((and gnus-thread-ignore-subject
8375                     gnus-tmp-prev-subject
8376                     (not (inline (gnus-subject-equal
8377                                   gnus-tmp-prev-subject subject))))
8378                subject)
8379               ((zerop gnus-tmp-level)
8380                (if (and (eq gnus-summary-make-false-root 'empty)
8381                         (memq number gnus-tmp-gathered)
8382                         gnus-tmp-prev-subject
8383                         (inline (gnus-subject-equal
8384                                  gnus-tmp-prev-subject subject)))
8385                    gnus-summary-same-subject
8386                  subject))
8387               (t gnus-summary-same-subject)))
8388             (if (and (eq gnus-summary-make-false-root 'adopt)
8389                      (= gnus-tmp-level 1)
8390                      (memq number gnus-tmp-gathered))
8391                 (setq gnus-tmp-opening-bracket ?\<
8392                       gnus-tmp-closing-bracket ?\>)
8393               (setq gnus-tmp-opening-bracket ?\[
8394                     gnus-tmp-closing-bracket ?\]))
8395             (setq
8396              gnus-tmp-indentation
8397              (aref gnus-thread-indent-array gnus-tmp-level)
8398              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8399              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8400                                 gnus-summary-default-score 0)
8401              gnus-tmp-score-char
8402              (if (or (null gnus-summary-default-score)
8403                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8404                          gnus-summary-zcore-fuzz)) ? 
8405                (if (< gnus-tmp-score gnus-summary-default-score)
8406                    gnus-score-below-mark gnus-score-over-mark))
8407              gnus-tmp-replied
8408              (cond ((memq number gnus-newsgroup-processable)
8409                     gnus-process-mark)
8410                    ((memq number gnus-newsgroup-cached)
8411                     gnus-cached-mark)
8412                    ((memq number gnus-newsgroup-replied)
8413                     gnus-replied-mark)
8414                    (t gnus-unread-mark))
8415              gnus-tmp-from (mail-header-from gnus-tmp-header)
8416              gnus-tmp-name
8417              (cond
8418               ((string-match "(.+)" gnus-tmp-from)
8419                (substring gnus-tmp-from
8420                           (1+ (match-beginning 0)) (1- (match-end 0))))
8421               ((string-match "<[^>]+> *$" gnus-tmp-from)
8422                (setq beg-match (match-beginning 0))
8423                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8424                         (substring gnus-tmp-from (1+ (match-beginning 0))
8425                                    (1- (match-end 0))))
8426                    (substring gnus-tmp-from 0 beg-match)))
8427               (t gnus-tmp-from)))
8428             (when (string= gnus-tmp-name "")
8429               (setq gnus-tmp-name gnus-tmp-from))
8430             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8431             (put-text-property
8432              (point)
8433              (progn (eval gnus-summary-line-format-spec) (point))
8434              'gnus-number number)
8435             (when gnus-visual-p
8436               (forward-line -1)
8437               (run-hooks 'gnus-summary-update-hook)
8438               (forward-line 1))
8439
8440             (setq gnus-tmp-prev-subject subject)))
8441
8442         (when (nth 1 thread)
8443           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8444         (incf gnus-tmp-level)
8445         (setq threads (if thread-end nil (cdar thread)))
8446         (unless threads
8447           (setq gnus-tmp-level 0)))))
8448   (gnus-message 7 "Generating summary...done"))
8449
8450 (defun gnus-summary-prepare-unthreaded (headers)
8451   "Generate an unthreaded summary buffer based on HEADERS."
8452   (let (header number mark)
8453
8454     (while headers
8455       (setq header (car headers)
8456             headers (cdr headers)
8457             number (mail-header-number header))
8458
8459       ;; We may have to root out some bad articles...
8460       (when (memq number gnus-newsgroup-limit)
8461         (when (and gnus-summary-mark-below
8462                    (< (or (cdr (assq number gnus-newsgroup-scored))
8463                           gnus-summary-default-score 0)
8464                       gnus-summary-mark-below))
8465           (setq gnus-newsgroup-unreads
8466                 (delq number gnus-newsgroup-unreads))
8467           (if gnus-newsgroup-auto-expire
8468               (push number gnus-newsgroup-expirable)
8469             (push (cons number gnus-low-score-mark)
8470                   gnus-newsgroup-reads)))
8471
8472         (setq mark
8473               (cond
8474                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8475                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8476                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8477                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8478                (t (or (cdr (assq number gnus-newsgroup-reads))
8479                       gnus-ancient-mark))))
8480         (setq gnus-newsgroup-data
8481               (cons (gnus-data-make number mark (1+ (point)) header 0)
8482                     gnus-newsgroup-data))
8483         (gnus-summary-insert-line
8484          header 0 nil mark (memq number gnus-newsgroup-replied)
8485          (memq number gnus-newsgroup-expirable)
8486          (mail-header-subject header) nil
8487          (cdr (assq number gnus-newsgroup-scored))
8488          (memq number gnus-newsgroup-processable))))))
8489
8490 (defun gnus-select-newsgroup (group &optional read-all)
8491   "Select newsgroup GROUP.
8492 If READ-ALL is non-nil, all articles in the group are selected."
8493   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8494          (info (nth 2 entry))
8495          articles fetched-articles cached)
8496
8497     (or (gnus-check-server
8498          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8499         (error "Couldn't open server"))
8500
8501     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8502         (gnus-activate-group group) ; Or we can activate it...
8503         (progn ; Or we bug out.
8504           (when (equal major-mode 'gnus-summary-mode)
8505             (kill-buffer (current-buffer)))
8506           (error "Couldn't request group %s: %s"
8507                  group (gnus-status-message group))))
8508
8509     (setq gnus-newsgroup-name group)
8510     (setq gnus-newsgroup-unselected nil)
8511     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8512
8513     (and gnus-asynchronous
8514          (gnus-check-backend-function
8515           'request-asynchronous gnus-newsgroup-name)
8516          (setq gnus-newsgroup-async
8517                (gnus-request-asynchronous gnus-newsgroup-name)))
8518
8519     ;; Adjust and set lists of article marks.
8520     (when info
8521       (gnus-adjust-marked-articles info))
8522
8523     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8524     (when (gnus-virtual-group-p group)
8525       (setq cached gnus-newsgroup-cached))
8526
8527     (setq gnus-newsgroup-unreads
8528           (gnus-set-difference
8529            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8530            gnus-newsgroup-dormant))
8531
8532     (setq gnus-newsgroup-processable nil)
8533
8534     (setq articles (gnus-articles-to-read group read-all))
8535
8536     (cond
8537      ((null articles)
8538       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8539       'quit)
8540      ((eq articles 0) nil)
8541      (t
8542       ;; Init the dependencies hash table.
8543       (setq gnus-newsgroup-dependencies
8544             (gnus-make-hashtable (length articles)))
8545       ;; Retrieve the headers and read them in.
8546       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8547       (setq gnus-newsgroup-headers
8548             (if (eq 'nov
8549                     (setq gnus-headers-retrieved-by
8550                           (gnus-retrieve-headers
8551                            articles gnus-newsgroup-name
8552                            ;; We might want to fetch old headers, but
8553                            ;; not if there is only 1 article.
8554                            (and gnus-fetch-old-headers
8555                                 (or (and
8556                                      (not (eq gnus-fetch-old-headers 'some))
8557                                      (not (numberp gnus-fetch-old-headers)))
8558                                     (> (length articles) 1))))))
8559                 (gnus-get-newsgroup-headers-xover articles)
8560               (gnus-get-newsgroup-headers)))
8561       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8562
8563       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8564       (when cached
8565         (setq gnus-newsgroup-cached cached))
8566
8567       ;; Set the initial limit.
8568       (setq gnus-newsgroup-limit (copy-sequence articles))
8569       ;; Remove canceled articles from the list of unread articles.
8570       (setq gnus-newsgroup-unreads
8571             (gnus-set-sorted-intersection
8572              gnus-newsgroup-unreads
8573              (setq fetched-articles
8574                    (mapcar (lambda (headers) (mail-header-number headers))
8575                            gnus-newsgroup-headers))))
8576       ;; Removed marked articles that do not exist.
8577       (gnus-update-missing-marks
8578        (gnus-sorted-complement fetched-articles articles))
8579       ;; We might want to build some more threads first.
8580       (and gnus-fetch-old-headers
8581            (eq gnus-headers-retrieved-by 'nov)
8582            (gnus-build-old-threads))
8583       ;; Check whether auto-expire is to be done in this group.
8584       (setq gnus-newsgroup-auto-expire
8585             (gnus-group-auto-expirable-p group))
8586       ;; Set up the article buffer now, if necessary.
8587       (unless gnus-single-article-buffer
8588         (gnus-article-setup-buffer))
8589       ;; First and last article in this newsgroup.
8590       (and gnus-newsgroup-headers
8591            (setq gnus-newsgroup-begin
8592                  (mail-header-number (car gnus-newsgroup-headers)))
8593            (setq gnus-newsgroup-end
8594                  (mail-header-number
8595                   (gnus-last-element gnus-newsgroup-headers))))
8596       (setq gnus-reffed-article-number -1)
8597       ;; GROUP is successfully selected.
8598       (or gnus-newsgroup-headers t)))))
8599
8600 (defun gnus-articles-to-read (group read-all)
8601   ;; Find out what articles the user wants to read.
8602   (let* ((articles
8603           ;; Select all articles if `read-all' is non-nil, or if there
8604           ;; are no unread articles.
8605           (if (or read-all
8606                   (and (zerop (length gnus-newsgroup-marked))
8607                        (zerop (length gnus-newsgroup-unreads))))
8608               (gnus-uncompress-range (gnus-active group))
8609             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8610                           (copy-sequence gnus-newsgroup-unreads))
8611                   '<)))
8612          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8613          (scored (length scored-list))
8614          (number (length articles))
8615          (marked (+ (length gnus-newsgroup-marked)
8616                     (length gnus-newsgroup-dormant)))
8617          (select
8618           (cond
8619            ((numberp read-all)
8620             read-all)
8621            (t
8622             (condition-case ()
8623                 (cond
8624                  ((and (or (<= scored marked) (= scored number))
8625                        (numberp gnus-large-newsgroup)
8626                        (> number gnus-large-newsgroup))
8627                   (let ((input
8628                          (read-string
8629                           (format
8630                            "How many articles from %s (default %d): "
8631                            gnus-newsgroup-name number))))
8632                     (if (string-match "^[ \t]*$" input) number input)))
8633                  ((and (> scored marked) (< scored number))
8634                   (let ((input
8635                          (read-string
8636                           (format "%s %s (%d scored, %d total): "
8637                                   "How many articles from"
8638                                   group scored number))))
8639                     (if (string-match "^[ \t]*$" input)
8640                         number input)))
8641                  (t number))
8642               (quit nil))))))
8643     (setq select (if (stringp select) (string-to-number select) select))
8644     (if (or (null select) (zerop select))
8645         select
8646       (if (and (not (zerop scored)) (<= (abs select) scored))
8647           (progn
8648             (setq articles (sort scored-list '<))
8649             (setq number (length articles)))
8650         (setq articles (copy-sequence articles)))
8651
8652       (if (< (abs select) number)
8653           (if (< select 0)
8654               ;; Select the N oldest articles.
8655               (setcdr (nthcdr (1- (abs select)) articles) nil)
8656             ;; Select the N most recent articles.
8657             (setq articles (nthcdr (- number select) articles))))
8658       (setq gnus-newsgroup-unselected
8659             (gnus-sorted-intersection
8660              gnus-newsgroup-unreads
8661              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8662       articles)))
8663
8664 (defun gnus-killed-articles (killed articles)
8665   (let (out)
8666     (while articles
8667       (if (inline (gnus-member-of-range (car articles) killed))
8668           (setq out (cons (car articles) out)))
8669       (setq articles (cdr articles)))
8670     out))
8671
8672 (defun gnus-uncompress-marks (marks)
8673   "Uncompress the mark ranges in MARKS."
8674   (let ((uncompressed '(score bookmark))
8675         out)
8676     (while marks
8677       (if (memq (caar marks) uncompressed)
8678           (push (car marks) out)
8679         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8680       (setq marks (cdr marks)))
8681     out))
8682
8683 (defun gnus-adjust-marked-articles (info)
8684   "Set all article lists and remove all marks that are no longer legal."
8685   (let* ((marked-lists (gnus-info-marks info))
8686          (active (gnus-active (gnus-info-group info)))
8687          (min (car active))
8688          (max (cdr active))
8689          (types gnus-article-mark-lists)
8690          (uncompressed '(score bookmark))
8691          marks var articles article mark)
8692
8693     (while marked-lists
8694       (setq marks (pop marked-lists))
8695       (set (setq var (intern (format "gnus-newsgroup-%s"
8696                                      (car (rassq (setq mark (car marks))
8697                                                  types)))))
8698            (if (memq (car marks) uncompressed) (cdr marks)
8699              (gnus-uncompress-range (cdr marks))))
8700
8701       (setq articles (symbol-value var))
8702
8703       ;; All articles have to be subsets of the active articles.
8704       (cond
8705        ;; Adjust "simple" lists.
8706        ((memq mark '(tick dormant expirable reply killed save))
8707         (while articles
8708           (when (or (< (setq article (pop articles)) min) (> article max))
8709             (set var (delq article (symbol-value var))))))
8710        ;; Adjust assocs.
8711        ((memq mark '(score bookmark))
8712         (while articles
8713           (when (or (< (car (setq article (pop articles))) min)
8714                     (> (car article) max))
8715             (set var (delq article (symbol-value var))))))))))
8716
8717 (defun gnus-update-missing-marks (missing)
8718   "Go through the list of MISSING articles and remove them mark lists."
8719   (when missing
8720     (let ((types gnus-article-mark-lists)
8721           var m)
8722       ;; Go through all types.
8723       (while types
8724         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8725         (when (symbol-value var)
8726           ;; This list has articles.  So we delete all missing articles
8727           ;; from it.
8728           (setq m missing)
8729           (while m
8730             (set var (delq (pop m) (symbol-value var)))))))))
8731
8732 (defun gnus-update-marks ()
8733   "Enter the various lists of marked articles into the newsgroup info list."
8734   (let ((types gnus-article-mark-lists)
8735         (info (gnus-get-info gnus-newsgroup-name))
8736         (uncompressed '(score bookmark killed))
8737         type list newmarked symbol)
8738     (when info
8739       ;; Add all marks lists that are non-nil to the list of marks lists.
8740       (while types
8741         (setq type (pop types))
8742         (when (setq list (symbol-value
8743                           (setq symbol
8744                                 (intern (format "gnus-newsgroup-%s"
8745                                                 (car type))))))
8746           (push (cons (cdr type)
8747                       (if (memq (cdr type) uncompressed) list
8748                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8749                 newmarked)))
8750
8751       ;; Enter these new marks into the info of the group.
8752       (if (nthcdr 3 info)
8753           (setcar (nthcdr 3 info) newmarked)
8754         ;; Add the marks lists to the end of the info.
8755         (when newmarked
8756           (setcdr (nthcdr 2 info) (list newmarked))))
8757
8758       ;; Cut off the end of the info if there's nothing else there.
8759       (let ((i 5))
8760         (while (and (> i 2)
8761                     (not (nth i info)))
8762           (when (nthcdr (decf i) info)
8763             (setcdr (nthcdr i info) nil)))))))
8764
8765 (defun gnus-add-marked-articles (group type articles &optional info force)
8766   ;; Add ARTICLES of TYPE to the info of GROUP.
8767   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8768   ;; add, but replace marked articles of TYPE with ARTICLES.
8769   (let ((info (or info (gnus-get-info group)))
8770         (uncompressed '(score bookmark killed))
8771         marked m)
8772     (or (not info)
8773         (and (not (setq marked (nthcdr 3 info)))
8774              (or (null articles)
8775                  (setcdr (nthcdr 2 info)
8776                          (list (list (cons type (gnus-compress-sequence
8777                                                  articles t)))))))
8778         (and (not (setq m (assq type (car marked))))
8779              (or (null articles)
8780                  (setcar marked
8781                          (cons (cons type (gnus-compress-sequence articles t) )
8782                                (car marked)))))
8783         (if force
8784             (if (null articles)
8785                 (setcar (nthcdr 3 info)
8786                         (delq (assq type (car marked)) (car marked)))
8787               (setcdr m (gnus-compress-sequence articles t)))
8788           (setcdr m (gnus-compress-sequence
8789                      (sort (nconc (gnus-uncompress-range (cdr m))
8790                                   (copy-sequence articles)) '<) t))))))
8791
8792 (defun gnus-set-mode-line (where)
8793   "This function sets the mode line of the article or summary buffers.
8794 If WHERE is `summary', the summary mode line format will be used."
8795   ;; Is this mode line one we keep updated?
8796   (when (memq where gnus-updated-mode-lines)
8797     (let (mode-string)
8798       (save-excursion
8799         ;; We evaluate this in the summary buffer since these
8800         ;; variables are buffer-local to that buffer.
8801         (set-buffer gnus-summary-buffer)
8802         ;; We bind all these variables that are used in the `eval' form
8803         ;; below.
8804         (let* ((mformat (symbol-value
8805                          (intern
8806                           (format "gnus-%s-mode-line-format-spec" where))))
8807                (gnus-tmp-group-name gnus-newsgroup-name)
8808                (gnus-tmp-article-number (or gnus-current-article 0))
8809                (gnus-tmp-unread gnus-newsgroup-unreads)
8810                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8811                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8812                (gnus-tmp-unread-and-unselected
8813                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8814                             (zerop gnus-tmp-unselected)) "")
8815                       ((zerop gnus-tmp-unselected)
8816                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8817                       (t (format "{%d(+%d) more}"
8818                                  gnus-tmp-unread-and-unticked
8819                                  gnus-tmp-unselected))))
8820                (gnus-tmp-subject
8821                 (if (and gnus-current-headers
8822                          (vectorp gnus-current-headers))
8823                     (mail-header-subject gnus-current-headers) ""))
8824                max-len
8825                gnus-tmp-header);; passed as argument to any user-format-funcs
8826           (setq mode-string (eval mformat))
8827           (setq max-len (max 4 (if gnus-mode-non-string-length
8828                                    (- (frame-width)
8829                                       gnus-mode-non-string-length)
8830                                  (length mode-string))))
8831           ;; We might have to chop a bit of the string off...
8832           (when (> (length mode-string) max-len)
8833             (setq mode-string
8834                   (concat (gnus-truncate-string mode-string (- max-len 3))
8835                           "...")))
8836           ;; Pad the mode string a bit.
8837           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8838       ;; Update the mode line.
8839       (setq mode-line-buffer-identification (list mode-string))
8840       (set-buffer-modified-p t))))
8841
8842 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8843   "Go through the HEADERS list and add all Xrefs to a hash table.
8844 The resulting hash table is returned, or nil if no Xrefs were found."
8845   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8846          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8847          (xref-hashtb (make-vector 63 0))
8848          start group entry number xrefs header)
8849     (while headers
8850       (setq header (pop headers))
8851       (when (and (setq xrefs (mail-header-xref header))
8852                  (not (memq (setq number (mail-header-number header))
8853                             unreads)))
8854         (setq start 0)
8855         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8856           (setq start (match-end 0))
8857           (setq group (if prefix
8858                           (concat prefix (substring xrefs (match-beginning 1)
8859                                                     (match-end 1)))
8860                         (substring xrefs (match-beginning 1) (match-end 1))))
8861           (setq number
8862                 (string-to-int (substring xrefs (match-beginning 2)
8863                                           (match-end 2))))
8864           (if (setq entry (gnus-gethash group xref-hashtb))
8865               (setcdr entry (cons number (cdr entry)))
8866             (gnus-sethash group (cons number nil) xref-hashtb)))))
8867     (and start xref-hashtb)))
8868
8869 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8870   "Look through all the headers and mark the Xrefs as read."
8871   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8872         name entry info xref-hashtb idlist method nth4)
8873     (save-excursion
8874       (set-buffer gnus-group-buffer)
8875       (when (setq xref-hashtb
8876                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8877         (mapatoms
8878          (lambda (group)
8879            (unless (string= from-newsgroup (setq name (symbol-name group)))
8880              (setq idlist (symbol-value group))
8881              ;; Dead groups are not updated.
8882              (and (prog1
8883                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8884                             info (nth 2 entry))
8885                     (if (stringp (setq nth4 (gnus-info-method info)))
8886                         (setq nth4 (gnus-server-to-method nth4))))
8887                   ;; Only do the xrefs if the group has the same
8888                   ;; select method as the group we have just read.
8889                   (or (gnus-methods-equal-p
8890                        nth4 (gnus-find-method-for-group from-newsgroup))
8891                       virtual
8892                       (equal nth4 (setq method (gnus-find-method-for-group
8893                                                 from-newsgroup)))
8894                       (and (equal (car nth4) (car method))
8895                            (equal (nth 1 nth4) (nth 1 method))))
8896                   gnus-use-cross-reference
8897                   (or (not (eq gnus-use-cross-reference t))
8898                       virtual
8899                       ;; Only do cross-references on subscribed
8900                       ;; groups, if that is what is wanted.
8901                       (<= (gnus-info-level info) gnus-level-subscribed))
8902                   (gnus-group-make-articles-read name idlist))))
8903          xref-hashtb)))))
8904
8905 (defun gnus-group-make-articles-read (group articles)
8906   (let* ((num 0)
8907          (entry (gnus-gethash group gnus-newsrc-hashtb))
8908          (info (nth 2 entry))
8909          (active (gnus-active group))
8910          range)
8911     ;; First peel off all illegal article numbers.
8912     (if active
8913         (let ((ids articles)
8914               id first)
8915           (while ids
8916             (setq id (car ids))
8917             (if (and first (> id (cdr active)))
8918                 (progn
8919                   ;; We'll end up in this situation in one particular
8920                   ;; obscure situation.  If you re-scan a group and get
8921                   ;; a new article that is cross-posted to a different
8922                   ;; group that has not been re-scanned, you might get
8923                   ;; crossposted article that has a higher number than
8924                   ;; Gnus believes possible.  So we re-activate this
8925                   ;; group as well.  This might mean doing the
8926                   ;; crossposting thingy will *increase* the number
8927                   ;; of articles in some groups.  Tsk, tsk.
8928                   (setq active (or (gnus-activate-group group) active))))
8929             (if (or (> id (cdr active))
8930                     (< id (car active)))
8931                 (setq articles (delq id articles)))
8932             (setq ids (cdr ids)))))
8933     ;; If the read list is nil, we init it.
8934     (and active
8935          (null (gnus-info-read info))
8936          (> (car active) 1)
8937          (gnus-info-set-read info (cons 1 (1- (car active)))))
8938     ;; Then we add the read articles to the range.
8939     (gnus-info-set-read
8940      info
8941      (setq range
8942            (gnus-add-to-range
8943             (gnus-info-read info) (setq articles (sort articles '<)))))
8944     ;; Then we have to re-compute how many unread
8945     ;; articles there are in this group.
8946     (if active
8947         (progn
8948           (cond
8949            ((not range)
8950             (setq num (- (1+ (cdr active)) (car active))))
8951            ((not (listp (cdr range)))
8952             (setq num (- (cdr active) (- (1+ (cdr range))
8953                                          (car range)))))
8954            (t
8955             (while range
8956               (if (numberp (car range))
8957                   (setq num (1+ num))
8958                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
8959               (setq range (cdr range)))
8960             (setq num (- (cdr active) num))))
8961           ;; Update the number of unread articles.
8962           (setcar entry num)
8963           ;; Update the group buffer.
8964           (gnus-group-update-group group t)))))
8965
8966 (defun gnus-methods-equal-p (m1 m2)
8967   (let ((m1 (or m1 gnus-select-method))
8968         (m2 (or m2 gnus-select-method)))
8969     (or (equal m1 m2)
8970         (and (eq (car m1) (car m2))
8971              (or (not (memq 'address (assoc (symbol-name (car m1))
8972                                             gnus-valid-select-methods)))
8973                  (equal (nth 1 m1) (nth 1 m2)))))))
8974
8975 (defsubst gnus-header-value ()
8976   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8977
8978 (defvar gnus-newsgroup-none-id 0)
8979
8980 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
8981   (let ((cur nntp-server-buffer)
8982         (dependencies
8983          (or dependencies
8984              (save-excursion (set-buffer gnus-summary-buffer)
8985                              gnus-newsgroup-dependencies)))
8986         headers id id-dep ref-dep end ref)
8987     (save-excursion
8988       (set-buffer nntp-server-buffer)
8989       (let ((case-fold-search t)
8990             in-reply-to header p lines)
8991         (goto-char (point-min))
8992         ;; Search to the beginning of the next header.  Error messages
8993         ;; do not begin with 2 or 3.
8994         (while (re-search-forward "^[23][0-9]+ " nil t)
8995           (setq id nil
8996                 ref nil)
8997           ;; This implementation of this function, with nine
8998           ;; search-forwards instead of the one re-search-forward and
8999           ;; a case (which basically was the old function) is actually
9000           ;; about twice as fast, even though it looks messier.  You
9001           ;; can't have everything, I guess.  Speed and elegance
9002           ;; doesn't always go hand in hand.
9003           (setq
9004            header
9005            (vector
9006             ;; Number.
9007             (prog1
9008                 (read cur)
9009               (end-of-line)
9010               (setq p (point))
9011               (narrow-to-region (point)
9012                                 (or (and (search-forward "\n.\n" nil t)
9013                                          (- (point) 2))
9014                                     (point))))
9015             ;; Subject.
9016             (progn
9017               (goto-char p)
9018               (if (search-forward "\nsubject: " nil t)
9019                   (gnus-header-value) "(none)"))
9020             ;; From.
9021             (progn
9022               (goto-char p)
9023               (if (search-forward "\nfrom: " nil t)
9024                   (gnus-header-value) "(nobody)"))
9025             ;; Date.
9026             (progn
9027               (goto-char p)
9028               (if (search-forward "\ndate: " nil t)
9029                   (gnus-header-value) ""))
9030             ;; Message-ID.
9031             (progn
9032               (goto-char p)
9033               (if (search-forward "\nmessage-id: " nil t)
9034                   (setq id (gnus-header-value))
9035                 ;; If there was no message-id, we just fake one to make
9036                 ;; subsequent routines simpler.
9037                 (setq id (concat "none+"
9038                                  (int-to-string
9039                                   (setq gnus-newsgroup-none-id
9040                                         (1+ gnus-newsgroup-none-id)))))))
9041             ;; References.
9042             (progn
9043               (goto-char p)
9044               (if (search-forward "\nreferences: " nil t)
9045                   (prog1
9046                       (gnus-header-value)
9047                     (setq end (match-end 0))
9048                     (save-excursion
9049                       (setq ref
9050                             (buffer-substring
9051                              (progn
9052                                (end-of-line)
9053                                (search-backward ">" end t)
9054                                (1+ (point)))
9055                              (progn
9056                                (search-backward "<" end t)
9057                                (point))))))
9058                 ;; Get the references from the in-reply-to header if there
9059                 ;; were no references and the in-reply-to header looks
9060                 ;; promising.
9061                 (if (and (search-forward "\nin-reply-to: " nil t)
9062                          (setq in-reply-to (gnus-header-value))
9063                          (string-match "<[^>]+>" in-reply-to))
9064                     (setq ref (substring in-reply-to (match-beginning 0)
9065                                          (match-end 0)))
9066                   (setq ref ""))))
9067             ;; Chars.
9068             0
9069             ;; Lines.
9070             (progn
9071               (goto-char p)
9072               (if (search-forward "\nlines: " nil t)
9073                   (if (numberp (setq lines (read cur)))
9074                       lines 0)
9075                 0))
9076             ;; Xref.
9077             (progn
9078               (goto-char p)
9079               (and (search-forward "\nxref: " nil t)
9080                    (gnus-header-value)))))
9081           ;; We do the threading while we read the headers.  The
9082           ;; message-id and the last reference are both entered into
9083           ;; the same hash table.  Some tippy-toeing around has to be
9084           ;; done in case an article has arrived before the article
9085           ;; which it refers to.
9086           (if (boundp (setq id-dep (intern id dependencies)))
9087               (if (and (car (symbol-value id-dep))
9088                        (not force-new))
9089                   ;; An article with this Message-ID has already
9090                   ;; been seen, so we ignore this one, except we add
9091                   ;; any additional Xrefs (in case the two articles
9092                   ;; came from different servers).
9093                   (progn
9094                     (mail-header-set-xref
9095                      (car (symbol-value id-dep))
9096                      (concat (or (mail-header-xref
9097                                   (car (symbol-value id-dep))) "")
9098                              (or (mail-header-xref header) "")))
9099                     (setq header nil))
9100                 (setcar (symbol-value id-dep) header))
9101             (set id-dep (list header)))
9102           (when header
9103             (if (boundp (setq ref-dep (intern ref dependencies)))
9104                 (setcdr (symbol-value ref-dep)
9105                         (nconc (cdr (symbol-value ref-dep))
9106                                (list (symbol-value id-dep))))
9107               (set ref-dep (list nil (symbol-value id-dep))))
9108             (setq headers (cons header headers)))
9109           (goto-char (point-max))
9110           (widen))
9111         (nreverse headers)))))
9112
9113 ;; The following macros and functions were written by Felix Lee
9114 ;; <flee@cse.psu.edu>.
9115
9116 (defmacro gnus-nov-read-integer ()
9117   '(prog1
9118        (if (= (following-char) ?\t)
9119            0
9120          (let ((num (condition-case nil (read buffer) (error nil))))
9121            (if (numberp num) num 0)))
9122      (or (eobp) (forward-char 1))))
9123
9124 (defmacro gnus-nov-skip-field ()
9125   '(search-forward "\t" eol 'move))
9126
9127 (defmacro gnus-nov-field ()
9128   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9129
9130 ;; Goes through the xover lines and returns a list of vectors
9131 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9132                                                   force-new dependencies)
9133   "Parse the news overview data in the server buffer, and return a
9134 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9135   ;; Get the Xref when the users reads the articles since most/some
9136   ;; NNTP servers do not include Xrefs when using XOVER.
9137   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9138   (let ((cur nntp-server-buffer)
9139         (dependencies (or dependencies gnus-newsgroup-dependencies))
9140         number headers header)
9141     (save-excursion
9142       (set-buffer nntp-server-buffer)
9143       ;; Allow the user to mangle the headers before parsing them.
9144       (run-hooks 'gnus-parse-headers-hook)
9145       ;; Allow the user to mangle the headers before parsing them.
9146       (run-hooks 'gnus-parse-headers-hook)
9147       (goto-char (point-min))
9148       (while (and sequence (not (eobp)))
9149         (setq number (read cur))
9150         (while (and sequence (< (car sequence) number))
9151           (setq sequence (cdr sequence)))
9152         (and sequence
9153              (eq number (car sequence))
9154              (progn
9155                (setq sequence (cdr sequence))
9156                (if (setq header
9157                          (inline (gnus-nov-parse-line
9158                                   number dependencies force-new)))
9159                    (setq headers (cons header headers)))))
9160         (forward-line 1))
9161       (setq headers (nreverse headers)))
9162     headers))
9163
9164 ;; This function has to be called with point after the article number
9165 ;; on the beginning of the line.
9166 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9167   (let ((none 0)
9168         (eol (gnus-point-at-eol))
9169         (buffer (current-buffer))
9170         header ref id id-dep ref-dep)
9171
9172     ;; overview: [num subject from date id refs chars lines misc]
9173     (narrow-to-region (point) eol)
9174     (or (eobp) (forward-char))
9175
9176     (condition-case nil
9177         (setq header
9178               (vector
9179                number                   ; number
9180                (gnus-nov-field)         ; subject
9181                (gnus-nov-field)         ; from
9182                (gnus-nov-field)         ; date
9183                (setq id (or (gnus-nov-field)
9184                             (concat "none+"
9185                                     (int-to-string
9186                                      (setq none (1+ none)))))) ; id
9187                (progn
9188                  (save-excursion
9189                    (let ((beg (point)))
9190                      (search-forward "\t" eol)
9191                      (if (search-backward ">" beg t)
9192                          (setq ref
9193                                (buffer-substring
9194                                 (1+ (point))
9195                                 (search-backward "<" beg t)))
9196                        (setq ref nil))))
9197                  (gnus-nov-field))      ; refs
9198                (gnus-nov-read-integer)  ; chars
9199                (gnus-nov-read-integer)  ; lines
9200                (if (= (following-char) ?\n)
9201                    nil
9202                  (gnus-nov-field))      ; misc
9203                ))
9204       (error (progn
9205                (ding)
9206                (gnus-message 4 "Strange nov line")
9207                (setq header nil)
9208                (goto-char eol))))
9209
9210     (widen)
9211
9212     ;; We build the thread tree.
9213     (when header
9214       (if (boundp (setq id-dep (intern id dependencies)))
9215           (if (and (car (symbol-value id-dep))
9216                    (not force-new))
9217               ;; An article with this Message-ID has already been seen,
9218               ;; so we ignore this one, except we add any additional
9219               ;; Xrefs (in case the two articles came from different
9220               ;; servers.
9221               (progn
9222                 (mail-header-set-xref
9223                  (car (symbol-value id-dep))
9224                  (concat (or (mail-header-xref
9225                               (car (symbol-value id-dep))) "")
9226                          (or (mail-header-xref header) "")))
9227                 (setq header nil))
9228             (setcar (symbol-value id-dep) header))
9229         (set id-dep (list header))))
9230     (if header
9231         (progn
9232           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9233               (setcdr (symbol-value ref-dep)
9234                       (nconc (cdr (symbol-value ref-dep))
9235                              (list (symbol-value id-dep))))
9236             (set ref-dep (list nil (symbol-value id-dep))))))
9237     header))
9238
9239 (defun gnus-article-get-xrefs ()
9240   "Fill in the Xref value in `gnus-current-headers', if necessary.
9241 This is meant to be called in `gnus-article-internal-prepare-hook'."
9242   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9243                                  gnus-current-headers)))
9244     (or (not gnus-use-cross-reference)
9245         (not headers)
9246         (and (mail-header-xref headers)
9247              (not (string= (mail-header-xref headers) "")))
9248         (let ((case-fold-search t)
9249               xref)
9250           (save-restriction
9251             (nnheader-narrow-to-headers)
9252             (goto-char (point-min))
9253             (if (or (and (eq (downcase (following-char)) ?x)
9254                          (looking-at "Xref:"))
9255                     (search-forward "\nXref:" nil t))
9256                 (progn
9257                   (goto-char (1+ (match-end 0)))
9258                   (setq xref (buffer-substring (point)
9259                                                (progn (end-of-line) (point))))
9260                   (mail-header-set-xref headers xref))))))))
9261
9262 (defun gnus-summary-insert-subject (id &optional old-header)
9263   "Find article ID and insert the summary line for that article."
9264   (let ((header (gnus-read-header id))
9265         (number (and (numberp id) id))
9266         pos)
9267     (when header
9268       ;; Rebuild the thread that this article is part of and go to the
9269       ;; article we have fetched.
9270       (when old-header
9271         (when (setq pos (text-property-any
9272                          (point-min) (point-max) 'gnus-number 
9273                          (mail-header-number old-header)))
9274           (goto-char pos)
9275           (gnus-delete-line)
9276           (gnus-data-remove (mail-header-number old-header))))
9277       (gnus-rebuild-thread (mail-header-id header))
9278       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9279     (when (and (numberp number)
9280                (> number 0))
9281       ;; We have to update the boundaries even if we can't fetch the
9282       ;; article if ID is a number -- so that the next `P' or `N'
9283       ;; command will fetch the previous (or next) article even
9284       ;; if the one we tried to fetch this time has been canceled.
9285       (and (> number gnus-newsgroup-end)
9286            (setq gnus-newsgroup-end number))
9287       (and (< number gnus-newsgroup-begin)
9288            (setq gnus-newsgroup-begin number))
9289       (setq gnus-newsgroup-unselected
9290             (delq number gnus-newsgroup-unselected)))
9291     ;; Report back a success?
9292     (and header (mail-header-number header))))
9293
9294 (defun gnus-summary-work-articles (n)
9295   "Return a list of articles to be worked upon.  The prefix argument,
9296 the list of process marked articles, and the current article will be
9297 taken into consideration."
9298   (cond
9299    ((and n (numberp n))
9300     ;; A numerical prefix has been given.
9301     (let ((backward (< n 0))
9302           (n (abs n))
9303           articles article)
9304       (save-excursion
9305         (while
9306             (and (> n 0)
9307                  (push (setq article (gnus-summary-article-number))
9308                        articles)
9309                  (if backward
9310                      (gnus-summary-find-prev nil article)
9311                    (gnus-summary-find-next nil article)))
9312           (decf n)))
9313       (nreverse articles)))
9314    ((and (boundp 'transient-mark-mode)
9315          transient-mark-mode
9316          mark-active)
9317     ;; Work on the region between point and mark.
9318     (let ((max (max (point) (mark)))
9319           articles article)
9320       (save-excursion
9321         (goto-char (min (point) (mark)))
9322         (while
9323             (and
9324              (push (setq article (gnus-summary-article-number)) articles)
9325              (gnus-summary-find-next nil article)
9326              (< (point) max)))
9327         (nreverse articles))))
9328    (gnus-newsgroup-processable
9329     ;; There are process-marked articles present.
9330     (reverse gnus-newsgroup-processable))
9331    (t
9332     ;; Just return the current article.
9333     (list (gnus-summary-article-number)))))
9334
9335 (defun gnus-summary-search-group (&optional backward use-level)
9336   "Search for next unread newsgroup.
9337 If optional argument BACKWARD is non-nil, search backward instead."
9338   (save-excursion
9339     (set-buffer gnus-group-buffer)
9340     (if (gnus-group-search-forward
9341          backward nil (if use-level (gnus-group-group-level) nil))
9342         (gnus-group-group-name))))
9343
9344 (defun gnus-summary-best-group (&optional exclude-group)
9345   "Find the name of the best unread group.
9346 If EXCLUDE-GROUP, do not go to this group."
9347   (save-excursion
9348     (set-buffer gnus-group-buffer)
9349     (save-excursion
9350       (gnus-group-best-unread-group exclude-group))))
9351
9352 (defun gnus-summary-find-next (&optional unread article backward)
9353   (if backward (gnus-summary-find-prev)
9354     (let* ((article (or article (gnus-summary-article-number)))
9355            (arts (gnus-data-find-list article))
9356            result)
9357       (when (or (not gnus-summary-check-current)
9358                 (not unread)
9359                 (not (gnus-data-unread-p (car arts))))
9360         (setq arts (cdr arts)))
9361       (when (setq result
9362                   (if unread
9363                       (progn
9364                         (while arts
9365                           (when (gnus-data-unread-p (car arts))
9366                             (setq result (car arts)
9367                                   arts nil))
9368                           (setq arts (cdr arts)))
9369                         result)
9370                     (car arts)))
9371         (goto-char (gnus-data-pos result))
9372         (gnus-data-number result)))))
9373
9374 (defun gnus-summary-find-prev (&optional unread article)
9375   (let* ((article (or article (gnus-summary-article-number)))
9376          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9377          result)
9378     (when (or (not gnus-summary-check-current)
9379               (not unread)
9380               (not (gnus-data-unread-p (car arts))))
9381       (setq arts (cdr arts)))
9382     (if (setq result
9383               (if unread
9384                   (progn
9385                     (while arts
9386                       (and (gnus-data-unread-p (car arts))
9387                            (setq result (car arts)
9388                                  arts nil))
9389                       (setq arts (cdr arts)))
9390                     result)
9391                 (car arts)))
9392         (progn
9393           (goto-char (gnus-data-pos result))
9394           (gnus-data-number result)))))
9395
9396 (defun gnus-summary-find-subject (subject &optional unread backward article)
9397   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9398          (article (or article (gnus-summary-article-number)))
9399          (articles (gnus-data-list backward))
9400          (arts (gnus-data-find-list article articles))
9401          result)
9402     (when (or (not gnus-summary-check-current)
9403               (not unread)
9404               (not (gnus-data-unread-p (car arts))))
9405       (setq arts (cdr arts)))
9406     (while arts
9407       (and (or (not unread)
9408                (gnus-data-unread-p (car arts)))
9409            (vectorp (gnus-data-header (car arts)))
9410            (gnus-subject-equal
9411             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9412            (setq result (car arts)
9413                  arts nil))
9414       (setq arts (cdr arts)))
9415     (and result
9416          (goto-char (gnus-data-pos result))
9417          (gnus-data-number result))))
9418
9419 (defun gnus-summary-search-forward (&optional unread subject backward)
9420   "Search forward for an article.
9421 If UNREAD, look for unread articles.  If SUBJECT, look for
9422 articles with that subject.  If BACKWARD, search backward instead."
9423   (cond (subject (gnus-summary-find-subject subject unread backward))
9424         (backward (gnus-summary-find-prev unread))
9425         (t (gnus-summary-find-next unread))))
9426
9427 (defun gnus-recenter (&optional n)
9428   "Center point in window and redisplay frame.
9429 Also do horizontal recentering."
9430   (interactive "P")
9431   (when (and gnus-auto-center-summary
9432              (not (eq gnus-auto-center-summary 'vertical)))
9433     (gnus-horizontal-recenter))
9434   (recenter n))
9435
9436 (defun gnus-summary-recenter ()
9437   "Center point in the summary window.
9438 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9439 displayed, no centering will be performed."
9440   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9441   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9442   (let* ((top (cond ((< (window-height) 4) 0)
9443                     ((< (window-height) 7) 1)
9444                     (t 2)))
9445          (height (1- (window-height)))
9446          (bottom (save-excursion (goto-char (point-max))
9447                                  (forward-line (- height))
9448                                  (point)))
9449          (window (get-buffer-window (current-buffer))))
9450     ;; The user has to want it.
9451     (when gnus-auto-center-summary
9452       (when (get-buffer-window gnus-article-buffer)
9453        ;; Only do recentering when the article buffer is displayed,
9454        ;; Set the window start to either `bottom', which is the biggest
9455        ;; possible valid number, or the second line from the top,
9456        ;; whichever is the least.
9457        (set-window-start
9458         window (min bottom (save-excursion 
9459                              (forward-line (- top)) (point)))))
9460       ;; Do horizontal recentering while we're at it.
9461       (when (and (get-buffer-window (current-buffer) t)
9462                  (not (eq gnus-auto-center-summary 'vertical)))
9463         (let ((selected (selected-window)))
9464           (select-window (get-buffer-window (current-buffer) t))
9465           (gnus-summary-position-point)
9466           (gnus-horizontal-recenter)
9467           (select-window selected))))))
9468
9469 (defun gnus-horizontal-recenter ()
9470   "Recenter the current buffer horizontally."
9471   (if (< (current-column) (/ (window-width) 2))
9472       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9473     (let* ((orig (point))
9474            (end (window-end (get-buffer-window (current-buffer) t)))
9475            (max 0))
9476       ;; Find the longest line currently displayed in the window.
9477       (goto-char (window-start))
9478       (while (and (not (eobp)) 
9479                   (< (point) end))
9480         (end-of-line)
9481         (setq max (max max (current-column)))
9482         (forward-line 1))
9483       (goto-char orig)
9484       ;; Scroll horizontally to center (sort of) the point.
9485       (if (> max (window-width))
9486           (set-window-hscroll 
9487            (get-buffer-window (current-buffer) t)
9488            (min (- (current-column) (/ (window-width) 3))
9489                 (+ 2 (- max (window-width)))))
9490         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9491       max)))
9492
9493 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9494 (defun gnus-short-group-name (group &optional levels)
9495   "Collapse GROUP name LEVELS."
9496   (let* ((name "") 
9497          (foreign "")
9498          (depth 0) 
9499          (skip 1)
9500          (levels (or levels
9501                      (progn
9502                        (while (string-match "\\." group skip)
9503                          (setq skip (match-end 0)
9504                                depth (+ depth 1)))
9505                        depth))))
9506     (if (string-match ":" group)
9507         (setq foreign (substring group 0 (match-end 0))
9508               group (substring group (match-end 0))))
9509     (while group
9510       (if (and (string-match "\\." group)
9511                (> levels (- gnus-group-uncollapsed-levels 1)))
9512           (setq name (concat name (substring group 0 1))
9513                 group (substring group (match-end 0))
9514                 levels (- levels 1)
9515                 name (concat name "."))
9516         (setq name (concat foreign name group)
9517               group nil)))
9518     name))
9519
9520 (defun gnus-summary-jump-to-group (newsgroup)
9521   "Move point to NEWSGROUP in group mode buffer."
9522   ;; Keep update point of group mode buffer if visible.
9523   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9524       (save-window-excursion
9525         ;; Take care of tree window mode.
9526         (if (get-buffer-window gnus-group-buffer)
9527             (pop-to-buffer gnus-group-buffer))
9528         (gnus-group-jump-to-group newsgroup))
9529     (save-excursion
9530       ;; Take care of tree window mode.
9531       (if (get-buffer-window gnus-group-buffer)
9532           (pop-to-buffer gnus-group-buffer)
9533         (set-buffer gnus-group-buffer))
9534       (gnus-group-jump-to-group newsgroup))))
9535
9536 ;; This function returns a list of article numbers based on the
9537 ;; difference between the ranges of read articles in this group and
9538 ;; the range of active articles.
9539 (defun gnus-list-of-unread-articles (group)
9540   (let* ((read (gnus-info-read (gnus-get-info group)))
9541          (active (gnus-active group))
9542          (last (cdr active))
9543          first nlast unread)
9544     ;; If none are read, then all are unread.
9545     (if (not read)
9546         (setq first (car active))
9547       ;; If the range of read articles is a single range, then the
9548       ;; first unread article is the article after the last read
9549       ;; article.  Sounds logical, doesn't it?
9550       (if (not (listp (cdr read)))
9551           (setq first (1+ (cdr read)))
9552         ;; `read' is a list of ranges.
9553         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9554                                 (caar read))) 1)
9555             (setq first 1))
9556         (while read
9557           (if first
9558               (while (< first nlast)
9559                 (setq unread (cons first unread))
9560                 (setq first (1+ first))))
9561           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9562           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9563           (setq read (cdr read)))))
9564     ;; And add the last unread articles.
9565     (while (<= first last)
9566       (setq unread (cons first unread))
9567       (setq first (1+ first)))
9568     ;; Return the list of unread articles.
9569     (nreverse unread)))
9570
9571 (defun gnus-list-of-read-articles (group)
9572   "Return a list of unread, unticked and non-dormant articles."
9573   (let* ((info (gnus-get-info group))
9574          (marked (gnus-info-marks info))
9575          (active (gnus-active group)))
9576     (and info active
9577          (gnus-set-difference
9578           (gnus-sorted-complement
9579            (gnus-uncompress-range active)
9580            (gnus-list-of-unread-articles group))
9581           (append
9582            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9583            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9584
9585 ;; Various summary commands
9586
9587 (defun gnus-summary-universal-argument (arg)
9588   "Perform any operation on all articles that are process/prefixed."
9589   (interactive "P")
9590   (gnus-set-global-variables)
9591   (let ((articles (gnus-summary-work-articles arg))
9592         func article)
9593     (if (eq
9594          (setq
9595           func
9596           (key-binding
9597            (read-key-sequence
9598             (substitute-command-keys
9599              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9600              ))))
9601          'undefined)
9602         (progn
9603           (message "Undefined key")
9604           (ding))
9605       (save-excursion
9606         (while articles
9607           (gnus-summary-goto-subject (setq article (pop articles)))
9608           (command-execute func)
9609           (gnus-summary-remove-process-mark article)))))
9610   (gnus-summary-position-point))
9611
9612 (defun gnus-summary-toggle-truncation (&optional arg)
9613   "Toggle truncation of summary lines.
9614 With arg, turn line truncation on iff arg is positive."
9615   (interactive "P")
9616   (setq truncate-lines
9617         (if (null arg) (not truncate-lines)
9618           (> (prefix-numeric-value arg) 0)))
9619   (redraw-display))
9620
9621 (defun gnus-summary-reselect-current-group (&optional all rescan)
9622   "Exit and then reselect the current newsgroup.
9623 The prefix argument ALL means to select all articles."
9624   (interactive "P")
9625   (gnus-set-global-variables)
9626   (let ((current-subject (gnus-summary-article-number))
9627         (group gnus-newsgroup-name))
9628     (setq gnus-newsgroup-begin nil)
9629     (gnus-summary-exit)
9630     ;; We have to adjust the point of group mode buffer because the
9631     ;; current point was moved to the next unread newsgroup by
9632     ;; exiting.
9633     (gnus-summary-jump-to-group group)
9634     (when rescan
9635       (save-excursion
9636         (gnus-group-get-new-news-this-group 1)))
9637     (gnus-group-read-group all t)
9638     (gnus-summary-goto-subject current-subject)))
9639
9640 (defun gnus-summary-rescan-group (&optional all)
9641   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9642   (interactive "P")
9643   (gnus-summary-reselect-current-group all t))
9644
9645 (defun gnus-summary-update-info ()
9646   (let* ((group gnus-newsgroup-name))
9647     (when gnus-newsgroup-kill-headers
9648       (setq gnus-newsgroup-killed
9649             (gnus-compress-sequence
9650              (nconc
9651               (gnus-set-sorted-intersection
9652                (gnus-uncompress-range gnus-newsgroup-killed)
9653                (setq gnus-newsgroup-unselected
9654                      (sort gnus-newsgroup-unselected '<)))
9655               (setq gnus-newsgroup-unreads
9656                     (sort gnus-newsgroup-unreads '<))) t)))
9657     (unless (listp (cdr gnus-newsgroup-killed))
9658       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9659     (let ((headers gnus-newsgroup-headers))
9660       (run-hooks 'gnus-exit-group-hook)
9661       (unless gnus-save-score
9662         (setq gnus-newsgroup-scored nil))
9663       ;; Set the new ranges of read articles.
9664       (gnus-update-read-articles
9665        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9666       ;; Set the current article marks.
9667       (gnus-update-marks)
9668       ;; Do the cross-ref thing.
9669       (when gnus-use-cross-reference
9670         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9671       ;; Do adaptive scoring, and possibly save score files.
9672       (when gnus-newsgroup-adaptive
9673         (gnus-score-adaptive))
9674       (when gnus-use-scoring
9675         (gnus-score-save))
9676       ;; Do not switch windows but change the buffer to work.
9677       (set-buffer gnus-group-buffer)
9678       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9679           (gnus-group-update-group group)))))
9680
9681 (defun gnus-summary-exit (&optional temporary)
9682   "Exit reading current newsgroup, and then return to group selection mode.
9683 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9684   (interactive)
9685   (gnus-set-global-variables)
9686   (gnus-kill-save-kill-buffer)
9687   (let* ((group gnus-newsgroup-name)
9688          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9689          (mode major-mode)
9690          (buf (current-buffer)))
9691     (unless temporary
9692       (run-hooks 'gnus-summary-prepare-exit-hook))
9693     ;; If we have several article buffers, we kill them at exit.
9694     (unless gnus-single-article-buffer
9695       (gnus-kill-buffer gnus-article-buffer)
9696       (gnus-kill-buffer gnus-original-article-buffer)
9697       (setq gnus-article-current nil))
9698     (when gnus-use-cache
9699       (gnus-cache-possibly-remove-articles)
9700       (gnus-cache-save-buffers))
9701     (when gnus-use-trees
9702       (gnus-tree-close group))
9703     ;; Make all changes in this group permanent.
9704     (unless quit-config
9705       (gnus-summary-update-info))
9706     (gnus-close-group group)
9707     ;; Make sure where I was, and go to next newsgroup.
9708     (set-buffer gnus-group-buffer)
9709     (unless quit-config
9710       (gnus-group-jump-to-group group)
9711       (gnus-group-next-unread-group 1))
9712     (run-hooks 'gnus-summary-exit-hook)
9713     (unless gnus-single-article-buffer
9714       (setq gnus-article-current nil))
9715     (if temporary
9716         nil                             ;Nothing to do.
9717       ;; If we have several article buffers, we kill them at exit.
9718       (unless gnus-single-article-buffer
9719         (gnus-kill-buffer gnus-article-buffer)
9720         (gnus-kill-buffer gnus-original-article-buffer)
9721         (setq gnus-article-current nil))
9722       (set-buffer buf)
9723       (if (not gnus-kill-summary-on-exit)
9724           (gnus-deaden-summary)
9725         ;; We set all buffer-local variables to nil.  It is unclear why
9726         ;; this is needed, but if we don't, buffer-local variables are
9727         ;; not garbage-collected, it seems.  This would the lead to en
9728         ;; ever-growing Emacs.
9729         (gnus-summary-clear-local-variables)
9730         (when (get-buffer gnus-article-buffer)
9731           (bury-buffer gnus-article-buffer))
9732         ;; We clear the global counterparts of the buffer-local
9733         ;; variables as well, just to be on the safe side.
9734         (gnus-configure-windows 'group 'force)
9735         (gnus-summary-clear-local-variables)
9736         ;; Return to group mode buffer.
9737         (if (eq mode 'gnus-summary-mode)
9738             (gnus-kill-buffer buf)))
9739       (setq gnus-current-select-method gnus-select-method)
9740       (pop-to-buffer gnus-group-buffer)
9741       ;; Clear the current group name.
9742       (if (not quit-config)
9743           (progn
9744             (gnus-group-jump-to-group group)
9745             (gnus-group-next-unread-group 1)
9746             (gnus-configure-windows 'group 'force))
9747         (if (not (buffer-name (car quit-config)))
9748             (gnus-configure-windows 'group 'force)
9749           (set-buffer (car quit-config))
9750           (and (eq major-mode 'gnus-summary-mode)
9751                (gnus-set-global-variables))
9752           (gnus-configure-windows (cdr quit-config))))
9753       (unless quit-config
9754         (setq gnus-newsgroup-name nil)))))
9755
9756 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9757 (defun gnus-summary-exit-no-update (&optional no-questions)
9758   "Quit reading current newsgroup without updating read article info."
9759   (interactive)
9760   (gnus-set-global-variables)
9761   (let* ((group gnus-newsgroup-name)
9762          (quit-config (gnus-group-quit-config group)))
9763     (when (or no-questions
9764               gnus-expert-user
9765               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9766       ;; If we have several article buffers, we kill them at exit.
9767       (unless gnus-single-article-buffer
9768         (gnus-kill-buffer gnus-article-buffer)
9769         (gnus-kill-buffer gnus-original-article-buffer)
9770         (setq gnus-article-current nil))
9771       (if (not gnus-kill-summary-on-exit)
9772           (gnus-deaden-summary)
9773         (gnus-close-group group)
9774         (gnus-summary-clear-local-variables)
9775         (set-buffer gnus-group-buffer)
9776         (gnus-summary-clear-local-variables)
9777         (when (get-buffer gnus-summary-buffer)
9778           (kill-buffer gnus-summary-buffer)))
9779       (unless gnus-single-article-buffer
9780         (setq gnus-article-current nil))
9781       (when gnus-use-trees
9782         (gnus-tree-close group))
9783       (when (get-buffer gnus-article-buffer)
9784         (bury-buffer gnus-article-buffer))
9785       ;; Return to the group buffer.
9786       (gnus-configure-windows 'group 'force)
9787       ;; Clear the current group name.
9788       (setq gnus-newsgroup-name nil)
9789       (when (equal (gnus-group-group-name) group)
9790         (gnus-group-next-unread-group 1))
9791       (when quit-config
9792         (if (not (buffer-name (car quit-config)))
9793             (gnus-configure-windows 'group 'force)
9794           (set-buffer (car quit-config))
9795           (when (eq major-mode 'gnus-summary-mode)
9796             (gnus-set-global-variables))
9797           (gnus-configure-windows (cdr quit-config)))))))
9798
9799 ;;; Dead summaries.
9800
9801 (defvar gnus-dead-summary-mode-map nil)
9802
9803 (if gnus-dead-summary-mode-map
9804     nil
9805   (setq gnus-dead-summary-mode-map (make-keymap))
9806   (suppress-keymap gnus-dead-summary-mode-map)
9807   (substitute-key-definition
9808    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9809   (let ((keys '("\C-d" "\r" "\177")))
9810     (while keys
9811       (define-key gnus-dead-summary-mode-map
9812         (pop keys) 'gnus-summary-wake-up-the-dead))))
9813
9814 (defvar gnus-dead-summary-mode nil
9815   "Minor mode for Gnus summary buffers.")
9816
9817 (defun gnus-dead-summary-mode (&optional arg)
9818   "Minor mode for Gnus summary buffers."
9819   (interactive "P")
9820   (when (eq major-mode 'gnus-summary-mode)
9821     (make-local-variable 'gnus-dead-summary-mode)
9822     (setq gnus-dead-summary-mode
9823           (if (null arg) (not gnus-dead-summary-mode)
9824             (> (prefix-numeric-value arg) 0)))
9825     (when gnus-dead-summary-mode
9826       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9827         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9828       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9829         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9830               minor-mode-map-alist)))))
9831
9832 (defun gnus-deaden-summary ()
9833   "Make the current summary buffer into a dead summary buffer."
9834   ;; Kill any previous dead summary buffer.
9835   (when (and gnus-dead-summary
9836              (buffer-name gnus-dead-summary))
9837     (save-excursion
9838       (set-buffer gnus-dead-summary)
9839       (when gnus-dead-summary-mode
9840         (kill-buffer (current-buffer)))))
9841   ;; Make this the current dead summary.
9842   (setq gnus-dead-summary (current-buffer))
9843   (gnus-dead-summary-mode 1)
9844   (let ((name (buffer-name)))
9845     (when (string-match "Summary" name)
9846       (rename-buffer
9847        (concat (substring name 0 (match-beginning 0)) "Dead "
9848                (substring name (match-beginning 0))) t))))
9849
9850 (defun gnus-kill-or-deaden-summary (buffer)
9851   "Kill or deaden the summary BUFFER."
9852   (cond (gnus-kill-summary-on-exit
9853          (when (and gnus-use-trees
9854                     (and (get-buffer buffer)
9855                          (buffer-name (get-buffer buffer))))
9856            (save-excursion
9857              (set-buffer (get-buffer buffer))
9858              (gnus-tree-close gnus-newsgroup-name)))
9859          (gnus-kill-buffer buffer))
9860         ((and (get-buffer buffer)
9861               (buffer-name (get-buffer buffer)))
9862          (save-excursion
9863            (set-buffer buffer)
9864            (gnus-deaden-summary)))))
9865
9866 (defun gnus-summary-wake-up-the-dead (&rest args)
9867   "Wake up the dead summary buffer."
9868   (interactive)
9869   (gnus-dead-summary-mode -1)
9870   (let ((name (buffer-name)))
9871     (when (string-match "Dead " name)
9872       (rename-buffer
9873        (concat (substring name 0 (match-beginning 0))
9874                (substring name (match-end 0))) t)))
9875   (gnus-message 3 "This dead summary is now alive again"))
9876
9877 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9878 (defun gnus-summary-fetch-faq (&optional faq-dir)
9879   "Fetch the FAQ for the current group.
9880 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9881 in."
9882   (interactive
9883    (list
9884     (if current-prefix-arg
9885         (completing-read
9886          "Faq dir: " (and (listp gnus-group-faq-directory)
9887                           gnus-group-faq-directory)))))
9888   (let (gnus-faq-buffer)
9889     (and (setq gnus-faq-buffer
9890                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9891          (gnus-configure-windows 'summary-faq))))
9892
9893 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9894 (defun gnus-summary-describe-group (&optional force)
9895   "Describe the current newsgroup."
9896   (interactive "P")
9897   (gnus-group-describe-group force gnus-newsgroup-name))
9898
9899 (defun gnus-summary-describe-briefly ()
9900   "Describe summary mode commands briefly."
9901   (interactive)
9902   (gnus-message 6
9903                 (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")))
9904
9905 ;; Walking around group mode buffer from summary mode.
9906
9907 (defun gnus-summary-next-group (&optional no-article target-group backward)
9908   "Exit current newsgroup and then select next unread newsgroup.
9909 If prefix argument NO-ARTICLE is non-nil, no article is selected
9910 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9911 previous group instead."
9912   (interactive "P")
9913   (gnus-set-global-variables)
9914   (let ((current-group gnus-newsgroup-name)
9915         (current-buffer (current-buffer))
9916         entered)
9917     ;; First we semi-exit this group to update Xrefs and all variables.
9918     ;; We can't do a real exit, because the window conf must remain
9919     ;; the same in case the user is prompted for info, and we don't
9920     ;; want the window conf to change before that...
9921     (gnus-summary-exit t)
9922     (while (not entered)
9923       ;; Then we find what group we are supposed to enter.
9924       (set-buffer gnus-group-buffer)
9925       (gnus-group-jump-to-group current-group)
9926       (setq target-group
9927             (or target-group
9928                 (if (eq gnus-keep-same-level 'best)
9929                     (gnus-summary-best-group gnus-newsgroup-name)
9930                   (gnus-summary-search-group backward gnus-keep-same-level))))
9931       (if (not target-group)
9932           ;; There are no further groups, so we return to the group
9933           ;; buffer.
9934           (progn
9935             (gnus-message 5 "Returning to the group buffer")
9936             (setq entered t)
9937             (set-buffer current-buffer)
9938             (gnus-summary-exit))
9939         ;; We try to enter the target group.
9940         (gnus-group-jump-to-group target-group)
9941         (let ((unreads (gnus-group-group-unread)))
9942           (if (and (or (eq t unreads)
9943                        (and unreads (not (zerop unreads))))
9944                    (gnus-summary-read-group
9945                     target-group nil no-article current-buffer))
9946               (setq entered t)
9947             (setq current-group target-group
9948                   target-group nil)))))))
9949
9950 (defun gnus-summary-prev-group (&optional no-article)
9951   "Exit current newsgroup and then select previous unread newsgroup.
9952 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9953   (interactive "P")
9954   (gnus-summary-next-group no-article nil t))
9955
9956 ;; Walking around summary lines.
9957
9958 (defun gnus-summary-first-subject (&optional unread)
9959   "Go to the first unread subject.
9960 If UNREAD is non-nil, go to the first unread article.
9961 Returns the article selected or nil if there are no unread articles."
9962   (interactive "P")
9963   (prog1
9964       (cond
9965        ;; Empty summary.
9966        ((null gnus-newsgroup-data)
9967         (gnus-message 3 "No articles in the group")
9968         nil)
9969        ;; Pick the first article.
9970        ((not unread)
9971         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9972         (gnus-data-number (car gnus-newsgroup-data)))
9973        ;; No unread articles.
9974        ((null gnus-newsgroup-unreads)
9975         (gnus-message 3 "No more unread articles")
9976         nil)
9977        ;; Find the first unread article.
9978        (t
9979         (let ((data gnus-newsgroup-data))
9980           (while (and data
9981                       (not (gnus-data-unread-p (car data))))
9982             (setq data (cdr data)))
9983           (if data
9984               (progn
9985                 (goto-char (gnus-data-pos (car data)))
9986                 (gnus-data-number (car data)))))))
9987     (gnus-summary-position-point)))
9988
9989 (defun gnus-summary-next-subject (n &optional unread dont-display)
9990   "Go to next N'th summary line.
9991 If N is negative, go to the previous N'th subject line.
9992 If UNREAD is non-nil, only unread articles are selected.
9993 The difference between N and the actual number of steps taken is
9994 returned."
9995   (interactive "p")
9996   (let ((backward (< n 0))
9997         (n (abs n)))
9998     (while (and (> n 0)
9999                 (if backward
10000                     (gnus-summary-find-prev unread)
10001                   (gnus-summary-find-next unread)))
10002       (setq n (1- n)))
10003     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10004                                (if unread " unread" "")))
10005     (unless dont-display
10006       (gnus-summary-recenter)
10007       (gnus-summary-position-point))
10008     n))
10009
10010 (defun gnus-summary-next-unread-subject (n)
10011   "Go to next N'th unread summary line."
10012   (interactive "p")
10013   (gnus-summary-next-subject n t))
10014
10015 (defun gnus-summary-prev-subject (n &optional unread)
10016   "Go to previous N'th summary line.
10017 If optional argument UNREAD is non-nil, only unread article is selected."
10018   (interactive "p")
10019   (gnus-summary-next-subject (- n) unread))
10020
10021 (defun gnus-summary-prev-unread-subject (n)
10022   "Go to previous N'th unread summary line."
10023   (interactive "p")
10024   (gnus-summary-next-subject (- n) t))
10025
10026 (defun gnus-summary-goto-subject (article &optional force silent)
10027   "Go the subject line of ARTICLE.
10028 If FORCE, also allow jumping to articles not currently shown."
10029   (let ((b (point))
10030         (data (gnus-data-find article)))
10031     ;; We read in the article if we have to.
10032     (and (not data)
10033          force
10034          (gnus-summary-insert-subject article)
10035          (setq data (gnus-data-find article)))
10036     (goto-char b)
10037     (if (not data)
10038         (progn
10039           (unless silent
10040             (gnus-message 3 "Can't find article %d" article))
10041           nil)
10042       (goto-char (gnus-data-pos data))
10043       article)))
10044
10045 ;; Walking around summary lines with displaying articles.
10046
10047 (defun gnus-summary-expand-window (&optional arg)
10048   "Make the summary buffer take up the entire Emacs frame.
10049 Given a prefix, will force an `article' buffer configuration."
10050   (interactive "P")
10051   (gnus-set-global-variables)
10052   (if arg
10053       (gnus-configure-windows 'article 'force)
10054     (gnus-configure-windows 'summary 'force)))
10055
10056 (defun gnus-summary-display-article (article &optional all-header)
10057   "Display ARTICLE in article buffer."
10058   (gnus-set-global-variables)
10059   (if (null article)
10060       nil
10061     (prog1
10062         (if gnus-summary-display-article-function
10063             (funcall gnus-summary-display-article-function article all-header)
10064           (gnus-article-prepare article all-header))
10065       (run-hooks 'gnus-select-article-hook)
10066       (gnus-summary-recenter)
10067       (gnus-summary-goto-subject article)
10068       (when gnus-use-trees
10069         (gnus-possibly-generate-tree article)
10070         (gnus-highlight-selected-tree article))
10071       ;; Successfully display article.
10072       (gnus-article-set-window-start
10073        (cdr (assq article gnus-newsgroup-bookmarks)))
10074       t)))
10075
10076 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10077   "Select the current article.
10078 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10079 non-nil, the article will be re-fetched even if it already present in
10080 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10081 be displayed."
10082   (let ((article (or article (gnus-summary-article-number)))
10083         (all-headers (not (not all-headers))) ;Must be T or NIL.
10084         gnus-summary-display-article-function
10085         did)
10086     (and (not pseudo)
10087          (gnus-summary-article-pseudo-p article)
10088          (error "This is a pseudo-article."))
10089     (prog1
10090         (save-excursion
10091           (set-buffer gnus-summary-buffer)
10092           (if (or (and gnus-single-article-buffer
10093                        (or (null gnus-current-article)
10094                            (null gnus-article-current)
10095                            (null (get-buffer gnus-article-buffer))
10096                            (not (eq article (cdr gnus-article-current)))
10097                            (not (equal (car gnus-article-current)
10098                                        gnus-newsgroup-name))))
10099                   (and (not gnus-single-article-buffer)
10100                        (or (null gnus-current-article)
10101                            (not (eq gnus-current-article article))))
10102                   force)
10103               ;; The requested article is different from the current article.
10104               (prog1
10105                   (gnus-summary-display-article article all-headers)
10106                 (setq did article))
10107             (if (or all-headers gnus-show-all-headers)
10108                 (gnus-article-show-all-headers))
10109             'old))
10110       (if did
10111           (gnus-article-set-window-start
10112            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10113
10114 (defun gnus-summary-set-current-mark (&optional current-mark)
10115   "Obsolete function."
10116   nil)
10117
10118 (defun gnus-summary-next-article (&optional unread subject backward push)
10119   "Select the next article.
10120 If UNREAD, only unread articles are selected.
10121 If SUBJECT, only articles with SUBJECT are selected.
10122 If BACKWARD, the previous article is selected instead of the next."
10123   (interactive "P")
10124   (gnus-set-global-variables)
10125   (cond
10126    ;; Is there such an article?
10127    ((and (gnus-summary-search-forward unread subject backward)
10128          (or (gnus-summary-display-article (gnus-summary-article-number))
10129              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10130     (gnus-summary-position-point))
10131    ;; If not, we try the first unread, if that is wanted.
10132    ((and subject
10133          gnus-auto-select-same
10134          (or (gnus-summary-first-unread-article)
10135              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10136     (gnus-summary-position-point)
10137     (gnus-message 6 "Wrapped"))
10138    ;; Try to get next/previous article not displayed in this group.
10139    ((and gnus-auto-extend-newsgroup
10140          (not unread) (not subject))
10141     (gnus-summary-goto-article
10142      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10143      nil t))
10144    ;; Go to next/previous group.
10145    (t
10146     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10147         (gnus-summary-jump-to-group gnus-newsgroup-name))
10148     (let ((cmd last-command-char)
10149           (group
10150            (if (eq gnus-keep-same-level 'best)
10151                (gnus-summary-best-group gnus-newsgroup-name)
10152              (gnus-summary-search-group backward gnus-keep-same-level))))
10153       ;; For some reason, the group window gets selected.  We change
10154       ;; it back.
10155       (select-window (get-buffer-window (current-buffer)))
10156       ;; Select next unread newsgroup automagically.
10157       (cond
10158        ((not gnus-auto-select-next)
10159         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10160        ((or (eq gnus-auto-select-next 'quietly)
10161             (and (eq gnus-auto-select-next 'slightly-quietly)
10162                  push)
10163             (and (eq gnus-auto-select-next 'almost-quietly)
10164                  (gnus-summary-last-article-p)))
10165         ;; Select quietly.
10166         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10167             (gnus-summary-exit)
10168           (gnus-message 7 "No more%s articles (%s)..."
10169                         (if unread " unread" "")
10170                         (if group (concat "selecting " group)
10171                           "exiting"))
10172           (gnus-summary-next-group nil group backward)))
10173        (t
10174         (gnus-summary-walk-group-buffer
10175          gnus-newsgroup-name cmd unread backward)))))))
10176
10177 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10178   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10179                       (?\C-p (gnus-group-prev-unread-group 1))))
10180         keve key group ended)
10181     (save-excursion
10182       (set-buffer gnus-group-buffer)
10183       (gnus-summary-jump-to-group from-group)
10184       (setq group
10185             (if (eq gnus-keep-same-level 'best)
10186                 (gnus-summary-best-group gnus-newsgroup-name)
10187               (gnus-summary-search-group backward gnus-keep-same-level))))
10188     (while (not ended)
10189       (gnus-message
10190        5 "No more%s articles%s" (if unread " unread" "")
10191        (if (and group
10192                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10193            (format " (Type %s for %s [%s])"
10194                    (single-key-description cmd) group
10195                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10196          (format " (Type %s to exit %s)"
10197                  (single-key-description cmd)
10198                  gnus-newsgroup-name)))
10199       ;; Confirm auto selection.
10200       (setq key (car (setq keve (gnus-read-event-char))))
10201       (setq ended t)
10202       (cond
10203        ((assq key keystrokes)
10204         (let ((obuf (current-buffer)))
10205           (switch-to-buffer gnus-group-buffer)
10206           (and group
10207                (gnus-group-jump-to-group group))
10208           (eval (cadr (assq key keystrokes)))
10209           (setq group (gnus-group-group-name))
10210           (switch-to-buffer obuf))
10211         (setq ended nil))
10212        ((equal key cmd)
10213         (if (or (not group)
10214                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10215             (gnus-summary-exit)
10216           (gnus-summary-next-group nil group backward)))
10217        (t
10218         (push (cdr keve) unread-command-events))))))
10219
10220 (defun gnus-read-event-char ()
10221   "Get the next event."
10222   (let ((event (read-event)))
10223     (cons (and (numberp event) event) event)))
10224
10225 (defun gnus-summary-next-unread-article ()
10226   "Select unread article after current one."
10227   (interactive)
10228   (gnus-summary-next-article t (and gnus-auto-select-same
10229                                     (gnus-summary-article-subject))))
10230
10231 (defun gnus-summary-prev-article (&optional unread subject)
10232   "Select the article after the current one.
10233 If UNREAD is non-nil, only unread articles are selected."
10234   (interactive "P")
10235   (gnus-summary-next-article unread subject t))
10236
10237 (defun gnus-summary-prev-unread-article ()
10238   "Select unred article before current one."
10239   (interactive)
10240   (gnus-summary-prev-article t (and gnus-auto-select-same
10241                                     (gnus-summary-article-subject))))
10242
10243 (defun gnus-summary-next-page (&optional lines circular)
10244   "Show next page of the selected article.
10245 If at the end of the current article, select the next article.
10246 LINES says how many lines should be scrolled up.
10247
10248 If CIRCULAR is non-nil, go to the start of the article instead of
10249 selecting the next article when reaching the end of the current
10250 article."
10251   (interactive "P")
10252   (setq gnus-summary-buffer (current-buffer))
10253   (gnus-set-global-variables)
10254   (let ((article (gnus-summary-article-number))
10255         (endp nil))
10256     (gnus-configure-windows 'article)
10257     (if (or (null gnus-current-article)
10258             (null gnus-article-current)
10259             (/= article (cdr gnus-article-current))
10260             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10261         ;; Selected subject is different from current article's.
10262         (gnus-summary-display-article article)
10263       (gnus-eval-in-buffer-window
10264        gnus-article-buffer
10265        (setq endp (gnus-article-next-page lines)))
10266       (if endp
10267           (cond (circular
10268                  (gnus-summary-beginning-of-article))
10269                 (lines
10270                  (gnus-message 3 "End of message"))
10271                 ((null lines)
10272                  (if (and (eq gnus-summary-goto-unread 'never)
10273                           (not (gnus-summary-last-article-p article)))
10274                      (gnus-summary-next-article)
10275                    (gnus-summary-next-unread-article))))))
10276     (gnus-summary-recenter)
10277     (gnus-summary-position-point)))
10278
10279 (defun gnus-summary-prev-page (&optional lines)
10280   "Show previous page of selected article.
10281 Argument LINES specifies lines to be scrolled down."
10282   (interactive "P")
10283   (gnus-set-global-variables)
10284   (let ((article (gnus-summary-article-number)))
10285     (gnus-configure-windows 'article)
10286     (if (or (null gnus-current-article)
10287             (null gnus-article-current)
10288             (/= article (cdr gnus-article-current))
10289             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10290         ;; Selected subject is different from current article's.
10291         (gnus-summary-display-article article)
10292       (gnus-summary-recenter)
10293       (gnus-eval-in-buffer-window gnus-article-buffer
10294                                   (gnus-article-prev-page lines))))
10295   (gnus-summary-position-point))
10296
10297 (defun gnus-summary-scroll-up (lines)
10298   "Scroll up (or down) one line current article.
10299 Argument LINES specifies lines to be scrolled up (or down if negative)."
10300   (interactive "p")
10301   (gnus-set-global-variables)
10302   (gnus-configure-windows 'article)
10303   (gnus-summary-show-thread)
10304   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10305     (gnus-eval-in-buffer-window
10306      gnus-article-buffer
10307      (cond ((> lines 0)
10308             (if (gnus-article-next-page lines)
10309                 (gnus-message 3 "End of message")))
10310            ((< lines 0)
10311             (gnus-article-prev-page (- lines))))))
10312   (gnus-summary-recenter)
10313   (gnus-summary-position-point))
10314
10315 (defun gnus-summary-next-same-subject ()
10316   "Select next article which has the same subject as current one."
10317   (interactive)
10318   (gnus-set-global-variables)
10319   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10320
10321 (defun gnus-summary-prev-same-subject ()
10322   "Select previous article which has the same subject as current one."
10323   (interactive)
10324   (gnus-set-global-variables)
10325   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10326
10327 (defun gnus-summary-next-unread-same-subject ()
10328   "Select next unread article which has the same subject as current one."
10329   (interactive)
10330   (gnus-set-global-variables)
10331   (gnus-summary-next-article t (gnus-summary-article-subject)))
10332
10333 (defun gnus-summary-prev-unread-same-subject ()
10334   "Select previous unread article which has the same subject as current one."
10335   (interactive)
10336   (gnus-set-global-variables)
10337   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10338
10339 (defun gnus-summary-first-unread-article ()
10340   "Select the first unread article.
10341 Return nil if there are no unread articles."
10342   (interactive)
10343   (gnus-set-global-variables)
10344   (prog1
10345       (if (gnus-summary-first-subject t)
10346           (progn
10347             (gnus-summary-show-thread)
10348             (gnus-summary-first-subject t)
10349             (gnus-summary-display-article (gnus-summary-article-number))))
10350     (gnus-summary-position-point)))
10351
10352 (defun gnus-summary-best-unread-article ()
10353   "Select the unread article with the highest score."
10354   (interactive)
10355   (gnus-set-global-variables)
10356   (let ((best -1000000)
10357         (data gnus-newsgroup-data)
10358         article score)
10359     (while data
10360       (and (gnus-data-unread-p (car data))
10361            (> (setq score
10362                     (gnus-summary-article-score (gnus-data-number (car data))))
10363               best)
10364            (setq best score
10365                  article (gnus-data-number (car data))))
10366       (setq data (cdr data)))
10367     (prog1
10368         (if article
10369             (gnus-summary-goto-article article)
10370           (error "No unread articles"))
10371       (gnus-summary-position-point))))
10372
10373 (defun gnus-summary-last-subject ()
10374   "Go to the last displayed subject line in the group."
10375   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10376     (when article
10377       (gnus-summary-goto-subject article))))
10378
10379 (defun gnus-summary-goto-article (article &optional all-headers force)
10380   "Fetch ARTICLE and display it if it exists.
10381 If ALL-HEADERS is non-nil, no header lines are hidden."
10382   (interactive
10383    (list
10384     (string-to-int
10385      (completing-read
10386       "Article number: "
10387       (mapcar (lambda (number) (list (int-to-string number)))
10388               gnus-newsgroup-limit)))
10389     current-prefix-arg
10390     t))
10391   (prog1
10392       (if (gnus-summary-goto-subject article force)
10393           (gnus-summary-display-article article all-headers)
10394         (gnus-message 4 "Couldn't go to article %s" article) nil)
10395     (gnus-summary-position-point)))
10396
10397 (defun gnus-summary-goto-last-article ()
10398   "Go to the previously read article."
10399   (interactive)
10400   (prog1
10401       (and gnus-last-article
10402            (gnus-summary-goto-article gnus-last-article))
10403     (gnus-summary-position-point)))
10404
10405 (defun gnus-summary-pop-article (number)
10406   "Pop one article off the history and go to the previous.
10407 NUMBER articles will be popped off."
10408   (interactive "p")
10409   (let (to)
10410     (setq gnus-newsgroup-history
10411           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10412     (if to
10413         (gnus-summary-goto-article (car to))
10414       (error "Article history empty")))
10415   (gnus-summary-position-point))
10416
10417 ;; Summary commands and functions for limiting the summary buffer.
10418
10419 (defun gnus-summary-limit-to-articles (n)
10420   "Limit the summary buffer to the next N articles.
10421 If not given a prefix, use the process marked articles instead."
10422   (interactive "P")
10423   (gnus-set-global-variables)
10424   (prog1
10425       (let ((articles (gnus-summary-work-articles n)))
10426         (setq gnus-newsgroup-processable nil)
10427         (gnus-summary-limit articles))
10428     (gnus-summary-position-point)))
10429
10430 (defun gnus-summary-pop-limit (&optional total)
10431   "Restore the previous limit.
10432 If given a prefix, remove all limits."
10433   (interactive "P")
10434   (gnus-set-global-variables)
10435   (when total 
10436     (setq gnus-newsgroup-limits
10437           (list (mapcar (lambda (h) (mail-header-number h))
10438                         gnus-newsgroup-headers))))
10439   (unless gnus-newsgroup-limits
10440     (error "No limit to pop"))
10441   (prog1
10442       (gnus-summary-limit nil 'pop)
10443     (gnus-summary-position-point)))
10444
10445 (defun gnus-summary-limit-to-subject (subject &optional header)
10446   "Limit the summary buffer to articles that have subjects that match a regexp."
10447   (interactive "sRegexp: ")
10448   (unless header
10449     (setq header "subject"))
10450   (when (not (equal "" subject))
10451     (prog1
10452         (let ((articles (gnus-summary-find-matching
10453                          (or header "subject") subject 'all)))
10454           (or articles (error "Found no matches for \"%s\"" subject))
10455           (gnus-summary-limit articles))
10456       (gnus-summary-position-point))))
10457
10458 (defun gnus-summary-limit-to-author (from)
10459   "Limit the summary buffer to articles that have authors that match a regexp."
10460   (interactive "sRegexp: ")
10461   (gnus-summary-limit-to-subject from "from"))
10462
10463 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10464 (make-obsolete
10465  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10466
10467 (defun gnus-summary-limit-to-unread (&optional all)
10468   "Limit the summary buffer to articles that are not marked as read.
10469 If ALL is non-nil, limit strictly to unread articles."
10470   (interactive "P")
10471   (if all
10472       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10473     (gnus-summary-limit-to-marks
10474      ;; Concat all the marks that say that an article is read and have
10475      ;; those removed.
10476      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10477            gnus-killed-mark gnus-kill-file-mark
10478            gnus-low-score-mark gnus-expirable-mark
10479            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10480      'reverse)))
10481
10482 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10483 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10484
10485 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10486   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10487 If REVERSE, limit the summary buffer to articles that are not marked
10488 with MARKS.  MARKS can either be a string of marks or a list of marks.
10489 Returns how many articles were removed."
10490   (interactive "sMarks: ")
10491   (gnus-set-global-variables)
10492   (prog1
10493       (let ((data gnus-newsgroup-data)
10494             (marks (if (listp marks) marks
10495                      (append marks nil))) ; Transform to list.
10496             articles)
10497         (while data
10498           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10499                  (memq (gnus-data-mark (car data)) marks))
10500                (setq articles (cons (gnus-data-number (car data)) articles)))
10501           (setq data (cdr data)))
10502         (gnus-summary-limit articles))
10503     (gnus-summary-position-point)))
10504
10505 (defun gnus-summary-limit-to-score (&optional score)
10506   "Limit to articles with score at or above SCORE."
10507   (interactive "P")
10508   (gnus-set-global-variables)
10509   (setq score (if score
10510                   (prefix-numeric-value score)
10511                 (or gnus-summary-default-score 0)))
10512   (let ((data gnus-newsgroup-data)
10513         articles)
10514     (while data
10515       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10516                 score)
10517         (push (gnus-data-number (car data)) articles))
10518       (setq data (cdr data)))
10519     (prog1
10520         (gnus-summary-limit articles)
10521       (gnus-summary-position-point))))
10522
10523 (defun gnus-summary-limit-include-dormant ()
10524   "Display all the hidden articles that are marked as dormant."
10525   (interactive)
10526   (gnus-set-global-variables)
10527   (or gnus-newsgroup-dormant
10528       (error "There are no dormant articles in this group"))
10529   (prog1
10530       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10531     (gnus-summary-position-point)))
10532
10533 (defun gnus-summary-limit-exclude-dormant ()
10534   "Hide all dormant articles."
10535   (interactive)
10536   (gnus-set-global-variables)
10537   (prog1
10538       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10539     (gnus-summary-position-point)))
10540
10541 (defun gnus-summary-limit-exclude-childless-dormant ()
10542   "Hide all dormant articles that have no children."
10543   (interactive)
10544   (gnus-set-global-variables)
10545   (let ((data (gnus-data-list t))
10546         articles d children)
10547     ;; Find all articles that are either not dormant or have
10548     ;; children.
10549     (while (setq d (pop data))
10550       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10551                 (and (setq children 
10552                            (gnus-article-children (gnus-data-number d)))
10553                      (let (found)
10554                        (while children
10555                          (when (memq (car children) articles)
10556                            (setq children nil
10557                                  found t))
10558                          (pop children))
10559                        found)))
10560         (push (gnus-data-number d) articles)))
10561     ;; Do the limiting.
10562     (prog1
10563         (gnus-summary-limit articles)
10564       (gnus-summary-position-point))))
10565
10566 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10567   "Mark all unread excluded articles as read.
10568 If ALL, mark even excluded ticked and dormants as read."
10569   (interactive "P")
10570   (let ((articles (gnus-sorted-complement
10571                    (sort
10572                     (mapcar (lambda (h) (mail-header-number h))
10573                             gnus-newsgroup-headers)
10574                     '<)
10575                    (sort gnus-newsgroup-limit '<)))
10576         article)
10577     (setq gnus-newsgroup-unreads nil)
10578     (if all
10579         (setq gnus-newsgroup-dormant nil
10580               gnus-newsgroup-marked nil
10581               gnus-newsgroup-reads
10582               (nconc
10583                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10584                gnus-newsgroup-reads))
10585       (while (setq article (pop articles))
10586         (unless (or (memq article gnus-newsgroup-dormant)
10587                     (memq article gnus-newsgroup-marked))
10588           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10589
10590 (defun gnus-summary-limit (articles &optional pop)
10591   (if pop
10592       ;; We pop the previous limit off the stack and use that.
10593       (setq articles (car gnus-newsgroup-limits)
10594             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10595     ;; We use the new limit, so we push the old limit on the stack.
10596     (setq gnus-newsgroup-limits
10597           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10598   ;; Set the limit.
10599   (setq gnus-newsgroup-limit articles)
10600   (let ((total (length gnus-newsgroup-data))
10601         (data (gnus-data-find-list (gnus-summary-article-number)))
10602         found)
10603     ;; This will do all the work of generating the new summary buffer
10604     ;; according to the new limit.
10605     (gnus-summary-prepare)
10606     ;; Hide any threads, possibly.
10607     (and gnus-show-threads
10608          gnus-thread-hide-subtree
10609          (gnus-summary-hide-all-threads))
10610     ;; Try to return to the article you were at, or one in the
10611     ;; neighborhood.
10612     (if data
10613         ;; We try to find some article after the current one.
10614         (while data
10615           (and (gnus-summary-goto-subject
10616                 (gnus-data-number (car data)) nil t)
10617                (setq data nil
10618                      found t))
10619           (setq data (cdr data))))
10620     (or found
10621         ;; If there is no data, that means that we were after the last
10622         ;; article.  The same goes when we can't find any articles
10623         ;; after the current one.
10624         (progn
10625           (goto-char (point-max))
10626           (gnus-summary-find-prev)))
10627     ;; We return how many articles were removed from the summary
10628     ;; buffer as a result of the new limit.
10629     (- total (length gnus-newsgroup-data))))
10630
10631 (defsubst gnus-cut-thread (thread)
10632   "Go forwards in the thread until we find an article that we want to display."
10633   (when (eq gnus-fetch-old-headers 'some)
10634     ;; Deal with old-fetched headers.
10635     (while (and thread
10636                 (memq (mail-header-number (car thread)) 
10637                       gnus-newsgroup-ancient)
10638                 (<= (length (cdr thread)) 1))
10639       (setq thread (cadr thread))))
10640   ;; Deal with sparse threads.
10641   (when (or (eq gnus-build-sparse-threads 'some)
10642             (eq gnus-build-sparse-threads 'more))
10643     (while (and thread
10644                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10645                 (= (length (cdr thread)) 1))
10646       (setq thread (cadr thread))))
10647   thread)
10648
10649 (defun gnus-cut-threads (threads)
10650   "Cut off all uninteresting articles from the beginning of threads."
10651   (when (or (eq gnus-fetch-old-headers 'some)
10652             (eq gnus-build-sparse-threads 'some)
10653             (eq gnus-build-sparse-threads 'more))
10654     (let ((th threads))
10655       (while th
10656         (setcar th (gnus-cut-thread (car th)))
10657         (setq th (cdr th)))))
10658   ;; Remove nixed out threads.
10659   (delq nil threads))
10660
10661 (defun gnus-summary-initial-limit (&optional show-if-empty)
10662   "Figure out what the initial limit is supposed to be on group entry.
10663 This entails weeding out unwanted dormants, low-scored articles,
10664 fetch-old-headers verbiage, and so on."
10665   ;; Most groups have nothing to remove.
10666   (if (or gnus-inhibit-limiting
10667           (and (null gnus-newsgroup-dormant)
10668                (not (eq gnus-fetch-old-headers 'some))
10669                (null gnus-summary-expunge-below)
10670                (not (eq gnus-build-sparse-threads 'some))
10671                (not (eq gnus-build-sparse-threads 'more))
10672                (null gnus-thread-expunge-below)
10673                (not gnus-use-nocem)))
10674       () ; Do nothing.
10675     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10676     (setq gnus-newsgroup-limit nil)
10677     (mapatoms
10678      (lambda (node)
10679        (unless (car (symbol-value node))
10680          ;; These threads have no parents -- they are roots.
10681          (let ((nodes (cdr (symbol-value node)))
10682                thread)
10683            (while nodes
10684              (if (and gnus-thread-expunge-below
10685                       (< (gnus-thread-total-score (car nodes))
10686                          gnus-thread-expunge-below))
10687                  (gnus-expunge-thread (pop nodes))
10688                (setq thread (pop nodes))
10689                (gnus-summary-limit-children thread))))))
10690      gnus-newsgroup-dependencies)
10691     ;; If this limitation resulted in an empty group, we might
10692     ;; pop the previous limit and use it instead.
10693     (when (and (not gnus-newsgroup-limit)
10694                show-if-empty)
10695       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10696     gnus-newsgroup-limit))
10697
10698 (defun gnus-summary-limit-children (thread)
10699   "Return 1 if this subthread is visible and 0 if it is not."
10700   ;; First we get the number of visible children to this thread.  This
10701   ;; is done by recursing down the thread using this function, so this
10702   ;; will really go down to a leaf article first, before slowly
10703   ;; working its way up towards the root.
10704   (when thread
10705     (let ((children
10706            (if (cdr thread)
10707                (apply '+ (mapcar 'gnus-summary-limit-children
10708                                  (cdr thread)))
10709              0))
10710           (number (mail-header-number (car thread)))
10711           score)
10712       (if (or
10713            ;; If this article is dormant and has absolutely no visible
10714            ;; children, then this article isn't visible.
10715            (and (memq number gnus-newsgroup-dormant)
10716                 (= children 0))
10717            ;; If this is a "fetch-old-headered" and there is only one
10718            ;; visible child (or less), then we don't want this article.
10719            (and (eq gnus-fetch-old-headers 'some)
10720                 (memq number gnus-newsgroup-ancient)
10721                 (zerop children))
10722            ;; If this is a sparsely inserted article with no children,
10723            ;; we don't want it.
10724            (and (eq gnus-build-sparse-threads 'some)
10725                 (memq number gnus-newsgroup-sparse)
10726                 (zerop children))
10727            ;; If we use expunging, and this article is really
10728            ;; low-scored, then we don't want this article.
10729            (when (and gnus-summary-expunge-below
10730                       (< (setq score
10731                                (or (cdr (assq number gnus-newsgroup-scored))
10732                                    gnus-summary-default-score))
10733                          gnus-summary-expunge-below))
10734              ;; We increase the expunge-tally here, but that has
10735              ;; nothing to do with the limits, really.
10736              (incf gnus-newsgroup-expunged-tally)
10737              ;; We also mark as read here, if that's wanted.
10738              (when (and gnus-summary-mark-below
10739                         (< score gnus-summary-mark-below))
10740                (setq gnus-newsgroup-unreads
10741                      (delq number gnus-newsgroup-unreads))
10742                (if gnus-newsgroup-auto-expire
10743                    (push number gnus-newsgroup-expirable)
10744                  (push (cons number gnus-low-score-mark)
10745                        gnus-newsgroup-reads)))
10746              t)
10747            (and gnus-use-nocem
10748                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10749           ;; Nope, invisible article.
10750           0
10751         ;; Ok, this article is to be visible, so we add it to the limit
10752         ;; and return 1.
10753         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10754         1))))
10755
10756 (defun gnus-expunge-thread (thread)
10757   "Mark all articles in THREAD as read."
10758   (let* ((number (mail-header-number (car thread))))
10759     (incf gnus-newsgroup-expunged-tally)
10760     ;; We also mark as read here, if that's wanted.
10761     (setq gnus-newsgroup-unreads
10762           (delq number gnus-newsgroup-unreads))
10763     (if gnus-newsgroup-auto-expire
10764         (push number gnus-newsgroup-expirable)
10765       (push (cons number gnus-low-score-mark)
10766             gnus-newsgroup-reads)))
10767   ;; Go recursively through all subthreads.
10768   (mapcar 'gnus-expunge-thread (cdr thread)))
10769
10770 ;; Summary article oriented commands
10771
10772 (defun gnus-summary-refer-parent-article (n)
10773   "Refer parent article N times.
10774 The difference between N and the number of articles fetched is returned."
10775   (interactive "p")
10776   (gnus-set-global-variables)
10777   (while
10778       (and
10779        (> n 0)
10780        (let* ((header (gnus-summary-article-header))
10781               (ref
10782                ;; If we try to find the parent of the currently
10783                ;; displayed article, then we take a look at the actual
10784                ;; References header, since this is slightly more
10785                ;; reliable than the References field we got from the
10786                ;; server.
10787                (if (and (eq (mail-header-number header)
10788                             (cdr gnus-article-current))
10789                         (equal gnus-newsgroup-name
10790                                (car gnus-article-current)))
10791                    (save-excursion
10792                      (set-buffer gnus-original-article-buffer)
10793                      (nnheader-narrow-to-headers)
10794                      (prog1
10795                          (mail-fetch-field "references")
10796                        (widen)))
10797                  ;; It's not the current article, so we take a bet on
10798                  ;; the value we got from the server.
10799                  (mail-header-references header))))
10800          (if (setq ref (or ref (mail-header-references header)))
10801              (or (gnus-summary-refer-article (gnus-parent-id ref))
10802                  (gnus-message 1 "Couldn't find parent"))
10803            (gnus-message 1 "No references in article %d"
10804                          (gnus-summary-article-number))
10805            nil)))
10806     (setq n (1- n)))
10807   (gnus-summary-position-point)
10808   n)
10809
10810 (defun gnus-summary-refer-references ()
10811   "Fetch all articles mentioned in the References header.
10812 Return how many articles were fetched."
10813   (interactive)
10814   (gnus-set-global-variables)
10815   (let ((ref (mail-header-references (gnus-summary-article-header)))
10816         (current (gnus-summary-article-number))
10817         (n 0))
10818     ;; For each Message-ID in the References header...
10819     (while (string-match "<[^>]*>" ref)
10820       (incf n)
10821       ;; ... fetch that article.
10822       (gnus-summary-refer-article
10823        (prog1 (match-string 0 ref)
10824          (setq ref (substring ref (match-end 0))))))
10825     (gnus-summary-goto-subject current)
10826     (gnus-summary-position-point)
10827     n))
10828
10829 (defun gnus-summary-refer-article (message-id)
10830   "Fetch an article specified by MESSAGE-ID."
10831   (interactive "sMessage-ID: ")
10832   (when (and (stringp message-id)
10833              (not (zerop (length message-id))))
10834     ;; Construct the correct Message-ID if necessary.
10835     ;; Suggested by tale@pawl.rpi.edu.
10836     (unless (string-match "^<" message-id)
10837       (setq message-id (concat "<" message-id)))
10838     (unless (string-match ">$" message-id)
10839       (setq message-id (concat message-id ">")))
10840     (let ((header (car (gnus-gethash message-id
10841                                      gnus-newsgroup-dependencies))))
10842       (if header
10843           ;; The article is present in the buffer, to we just go to it.
10844           (gnus-summary-goto-article (mail-header-number header) nil t)
10845         ;; We fetch the article
10846         (let ((gnus-override-method 
10847                (and (gnus-news-group-p gnus-newsgroup-name)
10848                     gnus-refer-article-method))
10849               number)
10850           ;; Start the special refer-article method, if necessary.
10851           (when gnus-refer-article-method
10852             (gnus-check-server gnus-refer-article-method))
10853           ;; Fetch the header, and display the article.
10854           (if (setq number (gnus-summary-insert-subject message-id))
10855               (gnus-summary-select-article nil nil nil number)
10856             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10857
10858 (defun gnus-summary-enter-digest-group (&optional force)
10859   "Enter a digest group based on the current article."
10860   (interactive "P")
10861   (gnus-set-global-variables)
10862   (gnus-summary-select-article)
10863   (let ((name (format "%s-%d"
10864                       (gnus-group-prefixed-name
10865                        gnus-newsgroup-name (list 'nndoc ""))
10866                       gnus-current-article))
10867         (ogroup gnus-newsgroup-name)
10868         (case-fold-search t)
10869         (buf (current-buffer))
10870         dig)
10871     (save-excursion
10872       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10873       (insert-buffer-substring gnus-original-article-buffer)
10874       (narrow-to-region
10875        (goto-char (point-min))
10876        (or (search-forward "\n\n" nil t) (point)))
10877       (goto-char (point-min))
10878       (delete-matching-lines "^\\(Path\\):\\|^From ")
10879       (widen))
10880     (unwind-protect
10881         (if (gnus-group-read-ephemeral-group
10882              name `(nndoc ,name (nndoc-address
10883                                  ,(get-buffer dig))
10884                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10885             ;; Make all postings to this group go to the parent group.
10886             (nconc (gnus-info-params (gnus-get-info name))
10887                    (list (cons 'to-group ogroup)))
10888           ;; Couldn't select this doc group.
10889           (switch-to-buffer buf)
10890           (gnus-set-global-variables)
10891           (gnus-configure-windows 'summary)
10892           (gnus-message 3 "Article couldn't be entered?"))
10893       (kill-buffer dig))))
10894
10895 (defun gnus-summary-isearch-article (&optional regexp-p)
10896   "Do incremental search forward on the current article.
10897 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10898   (interactive "P")
10899   (gnus-set-global-variables)
10900   (gnus-summary-select-article)
10901   (gnus-configure-windows 'article)
10902   (gnus-eval-in-buffer-window
10903    gnus-article-buffer
10904    (goto-char (point-min))
10905    (isearch-forward regexp-p)))
10906
10907 (defun gnus-summary-search-article-forward (regexp &optional backward)
10908   "Search for an article containing REGEXP forward.
10909 If BACKWARD, search backward instead."
10910   (interactive
10911    (list (read-string
10912           (format "Search article %s (regexp%s): "
10913                   (if current-prefix-arg "backward" "forward")
10914                   (if gnus-last-search-regexp
10915                       (concat ", default " gnus-last-search-regexp)
10916                     "")))
10917          current-prefix-arg))
10918   (gnus-set-global-variables)
10919   (if (string-equal regexp "")
10920       (setq regexp (or gnus-last-search-regexp ""))
10921     (setq gnus-last-search-regexp regexp))
10922   (if (gnus-summary-search-article regexp backward)
10923       (gnus-article-set-window-start
10924        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10925     (error "Search failed: \"%s\"" regexp)))
10926
10927 (defun gnus-summary-search-article-backward (regexp)
10928   "Search for an article containing REGEXP backward."
10929   (interactive
10930    (list (read-string
10931           (format "Search article backward (regexp%s): "
10932                   (if gnus-last-search-regexp
10933                       (concat ", default " gnus-last-search-regexp)
10934                     "")))))
10935   (gnus-summary-search-article-forward regexp 'backward))
10936
10937 (defun gnus-summary-search-article (regexp &optional backward)
10938   "Search for an article containing REGEXP.
10939 Optional argument BACKWARD means do search for backward.
10940 gnus-select-article-hook is not called during the search."
10941   (let ((gnus-select-article-hook nil)  ;Disable hook.
10942         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10943         (re-search
10944          (if backward
10945              (function re-search-backward) (function re-search-forward)))
10946         (found nil)
10947         (last nil))
10948     ;; Hidden thread subtrees must be searched for ,too.
10949     (gnus-summary-show-all-threads)
10950     ;; First of all, search current article.
10951     ;; We don't want to read article again from NNTP server nor reset
10952     ;; current point.
10953     (gnus-summary-select-article)
10954     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10955     (setq last gnus-current-article)
10956     (gnus-eval-in-buffer-window
10957      gnus-article-buffer
10958      (save-restriction
10959        (widen)
10960        ;; Begin search from current point.
10961        (setq found (funcall re-search regexp nil t))))
10962     ;; Then search next articles.
10963     (while (and (not found)
10964                 (gnus-summary-display-article
10965                  (if backward (gnus-summary-find-prev)
10966                    (gnus-summary-find-next))))
10967       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10968       (gnus-eval-in-buffer-window
10969        gnus-article-buffer
10970        (save-restriction
10971          (widen)
10972          (goto-char (if backward (point-max) (point-min)))
10973          (setq found (funcall re-search regexp nil t)))))
10974     (message "")
10975     ;; Adjust article pointer.
10976     (or (eq last gnus-current-article)
10977         (setq gnus-last-article last))
10978     ;; Return T if found such article.
10979     found))
10980
10981 (defun gnus-summary-find-matching (header regexp &optional backward unread
10982                                           not-case-fold)
10983   "Return a list of all articles that match REGEXP on HEADER.
10984 The search stars on the current article and goes forwards unless
10985 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10986 If UNREAD is non-nil, only unread articles will
10987 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10988 in the comparisons."
10989   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10990                 (gnus-data-find-list
10991                  (gnus-summary-article-number) (gnus-data-list backward))))
10992         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
10993         (case-fold-search (not not-case-fold))
10994         articles d)
10995     (or (fboundp (intern (concat "mail-header-" header)))
10996         (error "%s is not a valid header" header))
10997     (while data
10998       (setq d (car data))
10999       (and (or (not unread)             ; We want all articles...
11000                (gnus-data-unread-p d))  ; Or just unreads.
11001            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11002            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11003            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11004       (setq data (cdr data)))
11005     (nreverse articles)))
11006
11007 (defun gnus-summary-execute-command (header regexp command &optional backward)
11008   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11009 If HEADER is an empty string (or nil), the match is done on the entire
11010 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11011   (interactive
11012    (list (let ((completion-ignore-case t))
11013            (completing-read
11014             "Header name: "
11015             (mapcar (lambda (string) (list string))
11016                     '("Number" "Subject" "From" "Lines" "Date"
11017                       "Message-ID" "Xref" "References" "Body"))
11018             nil 'require-match))
11019          (read-string "Regexp: ")
11020          (read-key-sequence "Command: ")
11021          current-prefix-arg))
11022   (when (equal header "Body")
11023     (setq header ""))
11024   (gnus-set-global-variables)
11025   ;; Hidden thread subtrees must be searched as well.
11026   (gnus-summary-show-all-threads)
11027   ;; We don't want to change current point nor window configuration.
11028   (save-excursion
11029     (save-window-excursion
11030       (gnus-message 6 "Executing %s..." (key-description command))
11031       ;; We'd like to execute COMMAND interactively so as to give arguments.
11032       (gnus-execute header regexp
11033                     `(lambda () (call-interactively ',(key-binding command)))
11034                     backward)
11035       (gnus-message 6 "Executing %s...done" (key-description command)))))
11036
11037 (defun gnus-summary-beginning-of-article ()
11038   "Scroll the article back to the beginning."
11039   (interactive)
11040   (gnus-set-global-variables)
11041   (gnus-summary-select-article)
11042   (gnus-configure-windows 'article)
11043   (gnus-eval-in-buffer-window
11044    gnus-article-buffer
11045    (widen)
11046    (goto-char (point-min))
11047    (and gnus-break-pages (gnus-narrow-to-page))))
11048
11049 (defun gnus-summary-end-of-article ()
11050   "Scroll to the end of the article."
11051   (interactive)
11052   (gnus-set-global-variables)
11053   (gnus-summary-select-article)
11054   (gnus-configure-windows 'article)
11055   (gnus-eval-in-buffer-window
11056    gnus-article-buffer
11057    (widen)
11058    (goto-char (point-max))
11059    (recenter -3)
11060    (and gnus-break-pages (gnus-narrow-to-page))))
11061
11062 (defun gnus-summary-show-article (&optional arg)
11063   "Force re-fetching of the current article.
11064 If ARG (the prefix) is non-nil, show the raw article without any
11065 article massaging functions being run."
11066   (interactive "P")
11067   (gnus-set-global-variables)
11068   (if (not arg)
11069       ;; Select the article the normal way.
11070       (gnus-summary-select-article nil 'force)
11071     ;; Bind the article treatment functions to nil.
11072     (let ((gnus-have-all-headers t)
11073           gnus-article-display-hook
11074           gnus-article-prepare-hook
11075           gnus-visual)
11076       (gnus-summary-select-article nil 'force)))
11077 ;  (gnus-configure-windows 'article)
11078   (gnus-summary-position-point))
11079
11080 (defun gnus-summary-verbose-headers (&optional arg)
11081   "Toggle permanent full header display.
11082 If ARG is a positive number, turn header display on.
11083 If ARG is a negative number, turn header display off."
11084   (interactive "P")
11085   (gnus-set-global-variables)
11086   (gnus-summary-toggle-header arg)
11087   (setq gnus-show-all-headers
11088         (cond ((or (not (numberp arg))
11089                    (zerop arg))
11090                (not gnus-show-all-headers))
11091               ((natnump arg)
11092                t))))
11093
11094 (defun gnus-summary-toggle-header (&optional arg)
11095   "Show the headers if they are hidden, or hide them if they are shown.
11096 If ARG is a positive number, show the entire header.
11097 If ARG is a negative number, hide the unwanted header lines."
11098   (interactive "P")
11099   (gnus-set-global-variables)
11100   (save-excursion
11101     (set-buffer gnus-article-buffer)
11102     (let* ((buffer-read-only nil)
11103            (inhibit-point-motion-hooks t)
11104            (hidden (text-property-any
11105                     (goto-char (point-min)) (search-forward "\n\n")
11106                     'invisible t))
11107            e)
11108       (goto-char (point-min))
11109       (when (search-forward "\n\n" nil t)
11110         (delete-region (point-min) (1- (point))))
11111       (goto-char (point-min))
11112       (save-excursion
11113         (set-buffer gnus-original-article-buffer)
11114         (goto-char (point-min))
11115         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11116       (insert-buffer-substring gnus-original-article-buffer 1 e)
11117       (let ((gnus-inhibit-hiding t))
11118         (run-hooks 'gnus-article-display-hook))
11119       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11120           (gnus-article-hide-headers)))))
11121
11122 (defun gnus-summary-show-all-headers ()
11123   "Make all header lines visible."
11124   (interactive)
11125   (gnus-set-global-variables)
11126   (gnus-article-show-all-headers))
11127
11128 (defun gnus-summary-toggle-mime (&optional arg)
11129   "Toggle MIME processing.
11130 If ARG is a positive number, turn MIME processing on."
11131   (interactive "P")
11132   (gnus-set-global-variables)
11133   (setq gnus-show-mime
11134         (if (null arg) (not gnus-show-mime)
11135           (> (prefix-numeric-value arg) 0)))
11136   (gnus-summary-select-article t 'force))
11137
11138 (defun gnus-summary-caesar-message (&optional arg)
11139   "Caesar rotate the current article by 13.
11140 The numerical prefix specifies how manu places to rotate each letter
11141 forward."
11142   (interactive "P")
11143   (gnus-set-global-variables)
11144   (gnus-summary-select-article)
11145   (let ((mail-header-separator ""))
11146     (gnus-eval-in-buffer-window
11147      gnus-article-buffer
11148      (save-restriction
11149        (widen)
11150        (let ((start (window-start)))
11151          (news-caesar-buffer-body arg)
11152          (set-window-start (get-buffer-window (current-buffer)) start))))))
11153
11154 (defun gnus-summary-stop-page-breaking ()
11155   "Stop page breaking in the current article."
11156   (interactive)
11157   (gnus-set-global-variables)
11158   (gnus-summary-select-article)
11159   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11160
11161 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11162   "Move the current article to a different newsgroup.
11163 If N is a positive number, move the N next articles.
11164 If N is a negative number, move the N previous articles.
11165 If N is nil and any articles have been marked with the process mark,
11166 move those articles instead.
11167 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11168 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11169 re-spool using this method.
11170
11171 For this function to work, both the current newsgroup and the
11172 newsgroup that you want to move to have to support the `request-move'
11173 and `request-accept' functions."
11174   (interactive "P")
11175   (unless action (setq action 'move))
11176   (gnus-set-global-variables)
11177   ;; Check whether the source group supports the required functions.
11178   (cond ((and (eq action 'move)
11179               (not (gnus-check-backend-function
11180                     'request-move-article gnus-newsgroup-name)))
11181          (error "The current group does not support article moving"))
11182         ((and (eq action 'crosspost)
11183               (not (gnus-check-backend-function
11184                     'request-replace-article gnus-newsgroup-name)))
11185          (error "The current group does not support article editing")))
11186   (let ((articles (gnus-summary-work-articles n))
11187         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11188         (names '((move "move" "Moving")
11189                  (copy "copy" "Copying")
11190                  (crosspost "crosspost" "Crossposting")))
11191         (copy-buf (save-excursion
11192                     (nnheader-set-temp-buffer " *copy article*")))
11193         art-group to-method new-xref article to-groups)
11194     (unless (assq action names)
11195       (error "Unknown action %s" action))
11196     ;; Read the newsgroup name.
11197     (when (and (not to-newsgroup)
11198                (not select-method))
11199       (setq to-newsgroup
11200             (gnus-read-move-group-name
11201              (cadr (assq action names))
11202              gnus-current-move-group articles prefix))
11203       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11204     (setq to-method (or select-method 
11205                         (gnus-find-method-for-group to-newsgroup)))
11206     ;; Check the method we are to move this article to...
11207     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11208         (error "%s does not support article copying" (car to-method)))
11209     (or (gnus-check-server to-method)
11210         (error "Can't open server %s" (car to-method)))
11211     (gnus-message 6 "%s to %s: %s..."
11212                   (caddr (assq action names))
11213                   (or (car select-method) to-newsgroup) articles)
11214     (while articles
11215       (setq article (pop articles))
11216       (setq
11217        art-group
11218        (cond
11219         ;; Move the article.
11220         ((eq action 'move)
11221          (gnus-request-move-article
11222           article                       ; Article to move
11223           gnus-newsgroup-name           ; From newsgrouo
11224           (nth 1 (gnus-find-method-for-group
11225                   gnus-newsgroup-name)) ; Server
11226           (list 'gnus-request-accept-article
11227                 to-newsgroup (list 'quote select-method)
11228                 (not articles))         ; Accept form
11229           (not articles)))              ; Only save nov last time
11230         ;; Copy the article.
11231         ((eq action 'copy)
11232          (save-excursion
11233            (set-buffer copy-buf)
11234            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11235            (gnus-request-accept-article
11236             to-newsgroup select-method (not articles))))
11237         ;; Crosspost the article.
11238         ((eq action 'crosspost)
11239          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11240            (setq new-xref (concat gnus-newsgroup-name ":" article))
11241            (if (and xref (not (string= xref "")))
11242                (progn
11243                  (when (string-match "^Xref: " xref)
11244                    (setq xref (substring xref (match-end 0))))
11245                  (setq new-xref (concat xref " " new-xref)))
11246              (setq new-xref (concat (system-name) " " new-xref)))
11247            (save-excursion
11248              (set-buffer copy-buf)
11249              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11250              (nnheader-replace-header "xref" new-xref)
11251              (gnus-request-accept-article
11252               to-newsgroup select-method (not articles)))))))
11253       (if (not art-group)
11254           (gnus-message 1 "Couldn't %s article %s"
11255                         (cadr (assq action names)) article)
11256         (let* ((entry
11257                 (or
11258                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11259                  (gnus-gethash
11260                   (gnus-group-prefixed-name
11261                    (car art-group)
11262                    (or select-method 
11263                        (gnus-find-method-for-group to-newsgroup)))
11264                   gnus-newsrc-hashtb)))
11265                (info (nth 2 entry))
11266                (to-group (gnus-info-group info)))
11267           ;; Update the group that has been moved to.
11268           (when (and info
11269                      (memq action '(move copy)))
11270             (unless (member to-group to-groups)
11271               (push to-group to-groups))
11272
11273             (unless (memq article gnus-newsgroup-unreads)
11274               (gnus-info-set-read
11275                info (gnus-add-to-range (gnus-info-read info)
11276                                        (list (cdr art-group)))))
11277
11278             ;; Copy any marks over to the new group.
11279             (let ((marks gnus-article-mark-lists)
11280                   (to-article (cdr art-group)))
11281
11282               ;; See whether the article is to be put in the cache.
11283               (when gnus-use-cache
11284                 (gnus-cache-possibly-enter-article
11285                  to-group to-article
11286                  (let ((header (copy-sequence
11287                                 (gnus-summary-article-header article))))
11288                    (mail-header-set-number header to-article)
11289                    header)
11290                  (memq article gnus-newsgroup-marked)
11291                  (memq article gnus-newsgroup-dormant)
11292                  (memq article gnus-newsgroup-unreads)))
11293
11294               (while marks
11295                 (when (memq article (symbol-value
11296                                      (intern (format "gnus-newsgroup-%s"
11297                                                      (caar marks)))))
11298                   ;; If the other group is the same as this group,
11299                   ;; then we have to add the mark to the list.
11300                   (when (equal to-group gnus-newsgroup-name)
11301                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11302                          (cons to-article
11303                                (symbol-value
11304                                 (intern (format "gnus-newsgroup-%s"
11305                                                 (caar marks)))))))
11306                   ;; Copy mark to other group.
11307                   (gnus-add-marked-articles
11308                    to-group (cdar marks) (list to-article) info))
11309                 (setq marks (cdr marks)))))
11310
11311           ;; Update the Xref header in this article to point to
11312           ;; the new crossposted article we have just created.
11313           (when (eq action 'crosspost)
11314             (save-excursion
11315               (set-buffer copy-buf)
11316               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11317               (nnheader-replace-header
11318                "xref" (concat new-xref " " (gnus-group-prefixed-name
11319                                             (car art-group) to-method)
11320                               ":" (cdr art-group)))
11321               (gnus-request-replace-article
11322                article gnus-newsgroup-name (current-buffer)))))
11323
11324         (gnus-summary-goto-subject article)
11325         (when (eq action 'move)
11326           (gnus-summary-mark-article article gnus-canceled-mark)))
11327       (gnus-summary-remove-process-mark article))
11328     ;; Re-activate all groups that have been moved to.
11329     (while to-groups
11330       (gnus-activate-group (pop to-groups)))
11331     
11332     (gnus-kill-buffer copy-buf)
11333     (gnus-summary-position-point)
11334     (gnus-set-mode-line 'summary)))
11335
11336 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11337   "Move the current article to a different newsgroup.
11338 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11339 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11340 re-spool using this method."
11341   (interactive "P")
11342   (gnus-summary-move-article n nil select-method 'copy))
11343
11344 (defun gnus-summary-crosspost-article (&optional n)
11345   "Crosspost the current article to some other group."
11346   (interactive "P")
11347   (gnus-summary-move-article n nil nil 'crosspost))
11348
11349 (defun gnus-summary-respool-article (&optional n method)
11350   "Respool the current article.
11351 The article will be squeezed through the mail spooling process again,
11352 which means that it will be put in some mail newsgroup or other
11353 depending on `nnmail-split-methods'.
11354 If N is a positive number, respool the N next articles.
11355 If N is a negative number, respool the N previous articles.
11356 If N is nil and any articles have been marked with the process mark,
11357 respool those articles instead.
11358
11359 Respooling can be done both from mail groups and \"real\" newsgroups.
11360 In the former case, the articles in question will be moved from the
11361 current group into whatever groups they are destined to.  In the
11362 latter case, they will be copied into the relevant groups."
11363   (interactive 
11364    (list current-prefix-arg
11365          (let* ((methods (gnus-methods-using 'respool))
11366                 (methname
11367                  (symbol-name (car (gnus-find-method-for-group
11368                                     gnus-newsgroup-name))))
11369                 (method
11370                  (completing-read
11371                   "What backend do you want to use when respooling? "
11372                   methods nil t (cons methname 0)))
11373                 ms)
11374            (cond
11375             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11376              (list (intern method) ""))
11377             ((= 1 (length ms))
11378              (car ms))
11379             (t
11380              (cdr (completing-read 
11381                    "Server name: "
11382                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11383   (gnus-set-global-variables)
11384   (unless method
11385     (error "No method given for respooling"))
11386   (if (assoc (symbol-name
11387               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11388              (gnus-methods-using 'respool))
11389       (gnus-summary-move-article n nil method)
11390     (gnus-summary-copy-article n nil method)))
11391
11392 (defun gnus-summary-import-article (file)
11393   "Import a random file into a mail newsgroup."
11394   (interactive "fImport file: ")
11395   (gnus-set-global-variables)
11396   (let ((group gnus-newsgroup-name)
11397         (now (current-time))
11398         atts lines)
11399     (or (gnus-check-backend-function 'request-accept-article group)
11400         (error "%s does not support article importing" group))
11401     (or (file-readable-p file)
11402         (not (file-regular-p file))
11403         (error "Can't read %s" file))
11404     (save-excursion
11405       (set-buffer (get-buffer-create " *import file*"))
11406       (buffer-disable-undo (current-buffer))
11407       (erase-buffer)
11408       (insert-file-contents file)
11409       (goto-char (point-min))
11410       (unless (nnheader-article-p)
11411         ;; This doesn't look like an article, so we fudge some headers.
11412         (setq atts (file-attributes file)
11413               lines (count-lines (point-min) (point-max)))
11414         (insert "From: " (read-string "From: ") "\n"
11415                 "Subject: " (read-string "Subject: ") "\n"
11416                 "Date: " (timezone-make-date-arpa-standard
11417                           (current-time-string (nth 5 atts))
11418                           (current-time-zone now)
11419                           (current-time-zone now)) "\n"
11420                 "Message-ID: " (gnus-inews-message-id) "\n"
11421                 "Lines: " (int-to-string lines) "\n"
11422                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11423       (gnus-request-accept-article group nil t)
11424       (kill-buffer (current-buffer)))))
11425
11426 (defun gnus-summary-expire-articles ()
11427   "Expire all articles that are marked as expirable in the current group."
11428   (interactive)
11429   (gnus-set-global-variables)
11430   (when (gnus-check-backend-function
11431          'request-expire-articles gnus-newsgroup-name)
11432     ;; This backend supports expiry.
11433     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11434            (expirable (if total
11435                           (gnus-list-of-read-articles gnus-newsgroup-name)
11436                         (setq gnus-newsgroup-expirable
11437                               (sort gnus-newsgroup-expirable '<))))
11438            (expiry-wait (gnus-group-get-parameter
11439                          gnus-newsgroup-name 'expiry-wait))
11440            es)
11441       (when expirable
11442         ;; There are expirable articles in this group, so we run them
11443         ;; through the expiry process.
11444         (gnus-message 6 "Expiring articles...")
11445         ;; The list of articles that weren't expired is returned.
11446         (if expiry-wait
11447             (let ((nnmail-expiry-wait-function nil)
11448                   (nnmail-expiry-wait expiry-wait))
11449               (setq es (gnus-request-expire-articles
11450                         expirable gnus-newsgroup-name)))
11451           (setq es (gnus-request-expire-articles
11452                     expirable gnus-newsgroup-name)))
11453         (or total (setq gnus-newsgroup-expirable es))
11454         ;; We go through the old list of expirable, and mark all
11455         ;; really expired articles as nonexistent.
11456         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11457           (let ((gnus-use-cache nil))
11458             (while expirable
11459               (unless (memq (car expirable) es)
11460                 (when (gnus-data-find (car expirable))
11461                   (gnus-summary-mark-article
11462                    (car expirable) gnus-canceled-mark)))
11463               (setq expirable (cdr expirable)))))
11464         (gnus-message 6 "Expiring articles...done")))))
11465
11466 (defun gnus-summary-expire-articles-now ()
11467   "Expunge all expirable articles in the current group.
11468 This means that *all* articles that are marked as expirable will be
11469 deleted forever, right now."
11470   (interactive)
11471   (gnus-set-global-variables)
11472   (or gnus-expert-user
11473       (gnus-y-or-n-p
11474        "Are you really, really, really sure you want to expunge? ")
11475       (error "Phew!"))
11476   (let ((nnmail-expiry-wait 'immediate)
11477         (nnmail-expiry-wait-function nil))
11478     (gnus-summary-expire-articles)))
11479
11480 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11481 (defun gnus-summary-delete-article (&optional n)
11482   "Delete the N next (mail) articles.
11483 This command actually deletes articles.  This is not a marking
11484 command.  The article will disappear forever from your life, never to
11485 return.
11486 If N is negative, delete backwards.
11487 If N is nil and articles have been marked with the process mark,
11488 delete these instead."
11489   (interactive "P")
11490   (gnus-set-global-variables)
11491   (or (gnus-check-backend-function 'request-expire-articles
11492                                    gnus-newsgroup-name)
11493       (error "The current newsgroup does not support article deletion."))
11494   ;; Compute the list of articles to delete.
11495   (let ((articles (gnus-summary-work-articles n))
11496         not-deleted)
11497     (if (and gnus-novice-user
11498              (not (gnus-y-or-n-p
11499                    (format "Do you really want to delete %s forever? "
11500                            (if (> (length articles) 1) "these articles"
11501                              "this article")))))
11502         ()
11503       ;; Delete the articles.
11504       (setq not-deleted (gnus-request-expire-articles
11505                          articles gnus-newsgroup-name 'force))
11506       (while articles
11507         (gnus-summary-remove-process-mark (car articles))
11508         ;; The backend might not have been able to delete the article
11509         ;; after all.
11510         (or (memq (car articles) not-deleted)
11511             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11512         (setq articles (cdr articles))))
11513     (gnus-summary-position-point)
11514     (gnus-set-mode-line 'summary)
11515     not-deleted))
11516
11517 (defun gnus-summary-edit-article (&optional force)
11518   "Enter into a buffer and edit the current article.
11519 This will have permanent effect only in mail groups.
11520 If FORCE is non-nil, allow editing of articles even in read-only
11521 groups."
11522   (interactive "P")
11523   (save-excursion
11524     (set-buffer gnus-summary-buffer)
11525     (gnus-set-global-variables)
11526     (when (and (not force)
11527                (gnus-group-read-only-p))
11528       (error "The current newsgroup does not support article editing."))
11529     (gnus-summary-select-article t nil t)
11530     (gnus-configure-windows 'article)
11531     (select-window (get-buffer-window gnus-article-buffer))
11532     (gnus-message 6 "C-c C-c to end edits")
11533     (setq buffer-read-only nil)
11534     (text-mode)
11535     (use-local-map (copy-keymap (current-local-map)))
11536     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11537     (buffer-enable-undo)
11538     (widen)
11539     (goto-char (point-min))
11540     (search-forward "\n\n" nil t)))
11541
11542 (defun gnus-summary-edit-article-done ()
11543   "Make edits to the current article permanent."
11544   (interactive)
11545   (if (gnus-group-read-only-p)
11546       (progn
11547         (gnus-summary-edit-article-postpone)
11548         (gnus-message
11549          1 "The current newsgroup does not support article editing.")
11550         (ding))
11551     (let ((buf (format "%s" (buffer-string))))
11552       (erase-buffer)
11553       (insert buf)
11554       (if (not (gnus-request-replace-article
11555                 (cdr gnus-article-current) (car gnus-article-current)
11556                 (current-buffer)))
11557           (error "Couldn't replace article.")
11558         (gnus-article-mode)
11559         (use-local-map gnus-article-mode-map)
11560         (setq buffer-read-only t)
11561         (buffer-disable-undo (current-buffer))
11562         (gnus-configure-windows 'summary)
11563         (gnus-summary-update-article (cdr gnus-article-current))
11564         (when gnus-use-cache
11565           (gnus-cache-update-article 
11566            (cdr gnus-article-current) (car gnus-article-current))))
11567       (run-hooks 'gnus-article-display-hook)
11568       (and (gnus-visual-p 'summary-highlight 'highlight)
11569            (run-hooks 'gnus-visual-mark-article-hook)))))
11570
11571 (defun gnus-summary-edit-article-postpone ()
11572   "Postpone changes to the current article."
11573   (interactive)
11574   (gnus-article-mode)
11575   (use-local-map gnus-article-mode-map)
11576   (setq buffer-read-only t)
11577   (buffer-disable-undo (current-buffer))
11578   (gnus-configure-windows 'summary)
11579   (and (gnus-visual-p 'summary-highlight 'highlight)
11580        (run-hooks 'gnus-visual-mark-article-hook)))
11581
11582 (defun gnus-summary-respool-query ()
11583   "Query where the respool algorithm would put this article."
11584   (interactive)
11585   (gnus-set-global-variables)
11586   (gnus-summary-select-article)
11587   (save-excursion
11588     (set-buffer gnus-article-buffer)
11589     (save-restriction
11590       (goto-char (point-min))
11591       (search-forward "\n\n")
11592       (narrow-to-region (point-min) (point))
11593       (pp-eval-expression
11594        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11595
11596 ;; Summary score commands.
11597
11598 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11599
11600 (defun gnus-summary-raise-score (n)
11601   "Raise the score of the current article by N."
11602   (interactive "p")
11603   (gnus-set-global-variables)
11604   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11605
11606 (defun gnus-summary-set-score (n)
11607   "Set the score of the current article to N."
11608   (interactive "p")
11609   (gnus-set-global-variables)
11610   (save-excursion
11611     (gnus-summary-show-thread)
11612     (let ((buffer-read-only nil))
11613       ;; Set score.
11614       (gnus-summary-update-mark
11615        (if (= n (or gnus-summary-default-score 0)) ? 
11616          (if (< n (or gnus-summary-default-score 0))
11617              gnus-score-below-mark gnus-score-over-mark)) 'score))
11618     (let* ((article (gnus-summary-article-number))
11619            (score (assq article gnus-newsgroup-scored)))
11620       (if score (setcdr score n)
11621         (setq gnus-newsgroup-scored
11622               (cons (cons article n) gnus-newsgroup-scored))))
11623     (gnus-summary-update-line)))
11624
11625 (defun gnus-summary-current-score ()
11626   "Return the score of the current article."
11627   (interactive)
11628   (gnus-set-global-variables)
11629   (message "%s" (gnus-summary-article-score)))
11630
11631 ;; Summary marking commands.
11632
11633 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11634   "Mark articles which has the same subject as read, and then select the next.
11635 If UNMARK is positive, remove any kind of mark.
11636 If UNMARK is negative, tick articles."
11637   (interactive "P")
11638   (gnus-set-global-variables)
11639   (if unmark
11640       (setq unmark (prefix-numeric-value unmark)))
11641   (let ((count
11642          (gnus-summary-mark-same-subject
11643           (gnus-summary-article-subject) unmark)))
11644     ;; Select next unread article.  If auto-select-same mode, should
11645     ;; select the first unread article.
11646     (gnus-summary-next-article t (and gnus-auto-select-same
11647                                       (gnus-summary-article-subject)))
11648     (gnus-message 7 "%d article%s marked as %s"
11649                   count (if (= count 1) " is" "s are")
11650                   (if unmark "unread" "read"))))
11651
11652 (defun gnus-summary-kill-same-subject (&optional unmark)
11653   "Mark articles which has the same subject as read.
11654 If UNMARK is positive, remove any kind of mark.
11655 If UNMARK is negative, tick articles."
11656   (interactive "P")
11657   (gnus-set-global-variables)
11658   (if unmark
11659       (setq unmark (prefix-numeric-value unmark)))
11660   (let ((count
11661          (gnus-summary-mark-same-subject
11662           (gnus-summary-article-subject) unmark)))
11663     ;; If marked as read, go to next unread subject.
11664     (if (null unmark)
11665         ;; Go to next unread subject.
11666         (gnus-summary-next-subject 1 t))
11667     (gnus-message 7 "%d articles are marked as %s"
11668                   count (if unmark "unread" "read"))))
11669
11670 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11671   "Mark articles with same SUBJECT as read, and return marked number.
11672 If optional argument UNMARK is positive, remove any kinds of marks.
11673 If optional argument UNMARK is negative, mark articles as unread instead."
11674   (let ((count 1))
11675     (save-excursion
11676       (cond
11677        ((null unmark)                   ; Mark as read.
11678         (while (and
11679                 (progn
11680                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11681                   (gnus-summary-show-thread) t)
11682                 (gnus-summary-find-subject subject))
11683           (setq count (1+ count))))
11684        ((> unmark 0)                    ; Tick.
11685         (while (and
11686                 (progn
11687                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11688                   (gnus-summary-show-thread) t)
11689                 (gnus-summary-find-subject subject))
11690           (setq count (1+ count))))
11691        (t                               ; Mark as unread.
11692         (while (and
11693                 (progn
11694                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11695                   (gnus-summary-show-thread) t)
11696                 (gnus-summary-find-subject subject))
11697           (setq count (1+ count)))))
11698       (gnus-set-mode-line 'summary)
11699       ;; Return the number of marked articles.
11700       count)))
11701
11702 (defun gnus-summary-mark-as-processable (n &optional unmark)
11703   "Set the process mark on the next N articles.
11704 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11705 the process mark instead.  The difference between N and the actual
11706 number of articles marked is returned."
11707   (interactive "p")
11708   (gnus-set-global-variables)
11709   (let ((backward (< n 0))
11710         (n (abs n)))
11711     (while (and
11712             (> n 0)
11713             (if unmark
11714                 (gnus-summary-remove-process-mark
11715                  (gnus-summary-article-number))
11716               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11717             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11718       (setq n (1- n)))
11719     (if (/= 0 n) (gnus-message 7 "No more articles"))
11720     (gnus-summary-recenter)
11721     (gnus-summary-position-point)
11722     n))
11723
11724 (defun gnus-summary-unmark-as-processable (n)
11725   "Remove the process mark from the next N articles.
11726 If N is negative, mark backward instead.  The difference between N and
11727 the actual number of articles marked is returned."
11728   (interactive "p")
11729   (gnus-set-global-variables)
11730   (gnus-summary-mark-as-processable n t))
11731
11732 (defun gnus-summary-unmark-all-processable ()
11733   "Remove the process mark from all articles."
11734   (interactive)
11735   (gnus-set-global-variables)
11736   (save-excursion
11737     (while gnus-newsgroup-processable
11738       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11739   (gnus-summary-position-point))
11740
11741 (defun gnus-summary-mark-as-expirable (n)
11742   "Mark N articles forward as expirable.
11743 If N is negative, mark backward instead.  The difference between N and
11744 the actual number of articles marked is returned."
11745   (interactive "p")
11746   (gnus-set-global-variables)
11747   (gnus-summary-mark-forward n gnus-expirable-mark))
11748
11749 (defun gnus-summary-mark-article-as-replied (article)
11750   "Mark ARTICLE replied and update the summary line."
11751   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11752   (let ((buffer-read-only nil))
11753     (when (gnus-summary-goto-subject article)
11754       (gnus-summary-update-secondary-mark article))))
11755
11756 (defun gnus-summary-set-bookmark (article)
11757   "Set a bookmark in current article."
11758   (interactive (list (gnus-summary-article-number)))
11759   (gnus-set-global-variables)
11760   (if (or (not (get-buffer gnus-article-buffer))
11761           (not gnus-current-article)
11762           (not gnus-article-current)
11763           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11764       (error "No current article selected"))
11765   ;; Remove old bookmark, if one exists.
11766   (let ((old (assq article gnus-newsgroup-bookmarks)))
11767     (if old (setq gnus-newsgroup-bookmarks
11768                   (delq old gnus-newsgroup-bookmarks))))
11769   ;; Set the new bookmark, which is on the form
11770   ;; (article-number . line-number-in-body).
11771   (setq gnus-newsgroup-bookmarks
11772         (cons
11773          (cons article
11774                (save-excursion
11775                  (set-buffer gnus-article-buffer)
11776                  (count-lines
11777                   (min (point)
11778                        (save-excursion
11779                          (goto-char (point-min))
11780                          (search-forward "\n\n" nil t)
11781                          (point)))
11782                   (point))))
11783          gnus-newsgroup-bookmarks))
11784   (gnus-message 6 "A bookmark has been added to the current article."))
11785
11786 (defun gnus-summary-remove-bookmark (article)
11787   "Remove the bookmark from the current article."
11788   (interactive (list (gnus-summary-article-number)))
11789   (gnus-set-global-variables)
11790   ;; Remove old bookmark, if one exists.
11791   (let ((old (assq article gnus-newsgroup-bookmarks)))
11792     (if old
11793         (progn
11794           (setq gnus-newsgroup-bookmarks
11795                 (delq old gnus-newsgroup-bookmarks))
11796           (gnus-message 6 "Removed bookmark."))
11797       (gnus-message 6 "No bookmark in current article."))))
11798
11799 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11800 (defun gnus-summary-mark-as-dormant (n)
11801   "Mark N articles forward as dormant.
11802 If N is negative, mark backward instead.  The difference between N and
11803 the actual number of articles marked is returned."
11804   (interactive "p")
11805   (gnus-set-global-variables)
11806   (gnus-summary-mark-forward n gnus-dormant-mark))
11807
11808 (defun gnus-summary-set-process-mark (article)
11809   "Set the process mark on ARTICLE and update the summary line."
11810   (setq gnus-newsgroup-processable
11811         (cons article
11812               (delq article gnus-newsgroup-processable)))
11813   (when (gnus-summary-goto-subject article)
11814     (gnus-summary-show-thread)
11815     (gnus-summary-update-secondary-mark article)))
11816
11817 (defun gnus-summary-remove-process-mark (article)
11818   "Remove the process mark from ARTICLE and update the summary line."
11819   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11820   (when (gnus-summary-goto-subject article)
11821     (gnus-summary-show-thread)
11822     (gnus-summary-update-secondary-mark article)))
11823
11824 (defun gnus-summary-set-saved-mark (article)
11825   "Set the process mark on ARTICLE and update the summary line."
11826   (push article gnus-newsgroup-saved)
11827   (when (gnus-summary-goto-subject article)
11828     (gnus-summary-update-secondary-mark article)))
11829
11830 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11831   "Mark N articles as read forwards.
11832 If N is negative, mark backwards instead.
11833 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11834 marked as unread.
11835 The difference between N and the actual number of articles marked is
11836 returned."
11837   (interactive "p")
11838   (gnus-set-global-variables)
11839   (let ((backward (< n 0))
11840         (gnus-summary-goto-unread
11841          (and gnus-summary-goto-unread
11842               (not (eq gnus-summary-goto-unread 'never))
11843               (not (memq mark (list gnus-unread-mark
11844                                     gnus-ticked-mark gnus-dormant-mark)))))
11845         (n (abs n))
11846         (mark (or mark gnus-del-mark)))
11847     (while (and (> n 0)
11848                 (gnus-summary-mark-article nil mark no-expire)
11849                 (zerop (gnus-summary-next-subject
11850                         (if backward -1 1)
11851                         (and gnus-summary-goto-unread
11852                              (not (eq gnus-summary-goto-unread 'never)))
11853                         t)))
11854       (setq n (1- n)))
11855     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11856     (gnus-summary-recenter)
11857     (gnus-summary-position-point)
11858     (gnus-set-mode-line 'summary)
11859     n))
11860
11861 (defun gnus-summary-mark-article-as-read (mark)
11862   "Mark the current article quickly as read with MARK."
11863   (let ((article (gnus-summary-article-number)))
11864     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11865     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11866     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11867     (setq gnus-newsgroup-reads
11868           (cons (cons article mark) gnus-newsgroup-reads))
11869     ;; Possibly remove from cache, if that is used.
11870     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11871     ;; Allow the backend to change the mark.
11872     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11873     ;; Check for auto-expiry.
11874     (when (and gnus-newsgroup-auto-expire
11875                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11876                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11877                    (= mark gnus-ancient-mark)
11878                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11879       (setq mark gnus-expirable-mark)
11880       (push article gnus-newsgroup-expirable))
11881     ;; Set the mark in the buffer.
11882     (gnus-summary-update-mark mark 'unread)
11883     t))
11884
11885 (defun gnus-summary-mark-article-as-unread (mark)
11886   "Mark the current article quickly as unread with MARK."
11887   (let ((article (gnus-summary-article-number)))
11888     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11889     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11890     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11891     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11892     (cond ((= mark gnus-ticked-mark)
11893            (push article gnus-newsgroup-marked))
11894           ((= mark gnus-dormant-mark)
11895            (push article gnus-newsgroup-dormant))
11896           (t
11897            (push article gnus-newsgroup-unreads)))
11898     (setq gnus-newsgroup-reads
11899           (delq (assq article gnus-newsgroup-reads)
11900                 gnus-newsgroup-reads))
11901
11902     ;; See whether the article is to be put in the cache.
11903     (and gnus-use-cache
11904          (vectorp (gnus-summary-article-header article))
11905          (save-excursion
11906            (gnus-cache-possibly-enter-article
11907             gnus-newsgroup-name article
11908             (gnus-summary-article-header article)
11909             (= mark gnus-ticked-mark)
11910             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11911
11912     ;; Fix the mark.
11913     (gnus-summary-update-mark mark 'unread)
11914     t))
11915
11916 (defun gnus-summary-mark-article (&optional article mark no-expire)
11917   "Mark ARTICLE with MARK.  MARK can be any character.
11918 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11919 `??' (dormant) and `?E' (expirable).
11920 If MARK is nil, then the default character `?D' is used.
11921 If ARTICLE is nil, then the article on the current line will be
11922 marked."
11923   ;; The mark might be a string.
11924   (and (stringp mark)
11925        (setq mark (aref mark 0)))
11926   ;; If no mark is given, then we check auto-expiring.
11927   (and (not no-expire)
11928        gnus-newsgroup-auto-expire
11929        (or (not mark)
11930            (and (numberp mark)
11931                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11932                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11933                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11934        (setq mark gnus-expirable-mark))
11935   (let* ((mark (or mark gnus-del-mark))
11936          (article (or article (gnus-summary-article-number))))
11937     (or article (error "No article on current line"))
11938     (if (or (= mark gnus-unread-mark)
11939             (= mark gnus-ticked-mark)
11940             (= mark gnus-dormant-mark))
11941         (gnus-mark-article-as-unread article mark)
11942       (gnus-mark-article-as-read article mark))
11943
11944     ;; See whether the article is to be put in the cache.
11945     (and gnus-use-cache
11946          (not (= mark gnus-canceled-mark))
11947          (vectorp (gnus-summary-article-header article))
11948          (save-excursion
11949            (gnus-cache-possibly-enter-article
11950             gnus-newsgroup-name article
11951             (gnus-summary-article-header article)
11952             (= mark gnus-ticked-mark)
11953             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11954
11955     (if (gnus-summary-goto-subject article nil t)
11956         (let ((buffer-read-only nil))
11957           (gnus-summary-show-thread)
11958           ;; Fix the mark.
11959           (gnus-summary-update-mark mark 'unread)
11960           t))))
11961
11962 (defun gnus-summary-update-secondary-mark (article)
11963   "Update the secondary (read, process, cache) mark."
11964   (gnus-summary-update-mark
11965    (cond ((memq article gnus-newsgroup-processable)
11966           gnus-process-mark)
11967          ((memq article gnus-newsgroup-cached)
11968           gnus-cached-mark)
11969          ((memq article gnus-newsgroup-replied)
11970           gnus-replied-mark)
11971          ((memq article gnus-newsgroup-saved)
11972           gnus-saved-mark)
11973          (t gnus-unread-mark))
11974    'replied)
11975   (when (gnus-visual-p 'summary-highlight 'highlight)
11976     (run-hooks 'gnus-summary-update-hook))
11977   t)
11978
11979 (defun gnus-summary-update-mark (mark type)
11980   (beginning-of-line)
11981   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11982         (buffer-read-only nil))
11983     (when forward
11984       ;; Go to the right position on the line.
11985       (forward-char forward)
11986       ;; Replace the old mark with the new mark.
11987       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11988       ;; Optionally update the marks by some user rule.
11989       (when (eq type 'unread)
11990         (gnus-data-set-mark
11991          (gnus-data-find (gnus-summary-article-number)) mark)
11992         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11993
11994 (defun gnus-mark-article-as-read (article &optional mark)
11995   "Enter ARTICLE in the pertinent lists and remove it from others."
11996   ;; Make the article expirable.
11997   (let ((mark (or mark gnus-del-mark)))
11998     (if (= mark gnus-expirable-mark)
11999         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12000       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12001     ;; Remove from unread and marked lists.
12002     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12003     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12004     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12005     (push (cons article mark) gnus-newsgroup-reads)
12006     ;; Possibly remove from cache, if that is used.
12007     (when gnus-use-cache
12008       (gnus-cache-enter-remove-article article))))
12009
12010 (defun gnus-mark-article-as-unread (article &optional mark)
12011   "Enter ARTICLE in the pertinent lists and remove it from others."
12012   (let ((mark (or mark gnus-ticked-mark)))
12013     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12014     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12015     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12016     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12017     (cond ((= mark gnus-ticked-mark)
12018            (push article gnus-newsgroup-marked))
12019           ((= mark gnus-dormant-mark)
12020            (push article gnus-newsgroup-dormant))
12021           (t
12022            (push article gnus-newsgroup-unreads)))
12023     (setq gnus-newsgroup-reads
12024           (delq (assq article gnus-newsgroup-reads)
12025                 gnus-newsgroup-reads))))
12026
12027 (defalias 'gnus-summary-mark-as-unread-forward
12028   'gnus-summary-tick-article-forward)
12029 (make-obsolete 'gnus-summary-mark-as-unread-forward
12030                'gnus-summary-tick-article-forward)
12031 (defun gnus-summary-tick-article-forward (n)
12032   "Tick N articles forwards.
12033 If N is negative, tick backwards instead.
12034 The difference between N and the number of articles ticked is returned."
12035   (interactive "p")
12036   (gnus-summary-mark-forward n gnus-ticked-mark))
12037
12038 (defalias 'gnus-summary-mark-as-unread-backward
12039   'gnus-summary-tick-article-backward)
12040 (make-obsolete 'gnus-summary-mark-as-unread-backward
12041                'gnus-summary-tick-article-backward)
12042 (defun gnus-summary-tick-article-backward (n)
12043   "Tick N articles backwards.
12044 The difference between N and the number of articles ticked is returned."
12045   (interactive "p")
12046   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12047
12048 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12049 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12050 (defun gnus-summary-tick-article (&optional article clear-mark)
12051   "Mark current article as unread.
12052 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12053 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12054   (interactive)
12055   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12056                                        gnus-ticked-mark)))
12057
12058 (defun gnus-summary-mark-as-read-forward (n)
12059   "Mark N articles as read forwards.
12060 If N is negative, mark backwards instead.
12061 The difference between N and the actual number of articles marked is
12062 returned."
12063   (interactive "p")
12064   (gnus-summary-mark-forward n gnus-del-mark t))
12065
12066 (defun gnus-summary-mark-as-read-backward (n)
12067   "Mark the N articles as read backwards.
12068 The difference between N and the actual number of articles marked is
12069 returned."
12070   (interactive "p")
12071   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12072
12073 (defun gnus-summary-mark-as-read (&optional article mark)
12074   "Mark current article as read.
12075 ARTICLE specifies the article to be marked as read.
12076 MARK specifies a string to be inserted at the beginning of the line."
12077   (gnus-summary-mark-article article mark))
12078
12079 (defun gnus-summary-clear-mark-forward (n)
12080   "Clear marks from N articles forward.
12081 If N is negative, clear backward instead.
12082 The difference between N and the number of marks cleared is returned."
12083   (interactive "p")
12084   (gnus-summary-mark-forward n gnus-unread-mark))
12085
12086 (defun gnus-summary-clear-mark-backward (n)
12087   "Clear marks from N articles backward.
12088 The difference between N and the number of marks cleared is returned."
12089   (interactive "p")
12090   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12091
12092 (defun gnus-summary-mark-unread-as-read ()
12093   "Intended to be used by `gnus-summary-mark-article-hook'."
12094   (when (memq gnus-current-article gnus-newsgroup-unreads)
12095     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12096
12097 (defun gnus-summary-mark-read-and-unread-as-read ()
12098   "Intended to be used by `gnus-summary-mark-article-hook'."
12099   (let ((mark (gnus-summary-article-mark)))
12100     (when (or (gnus-unread-mark-p mark)
12101               (gnus-read-mark-p mark))
12102       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12103
12104 (defun gnus-summary-mark-region-as-read (point mark all)
12105   "Mark all unread articles between point and mark as read.
12106 If given a prefix, mark all articles between point and mark as read,
12107 even ticked and dormant ones."
12108   (interactive "r\nP")
12109   (save-excursion
12110     (let (article)
12111       (goto-char point)
12112       (beginning-of-line)
12113       (while (and
12114               (< (point) mark)
12115               (progn
12116                 (when (or all
12117                           (memq (setq article (gnus-summary-article-number))
12118                                 gnus-newsgroup-unreads))
12119                   (gnus-summary-mark-article article gnus-del-mark))
12120                 t)
12121               (gnus-summary-find-next))))))
12122
12123 (defun gnus-summary-mark-below (score mark)
12124   "Mark articles with score less than SCORE with MARK."
12125   (interactive "P\ncMark: ")
12126   (gnus-set-global-variables)
12127   (setq score (if score
12128                   (prefix-numeric-value score)
12129                 (or gnus-summary-default-score 0)))
12130   (save-excursion
12131     (set-buffer gnus-summary-buffer)
12132     (goto-char (point-min))
12133     (while 
12134         (progn
12135           (and (< (gnus-summary-article-score) score)
12136                (gnus-summary-mark-article nil mark))
12137           (gnus-summary-find-next)))))
12138
12139 (defun gnus-summary-kill-below (&optional score)
12140   "Mark articles with score below SCORE as read."
12141   (interactive "P")
12142   (gnus-set-global-variables)
12143   (gnus-summary-mark-below score gnus-killed-mark))
12144
12145 (defun gnus-summary-clear-above (&optional score)
12146   "Clear all marks from articles with score above SCORE."
12147   (interactive "P")
12148   (gnus-set-global-variables)
12149   (gnus-summary-mark-above score gnus-unread-mark))
12150
12151 (defun gnus-summary-tick-above (&optional score)
12152   "Tick all articles with score above SCORE."
12153   (interactive "P")
12154   (gnus-set-global-variables)
12155   (gnus-summary-mark-above score gnus-ticked-mark))
12156
12157 (defun gnus-summary-mark-above (score mark)
12158   "Mark articles with score over SCORE with MARK."
12159   (interactive "P\ncMark: ")
12160   (gnus-set-global-variables)
12161   (setq score (if score
12162                   (prefix-numeric-value score)
12163                 (or gnus-summary-default-score 0)))
12164   (save-excursion
12165     (set-buffer gnus-summary-buffer)
12166     (goto-char (point-min))
12167     (while (and (progn
12168                   (if (> (gnus-summary-article-score) score)
12169                       (gnus-summary-mark-article nil mark))
12170                   t)
12171                 (gnus-summary-find-next)))))
12172
12173 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12174 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12175 (defun gnus-summary-limit-include-expunged ()
12176   "Display all the hidden articles that were expunged for low scores."
12177   (interactive)
12178   (gnus-set-global-variables)
12179   (let ((buffer-read-only nil))
12180     (let ((scored gnus-newsgroup-scored)
12181           headers h)
12182       (while scored
12183         (or (gnus-summary-goto-subject (caar scored))
12184             (and (setq h (gnus-summary-article-header (caar scored)))
12185                  (< (cdar scored) gnus-summary-expunge-below)
12186                  (setq headers (cons h headers))))
12187         (setq scored (cdr scored)))
12188       (or headers (error "No expunged articles hidden."))
12189       (goto-char (point-min))
12190       (gnus-summary-prepare-unthreaded (nreverse headers)))
12191     (goto-char (point-min))
12192     (gnus-summary-position-point)))
12193
12194 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12195   "Mark all articles not marked as unread in this newsgroup as read.
12196 If prefix argument ALL is non-nil, all articles are marked as read.
12197 If QUIETLY is non-nil, no questions will be asked.
12198 If TO-HERE is non-nil, it should be a point in the buffer.  All
12199 articles before this point will be marked as read.
12200 The number of articles marked as read is returned."
12201   (interactive "P")
12202   (gnus-set-global-variables)
12203   (prog1
12204       (if (or quietly
12205               (not gnus-interactive-catchup) ;Without confirmation?
12206               gnus-expert-user
12207               (gnus-y-or-n-p
12208                (if all
12209                    "Mark absolutely all articles as read? "
12210                  "Mark all unread articles as read? ")))
12211           (if (and not-mark
12212                    (not gnus-newsgroup-adaptive)
12213                    (not gnus-newsgroup-auto-expire))
12214               (progn
12215                 (when all
12216                   (setq gnus-newsgroup-marked nil
12217                         gnus-newsgroup-dormant nil))
12218                 (setq gnus-newsgroup-unreads nil))
12219             ;; We actually mark all articles as canceled, which we
12220             ;; have to do when using auto-expiry or adaptive scoring.
12221             (gnus-summary-show-all-threads)
12222             (if (gnus-summary-first-subject (not all))
12223                 (while (and
12224                         (if to-here (< (point) to-here) t)
12225                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12226                         (gnus-summary-find-next (not all)))))
12227             (unless to-here
12228               (setq gnus-newsgroup-unreads nil))
12229             (gnus-set-mode-line 'summary)))
12230     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12231       (if (and (not to-here) (eq 'nnvirtual (car method)))
12232           (nnvirtual-catchup-group
12233            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12234     (gnus-summary-position-point)))
12235
12236 (defun gnus-summary-catchup-to-here (&optional all)
12237   "Mark all unticked articles before the current one as read.
12238 If ALL is non-nil, also mark ticked and dormant articles as read."
12239   (interactive "P")
12240   (gnus-set-global-variables)
12241   (save-excursion
12242     (let ((beg (point)))
12243       ;; We check that there are unread articles.
12244       (when (or all (gnus-summary-find-prev))
12245         (gnus-summary-catchup all t beg))))
12246   (gnus-summary-position-point))
12247
12248 (defun gnus-summary-catchup-all (&optional quietly)
12249   "Mark all articles in this newsgroup as read."
12250   (interactive "P")
12251   (gnus-set-global-variables)
12252   (gnus-summary-catchup t quietly))
12253
12254 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12255   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12256 If prefix argument ALL is non-nil, all articles are marked as read."
12257   (interactive "P")
12258   (gnus-set-global-variables)
12259   (gnus-summary-catchup all quietly nil 'fast)
12260   ;; Select next newsgroup or exit.
12261   (if (eq gnus-auto-select-next 'quietly)
12262       (gnus-summary-next-group nil)
12263     (gnus-summary-exit)))
12264
12265 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12266   "Mark all articles in this newsgroup as read, and then exit."
12267   (interactive "P")
12268   (gnus-set-global-variables)
12269   (gnus-summary-catchup-and-exit t quietly))
12270
12271 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12272 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12273   "Mark all articles in this group as read and select the next group.
12274 If given a prefix, mark all articles, unread as well as ticked, as
12275 read."
12276   (interactive "P")
12277   (gnus-set-global-variables)
12278   (save-excursion
12279     (gnus-summary-catchup all))
12280   (gnus-summary-next-article t nil nil t))
12281
12282 ;; Thread-based commands.
12283
12284 (defun gnus-summary-articles-in-thread (&optional article)
12285   "Return a list of all articles in the current thread.
12286 If ARTICLE is non-nil, return all articles in the thread that starts
12287 with that article."
12288   (let* ((article (or article (gnus-summary-article-number)))
12289          (data (gnus-data-find-list article))
12290          (top-level (gnus-data-level (car data)))
12291          (top-subject
12292           (cond ((null gnus-thread-operation-ignore-subject)
12293                  (gnus-simplify-subject-re
12294                   (mail-header-subject (gnus-data-header (car data)))))
12295                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12296                  (gnus-simplify-subject-fuzzy
12297                   (mail-header-subject (gnus-data-header (car data)))))
12298                 (t nil)))
12299          articles)
12300     (if (not data)
12301         ()                              ; This article doesn't exist.
12302       (while data
12303         (and (or (not top-subject)
12304                  (string= top-subject
12305                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12306                               (gnus-simplify-subject-fuzzy
12307                                (mail-header-subject
12308                                 (gnus-data-header (car data))))
12309                             (gnus-simplify-subject-re
12310                              (mail-header-subject
12311                               (gnus-data-header (car data)))))))
12312              (setq articles (cons (gnus-data-number (car data)) articles)))
12313         (if (and (setq data (cdr data))
12314                  (> (gnus-data-level (car data)) top-level))
12315             ()
12316           (setq data nil)))
12317       ;; Return the list of articles.
12318       (nreverse articles))))
12319
12320 (defun gnus-summary-rethread-current ()
12321   "Rethread the thread the current article is part of."
12322   (interactive)
12323   (gnus-set-global-variables)
12324   (let* ((gnus-show-threads t)
12325          (article (gnus-summary-article-number))
12326          (id (mail-header-id (gnus-summary-article-header)))
12327          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12328     (unless id
12329       (error "No article on the current line"))
12330     (gnus-rebuild-thread id)
12331     (gnus-summary-goto-subject article)))
12332
12333 (defun gnus-summary-reparent-thread ()
12334   "Make current article child of the marked (or previous) article.
12335
12336 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12337 is non-nil or the Subject: of both articles are the same."
12338   (interactive)
12339   (or (not (gnus-group-read-only-p))
12340       (error "The current newsgroup does not support article editing."))
12341   (or (<= (length gnus-newsgroup-processable) 1)
12342       (error "No more than one article may be marked."))
12343   (save-window-excursion
12344     (let ((gnus-article-buffer " *reparent*")
12345           (current-article (gnus-summary-article-number))
12346           ; first grab the marked article, otherwise one line up.
12347           (parent-article (if (not (null gnus-newsgroup-processable))
12348                               (car gnus-newsgroup-processable)
12349                             (save-excursion
12350                               (if (eq (forward-line -1) 0)
12351                                   (gnus-summary-article-number)
12352                                 (error "Beginning of summary buffer."))))))
12353       (or (not (eq current-article parent-article))
12354           (error "An article may not be self-referential."))
12355       (let ((message-id (mail-header-id 
12356                          (gnus-summary-article-header parent-article))))
12357         (or (and message-id (not (equal message-id "")))
12358             (error "No message-id in desired parent."))
12359         (gnus-summary-select-article t t nil current-article)
12360         (set-buffer gnus-article-buffer)
12361         (setq buffer-read-only nil)
12362         (let ((buf (format "%s" (buffer-string))))
12363           (erase-buffer)
12364           (insert buf))
12365         (goto-char (point-min))
12366         (if (search-forward-regexp "^References: " nil t)
12367             (insert message-id " " )
12368           (insert "References: " message-id "\n"))
12369         (or (gnus-request-replace-article current-article
12370                                           (car gnus-article-current)
12371                                           gnus-article-buffer)
12372             (error "Couldn't replace article."))
12373         (set-buffer gnus-summary-buffer)
12374         (gnus-summary-unmark-all-processable)
12375         (gnus-summary-rethread-current)
12376         (message "Article %d is now the child of article %d."
12377                  current-article parent-article)))))
12378
12379 (defun gnus-summary-toggle-threads (&optional arg)
12380   "Toggle showing conversation threads.
12381 If ARG is positive number, turn showing conversation threads on."
12382   (interactive "P")
12383   (gnus-set-global-variables)
12384   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12385     (setq gnus-show-threads
12386           (if (null arg) (not gnus-show-threads)
12387             (> (prefix-numeric-value arg) 0)))
12388     (gnus-summary-prepare)
12389     (gnus-summary-goto-subject current)
12390     (gnus-summary-position-point)))
12391
12392 (defun gnus-summary-show-all-threads ()
12393   "Show all threads."
12394   (interactive)
12395   (gnus-set-global-variables)
12396   (save-excursion
12397     (let ((buffer-read-only nil))
12398       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12399   (gnus-summary-position-point))
12400
12401 (defun gnus-summary-show-thread ()
12402   "Show thread subtrees.
12403 Returns nil if no thread was there to be shown."
12404   (interactive)
12405   (gnus-set-global-variables)
12406   (let ((buffer-read-only nil)
12407         (orig (point))
12408         ;; first goto end then to beg, to have point at beg after let
12409         (end (progn (end-of-line) (point)))
12410         (beg (progn (beginning-of-line) (point))))
12411     (prog1
12412         ;; Any hidden lines here?
12413         (search-forward "\r" end t)
12414       (subst-char-in-region beg end ?\^M ?\n t)
12415       (goto-char orig)
12416       (gnus-summary-position-point))))
12417
12418 (defun gnus-summary-hide-all-threads ()
12419   "Hide all thread subtrees."
12420   (interactive)
12421   (gnus-set-global-variables)
12422   (save-excursion
12423     (goto-char (point-min))
12424     (gnus-summary-hide-thread)
12425     (while (zerop (gnus-summary-next-thread 1 t))
12426       (gnus-summary-hide-thread)))
12427   (gnus-summary-position-point))
12428
12429 (defun gnus-summary-hide-thread ()
12430   "Hide thread subtrees.
12431 Returns nil if no threads were there to be hidden."
12432   (interactive)
12433   (gnus-set-global-variables)
12434   (let ((buffer-read-only nil)
12435         (start (point))
12436         (article (gnus-summary-article-number)))
12437     (goto-char start)
12438     ;; Go forward until either the buffer ends or the subthread
12439     ;; ends.
12440     (when (and (not (eobp))
12441                (or (zerop (gnus-summary-next-thread 1 t))
12442                    (goto-char (point-max))))
12443       (prog1
12444           (if (and (> (point) start)
12445                    (search-backward "\n" start t))
12446               (progn
12447                 (subst-char-in-region start (point) ?\n ?\^M)
12448                 (gnus-summary-goto-subject article))
12449             (goto-char start)
12450             nil)
12451         ;;(gnus-summary-position-point)
12452         ))))
12453
12454 (defun gnus-summary-go-to-next-thread (&optional previous)
12455   "Go to the same level (or less) next thread.
12456 If PREVIOUS is non-nil, go to previous thread instead.
12457 Return the article number moved to, or nil if moving was impossible."
12458   (let ((level (gnus-summary-thread-level))
12459         (way (if previous -1 1))
12460         (beg (point)))
12461     (forward-line way)
12462     (while (and (not (eobp))
12463                 (< level (gnus-summary-thread-level)))
12464       (forward-line way))
12465     (if (eobp)
12466         (progn
12467           (goto-char beg)
12468           nil)
12469       (setq beg (point))
12470       (prog1
12471           (gnus-summary-article-number)
12472         (goto-char beg)))))
12473
12474 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12475   "Go to the same level (or less) next thread.
12476 If PREVIOUS is non-nil, go to previous thread instead.
12477 Return the article number moved to, or nil if moving was impossible."
12478   (if (and (eq gnus-summary-make-false-root 'dummy)
12479            (gnus-summary-article-intangible-p))
12480       (let ((beg (point)))
12481         (while (and (zerop (forward-line 1))
12482                     (not (gnus-summary-article-intangible-p))
12483                     (not (zerop (save-excursion 
12484                                   (gnus-summary-thread-level))))))
12485         (if (eobp)
12486             (progn
12487               (goto-char beg)
12488               nil)
12489           (point)))
12490     (let* ((level (gnus-summary-thread-level))
12491            (article (gnus-summary-article-number))
12492            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12493            oart)
12494       (while data
12495         (if (<= (gnus-data-level (car data)) level)
12496             (setq oart (gnus-data-number (car data))
12497                   data nil)
12498           (setq data (cdr data))))
12499       (and oart
12500            (gnus-summary-goto-subject oart)))))
12501
12502 (defun gnus-summary-next-thread (n &optional silent)
12503   "Go to the same level next N'th thread.
12504 If N is negative, search backward instead.
12505 Returns the difference between N and the number of skips actually
12506 done.
12507
12508 If SILENT, don't output messages."
12509   (interactive "p")
12510   (gnus-set-global-variables)
12511   (let ((backward (< n 0))
12512         (n (abs n))
12513         old dum int)
12514     (while (and (> n 0)
12515                 (gnus-summary-go-to-next-thread backward))
12516       (decf n))
12517     (unless silent 
12518       (gnus-summary-position-point))
12519     (when (and (not silent) (/= 0 n))
12520       (gnus-message 7 "No more threads"))
12521     n))
12522
12523 (defun gnus-summary-prev-thread (n)
12524   "Go to the same level previous N'th thread.
12525 Returns the difference between N and the number of skips actually
12526 done."
12527   (interactive "p")
12528   (gnus-set-global-variables)
12529   (gnus-summary-next-thread (- n)))
12530
12531 (defun gnus-summary-go-down-thread ()
12532   "Go down one level in the current thread."
12533   (let ((children (gnus-summary-article-children)))
12534     (and children
12535          (gnus-summary-goto-subject (car children)))))
12536
12537 (defun gnus-summary-go-up-thread ()
12538   "Go up one level in the current thread."
12539   (let ((parent (gnus-summary-article-parent)))
12540     (and parent
12541          (gnus-summary-goto-subject parent))))
12542
12543 (defun gnus-summary-down-thread (n)
12544   "Go down thread N steps.
12545 If N is negative, go up instead.
12546 Returns the difference between N and how many steps down that were
12547 taken."
12548   (interactive "p")
12549   (gnus-set-global-variables)
12550   (let ((up (< n 0))
12551         (n (abs n)))
12552     (while (and (> n 0)
12553                 (if up (gnus-summary-go-up-thread)
12554                   (gnus-summary-go-down-thread)))
12555       (setq n (1- n)))
12556     (gnus-summary-position-point)
12557     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12558     n))
12559
12560 (defun gnus-summary-up-thread (n)
12561   "Go up thread N steps.
12562 If N is negative, go up instead.
12563 Returns the difference between N and how many steps down that were
12564 taken."
12565   (interactive "p")
12566   (gnus-set-global-variables)
12567   (gnus-summary-down-thread (- n)))
12568
12569 (defun gnus-summary-top-thread ()
12570   "Go to the top of the thread."
12571   (interactive)
12572   (gnus-set-global-variables)
12573   (while (gnus-summary-go-up-thread))
12574   (gnus-summary-article-number))
12575
12576 (defun gnus-summary-kill-thread (&optional unmark)
12577   "Mark articles under current thread as read.
12578 If the prefix argument is positive, remove any kinds of marks.
12579 If the prefix argument is negative, tick articles instead."
12580   (interactive "P")
12581   (gnus-set-global-variables)
12582   (if unmark
12583       (setq unmark (prefix-numeric-value unmark)))
12584   (let ((articles (gnus-summary-articles-in-thread)))
12585     (save-excursion
12586       ;; Expand the thread.
12587       (gnus-summary-show-thread)
12588       ;; Mark all the articles.
12589       (while articles
12590         (gnus-summary-goto-subject (car articles))
12591         (cond ((null unmark)
12592                (gnus-summary-mark-article-as-read gnus-killed-mark))
12593               ((> unmark 0)
12594                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12595               (t
12596                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12597         (setq articles (cdr articles))))
12598     ;; Hide killed subtrees.
12599     (and (null unmark)
12600          gnus-thread-hide-killed
12601          (gnus-summary-hide-thread))
12602     ;; If marked as read, go to next unread subject.
12603     (if (null unmark)
12604         ;; Go to next unread subject.
12605         (gnus-summary-next-subject 1 t)))
12606   (gnus-set-mode-line 'summary))
12607
12608 ;; Summary sorting commands
12609
12610 (defun gnus-summary-sort-by-number (&optional reverse)
12611   "Sort summary buffer by article number.
12612 Argument REVERSE means reverse order."
12613   (interactive "P")
12614   (gnus-summary-sort 'number reverse))
12615
12616 (defun gnus-summary-sort-by-author (&optional reverse)
12617   "Sort summary buffer by author name alphabetically.
12618 If case-fold-search is non-nil, case of letters is ignored.
12619 Argument REVERSE means reverse order."
12620   (interactive "P")
12621   (gnus-summary-sort 'author reverse))
12622
12623 (defun gnus-summary-sort-by-subject (&optional reverse)
12624   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12625 If case-fold-search is non-nil, case of letters is ignored.
12626 Argument REVERSE means reverse order."
12627   (interactive "P")
12628   (gnus-summary-sort 'subject reverse))
12629
12630 (defun gnus-summary-sort-by-date (&optional reverse)
12631   "Sort summary buffer by date.
12632 Argument REVERSE means reverse order."
12633   (interactive "P")
12634   (gnus-summary-sort 'date reverse))
12635
12636 (defun gnus-summary-sort-by-score (&optional reverse)
12637   "Sort summary buffer by score.
12638 Argument REVERSE means reverse order."
12639   (interactive "P")
12640   (gnus-summary-sort 'score reverse))
12641
12642 (defun gnus-summary-sort (predicate reverse)
12643   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12644   (gnus-set-global-variables)
12645   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12646          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12647          (gnus-thread-sort-functions
12648           (list
12649            (if (not reverse)
12650                thread
12651              `(lambda (t1 t2)
12652                 (,thread t2 t1)))))
12653          (gnus-article-sort-functions
12654           (list
12655            (if (not reverse)
12656                article
12657              `(lambda (t1 t2)
12658                 (,article t2 t1)))))
12659          (buffer-read-only)
12660          (gnus-summary-prepare-hook nil))
12661     ;; We do the sorting by regenerating the threads.
12662     (gnus-summary-prepare)
12663     ;; Hide subthreads if needed.
12664     (when (and gnus-show-threads gnus-thread-hide-subtree)
12665       (gnus-summary-hide-all-threads)))
12666   ;; If in async mode, we send some info to the backend.
12667   (when gnus-newsgroup-async
12668     (gnus-request-asynchronous
12669      gnus-newsgroup-name gnus-newsgroup-data)))
12670
12671 (defun gnus-sortable-date (date)
12672   "Make sortable string by string-lessp from DATE.
12673 Timezone package is used."
12674   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12675          (year (aref date 0))
12676          (month (aref date 1))
12677          (day (aref date 2)))
12678     (timezone-make-sortable-date
12679      year month day
12680      (timezone-make-time-string
12681       (aref date 3) (aref date 4) (aref date 5)))))
12682
12683 ;; Summary saving commands.
12684
12685 (defun gnus-summary-save-article (&optional n not-saved)
12686   "Save the current article using the default saver function.
12687 If N is a positive number, save the N next articles.
12688 If N is a negative number, save the N previous articles.
12689 If N is nil and any articles have been marked with the process mark,
12690 save those articles instead.
12691 The variable `gnus-default-article-saver' specifies the saver function."
12692   (interactive "P")
12693   (gnus-set-global-variables)
12694   (let ((articles (gnus-summary-work-articles n))
12695         file header article)
12696     (while articles
12697       (setq header (gnus-summary-article-header
12698                     (setq article (pop articles))))
12699       (if (not (vectorp header))
12700           ;; This is a pseudo-article.
12701           (if (assq 'name header)
12702               (gnus-copy-file (cdr (assq 'name header)))
12703             (gnus-message 1 "Article %d is unsaveable" article))
12704         ;; This is a real article.
12705         (save-window-excursion
12706           (gnus-summary-select-article t nil nil article))
12707         (unless gnus-save-all-headers
12708           ;; Remove headers accoring to `gnus-saved-headers'.
12709           (let ((gnus-visible-headers
12710                  (or gnus-saved-headers gnus-visible-headers)))
12711             (gnus-article-hide-headers nil t)))
12712         ;; Remove any X-Gnus lines.
12713         (save-excursion
12714           (set-buffer gnus-article-buffer)
12715           (save-restriction
12716             (let ((buffer-read-only nil))
12717               (nnheader-narrow-to-headers)
12718               (while (re-search-forward "^X-Gnus" nil t)
12719                 (gnus-delete-line)))))
12720         (save-window-excursion
12721           (if (not gnus-default-article-saver)
12722               (error "No default saver is defined.")
12723             (setq file (funcall
12724                         gnus-default-article-saver
12725                         (cond
12726                          ((not gnus-prompt-before-saving)
12727                           'default)
12728                          ((eq gnus-prompt-before-saving 'always)
12729                           nil)
12730                          (t file))))))
12731         (gnus-summary-remove-process-mark article)
12732         (unless not-saved
12733           (gnus-summary-set-saved-mark article))))
12734     (gnus-summary-position-point)
12735     n))
12736
12737 (defun gnus-summary-pipe-output (&optional arg)
12738   "Pipe the current article to a subprocess.
12739 If N is a positive number, pipe the N next articles.
12740 If N is a negative number, pipe the N previous articles.
12741 If N is nil and any articles have been marked with the process mark,
12742 pipe those articles instead."
12743   (interactive "P")
12744   (gnus-set-global-variables)
12745   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12746     (gnus-summary-save-article arg t))
12747   (gnus-configure-windows 'pipe))
12748
12749 (defun gnus-summary-save-article-mail (&optional arg)
12750   "Append the current article to an mail file.
12751 If N is a positive number, save the N next articles.
12752 If N is a negative number, save the N previous articles.
12753 If N is nil and any articles have been marked with the process mark,
12754 save those articles instead."
12755   (interactive "P")
12756   (gnus-set-global-variables)
12757   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12758     (gnus-summary-save-article arg)))
12759
12760 (defun gnus-summary-save-article-rmail (&optional arg)
12761   "Append the current article to an rmail file.
12762 If N is a positive number, save the N next articles.
12763 If N is a negative number, save the N previous articles.
12764 If N is nil and any articles have been marked with the process mark,
12765 save those articles instead."
12766   (interactive "P")
12767   (gnus-set-global-variables)
12768   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12769     (gnus-summary-save-article arg)))
12770
12771 (defun gnus-summary-save-article-file (&optional arg)
12772   "Append the current article to a file.
12773 If N is a positive number, save the N next articles.
12774 If N is a negative number, save the N previous articles.
12775 If N is nil and any articles have been marked with the process mark,
12776 save those articles instead."
12777   (interactive "P")
12778   (gnus-set-global-variables)
12779   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12780     (gnus-summary-save-article arg)))
12781
12782 (defun gnus-summary-save-article-body-file (&optional arg)
12783   "Append the current article body to a file.
12784 If N is a positive number, save the N next articles.
12785 If N is a negative number, save the N previous articles.
12786 If N is nil and any articles have been marked with the process mark,
12787 save those articles instead."
12788   (interactive "P")
12789   (gnus-set-global-variables)
12790   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12791     (gnus-summary-save-article arg)))
12792
12793 (defun gnus-get-split-value (methods)
12794   "Return a value based on the split METHODS."
12795   (let (split-name method result match)
12796     (when methods
12797       (save-excursion
12798         (set-buffer gnus-original-article-buffer)
12799         (save-restriction
12800           (nnheader-narrow-to-headers)
12801           (while methods
12802             (goto-char (point-min))
12803             (setq method (pop methods))
12804             (setq match (car method))
12805             (when (cond
12806                    ((stringp match)
12807                     ;; Regular expression.
12808                     (condition-case ()
12809                         (re-search-forward match nil t)
12810                       (error nil)))
12811                    ((gnus-functionp match)
12812                     ;; Function.
12813                     (save-restriction
12814                       (widen)
12815                       (setq result (funcall match gnus-newsgroup-name))))
12816                    ((consp match)
12817                     ;; Form.
12818                     (save-restriction
12819                       (widen)
12820                       (setq result (eval match)))))
12821               (setq split-name (append (cdr method) split-name))
12822               (cond ((stringp result)
12823                      (push result split-name))
12824                     ((consp result)
12825                      (setq split-name (append result split-name)))))))))
12826     split-name))
12827
12828 (defun gnus-read-move-group-name (prompt default articles prefix)
12829   "Read a group name."
12830   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12831          (prom
12832           (format "Where do you want to %s %s? "
12833                   prompt
12834                   (if (> (length articles) 1)
12835                       (format "these %d articles" (length articles))
12836                     "this article")))
12837          (to-newsgroup
12838           (cond
12839            ((null split-name)
12840             (completing-read
12841              (concat prom
12842                      (if default
12843                          (format "(default %s) " default)
12844                        ""))
12845              gnus-active-hashtb nil nil prefix))
12846            ((= 1 (length split-name))
12847             (completing-read prom gnus-active-hashtb
12848                              nil nil (cons (car split-name) 0)))
12849            (t
12850             (completing-read
12851              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12852
12853     (when to-newsgroup
12854       (if (or (string= to-newsgroup "")
12855               (string= to-newsgroup prefix))
12856           (setq to-newsgroup (or default "")))
12857       (or (gnus-active to-newsgroup)
12858           (gnus-activate-group to-newsgroup)
12859           (error "No such group: %s" to-newsgroup)))
12860     to-newsgroup))
12861
12862 (defun gnus-read-save-file-name (prompt default-name)
12863   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12864          (file
12865           ;; Let the split methods have their say.
12866           (cond
12867            ;; No split name was found.
12868            ((null split-name)
12869             (read-file-name
12870              (concat prompt " (default "
12871                      (file-name-nondirectory default-name) ") ")
12872              (file-name-directory default-name)
12873              default-name))
12874            ;; A single split name was found
12875            ((= 1 (length split-name))
12876             (read-file-name
12877              (concat prompt " (default " (car split-name) ") ")
12878              gnus-article-save-directory
12879              (concat gnus-article-save-directory (car split-name))))
12880            ;; A list of splits was found.
12881            (t
12882             (setq split-name (mapcar (lambda (el) (list el))
12883                                      (nreverse split-name)))
12884             (let ((result (completing-read
12885                            (concat prompt " ") split-name nil nil)))
12886               (concat gnus-article-save-directory
12887                       (if (string= result "")
12888                           (caar split-name)
12889                         result)))))))
12890     ;; If we have read a directory, we append the default file name.
12891     (when (file-directory-p file)
12892       (setq file (concat (file-name-as-directory file)
12893                          (file-name-nondirectory default-name))))
12894     ;; Possibly translate some charaters.
12895     (nnheader-translate-file-chars file)))
12896
12897 (defun gnus-article-archive-name (group)
12898   "Return the first instance of an \"Archive-name\" in the current buffer."
12899   (let ((case-fold-search t))
12900     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12901       (match-string 1))))
12902
12903 (defun gnus-summary-save-in-rmail (&optional filename)
12904   "Append this article to Rmail file.
12905 Optional argument FILENAME specifies file name.
12906 Directory to save to is default to `gnus-article-save-directory' which
12907 is initialized from the SAVEDIR environment variable."
12908   (interactive)
12909   (gnus-set-global-variables)
12910   (let ((default-name
12911           (funcall gnus-rmail-save-name gnus-newsgroup-name
12912                    gnus-current-headers gnus-newsgroup-last-rmail)))
12913     (setq filename
12914           (cond ((eq filename 'default)
12915                  default-name)
12916                 (filename filename)
12917                 (t (gnus-read-save-file-name
12918                     "Save in rmail file:" default-name))))
12919     (gnus-make-directory (file-name-directory filename))
12920     (gnus-eval-in-buffer-window
12921      gnus-original-article-buffer
12922      (save-excursion
12923        (save-restriction
12924          (widen)
12925          (gnus-output-to-rmail filename))))
12926     ;; Remember the directory name to save articles
12927     (setq gnus-newsgroup-last-rmail filename)))
12928
12929 (defun gnus-summary-save-in-mail (&optional filename)
12930   "Append this article to Unix mail file.
12931 Optional argument FILENAME specifies file name.
12932 Directory to save to is default to `gnus-article-save-directory' which
12933 is initialized from the SAVEDIR environment variable."
12934   (interactive)
12935   (gnus-set-global-variables)
12936   (let ((default-name
12937           (funcall gnus-mail-save-name gnus-newsgroup-name
12938                    gnus-current-headers gnus-newsgroup-last-mail)))
12939     (setq filename
12940           (cond ((eq filename 'default)
12941                  default-name)
12942                 (filename filename)
12943                 (t (gnus-read-save-file-name
12944                     "Save in Unix mail file:" default-name))))
12945     (setq filename
12946           (expand-file-name filename
12947                             (and default-name
12948                                  (file-name-directory default-name))))
12949     (gnus-make-directory (file-name-directory filename))
12950     (gnus-eval-in-buffer-window
12951      gnus-original-article-buffer
12952      (save-excursion
12953        (save-restriction
12954          (widen)
12955          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12956              (gnus-output-to-rmail filename)
12957            (let ((mail-use-rfc822 t))
12958              (rmail-output filename 1 t t))))))
12959     ;; Remember the directory name to save articles.
12960     (setq gnus-newsgroup-last-mail filename)))
12961
12962 (defun gnus-summary-save-in-file (&optional filename)
12963   "Append this article to file.
12964 Optional argument FILENAME specifies file name.
12965 Directory to save to is default to `gnus-article-save-directory' which
12966 is initialized from the SAVEDIR environment variable."
12967   (interactive)
12968   (gnus-set-global-variables)
12969   (let ((default-name
12970           (funcall gnus-file-save-name gnus-newsgroup-name
12971                    gnus-current-headers gnus-newsgroup-last-file)))
12972     (setq filename
12973           (cond ((eq filename 'default)
12974                  default-name)
12975                 (filename filename)
12976                 (t (gnus-read-save-file-name
12977                     "Save in file:" default-name))))
12978     (gnus-make-directory (file-name-directory filename))
12979     (gnus-eval-in-buffer-window
12980      gnus-original-article-buffer
12981      (save-excursion
12982        (save-restriction
12983          (widen)
12984          (gnus-output-to-file filename))))
12985     ;; Remember the directory name to save articles.
12986     (setq gnus-newsgroup-last-file filename)))
12987
12988 (defun gnus-summary-save-body-in-file (&optional filename)
12989   "Append this article body to a file.
12990 Optional argument FILENAME specifies file name.
12991 The directory to save in defaults to `gnus-article-save-directory' which
12992 is initialized from the SAVEDIR environment variable."
12993   (interactive)
12994   (gnus-set-global-variables)
12995   (let ((default-name
12996           (funcall gnus-file-save-name gnus-newsgroup-name
12997                    gnus-current-headers gnus-newsgroup-last-file)))
12998     (setq filename
12999           (cond ((eq filename 'default)
13000                  default-name)
13001                 (filename filename)
13002                 (t (gnus-read-save-file-name
13003                     "Save body in file:" default-name))))
13004     (gnus-make-directory (file-name-directory filename))
13005     (gnus-eval-in-buffer-window
13006      gnus-article-buffer
13007      (save-excursion
13008        (save-restriction
13009          (widen)
13010          (goto-char (point-min))
13011          (and (search-forward "\n\n" nil t)
13012               (narrow-to-region (point) (point-max)))
13013          (gnus-output-to-file filename))))
13014     ;; Remember the directory name to save articles.
13015     (setq gnus-newsgroup-last-file filename)))
13016
13017 (defun gnus-summary-save-in-pipe (&optional command)
13018   "Pipe this article to subprocess."
13019   (interactive)
13020   (gnus-set-global-variables)
13021   (setq command
13022         (cond ((eq command 'default)
13023                gnus-last-shell-command)
13024               (command command)
13025               (t (read-string "Shell command on article: "
13026                               gnus-last-shell-command))))
13027   (if (string-equal command "")
13028       (setq command gnus-last-shell-command))
13029   (gnus-eval-in-buffer-window
13030    gnus-article-buffer
13031    (save-restriction
13032      (widen)
13033      (shell-command-on-region (point-min) (point-max) command nil)))
13034   (setq gnus-last-shell-command command))
13035
13036 ;; Summary extract commands
13037
13038 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13039   (let ((buffer-read-only nil)
13040         (article (gnus-summary-article-number))
13041         after-article b e)
13042     (or (gnus-summary-goto-subject article)
13043         (error (format "No such article: %d" article)))
13044     (gnus-summary-position-point)
13045     ;; If all commands are to be bunched up on one line, we collect
13046     ;; them here.
13047     (if gnus-view-pseudos-separately
13048         ()
13049       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13050             files action)
13051         (while ps
13052           (setq action (cdr (assq 'action (car ps))))
13053           (setq files (list (cdr (assq 'name (car ps)))))
13054           (while (and ps (cdr ps)
13055                       (string= (or action "1")
13056                                (or (cdr (assq 'action (cadr ps))) "2")))
13057             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13058             (setcdr ps (cddr ps)))
13059           (if (not files)
13060               ()
13061             (if (not (string-match "%s" action))
13062                 (setq files (cons " " files)))
13063             (setq files (cons " " files))
13064             (and (assq 'execute (car ps))
13065                  (setcdr (assq 'execute (car ps))
13066                          (funcall (if (string-match "%s" action)
13067                                       'format 'concat)
13068                                   action
13069                                   (mapconcat (lambda (f) f) files " ")))))
13070           (setq ps (cdr ps)))))
13071     (if (and gnus-view-pseudos (not not-view))
13072         (while pslist
13073           (and (assq 'execute (car pslist))
13074                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13075                                      (eq gnus-view-pseudos 'not-confirm)))
13076           (setq pslist (cdr pslist)))
13077       (save-excursion
13078         (while pslist
13079           (setq after-article (or (cdr (assq 'article (car pslist)))
13080                                   (gnus-summary-article-number)))
13081           (gnus-summary-goto-subject after-article)
13082           (forward-line 1)
13083           (setq b (point))
13084           (insert "    " (file-name-nondirectory
13085                                 (cdr (assq 'name (car pslist))))
13086                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13087           (setq e (point))
13088           (forward-line -1)             ; back to `b'
13089           (add-text-properties
13090            b e (list 'gnus-number gnus-reffed-article-number
13091                      gnus-mouse-face-prop gnus-mouse-face))
13092           (gnus-data-enter
13093            after-article gnus-reffed-article-number
13094            gnus-unread-mark b (car pslist) 0 (- e b))
13095           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13096           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13097           (setq pslist (cdr pslist)))))))
13098
13099 (defun gnus-pseudos< (p1 p2)
13100   (let ((c1 (cdr (assq 'action p1)))
13101         (c2 (cdr (assq 'action p2))))
13102     (and c1 c2 (string< c1 c2))))
13103
13104 (defun gnus-request-pseudo-article (props)
13105   (cond ((assq 'execute props)
13106          (gnus-execute-command (cdr (assq 'execute props)))))
13107   (let ((gnus-current-article (gnus-summary-article-number)))
13108     (run-hooks 'gnus-mark-article-hook)))
13109
13110 (defun gnus-execute-command (command &optional automatic)
13111   (save-excursion
13112     (gnus-article-setup-buffer)
13113     (set-buffer gnus-article-buffer)
13114     (let ((command (if automatic command (read-string "Command: " command)))
13115           (buffer-read-only nil))
13116       (erase-buffer)
13117       (insert "$ " command "\n\n")
13118       (if gnus-view-pseudo-asynchronously
13119           (start-process "gnus-execute" nil "sh" "-c" command)
13120         (call-process "sh" nil t nil "-c" command)))))
13121
13122 (defun gnus-copy-file (file &optional to)
13123   "Copy FILE to TO."
13124   (interactive
13125    (list (read-file-name "Copy file: " default-directory)
13126          (read-file-name "Copy file to: " default-directory)))
13127   (gnus-set-global-variables)
13128   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13129   (and (file-directory-p to)
13130        (setq to (concat (file-name-as-directory to)
13131                         (file-name-nondirectory file))))
13132   (copy-file file to))
13133
13134 ;; Summary kill commands.
13135
13136 (defun gnus-summary-edit-global-kill (article)
13137   "Edit the \"global\" kill file."
13138   (interactive (list (gnus-summary-article-number)))
13139   (gnus-set-global-variables)
13140   (gnus-group-edit-global-kill article))
13141
13142 (defun gnus-summary-edit-local-kill ()
13143   "Edit a local kill file applied to the current newsgroup."
13144   (interactive)
13145   (gnus-set-global-variables)
13146   (setq gnus-current-headers (gnus-summary-article-header))
13147   (gnus-set-global-variables)
13148   (gnus-group-edit-local-kill
13149    (gnus-summary-article-number) gnus-newsgroup-name))
13150
13151 \f
13152 ;;;
13153 ;;; Gnus article mode
13154 ;;;
13155
13156 (put 'gnus-article-mode 'mode-class 'special)
13157
13158 (if gnus-article-mode-map
13159     nil
13160   (setq gnus-article-mode-map (make-keymap))
13161   (suppress-keymap gnus-article-mode-map)
13162
13163   (gnus-define-keys gnus-article-mode-map
13164     " " gnus-article-goto-next-page
13165     "\177" gnus-article-goto-prev-page
13166     [delete] gnus-article-goto-prev-page
13167     "\C-c^" gnus-article-refer-article
13168     "h" gnus-article-show-summary
13169     "s" gnus-article-show-summary
13170     "\C-c\C-m" gnus-article-mail
13171     "?" gnus-article-describe-briefly
13172     gnus-mouse-2 gnus-article-push-button
13173     "\r" gnus-article-press-button
13174     "\t" gnus-article-next-button
13175     "\M-\t" gnus-article-prev-button
13176     "\C-c\C-b" gnus-bug)
13177
13178   (substitute-key-definition
13179    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13180
13181 (defun gnus-article-mode ()
13182   "Major mode for displaying an article.
13183
13184 All normal editing commands are switched off.
13185
13186 The following commands are available:
13187
13188 \\<gnus-article-mode-map>
13189 \\[gnus-article-next-page]\t Scroll the article one page forwards
13190 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13191 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13192 \\[gnus-article-show-summary]\t Display the summary buffer
13193 \\[gnus-article-mail]\t Send a reply to the address near point
13194 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13195 \\[gnus-info-find-node]\t Go to the Gnus info node"
13196   (interactive)
13197   (when (and menu-bar-mode
13198              (gnus-visual-p 'article-menu 'menu))
13199     (gnus-article-make-menu-bar))
13200   (kill-all-local-variables)
13201   (gnus-simplify-mode-line)
13202   (setq mode-name "Article")
13203   (setq major-mode 'gnus-article-mode)
13204   (make-local-variable 'minor-mode-alist)
13205   (or (assq 'gnus-show-mime minor-mode-alist)
13206       (setq minor-mode-alist
13207             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13208   (use-local-map gnus-article-mode-map)
13209   (make-local-variable 'page-delimiter)
13210   (setq page-delimiter gnus-page-delimiter)
13211   (buffer-disable-undo (current-buffer))
13212   (setq buffer-read-only t)             ;Disable modification
13213   (run-hooks 'gnus-article-mode-hook))
13214
13215 (defun gnus-article-setup-buffer ()
13216   "Initialize the article buffer."
13217   (let* ((name (if gnus-single-article-buffer "*Article*"
13218                  (concat "*Article " gnus-newsgroup-name "*")))
13219          (original
13220           (progn (string-match "\\*Article" name)
13221                  (concat " *Original Article"
13222                          (substring name (match-end 0))))))
13223     (setq gnus-article-buffer name)
13224     (setq gnus-original-article-buffer original)
13225     ;; This might be a variable local to the summary buffer.
13226     (unless gnus-single-article-buffer
13227       (save-excursion
13228         (set-buffer gnus-summary-buffer)
13229         (setq gnus-article-buffer name)
13230         (setq gnus-original-article-buffer original)
13231         (gnus-set-global-variables))
13232       (make-local-variable 'gnus-summary-buffer))
13233     ;; Init original article buffer.
13234     (save-excursion
13235       (set-buffer (get-buffer-create gnus-original-article-buffer))
13236       (buffer-disable-undo (current-buffer))
13237       (setq major-mode 'gnus-original-article-mode)
13238       (make-local-variable 'gnus-original-article))
13239     (if (get-buffer name)
13240         (save-excursion
13241           (set-buffer name)
13242           (buffer-disable-undo (current-buffer))
13243           (setq buffer-read-only t)
13244           (gnus-add-current-to-buffer-list)
13245           (or (eq major-mode 'gnus-article-mode)
13246               (gnus-article-mode))
13247           (current-buffer))
13248       (save-excursion
13249         (set-buffer (get-buffer-create name))
13250         (gnus-add-current-to-buffer-list)
13251         (gnus-article-mode)
13252         (current-buffer)))))
13253
13254 ;; Set article window start at LINE, where LINE is the number of lines
13255 ;; from the head of the article.
13256 (defun gnus-article-set-window-start (&optional line)
13257   (set-window-start
13258    (get-buffer-window gnus-article-buffer)
13259    (save-excursion
13260      (set-buffer gnus-article-buffer)
13261      (goto-char (point-min))
13262      (if (not line)
13263          (point-min)
13264        (gnus-message 6 "Moved to bookmark")
13265        (search-forward "\n\n" nil t)
13266        (forward-line line)
13267        (point)))))
13268
13269 (defun gnus-kill-all-overlays ()
13270   "Delete all overlays in the current buffer."
13271   (when (fboundp 'overlay-lists)
13272     (let* ((overlayss (overlay-lists))
13273            (buffer-read-only nil)
13274            (overlays (nconc (car overlayss) (cdr overlayss))))
13275       (while overlays
13276         (delete-overlay (pop overlays))))))
13277
13278 (defun gnus-request-article-this-buffer (article group)
13279   "Get an article and insert it into this buffer."
13280   (let (do-update-line)
13281     (prog1
13282         (save-excursion
13283           (erase-buffer)
13284           (gnus-kill-all-overlays)
13285           (setq group (or group gnus-newsgroup-name))
13286
13287           ;; Open server if it has closed.
13288           (gnus-check-server (gnus-find-method-for-group group))
13289
13290           ;; Using `gnus-request-article' directly will insert the article into
13291           ;; `nntp-server-buffer' - so we'll save some time by not having to
13292           ;; copy it from the server buffer into the article buffer.
13293
13294           ;; We only request an article by message-id when we do not have the
13295           ;; headers for it, so we'll have to get those.
13296           (when (stringp article)
13297             (let ((gnus-override-method gnus-refer-article-method))
13298               (gnus-read-header article)))
13299
13300           ;; If the article number is negative, that means that this article
13301           ;; doesn't belong in this newsgroup (possibly), so we find its
13302           ;; message-id and request it by id instead of number.
13303           (when (and (numberp article)
13304                      gnus-summary-buffer
13305                      (get-buffer gnus-summary-buffer)
13306                      (buffer-name (get-buffer gnus-summary-buffer)))
13307             (save-excursion
13308               (set-buffer gnus-summary-buffer)
13309               (let ((header (gnus-summary-article-header article)))
13310                 (if (< article 0)
13311                     (cond 
13312                      ((memq article gnus-newsgroup-sparse)
13313                       ;; This is a sparse gap article.
13314                       (setq do-update-line article)
13315                       (setq article (mail-header-id header))
13316                       (let ((gnus-override-method gnus-refer-article-method))
13317                         (gnus-read-header article)))
13318                      ((vectorp header)
13319                       ;; It's a real article.
13320                       (setq article (mail-header-id header)))
13321                      (t
13322                       ;; It is an extracted pseudo-article.
13323                       (setq article 'pseudo)
13324                       (gnus-request-pseudo-article header))))
13325                 
13326                 (let ((method (gnus-find-method-for-group 
13327                                gnus-newsgroup-name)))
13328                   (if (not (eq (car method) 'nneething))
13329                       ()
13330                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13331                                        (mail-header-subject header))))
13332                       (if (file-directory-p dir)
13333                           (progn
13334                             (setq article 'nneething)
13335                             (gnus-group-enter-directory dir)))))))))
13336
13337           (cond
13338            ;; We first check `gnus-original-article-buffer'.
13339            ((and (get-buffer gnus-original-article-buffer)
13340                  (save-excursion
13341                    (set-buffer gnus-original-article-buffer)
13342                    (and (equal (car gnus-original-article) group)
13343                         (eq (cdr gnus-original-article) article))))
13344             (insert-buffer-substring gnus-original-article-buffer)
13345             'article)
13346            ;; Check the backlog.
13347            ((and gnus-keep-backlog
13348                  (gnus-backlog-request-article group article (current-buffer)))
13349             'article)
13350            ;; Check the cache.
13351            ((and gnus-use-cache
13352                  (numberp article)
13353                  (gnus-cache-request-article article group))
13354             'article)
13355            ;; Get the article and put into the article buffer.
13356            ((or (stringp article) (numberp article))
13357             (let ((gnus-override-method
13358                    (and (stringp article) gnus-refer-article-method))
13359                   (buffer-read-only nil))
13360               (erase-buffer)
13361               (gnus-kill-all-overlays)
13362               (if (gnus-request-article article group (current-buffer))
13363                   (progn
13364                     (and gnus-keep-backlog
13365                          (numberp article)
13366                          (gnus-backlog-enter-article
13367                           group article (current-buffer)))
13368                     'article))))
13369            ;; It was a pseudo.
13370            (t article)))
13371
13372       ;; Take the article from the original article buffer
13373       ;; and place it in the buffer it's supposed to be in.
13374       (when (and (get-buffer gnus-article-buffer)
13375                  (equal (buffer-name (current-buffer))
13376                         (buffer-name (get-buffer gnus-article-buffer))))
13377         (save-excursion
13378           (if (get-buffer gnus-original-article-buffer)
13379               (set-buffer (get-buffer gnus-original-article-buffer))
13380             (set-buffer (get-buffer-create gnus-original-article-buffer))
13381             (buffer-disable-undo (current-buffer))
13382             (setq major-mode 'gnus-original-article-mode)
13383             (setq buffer-read-only t)
13384             (gnus-add-current-to-buffer-list))
13385           (let (buffer-read-only)
13386             (erase-buffer)
13387             (insert-buffer-substring gnus-article-buffer))
13388           (setq gnus-original-article (cons group article))))
13389     
13390       ;; Update sparse articles.
13391       (when do-update-line
13392         (save-excursion
13393           (set-buffer gnus-summary-buffer)
13394           (gnus-summary-update-article do-update-line)
13395           (gnus-summary-goto-subject do-update-line)
13396           (set-window-point (get-buffer-window (current-buffer) t)
13397                             (point)))))))
13398
13399 (defun gnus-read-header (id &optional header)
13400   "Read the headers of article ID and enter them into the Gnus system."
13401   (let ((group gnus-newsgroup-name)
13402         where)
13403     ;; First we check to see whether the header in question is already
13404     ;; fetched.
13405     (if (stringp id)
13406         ;; This is a Message-ID.
13407         (setq header (or header (gnus-id-to-header id)))
13408       ;; This is an article number.
13409       (setq header (or header (gnus-summary-article-header id))))
13410     (if (and header
13411              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13412         ;; We have found the header.
13413         header
13414       ;; We have to really fetch the header to this article.
13415       (when (setq where
13416                   (if (gnus-check-backend-function 'request-head group)
13417                       (gnus-request-head id group)
13418                     (gnus-request-article id group)))
13419         (save-excursion
13420           (set-buffer nntp-server-buffer)
13421           (and (search-forward "\n\n" nil t)
13422                (delete-region (1- (point)) (point-max)))
13423           (goto-char (point-max))
13424           (insert ".\n")
13425           (goto-char (point-min))
13426           (insert "211 ")
13427           (princ (cond
13428                   ((numberp id) id)
13429                   ((cdr where) (cdr where))
13430                   (header (mail-header-number header))
13431                   (t gnus-reffed-article-number))
13432                  (current-buffer))
13433           (insert " Article retrieved.\n"))
13434         ;(when (and header
13435         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13436         ;  (setcar (gnus-id-to-thread id) nil))
13437         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13438             ()                          ; Malformed head.
13439           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13440             (if (and (stringp id)
13441                      (not (string= (gnus-group-real-name group)
13442                                    (car where))))
13443                 ;; If we fetched by Message-ID and the article came
13444                 ;; from a different group, we fudge some bogus article
13445                 ;; numbers for this article.
13446                 (mail-header-set-number header gnus-reffed-article-number))
13447             (decf gnus-reffed-article-number)
13448             (push header gnus-newsgroup-headers)
13449             (setq gnus-current-headers header)
13450             (push (mail-header-number header) gnus-newsgroup-limit))
13451           header)))))
13452
13453 (defun gnus-article-prepare (article &optional all-headers header)
13454   "Prepare ARTICLE in article mode buffer.
13455 ARTICLE should either be an article number or a Message-ID.
13456 If ARTICLE is an id, HEADER should be the article headers.
13457 If ALL-HEADERS is non-nil, no headers are hidden."
13458   (save-excursion
13459     ;; Make sure we start in a summary buffer.
13460     (unless (eq major-mode 'gnus-summary-mode)
13461       (set-buffer gnus-summary-buffer))
13462     (setq gnus-summary-buffer (current-buffer))
13463     ;; Make sure the connection to the server is alive.
13464     (unless (gnus-server-opened
13465              (gnus-find-method-for-group gnus-newsgroup-name))
13466       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13467       (gnus-request-group gnus-newsgroup-name t))
13468     (let* ((article (if header (mail-header-number header) article))
13469            (summary-buffer (current-buffer))
13470            (internal-hook gnus-article-internal-prepare-hook)
13471            (group gnus-newsgroup-name)
13472            result)
13473       (save-excursion
13474         (gnus-article-setup-buffer)
13475         (set-buffer gnus-article-buffer)
13476         ;; Deactivate active regions.
13477         (when (and (boundp 'transient-mark-mode)
13478                    transient-mark-mode)
13479           (setq mark-active nil))
13480         (if (not (setq result (let ((buffer-read-only nil))
13481                                 (gnus-request-article-this-buffer
13482                                  article group))))
13483             ;; There is no such article.
13484             (save-excursion
13485               (when (and (numberp article)
13486                          (not (memq article gnus-newsgroup-sparse)))
13487                 (setq gnus-article-current
13488                       (cons gnus-newsgroup-name article))
13489                 (set-buffer gnus-summary-buffer)
13490                 (setq gnus-current-article article)
13491                 (gnus-summary-mark-article article gnus-canceled-mark))
13492               (unless (memq article gnus-newsgroup-sparse)
13493                 (gnus-message
13494                  1 "No such article (may have expired or been canceled)")
13495                 (ding)
13496                 nil))
13497           (if (or (eq result 'pseudo) (eq result 'nneething))
13498               (progn
13499                 (save-excursion
13500                   (set-buffer summary-buffer)
13501                   (setq gnus-last-article gnus-current-article
13502                         gnus-newsgroup-history (cons gnus-current-article
13503                                                      gnus-newsgroup-history)
13504                         gnus-current-article 0
13505                         gnus-current-headers nil
13506                         gnus-article-current nil)
13507                   (if (eq result 'nneething)
13508                       (gnus-configure-windows 'summary)
13509                     (gnus-configure-windows 'article))
13510                   (gnus-set-global-variables))
13511                 (gnus-set-mode-line 'article))
13512             ;; The result from the `request' was an actual article -
13513             ;; or at least some text that is now displayed in the
13514             ;; article buffer.
13515             (if (and (numberp article)
13516                      (not (eq article gnus-current-article)))
13517                 ;; Seems like a new article has been selected.
13518                 ;; `gnus-current-article' must be an article number.
13519                 (save-excursion
13520                   (set-buffer summary-buffer)
13521                   (setq gnus-last-article gnus-current-article
13522                         gnus-newsgroup-history (cons gnus-current-article
13523                                                      gnus-newsgroup-history)
13524                         gnus-current-article article
13525                         gnus-current-headers
13526                         (gnus-summary-article-header gnus-current-article)
13527                         gnus-article-current
13528                         (cons gnus-newsgroup-name gnus-current-article))
13529                   (unless (vectorp gnus-current-headers)
13530                     (setq gnus-current-headers nil))
13531                   (gnus-summary-show-thread)
13532                   (run-hooks 'gnus-mark-article-hook)
13533                   (gnus-set-mode-line 'summary)
13534                   (and (gnus-visual-p 'article-highlight 'highlight)
13535                        (run-hooks 'gnus-visual-mark-article-hook))
13536                   ;; Set the global newsgroup variables here.
13537                   ;; Suggested by Jim Sisolak
13538                   ;; <sisolak@trans4.neep.wisc.edu>.
13539                   (gnus-set-global-variables)
13540                   (setq gnus-have-all-headers
13541                         (or all-headers gnus-show-all-headers))
13542                   (and gnus-use-cache
13543                        (vectorp (gnus-summary-article-header article))
13544                        (gnus-cache-possibly-enter-article
13545                         group article
13546                         (gnus-summary-article-header article)
13547                         (memq article gnus-newsgroup-marked)
13548                         (memq article gnus-newsgroup-dormant)
13549                         (memq article gnus-newsgroup-unreads)))))
13550             ;; Hooks for getting information from the article.
13551             ;; This hook must be called before being narrowed.
13552             (let (buffer-read-only)
13553               (run-hooks 'internal-hook)
13554               (run-hooks 'gnus-article-prepare-hook)
13555               ;; Decode MIME message.
13556               (if gnus-show-mime
13557                   (if (or (not gnus-strict-mime)
13558                           (gnus-fetch-field "Mime-Version"))
13559                       (funcall gnus-show-mime-method)
13560                     (funcall gnus-decode-encoded-word-method)))
13561               ;; Perform the article display hooks.
13562               (run-hooks 'gnus-article-display-hook))
13563             ;; Do page break.
13564             (goto-char (point-min))
13565             (and gnus-break-pages (gnus-narrow-to-page))
13566             (gnus-set-mode-line 'article)
13567             (gnus-configure-windows 'article)
13568             (goto-char (point-min))
13569             t))))))
13570
13571 (defun gnus-article-show-all-headers ()
13572   "Show all article headers in article mode buffer."
13573   (save-excursion
13574     (gnus-article-setup-buffer)
13575     (set-buffer gnus-article-buffer)
13576     (let ((buffer-read-only nil))
13577       (gnus-unhide-text (point-min) (point-max)))))
13578
13579 (defun gnus-article-hide-headers-if-wanted ()
13580   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13581 Provided for backwards compatibility."
13582   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13583       gnus-inhibit-hiding
13584       (gnus-article-hide-headers)))
13585
13586 (defun gnus-article-hide-headers (&optional arg delete)
13587   "Toggle whether to hide unwanted headers and possibly sort them as well.
13588 If given a negative prefix, always show; if given a positive prefix,
13589 always hide."
13590   (interactive "P")
13591   (unless (gnus-article-check-hidden-text 'headers arg)
13592     ;; This function might be inhibited.
13593     (unless gnus-inhibit-hiding
13594       (save-excursion
13595         (set-buffer gnus-article-buffer)
13596         (save-restriction
13597           (let ((buffer-read-only nil)
13598                 (props (nconc (list 'gnus-type 'headers)
13599                               gnus-hidden-properties))
13600                 (ignored (when (not (stringp gnus-visible-headers))
13601                            (cond ((stringp gnus-ignored-headers)
13602                                   gnus-ignored-headers)
13603                                  ((listp gnus-ignored-headers)
13604                                   (mapconcat 'identity gnus-ignored-headers
13605                                              "\\|")))))
13606                 (visible
13607                  (cond ((stringp gnus-visible-headers)
13608                         gnus-visible-headers)
13609                        ((and gnus-visible-headers
13610                              (listp gnus-visible-headers))
13611                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13612                 want-list beg)
13613             ;; First we narrow to just the headers.
13614             (widen)
13615             (goto-char (point-min))
13616             ;; Hide any "From " lines at the beginning of (mail) articles.
13617             (while (looking-at "From ")
13618               (forward-line 1))
13619             (unless (bobp)
13620               (gnus-hide-text (point-min) (point) props))
13621             ;; Then treat the rest of the header lines.
13622             (narrow-to-region
13623              (point)
13624              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13625             ;; Then we use the two regular expressions
13626             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13627             ;; select which header lines is to remain visible in the
13628             ;; article buffer.
13629             (goto-char (point-min))
13630             (while (re-search-forward "^[^ \t]*:" nil t)
13631               (beginning-of-line)
13632               ;; We add the headers we want to keep to a list and delete
13633               ;; them from the buffer.
13634               (if (or (and visible (looking-at visible))
13635                       (and ignored (not (looking-at ignored))))
13636                   (progn
13637                     (push (buffer-substring
13638                            (setq beg (point))
13639                            (progn
13640                              (forward-line 1)
13641                              ;; Be sure to get multi-line headers...
13642                              (re-search-forward "^[^ \t]*:" nil t)
13643                              (beginning-of-line)
13644                              (point)))
13645                           want-list)
13646                     (delete-region beg (point)))
13647                 (forward-line 1)))
13648             ;; Sort the headers that we want to display.
13649             (setq want-list (sort want-list 'gnus-article-header-less))
13650             (goto-char (point-min))
13651             (while want-list
13652               (insert (pop want-list)))
13653             ;; We make the unwanted headers invisible.
13654             (if delete
13655                 (delete-region (point-min) (point-max))
13656               ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13657               (gnus-hide-text-type (point) (point-max) 'headers))))))))
13658
13659 (defsubst gnus-article-header-rank (header)
13660   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13661   (let ((list gnus-sorted-header-list)
13662         (i 0))
13663     (while list
13664       (when (string-match (car list) header)
13665         (setq list nil))
13666       (setq list (cdr list))
13667       (incf i))
13668     i))
13669
13670 (defun gnus-article-header-less (h1 h2)
13671   "Say whether string H1 is \"less\" than string H2."
13672   (< (gnus-article-header-rank h1)
13673      (gnus-article-header-rank h2)))
13674
13675 (defun gnus-article-hide-boring-headers (&optional arg)
13676   "Toggle hiding of headers that aren't very interesting.
13677 If given a negative prefix, always show; if given a positive prefix,
13678 always hide."
13679   (interactive "P")
13680   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13681     (save-excursion
13682       (set-buffer gnus-article-buffer)
13683       (save-restriction
13684         (let ((buffer-read-only nil)
13685               (list gnus-boring-article-headers)
13686               (inhibit-point-motion-hooks t)
13687               elem)
13688           (nnheader-narrow-to-headers)
13689           (while list
13690             (setq elem (pop list))
13691             (goto-char (point-min))
13692             (cond
13693              ;; Hide empty headers.
13694              ((eq elem 'empty)
13695               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13696                 (forward-line -1)
13697                 (gnus-hide-text-type
13698                  (progn (beginning-of-line) (point))
13699                  (progn 
13700                    (end-of-line)
13701                    (if (re-search-forward "^[^ \t]" nil t)
13702                        (match-beginning 0)
13703                      (point-max)))
13704                  'boring-headers)))
13705              ;; Hide boring Newsgroups header.
13706              ((eq elem 'newsgroups)
13707               (when (equal (mail-fetch-field "newsgroups")
13708                            (gnus-group-real-name gnus-newsgroup-name))
13709                 (gnus-article-hide-header "newsgroups")))
13710              ((eq elem 'followup-to)
13711               (when (equal (mail-fetch-field "followup-to")
13712                            (mail-fetch-field "newsgroups"))
13713                 (gnus-article-hide-header "followup-to")))
13714              ((eq elem 'reply-to)
13715               (let ((from (mail-fetch-field "from"))
13716                     (reply-to (mail-fetch-field "reply-to")))
13717                 (when (and
13718                        from reply-to
13719                        (equal 
13720                         (nth 1 (funcall gnus-extract-address-components from))
13721                         (nth 1 (funcall gnus-extract-address-components
13722                                         reply-to))))
13723                   (gnus-article-hide-header "reply-to"))))
13724              ((eq elem 'date)
13725               (let ((date (mail-fetch-field "date")))
13726                 (when (and date
13727                            (< (gnus-days-between date (current-time-string))
13728                               4))
13729                   (gnus-article-hide-header "date")))))))))))
13730
13731 (defun gnus-article-hide-header (header)
13732   (save-excursion
13733     (goto-char (point-min))
13734     (when (re-search-forward (concat "^" header ":") nil t)
13735       (gnus-hide-text-type
13736        (progn (beginning-of-line) (point))
13737        (progn 
13738          (end-of-line)
13739          (if (re-search-forward "^[^ \t]" nil t)
13740              (match-beginning 0)
13741            (point-max)))
13742        'boring-headers))))
13743
13744 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13745 (defun gnus-article-treat-overstrike ()
13746   "Translate overstrikes into bold text."
13747   (interactive)
13748   (save-excursion
13749     (set-buffer gnus-article-buffer)
13750     (let ((buffer-read-only nil))
13751       (while (search-forward "\b" nil t)
13752         (let ((next (following-char))
13753               (previous (char-after (- (point) 2))))
13754           (cond ((eq next previous)
13755                  (put-text-property (- (point) 2) (point) 'invisible t)
13756                  (put-text-property (point) (1+ (point)) 'face 'bold))
13757                 ((eq next ?_)
13758                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13759                  (put-text-property
13760                   (- (point) 2) (1- (point)) 'face 'underline))
13761                 ((eq previous ?_)
13762                  (put-text-property (- (point) 2) (point) 'invisible t)
13763                  (put-text-property
13764                   (point) (1+ (point))  'face 'underline))))))))
13765
13766 (defun gnus-article-word-wrap ()
13767   "Format too long lines."
13768   (interactive)
13769   (save-excursion
13770     (set-buffer gnus-article-buffer)
13771     (let ((buffer-read-only nil))
13772       (widen)
13773       (goto-char (point-min))
13774       (search-forward "\n\n" nil t)
13775       (end-of-line 1)
13776       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13777             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13778             (adaptive-fill-mode t))
13779         (while (not (eobp))
13780           (and (>= (current-column) (min fill-column (window-width)))
13781                (/= (preceding-char) ?:)
13782                (fill-paragraph nil))
13783           (end-of-line 2))))))
13784
13785 (defun gnus-article-remove-cr ()
13786   "Remove carriage returns from an article."
13787   (interactive)
13788   (save-excursion
13789     (set-buffer gnus-article-buffer)
13790     (let ((buffer-read-only nil))
13791       (goto-char (point-min))
13792       (while (search-forward "\r" nil t)
13793         (replace-match "" t t)))))
13794
13795 (defun gnus-article-remove-trailing-blank-lines ()
13796   "Remove all trailing blank lines from the article."
13797   (interactive)
13798   (save-excursion
13799     (set-buffer gnus-article-buffer)
13800     (let ((buffer-read-only nil))
13801       (goto-char (point-max))
13802       (delete-region
13803        (point)
13804        (progn
13805          (while (looking-at "^[ \t]*$")
13806            (forward-line -1))
13807          (forward-line 1)
13808          (point))))))
13809
13810 (defun gnus-article-display-x-face (&optional force)
13811   "Look for an X-Face header and display it if present."
13812   (interactive (list 'force))
13813   (save-excursion
13814     (set-buffer gnus-article-buffer)
13815     ;; Delete the old process, if any.
13816     (when (process-status "gnus-x-face")
13817       (delete-process "gnus-x-face"))
13818     (let ((inhibit-point-motion-hooks t)
13819           (case-fold-search nil)
13820           from)
13821       (save-restriction
13822         (nnheader-narrow-to-headers)
13823         (setq from (mail-fetch-field "from"))
13824         (goto-char (point-min))
13825         (when (and gnus-article-x-face-command
13826                    (or force
13827                        ;; Check whether this face is censored.
13828                        (not gnus-article-x-face-too-ugly)
13829                        (and gnus-article-x-face-too-ugly from
13830                             (not (string-match gnus-article-x-face-too-ugly
13831                                                from))))
13832                    ;; Has to be present.
13833                    (re-search-forward "^X-Face: " nil t))
13834           ;; We now have the area of the buffer where the X-Face is stored.
13835           (let ((beg (point))
13836                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13837             ;; We display the face.
13838             (if (symbolp gnus-article-x-face-command)
13839                 ;; The command is a lisp function, so we call it.
13840                 (if (gnus-functionp gnus-article-x-face-command)
13841                     (funcall gnus-article-x-face-command beg end)
13842                   (error "%s is not a function" gnus-article-x-face-command))
13843               ;; The command is a string, so we interpret the command
13844               ;; as a, well, command, and fork it off.
13845               (let ((process-connection-type nil))
13846                 (process-kill-without-query
13847                  (start-process
13848                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13849                 (process-send-region "gnus-x-face" beg end)
13850                 (process-send-eof "gnus-x-face")))))))))
13851
13852 (defun gnus-headers-decode-quoted-printable ()
13853   "Hack to remove QP encoding from headers."
13854   (let ((case-fold-search t)
13855         (inhibit-point-motion-hooks t)
13856         string)
13857     (goto-char (point-min))
13858     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13859       (setq string (match-string 1))
13860       (narrow-to-region (match-beginning 0) (match-end 0))
13861       (delete-region (point-min) (point-max))
13862       (insert string)
13863       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13864       (subst-char-in-region (point-min) (point-max) ?_ ? )
13865       (widen)
13866       (goto-char (point-min)))))
13867
13868 (defun gnus-article-de-quoted-unreadable (&optional force)
13869   "Do a naive translation of a quoted-printable-encoded article.
13870 This is in no way, shape or form meant as a replacement for real MIME
13871 processing, but is simply a stop-gap measure until MIME support is
13872 written.
13873 If FORCE, decode the article whether it is marked as quoted-printable
13874 or not."
13875   (interactive (list 'force))
13876   (save-excursion
13877     (set-buffer gnus-article-buffer)
13878     (let ((case-fold-search t)
13879           (buffer-read-only nil)
13880           (type (gnus-fetch-field "content-transfer-encoding")))
13881       (when (or force
13882                 (and type (string-match "quoted-printable" (downcase type))))
13883         (gnus-headers-decode-quoted-printable)
13884         (goto-char (point-min))
13885         (search-forward "\n\n" nil 'move)
13886         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13887
13888 (defun gnus-mime-decode-quoted-printable (from to)
13889   "Decode Quoted-Printable in the region between FROM and TO."
13890   (goto-char from)
13891   (while (search-forward "=" to t)
13892     (cond ((eq (following-char) ?\n)
13893            (delete-char -1)
13894            (delete-char 1))
13895           ((looking-at "[0-9A-F][0-9A-F]")
13896            (delete-char -1)
13897            (insert (hexl-hex-string-to-integer
13898                     (buffer-substring (point) (+ 2 (point)))))
13899            (delete-char 2))
13900           ((looking-at "=")
13901            (delete-char 1))
13902           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13903
13904 (defun gnus-article-hide-pgp (&optional arg)
13905   "Toggle hiding of any PGP headers and signatures in the current article.
13906 If given a negative prefix, always show; if given a positive prefix,
13907 always hide."
13908   (interactive "P")
13909   (unless (gnus-article-check-hidden-text 'pgp arg)
13910     (save-excursion
13911       (set-buffer gnus-article-buffer)
13912       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13913             buffer-read-only beg end)
13914         (widen)
13915         (goto-char (point-min))
13916         ;; Hide the "header".
13917         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13918              (gnus-hide-text (match-beginning 0) (match-end 0) props))
13919         (setq beg (point))
13920         ;; Hide the actual signature.
13921         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13922              (setq end (1+ (match-beginning 0)))
13923              (gnus-hide-text
13924               end
13925               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13926                   (match-end 0)
13927                 ;; Perhaps we shouldn't hide to the end of the buffer
13928                 ;; if there is no end to the signature?
13929                 (point-max))
13930               props))
13931         ;; Hide "- " PGP quotation markers.
13932         (when (and beg end)
13933           (narrow-to-region beg end)
13934           (goto-char (point-min))
13935           (while (re-search-forward "^- " nil t)
13936             (gnus-hide-text (match-beginning 0) (match-end 0) props))
13937           (widen))))))
13938
13939 (defun gnus-article-hide-signature (&optional arg)
13940   "Hide the signature in the current article.
13941 If given a negative prefix, always show; if given a positive prefix,
13942 always hide."
13943   (interactive "P")
13944   (unless (gnus-article-check-hidden-text 'signature arg)
13945     (save-excursion
13946       (set-buffer gnus-article-buffer)
13947       (save-restriction
13948         (let ((buffer-read-only nil))
13949           (when (gnus-narrow-to-signature)
13950             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
13951
13952 (defun gnus-article-strip-leading-blank-lines ()
13953   "Remove all blank lines from the beginning of the article."
13954   (interactive)
13955   (save-excursion
13956     (set-buffer gnus-article-buffer)
13957     (let (buffer-read-only)
13958       (goto-char (point-min))
13959       (when (search-forward "\n\n" nil t)
13960         (while (looking-at "[ \t]$")
13961           (gnus-delete-line))))))
13962
13963 (defun gnus-narrow-to-signature ()
13964   "Narrow to the signature."
13965   (widen)
13966   (goto-char (point-max))
13967   (when (re-search-backward gnus-signature-separator nil t)
13968     (forward-line 1)
13969     (when (or (null gnus-signature-limit)
13970               (and (numberp gnus-signature-limit)
13971                    (< (- (point-max) (point)) gnus-signature-limit))
13972               (and (gnus-functionp gnus-signature-limit)
13973                    (funcall gnus-signature-limit))
13974               (and (stringp gnus-signature-limit)
13975                    (not (re-search-forward gnus-signature-limit nil t))))
13976       (narrow-to-region (point) (point-max))
13977       t)))
13978
13979 (defun gnus-article-check-hidden-text (type arg)
13980   "Return nil if hiding is necessary."
13981   (save-excursion
13982     (set-buffer gnus-article-buffer)
13983     (let ((hide (gnus-article-hidden-text-p type)))
13984       (cond ((or (and (null arg) (eq hide 'hidden))
13985                  (and arg (< 0 (prefix-numeric-value arg))))
13986              (gnus-article-show-hidden-text type))
13987             ((eq hide 'shown)
13988              (gnus-article-show-hidden-text type t))
13989             (t nil)))))
13990
13991 (defun gnus-article-hidden-text-p (type)
13992   "Say whether the current buffer contains hidden text of type TYPE."
13993   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
13994     (when pos
13995       (if (get-text-property pos 'invisible)
13996           'hidden
13997         'shown))))
13998
13999 (defun gnus-article-hide (&optional arg force)
14000   "Hide all the gruft in the current article.
14001 This means that PGP stuff, signatures, cited text and (some)
14002 headers will be hidden.
14003 If given a prefix, show the hidden text instead."
14004   (interactive (list current-prefix-arg 'force))
14005   (gnus-article-hide-headers arg)
14006   (gnus-article-hide-pgp arg)
14007   (gnus-article-hide-citation-maybe arg force)
14008   (gnus-article-hide-signature arg))
14009
14010 (defun gnus-article-show-hidden-text (type &optional hide)
14011   "Show all hidden text of type TYPE.
14012 If HIDE, hide the text instead."
14013   (save-excursion
14014     (set-buffer gnus-article-buffer)
14015     (let ((buffer-read-only nil)
14016           (inhibit-point-motion-hooks t)
14017           (beg (point-min)))
14018       (while (gnus-goto-char (text-property-any
14019                               beg (point-max) 'gnus-type type))
14020         (setq beg (point))
14021         (forward-char)
14022         (if hide
14023             (gnus-hide-text beg (point) gnus-hidden-properties)
14024           (gnus-unhide-text beg (point)))
14025         (setq beg (point)))
14026       t)))
14027
14028 (defvar gnus-article-time-units
14029   `((year . ,(* 365.25 24 60 60))
14030     (week . ,(* 7 24 60 60))
14031     (day . ,(* 24 60 60))
14032     (hour . ,(* 60 60))
14033     (minute . 60)
14034     (second . 1))
14035   "Mapping from time units to seconds.")
14036
14037 (defun gnus-article-date-ut (&optional type highlight)
14038   "Convert DATE date to universal time in the current article.
14039 If TYPE is `local', convert to local time; if it is `lapsed', output
14040 how much time has lapsed since DATE."
14041   (interactive (list 'ut t))
14042   (let* ((header (or gnus-current-headers
14043                      (gnus-summary-article-header) ""))
14044          (date (and (vectorp header) (mail-header-date header)))
14045          (date-regexp "^Date: \\|^X-Sent: ")
14046          (now (current-time))
14047          (inhibit-point-motion-hooks t)
14048          bface eface)
14049     (when (and date (not (string= date "")))
14050       (save-excursion
14051         (set-buffer gnus-article-buffer)
14052         (save-restriction
14053           (nnheader-narrow-to-headers)
14054           (let ((buffer-read-only nil))
14055             ;; Delete any old Date headers.
14056             (if (re-search-forward date-regexp nil t)
14057                 (progn
14058                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14059                         eface (get-text-property (gnus-point-at-eol) 'face))
14060                   (message-remove-header date-regexp t)
14061                   (beginning-of-line))
14062               (goto-char (point-max)))
14063             (insert
14064              (cond
14065               ;; Convert to the local timezone.  We have to slap a
14066               ;; `condition-case' round the calls to the timezone
14067               ;; functions since they aren't particularly resistant to
14068               ;; buggy dates.
14069               ((eq type 'local)
14070                (concat "Date: " (condition-case ()
14071                                     (timezone-make-date-arpa-standard date)
14072                                   (error date))
14073                        "\n"))
14074               ;; Convert to Universal Time.
14075               ((eq type 'ut)
14076                (concat "Date: "
14077                        (condition-case ()
14078                            (timezone-make-date-arpa-standard date nil "UT")
14079                          (error date))
14080                        "\n"))
14081               ;; Get the original date from the article.
14082               ((eq type 'original)
14083                (concat "Date: " date "\n"))
14084               ;; Do an X-Sent lapsed format.
14085               ((eq type 'lapsed)
14086                ;; If the date is seriously mangled, the timezone
14087                ;; functions are liable to bug out, so we condition-case
14088                ;; the entire thing.
14089                (let* ((real-time
14090                        (condition-case ()
14091                            (gnus-time-minus
14092                             (gnus-encode-date
14093                              (timezone-make-date-arpa-standard
14094                               (current-time-string now)
14095                               (current-time-zone now) "UT"))
14096                             (gnus-encode-date
14097                              (timezone-make-date-arpa-standard
14098                               date nil "UT")))
14099                          (error '(0 0))))
14100                       (real-sec (+ (* (float (car real-time)) 65536)
14101                                    (cadr real-time)))
14102                       (sec (abs real-sec))
14103                       num prev)
14104                  (if (zerop sec)
14105                      "X-Sent: Now\n"
14106                    (concat
14107                     "X-Sent: "
14108                     ;; This is a bit convoluted, but basically we go
14109                     ;; through the time units for years, weeks, etc,
14110                     ;; and divide things to see whether that results
14111                     ;; in positive answers.
14112                     (mapconcat
14113                      (lambda (unit)
14114                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14115                            ;; The (remaining) seconds are too few to
14116                            ;; be divided into this time unit.
14117                            ""
14118                          ;; It's big enough, so we output it.
14119                          (setq sec (- sec (* num (cdr unit))))
14120                          (prog1
14121                              (concat (if prev ", " "") (int-to-string
14122                                                         (floor num))
14123                                      " " (symbol-name (car unit))
14124                                      (if (> num 1) "s" ""))
14125                            (setq prev t))))
14126                      gnus-article-time-units "")
14127                     ;; If dates are odd, then it might appear like the
14128                     ;; article was sent in the future.
14129                     (if (> real-sec 0)
14130                         " ago\n"
14131                       " in the future\n")))))
14132               (t
14133                (error "Unknown conversion type: %s" type)))))
14134           ;; Do highlighting.
14135           (beginning-of-line)
14136           (when (and highlight (gnus-visual-p 'article-highlight 'highlight)
14137                      (looking-at "\\([^:]\\): *\\(.*\\)$"))
14138             (put-text-property (match-beginning 1) (match-end 1)
14139                                'face bface)
14140             (put-text-property (match-beginning 2) (match-end 2)
14141                                'face eface)))))))
14142
14143 (defun gnus-article-date-local (&optional highlight)
14144   "Convert the current article date to the local timezone."
14145   (interactive (list t))
14146   (gnus-article-date-ut 'local highlight))
14147
14148 (defun gnus-article-date-original (&optional highlight)
14149   "Convert the current article date to what it was originally.
14150 This is only useful if you have used some other date conversion
14151 function and want to see what the date was before converting."
14152   (interactive (list t))
14153   (gnus-article-date-ut 'original highlight))
14154
14155 (defun gnus-article-date-lapsed (&optional highlight)
14156   "Convert the current article date to time lapsed since it was sent."
14157   (interactive (list t))
14158   (gnus-article-date-ut 'lapsed highlight))
14159
14160 (defun gnus-article-maybe-highlight ()
14161   "Do some article highlighting if `gnus-visual' is non-nil."
14162   (if (gnus-visual-p 'article-highlight 'highlight)
14163       (gnus-article-highlight-some)))
14164
14165 ;; Article savers.
14166
14167 (defun gnus-output-to-rmail (file-name)
14168   "Append the current article to an Rmail file named FILE-NAME."
14169   (require 'rmail)
14170   ;; Most of these codes are borrowed from rmailout.el.
14171   (setq file-name (expand-file-name file-name))
14172   (setq rmail-default-rmail-file file-name)
14173   (let ((artbuf (current-buffer))
14174         (tmpbuf (get-buffer-create " *Gnus-output*")))
14175     (save-excursion
14176       (or (get-file-buffer file-name)
14177           (file-exists-p file-name)
14178           (if (gnus-yes-or-no-p
14179                (concat "\"" file-name "\" does not exist, create it? "))
14180               (let ((file-buffer (create-file-buffer file-name)))
14181                 (save-excursion
14182                   (set-buffer file-buffer)
14183                   (rmail-insert-rmail-file-header)
14184                   (let ((require-final-newline nil))
14185                     (write-region (point-min) (point-max) file-name t 1)))
14186                 (kill-buffer file-buffer))
14187             (error "Output file does not exist")))
14188       (set-buffer tmpbuf)
14189       (buffer-disable-undo (current-buffer))
14190       (erase-buffer)
14191       (insert-buffer-substring artbuf)
14192       (gnus-convert-article-to-rmail)
14193       ;; Decide whether to append to a file or to an Emacs buffer.
14194       (let ((outbuf (get-file-buffer file-name)))
14195         (if (not outbuf)
14196             (append-to-file (point-min) (point-max) file-name)
14197           ;; File has been visited, in buffer OUTBUF.
14198           (set-buffer outbuf)
14199           (let ((buffer-read-only nil)
14200                 (msg (and (boundp 'rmail-current-message)
14201                           (symbol-value 'rmail-current-message))))
14202             ;; If MSG is non-nil, buffer is in RMAIL mode.
14203             (if msg
14204                 (progn (widen)
14205                        (narrow-to-region (point-max) (point-max))))
14206             (insert-buffer-substring tmpbuf)
14207             (if msg
14208                 (progn
14209                   (goto-char (point-min))
14210                   (widen)
14211                   (search-backward "\^_")
14212                   (narrow-to-region (point) (point-max))
14213                   (goto-char (1+ (point-min)))
14214                   (rmail-count-new-messages t)
14215                   (rmail-show-message msg)))))))
14216     (kill-buffer tmpbuf)))
14217
14218 (defun gnus-output-to-file (file-name)
14219   "Append the current article to a file named FILE-NAME."
14220   (setq file-name (expand-file-name file-name))
14221   (let ((artbuf (current-buffer))
14222         (tmpbuf (get-buffer-create " *Gnus-output*")))
14223     (save-excursion
14224       (set-buffer tmpbuf)
14225       (buffer-disable-undo (current-buffer))
14226       (erase-buffer)
14227       (insert-buffer-substring artbuf)
14228       ;; Append newline at end of the buffer as separator, and then
14229       ;; save it to file.
14230       (goto-char (point-max))
14231       (insert "\n")
14232       (append-to-file (point-min) (point-max) file-name))
14233     (kill-buffer tmpbuf)))
14234
14235 (defun gnus-convert-article-to-rmail ()
14236   "Convert article in current buffer to Rmail message format."
14237   (let ((buffer-read-only nil))
14238     ;; Convert article directly into Babyl format.
14239     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14240     (goto-char (point-min))
14241     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14242     (while (search-forward "\n\^_" nil t) ;single char
14243       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14244     (goto-char (point-max))
14245     (insert "\^_")))
14246
14247 (defun gnus-narrow-to-page (&optional arg)
14248   "Narrow the article buffer to a page.
14249 If given a numerical ARG, move forward ARG pages."
14250   (interactive "P")
14251   (setq arg (if arg (prefix-numeric-value arg) 0))
14252   (save-excursion
14253     (set-buffer gnus-article-buffer)
14254     (goto-char (point-min))
14255     (widen)
14256     (when (gnus-visual-p 'page-marker)
14257       (let ((buffer-read-only nil))
14258         (gnus-remove-text-with-property 'gnus-prev)
14259         (gnus-remove-text-with-property 'gnus-next)))
14260     (when
14261         (cond ((< arg 0)
14262                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14263               ((> arg 0)
14264                (re-search-forward page-delimiter nil 'move arg)))
14265       (goto-char (match-end 0)))
14266     (narrow-to-region
14267      (point)
14268      (if (re-search-forward page-delimiter nil 'move)
14269          (match-beginning 0)
14270        (point)))
14271     (when (and (gnus-visual-p 'page-marker)
14272                (not (= (point-min) 1)))
14273       (save-excursion
14274         (goto-char (point-min))
14275         (gnus-insert-prev-page-button)))
14276     (when (and (gnus-visual-p 'page-marker)
14277                (not (= (1- (point-max)) (buffer-size))))
14278       (save-excursion
14279         (goto-char (point-max))
14280         (gnus-insert-next-page-button)))))
14281
14282 ;; Article mode commands
14283
14284 (defun gnus-article-goto-next-page ()
14285   "Show the next page of the article."
14286   (interactive)
14287   (when (gnus-article-next-page)
14288     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14289
14290 (defun gnus-article-goto-prev-page ()
14291   "Show the next page of the article."
14292   (interactive)
14293   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14294     (gnus-article-prev-page nil)))
14295
14296 (defun gnus-article-next-page (&optional lines)
14297   "Show the next page of the current article.
14298 If end of article, return non-nil.  Otherwise return nil.
14299 Argument LINES specifies lines to be scrolled up."
14300   (interactive "p")
14301   (move-to-window-line -1)
14302   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14303   (if (save-excursion
14304         (end-of-line)
14305         (and (pos-visible-in-window-p)  ;Not continuation line.
14306              (eobp)))
14307       ;; Nothing in this page.
14308       (if (or (not gnus-break-pages)
14309               (save-excursion
14310                 (save-restriction
14311                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14312           t                             ;Nothing more.
14313         (gnus-narrow-to-page 1)         ;Go to next page.
14314         nil)
14315     ;; More in this page.
14316     (condition-case ()
14317         (scroll-up lines)
14318       (end-of-buffer
14319        ;; Long lines may cause an end-of-buffer error.
14320        (goto-char (point-max))))
14321     (move-to-window-line 0)
14322     nil))
14323
14324 (defun gnus-article-prev-page (&optional lines)
14325   "Show previous page of current article.
14326 Argument LINES specifies lines to be scrolled down."
14327   (interactive "p")
14328   (move-to-window-line 0)
14329   (if (and gnus-break-pages
14330            (bobp)
14331            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14332       (progn
14333         (gnus-narrow-to-page -1)        ;Go to previous page.
14334         (goto-char (point-max))
14335         (recenter -1))
14336     (prog1
14337         (condition-case ()
14338             (scroll-down lines)
14339           (error nil))
14340       (move-to-window-line 0))))
14341
14342 (defun gnus-article-refer-article ()
14343   "Read article specified by message-id around point."
14344   (interactive)
14345   (let ((point (point)))
14346     (search-forward ">" nil t)          ;Move point to end of "<....>".
14347     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14348         (let ((message-id (match-string 1)))
14349           (goto-char point)
14350           (set-buffer gnus-summary-buffer)
14351           (gnus-summary-refer-article message-id))
14352       (goto-char (point))
14353       (error "No references around point"))))
14354
14355 (defun gnus-article-show-summary ()
14356   "Reconfigure windows to show summary buffer."
14357   (interactive)
14358   (gnus-configure-windows 'article)
14359   (gnus-summary-goto-subject gnus-current-article))
14360
14361 (defun gnus-article-describe-briefly ()
14362   "Describe article mode commands briefly."
14363   (interactive)
14364   (gnus-message 6
14365                 (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")))
14366
14367 (defun gnus-article-summary-command ()
14368   "Execute the last keystroke in the summary buffer."
14369   (interactive)
14370   (let ((obuf (current-buffer))
14371         (owin (current-window-configuration))
14372         func)
14373     (switch-to-buffer gnus-summary-buffer 'norecord)
14374     (setq func (lookup-key (current-local-map) (this-command-keys)))
14375     (call-interactively func)
14376     (set-buffer obuf)
14377     (set-window-configuration owin)
14378     (set-window-point (get-buffer-window (current-buffer)) (point))))
14379
14380 (defun gnus-article-summary-command-nosave ()
14381   "Execute the last keystroke in the summary buffer."
14382   (interactive)
14383   (let (func)
14384     (pop-to-buffer gnus-summary-buffer 'norecord)
14385     (setq func (lookup-key (current-local-map) (this-command-keys)))
14386     (call-interactively func)))
14387
14388 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14389   "Read a summary buffer key sequence and execute it from the article buffer."
14390   (interactive "P")
14391   (let ((nosaves
14392          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14393            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14394            "=" "^" "\M-^"))
14395         keys)
14396     (save-excursion
14397       (set-buffer gnus-summary-buffer)
14398       (push (or key last-command-event) unread-command-events)
14399       (setq keys (read-key-sequence nil)))
14400     (message "")
14401
14402     (if (member keys nosaves)
14403         (let (func)
14404           (pop-to-buffer gnus-summary-buffer 'norecord)
14405           (if (setq func (lookup-key (current-local-map) keys))
14406               (call-interactively func)
14407             (ding)))
14408       (let ((obuf (current-buffer))
14409             (owin (current-window-configuration))
14410             (opoint (point))
14411             func in-buffer)
14412         (if not-restore-window
14413             (pop-to-buffer gnus-summary-buffer 'norecord)
14414           (switch-to-buffer gnus-summary-buffer 'norecord))
14415         (setq in-buffer (current-buffer))
14416         (if (setq func (lookup-key (current-local-map) keys))
14417             (call-interactively func)
14418           (ding))
14419         (when (eq in-buffer (current-buffer))
14420           (set-buffer obuf)
14421           (unless not-restore-window
14422             (set-window-configuration owin))
14423           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14424
14425 \f
14426 ;;;
14427 ;;; Kill file handling.
14428 ;;;
14429
14430 ;;;###autoload
14431 (defalias 'gnus-batch-kill 'gnus-batch-score)
14432 ;;;###autoload
14433 (defun gnus-batch-score ()
14434   "Run batched scoring.
14435 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14436 Newsgroups is a list of strings in Bnews format.  If you want to score
14437 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14438 score the alt hierarchy, you'd say \"!alt.all\"."
14439   (interactive)
14440   (let* ((yes-and-no
14441           (gnus-newsrc-parse-options
14442            (apply (function concat)
14443                   (mapcar (lambda (g) (concat g " "))
14444                           command-line-args-left))))
14445          (gnus-expert-user t)
14446          (nnmail-spool-file nil)
14447          (gnus-use-dribble-file nil)
14448          (yes (car yes-and-no))
14449          (no (cdr yes-and-no))
14450          group newsrc entry
14451          ;; Disable verbose message.
14452          gnus-novice-user gnus-large-newsgroup)
14453     ;; Eat all arguments.
14454     (setq command-line-args-left nil)
14455     ;; Start Gnus.
14456     (gnus)
14457     ;; Apply kills to specified newsgroups in command line arguments.
14458     (setq newsrc (cdr gnus-newsrc-alist))
14459     (while newsrc
14460       (setq group (caar newsrc))
14461       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14462       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14463                (and (car entry)
14464                     (or (eq (car entry) t)
14465                         (not (zerop (car entry)))))
14466                (if yes (string-match yes group) t)
14467                (or (null no) (not (string-match no group))))
14468           (progn
14469             (gnus-summary-read-group group nil t nil t)
14470             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14471                  (gnus-summary-exit))))
14472       (setq newsrc (cdr newsrc)))
14473     ;; Exit Emacs.
14474     (switch-to-buffer gnus-group-buffer)
14475     (gnus-group-save-newsrc)))
14476
14477 (defun gnus-apply-kill-file ()
14478   "Apply a kill file to the current newsgroup.
14479 Returns the number of articles marked as read."
14480   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14481           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14482       (gnus-apply-kill-file-internal)
14483     0))
14484
14485 (defun gnus-kill-save-kill-buffer ()
14486   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14487     (when (get-file-buffer file)
14488       (save-excursion
14489         (set-buffer (get-file-buffer file))
14490         (and (buffer-modified-p) (save-buffer))
14491         (kill-buffer (current-buffer))))))
14492
14493 (defvar gnus-kill-file-name "KILL"
14494   "Suffix of the kill files.")
14495
14496 (defun gnus-newsgroup-kill-file (newsgroup)
14497   "Return the name of a kill file name for NEWSGROUP.
14498 If NEWSGROUP is nil, return the global kill file name instead."
14499   (cond 
14500    ;; The global KILL file is placed at top of the directory.
14501    ((or (null newsgroup)
14502         (string-equal newsgroup ""))
14503     (expand-file-name gnus-kill-file-name
14504                       (or gnus-kill-files-directory "~/News")))
14505    ;; Append ".KILL" to newsgroup name.
14506    ((gnus-use-long-file-name 'not-kill)
14507     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14508                               "." gnus-kill-file-name)
14509                       (or gnus-kill-files-directory "~/News")))
14510    ;; Place "KILL" under the hierarchical directory.
14511    (t
14512     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14513                               "/" gnus-kill-file-name)
14514                       (or gnus-kill-files-directory "~/News")))))
14515
14516 \f
14517 ;;;
14518 ;;; Dribble file
14519 ;;;
14520
14521 (defvar gnus-dribble-ignore nil)
14522 (defvar gnus-dribble-eval-file nil)
14523
14524 (defun gnus-dribble-file-name ()
14525   "Return the dribble file for the current .newsrc."
14526   (concat
14527    (if gnus-dribble-directory
14528        (concat (file-name-as-directory gnus-dribble-directory)
14529                (file-name-nondirectory gnus-current-startup-file))
14530      gnus-current-startup-file)
14531    "-dribble"))
14532
14533 (defun gnus-dribble-enter (string)
14534   "Enter STRING into the dribble buffer."
14535   (if (and (not gnus-dribble-ignore)
14536            gnus-dribble-buffer
14537            (buffer-name gnus-dribble-buffer))
14538       (let ((obuf (current-buffer)))
14539         (set-buffer gnus-dribble-buffer)
14540         (insert string "\n")
14541         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14542         (set-buffer obuf))))
14543
14544 (defun gnus-dribble-read-file ()
14545   "Read the dribble file from disk."
14546   (let ((dribble-file (gnus-dribble-file-name)))
14547     (save-excursion
14548       (set-buffer (setq gnus-dribble-buffer
14549                         (get-buffer-create
14550                          (file-name-nondirectory dribble-file))))
14551       (gnus-add-current-to-buffer-list)
14552       (erase-buffer)
14553       (setq buffer-file-name dribble-file)
14554       (auto-save-mode t)
14555       (buffer-disable-undo (current-buffer))
14556       (bury-buffer (current-buffer))
14557       (set-buffer-modified-p nil)
14558       (let ((auto (make-auto-save-file-name))
14559             (gnus-dribble-ignore t))
14560         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14561           ;; Load whichever file is newest -- the auto save file
14562           ;; or the "real" file.
14563           (if (file-newer-than-file-p auto dribble-file)
14564               (insert-file-contents auto)
14565             (insert-file-contents dribble-file))
14566           (unless (zerop (buffer-size))
14567             (set-buffer-modified-p t))
14568           ;; Set the file modes to reflect the .newsrc file modes.
14569           (save-buffer)
14570           (when (file-exists-p gnus-current-startup-file)
14571             (set-file-modes dribble-file
14572                             (file-modes gnus-current-startup-file)))
14573           ;; Possibly eval the file later.
14574           (when (gnus-y-or-n-p
14575                  "Auto-save file exists.  Do you want to read it? ")
14576             (setq gnus-dribble-eval-file t)))))))
14577
14578 (defun gnus-dribble-eval-file ()
14579   (if (not gnus-dribble-eval-file)
14580       ()
14581     (setq gnus-dribble-eval-file nil)
14582     (save-excursion
14583       (let ((gnus-dribble-ignore t))
14584         (set-buffer gnus-dribble-buffer)
14585         (eval-buffer (current-buffer))))))
14586
14587 (defun gnus-dribble-delete-file ()
14588   (if (file-exists-p (gnus-dribble-file-name))
14589       (delete-file (gnus-dribble-file-name)))
14590   (if gnus-dribble-buffer
14591       (save-excursion
14592         (set-buffer gnus-dribble-buffer)
14593         (let ((auto (make-auto-save-file-name)))
14594           (if (file-exists-p auto)
14595               (delete-file auto))
14596           (erase-buffer)
14597           (set-buffer-modified-p nil)))))
14598
14599 (defun gnus-dribble-save ()
14600   (if (and gnus-dribble-buffer
14601            (buffer-name gnus-dribble-buffer))
14602       (save-excursion
14603         (set-buffer gnus-dribble-buffer)
14604         (save-buffer))))
14605
14606 (defun gnus-dribble-clear ()
14607   (save-excursion
14608     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14609         (progn
14610           (set-buffer gnus-dribble-buffer)
14611           (erase-buffer)
14612           (set-buffer-modified-p nil)
14613           (setq buffer-saved-size (buffer-size))))))
14614
14615 \f
14616 ;;;
14617 ;;; Server Communication
14618 ;;;
14619
14620 (defun gnus-start-news-server (&optional confirm)
14621   "Open a method for getting news.
14622 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14623   (let (how)
14624     (if gnus-current-select-method
14625         ;; Stream is already opened.
14626         nil
14627       ;; Open NNTP server.
14628       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14629       (if confirm
14630           (progn
14631             ;; Read server name with completion.
14632             (setq gnus-nntp-server
14633                   (completing-read "NNTP server: "
14634                                    (mapcar (lambda (server) (list server))
14635                                            (cons (list gnus-nntp-server)
14636                                                  gnus-secondary-servers))
14637                                    nil nil gnus-nntp-server))))
14638
14639       (if (and gnus-nntp-server
14640                (stringp gnus-nntp-server)
14641                (not (string= gnus-nntp-server "")))
14642           (setq gnus-select-method
14643                 (cond ((or (string= gnus-nntp-server "")
14644                            (string= gnus-nntp-server "::"))
14645                        (list 'nnspool (system-name)))
14646                       ((string-match "^:" gnus-nntp-server)
14647                        (list 'nnmh gnus-nntp-server
14648                              (list 'nnmh-directory
14649                                    (file-name-as-directory
14650                                     (expand-file-name
14651                                      (concat "~/" (substring
14652                                                    gnus-nntp-server 1)))))
14653                              (list 'nnmh-get-new-mail nil)))
14654                       (t
14655                        (list 'nntp gnus-nntp-server)))))
14656
14657       (setq how (car gnus-select-method))
14658       (cond ((eq how 'nnspool)
14659              (require 'nnspool)
14660              (gnus-message 5 "Looking up local news spool..."))
14661             ((eq how 'nnmh)
14662              (require 'nnmh)
14663              (gnus-message 5 "Looking up mh spool..."))
14664             (t
14665              (require 'nntp)))
14666       (setq gnus-current-select-method gnus-select-method)
14667       (run-hooks 'gnus-open-server-hook)
14668       (or
14669        ;; gnus-open-server-hook might have opened it
14670        (gnus-server-opened gnus-select-method)
14671        (gnus-open-server gnus-select-method)
14672        (gnus-y-or-n-p
14673         (format
14674          "%s (%s) open error: '%s'.     Continue? "
14675          (car gnus-select-method) (cadr gnus-select-method)
14676          (gnus-status-message gnus-select-method)))
14677        (progn
14678          (gnus-message 1 "Couldn't open server on %s"
14679                        (nth 1 gnus-select-method))
14680          (ding)
14681          nil)))))
14682
14683 (defun gnus-check-group (group)
14684   "Try to make sure that the server where GROUP exists is alive."
14685   (let ((method (gnus-find-method-for-group group)))
14686     (or (gnus-server-opened method)
14687         (gnus-open-server method))))
14688
14689 (defun gnus-check-server (&optional method silent)
14690   "Check whether the connection to METHOD is down.
14691 If METHOD is nil, use `gnus-select-method'.
14692 If it is down, start it up (again)."
14693   (let ((method (or method gnus-select-method)))
14694     ;; Transform virtual server names into select methods.
14695     (when (stringp method)
14696       (setq method (gnus-server-to-method method)))
14697     (if (gnus-server-opened method)
14698         ;; The stream is already opened.
14699         t
14700       ;; Open the server.
14701       (unless silent
14702         (gnus-message 5 "Opening %s server%s..." (car method)
14703                       (if (equal (nth 1 method) "") ""
14704                         (format " on %s" (nth 1 method)))))
14705       (run-hooks 'gnus-open-server-hook)
14706       (prog1
14707           (gnus-open-server method)
14708         (unless silent
14709           (message ""))))))
14710
14711 (defun gnus-get-function (method function)
14712   "Return a function symbol based on METHOD and FUNCTION."
14713   ;; Translate server names into methods.
14714   (unless method
14715     (error "Attempted use of a nil select method"))
14716   (when (stringp method)
14717     (setq method (gnus-server-to-method method)))
14718   (let ((func (intern (format "%s-%s" (car method) function))))
14719     ;; If the functions isn't bound, we require the backend in
14720     ;; question.
14721     (unless (fboundp func)
14722       (require (car method))
14723       (unless (fboundp func)
14724         ;; This backend doesn't implement this function.
14725         (error "No such function: %s" func)))
14726     func))
14727
14728 \f
14729 ;;;
14730 ;;; Interface functions to the backends.
14731 ;;;
14732
14733 (defun gnus-open-server (method)
14734   "Open a connection to METHOD."
14735   (when (stringp method)
14736     (setq method (gnus-server-to-method method)))
14737   (let ((elem (assoc method gnus-opened-servers)))
14738     ;; If this method was previously denied, we just return nil.
14739     (if (eq (nth 1 elem) 'denied)
14740         (progn
14741           (gnus-message 1 "Denied server")
14742           nil)
14743       ;; Open the server.
14744       (let ((result
14745              (funcall (gnus-get-function method 'open-server)
14746                       (nth 1 method) (nthcdr 2 method))))
14747         ;; If this hasn't been opened before, we add it to the list.
14748         (unless elem
14749           (setq elem (list method nil)
14750                 gnus-opened-servers (cons elem gnus-opened-servers)))
14751         ;; Set the status of this server.
14752         (setcar (cdr elem) (if result 'ok 'denied))
14753         ;; Return the result from the "open" call.
14754         result))))
14755
14756 (defun gnus-close-server (method)
14757   "Close the connection to METHOD."
14758   (when (stringp method)
14759     (setq method (gnus-server-to-method method)))
14760   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14761
14762 (defun gnus-request-list (method)
14763   "Request the active file from METHOD."
14764   (when (stringp method)
14765     (setq method (gnus-server-to-method method)))
14766   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14767
14768 (defun gnus-request-list-newsgroups (method)
14769   "Request the newsgroups file from METHOD."
14770   (when (stringp method)
14771     (setq method (gnus-server-to-method method)))
14772   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14773
14774 (defun gnus-request-newgroups (date method)
14775   "Request all new groups since DATE from METHOD."
14776   (when (stringp method)
14777     (setq method (gnus-server-to-method method)))
14778   (funcall (gnus-get-function method 'request-newgroups)
14779            date (nth 1 method)))
14780
14781 (defun gnus-server-opened (method)
14782   "Check whether a connection to METHOD has been opened."
14783   (when (stringp method)
14784     (setq method (gnus-server-to-method method)))
14785   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14786
14787 (defun gnus-status-message (method)
14788   "Return the status message from METHOD.
14789 If METHOD is a string, it is interpreted as a group name.   The method
14790 this group uses will be queried."
14791   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14792                   method)))
14793     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14794
14795 (defun gnus-request-group (group &optional dont-check method)
14796   "Request GROUP.  If DONT-CHECK, no information is required."
14797   (let ((method (or method (gnus-find-method-for-group group))))
14798     (when (stringp method)
14799       (setq method (gnus-server-to-method method)))
14800     (funcall (gnus-get-function method 'request-group)
14801              (gnus-group-real-name group) (nth 1 method) dont-check)))
14802
14803 (defun gnus-request-asynchronous (group &optional articles)
14804   "Request that GROUP behave asynchronously.
14805 ARTICLES is the `data' of the group."
14806   (let ((method (gnus-find-method-for-group group)))
14807     (funcall (gnus-get-function method 'request-asynchronous)
14808              (gnus-group-real-name group) (nth 1 method) articles)))
14809
14810 (defun gnus-list-active-group (group)
14811   "Request active information on GROUP."
14812   (let ((method (gnus-find-method-for-group group))
14813         (func 'list-active-group))
14814     (when (gnus-check-backend-function func group)
14815       (funcall (gnus-get-function method func)
14816                (gnus-group-real-name group) (nth 1 method)))))
14817
14818 (defun gnus-request-group-description (group)
14819   "Request a description of GROUP."
14820   (let ((method (gnus-find-method-for-group group))
14821         (func 'request-group-description))
14822     (when (gnus-check-backend-function func group)
14823       (funcall (gnus-get-function method func)
14824                (gnus-group-real-name group) (nth 1 method)))))
14825
14826 (defun gnus-close-group (group)
14827   "Request the GROUP be closed."
14828   (let ((method (gnus-find-method-for-group group)))
14829     (funcall (gnus-get-function method 'close-group)
14830              (gnus-group-real-name group) (nth 1 method))))
14831
14832 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14833   "Request headers for ARTICLES in GROUP.
14834 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14835   (let ((method (gnus-find-method-for-group group)))
14836     (if (and gnus-use-cache (numberp (car articles)))
14837         (gnus-cache-retrieve-headers articles group fetch-old)
14838       (funcall (gnus-get-function method 'retrieve-headers)
14839                articles (gnus-group-real-name group) (nth 1 method)
14840                fetch-old))))
14841
14842 (defun gnus-retrieve-groups (groups method)
14843   "Request active information on GROUPS from METHOD."
14844   (when (stringp method)
14845     (setq method (gnus-server-to-method method)))
14846   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14847
14848 (defun gnus-request-type (group &optional article)
14849   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14850   (let ((method (gnus-find-method-for-group group)))
14851     (if (not (gnus-check-backend-function 'request-type (car method)))
14852         'unknown
14853       (funcall (gnus-get-function method 'request-type)
14854                (gnus-group-real-name group) article))))
14855
14856 (defun gnus-request-update-mark (group article mark)
14857   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14858   (let ((method (gnus-find-method-for-group group)))
14859     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14860         mark
14861       (funcall (gnus-get-function method 'request-update-mark)
14862                (gnus-group-real-name group) article mark))))
14863
14864 (defun gnus-request-article (article group &optional buffer)
14865   "Request the ARTICLE in GROUP.
14866 ARTICLE can either be an article number or an article Message-ID.
14867 If BUFFER, insert the article in that group."
14868   (let ((method (gnus-find-method-for-group group)))
14869     (funcall (gnus-get-function method 'request-article)
14870              article (gnus-group-real-name group) (nth 1 method) buffer)))
14871
14872 (defun gnus-request-head (article group)
14873   "Request the head of ARTICLE in GROUP."
14874   (let ((method (gnus-find-method-for-group group)))
14875     (funcall (gnus-get-function method 'request-head)
14876              article (gnus-group-real-name group) (nth 1 method))))
14877
14878 (defun gnus-request-body (article group)
14879   "Request the body of ARTICLE in GROUP."
14880   (let ((method (gnus-find-method-for-group group)))
14881     (funcall (gnus-get-function method 'request-body)
14882              article (gnus-group-real-name group) (nth 1 method))))
14883
14884 (defun gnus-request-post (method)
14885   "Post the current buffer using METHOD."
14886   (when (stringp method)
14887     (setq method (gnus-server-to-method method)))
14888   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14889
14890 (defun gnus-request-scan (group method)
14891   "Request a SCAN being performed in GROUP from METHOD.
14892 If GROUP is nil, all groups on METHOD are scanned."
14893   (let ((method (if group (gnus-find-method-for-group group) method)))
14894     (funcall (gnus-get-function method 'request-scan)
14895              (and group (gnus-group-real-name group)) (nth 1 method))))
14896
14897 (defsubst gnus-request-update-info (info method)
14898   "Request that METHOD update INFO."
14899   (when (stringp method)
14900     (setq method (gnus-server-to-method method)))
14901   (when (gnus-check-backend-function 'request-update-info (car method))
14902     (funcall (gnus-get-function method 'request-update-info)
14903              (gnus-group-real-name (gnus-info-group info))
14904              info (nth 1 method))))
14905
14906 (defun gnus-request-expire-articles (articles group &optional force)
14907   (let ((method (gnus-find-method-for-group group)))
14908     (funcall (gnus-get-function method 'request-expire-articles)
14909              articles (gnus-group-real-name group) (nth 1 method)
14910              force)))
14911
14912 (defun gnus-request-move-article
14913   (article group server accept-function &optional last)
14914   (let ((method (gnus-find-method-for-group group)))
14915     (funcall (gnus-get-function method 'request-move-article)
14916              article (gnus-group-real-name group)
14917              (nth 1 method) accept-function last)))
14918
14919 (defun gnus-request-accept-article (group method &optional last)
14920   ;; Make sure there's a newline at the end of the article.
14921   (when (stringp method)
14922     (setq method (gnus-server-to-method method)))
14923   (when (and (not method)
14924              (stringp group))
14925     (setq method (gnus-find-method-for-group group)))
14926   (goto-char (point-max))
14927   (unless (bolp)
14928     (insert "\n"))
14929   (let ((func (car (or method (gnus-find-method-for-group group)))))
14930     (funcall (intern (format "%s-request-accept-article" func))
14931              (if (stringp group) (gnus-group-real-name group) group)
14932              (cadr method)
14933              last)))
14934
14935 (defun gnus-request-replace-article (article group buffer)
14936   (let ((func (car (gnus-find-method-for-group group))))
14937     (funcall (intern (format "%s-request-replace-article" func))
14938              article (gnus-group-real-name group) buffer)))
14939
14940 (defun gnus-request-associate-buffer (group)
14941   (let ((method (gnus-find-method-for-group group)))
14942     (funcall (gnus-get-function method 'request-associate-buffer)
14943              (gnus-group-real-name group))))
14944
14945 (defun gnus-request-restore-buffer (article group)
14946   "Request a new buffer restored to the state of ARTICLE."
14947   (let ((method (gnus-find-method-for-group group)))
14948     (funcall (gnus-get-function method 'request-restore-buffer)
14949              article (gnus-group-real-name group) (nth 1 method))))
14950
14951 (defun gnus-request-create-group (group &optional method)
14952   (when (stringp method)
14953     (setq method (gnus-server-to-method method)))
14954   (let ((method (or method (gnus-find-method-for-group group))))
14955     (funcall (gnus-get-function method 'request-create-group)
14956              (gnus-group-real-name group) (nth 1 method))))
14957
14958 (defun gnus-request-delete-group (group &optional force)
14959   (let ((method (gnus-find-method-for-group group)))
14960     (funcall (gnus-get-function method 'request-delete-group)
14961              (gnus-group-real-name group) force (nth 1 method))))
14962
14963 (defun gnus-request-rename-group (group new-name)
14964   (let ((method (gnus-find-method-for-group group)))
14965     (funcall (gnus-get-function method 'request-rename-group)
14966              (gnus-group-real-name group)
14967              (gnus-group-real-name new-name) (nth 1 method))))
14968
14969 (defun gnus-member-of-valid (symbol group)
14970   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14971   (memq symbol (assoc
14972                 (symbol-name (car (gnus-find-method-for-group group)))
14973                 gnus-valid-select-methods)))
14974
14975 (defun gnus-method-option-p (method option)
14976   "Return non-nil if select METHOD has OPTION as a parameter."
14977   (when (stringp method)
14978     (setq method (gnus-server-to-method method)))
14979   (memq option (assoc (format "%s" (car method))
14980                       gnus-valid-select-methods)))
14981
14982 (defun gnus-server-extend-method (group method)
14983   ;; This function "extends" a virtual server.  If the server is
14984   ;; "hello", and the select method is ("hello" (my-var "something"))
14985   ;; in the group "alt.alt", this will result in a new virtual server
14986   ;; called "hello+alt.alt".
14987   (let ((entry
14988          (gnus-copy-sequence
14989           (if (equal (car method) "native") gnus-select-method
14990             (cdr (assoc (car method) gnus-server-alist))))))
14991     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14992     (nconc entry (cdr method))))
14993
14994 (defun gnus-find-method-for-group (group &optional info)
14995   "Find the select method that GROUP uses."
14996   (or gnus-override-method
14997       (and (not group)
14998            gnus-select-method)
14999       (let ((info (or info (gnus-get-info group)))
15000             method)
15001         (if (or (not info)
15002                 (not (setq method (gnus-info-method info)))
15003                 (equal method "native"))
15004             gnus-select-method
15005           (setq method
15006                 (cond ((stringp method)
15007                        (gnus-server-to-method method))
15008                       ((stringp (car method))
15009                        (gnus-server-extend-method group method))
15010                       (t
15011                        method)))
15012           (if (equal (cadr method) "")
15013               method
15014             (gnus-server-add-address method))))))
15015
15016 (defun gnus-check-backend-function (func group)
15017   "Check whether GROUP supports function FUNC."
15018   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15019                   group)))
15020     (fboundp (intern (format "%s-%s" method func)))))
15021
15022 (defun gnus-methods-using (feature)
15023   "Find all methods that have FEATURE."
15024   (let ((valids gnus-valid-select-methods)
15025         outs)
15026     (while valids
15027       (if (memq feature (car valids))
15028           (setq outs (cons (car valids) outs)))
15029       (setq valids (cdr valids)))
15030     outs))
15031
15032 \f
15033 ;;;
15034 ;;; Active & Newsrc File Handling
15035 ;;;
15036
15037 (defun gnus-setup-news (&optional rawfile level dont-connect)
15038   "Setup news information.
15039 If RAWFILE is non-nil, the .newsrc file will also be read.
15040 If LEVEL is non-nil, the news will be set up at level LEVEL."
15041   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15042
15043     (when init 
15044       ;; Clear some variables to re-initialize news information.
15045       (setq gnus-newsrc-alist nil
15046             gnus-active-hashtb nil)
15047       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15048       (gnus-read-newsrc-file rawfile))
15049
15050     (when (and (not (assoc "archive" gnus-server-alist))
15051                gnus-message-archive-method)
15052       (push (cons "archive" gnus-message-archive-method)
15053             gnus-server-alist))
15054
15055     ;; If we don't read the complete active file, we fill in the
15056     ;; hashtb here.
15057     (if (or (null gnus-read-active-file)
15058             (eq gnus-read-active-file 'some))
15059         (gnus-update-active-hashtb-from-killed))
15060
15061     ;; Read the active file and create `gnus-active-hashtb'.
15062     ;; If `gnus-read-active-file' is nil, then we just create an empty
15063     ;; hash table.  The partial filling out of the hash table will be
15064     ;; done in `gnus-get-unread-articles'.
15065     (and gnus-read-active-file
15066          (not level)
15067          (gnus-read-active-file))
15068
15069     (or gnus-active-hashtb
15070         (setq gnus-active-hashtb (make-vector 4095 0)))
15071
15072     ;; Initialize the cache.
15073     (when gnus-use-cache
15074       (gnus-cache-open))
15075
15076     ;; Possibly eval the dribble file.
15077     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15078
15079     (gnus-update-format-specifications)
15080
15081     ;; See whether we need to read the description file.
15082     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15083              (not gnus-description-hashtb)
15084              (not dont-connect)
15085              gnus-read-active-file)
15086         (gnus-read-all-descriptions-files))
15087
15088     ;; Find new newsgroups and treat them.
15089     (if (and init gnus-check-new-newsgroups (not level)
15090              (gnus-check-server gnus-select-method))
15091         (gnus-find-new-newsgroups))
15092
15093     ;; We might read in new NoCeM messages here.
15094     (when gnus-use-nocem 
15095       (gnus-nocem-scan-groups))
15096
15097     ;; Find the number of unread articles in each non-dead group.
15098     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15099       (gnus-get-unread-articles level))
15100
15101     (if (and init gnus-check-bogus-newsgroups
15102              gnus-read-active-file (not level)
15103              (gnus-server-opened gnus-select-method))
15104         (gnus-check-bogus-newsgroups))))
15105
15106 (defun gnus-find-new-newsgroups (&optional arg)
15107   "Search for new newsgroups and add them.
15108 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15109 The `-n' option line from .newsrc is respected.
15110 If ARG (the prefix), use the `ask-server' method to query
15111 the server for new groups."
15112   (interactive "P")
15113   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15114                        (null gnus-read-active-file)
15115                        (eq gnus-read-active-file 'some))
15116                    'ask-server gnus-check-new-newsgroups)))
15117     (unless (gnus-check-first-time-used)
15118       (if (or (consp check)
15119               (eq check 'ask-server))
15120           ;; Ask the server for new groups.
15121           (gnus-ask-server-for-new-groups)
15122         ;; Go through the active hashtb and look for new groups.
15123         (let ((groups 0)
15124               group new-newsgroups)
15125           (gnus-message 5 "Looking for new newsgroups...")
15126           (unless gnus-have-read-active-file
15127             (gnus-read-active-file))
15128           (setq gnus-newsrc-last-checked-date (current-time-string))
15129           (unless gnus-killed-hashtb
15130             (gnus-make-hashtable-from-killed))
15131           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15132           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15133           (mapatoms
15134            (lambda (sym)
15135              (if (or (null (setq group (symbol-name sym)))
15136                      (not (boundp sym))
15137                      (null (symbol-value sym))
15138                      (gnus-gethash group gnus-killed-hashtb)
15139                      (gnus-gethash group gnus-newsrc-hashtb))
15140                  ()
15141                (let ((do-sub (gnus-matches-options-n group)))
15142                  (cond
15143                   ((eq do-sub 'subscribe)
15144                    (setq groups (1+ groups))
15145                    (gnus-sethash group group gnus-killed-hashtb)
15146                    (funcall gnus-subscribe-options-newsgroup-method group))
15147                   ((eq do-sub 'ignore)
15148                    nil)
15149                   (t
15150                    (setq groups (1+ groups))
15151                    (gnus-sethash group group gnus-killed-hashtb)
15152                    (if gnus-subscribe-hierarchical-interactive
15153                        (setq new-newsgroups (cons group new-newsgroups))
15154                      (funcall gnus-subscribe-newsgroup-method group)))))))
15155            gnus-active-hashtb)
15156           (when new-newsgroups
15157             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15158           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15159           (if (> groups 0)
15160               (gnus-message 6 "%d new newsgroup%s arrived."
15161                             groups (if (> groups 1) "s have" " has"))
15162             (gnus-message 6 "No new newsgroups.")))))))
15163
15164 (defun gnus-matches-options-n (group)
15165   ;; Returns `subscribe' if the group is to be unconditionally
15166   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15167   ;; no match for the group.
15168
15169   ;; First we check the two user variables.
15170   (cond
15171    ((and gnus-options-subscribe
15172          (string-match gnus-options-subscribe group))
15173     'subscribe)
15174    ((and gnus-auto-subscribed-groups
15175          (string-match gnus-auto-subscribed-groups group))
15176     'subscribe)
15177    ((and gnus-options-not-subscribe
15178          (string-match gnus-options-not-subscribe group))
15179     'ignore)
15180    ;; Then we go through the list that was retrieved from the .newsrc
15181    ;; file.  This list has elements on the form
15182    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15183    ;; is in the reverse order of the options line) is returned.
15184    (t
15185     (let ((regs gnus-newsrc-options-n))
15186       (while (and regs
15187                   (not (string-match (caar regs) group)))
15188         (setq regs (cdr regs)))
15189       (and regs (cdar regs))))))
15190
15191 (defun gnus-ask-server-for-new-groups ()
15192   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15193          (methods (cons gnus-select-method
15194                         (nconc
15195                          (when gnus-message-archive-method
15196                            (list "archive"))
15197                          (append
15198                           (and (consp gnus-check-new-newsgroups)
15199                                gnus-check-new-newsgroups)
15200                           gnus-secondary-select-methods))))
15201          (groups 0)
15202          (new-date (current-time-string))
15203          group new-newsgroups got-new method hashtb
15204          gnus-override-subscribe-method)
15205     ;; Go through both primary and secondary select methods and
15206     ;; request new newsgroups.
15207     (while (setq method (gnus-server-get-method nil (pop methods)))
15208       (setq new-newsgroups nil)
15209       (setq gnus-override-subscribe-method method)
15210       (when (and (gnus-check-server method)
15211                  (gnus-request-newgroups date method))
15212         (save-excursion
15213           (setq got-new t)
15214           (setq hashtb (gnus-make-hashtable 100))
15215           (set-buffer nntp-server-buffer)
15216           ;; Enter all the new groups into a hashtable.
15217           (gnus-active-to-gnus-format method hashtb 'ignore)))
15218       ;; Now all new groups from `method' are in `hashtb'.
15219       (mapatoms
15220        (lambda (group-sym)
15221          (if (or (null (setq group (symbol-name group-sym)))
15222                  (null (symbol-value group-sym))
15223                  (gnus-gethash group gnus-newsrc-hashtb)
15224                  (member group gnus-zombie-list)
15225                  (member group gnus-killed-list))
15226              ;; The group is already known.
15227              ()
15228            ;; Make this group active.
15229            (when (symbol-value group-sym)
15230              (gnus-set-active group (symbol-value group-sym)))
15231            ;; Check whether we want it or not.
15232            (let ((do-sub (gnus-matches-options-n group)))
15233              (cond
15234               ((eq do-sub 'subscribe)
15235                (incf groups)
15236                (gnus-sethash group group gnus-killed-hashtb)
15237                (funcall gnus-subscribe-options-newsgroup-method group))
15238               ((eq do-sub 'ignore)
15239                nil)
15240               (t
15241                (incf groups)
15242                (gnus-sethash group group gnus-killed-hashtb)
15243                (if gnus-subscribe-hierarchical-interactive
15244                    (push group new-newsgroups)
15245                  (funcall gnus-subscribe-newsgroup-method group)))))))
15246        hashtb)
15247       (when new-newsgroups
15248         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15249     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15250     (when (> groups 0)
15251       (gnus-message 6 "%d new newsgroup%s arrived."
15252                     groups (if (> groups 1) "s have" " has")))
15253     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15254     got-new))
15255
15256 (defun gnus-check-first-time-used ()
15257   (if (or (> (length gnus-newsrc-alist) 1)
15258           (file-exists-p gnus-startup-file)
15259           (file-exists-p (concat gnus-startup-file ".el"))
15260           (file-exists-p (concat gnus-startup-file ".eld")))
15261       nil
15262     (gnus-message 6 "First time user; subscribing you to default groups")
15263     (unless gnus-have-read-active-file
15264       (gnus-read-active-file))
15265     (setq gnus-newsrc-last-checked-date (current-time-string))
15266     (let ((groups gnus-default-subscribed-newsgroups)
15267           group)
15268       (if (eq groups t)
15269           nil
15270         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15271         (mapatoms
15272          (lambda (sym)
15273            (if (null (setq group (symbol-name sym)))
15274                ()
15275              (let ((do-sub (gnus-matches-options-n group)))
15276                (cond
15277                 ((eq do-sub 'subscribe)
15278                  (gnus-sethash group group gnus-killed-hashtb)
15279                  (funcall gnus-subscribe-options-newsgroup-method group))
15280                 ((eq do-sub 'ignore)
15281                  nil)
15282                 (t
15283                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15284          gnus-active-hashtb)
15285         (while groups
15286           (if (gnus-active (car groups))
15287               (gnus-group-change-level
15288                (car groups) gnus-level-default-subscribed gnus-level-killed))
15289           (setq groups (cdr groups)))
15290         (gnus-group-make-help-group)
15291         (and gnus-novice-user
15292              (gnus-message 7 "`A k' to list killed groups"))))))
15293
15294 (defun gnus-subscribe-group (group previous &optional method)
15295   (gnus-group-change-level
15296    (if method
15297        (list t group gnus-level-default-subscribed nil nil method)
15298      group)
15299    gnus-level-default-subscribed gnus-level-killed previous t))
15300
15301 ;; `gnus-group-change-level' is the fundamental function for changing
15302 ;; subscription levels of newsgroups.  This might mean just changing
15303 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15304 ;; again, which subscribes/unsubscribes a group, which is equally
15305 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15306 ;; from 8-9 to 1-7 means that you remove the group from the list of
15307 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15308 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15309 ;; which is trivial.
15310 ;; ENTRY can either be a string (newsgroup name) or a list (if
15311 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15312 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15313 ;; entries.
15314 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15315 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15316 ;; after.
15317 (defun gnus-group-change-level (entry level &optional oldlevel
15318                                       previous fromkilled)
15319   (let (group info active num)
15320     ;; Glean what info we can from the arguments
15321     (if (consp entry)
15322         (if fromkilled (setq group (nth 1 entry))
15323           (setq group (car (nth 2 entry))))
15324       (setq group entry))
15325     (if (and (stringp entry)
15326              oldlevel
15327              (< oldlevel gnus-level-zombie))
15328         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15329     (if (and (not oldlevel)
15330              (consp entry))
15331         (setq oldlevel (gnus-info-level (nth 2 entry)))
15332       (setq oldlevel (or oldlevel 9)))
15333     (if (stringp previous)
15334         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15335
15336     (if (and (>= oldlevel gnus-level-zombie)
15337              (gnus-gethash group gnus-newsrc-hashtb))
15338         ;; We are trying to subscribe a group that is already
15339         ;; subscribed.
15340         ()                              ; Do nothing.
15341
15342       (or (gnus-ephemeral-group-p group)
15343           (gnus-dribble-enter
15344            (format "(gnus-group-change-level %S %S %S %S %S)"
15345                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15346
15347       ;; Then we remove the newgroup from any old structures, if needed.
15348       ;; If the group was killed, we remove it from the killed or zombie
15349       ;; list.  If not, and it is in fact going to be killed, we remove
15350       ;; it from the newsrc hash table and assoc.
15351       (cond
15352        ((>= oldlevel gnus-level-zombie)
15353         (if (= oldlevel gnus-level-zombie)
15354             (setq gnus-zombie-list (delete group gnus-zombie-list))
15355           (setq gnus-killed-list (delete group gnus-killed-list))))
15356        (t
15357         (if (and (>= level gnus-level-zombie)
15358                  entry)
15359             (progn
15360               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15361               (if (nth 3 entry)
15362                   (setcdr (gnus-gethash (car (nth 3 entry))
15363                                         gnus-newsrc-hashtb)
15364                           (cdr entry)))
15365               (setcdr (cdr entry) (cdddr entry))))))
15366
15367       ;; Finally we enter (if needed) the list where it is supposed to
15368       ;; go, and change the subscription level.  If it is to be killed,
15369       ;; we enter it into the killed or zombie list.
15370       (cond 
15371        ((>= level gnus-level-zombie)
15372         ;; Remove from the hash table.
15373         (gnus-sethash group nil gnus-newsrc-hashtb)
15374         ;; We do not enter foreign groups into the list of dead
15375         ;; groups.
15376         (unless (gnus-group-foreign-p group)
15377           (if (= level gnus-level-zombie)
15378               (setq gnus-zombie-list (cons group gnus-zombie-list))
15379             (setq gnus-killed-list (cons group gnus-killed-list)))))
15380        (t
15381         ;; If the list is to be entered into the newsrc assoc, and
15382         ;; it was killed, we have to create an entry in the newsrc
15383         ;; hashtb format and fix the pointers in the newsrc assoc.
15384         (if (< oldlevel gnus-level-zombie)
15385             ;; It was alive, and it is going to stay alive, so we
15386             ;; just change the level and don't change any pointers or
15387             ;; hash table entries.
15388             (setcar (cdaddr entry) level)
15389           (if (listp entry)
15390               (setq info (cdr entry)
15391                     num (car entry))
15392             (setq active (gnus-active group))
15393             (setq num
15394                   (if active (- (1+ (cdr active)) (car active)) t))
15395             ;; Check whether the group is foreign.  If so, the
15396             ;; foreign select method has to be entered into the
15397             ;; info.
15398             (let ((method (or gnus-override-subscribe-method
15399                               (gnus-group-method group))))
15400               (if (eq method gnus-select-method)
15401                   (setq info (list group level nil))
15402                 (setq info (list group level nil nil method)))))
15403           (unless previous
15404             (setq previous
15405                   (let ((p gnus-newsrc-alist))
15406                     (while (cddr p)
15407                       (setq p (cdr p)))
15408                     p)))
15409           (setq entry (cons info (cddr previous)))
15410           (if (cdr previous)
15411               (progn
15412                 (setcdr (cdr previous) entry)
15413                 (gnus-sethash group (cons num (cdr previous))
15414                               gnus-newsrc-hashtb))
15415             (setcdr previous entry)
15416             (gnus-sethash group (cons num previous)
15417                           gnus-newsrc-hashtb))
15418           (when (cdr entry)
15419             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15420       (when gnus-group-change-level-function
15421         (funcall gnus-group-change-level-function group level oldlevel)))))
15422
15423 (defun gnus-kill-newsgroup (newsgroup)
15424   "Obsolete function.  Kills a newsgroup."
15425   (gnus-group-change-level
15426    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15427
15428 (defun gnus-check-bogus-newsgroups (&optional confirm)
15429   "Remove bogus newsgroups.
15430 If CONFIRM is non-nil, the user has to confirm the deletion of every
15431 newsgroup."
15432   (let ((newsrc (cdr gnus-newsrc-alist))
15433         bogus group entry info)
15434     (gnus-message 5 "Checking bogus newsgroups...")
15435     (unless gnus-have-read-active-file
15436       (gnus-read-active-file))
15437     (when (member gnus-select-method gnus-have-read-active-file)
15438       ;; Find all bogus newsgroup that are subscribed.
15439       (while newsrc
15440         (setq info (pop newsrc)
15441               group (gnus-info-group info))
15442         (unless (or (gnus-active group) ; Active
15443                     (gnus-info-method info) ; Foreign
15444                     (and confirm
15445                          (not (gnus-y-or-n-p
15446                                (format "Remove bogus newsgroup: %s " group)))))
15447           ;; Found a bogus newsgroup.
15448           (push group bogus)))
15449       ;; Remove all bogus subscribed groups by first killing them, and
15450       ;; then removing them from the list of killed groups.
15451       (while bogus
15452         (when (setq entry (gnus-gethash (setq group (pop bogus))
15453                                         gnus-newsrc-hashtb))
15454           (gnus-group-change-level entry gnus-level-killed)
15455           (setq gnus-killed-list (delete group gnus-killed-list))))
15456       ;; Then we remove all bogus groups from the list of killed and
15457       ;; zombie groups.  They are are removed without confirmation.
15458       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15459             killed)
15460         (while dead-lists
15461           (setq killed (symbol-value (car dead-lists)))
15462           (while killed
15463             (unless (gnus-active (setq group (pop killed)))
15464               ;; The group is bogus.
15465               ;; !!!Slow as hell.
15466               (set (car dead-lists)
15467                    (delete group (symbol-value (car dead-lists))))))
15468           (setq dead-lists (cdr dead-lists))))
15469       (gnus-message 5 "Checking bogus newsgroups...done"))))
15470
15471 (defun gnus-check-duplicate-killed-groups ()
15472   "Remove duplicates from the list of killed groups."
15473   (interactive)
15474   (let ((killed gnus-killed-list))
15475     (while killed
15476       (gnus-message 9 "%d" (length killed))
15477       (setcdr killed (delete (car killed) (cdr killed)))
15478       (setq killed (cdr killed)))))
15479
15480 ;; We want to inline a function from gnus-cache, so we cheat here:
15481 (eval-when-compile
15482   (provide 'gnus)
15483   (require 'gnus-cache))
15484
15485 (defun gnus-get-unread-articles-in-group (info active &optional update)
15486   (when active
15487     ;; Allow the backend to update the info in the group.
15488     (when (and update 
15489                (gnus-request-update-info
15490                 info (gnus-find-method-for-group (gnus-info-group info))))
15491       (gnus-activate-group (gnus-info-group info) nil t))
15492     (let* ((range (gnus-info-read info))
15493            (num 0))
15494       ;; If a cache is present, we may have to alter the active info.
15495       (when (and gnus-use-cache info)
15496         (inline (gnus-cache-possibly-alter-active 
15497                  (gnus-info-group info) active)))
15498       ;; Modify the list of read articles according to what articles
15499       ;; are available; then tally the unread articles and add the
15500       ;; number to the group hash table entry.
15501       (cond
15502        ((zerop (cdr active))
15503         (setq num 0))
15504        ((not range)
15505         (setq num (- (1+ (cdr active)) (car active))))
15506        ((not (listp (cdr range)))
15507         ;; Fix a single (num . num) range according to the
15508         ;; active hash table.
15509         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15510         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15511         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15512         ;; Compute number of unread articles.
15513         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15514        (t
15515         ;; The read list is a list of ranges.  Fix them according to
15516         ;; the active hash table.
15517         ;; First peel off any elements that are below the lower
15518         ;; active limit.
15519         (while (and (cdr range)
15520                     (>= (car active)
15521                         (or (and (atom (cadr range)) (cadr range))
15522                             (caadr range))))
15523           (if (numberp (car range))
15524               (setcar range
15525                       (cons (car range)
15526                             (or (and (numberp (cadr range))
15527                                      (cadr range))
15528                                 (cdadr range))))
15529             (setcdr (car range)
15530                     (or (and (numberp (nth 1 range)) (nth 1 range))
15531                         (cdadr range))))
15532           (setcdr range (cddr range)))
15533         ;; Adjust the first element to be the same as the lower limit.
15534         (if (and (not (atom (car range)))
15535                  (< (cdar range) (car active)))
15536             (setcdr (car range) (1- (car active))))
15537         ;; Then we want to peel off any elements that are higher
15538         ;; than the upper active limit.
15539         (let ((srange range))
15540           ;; Go past all legal elements.
15541           (while (and (cdr srange)
15542                       (<= (or (and (atom (cadr srange))
15543                                    (cadr srange))
15544                               (caadr srange)) (cdr active)))
15545             (setq srange (cdr srange)))
15546           (if (cdr srange)
15547               ;; Nuke all remaining illegal elements.
15548               (setcdr srange nil))
15549
15550           ;; Adjust the final element.
15551           (if (and (not (atom (car srange)))
15552                    (> (cdar srange) (cdr active)))
15553               (setcdr (car srange) (cdr active))))
15554         ;; Compute the number of unread articles.
15555         (while range
15556           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15557                                       (cdar range)))
15558                               (or (and (atom (car range)) (car range))
15559                                   (caar range)))))
15560           (setq range (cdr range)))
15561         (setq num (max 0 (- (cdr active) num)))))
15562       ;; Set the number of unread articles.
15563       (when info
15564         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15565       num)))
15566
15567 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15568 ;; and compute how many unread articles there are in each group.
15569 (defun gnus-get-unread-articles (&optional level)
15570   (let* ((newsrc (cdr gnus-newsrc-alist))
15571          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15572          (foreign-level
15573           (min
15574            (cond ((and gnus-activate-foreign-newsgroups
15575                        (not (numberp gnus-activate-foreign-newsgroups)))
15576                   (1+ gnus-level-subscribed))
15577                  ((numberp gnus-activate-foreign-newsgroups)
15578                   gnus-activate-foreign-newsgroups)
15579                  (t 0))
15580            level))
15581          info group active method)
15582     (gnus-message 5 "Checking new news...")
15583
15584     (while newsrc
15585       (setq active (gnus-active (setq group (gnus-info-group
15586                                              (setq info (pop newsrc))))))
15587
15588       ;; Check newsgroups.  If the user doesn't want to check them, or
15589       ;; they can't be checked (for instance, if the news server can't
15590       ;; be reached) we just set the number of unread articles in this
15591       ;; newsgroup to t.  This means that Gnus thinks that there are
15592       ;; unread articles, but it has no idea how many.
15593       (if (and (setq method (gnus-info-method info))
15594                (not (gnus-server-equal
15595                      gnus-select-method
15596                      (setq method (gnus-server-get-method nil method))))
15597                (not (gnus-secondary-method-p method)))
15598           ;; These groups are foreign.  Check the level.
15599           (when (<= (gnus-info-level info) foreign-level)
15600             (setq active (gnus-activate-group group 'scan))
15601             (unless (inline (gnus-virtual-group-p group))
15602               (inline (gnus-close-group group))))
15603
15604         ;; These groups are native or secondary.
15605         (when (and (<= (gnus-info-level info) level)
15606                    (not gnus-read-active-file))
15607           (setq active (gnus-activate-group group 'scan))
15608           (inline (gnus-close-group group))))
15609
15610       (if active
15611           (inline (gnus-get-unread-articles-in-group 
15612                    info active
15613                    (and method
15614                         (fboundp (intern (concat (symbol-name (car method))
15615                                                  "-request-update-info"))))))
15616         ;; The group couldn't be reached, so we nix out the number of
15617         ;; unread articles and stuff.
15618         (gnus-set-active group nil)
15619         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15620
15621     (gnus-message 5 "Checking new news...done")))
15622
15623 ;; Create a hash table out of the newsrc alist.  The `car's of the
15624 ;; alist elements are used as keys.
15625 (defun gnus-make-hashtable-from-newsrc-alist ()
15626   (let ((alist gnus-newsrc-alist)
15627         (ohashtb gnus-newsrc-hashtb)
15628         prev)
15629     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15630     (setq alist
15631           (setq prev (setq gnus-newsrc-alist
15632                            (if (equal (caar gnus-newsrc-alist)
15633                                       "dummy.group")
15634                                gnus-newsrc-alist
15635                              (cons (list "dummy.group" 0 nil) alist)))))
15636     (while alist
15637       (gnus-sethash
15638        (caar alist)
15639        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15640              prev)
15641        gnus-newsrc-hashtb)
15642       (setq prev alist
15643             alist (cdr alist)))))
15644
15645 (defun gnus-make-hashtable-from-killed ()
15646   "Create a hash table from the killed and zombie lists."
15647   (let ((lists '(gnus-killed-list gnus-zombie-list))
15648         list)
15649     (setq gnus-killed-hashtb
15650           (gnus-make-hashtable
15651            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15652     (while (setq list (pop lists))
15653       (setq list (symbol-value list))
15654       (while list
15655         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15656
15657 (defun gnus-activate-group (group &optional scan dont-check)
15658   ;; Check whether a group has been activated or not.
15659   ;; If SCAN, request a scan of that group as well.
15660   (let ((method (gnus-find-method-for-group group))
15661         active)
15662     (and (gnus-check-server method)
15663          ;; We escape all bugs and quit here to make it possible to
15664          ;; continue if a group is so out-there that it reports bugs
15665          ;; and stuff.
15666          (progn
15667            (and scan
15668                 (gnus-check-backend-function 'request-scan (car method))
15669                 (gnus-request-scan group method))
15670            t)
15671          (condition-case ()
15672              (gnus-request-group group dont-check)
15673         ;   (error nil)
15674            (quit nil))
15675          (save-excursion
15676            (set-buffer nntp-server-buffer)
15677            (goto-char (point-min))
15678            ;; Parse the result we got from `gnus-request-group'.
15679            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15680                 (progn
15681                   (goto-char (match-beginning 1))
15682                   (gnus-set-active
15683                    group (setq active (cons (read (current-buffer))
15684                                             (read (current-buffer)))))
15685                   ;; Return the new active info.
15686                   active))))))
15687
15688 (defun gnus-update-read-articles (group unread)
15689   "Update the list of read and ticked articles in GROUP using the
15690 UNREAD and TICKED lists.
15691 Note: UNSELECTED has to be sorted over `<'.
15692 Returns whether the updating was successful."
15693   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15694          (entry (gnus-gethash group gnus-newsrc-hashtb))
15695          (info (nth 2 entry))
15696          (prev 1)
15697          (unread (sort (copy-sequence unread) '<))
15698          read)
15699     (if (or (not info) (not active))
15700         ;; There is no info on this group if it was, in fact,
15701         ;; killed.  Gnus stores no information on killed groups, so
15702         ;; there's nothing to be done.
15703         ;; One could store the information somewhere temporarily,
15704         ;; perhaps...  Hmmm...
15705         ()
15706       ;; Remove any negative articles numbers.
15707       (while (and unread (< (car unread) 0))
15708         (setq unread (cdr unread)))
15709       ;; Remove any expired article numbers
15710       (while (and unread (< (car unread) (car active)))
15711         (setq unread (cdr unread)))
15712       ;; Compute the ranges of read articles by looking at the list of
15713       ;; unread articles.
15714       (while unread
15715         (if (/= (car unread) prev)
15716             (setq read (cons (if (= prev (1- (car unread))) prev
15717                                (cons prev (1- (car unread)))) read)))
15718         (setq prev (1+ (car unread)))
15719         (setq unread (cdr unread)))
15720       (when (<= prev (cdr active))
15721         (setq read (cons (cons prev (cdr active)) read)))
15722       ;; Enter this list into the group info.
15723       (gnus-info-set-read
15724        info (if (> (length read) 1) (nreverse read) read))
15725       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15726       (gnus-get-unread-articles-in-group info (gnus-active group))
15727       t)))
15728
15729 (defun gnus-make-articles-unread (group articles)
15730   "Mark ARTICLES in GROUP as unread."
15731   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15732                           (gnus-gethash (gnus-group-real-name group)
15733                                         gnus-newsrc-hashtb))))
15734          (ranges (gnus-info-read info))
15735          news article)
15736     (while articles
15737       (when (gnus-member-of-range
15738              (setq article (pop articles)) ranges)
15739         (setq news (cons article news))))
15740     (when news
15741       (gnus-info-set-read
15742        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15743       (gnus-group-update-group group t))))
15744
15745 ;; Enter all dead groups into the hashtb.
15746 (defun gnus-update-active-hashtb-from-killed ()
15747   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15748         (lists (list gnus-killed-list gnus-zombie-list))
15749         killed)
15750     (while lists
15751       (setq killed (car lists))
15752       (while killed
15753         (gnus-sethash (car killed) nil hashtb)
15754         (setq killed (cdr killed)))
15755       (setq lists (cdr lists)))))
15756
15757 ;; Get the active file(s) from the backend(s).
15758 (defun gnus-read-active-file ()
15759   (gnus-group-set-mode-line)
15760   (let ((methods 
15761          (append
15762           (if (gnus-check-server gnus-select-method)
15763               ;; The native server is available.
15764               (cons gnus-select-method gnus-secondary-select-methods)
15765             ;; The native server is down, so we just do the
15766             ;; secondary ones.
15767             gnus-secondary-select-methods)
15768           ;; Also read from the archive server.
15769           (when gnus-message-archive-method
15770             (list "archive"))))
15771         list-type)
15772     (setq gnus-have-read-active-file nil)
15773     (save-excursion
15774       (set-buffer nntp-server-buffer)
15775       (while methods
15776         (let* ((method (if (stringp (car methods))
15777                            (gnus-server-get-method nil (car methods))
15778                          (car methods)))
15779                (where (nth 1 method))
15780                (mesg (format "Reading active file%s via %s..."
15781                              (if (and where (not (zerop (length where))))
15782                                  (concat " from " where) "")
15783                              (car method))))
15784           (gnus-message 5 mesg)
15785           (when (gnus-check-server method)
15786             ;; Request that the backend scan its incoming messages.
15787             (and (gnus-check-backend-function 'request-scan (car method))
15788                  (gnus-request-scan nil method))
15789             (cond
15790              ((and (eq gnus-read-active-file 'some)
15791                    (gnus-check-backend-function 'retrieve-groups (car method)))
15792               (let ((newsrc (cdr gnus-newsrc-alist))
15793                     (gmethod (gnus-server-get-method nil method))
15794                     groups info)
15795                 (while (setq info (pop newsrc))
15796                   (when (gnus-server-equal
15797                          (gnus-find-method-for-group 
15798                           (gnus-info-group info) info)
15799                          gmethod)
15800                     (push (gnus-group-real-name (gnus-info-group info)) 
15801                           groups)))
15802                 (when groups
15803                   (gnus-check-server method)
15804                   (setq list-type (gnus-retrieve-groups groups method))
15805                   (cond
15806                    ((not list-type)
15807                     (gnus-message
15808                      1 "Cannot read partial active file from %s server."
15809                      (car method))
15810                     (ding)
15811                     (sit-for 2))
15812                    ((eq list-type 'active)
15813                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15814                    (t
15815                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15816              (t
15817               (if (not (gnus-request-list method))
15818                   (progn
15819                     (unless (equal method gnus-message-archive-method)
15820                       (gnus-message 1 "Cannot read active file from %s server."
15821                                     (car method))
15822                       (ding)))
15823                 (gnus-active-to-gnus-format method)
15824                 ;; We mark this active file as read.
15825                 (push method gnus-have-read-active-file)
15826                 (gnus-message 5 "%sdone" mesg))))))
15827         (setq methods (cdr methods))))))
15828
15829 ;; Read an active file and place the results in `gnus-active-hashtb'.
15830 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15831   (unless method
15832     (setq method gnus-select-method))
15833   (let ((cur (current-buffer))
15834         (hashtb (or hashtb
15835                     (if (and gnus-active-hashtb
15836                              (not (equal method gnus-select-method)))
15837                         gnus-active-hashtb
15838                       (setq gnus-active-hashtb
15839                             (if (equal method gnus-select-method)
15840                                 (gnus-make-hashtable
15841                                  (count-lines (point-min) (point-max)))
15842                               (gnus-make-hashtable 4096)))))))
15843     ;; Delete unnecessary lines.
15844     (goto-char (point-min))
15845     (while (search-forward "\nto." nil t)
15846       (delete-region (1+ (match-beginning 0))
15847                      (progn (forward-line 1) (point))))
15848     (or (string= gnus-ignored-newsgroups "")
15849         (progn
15850           (goto-char (point-min))
15851           (delete-matching-lines gnus-ignored-newsgroups)))
15852     ;; Make the group names readable as a lisp expression even if they
15853     ;; contain special characters.
15854     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15855     (goto-char (point-max))
15856     (while (re-search-backward "[][';?()#]" nil t)
15857       (insert ?\\))
15858     ;; If these are groups from a foreign select method, we insert the
15859     ;; group prefix in front of the group names.
15860     (and method (not (gnus-server-equal
15861                       (gnus-server-get-method nil method)
15862                       (gnus-server-get-method nil gnus-select-method)))
15863          (let ((prefix (gnus-group-prefixed-name "" method)))
15864            (goto-char (point-min))
15865            (while (and (not (eobp))
15866                        (progn (insert prefix)
15867                               (zerop (forward-line 1)))))))
15868     ;; Store the active file in a hash table.
15869     (goto-char (point-min))
15870     (if (string-match "%[oO]" gnus-group-line-format)
15871         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15872         ;; If we want information on moderated groups, we use this
15873         ;; loop...
15874         (let* ((mod-hashtb (make-vector 7 0))
15875                (m (intern "m" mod-hashtb))
15876                group max min)
15877           (while (not (eobp))
15878             (condition-case nil
15879                 (progn
15880                   (narrow-to-region (point) (gnus-point-at-eol))
15881                   (setq group (let ((obarray hashtb)) (read cur)))
15882                   (if (and (numberp (setq max (read cur)))
15883                            (numberp (setq min (read cur)))
15884                            (progn
15885                              (skip-chars-forward " \t")
15886                              (not
15887                               (or (= (following-char) ?=)
15888                                   (= (following-char) ?x)
15889                                   (= (following-char) ?j)))))
15890                       (set group (cons min max))
15891                     (set group nil))
15892                   ;; Enter moderated groups into a list.
15893                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15894                       (setq gnus-moderated-list
15895                             (cons (symbol-name group) gnus-moderated-list))))
15896               (error
15897                (and group
15898                     (symbolp group)
15899                     (set group nil))))
15900             (widen)
15901             (forward-line 1)))
15902       ;; And if we do not care about moderation, we use this loop,
15903       ;; which is faster.
15904       (let (group max min)
15905         (while (not (eobp))
15906           (condition-case ()
15907               (progn
15908                 (narrow-to-region (point) (gnus-point-at-eol))
15909                 ;; group gets set to a symbol interned in the hash table
15910                 ;; (what a hack!!) - jwz
15911                 (setq group (let ((obarray hashtb)) (read cur)))
15912                 (if (and (numberp (setq max (read cur)))
15913                          (numberp (setq min (read cur)))
15914                          (progn
15915                            (skip-chars-forward " \t")
15916                            (not
15917                             (or (= (following-char) ?=)
15918                                 (= (following-char) ?x)
15919                                 (= (following-char) ?j)))))
15920                     (set group (cons min max))
15921                   (set group nil)))
15922             (error
15923              (progn
15924                (and group
15925                     (symbolp group)
15926                     (set group nil))
15927                (or ignore-errors
15928                    (gnus-message 3 "Warning - illegal active: %s"
15929                                  (buffer-substring
15930                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15931           (widen)
15932           (forward-line 1))))))
15933
15934 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15935   ;; Parse a "groups" active file.
15936   (let ((cur (current-buffer))
15937         (hashtb (or hashtb
15938                     (if (and method gnus-active-hashtb)
15939                         gnus-active-hashtb
15940                       (setq gnus-active-hashtb
15941                             (gnus-make-hashtable
15942                              (count-lines (point-min) (point-max)))))))
15943         (prefix (and method
15944                      (not (gnus-server-equal
15945                            (gnus-server-get-method nil method)
15946                            (gnus-server-get-method nil gnus-select-method)))
15947                      (gnus-group-prefixed-name "" method))))
15948
15949     (goto-char (point-min))
15950     ;; We split this into to separate loops, one with the prefix
15951     ;; and one without to speed the reading up somewhat.
15952     (if prefix
15953         (let (min max opoint group)
15954           (while (not (eobp))
15955             (condition-case ()
15956                 (progn
15957                   (read cur) (read cur)
15958                   (setq min (read cur)
15959                         max (read cur)
15960                         opoint (point))
15961                   (skip-chars-forward " \t")
15962                   (insert prefix)
15963                   (goto-char opoint)
15964                   (set (let ((obarray hashtb)) (read cur))
15965                        (cons min max)))
15966               (error (and group (symbolp group) (set group nil))))
15967             (forward-line 1)))
15968       (let (min max group)
15969         (while (not (eobp))
15970           (condition-case ()
15971               (if (= (following-char) ?2)
15972                   (progn
15973                     (read cur) (read cur)
15974                     (setq min (read cur)
15975                           max (read cur))
15976                     (set (setq group (let ((obarray hashtb)) (read cur)))
15977                          (cons min max))))
15978             (error (and group (symbolp group) (set group nil))))
15979           (forward-line 1))))))
15980
15981 (defun gnus-read-newsrc-file (&optional force)
15982   "Read startup file.
15983 If FORCE is non-nil, the .newsrc file is read."
15984   ;; Reset variables that might be defined in the .newsrc.eld file.
15985   (let ((variables gnus-variable-list))
15986     (while variables
15987       (set (car variables) nil)
15988       (setq variables (cdr variables))))
15989   (let* ((newsrc-file gnus-current-startup-file)
15990          (quick-file (concat newsrc-file ".el")))
15991     (save-excursion
15992       ;; We always load the .newsrc.eld file.  If always contains
15993       ;; much information that can not be gotten from the .newsrc
15994       ;; file (ticked articles, killed groups, foreign methods, etc.)
15995       (gnus-read-newsrc-el-file quick-file)
15996
15997       (if (and (file-exists-p gnus-current-startup-file)
15998                (or force
15999                    (and (file-newer-than-file-p newsrc-file quick-file)
16000                         (file-newer-than-file-p newsrc-file
16001                                                 (concat quick-file "d")))
16002                    (not gnus-newsrc-alist)))
16003           ;; We read the .newsrc file.  Note that if there if a
16004           ;; .newsrc.eld file exists, it has already been read, and
16005           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16006           ;; the .newsrc file, Gnus will only use the information it
16007           ;; can find there for changing the data already read -
16008           ;; ie. reading the .newsrc file will not trash the data
16009           ;; already read (except for read articles).
16010           (save-excursion
16011             (gnus-message 5 "Reading %s..." newsrc-file)
16012             (set-buffer (find-file-noselect newsrc-file))
16013             (buffer-disable-undo (current-buffer))
16014             (gnus-newsrc-to-gnus-format)
16015             (kill-buffer (current-buffer))
16016             (gnus-message 5 "Reading %s...done" newsrc-file)))
16017
16018       ;; Read any slave files.
16019       (or gnus-slave
16020           (gnus-master-read-slave-newsrc)))))
16021
16022 (defun gnus-read-newsrc-el-file (file)
16023   (let ((ding-file (concat file "d")))
16024     ;; We always, always read the .eld file.
16025     (gnus-message 5 "Reading %s..." ding-file)
16026     (let (gnus-newsrc-assoc)
16027       (condition-case nil
16028           (load ding-file t t t)
16029         (error
16030          (gnus-message 1 "Error in %s" ding-file)
16031          (ding)))
16032       (when gnus-newsrc-assoc
16033         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16034     (gnus-make-hashtable-from-newsrc-alist)
16035     (when (file-newer-than-file-p file ding-file)
16036       ;; Old format quick file
16037       (gnus-message 5 "Reading %s..." file)
16038       ;; The .el file is newer than the .eld file, so we read that one
16039       ;; as well.
16040       (gnus-read-old-newsrc-el-file file))))
16041
16042 ;; Parse the old-style quick startup file
16043 (defun gnus-read-old-newsrc-el-file (file)
16044   (let (newsrc killed marked group m info)
16045     (prog1
16046         (let ((gnus-killed-assoc nil)
16047               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16048           (prog1
16049               (condition-case nil
16050                   (load file t t t)
16051                 (error nil))
16052             (setq newsrc gnus-newsrc-assoc
16053                   killed gnus-killed-assoc
16054                   marked gnus-marked-assoc)))
16055       (setq gnus-newsrc-alist nil)
16056       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16057         (if info
16058             (progn
16059               (gnus-info-set-read info (cddr group))
16060               (gnus-info-set-level
16061                info (if (nth 1 group) gnus-level-default-subscribed
16062                       gnus-level-default-unsubscribed))
16063               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16064           (push (setq info
16065                       (list (car group)
16066                             (if (nth 1 group) gnus-level-default-subscribed
16067                               gnus-level-default-unsubscribed)
16068                             (cddr group)))
16069                 gnus-newsrc-alist))
16070         ;; Copy marks into info.
16071         (when (setq m (assoc (car group) marked))
16072           (unless (nthcdr 3 info)
16073             (nconc info (list nil)))
16074           (gnus-info-set-marks
16075            info (list (cons 'tick (gnus-compress-sequence 
16076                                    (sort (cdr m) '<) t))))))
16077       (setq newsrc killed)
16078       (while newsrc
16079         (setcar newsrc (caar newsrc))
16080         (setq newsrc (cdr newsrc)))
16081       (setq gnus-killed-list killed))
16082     ;; The .el file version of this variable does not begin with
16083     ;; "options", while the .eld version does, so we just add it if it
16084     ;; isn't there.
16085     (and
16086      gnus-newsrc-options
16087      (progn
16088        (and (not (string-match "^ *options" gnus-newsrc-options))
16089             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16090        (and (not (string-match "\n$" gnus-newsrc-options))
16091             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16092        ;; Finally, if we read some options lines, we parse them.
16093        (or (string= gnus-newsrc-options "")
16094            (gnus-newsrc-parse-options gnus-newsrc-options))))
16095
16096     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16097     (gnus-make-hashtable-from-newsrc-alist)))
16098
16099 (defun gnus-make-newsrc-file (file)
16100   "Make server dependent file name by catenating FILE and server host name."
16101   (let* ((file (expand-file-name file nil))
16102          (real-file (concat file "-" (nth 1 gnus-select-method))))
16103     (if (or (file-exists-p real-file)
16104             (file-exists-p (concat real-file ".el"))
16105             (file-exists-p (concat real-file ".eld")))
16106         real-file file)))
16107
16108 (defun gnus-newsrc-to-gnus-format ()
16109   (setq gnus-newsrc-options "")
16110   (setq gnus-newsrc-options-n nil)
16111
16112   (or gnus-active-hashtb
16113       (setq gnus-active-hashtb (make-vector 4095 0)))
16114   (let ((buf (current-buffer))
16115         (already-read (> (length gnus-newsrc-alist) 1))
16116         group subscribed options-symbol newsrc Options-symbol
16117         symbol reads num1)
16118     (goto-char (point-min))
16119     ;; We intern the symbol `options' in the active hashtb so that we
16120     ;; can `eq' against it later.
16121     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16122     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16123
16124     (while (not (eobp))
16125       ;; We first read the first word on the line by narrowing and
16126       ;; then reading into `gnus-active-hashtb'.  Most groups will
16127       ;; already exist in that hashtb, so this will save some string
16128       ;; space.
16129       (narrow-to-region
16130        (point)
16131        (progn (skip-chars-forward "^ \t!:\n") (point)))
16132       (goto-char (point-min))
16133       (setq symbol
16134             (and (/= (point-min) (point-max))
16135                  (let ((obarray gnus-active-hashtb)) (read buf))))
16136       (widen)
16137       ;; Now, the symbol we have read is either `options' or a group
16138       ;; name.  If it is an options line, we just add it to a string.
16139       (cond
16140        ((or (eq symbol options-symbol)
16141             (eq symbol Options-symbol))
16142         (setq gnus-newsrc-options
16143               ;; This concating is quite inefficient, but since our
16144               ;; thorough studies show that approx 99.37% of all
16145               ;; .newsrc files only contain a single options line, we
16146               ;; don't give a damn, frankly, my dear.
16147               (concat gnus-newsrc-options
16148                       (buffer-substring
16149                        (gnus-point-at-bol)
16150                        ;; Options may continue on the next line.
16151                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16152                                 (progn (beginning-of-line) (point)))
16153                            (point)))))
16154         (forward-line -1))
16155        (symbol
16156         ;; Group names can be just numbers.  
16157         (when (numberp symbol) 
16158           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16159         (or (boundp symbol) (set symbol nil))
16160         ;; It was a group name.
16161         (setq subscribed (= (following-char) ?:)
16162               group (symbol-name symbol)
16163               reads nil)
16164         (if (eolp)
16165             ;; If the line ends here, this is clearly a buggy line, so
16166             ;; we put point a the beginning of line and let the cond
16167             ;; below do the error handling.
16168             (beginning-of-line)
16169           ;; We skip to the beginning of the ranges.
16170           (skip-chars-forward "!: \t"))
16171         ;; We are now at the beginning of the list of read articles.
16172         ;; We read them range by range.
16173         (while
16174             (cond
16175              ((looking-at "[0-9]+")
16176               ;; We narrow and read a number instead of buffer-substring/
16177               ;; string-to-int because it's faster.  narrow/widen is
16178               ;; faster than save-restriction/narrow, and save-restriction
16179               ;; produces a garbage object.
16180               (setq num1 (progn
16181                            (narrow-to-region (match-beginning 0) (match-end 0))
16182                            (read buf)))
16183               (widen)
16184               ;; If the next character is a dash, then this is a range.
16185               (if (= (following-char) ?-)
16186                   (progn
16187                     ;; We read the upper bound of the range.
16188                     (forward-char 1)
16189                     (if (not (looking-at "[0-9]+"))
16190                         ;; This is a buggy line, by we pretend that
16191                         ;; it's kinda OK.  Perhaps the user should be
16192                         ;; dinged?
16193                         (setq reads (cons num1 reads))
16194                       (setq reads
16195                             (cons
16196                              (cons num1
16197                                    (progn
16198                                      (narrow-to-region (match-beginning 0)
16199                                                        (match-end 0))
16200                                      (read buf)))
16201                              reads))
16202                       (widen)))
16203                 ;; It was just a simple number, so we add it to the
16204                 ;; list of ranges.
16205                 (setq reads (cons num1 reads)))
16206               ;; If the next char in ?\n, then we have reached the end
16207               ;; of the line and return nil.
16208               (/= (following-char) ?\n))
16209              ((= (following-char) ?\n)
16210               ;; End of line, so we end.
16211               nil)
16212              (t
16213               ;; Not numbers and not eol, so this might be a buggy
16214               ;; line...
16215               (or (eobp)
16216                   ;; If it was eob instead of ?\n, we allow it.
16217                   (progn
16218                     ;; The line was buggy.
16219                     (setq group nil)
16220                     (gnus-message 3 "Mangled line: %s"
16221                                   (buffer-substring (gnus-point-at-bol)
16222                                                     (gnus-point-at-eol)))
16223                     (ding)
16224                     (sit-for 1)))
16225               nil))
16226           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16227           ;; we allow them, because it's a common mistake to put a
16228           ;; space after the comma.
16229           (skip-chars-forward ", "))
16230
16231         ;; We have already read .newsrc.eld, so we gently update the
16232         ;; data in the hash table with the information we have just
16233         ;; read.
16234         (when group
16235           (let ((info (gnus-get-info group))
16236                 level)
16237             (if info
16238                 ;; There is an entry for this file in the alist.
16239                 (progn
16240                   (gnus-info-set-read info (nreverse reads))
16241                   ;; We update the level very gently.  In fact, we
16242                   ;; only change it if there's been a status change
16243                   ;; from subscribed to unsubscribed, or vice versa.
16244                   (setq level (gnus-info-level info))
16245                   (cond ((and (<= level gnus-level-subscribed)
16246                               (not subscribed))
16247                          (setq level (if reads
16248                                          gnus-level-default-unsubscribed
16249                                        (1+ gnus-level-default-unsubscribed))))
16250                         ((and (> level gnus-level-subscribed) subscribed)
16251                          (setq level gnus-level-default-subscribed)))
16252                   (gnus-info-set-level info level))
16253               ;; This is a new group.
16254               (setq info (list group
16255                                (if subscribed
16256                                    gnus-level-default-subscribed
16257                                  (if reads
16258                                      (1+ gnus-level-subscribed)
16259                                    gnus-level-default-unsubscribed))
16260                                (nreverse reads))))
16261             (setq newsrc (cons info newsrc))))))
16262       (forward-line 1))
16263
16264     (setq newsrc (nreverse newsrc))
16265
16266     (if (not already-read)
16267         ()
16268       ;; We now have two newsrc lists - `newsrc', which is what we
16269       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16270       ;; what we've read from .newsrc.eld.  We have to merge these
16271       ;; lists.  We do this by "attaching" any (foreign) groups in the
16272       ;; gnus-newsrc-alist to the (native) group that precedes them.
16273       (let ((rc (cdr gnus-newsrc-alist))
16274             (prev gnus-newsrc-alist)
16275             entry mentry)
16276         (while rc
16277           (or (null (nth 4 (car rc)))   ; It's a native group.
16278               (assoc (caar rc) newsrc) ; It's already in the alist.
16279               (if (setq entry (assoc (caar prev) newsrc))
16280                   (setcdr (setq mentry (memq entry newsrc))
16281                           (cons (car rc) (cdr mentry)))
16282                 (setq newsrc (cons (car rc) newsrc))))
16283           (setq prev rc
16284                 rc (cdr rc)))))
16285
16286     (setq gnus-newsrc-alist newsrc)
16287     ;; We make the newsrc hashtb.
16288     (gnus-make-hashtable-from-newsrc-alist)
16289
16290     ;; Finally, if we read some options lines, we parse them.
16291     (or (string= gnus-newsrc-options "")
16292         (gnus-newsrc-parse-options gnus-newsrc-options))))
16293
16294 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16295 ;; The return value will be a list on the form
16296 ;; ((regexp1 . ignore)
16297 ;;  (regexp2 . subscribe)...)
16298 ;; When handling new newsgroups, groups that match a `ignore' regexp
16299 ;; will be ignored, and groups that match a `subscribe' regexp will be
16300 ;; subscribed.  A line like
16301 ;; options -n !all rec.all
16302 ;; will lead to a list that looks like
16303 ;; (("^rec\\..+" . subscribe)
16304 ;;  ("^.+" . ignore))
16305 ;; So all "rec.*" groups will be subscribed, while all the other
16306 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16307 ;; different from "options -n rec.all !all".
16308 (defun gnus-newsrc-parse-options (options)
16309   (let (out eol)
16310     (save-excursion
16311       (gnus-set-work-buffer)
16312       (insert (regexp-quote options))
16313       ;; First we treat all continuation lines.
16314       (goto-char (point-min))
16315       (while (re-search-forward "\n[ \t]+" nil t)
16316         (replace-match " " t t))
16317       ;; Then we transform all "all"s into ".+"s.
16318       (goto-char (point-min))
16319       (while (re-search-forward "\\ball\\b" nil t)
16320         (replace-match ".+" t t))
16321       (goto-char (point-min))
16322       ;; We remove all other options than the "-n" ones.
16323       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16324         (replace-match " ")
16325         (forward-char -1))
16326       (goto-char (point-min))
16327
16328       ;; We are only interested in "options -n" lines - we
16329       ;; ignore the other option lines.
16330       (while (re-search-forward "[ \t]-n" nil t)
16331         (setq eol
16332               (or (save-excursion
16333                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16334                          (- (point) 2)))
16335                   (gnus-point-at-eol)))
16336         ;; Search for all "words"...
16337         (while (re-search-forward "[^ \t,\n]+" eol t)
16338           (if (= (char-after (match-beginning 0)) ?!)
16339               ;; If the word begins with a bang (!), this is a "not"
16340               ;; spec.  We put this spec (minus the bang) and the
16341               ;; symbol `ignore' into the list.
16342               (setq out (cons (cons (concat
16343                                      "^" (buffer-substring
16344                                           (1+ (match-beginning 0))
16345                                           (match-end 0)))
16346                                     'ignore) out))
16347             ;; There was no bang, so this is a "yes" spec.
16348             (setq out (cons (cons (concat "^" (match-string 0))
16349                                   'subscribe) out)))))
16350
16351       (setq gnus-newsrc-options-n out))))
16352
16353 (defun gnus-save-newsrc-file (&optional force)
16354   "Save .newsrc file."
16355   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16356   ;; from the variable gnus-newsrc-alist.
16357   (when (and (or gnus-newsrc-alist gnus-killed-list)
16358              gnus-current-startup-file)
16359     (save-excursion
16360       (if (and (or gnus-use-dribble-file gnus-slave)
16361                (not force)
16362                (or (not gnus-dribble-buffer)
16363                    (not (buffer-name gnus-dribble-buffer))
16364                    (zerop (save-excursion
16365                             (set-buffer gnus-dribble-buffer)
16366                             (buffer-size)))))
16367           (gnus-message 4 "(No changes need to be saved)")
16368         (run-hooks 'gnus-save-newsrc-hook)
16369         (if gnus-slave
16370             (gnus-slave-save-newsrc)
16371           ;; Save .newsrc.
16372           (when gnus-save-newsrc-file
16373             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16374             (gnus-gnus-to-newsrc-format)
16375             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16376           ;; Save .newsrc.eld.
16377           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16378           (make-local-variable 'version-control)
16379           (setq version-control 'never)
16380           (setq buffer-file-name
16381                 (concat gnus-current-startup-file ".eld"))
16382           (gnus-add-current-to-buffer-list)
16383           (buffer-disable-undo (current-buffer))
16384           (erase-buffer)
16385           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16386           (gnus-gnus-to-quick-newsrc-format)
16387           (run-hooks 'gnus-save-quick-newsrc-hook)
16388           (save-buffer)
16389           (kill-buffer (current-buffer))
16390           (gnus-message
16391            5 "Saving %s.eld...done" gnus-current-startup-file))
16392         (gnus-dribble-delete-file)))))
16393
16394 (defun gnus-gnus-to-quick-newsrc-format ()
16395   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16396   (insert ";; Gnus startup file.\n")
16397   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16398   (insert ";; to read .newsrc.\n")
16399   (insert "(setq gnus-newsrc-file-version "
16400           (prin1-to-string gnus-version) ")\n")
16401   (let ((variables
16402          (if gnus-save-killed-list gnus-variable-list
16403            ;; Remove the `gnus-killed-list' from the list of variables
16404            ;; to be saved, if required.
16405            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16406         ;; Peel off the "dummy" group.
16407         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16408         variable)
16409     ;; Insert the variables into the file.
16410     (while variables
16411       (when (and (boundp (setq variable (pop variables)))
16412                  (symbol-value variable))
16413         (insert "(setq " (symbol-name variable) " '")
16414         (prin1 (symbol-value variable) (current-buffer))
16415         (insert ")\n")))))
16416
16417 (defun gnus-gnus-to-newsrc-format ()
16418   ;; Generate and save the .newsrc file.
16419   (save-excursion
16420     (set-buffer (create-file-buffer gnus-current-startup-file))
16421     (let ((newsrc (cdr gnus-newsrc-alist))
16422           (standard-output (current-buffer))
16423           info ranges range method)
16424       (setq buffer-file-name gnus-current-startup-file)
16425       (buffer-disable-undo (current-buffer))
16426       (erase-buffer)
16427       ;; Write options.
16428       (if gnus-newsrc-options (insert gnus-newsrc-options))
16429       ;; Write subscribed and unsubscribed.
16430       (while (setq info (pop newsrc))
16431         ;; Don't write foreign groups to .newsrc.
16432         (when (or (null (setq method (gnus-info-method info)))
16433                   (equal method "native")
16434                   (gnus-server-equal method gnus-select-method))
16435           (insert (gnus-info-group info)
16436                   (if (> (gnus-info-level info) gnus-level-subscribed)
16437                       "!" ":"))
16438           (when (setq ranges (gnus-info-read info))
16439             (insert " ")
16440             (if (not (listp (cdr ranges)))
16441                 (if (= (car ranges) (cdr ranges))
16442                     (princ (car ranges))
16443                   (princ (car ranges))
16444                   (insert "-")
16445                   (princ (cdr ranges)))
16446               (while (setq range (pop ranges))
16447                 (if (or (atom range) (= (car range) (cdr range)))
16448                     (princ (or (and (atom range) range) (car range)))
16449                   (princ (car range))
16450                   (insert "-")
16451                   (princ (cdr range)))
16452                 (if ranges (insert ",")))))
16453           (insert "\n")))
16454       (make-local-variable 'version-control)
16455       (setq version-control 'never)
16456       ;; It has been reported that sometime the modtime on the .newsrc
16457       ;; file seems to be off.  We really do want to overwrite it, so
16458       ;; we clear the modtime here before saving.  It's a bit odd,
16459       ;; though...
16460       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16461       ;; delete the silly thing entirely first.  but this fails to provide
16462       ;; such niceties as .newsrc~ creation.
16463       (if gnus-modtime-botch
16464           (delete-file gnus-startup-file)
16465         (clear-visited-file-modtime))
16466       (run-hooks 'gnus-save-standard-newsrc-hook)
16467       (save-buffer)
16468       (kill-buffer (current-buffer)))))
16469
16470 \f
16471 ;;;
16472 ;;; Slave functions.
16473 ;;;
16474
16475 (defun gnus-slave-save-newsrc ()
16476   (save-excursion
16477     (set-buffer gnus-dribble-buffer)
16478     (let ((slave-name
16479            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16480       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16481
16482 (defun gnus-master-read-slave-newsrc ()
16483   (let ((slave-files
16484          (directory-files
16485           (file-name-directory gnus-current-startup-file)
16486           t (concat
16487              "^" (regexp-quote
16488                   (concat
16489                    (file-name-nondirectory gnus-current-startup-file)
16490                    "-slave-")))
16491           t))
16492         file)
16493     (if (not slave-files)
16494         ()                              ; There are no slave files to read.
16495       (gnus-message 7 "Reading slave newsrcs...")
16496       (save-excursion
16497         (set-buffer (get-buffer-create " *gnus slave*"))
16498         (buffer-disable-undo (current-buffer))
16499         (setq slave-files
16500               (sort (mapcar (lambda (file)
16501                               (list (nth 5 (file-attributes file)) file))
16502                             slave-files)
16503                     (lambda (f1 f2)
16504                       (or (< (caar f1) (caar f2))
16505                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16506         (while slave-files
16507           (erase-buffer)
16508           (setq file (nth 1 (car slave-files)))
16509           (insert-file-contents file)
16510           (if (condition-case ()
16511                   (progn
16512                     (eval-buffer (current-buffer))
16513                     t)
16514                 (error
16515                  (gnus-message 3 "Possible error in %s" file)
16516                  (ding)
16517                  (sit-for 2)
16518                  nil))
16519               (or gnus-slave ; Slaves shouldn't delete these files.
16520                   (condition-case ()
16521                       (delete-file file)
16522                     (error nil))))
16523           (setq slave-files (cdr slave-files))))
16524       (gnus-message 7 "Reading slave newsrcs...done"))))
16525
16526 \f
16527 ;;;
16528 ;;; Group description.
16529 ;;;
16530
16531 (defun gnus-read-all-descriptions-files ()
16532   (let ((methods (cons gnus-select-method 
16533                        (nconc
16534                         (when gnus-message-archive-method
16535                           (list "archive"))
16536                         gnus-secondary-select-methods))))
16537     (while methods
16538       (gnus-read-descriptions-file (car methods))
16539       (setq methods (cdr methods)))
16540     t))
16541
16542 (defun gnus-read-descriptions-file (&optional method)
16543   (let ((method (or method gnus-select-method)))
16544     (when (stringp method)
16545       (setq method (gnus-server-to-method method)))
16546     ;; We create the hashtable whether we manage to read the desc file
16547     ;; to avoid trying to re-read after a failed read.
16548     (or gnus-description-hashtb
16549         (setq gnus-description-hashtb
16550               (gnus-make-hashtable (length gnus-active-hashtb))))
16551     ;; Mark this method's desc file as read.
16552     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16553                   gnus-description-hashtb)
16554
16555     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16556     (cond
16557      ((not (gnus-check-server method))
16558       (gnus-message 1 "Couldn't open server")
16559       nil)
16560      ((not (gnus-request-list-newsgroups method))
16561       (gnus-message 1 "Couldn't read newsgroups descriptions")
16562       nil)
16563      (t
16564       (let (group)
16565         (save-excursion
16566           (save-restriction
16567             (set-buffer nntp-server-buffer)
16568             (goto-char (point-min))
16569             (if (or (search-forward "\n.\n" nil t)
16570                     (goto-char (point-max)))
16571                 (progn
16572                   (beginning-of-line)
16573                   (narrow-to-region (point-min) (point))))
16574             (goto-char (point-min))
16575             (while (not (eobp))
16576               ;; If we get an error, we set group to 0, which is not a
16577               ;; symbol...
16578               (setq group
16579                     (condition-case ()
16580                         (let ((obarray gnus-description-hashtb))
16581                           ;; Group is set to a symbol interned in this
16582                           ;; hash table.
16583                           (read nntp-server-buffer))
16584                       (error 0)))
16585               (skip-chars-forward " \t")
16586               ;; ...  which leads to this line being effectively ignored.
16587               (and (symbolp group)
16588                    (set group (buffer-substring
16589                                (point) (progn (end-of-line) (point)))))
16590               (forward-line 1))))
16591         (gnus-message 5 "Reading descriptions file...done")
16592         t)))))
16593
16594 (defun gnus-group-get-description (group)
16595   "Get the description of a group by sending XGTITLE to the server."
16596   (when (gnus-request-group-description group)
16597     (save-excursion
16598       (set-buffer nntp-server-buffer)
16599       (goto-char (point-min))
16600       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16601         (match-string 1)))))
16602
16603 \f
16604 ;;;
16605 ;;; Buffering of read articles.
16606 ;;;
16607
16608 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16609 (defvar gnus-backlog-articles nil)
16610 (defvar gnus-backlog-hashtb nil)
16611
16612 (defun gnus-backlog-buffer ()
16613   "Return the backlog buffer."
16614   (or (get-buffer gnus-backlog-buffer)
16615       (save-excursion
16616         (set-buffer (get-buffer-create gnus-backlog-buffer))
16617         (buffer-disable-undo (current-buffer))
16618         (setq buffer-read-only t)
16619         (gnus-add-current-to-buffer-list)
16620         (get-buffer gnus-backlog-buffer))))
16621
16622 (defun gnus-backlog-setup ()
16623   "Initialize backlog variables."
16624   (unless gnus-backlog-hashtb
16625     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16626
16627 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16628
16629 (defun gnus-backlog-shutdown ()
16630   "Clear all backlog variables and buffers."
16631   (when (get-buffer gnus-backlog-buffer)
16632     (kill-buffer gnus-backlog-buffer))
16633   (setq gnus-backlog-hashtb nil
16634         gnus-backlog-articles nil))
16635
16636 (defun gnus-backlog-enter-article (group number buffer)
16637   (gnus-backlog-setup)
16638   (let ((ident (intern (concat group ":" (int-to-string number))
16639                        gnus-backlog-hashtb))
16640         b)
16641     (if (memq ident gnus-backlog-articles)
16642         () ; It's already kept.
16643       ;; Remove the oldest article, if necessary.
16644       (and (numberp gnus-keep-backlog)
16645            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16646            (gnus-backlog-remove-oldest-article))
16647       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16648       ;; Insert the new article.
16649       (save-excursion
16650         (set-buffer (gnus-backlog-buffer))
16651         (let (buffer-read-only)
16652           (goto-char (point-max))
16653           (or (bolp) (insert "\n"))
16654           (setq b (point))
16655           (insert-buffer-substring buffer)
16656           ;; Tag the beginning of the article with the ident.
16657           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16658
16659 (defun gnus-backlog-remove-oldest-article ()
16660   (save-excursion
16661     (set-buffer (gnus-backlog-buffer))
16662     (goto-char (point-min))
16663     (if (zerop (buffer-size))
16664         () ; The buffer is empty.
16665       (let ((ident (get-text-property (point) 'gnus-backlog))
16666             buffer-read-only)
16667         ;; Remove the ident from the list of articles.
16668         (when ident
16669           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16670         ;; Delete the article itself.
16671         (delete-region
16672          (point) (next-single-property-change
16673                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16674
16675 (defun gnus-backlog-request-article (group number buffer)
16676   (when (numberp number)
16677     (gnus-backlog-setup)
16678     (let ((ident (intern (concat group ":" (int-to-string number))
16679                          gnus-backlog-hashtb))
16680           beg end)
16681       (when (memq ident gnus-backlog-articles)
16682         ;; It was in the backlog.
16683         (save-excursion
16684           (set-buffer (gnus-backlog-buffer))
16685           (if (not (setq beg (text-property-any
16686                               (point-min) (point-max) 'gnus-backlog
16687                               ident)))
16688               ;; It wasn't in the backlog after all.
16689               (ignore
16690                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16691             ;; Find the end (i. e., the beginning of the next article).
16692             (setq end
16693                   (next-single-property-change
16694                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16695         (let ((buffer-read-only nil))
16696           (erase-buffer)
16697           (insert-buffer-substring gnus-backlog-buffer beg end)
16698           t)))))
16699
16700 ;; Allow redefinition of Gnus functions.
16701
16702 (gnus-ems-redefine)
16703
16704 (provide 'gnus)
16705
16706 ;;; gnus.el ends here