*** 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 (require 'nnmail)
36 (require 'backquote)
37
38 (eval-when-compile (require 'cl))
39
40 ;;;###autoload
41 (defvar gnus-directory (or (getenv "SAVEDIR") "~/News/")
42   "*Directory variable from which all other Gnus file variables are derived.")
43
44 ;; Site dependent variables.  These variables should be defined in
45 ;; paths.el.
46
47 (defvar gnus-default-nntp-server nil
48   "Specify a default NNTP server.
49 This variable should be defined in paths.el, and should never be set
50 by the user.
51 If you want to change servers, you should use `gnus-select-method'.
52 See the documentation to that variable.")
53
54 (defvar gnus-backup-default-subscribed-newsgroups
55   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
56   "Default default new newsgroups the first time Gnus is run.
57 Should be set in paths.el, and shouldn't be touched by the user.")
58
59 (defvar gnus-local-domain nil
60   "Local domain name without a host name.
61 The DOMAINNAME environment variable is used instead if it is defined.
62 If the `system-name' function returns the full Internet name, there is
63 no need to set this variable.")
64
65 (defvar gnus-local-organization nil
66   "String with a description of what organization (if any) the user belongs to.
67 The ORGANIZATION environment variable is used instead if it is defined.
68 If this variable contains a function, this function will be called
69 with the current newsgroup name as the argument.  The function should
70 return a string.
71
72 In any case, if the string (either in the variable, in the environment
73 variable, or returned by the function) is a file name, the contents of
74 this file will be used as the organization.")
75
76 ;; Customization variables
77
78 ;; Don't touch this variable.
79 (defvar gnus-nntp-service "nntp"
80   "*NNTP service name (\"nntp\" or 119).
81 This is an obsolete variable, which is scarcely used.  If you use an
82 nntp server for your newsgroup and want to change the port number
83 used to 899, you would say something along these lines:
84
85  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
86
87 (defvar gnus-nntpserver-file "/etc/nntpserver"
88   "*A file with only the name of the nntp server in it.")
89
90 ;; This function is used to check both the environment variable
91 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
92 ;; an nntp server name default.
93 (defun gnus-getenv-nntpserver ()
94   (or (getenv "NNTPSERVER")
95       (and (file-readable-p gnus-nntpserver-file)
96            (save-excursion
97              (set-buffer (get-buffer-create " *gnus nntp*"))
98              (buffer-disable-undo (current-buffer))
99              (insert-file-contents gnus-nntpserver-file)
100              (let ((name (buffer-string)))
101                (prog1
102                    (if (string-match "^[ \t\n]*$" name)
103                        nil
104                      name)
105                  (kill-buffer (current-buffer))))))))
106
107 (defvar gnus-select-method
108   (nconc
109    (list 'nntp (or (condition-case ()
110                        (gnus-getenv-nntpserver)
111                      (error nil))
112                    (if (and gnus-default-nntp-server
113                             (not (string= gnus-default-nntp-server "")))
114                        gnus-default-nntp-server)
115                    (system-name)))
116    (if (or (null gnus-nntp-service)
117            (equal gnus-nntp-service "nntp"))
118        nil
119      (list gnus-nntp-service)))
120   "*Default method for selecting a newsgroup.
121 This variable should be a list, where the first element is how the
122 news is to be fetched, the second is the address.
123
124 For instance, if you want to get your news via NNTP from
125 \"flab.flab.edu\", you could say:
126
127 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
128
129 If you want to use your local spool, say:
130
131 (setq gnus-select-method (list 'nnspool (system-name)))
132
133 If you use this variable, you must set `gnus-nntp-server' to nil.
134
135 There is a lot more to know about select methods and virtual servers -
136 see the manual for details.")
137
138 (defvar gnus-message-archive-method 
139   `(nnfolder
140     "archive"
141     (nnfolder-directory ,(nnheader-concat message-directory "archive"))
142     (nnfolder-active-file 
143      ,(nnheader-concat message-directory "archive/active"))
144     (nnfolder-get-new-mail nil)
145     (nnfolder-inhibit-expiry t))
146   "*Method used for archiving messages you've sent.
147 This should be a mail method.
148
149 It's probably not a very effective to change this variable once you've
150 run Gnus once.  After doing that, you must edit this server from the
151 server buffer.")
152
153 (defvar gnus-refer-article-method nil
154   "*Preferred method for fetching an article by Message-ID.
155 If you are reading news from the local spool (with nnspool), fetching
156 articles by Message-ID is painfully slow.  By setting this method to an
157 nntp method, you might get acceptable results.
158
159 The value of this variable must be a valid select method as discussed
160 in the documentation of `gnus-select-method'.")
161
162 (defvar gnus-secondary-select-methods nil
163   "*A list of secondary methods that will be used for reading news.
164 This is a list where each element is a complete select method (see
165 `gnus-select-method').
166
167 If, for instance, you want to read your mail with the nnml backend,
168 you could set this variable:
169
170 (setq gnus-secondary-select-methods '((nnml \"\")))")
171
172 (defvar gnus-secondary-servers nil
173   "*List of NNTP servers that the user can choose between interactively.
174 To make Gnus query you for a server, you have to give `gnus' a
175 non-numeric prefix - `C-u M-x gnus', in short.")
176
177 (defvar gnus-nntp-server nil
178   "*The name of the host running the NNTP server.
179 This variable is semi-obsolete.  Use the `gnus-select-method'
180 variable instead.")
181
182 (defvar gnus-startup-file "~/.newsrc"
183   "*Your `.newsrc' file.
184 `.newsrc-SERVER' will be used instead if that exists.")
185
186 (defvar gnus-init-file "~/.gnus"
187   "*Your Gnus elisp startup file.
188 If a file with the .el or .elc suffixes exist, it will be read
189 instead.")
190
191 (defvar gnus-group-faq-directory
192   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
193     "/ftp@sunsite.auc.dk:/pub/usenet/"
194     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
195     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
196     "/ftp@rtfm.mit.edu:/pub/usenet/"
197     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
198     "/ftp@ftp.sunet.se:/pub/usenet/"
199     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
200     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
201     "/ftp@ftp.hk.super.net:/mirror/faqs/")
202   "*Directory where the group FAQs are stored.
203 This will most commonly be on a remote machine, and the file will be
204 fetched by ange-ftp.
205
206 This variable can also be a list of directories.  In that case, the
207 first element in the list will be used by default, and the others will
208 be used as backup sites.
209
210 Note that Gnus uses an aol machine as the default directory.  If this
211 feels fundamentally unclean, just think of it as a way to finally get
212 something of value back from them.
213
214 If the default site is too slow, try one of these:
215
216    North America: mirrors.aol.com                /pub/rtfm/usenet
217                   ftp.seas.gwu.edu               /pub/rtfm
218                   rtfm.mit.edu                   /pub/usenet
219    Europe:        ftp.uni-paderborn.de           /pub/FAQ
220                   src.doc.ic.ac.uk               /usenet/news-FAQS
221                   ftp.sunet.se                   /pub/usenet
222                   sunsite.auc.dk                 /pub/usenet
223    Asia:          nctuccca.edu.tw                /USENET/FAQ
224                   hwarang.postech.ac.kr          /pub/usenet
225                   ftp.hk.super.net               /mirror/faqs")
226
227 (defvar gnus-group-archive-directory
228   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
229   "*The address of the (ding) archives.")
230
231 (defvar gnus-group-recent-archive-directory
232   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
233   "*The address of the most recent (ding) articles.")
234
235 (defvar gnus-default-subscribed-newsgroups nil
236   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
237 It should be a list of strings.
238 If it is `t', Gnus will not do anything special the first time it is
239 started; it'll just use the normal newsgroups subscription methods.")
240
241 (defvar gnus-use-cross-reference t
242   "*Non-nil means that cross referenced articles will be marked as read.
243 If nil, ignore cross references.  If t, mark articles as read in
244 subscribed newsgroups.  If neither t nor nil, mark as read in all
245 newsgroups.")
246
247 (defvar gnus-single-article-buffer t
248   "*If non-nil, display all articles in the same buffer.
249 If nil, each group will get its own article buffer.")
250
251 (defvar gnus-use-dribble-file t
252   "*Non-nil means that Gnus will use a dribble file to store user updates.
253 If Emacs should crash without saving the .newsrc files, complete
254 information can be restored from the dribble file.")
255
256 (defvar gnus-dribble-directory nil
257   "*The directory where dribble files will be saved.
258 If this variable is nil, the directory where the .newsrc files are
259 saved will be used.")
260
261 (defvar gnus-asynchronous nil
262   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
263
264 (defvar gnus-kill-summary-on-exit t
265   "*If non-nil, kill the summary buffer when you exit from it.
266 If nil, the summary will become a \"*Dead Summary*\" buffer, and
267 it will be killed sometime later.")
268
269 (defvar gnus-large-newsgroup 200
270   "*The number of articles which indicates a large newsgroup.
271 If the number of articles in a newsgroup is greater than this value,
272 confirmation is required for selecting the newsgroup.")
273
274 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
275 (defvar gnus-no-groups-message "No news is horrible news"
276   "*Message displayed by Gnus when no groups are available.")
277
278 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
279   "*Non-nil means that the default name of a file to save articles in is the group name.
280 If it's nil, the directory form of the group name is used instead.
281
282 If this variable is a list, and the list contains the element
283 `not-score', long file names will not be used for score files; if it
284 contains the element `not-save', long file names will not be used for
285 saving; and if it contains the element `not-kill', long file names
286 will not be used for kill files.
287
288 Note that the default for this variable varies according to what system
289 type you're using.  On `usg-unix-v' and `xenix' this variable defaults
290 to nil while on all other systems it defaults to t.")
291
292 (defvar gnus-article-save-directory gnus-directory
293   "*Name of the directory articles will be saved in (default \"~/News\").")
294
295 (defvar gnus-kill-files-directory gnus-directory
296   "*Name of the directory where kill files will be stored (default \"~/News\").")
297
298 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
299   "*A function to save articles in your favorite format.
300 The function must be interactively callable (in other words, it must
301 be an Emacs command).
302
303 Gnus provides the following functions:
304
305 * gnus-summary-save-in-rmail (Rmail format)
306 * gnus-summary-save-in-mail (Unix mail format)
307 * gnus-summary-save-in-folder (MH folder)
308 * gnus-summary-save-in-file (article format).
309 * gnus-summary-save-in-vm (use VM's folder format).")
310
311 (defvar gnus-prompt-before-saving 'always
312   "*This variable says how much prompting is to be done when saving articles.
313 If it is nil, no prompting will be done, and the articles will be
314 saved to the default files.  If this variable is `always', each and
315 every article that is saved will be preceded by a prompt, even when
316 saving large batches of articles.  If this variable is neither nil not
317 `always', there the user will be prompted once for a file name for
318 each invocation of the saving commands.")
319
320 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
321   "*A function generating a file name to save articles in Rmail format.
322 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
323
324 (defvar gnus-mail-save-name (function gnus-plain-save-name)
325   "*A function generating a file name to save articles in Unix mail format.
326 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
327
328 (defvar gnus-folder-save-name (function gnus-folder-save-name)
329   "*A function generating a file name to save articles in MH folder.
330 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
331
332 (defvar gnus-file-save-name (function gnus-numeric-save-name)
333   "*A function generating a file name to save articles in article format.
334 The function is called with NEWSGROUP, HEADERS, and optional
335 LAST-FILE.")
336
337 (defvar gnus-split-methods
338   '((gnus-article-archive-name))
339   "*Variable used to suggest where articles are to be saved.
340 For instance, if you would like to save articles related to Gnus in
341 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
342 you could set this variable to something like:
343
344  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
345    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
346
347 This variable is an alist where the where the key is the match and the
348 value is a list of possible files to save in if the match is non-nil.
349
350 If the match is a string, it is used as a regexp match on the
351 article.  If the match is a symbol, that symbol will be funcalled
352 from the buffer of the article to be saved with the newsgroup as the
353 parameter.  If it is a list, it will be evaled in the same buffer.
354
355 If this form or function returns a string, this string will be used as
356 a possible file name; and if it returns a non-nil list, that list will
357 be used as possible file names.")
358
359 (defvar gnus-move-split-methods nil
360   "*Variable used to suggest where articles are to be moved to.
361 It uses the same syntax as the `gnus-split-methods' variable.")
362
363 (defvar gnus-save-score nil
364   "*If non-nil, save group scoring info.")
365
366 (defvar gnus-use-adaptive-scoring nil
367   "*If non-nil, use some adaptive scoring scheme.")
368
369 (defvar gnus-use-cache 'passive
370   "*If nil, Gnus will ignore the article cache.
371 If `passive', it will allow entering (and reading) articles
372 explicitly entered into the cache.  If anything else, use the
373 cache to the full extent of the law.")
374
375 (defvar gnus-use-trees nil
376   "*If non-nil, display a thread tree buffer.")
377
378 (defvar gnus-use-grouplens nil
379   "*If non-nil, use GroupLens ratings.")
380
381 (defvar gnus-keep-backlog nil
382   "*If non-nil, Gnus will keep read articles for later re-retrieval.
383 If it is a number N, then Gnus will only keep the last N articles
384 read.  If it is neither nil nor a number, Gnus will keep all read
385 articles.  This is not a good idea.")
386
387 (defvar gnus-use-nocem nil
388   "*If non-nil, Gnus will read NoCeM cancel messages.")
389
390 (defvar gnus-use-demon nil
391   "If non-nil, Gnus might use some demons.")
392
393 (defvar gnus-use-scoring t
394   "*If non-nil, enable scoring.")
395
396 (defvar gnus-use-picons nil
397   "*If non-nil, display picons.")
398
399 (defvar gnus-fetch-old-headers nil
400   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
401 If an unread article in the group refers to an older, already read (or
402 just marked as read) article, the old article will not normally be
403 displayed in the Summary buffer.  If this variable is non-nil, Gnus
404 will attempt to grab the headers to the old articles, and thereby
405 build complete threads.  If it has the value `some', only enough
406 headers to connect otherwise loose threads will be displayed.
407 This variable can also be a number.  In that case, no more than that
408 number of old headers will be fetched.
409
410 The server has to support NOV for any of this to work.")
411
412 ;see gnus-cus.el
413 ;(defvar gnus-visual t
414 ;  "*If non-nil, will do various highlighting.
415 ;If nil, no mouse highlights (or any other highlights) will be
416 ;performed.  This might speed up Gnus some when generating large group
417 ;and summary buffers.")
418
419 (defvar gnus-novice-user t
420   "*Non-nil means that you are a usenet novice.
421 If non-nil, verbose messages may be displayed and confirmations may be
422 required.")
423
424 (defvar gnus-expert-user nil
425   "*Non-nil means that you will never be asked for confirmation about anything.
426 And that means *anything*.")
427
428 (defvar gnus-verbose 7
429   "*Integer that says how verbose Gnus should be.
430 The higher the number, the more messages Gnus will flash to say what
431 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
432 display most important messages; and at ten, Gnus will keep on
433 jabbering all the time.")
434
435 (defvar gnus-keep-same-level nil
436   "*Non-nil means that the next newsgroup after the current will be on the same level.
437 When you type, for instance, `n' after reading the last article in the
438 current newsgroup, you will go to the next newsgroup.  If this variable
439 is nil, the next newsgroup will be the next from the group
440 buffer.
441 If this variable is non-nil, Gnus will either put you in the
442 next newsgroup with the same level, or, if no such newsgroup is
443 available, the next newsgroup with the lowest possible level higher
444 than the current level.
445 If this variable is `best', Gnus will make the next newsgroup the one
446 with the best level.")
447
448 (defvar gnus-summary-make-false-root 'adopt
449   "*nil means that Gnus won't gather loose threads.
450 If the root of a thread has expired or been read in a previous
451 session, the information necessary to build a complete thread has been
452 lost.  Instead of having many small sub-threads from this original thread
453 scattered all over the summary buffer, Gnus can gather them.
454
455 If non-nil, Gnus will try to gather all loose sub-threads from an
456 original thread into one large thread.
457
458 If this variable is non-nil, it should be one of `none', `adopt',
459 `dummy' or `empty'.
460
461 If this variable is `none', Gnus will not make a false root, but just
462 present the sub-threads after another.
463 If this variable is `dummy', Gnus will create a dummy root that will
464 have all the sub-threads as children.
465 If this variable is `adopt', Gnus will make one of the \"children\"
466 the parent and mark all the step-children as such.
467 If this variable is `empty', the \"children\" are printed with empty
468 subject fields.  (Or rather, they will be printed with a string
469 given by the `gnus-summary-same-subject' variable.)")
470
471 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
472   "*A regexp to match subjects to be excluded from loose thread gathering.
473 As loose thread gathering is done on subjects only, that means that
474 there can be many false gatherings performed.  By rooting out certain
475 common subjects, gathering might become saner.")
476
477 (defvar gnus-summary-gather-subject-limit nil
478   "*Maximum length of subject comparisons when gathering loose threads.
479 Use nil to compare full subjects.  Setting this variable to a low
480 number will help gather threads that have been corrupted by
481 newsreaders chopping off subject lines, but it might also mean that
482 unrelated articles that have subject that happen to begin with the
483 same few characters will be incorrectly gathered.
484
485 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
486 comparing subjects.")
487
488 (defvar gnus-simplify-ignored-prefixes nil
489   "*Regexp, matches for which are removed from subject lines when simplifying.")
490
491 (defvar gnus-build-sparse-threads nil
492   "*If non-nil, fill in the gaps in threads.
493 If `some', only fill in the gaps that are needed to tie loose threads
494 together.  If `more', fill in all leaf nodes that Gnus can find.  If
495 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
496
497 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
498   "Function used for gathering loose threads.
499 There are two pre-defined functions: `gnus-gather-threads-by-subject',
500 which only takes Subjects into consideration; and
501 `gnus-gather-threads-by-references', which compared the References
502 headers of the articles to find matches.")
503
504 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
505 (defvar gnus-summary-same-subject ""
506   "*String indicating that the current article has the same subject as the previous.
507 This variable will only be used if the value of
508 `gnus-summary-make-false-root' is `empty'.")
509
510 (defvar gnus-summary-goto-unread t
511   "*If non-nil, marking commands will go to the next unread article.
512 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
513 whether it is read or not.")
514
515 (defvar gnus-group-goto-unread t
516   "*If non-nil, movement commands will go to the next unread and subscribed group.")
517
518 (defvar gnus-goto-next-group-when-activating t
519   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
520
521 (defvar gnus-check-new-newsgroups t
522   "*Non-nil means that Gnus will add new newsgroups at startup.
523 If this variable is `ask-server', Gnus will ask the server for new
524 groups since the last time it checked.  This means that the killed list
525 is no longer necessary, so you could set `gnus-save-killed-list' to
526 nil.
527
528 A variant is to have this variable be a list of select methods.  Gnus
529 will then use the `ask-server' method on all these select methods to
530 query for new groups from all those servers.
531
532 Eg.
533   (setq gnus-check-new-newsgroups
534         '((nntp \"some.server\") (nntp \"other.server\")))
535
536 If this variable is nil, then you have to tell Gnus explicitly to
537 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
538
539 (defvar gnus-check-bogus-newsgroups nil
540   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
541 If this variable is nil, then you have to tell Gnus explicitly to
542 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
543
544 (defvar gnus-read-active-file t
545   "*Non-nil means that Gnus will read the entire active file at startup.
546 If this variable is nil, Gnus will only know about the groups in your
547 `.newsrc' file.
548
549 If this variable is `some', Gnus will try to only read the relevant
550 parts of the active file from the server.  Not all servers support
551 this, and it might be quite slow with other servers, but this should
552 generally be faster than both the t and nil value.
553
554 If you set this variable to nil or `some', you probably still want to
555 be told about new newsgroups that arrive.  To do that, set
556 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
557 properly with all servers.")
558
559 (defvar gnus-level-subscribed 5
560   "*Groups with levels less than or equal to this variable are subscribed.")
561
562 (defvar gnus-level-unsubscribed 7
563   "*Groups with levels less than or equal to this variable are unsubscribed.
564 Groups with levels less than `gnus-level-subscribed', which should be
565 less than this variable, are subscribed.")
566
567 (defvar gnus-level-zombie 8
568   "*Groups with this level are zombie groups.")
569
570 (defvar gnus-level-killed 9
571   "*Groups with this level are killed.")
572
573 (defvar gnus-level-default-subscribed 3
574   "*New subscribed groups will be subscribed at this level.")
575
576 (defvar gnus-level-default-unsubscribed 6
577   "*New unsubscribed groups will be unsubscribed at this level.")
578
579 (defvar gnus-activate-level (1+ gnus-level-subscribed)
580   "*Groups higher than this level won't be activated on startup.
581 Setting this variable to something log might save lots of time when
582 you have many groups that you aren't interested in.")
583
584 (defvar gnus-activate-foreign-newsgroups 4
585   "*If nil, Gnus will not check foreign newsgroups at startup.
586 If it is non-nil, it should be a number between one and nine.  Foreign
587 newsgroups that have a level lower or equal to this number will be
588 activated on startup.  For instance, if you want to active all
589 subscribed newsgroups, but not the rest, you'd set this variable to
590 `gnus-level-subscribed'.
591
592 If you subscribe to lots of newsgroups from different servers, startup
593 might take a while.  By setting this variable to nil, you'll save time,
594 but you won't be told how many unread articles there are in the
595 groups.")
596
597 (defvar gnus-save-newsrc-file t
598   "*Non-nil means that Gnus will save the `.newsrc' file.
599 Gnus always saves its own startup file, which is called
600 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
601 be readily understood by other newsreaders.  If you don't plan on
602 using other newsreaders, set this variable to nil to save some time on
603 exit.")
604
605 (defvar gnus-save-killed-list t
606   "*If non-nil, save the list of killed groups to the startup file.
607 If you set this variable to nil, you'll save both time (when starting
608 and quitting) and space (both memory and disk), but it will also mean
609 that Gnus has no record of which groups are new and which are old, so
610 the automatic new newsgroups subscription methods become meaningless.
611
612 You should always set `gnus-check-new-newsgroups' to `ask-server' or
613 nil if you set this variable to nil.")
614
615 (defvar gnus-interactive-catchup t
616   "*If non-nil, require your confirmation when catching up a group.")
617
618 (defvar gnus-interactive-exit t
619   "*If non-nil, require your confirmation when exiting Gnus.")
620
621 (defvar gnus-kill-killed t
622   "*If non-nil, Gnus will apply kill files to already killed articles.
623 If it is nil, Gnus will never apply kill files to articles that have
624 already been through the scoring process, which might very well save lots
625 of time.")
626
627 (defvar gnus-extract-address-components 'gnus-extract-address-components
628   "*Function for extracting address components from a From header.
629 Two pre-defined function exist: `gnus-extract-address-components',
630 which is the default, quite fast, and too simplistic solution, and
631 `mail-extract-address-components', which works much better, but is
632 slower.")
633
634 (defvar gnus-summary-default-score 0
635   "*Default article score level.
636 If this variable is nil, scoring will be disabled.")
637
638 (defvar gnus-summary-zcore-fuzz 0
639   "*Fuzziness factor for the zcore in the summary buffer.
640 Articles with scores closer than this to `gnus-summary-default-score'
641 will not be marked.")
642
643 (defvar gnus-simplify-subject-fuzzy-regexp nil
644   "*Strings to be removed when doing fuzzy matches.
645 This can either be a regular expression or list of regular expressions
646 that will be removed from subject strings if fuzzy subject
647 simplification is selected.")
648
649 (defvar gnus-permanently-visible-groups nil
650   "*Regexp to match groups that should always be listed in the group buffer.
651 This means that they will still be listed when there are no unread
652 articles in the groups.")
653
654 (defvar gnus-list-groups-with-ticked-articles t
655   "*If non-nil, list groups that have only ticked articles.
656 If nil, only list groups that have unread articles.")
657
658 (defvar gnus-group-default-list-level gnus-level-subscribed
659   "*Default listing level.
660 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
661
662 (defvar gnus-group-use-permanent-levels nil
663   "*If non-nil, once you set a level, Gnus will use this level.")
664
665 (defvar gnus-group-list-inactive-groups t
666   "*If non-nil, inactive groups will be listed.")
667
668 (defvar gnus-show-mime nil
669   "*If non-nil, do mime processing of articles.
670 The articles will simply be fed to the function given by
671 `gnus-show-mime-method'.")
672
673 (defvar gnus-strict-mime t
674   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
675
676 (defvar gnus-show-mime-method 'metamail-buffer
677   "*Function to process a MIME message.
678 The function is called from the article buffer.")
679
680 (defvar gnus-decode-encoded-word-method (lambda ())
681   "*Function to decode a MIME encoded-words.
682 The function is called from the article buffer.")
683
684 (defvar gnus-show-threads t
685   "*If non-nil, display threads in summary mode.")
686
687 (defvar gnus-thread-hide-subtree nil
688   "*If non-nil, hide all threads initially.
689 If threads are hidden, you have to run the command
690 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
691 to expose hidden threads.")
692
693 (defvar gnus-thread-hide-killed t
694   "*If non-nil, hide killed threads automatically.")
695
696 (defvar gnus-thread-ignore-subject nil
697   "*If non-nil, ignore subjects and do all threading based on the Reference header.
698 If nil, which is the default, articles that have different subjects
699 from their parents will start separate threads.")
700
701 (defvar gnus-thread-operation-ignore-subject t
702   "*If non-nil, subjects will be ignored when doing thread commands.
703 This affects commands like `gnus-summary-kill-thread' and
704 `gnus-summary-lower-thread'.
705
706 If this variable is nil, articles in the same thread with different
707 subjects will not be included in the operation in question.  If this
708 variable is `fuzzy', only articles that have subjects that are fuzzily
709 equal will be included.")
710
711 (defvar gnus-thread-indent-level 4
712   "*Number that says how much each sub-thread should be indented.")
713
714 (defvar gnus-ignored-newsgroups
715   (purecopy (mapconcat 'identity
716                        '("^to\\."       ; not "real" groups
717                          "^[0-9. \t]+ " ; all digits in name
718                          "[][\"#'()]"   ; bogus characters
719                          )
720                        "\\|"))
721   "*A regexp to match uninteresting newsgroups in the active file.
722 Any lines in the active file matching this regular expression are
723 removed from the newsgroup list before anything else is done to it,
724 thus making them effectively non-existent.")
725
726 (defvar gnus-ignored-headers
727   "^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:"
728   "*All headers that match this regexp will be hidden.
729 This variable can also be a list of regexps of headers to be ignored.
730 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
731
732 (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-"
733   "*All headers that do not match this regexp will be hidden.
734 This variable can also be a list of regexp of headers to remain visible.
735 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
736
737 (defvar gnus-sorted-header-list
738   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
739     "^Cc:" "^Date:" "^Organization:")
740   "*This variable is a list of regular expressions.
741 If it is non-nil, headers that match the regular expressions will
742 be placed first in the article buffer in the sequence specified by
743 this list.")
744
745 (defvar gnus-boring-article-headers
746   '(empty followup-to reply-to)
747   "*Headers that are only to be displayed if they have interesting data.
748 Possible values in this list are `empty', `newsgroups', `followup-to',
749 `reply-to', and `date'.")
750
751 (defvar gnus-show-all-headers nil
752   "*If non-nil, don't hide any headers.")
753
754 (defvar gnus-save-all-headers t
755   "*If non-nil, don't remove any headers before saving.")
756
757 (defvar gnus-saved-headers gnus-visible-headers
758   "*Headers to keep if `gnus-save-all-headers' is nil.
759 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
760 If that variable is nil, however, all headers that match this regexp
761 will be kept while the rest will be deleted before saving.")
762
763 (defvar gnus-inhibit-startup-message nil
764   "*If non-nil, the startup message will not be displayed.")
765
766 (defvar gnus-signature-separator "^-- *$"
767   "Regexp matching signature separator.")
768
769 (defvar gnus-signature-limit nil
770   "Provide a limit to what is considered a signature.
771 If it is a number, no signature may not be longer (in characters) than
772 that number.  If it is a function, the function will be called without
773 any parameters, and if it returns nil, there is no signature in the
774 buffer.  If it is a string, it will be used as a regexp.  If it
775 matches, the text in question is not a signature.")
776
777 (defvar gnus-auto-extend-newsgroup t
778   "*If non-nil, extend newsgroup forward and backward when requested.")
779
780 (defvar gnus-auto-select-first t
781   "*If nil, don't select the first unread article when entering a group.
782 If this variable is `best', select the highest-scored unread article
783 in the group.  If neither nil nor `best', select the first unread
784 article.
785
786 If you want to prevent automatic selection of the first unread article
787 in some newsgroups, set the variable to nil in
788 `gnus-select-group-hook'.")
789
790 (defvar gnus-auto-select-next t
791   "*If non-nil, offer to go to the next group from the end of the previous.
792 If the value is t and the next newsgroup is empty, Gnus will exit
793 summary mode and go back to group mode.  If the value is neither nil
794 nor t, Gnus will select the following unread newsgroup.  In
795 particular, if the value is the symbol `quietly', the next unread
796 newsgroup will be selected without any confirmation, and if it is
797 `almost-quietly', the next group will be selected without any
798 confirmation if you are located on the last article in the group.
799 Finally, if this variable is `slightly-quietly', the `Z n' command
800 will go to the next group without confirmation.")
801
802 (defvar gnus-auto-select-same nil
803   "*If non-nil, select the next article with the same subject.")
804
805 (defvar gnus-summary-check-current nil
806   "*If non-nil, consider the current article when moving.
807 The \"unread\" movement commands will stay on the same line if the
808 current article is unread.")
809
810 (defvar gnus-auto-center-summary t
811   "*If non-nil, always center the current summary buffer.
812 In particular, if `vertical' do only vertical recentering.  If non-nil
813 and non-`vertical', do both horizontal and vertical recentering.")
814
815 (defvar gnus-break-pages t
816   "*If non-nil, do page breaking on articles.
817 The page delimiter is specified by the `gnus-page-delimiter'
818 variable.")
819
820 (defvar gnus-page-delimiter "^\^L"
821   "*Regexp describing what to use as article page delimiters.
822 The default value is \"^\^L\", which is a form linefeed at the
823 beginning of a line.")
824
825 (defvar gnus-use-full-window t
826   "*If non-nil, use the entire Emacs screen.")
827
828 (defvar gnus-window-configuration nil
829   "Obsolete variable.  See `gnus-buffer-configuration'.")
830
831 (defvar gnus-window-min-width 2
832   "*Minimum width of Gnus buffers.")
833
834 (defvar gnus-window-min-height 1
835   "*Minimum height of Gnus buffers.")
836
837 (defvar gnus-buffer-configuration
838   '((group
839      (vertical 1.0
840                (group 1.0 point)
841                (if gnus-carpal '(group-carpal 4))))
842     (summary
843      (vertical 1.0
844                (summary 1.0 point)
845                (if gnus-carpal '(summary-carpal 4))))
846     (article
847      (cond 
848       (gnus-use-picons
849        '(frame 1.0
850                (vertical 1.0
851                          (summary 0.25 point)
852                          (if gnus-carpal '(summary-carpal 4))
853                          (article 1.0))
854                (vertical ((height . 5) (width . 15)
855                           (user-position . t)
856                           (left . -1) (top . 1))
857                          (picons 1.0))))
858       (gnus-use-trees
859        '(vertical 1.0
860                   (summary 0.25 point)
861                   (tree 0.25)
862                   (article 1.0)))
863       (t
864        '(vertical 1.0
865                  (summary 0.25 point)
866                  (if gnus-carpal '(summary-carpal 4))
867                  (if gnus-use-trees '(tree 0.25))
868                  (article 1.0)))))
869     (server
870      (vertical 1.0
871                (server 1.0 point)
872                (if gnus-carpal '(server-carpal 2))))
873     (browse
874      (vertical 1.0
875                (browse 1.0 point)
876                (if gnus-carpal '(browse-carpal 2))))
877     (message
878      (vertical 1.0
879                (message 1.0 point)))
880     (pick
881      (vertical 1.0
882                (article 1.0 point)))
883     (info
884      (vertical 1.0
885                (info 1.0 point)))
886     (summary-faq
887      (vertical 1.0
888                (summary 0.25)
889                (faq 1.0 point)))
890     (edit-group
891      (vertical 1.0
892                (group 0.5)
893                (edit-group 1.0 point)))
894     (edit-server
895      (vertical 1.0
896                (server 0.5)
897                (edit-server 1.0 point)))
898     (edit-score
899      (vertical 1.0
900                (summary 0.25)
901                (edit-score 1.0 point)))
902     (post
903      (vertical 1.0
904                (post 1.0 point)))
905     (reply
906      (vertical 1.0
907                (article-copy 0.5)
908                (message 1.0 point)))
909     (forward
910      (vertical 1.0
911                (message 1.0 point)))
912     (reply-yank
913      (vertical 1.0
914                (message 1.0 point)))
915     (mail-bounce
916      (vertical 1.0
917                (article 0.5)
918                (message 1.0 point)))
919     (draft
920      (vertical 1.0
921                (draft 1.0 point)))
922     (pipe
923      (vertical 1.0
924                (summary 0.25 point)
925                (if gnus-carpal '(summary-carpal 4))
926                ("*Shell Command Output*" 1.0)))
927     (bug
928      (vertical 1.0
929                ("*Gnus Help Bug*" 0.5)
930                ("*Gnus Bug*" 1.0 point)))
931     (compose-bounce
932      (vertical 1.0
933                (article 0.5)
934                (message 1.0 point))))
935   "Window configuration for all possible Gnus buffers.
936 This variable is a list of lists.  Each of these lists has a NAME and
937 a RULE.  The NAMEs are commonsense names like `group', which names a
938 rule used when displaying the group buffer; `summary', which names a
939 rule for what happens when you enter a group and do not display an
940 article buffer; and so on.  See the value of this variable for a
941 complete list of NAMEs.
942
943 Each RULE is a list of vectors.  The first element in this vector is
944 the name of the buffer to be displayed; the second element is the
945 percentage of the screen this buffer is to occupy (a number in the
946 0.0-0.99 range); the optional third element is `point', which should
947 be present to denote which buffer point is to go to after making this
948 buffer configuration.")
949
950 (defvar gnus-window-to-buffer
951   '((group . gnus-group-buffer)
952     (summary . gnus-summary-buffer)
953     (article . gnus-article-buffer)
954     (server . gnus-server-buffer)
955     (browse . "*Gnus Browse Server*")
956     (edit-group . gnus-group-edit-buffer)
957     (edit-server . gnus-server-edit-buffer)
958     (group-carpal . gnus-carpal-group-buffer)
959     (summary-carpal . gnus-carpal-summary-buffer)
960     (server-carpal . gnus-carpal-server-buffer)
961     (browse-carpal . gnus-carpal-browse-buffer)
962     (edit-score . gnus-score-edit-buffer)
963     (message . gnus-message-buffer)
964     (mail . gnus-message-buffer)
965     (post-news . gnus-message-buffer)
966     (faq . gnus-faq-buffer)
967     (picons . "*Picons*")
968     (tree . gnus-tree-buffer)
969     (info . gnus-info-buffer)
970     (article-copy . gnus-article-copy)
971     (draft . gnus-draft-buffer))
972   "Mapping from short symbols to buffer names or buffer variables.")
973
974 (defvar gnus-carpal nil
975   "*If non-nil, display clickable icons.")
976
977 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
978   "*Function called with a group name when new group is detected.
979 A few pre-made functions are supplied: `gnus-subscribe-randomly'
980 inserts new groups at the beginning of the list of groups;
981 `gnus-subscribe-alphabetically' inserts new groups in strict
982 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
983 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
984 for your decision; `gnus-subscribe-killed' kills all new groups;
985 `gnus-subscribe-zombies' will make all new groups into zombies.")
986
987 ;; Suggested by a bug report by Hallvard B Furuseth.
988 ;; <h.b.furuseth@usit.uio.no>.
989 (defvar gnus-subscribe-options-newsgroup-method
990   (function gnus-subscribe-alphabetically)
991   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
992 If, for instance, you want to subscribe to all newsgroups in the
993 \"no\" and \"alt\" hierarchies, you'd put the following in your
994 .newsrc file:
995
996 options -n no.all alt.all
997
998 Gnus will the subscribe all new newsgroups in these hierarchies with
999 the subscription method in this variable.")
1000
1001 (defvar gnus-subscribe-hierarchical-interactive nil
1002   "*If non-nil, Gnus will offer to subscribe hierarchically.
1003 When a new hierarchy appears, Gnus will ask the user:
1004
1005 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
1006
1007 If the user pressed `d', Gnus will descend the hierarchy, `y' will
1008 subscribe to all newsgroups in the hierarchy and `s' will skip this
1009 hierarchy in its entirety.")
1010
1011 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1012   "*Function used for sorting the group buffer.
1013 This function will be called with group info entries as the arguments
1014 for the groups to be sorted.  Pre-made functions include
1015 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1016 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1017 `gnus-group-sort-by-rank'.
1018
1019 This variable can also be a list of sorting functions.  In that case,
1020 the most significant sort function should be the last function in the
1021 list.")
1022
1023 ;; Mark variables suggested by Thomas Michanek
1024 ;; <Thomas.Michanek@telelogic.se>.
1025 (defvar gnus-unread-mark ? 
1026   "*Mark used for unread articles.")
1027 (defvar gnus-ticked-mark ?!
1028   "*Mark used for ticked articles.")
1029 (defvar gnus-dormant-mark ??
1030   "*Mark used for dormant articles.")
1031 (defvar gnus-del-mark ?r
1032   "*Mark used for del'd articles.")
1033 (defvar gnus-read-mark ?R
1034   "*Mark used for read articles.")
1035 (defvar gnus-expirable-mark ?E
1036   "*Mark used for expirable articles.")
1037 (defvar gnus-killed-mark ?K
1038   "*Mark used for killed articles.")
1039 (defvar gnus-souped-mark ?F
1040   "*Mark used for killed articles.")
1041 (defvar gnus-kill-file-mark ?X
1042   "*Mark used for articles killed by kill files.")
1043 (defvar gnus-low-score-mark ?Y
1044   "*Mark used for articles with a low score.")
1045 (defvar gnus-catchup-mark ?C
1046   "*Mark used for articles that are caught up.")
1047 (defvar gnus-replied-mark ?A
1048   "*Mark used for articles that have been replied to.")
1049 (defvar gnus-cached-mark ?*
1050   "*Mark used for articles that are in the cache.")
1051 (defvar gnus-saved-mark ?S
1052   "*Mark used for articles that have been saved to.")
1053 (defvar gnus-process-mark ?#
1054   "*Process mark.")
1055 (defvar gnus-ancient-mark ?O
1056   "*Mark used for ancient articles.")
1057 (defvar gnus-sparse-mark ?Q
1058   "*Mark used for sparsely reffed articles.")
1059 (defvar gnus-canceled-mark ?G
1060   "*Mark used for canceled articles.")
1061 (defvar gnus-score-over-mark ?+
1062   "*Score mark used for articles with high scores.")
1063 (defvar gnus-score-below-mark ?-
1064   "*Score mark used for articles with low scores.")
1065 (defvar gnus-empty-thread-mark ? 
1066   "*There is no thread under the article.")
1067 (defvar gnus-not-empty-thread-mark ?=
1068   "*There is a thread under the article.")
1069
1070 (defvar gnus-view-pseudo-asynchronously nil
1071   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1072
1073 (defvar gnus-view-pseudos nil
1074   "*If `automatic', pseudo-articles will be viewed automatically.
1075 If `not-confirm', pseudos will be viewed automatically, and the user
1076 will not be asked to confirm the command.")
1077
1078 (defvar gnus-view-pseudos-separately t
1079   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1080 If nil, all files that use the same viewing command will be given as a
1081 list of parameters to that command.")
1082
1083 (defvar gnus-insert-pseudo-articles t
1084   "*If non-nil, insert pseudo-articles when decoding articles.")
1085
1086 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1087   "*Format of group lines.
1088 It works along the same lines as a normal formatting string,
1089 with some simple extensions.
1090
1091 %M    Only marked articles (character, \"*\" or \" \")
1092 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1093 %L    Level of subscribedness (integer)
1094 %N    Number of unread articles (integer)
1095 %I    Number of dormant articles (integer)
1096 %i    Number of ticked and dormant (integer)
1097 %T    Number of ticked articles (integer)
1098 %R    Number of read articles (integer)
1099 %t    Total number of articles (integer)
1100 %y    Number of unread, unticked articles (integer)
1101 %G    Group name (string)
1102 %g    Qualified group name (string)
1103 %D    Group description (string)
1104 %s    Select method (string)
1105 %o    Moderated group (char, \"m\")
1106 %p    Process mark (char)
1107 %O    Moderated group (string, \"(m)\" or \"\")
1108 %P    Topic indentation (string)
1109 %l    Whether there are GroupLens predictions for this group (string)
1110 %n    Select from where (string)
1111 %z    A string that look like `<%s:%n>' if a foreign select method is used
1112 %u    User defined specifier.  The next character in the format string should
1113       be a letter.  Gnus will call the function gnus-user-format-function-X,
1114       where X is the letter following %u.  The function will be passed the
1115       current header as argument.  The function should return a string, which
1116       will be inserted into the buffer just like information from any other
1117       group specifier.
1118
1119 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1120 the mouse point move inside the area.  There can only be one such area.
1121
1122 Note that this format specification is not always respected.  For
1123 reasons of efficiency, when listing killed groups, this specification
1124 is ignored altogether.  If the spec is changed considerably, your
1125 output may end up looking strange when listing both alive and killed
1126 groups.
1127
1128 If you use %o or %O, reading the active file will be slower and quite
1129 a bit of extra memory will be used. %D will also worsen performance.
1130 Also note that if you change the format specification to include any
1131 of these specs, you must probably re-start Gnus to see them go into
1132 effect.")
1133
1134 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1135   "*The format specification of the lines in the summary buffer.
1136
1137 It works along the same lines as a normal formatting string,
1138 with some simple extensions.
1139
1140 %N   Article number, left padded with spaces (string)
1141 %S   Subject (string)
1142 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1143 %n   Name of the poster (string)
1144 %a   Extracted name of the poster (string)
1145 %A   Extracted address of the poster (string)
1146 %F   Contents of the From: header (string)
1147 %x   Contents of the Xref: header (string)
1148 %D   Date of the article (string)
1149 %d   Date of the article (string) in DD-MMM format
1150 %M   Message-id of the article (string)
1151 %r   References of the article (string)
1152 %c   Number of characters in the article (integer)
1153 %L   Number of lines in the article (integer)
1154 %I   Indentation based on thread level (a string of spaces)
1155 %T   A string with two possible values: 80 spaces if the article
1156      is on thread level two or larger and 0 spaces on level one
1157 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1158 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1159 %[   Opening bracket (character, \"[\" or \"<\")
1160 %]   Closing bracket (character, \"]\" or \">\")
1161 %>   Spaces of length thread-level (string)
1162 %<   Spaces of length (- 20 thread-level) (string)
1163 %i   Article score (number)
1164 %z   Article zcore (character)
1165 %t   Number of articles under the current thread (number).
1166 %e   Whether the thread is empty or not (character).
1167 %l   GroupLens score (string).
1168 %u   User defined specifier.  The next character in the format string should
1169      be a letter.  Gnus will call the function gnus-user-format-function-X,
1170      where X is the letter following %u.  The function will be passed the
1171      current header as argument.  The function should return a string, which
1172      will be inserted into the summary just like information from any other
1173      summary specifier.
1174
1175 Text between %( and %) will be highlighted with `gnus-mouse-face'
1176 when the mouse point is placed inside the area.  There can only be one
1177 such area.
1178
1179 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1180 with care.  For reasons of efficiency, Gnus will compute what column
1181 these characters will end up in, and \"hard-code\" that.  This means that
1182 it is illegal to have these specs after a variable-length spec.  Well,
1183 you might not be arrested, but your summary buffer will look strange,
1184 which is bad enough.
1185
1186 The smart choice is to have these specs as for to the left as
1187 possible.
1188
1189 This restriction may disappear in later versions of Gnus.")
1190
1191 (defvar gnus-summary-dummy-line-format
1192   "*  %(:                          :%) %S\n"
1193   "*The format specification for the dummy roots in the summary buffer.
1194 It works along the same lines as a normal formatting string,
1195 with some simple extensions.
1196
1197 %S  The subject")
1198
1199 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1200   "*The format specification for the summary mode line.
1201 It works along the same lines as a normal formatting string,
1202 with some simple extensions:
1203
1204 %G  Group name
1205 %p  Unprefixed group name
1206 %A  Current article number
1207 %V  Gnus version
1208 %U  Number of unread articles in the group
1209 %e  Number of unselected articles in the group
1210 %Z  A string with unread/unselected article counts
1211 %g  Shortish group name
1212 %S  Subject of the current article
1213 %u  User-defined spec
1214 %s  Current score file name
1215 %d  Number of dormant articles
1216 %r  Number of articles that have been marked as read in this session
1217 %E  Number of articles expunged by the score files")
1218
1219 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1220   "*The format specification for the article mode line.
1221 See `gnus-summary-mode-line-format' for a closer description.")
1222
1223 (defvar gnus-group-mode-line-format "Gnus: %%b {%M%:%S}"
1224   "*The format specification for the group mode line.
1225 It works along the same lines as a normal formatting string,
1226 with some simple extensions:
1227
1228 %S   The native news server.
1229 %M   The native select method.
1230 %:   \":\" if %S isn't \"\".")
1231
1232 (defvar gnus-valid-select-methods
1233   '(("nntp" post address prompt-address)
1234     ("nnspool" post address)
1235     ("nnvirtual" post-mail virtual prompt-address)
1236     ("nnmbox" mail respool address)
1237     ("nnml" mail respool address)
1238     ("nnmh" mail respool address)
1239     ("nndir" post-mail prompt-address address)
1240     ("nneething" none address prompt-address)
1241     ("nndoc" none address prompt-address)
1242     ("nnbabyl" mail address respool)
1243     ("nnkiboze" post address virtual)
1244     ("nnsoup" post-mail address)
1245     ("nndraft" post-mail)
1246     ("nnfolder" mail respool address))
1247   "An alist of valid select methods.
1248 The first element of each list lists should be a string with the name
1249 of the select method.  The other elements may be be the category of
1250 this method (ie. `post', `mail', `none' or whatever) or other
1251 properties that this method has (like being respoolable).
1252 If you implement a new select method, all you should have to change is
1253 this variable.  I think.")
1254
1255 (defvar gnus-updated-mode-lines '(group article summary tree)
1256   "*List of buffers that should update their mode lines.
1257 The list may contain the symbols `group', `article' and `summary'.  If
1258 the corresponding symbol is present, Gnus will keep that mode line
1259 updated with information that may be pertinent.
1260 If this variable is nil, screen refresh may be quicker.")
1261
1262 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1263 (defvar gnus-mode-non-string-length nil
1264   "*Max length of mode-line non-string contents.
1265 If this is nil, Gnus will take space as is needed, leaving the rest
1266 of the modeline intact.")
1267
1268 ;see gnus-cus.el
1269 ;(defvar gnus-mouse-face 'highlight
1270 ;  "*Face used for mouse highlighting in Gnus.
1271 ;No mouse highlights will be done if `gnus-visual' is nil.")
1272
1273 (defvar gnus-summary-mark-below 0
1274   "*Mark all articles with a score below this variable as read.
1275 This variable is local to each summary buffer and usually set by the
1276 score file.")
1277
1278 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1279   "*List of functions used for sorting articles in the summary buffer.
1280 This variable is only used when not using a threaded display.")
1281
1282 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1283   "*List of functions used for sorting threads in the summary buffer.
1284 By default, threads are sorted by article number.
1285
1286 Each function takes two threads and return non-nil if the first thread
1287 should be sorted before the other.  If you use more than one function,
1288 the primary sort function should be the last.  You should probably
1289 always include `gnus-thread-sort-by-number' in the list of sorting
1290 functions -- preferably first.
1291
1292 Ready-mady functions include `gnus-thread-sort-by-number',
1293 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1294 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1295 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1296
1297 (defvar gnus-thread-score-function '+
1298   "*Function used for calculating the total score of a thread.
1299
1300 The function is called with the scores of the article and each
1301 subthread and should then return the score of the thread.
1302
1303 Some functions you can use are `+', `max', or `min'.")
1304
1305 (defvar gnus-summary-expunge-below nil
1306   "All articles that have a score less than this variable will be expunged.")
1307
1308 (defvar gnus-thread-expunge-below nil
1309   "All threads that have a total score less than this variable will be expunged.
1310 See `gnus-thread-score-function' for en explanation of what a
1311 \"thread score\" is.")
1312
1313 (defvar gnus-auto-subscribed-groups
1314   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1315   "*All new groups that match this regexp will be subscribed automatically.
1316 Note that this variable only deals with new groups.  It has no effect
1317 whatsoever on old groups.")
1318
1319 (defvar gnus-options-subscribe nil
1320   "*All new groups matching this regexp will be subscribed unconditionally.
1321 Note that this variable deals only with new newsgroups.  This variable
1322 does not affect old newsgroups.")
1323
1324 (defvar gnus-options-not-subscribe nil
1325   "*All new groups matching this regexp will be ignored.
1326 Note that this variable deals only with new newsgroups.  This variable
1327 does not affect old (already subscribed) newsgroups.")
1328
1329 (defvar gnus-auto-expirable-newsgroups nil
1330   "*Groups in which to automatically mark read articles as expirable.
1331 If non-nil, this should be a regexp that should match all groups in
1332 which to perform auto-expiry.  This only makes sense for mail groups.")
1333
1334 (defvar gnus-total-expirable-newsgroups nil
1335   "*Groups in which to perform expiry of all read articles.
1336 Use with extreme caution.  All groups that match this regexp will be
1337 expiring - which means that all read articles will be deleted after
1338 (say) one week.  (This only goes for mail groups and the like, of
1339 course.)")
1340
1341 (defvar gnus-group-uncollapsed-levels 1
1342   "Number of group name elements to leave alone when making a short group name.")
1343
1344 (defvar gnus-hidden-properties '(invisible t intangible t)
1345   "Property list to use for hiding text.")
1346
1347 (defvar gnus-modtime-botch nil
1348   "*Non-nil means .newsrc should be deleted prior to save.  
1349 Its use is due to the bogus appearance that .newsrc was modified on
1350 disc.")
1351
1352 ;; Hooks.
1353
1354 (defvar gnus-group-mode-hook nil
1355   "*A hook for Gnus group mode.")
1356
1357 (defvar gnus-summary-mode-hook nil
1358   "*A hook for Gnus summary mode.
1359 This hook is run before any variables are set in the summary buffer.")
1360
1361 (defvar gnus-article-mode-hook nil
1362   "*A hook for Gnus article mode.")
1363
1364 (defvar gnus-summary-prepare-exit-hook nil
1365   "*A hook called when preparing to exit from the summary buffer.
1366 It calls `gnus-summary-expire-articles' by default.")
1367 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1368
1369 (defvar gnus-summary-exit-hook nil
1370   "*A hook called on exit from the summary buffer.")
1371
1372 (defvar gnus-group-catchup-group-hook nil
1373   "*A hook run when catching up a group from the group buffer.")
1374
1375 (defvar gnus-group-update-group-hook nil
1376   "*A hook called when updating group lines.")
1377
1378 (defvar gnus-open-server-hook nil
1379   "*A hook called just before opening connection to the news server.")
1380
1381 (defvar gnus-load-hook nil
1382   "*A hook run while Gnus is loaded.")
1383
1384 (defvar gnus-startup-hook nil
1385   "*A hook called at startup.
1386 This hook is called after Gnus is connected to the NNTP server.")
1387
1388 (defvar gnus-get-new-news-hook nil
1389   "*A hook run just before Gnus checks for new news.")
1390
1391 (defvar gnus-after-getting-new-news-hook nil
1392   "*A hook run after Gnus checks for new news.")
1393
1394 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1395   "*A function that is called to generate the group buffer.
1396 The function is called with three arguments: The first is a number;
1397 all group with a level less or equal to that number should be listed,
1398 if the second is non-nil, empty groups should also be displayed.  If
1399 the third is non-nil, it is a number.  No groups with a level lower
1400 than this number should be displayed.
1401
1402 The only current function implemented is `gnus-group-prepare-flat'.")
1403
1404 (defvar gnus-group-prepare-hook nil
1405   "*A hook called after the group buffer has been generated.
1406 If you want to modify the group buffer, you can use this hook.")
1407
1408 (defvar gnus-summary-prepare-hook nil
1409   "*A hook called after the summary buffer has been generated.
1410 If you want to modify the summary buffer, you can use this hook.")
1411
1412 (defvar gnus-summary-generate-hook nil
1413   "*A hook run just before generating the summary buffer.
1414 This hook is commonly used to customize threading variables and the
1415 like.")
1416
1417 (defvar gnus-article-prepare-hook nil
1418   "*A hook called after an article has been prepared in the article buffer.
1419 If you want to run a special decoding program like nkf, use this hook.")
1420
1421 ;(defvar gnus-article-display-hook nil
1422 ;  "*A hook called after the article is displayed in the article buffer.
1423 ;The hook is designed to change the contents of the article
1424 ;buffer.  Typical functions that this hook may contain are
1425 ;`gnus-article-hide-headers' (hide selected headers),
1426 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1427 ;`gnus-article-hide-signature' (hide signature) and
1428 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1429 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1430 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1431 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1432
1433 (defvar gnus-article-x-face-too-ugly nil
1434   "Regexp matching posters whose face shouldn't be shown automatically.")
1435
1436 (defvar gnus-select-group-hook nil
1437   "*A hook called when a newsgroup is selected.
1438
1439 If you'd like to simplify subjects like the
1440 `gnus-summary-next-same-subject' command does, you can use the
1441 following hook:
1442
1443  (setq gnus-select-group-hook
1444       (list
1445         (lambda ()
1446           (mapcar (lambda (header)
1447                      (mail-header-set-subject
1448                       header
1449                       (gnus-simplify-subject
1450                        (mail-header-subject header) 're-only)))
1451                   gnus-newsgroup-headers))))")
1452
1453 (defvar gnus-select-article-hook nil
1454   "*A hook called when an article is selected.")
1455
1456 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1457   "*A hook called to apply kill files to a group.
1458 This hook is intended to apply a kill file to the selected newsgroup.
1459 The function `gnus-apply-kill-file' is called by default.
1460
1461 Since a general kill file is too heavy to use only for a few
1462 newsgroups, I recommend you to use a lighter hook function.  For
1463 example, if you'd like to apply a kill file to articles which contains
1464 a string `rmgroup' in subject in newsgroup `control', you can use the
1465 following hook:
1466
1467  (setq gnus-apply-kill-hook
1468       (list
1469         (lambda ()
1470           (cond ((string-match \"control\" gnus-newsgroup-name)
1471                  (gnus-kill \"Subject\" \"rmgroup\")
1472                  (gnus-expunge \"X\"))))))")
1473
1474 (defvar gnus-visual-mark-article-hook
1475   (list 'gnus-highlight-selected-summary)
1476   "*Hook run after selecting an article in the summary buffer.
1477 It is meant to be used for highlighting the article in some way.  It
1478 is not run if `gnus-visual' is nil.")
1479
1480 (defvar gnus-parse-headers-hook nil
1481   "*A hook called before parsing the headers.")
1482 (add-hook 'gnus-parse-headers-hook 'gnus-decode-rfc1522)
1483
1484 (defvar gnus-exit-group-hook nil
1485   "*A hook called when exiting (not quitting) summary mode.")
1486
1487 (defvar gnus-suspend-gnus-hook nil
1488   "*A hook called when suspending (not exiting) Gnus.")
1489
1490 (defvar gnus-exit-gnus-hook nil
1491   "*A hook called when exiting Gnus.")
1492
1493 (defvar gnus-after-exiting-gnus-hook nil
1494   "*A hook called after exiting Gnus.")
1495
1496 (defvar gnus-save-newsrc-hook nil
1497   "*A hook called before saving any of the newsrc files.")
1498
1499 (defvar gnus-save-quick-newsrc-hook nil
1500   "*A hook called just before saving the quick newsrc file.
1501 Can be used to turn version control on or off.")
1502
1503 (defvar gnus-save-standard-newsrc-hook nil
1504   "*A hook called just before saving the standard newsrc file.
1505 Can be used to turn version control on or off.")
1506
1507 (defvar gnus-summary-update-hook
1508   (list 'gnus-summary-highlight-line)
1509   "*A hook called when a summary line is changed.
1510 The hook will not be called if `gnus-visual' is nil.
1511
1512 The default function `gnus-summary-highlight-line' will
1513 highlight the line according to the `gnus-summary-highlight'
1514 variable.")
1515
1516 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1517   "*A hook called when a group line is changed.
1518 The hook will not be called if `gnus-visual' is nil.
1519
1520 The default function `gnus-group-highlight-line' will
1521 highlight the line according to the `gnus-group-highlight'
1522 variable.")
1523
1524 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1525   "*A hook called when an article is selected for the first time.
1526 The hook is intended to mark an article as read (or unread)
1527 automatically when it is selected.")
1528
1529 (defvar gnus-group-change-level-function nil
1530   "Function run when a group level is changed.
1531 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1532
1533 ;; Remove any hilit infestation.
1534 (add-hook 'gnus-startup-hook
1535           (lambda ()
1536             (remove-hook 'gnus-summary-prepare-hook
1537                          'hilit-rehighlight-buffer-quietly)
1538             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1539             (setq gnus-mark-article-hook
1540                   '(gnus-summary-mark-read-and-unread-as-read))
1541             (remove-hook 'gnus-article-prepare-hook
1542                          'hilit-rehighlight-buffer-quietly)))
1543
1544 \f
1545 ;; Internal variables
1546
1547 (defvar gnus-tree-buffer "*Tree*"
1548   "Buffer where Gnus thread trees are displayed.")
1549
1550 ;; Dummy variable.
1551 (defvar gnus-use-generic-from nil)
1552
1553 (defvar gnus-thread-indent-array nil)
1554 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1555
1556 (defvar gnus-newsrc-file-version nil)
1557
1558 (defvar gnus-method-history nil)
1559 ;; Variable holding the user answers to all method prompts.
1560
1561 (defvar gnus-group-history nil)
1562 ;; Variable holding the user answers to all group prompts.
1563
1564 (defvar gnus-server-alist nil
1565   "List of available servers.")
1566
1567 (defvar gnus-group-indentation-function nil)
1568
1569 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1570
1571 (defvar gnus-goto-missing-group-function nil)
1572
1573 (defvar gnus-override-subscribe-method nil)
1574
1575 (defvar gnus-group-goto-next-group-function nil
1576   "Function to override finding the next group after listing groups.")
1577
1578 (defconst gnus-article-mark-lists
1579   '((marked . tick) (replied . reply)
1580     (expirable . expire) (killed . killed)
1581     (bookmarks . bookmark) (dormant . dormant)
1582     (scored . score) (saved . save)
1583     (cached . cache)
1584     ))
1585
1586 ;; Avoid highlighting in kill files.
1587 (defvar gnus-summary-inhibit-highlight nil)
1588 (defvar gnus-newsgroup-selected-overlay nil)
1589
1590 (defvar gnus-inhibit-hiding nil)
1591 (defvar gnus-group-indentation "")
1592 (defvar gnus-inhibit-limiting nil)
1593 (defvar gnus-created-frames nil)
1594
1595 (defvar gnus-article-mode-map nil)
1596 (defvar gnus-dribble-buffer nil)
1597 (defvar gnus-headers-retrieved-by nil)
1598 (defvar gnus-article-reply nil)
1599 (defvar gnus-override-method nil)
1600 (defvar gnus-article-check-size nil)
1601
1602 (defvar gnus-current-score-file nil)
1603 (defvar gnus-newsgroup-adaptive-score-file nil)
1604 (defvar gnus-scores-exclude-files nil)
1605
1606 (defvar gnus-opened-servers nil)
1607
1608 (defvar gnus-current-move-group nil)
1609 (defvar gnus-current-copy-group nil)
1610 (defvar gnus-current-crosspost-group nil)
1611
1612 (defvar gnus-newsgroup-dependencies nil)
1613 (defvar gnus-newsgroup-async nil)
1614 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1615
1616 (defvar gnus-newsgroup-adaptive nil)
1617
1618 (defvar gnus-summary-display-table nil)
1619 (defvar gnus-summary-display-article-function nil)
1620
1621 (defvar gnus-summary-highlight-line-function nil
1622   "Function called after highlighting a summary line.")
1623
1624 (defvar gnus-group-line-format-alist
1625   `((?M gnus-tmp-marked-mark ?c)
1626     (?S gnus-tmp-subscribed ?c)
1627     (?L gnus-tmp-level ?d)
1628     (?N (cond ((eq number t) "*" )
1629               ((numberp number) 
1630                (int-to-string
1631                 (+ number
1632                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1633                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1634               (t number)) ?s)
1635     (?R gnus-tmp-number-of-read ?s)
1636     (?t gnus-tmp-number-total ?d)
1637     (?y gnus-tmp-number-of-unread ?s)
1638     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1639     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1640     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1641            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1642     (?g gnus-tmp-group ?s)
1643     (?G gnus-tmp-qualified-group ?s)
1644     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1645     (?D gnus-tmp-newsgroup-description ?s)
1646     (?o gnus-tmp-moderated ?c)
1647     (?O gnus-tmp-moderated-string ?s)
1648     (?p gnus-tmp-process-marked ?c)
1649     (?s gnus-tmp-news-server ?s)
1650     (?n gnus-tmp-news-method ?s)
1651     (?P gnus-group-indentation ?s)
1652     (?l gnus-tmp-grouplens ?s)
1653     (?z gnus-tmp-news-method-string ?s)
1654     (?u gnus-tmp-user-defined ?s)))
1655
1656 (defvar gnus-summary-line-format-alist
1657   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1658     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1659     (?s gnus-tmp-subject-or-nil ?s)
1660     (?n gnus-tmp-name ?s)
1661     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1662         ?s)
1663     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1664             gnus-tmp-from) ?s)
1665     (?F gnus-tmp-from ?s)
1666     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1667     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1668     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1669     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1670     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1671     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1672     (?L gnus-tmp-lines ?d)
1673     (?I gnus-tmp-indentation ?s)
1674     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1675     (?R gnus-tmp-replied ?c)
1676     (?\[ gnus-tmp-opening-bracket ?c)
1677     (?\] gnus-tmp-closing-bracket ?c)
1678     (?\> (make-string gnus-tmp-level ? ) ?s)
1679     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1680     (?i gnus-tmp-score ?d)
1681     (?z gnus-tmp-score-char ?c)
1682     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1683     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1684     (?U gnus-tmp-unread ?c)
1685     (?t (gnus-summary-number-of-articles-in-thread
1686          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1687         ?d)
1688     (?e (gnus-summary-number-of-articles-in-thread
1689          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1690         ?c)
1691     (?u gnus-tmp-user-defined ?s))
1692   "An alist of format specifications that can appear in summary lines,
1693 and what variables they correspond with, along with the type of the
1694 variable (string, integer, character, etc).")
1695
1696 (defvar gnus-summary-dummy-line-format-alist
1697   `((?S gnus-tmp-subject ?s)
1698     (?N gnus-tmp-number ?d)
1699     (?u gnus-tmp-user-defined ?s)))
1700
1701 (defvar gnus-summary-mode-line-format-alist
1702   `((?G gnus-tmp-group-name ?s)
1703     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1704     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1705     (?A gnus-tmp-article-number ?d)
1706     (?Z gnus-tmp-unread-and-unselected ?s)
1707     (?V gnus-version ?s)
1708     (?U gnus-tmp-unread ?d)
1709     (?S gnus-tmp-subject ?s)
1710     (?e gnus-tmp-unselected ?d)
1711     (?u gnus-tmp-user-defined ?s)
1712     (?d (length gnus-newsgroup-dormant) ?d)
1713     (?t (length gnus-newsgroup-marked) ?d)
1714     (?r (length gnus-newsgroup-reads) ?d)
1715     (?E gnus-newsgroup-expunged-tally ?d)
1716     (?s (gnus-current-score-file-nondirectory) ?s)))
1717
1718 (defvar gnus-article-mode-line-format-alist
1719   gnus-summary-mode-line-format-alist)
1720
1721 (defvar gnus-group-mode-line-format-alist
1722   `((?S gnus-tmp-news-server ?s)
1723     (?M gnus-tmp-news-method ?s)
1724     (?u gnus-tmp-user-defined ?s)
1725     (?: gnus-tmp-colon ?s)))
1726
1727 (defvar gnus-have-read-active-file nil)
1728
1729 (defconst gnus-maintainer
1730   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1731   "The mail address of the Gnus maintainers.")
1732
1733 (defconst gnus-version-number "5.2.17"
1734   "Version number for this version of Gnus.")
1735
1736 (defconst gnus-version (format "Gnus v%s" gnus-version-number)
1737   "Version string for this version of Gnus.")
1738
1739 (defvar gnus-info-nodes
1740   '((gnus-group-mode "(gnus)The Group Buffer")
1741     (gnus-summary-mode "(gnus)The Summary Buffer")
1742     (gnus-article-mode "(gnus)The Article Buffer"))
1743   "Alist of major modes and related Info nodes.")
1744
1745 (defvar gnus-group-buffer "*Group*")
1746 (defvar gnus-summary-buffer "*Summary*")
1747 (defvar gnus-article-buffer "*Article*")
1748 (defvar gnus-server-buffer "*Server*")
1749
1750 (defvar gnus-work-buffer " *gnus work*")
1751
1752 (defvar gnus-original-article-buffer " *Original Article*")
1753 (defvar gnus-original-article nil)
1754
1755 (defvar gnus-buffer-list nil
1756   "Gnus buffers that should be killed on exit.")
1757
1758 (defvar gnus-slave nil
1759   "Whether this Gnus is a slave or not.")
1760
1761 (defvar gnus-variable-list
1762   '(gnus-newsrc-options gnus-newsrc-options-n
1763     gnus-newsrc-last-checked-date
1764     gnus-newsrc-alist gnus-server-alist
1765     gnus-killed-list gnus-zombie-list
1766     gnus-topic-topology gnus-topic-alist
1767     gnus-format-specs)
1768   "Gnus variables saved in the quick startup file.")
1769
1770 (defvar gnus-newsrc-options nil
1771   "Options line in the .newsrc file.")
1772
1773 (defvar gnus-newsrc-options-n nil
1774   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1775
1776 (defvar gnus-newsrc-last-checked-date nil
1777   "Date Gnus last asked server for new newsgroups.")
1778
1779 (defvar gnus-topic-topology nil
1780   "The complete topic hierarchy.")
1781
1782 (defvar gnus-topic-alist nil
1783   "The complete topic-group alist.")
1784
1785 (defvar gnus-newsrc-alist nil
1786   "Assoc list of read articles.
1787 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1788
1789 (defvar gnus-newsrc-hashtb nil
1790   "Hashtable of gnus-newsrc-alist.")
1791
1792 (defvar gnus-killed-list nil
1793   "List of killed newsgroups.")
1794
1795 (defvar gnus-killed-hashtb nil
1796   "Hash table equivalent of gnus-killed-list.")
1797
1798 (defvar gnus-zombie-list nil
1799   "List of almost dead newsgroups.")
1800
1801 (defvar gnus-description-hashtb nil
1802   "Descriptions of newsgroups.")
1803
1804 (defvar gnus-list-of-killed-groups nil
1805   "List of newsgroups that have recently been killed by the user.")
1806
1807 (defvar gnus-active-hashtb nil
1808   "Hashtable of active articles.")
1809
1810 (defvar gnus-moderated-list nil
1811   "List of moderated newsgroups.")
1812
1813 (defvar gnus-group-marked nil)
1814
1815 (defvar gnus-current-startup-file nil
1816   "Startup file for the current host.")
1817
1818 (defvar gnus-last-search-regexp nil
1819   "Default regexp for article search command.")
1820
1821 (defvar gnus-last-shell-command nil
1822   "Default shell command on article.")
1823
1824 (defvar gnus-current-select-method nil
1825   "The current method for selecting a newsgroup.")
1826
1827 (defvar gnus-group-list-mode nil)
1828
1829 (defvar gnus-article-internal-prepare-hook nil)
1830
1831 (defvar gnus-newsgroup-name nil)
1832 (defvar gnus-newsgroup-begin nil)
1833 (defvar gnus-newsgroup-end nil)
1834 (defvar gnus-newsgroup-last-rmail nil)
1835 (defvar gnus-newsgroup-last-mail nil)
1836 (defvar gnus-newsgroup-last-folder nil)
1837 (defvar gnus-newsgroup-last-file nil)
1838 (defvar gnus-newsgroup-auto-expire nil)
1839 (defvar gnus-newsgroup-active nil)
1840
1841 (defvar gnus-newsgroup-data nil)
1842 (defvar gnus-newsgroup-data-reverse nil)
1843 (defvar gnus-newsgroup-limit nil)
1844 (defvar gnus-newsgroup-limits nil)
1845
1846 (defvar gnus-newsgroup-unreads nil
1847   "List of unread articles in the current newsgroup.")
1848
1849 (defvar gnus-newsgroup-unselected nil
1850   "List of unselected unread articles in the current newsgroup.")
1851
1852 (defvar gnus-newsgroup-reads nil
1853   "Alist of read articles and article marks in the current newsgroup.")
1854
1855 (defvar gnus-newsgroup-expunged-tally nil)
1856
1857 (defvar gnus-newsgroup-marked nil
1858   "List of ticked articles in the current newsgroup (a subset of unread art).")
1859
1860 (defvar gnus-newsgroup-killed nil
1861   "List of ranges of articles that have been through the scoring process.")
1862
1863 (defvar gnus-newsgroup-cached nil
1864   "List of articles that come from the article cache.")
1865
1866 (defvar gnus-newsgroup-saved nil
1867   "List of articles that have been saved.")
1868
1869 (defvar gnus-newsgroup-kill-headers nil)
1870
1871 (defvar gnus-newsgroup-replied nil
1872   "List of articles that have been replied to in the current newsgroup.")
1873
1874 (defvar gnus-newsgroup-expirable nil
1875   "List of articles in the current newsgroup that can be expired.")
1876
1877 (defvar gnus-newsgroup-processable nil
1878   "List of articles in the current newsgroup that can be processed.")
1879
1880 (defvar gnus-newsgroup-bookmarks nil
1881   "List of articles in the current newsgroup that have bookmarks.")
1882
1883 (defvar gnus-newsgroup-dormant nil
1884   "List of dormant articles in the current newsgroup.")
1885
1886 (defvar gnus-newsgroup-scored nil
1887   "List of scored articles in the current newsgroup.")
1888
1889 (defvar gnus-newsgroup-headers nil
1890   "List of article headers in the current newsgroup.")
1891
1892 (defvar gnus-newsgroup-threads nil)
1893
1894 (defvar gnus-newsgroup-prepared nil
1895   "Whether the current group has been prepared properly.")
1896
1897 (defvar gnus-newsgroup-ancient nil
1898   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1899
1900 (defvar gnus-newsgroup-sparse nil)
1901
1902 (defvar gnus-current-article nil)
1903 (defvar gnus-article-current nil)
1904 (defvar gnus-current-headers nil)
1905 (defvar gnus-have-all-headers nil)
1906 (defvar gnus-last-article nil)
1907 (defvar gnus-newsgroup-history nil)
1908 (defvar gnus-current-kill-article nil)
1909
1910 ;; Save window configuration.
1911 (defvar gnus-prev-winconf nil)
1912
1913 (defvar gnus-summary-mark-positions nil)
1914 (defvar gnus-group-mark-positions nil)
1915
1916 (defvar gnus-reffed-article-number nil)
1917
1918 ;;; Let the byte-compiler know that we know about this variable.
1919 (defvar rmail-default-rmail-file)
1920
1921 (defvar gnus-cache-removable-articles nil)
1922
1923 (defvar gnus-dead-summary nil)
1924
1925 (defconst gnus-summary-local-variables
1926   '(gnus-newsgroup-name
1927     gnus-newsgroup-begin gnus-newsgroup-end
1928     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1929     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1930     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1931     gnus-newsgroup-unselected gnus-newsgroup-marked
1932     gnus-newsgroup-reads gnus-newsgroup-saved
1933     gnus-newsgroup-replied gnus-newsgroup-expirable
1934     gnus-newsgroup-processable gnus-newsgroup-killed
1935     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1936     gnus-newsgroup-headers gnus-newsgroup-threads
1937     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1938     gnus-current-article gnus-current-headers gnus-have-all-headers
1939     gnus-last-article gnus-article-internal-prepare-hook
1940     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1941     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1942     gnus-newsgroup-async gnus-thread-expunge-below
1943     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1944     (gnus-summary-mark-below . global)
1945     gnus-newsgroup-active gnus-scores-exclude-files
1946     gnus-newsgroup-history gnus-newsgroup-ancient
1947     gnus-newsgroup-sparse
1948     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1949     gnus-newsgroup-adaptive-score-file
1950     (gnus-newsgroup-expunged-tally . 0)
1951     gnus-cache-removable-articles gnus-newsgroup-cached
1952     gnus-newsgroup-data gnus-newsgroup-data-reverse
1953     gnus-newsgroup-limit gnus-newsgroup-limits)
1954   "Variables that are buffer-local to the summary buffers.")
1955
1956 (defconst gnus-bug-message
1957   "Sending a bug report to the Gnus Towers.
1958 ========================================
1959
1960 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1961 be sent to the Gnus Bug Exterminators.
1962
1963 At the bottom of the buffer you'll see lots of variable settings.
1964 Please do not delete those.  They will tell the Bug People what your
1965 environment is, so that it will be easier to locate the bugs.
1966
1967 If you have found a bug that makes Emacs go \"beep\", set
1968 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1969 and include the backtrace in your bug report.
1970
1971 Please describe the bug in annoying, painstaking detail.
1972
1973 Thank you for your help in stamping out bugs.
1974 ")
1975
1976 ;;; End of variables.
1977
1978 ;; Define some autoload functions Gnus might use.
1979 (eval-and-compile
1980
1981   ;; This little mapcar goes through the list below and marks the
1982   ;; symbols in question as autoloaded functions.
1983   (mapcar
1984    (lambda (package)
1985      (let ((interactive (nth 1 (memq ':interactive package))))
1986        (mapcar
1987         (lambda (function)
1988           (let (keymap)
1989             (when (consp function)
1990               (setq keymap (car (memq 'keymap function)))
1991               (setq function (car function)))
1992             (autoload function (car package) nil interactive keymap)))
1993         (if (eq (nth 1 package) ':interactive)
1994             (cdddr package)
1995           (cdr package)))))
1996    '(("metamail" metamail-buffer)
1997      ("info" Info-goto-node)
1998      ("hexl" hexl-hex-string-to-integer)
1999      ("pp" pp pp-to-string pp-eval-expression)
2000      ("mail-extr" mail-extract-address-components)
2001      ("nnmail" nnmail-split-fancy nnmail-article-group)
2002      ("nnvirtual" nnvirtual-catchup-group)
2003      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
2004       timezone-make-sortable-date timezone-make-time-string)
2005      ("rmailout" rmail-output)
2006      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
2007       rmail-show-message)
2008      ("gnus-soup" :interactive t
2009       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
2010       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
2011      ("nnsoup" nnsoup-pack-replies)
2012      ("gnus-scomo" :interactive t gnus-score-mode)
2013      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2014       gnus-Folder-save-name gnus-folder-save-name)
2015      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2016      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2017       gnus-server-make-menu-bar gnus-article-make-menu-bar
2018       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2019       gnus-summary-highlight-line gnus-carpal-setup-buffer
2020       gnus-group-highlight-line
2021       gnus-article-add-button gnus-insert-next-page-button
2022       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2023      ("gnus-vis" :interactive t
2024       gnus-article-push-button gnus-article-press-button
2025       gnus-article-highlight gnus-article-highlight-some
2026       gnus-article-highlight-headers gnus-article-highlight-signature
2027       gnus-article-add-buttons gnus-article-add-buttons-to-head
2028       gnus-article-next-button gnus-article-prev-button)
2029      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2030       gnus-demon-add-disconnection gnus-demon-add-handler
2031       gnus-demon-remove-handler)
2032      ("gnus-demon" :interactive t
2033       gnus-demon-init gnus-demon-cancel)
2034      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2035       gnus-tree-open gnus-tree-close)
2036      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2037       gnus-nocem-unwanted-article-p)
2038      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2039      ("gnus-srvr" gnus-browse-foreign-server)
2040      ("gnus-cite" :interactive t
2041       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2042       gnus-article-hide-citation gnus-article-fill-cited-article
2043       gnus-article-hide-citation-in-followups)
2044      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2045       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2046       gnus-execute gnus-expunge)
2047      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2048       gnus-cache-possibly-remove-articles gnus-cache-request-article
2049       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2050       gnus-cache-enter-remove-article gnus-cached-article-p
2051       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2052      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2053       gnus-cache-remove-article)
2054      ("gnus-score" :interactive t
2055       gnus-summary-increase-score gnus-summary-lower-score
2056       gnus-score-flush-cache gnus-score-close
2057       gnus-score-raise-same-subject-and-select
2058       gnus-score-raise-same-subject gnus-score-default
2059       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2060       gnus-score-lower-same-subject gnus-score-lower-thread
2061       gnus-possibly-score-headers gnus-summary-raise-score 
2062       gnus-summary-set-score gnus-summary-current-score)
2063      ("gnus-score"
2064       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2065       gnus-current-score-file-nondirectory gnus-score-adaptive
2066       gnus-score-find-trace gnus-score-file-name)
2067      ("gnus-edit" :interactive t gnus-score-customize)
2068      ("gnus-topic" :interactive t gnus-topic-mode)
2069      ("gnus-topic" gnus-topic-remove-group)
2070      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2071      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2072      ("gnus-uu" :interactive t
2073       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2074       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2075       gnus-uu-mark-by-regexp gnus-uu-mark-all
2076       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2077       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2078       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2079       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2080       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2081       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2082       gnus-uu-decode-binhex-view)
2083      ("gnus-msg" (gnus-summary-send-map keymap)
2084       gnus-mail-yank-original gnus-mail-send-and-exit
2085       gnus-article-mail gnus-new-mail gnus-mail-reply)
2086      ("gnus-msg" :interactive t
2087       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2088       gnus-summary-followup gnus-summary-followup-with-original
2089       gnus-summary-cancel-article gnus-summary-supersede-article
2090       gnus-post-news gnus-inews-news 
2091       gnus-summary-reply gnus-summary-reply-with-original
2092       gnus-summary-mail-forward gnus-summary-mail-other-window
2093       gnus-bug)
2094      ("gnus-picon" :interactive t gnus-article-display-picons
2095       gnus-group-display-picons gnus-picons-article-display-x-face)
2096      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2097       gnus-grouplens-mode)
2098      ("gnus-vm" gnus-vm-mail-setup)
2099      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2100       gnus-summary-save-article-vm))))
2101
2102 \f
2103
2104 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2105 ;; If you want the cursor to go somewhere else, set these two
2106 ;; functions in some startup hook to whatever you want.
2107 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2108 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2109
2110 ;;; Various macros and substs.
2111
2112 (defun gnus-header-from (header)
2113   (mail-header-from header))
2114
2115 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2116   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2117   (let ((tempvar (make-symbol "GnusStartBufferWindow"))
2118         (w (make-symbol "w"))
2119         (buf (make-symbol "buf")))
2120     `(let* ((,tempvar (selected-window))
2121             (,buf ,buffer)
2122             (,w (get-buffer-window ,buf 'visible)))
2123        (unwind-protect
2124            (progn
2125              (if ,w
2126                  (select-window ,w)
2127                (pop-to-buffer ,buf))
2128              ,@forms)
2129          (select-window ,tempvar)))))
2130
2131 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
2132 (put 'gnus-eval-in-buffer-window 'lisp-indent-hook 1)
2133 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
2134
2135 (defmacro gnus-gethash (string hashtable)
2136   "Get hash value of STRING in HASHTABLE."
2137   `(symbol-value (intern-soft ,string ,hashtable)))
2138
2139 (defmacro gnus-sethash (string value hashtable)
2140   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2141   `(set (intern ,string ,hashtable) ,value))
2142
2143 (defmacro gnus-intern-safe (string hashtable)
2144   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2145   `(let ((symbol (intern ,string ,hashtable)))
2146      (or (boundp symbol)
2147          (set symbol nil))
2148      symbol))
2149
2150 (defmacro gnus-group-unread (group)
2151   "Get the currently computed number of unread articles in GROUP."
2152   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2153
2154 (defmacro gnus-group-entry (group)
2155   "Get the newsrc entry for GROUP."
2156   `(gnus-gethash ,group gnus-newsrc-hashtb))
2157
2158 (defmacro gnus-active (group)
2159   "Get active info on GROUP."
2160   `(gnus-gethash ,group gnus-active-hashtb))
2161
2162 (defmacro gnus-set-active (group active)
2163   "Set GROUP's active info."
2164   `(gnus-sethash ,group ,active gnus-active-hashtb))
2165
2166 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2167 ;;   function `substring' might cut on a middle of multi-octet
2168 ;;   character.
2169 (defun gnus-truncate-string (str width)
2170   (substring str 0 width))
2171
2172 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2173 ;; to limit the length of a string.  This function is necessary since
2174 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2175 (defsubst gnus-limit-string (str width)
2176   (if (> (length str) width)
2177       (substring str 0 width)
2178     str))
2179
2180 (defsubst gnus-simplify-subject-re (subject)
2181   "Remove \"Re:\" from subject lines."
2182   (if (string-match "^[Rr][Ee]: *" subject)
2183       (substring subject (match-end 0))
2184     subject))
2185
2186 (defsubst gnus-functionp (form)
2187   "Return non-nil if FORM is funcallable."
2188   (or (and (symbolp form) (fboundp form))
2189       (and (listp form) (eq (car form) 'lambda))))
2190
2191 (defsubst gnus-goto-char (point)
2192   (and point (goto-char point)))
2193
2194 (defmacro gnus-buffer-exists-p (buffer)
2195   `(let ((buffer ,buffer))
2196      (and buffer
2197           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2198                    buffer))))
2199
2200 (defmacro gnus-kill-buffer (buffer)
2201   `(let ((buf ,buffer))
2202      (if (gnus-buffer-exists-p buf)
2203          (kill-buffer buf))))
2204
2205 (defsubst gnus-point-at-bol ()
2206   "Return point at the beginning of the line."
2207   (let ((p (point)))
2208     (beginning-of-line)
2209     (prog1
2210         (point)
2211       (goto-char p))))
2212
2213 (defsubst gnus-point-at-eol ()
2214   "Return point at the end of the line."
2215   (let ((p (point)))
2216     (end-of-line)
2217     (prog1
2218         (point)
2219       (goto-char p))))
2220
2221 (defun gnus-alive-p ()
2222   "Say whether Gnus is running or not."
2223   (and gnus-group-buffer
2224        (get-buffer gnus-group-buffer)))
2225
2226 (defun gnus-delete-first (elt list)
2227   "Delete by side effect the first occurrence of ELT as a member of LIST."
2228   (if (equal (car list) elt)
2229       (cdr list)
2230     (let ((total list))
2231       (while (and (cdr list)
2232                   (not (equal (cadr list) elt)))
2233         (setq list (cdr list)))
2234       (when (cdr list)
2235         (setcdr list (cddr list)))
2236       total)))
2237
2238 ;; Delete the current line (and the next N lines.);
2239 (defmacro gnus-delete-line (&optional n)
2240   `(delete-region (progn (beginning-of-line) (point))
2241                   (progn (forward-line ,(or n 1)) (point))))
2242
2243 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2244 (defvar gnus-init-inhibit nil)
2245 (defun gnus-read-init-file (&optional inhibit-next)
2246   (if gnus-init-inhibit
2247       (setq gnus-init-inhibit nil)
2248     (setq gnus-init-inhibit inhibit-next)
2249     (and gnus-init-file
2250          (or (and (file-exists-p gnus-init-file)
2251                   ;; Don't try to load a directory.
2252                   (not (file-directory-p gnus-init-file)))
2253              (file-exists-p (concat gnus-init-file ".el"))
2254              (file-exists-p (concat gnus-init-file ".elc")))
2255          (condition-case var
2256              (load gnus-init-file nil t)
2257            (error
2258             (error "Error in %s: %s" gnus-init-file var))))))
2259
2260 ;; Info access macros.
2261
2262 (defmacro gnus-info-group (info)
2263   `(nth 0 ,info))
2264 (defmacro gnus-info-rank (info)
2265   `(nth 1 ,info))
2266 (defmacro gnus-info-read (info)
2267   `(nth 2 ,info))
2268 (defmacro gnus-info-marks (info)
2269   `(nth 3 ,info))
2270 (defmacro gnus-info-method (info)
2271   `(nth 4 ,info))
2272 (defmacro gnus-info-params (info)
2273   `(nth 5 ,info))
2274
2275 (defmacro gnus-info-level (info)
2276   `(let ((rank (gnus-info-rank ,info)))
2277      (if (consp rank)
2278          (car rank)
2279        rank)))
2280 (defmacro gnus-info-score (info)
2281   `(let ((rank (gnus-info-rank ,info)))
2282      (or (and (consp rank) (cdr rank)) 0)))
2283
2284 (defmacro gnus-info-set-group (info group)
2285   `(setcar ,info ,group))
2286 (defmacro gnus-info-set-rank (info rank)
2287   `(setcar (nthcdr 1 ,info) ,rank))
2288 (defmacro gnus-info-set-read (info read)
2289   `(setcar (nthcdr 2 ,info) ,read))
2290 (defmacro gnus-info-set-marks (info marks)
2291   `(setcar (nthcdr 3 ,info) ,marks))
2292 (defmacro gnus-info-set-method (info method)
2293   `(setcar (nthcdr 4 ,info) ,method))
2294 (defmacro gnus-info-set-params (info params)
2295   `(setcar (nthcdr 5 ,info) ,params))
2296
2297 (defmacro gnus-info-set-level (info level)
2298   `(let ((rank (cdr ,info)))
2299      (if (consp (car rank))
2300          (setcar (car rank) ,level)
2301        (setcar rank ,level))))
2302 (defmacro gnus-info-set-score (info score)
2303   `(let ((rank (cdr ,info)))
2304      (if (consp (car rank))
2305          (setcdr (car rank) ,score)
2306        (setcar rank (cons (car rank) ,score)))))
2307
2308 (defmacro gnus-get-info (group)
2309   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2310
2311 (defun gnus-byte-code (func)
2312   "Return a form that can be `eval'ed based on FUNC."
2313   (let ((fval (symbol-function func)))
2314     (if (byte-code-function-p fval)
2315         (let ((flist (append fval nil)))
2316           (setcar flist 'byte-code)
2317           flist)
2318       (cons 'progn (cddr fval)))))
2319
2320 ;; Find out whether the gnus-visual TYPE is wanted.
2321 (defun gnus-visual-p (&optional type class)
2322   (and gnus-visual                      ; Has to be non-nil, at least.
2323        (if (not type)                   ; We don't care about type.
2324            gnus-visual
2325          (if (listp gnus-visual)        ; It's a list, so we check it.
2326              (or (memq type gnus-visual)
2327                  (memq class gnus-visual))
2328            t))))
2329
2330 ;;; Load the compatability functions.
2331
2332 (require 'gnus-cus)
2333 (require 'gnus-ems)
2334
2335 \f
2336 ;;;
2337 ;;; Shutdown
2338 ;;;
2339
2340 (defvar gnus-shutdown-alist nil)
2341
2342 (defun gnus-add-shutdown (function &rest symbols)
2343   "Run FUNCTION whenever one of SYMBOLS is shut down."
2344   (push (cons function symbols) gnus-shutdown-alist))
2345
2346 (defun gnus-shutdown (symbol)
2347   "Shut down everything that waits for SYMBOL."
2348   (let ((alist gnus-shutdown-alist)
2349         entry)
2350     (while (setq entry (pop alist))
2351       (when (memq symbol (cdr entry))
2352         (funcall (car entry))))))
2353
2354 \f
2355
2356 ;; Format specs.  The chunks below are the machine-generated forms
2357 ;; that are to be evaled as the result of the default format strings.
2358 ;; We write them in here to get them byte-compiled.  That way the
2359 ;; default actions will be quite fast, while still retaining the full
2360 ;; flexibility of the user-defined format specs.
2361
2362 ;; First we have lots of dummy defvars to let the compiler know these
2363 ;; are really dynamic variables.
2364
2365 (defvar gnus-tmp-unread)
2366 (defvar gnus-tmp-replied)
2367 (defvar gnus-tmp-score-char)
2368 (defvar gnus-tmp-indentation)
2369 (defvar gnus-tmp-opening-bracket)
2370 (defvar gnus-tmp-lines)
2371 (defvar gnus-tmp-name)
2372 (defvar gnus-tmp-closing-bracket)
2373 (defvar gnus-tmp-subject-or-nil)
2374 (defvar gnus-tmp-subject)
2375 (defvar gnus-tmp-marked)
2376 (defvar gnus-tmp-marked-mark)
2377 (defvar gnus-tmp-subscribed)
2378 (defvar gnus-tmp-process-marked)
2379 (defvar gnus-tmp-number-of-unread)
2380 (defvar gnus-tmp-group-name)
2381 (defvar gnus-tmp-group)
2382 (defvar gnus-tmp-article-number)
2383 (defvar gnus-tmp-unread-and-unselected)
2384 (defvar gnus-tmp-news-method)
2385 (defvar gnus-tmp-news-server)
2386 (defvar gnus-tmp-article-number)
2387 (defvar gnus-mouse-face)
2388 (defvar gnus-mouse-face-prop)
2389
2390 (defun gnus-summary-line-format-spec ()
2391   (insert gnus-tmp-unread gnus-tmp-replied
2392           gnus-tmp-score-char gnus-tmp-indentation)
2393   (gnus-put-text-property
2394    (point)
2395    (progn
2396      (insert
2397       gnus-tmp-opening-bracket
2398       (format "%4d: %-20s"
2399               gnus-tmp-lines
2400               (if (> (length gnus-tmp-name) 20)
2401                   (substring gnus-tmp-name 0 20)
2402                 gnus-tmp-name))
2403       gnus-tmp-closing-bracket)
2404      (point))
2405    gnus-mouse-face-prop gnus-mouse-face)
2406   (insert " " gnus-tmp-subject-or-nil "\n"))
2407
2408 (defvar gnus-summary-line-format-spec
2409   (gnus-byte-code 'gnus-summary-line-format-spec))
2410
2411 (defun gnus-summary-dummy-line-format-spec ()
2412   (insert "*  ")
2413   (gnus-put-text-property
2414    (point)
2415    (progn
2416      (insert ":                          :")
2417      (point))
2418    gnus-mouse-face-prop gnus-mouse-face)
2419   (insert " " gnus-tmp-subject "\n"))
2420
2421 (defvar gnus-summary-dummy-line-format-spec
2422   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2423
2424 (defun gnus-group-line-format-spec ()
2425   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2426           gnus-tmp-process-marked
2427           gnus-group-indentation
2428           (format "%5s: " gnus-tmp-number-of-unread))
2429   (gnus-put-text-property
2430    (point)
2431    (progn
2432      (insert gnus-tmp-group "\n")
2433      (1- (point)))
2434    gnus-mouse-face-prop gnus-mouse-face))
2435 (defvar gnus-group-line-format-spec
2436   (gnus-byte-code 'gnus-group-line-format-spec))
2437
2438 (defvar gnus-format-specs
2439   `((version . ,emacs-version)
2440     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2441     (summary-dummy ,gnus-summary-dummy-line-format
2442                    ,gnus-summary-dummy-line-format-spec)
2443     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2444
2445 (defvar gnus-article-mode-line-format-spec nil)
2446 (defvar gnus-summary-mode-line-format-spec nil)
2447 (defvar gnus-group-mode-line-format-spec nil)
2448
2449 ;;; Phew.  All that gruft is over, fortunately.
2450
2451 \f
2452 ;;;
2453 ;;; Gnus Utility Functions
2454 ;;;
2455
2456 (defun gnus-extract-address-components (from)
2457   (let (name address)
2458     ;; First find the address - the thing with the @ in it.  This may
2459     ;; not be accurate in mail addresses, but does the trick most of
2460     ;; the time in news messages.
2461     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2462         (setq address (substring from (match-beginning 0) (match-end 0))))
2463     ;; Then we check whether the "name <address>" format is used.
2464     (and address
2465          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2466          ;; Linear white space is not required.
2467          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2468          (and (setq name (substring from 0 (match-beginning 0)))
2469               ;; Strip any quotes from the name.
2470               (string-match "\".*\"" name)
2471               (setq name (substring name 1 (1- (match-end 0))))))
2472     ;; If not, then "address (name)" is used.
2473     (or name
2474         (and (string-match "(.+)" from)
2475              (setq name (substring from (1+ (match-beginning 0))
2476                                    (1- (match-end 0)))))
2477         (and (string-match "()" from)
2478              (setq name address))
2479         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2480         ;; XOVER might not support folded From headers.
2481         (and (string-match "(.*" from)
2482              (setq name (substring from (1+ (match-beginning 0))
2483                                    (match-end 0)))))
2484     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2485     (list (or name from) (or address from))))
2486
2487 (defun gnus-fetch-field (field)
2488   "Return the value of the header FIELD of current article."
2489   (save-excursion
2490     (save-restriction
2491       (let ((case-fold-search t)
2492             (inhibit-point-motion-hooks t))
2493         (nnheader-narrow-to-headers)
2494         (message-fetch-field field)))))
2495
2496 (defun gnus-goto-colon ()
2497   (beginning-of-line)
2498   (search-forward ":" (gnus-point-at-eol) t))
2499
2500 ;;;###autoload
2501 (defun gnus-update-format (var)
2502   "Update the format specification near point."
2503   (interactive
2504    (list
2505     (save-excursion
2506       (eval-defun nil)
2507       ;; Find the end of the current word.
2508       (re-search-forward "[ \t\n]" nil t)
2509       ;; Search backward.
2510       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2511         (match-string 1)))))
2512   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2513                               (match-string 1 var))))
2514          (entry (assq type gnus-format-specs))
2515          value spec)
2516     (when entry
2517       (setq gnus-format-specs (delq entry gnus-format-specs)))
2518     (set
2519      (intern (format "%s-spec" var))
2520      (gnus-parse-format (setq value (symbol-value (intern var)))
2521                         (symbol-value (intern (format "%s-alist" var)))
2522                         (not (string-match "mode" var))))
2523     (setq spec (symbol-value (intern (format "%s-spec" var))))
2524     (push (list type value spec) gnus-format-specs)
2525
2526     (pop-to-buffer "*Gnus Format*")
2527     (erase-buffer)
2528     (lisp-interaction-mode)
2529     (insert (pp-to-string spec))))
2530
2531 (defun gnus-update-format-specifications (&optional force)
2532   "Update all (necessary) format specifications."
2533   ;; Make the indentation array.
2534   (gnus-make-thread-indent-array)
2535
2536   ;; See whether all the stored info needs to be flushed.
2537   (when (or force
2538             (not (equal emacs-version
2539                         (cdr (assq 'version gnus-format-specs)))))
2540     (setq gnus-format-specs nil))
2541
2542   ;; Go through all the formats and see whether they need updating.
2543   (let ((types '(summary summary-dummy group
2544                          summary-mode group-mode article-mode))
2545         new-format entry type val)
2546     (while (setq type (pop types))
2547       ;; Jump to the proper buffer to find out the value of
2548       ;; the variable, if possible.  (It may be buffer-local.)
2549       (save-excursion
2550         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2551               val)
2552           (when (and (boundp buffer)
2553                      (setq val (symbol-value buffer))
2554                      (get-buffer val)
2555                      (buffer-name (get-buffer val)))
2556             (set-buffer (get-buffer val)))
2557           (setq new-format (symbol-value
2558                             (intern (format "gnus-%s-line-format" type))))))
2559       (setq entry (cdr (assq type gnus-format-specs)))
2560       (if (and entry
2561                (equal (car entry) new-format))
2562           ;; Use the old format.
2563           (set (intern (format "gnus-%s-line-format-spec" type))
2564                (cadr entry))
2565         ;; This is a new format.
2566         (setq val
2567               (if (not (stringp new-format))
2568                   ;; This is a function call or something.
2569                   new-format
2570                 ;; This is a "real" format.
2571                 (gnus-parse-format
2572                  new-format
2573                  (symbol-value
2574                   (intern (format "gnus-%s-line-format-alist"
2575                                   (if (eq type 'article-mode)
2576                                       'summary-mode type))))
2577                  (not (string-match "mode$" (symbol-name type))))))
2578         ;; Enter the new format spec into the list.
2579         (if entry
2580             (progn
2581               (setcar (cdr entry) val)
2582               (setcar entry new-format))
2583           (push (list type new-format val) gnus-format-specs))
2584         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2585
2586   (unless (assq 'version gnus-format-specs)
2587     (push (cons 'version emacs-version) gnus-format-specs))
2588
2589   (gnus-update-group-mark-positions)
2590   (gnus-update-summary-mark-positions))
2591
2592 (defun gnus-update-summary-mark-positions ()
2593   "Compute where the summary marks are to go."
2594   (save-excursion
2595     (when (and gnus-summary-buffer
2596                (get-buffer gnus-summary-buffer)
2597                (buffer-name (get-buffer gnus-summary-buffer)))
2598       (set-buffer gnus-summary-buffer))
2599     (let ((gnus-replied-mark 129)
2600           (gnus-score-below-mark 130)
2601           (gnus-score-over-mark 130)
2602           (thread nil)
2603           (gnus-visual nil)
2604           (spec gnus-summary-line-format-spec)
2605           pos)
2606       (save-excursion
2607         (gnus-set-work-buffer)
2608         (let ((gnus-summary-line-format-spec spec))
2609           (gnus-summary-insert-line
2610            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2611           (goto-char (point-min))
2612           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2613                                              (- (point) 2)))))
2614           (goto-char (point-min))
2615           (push (cons 'replied (and (search-forward "\201" nil t) 
2616                                     (- (point) 2)))
2617                 pos)
2618           (goto-char (point-min))
2619           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2620                 pos)))
2621       (setq gnus-summary-mark-positions pos))))
2622
2623 (defun gnus-update-group-mark-positions ()
2624   (save-excursion
2625     (let ((gnus-process-mark 128)
2626           (gnus-group-marked '("dummy.group"))
2627           (gnus-active-hashtb (make-vector 10 0)))
2628       (gnus-set-active "dummy.group" '(0 . 0))
2629       (gnus-set-work-buffer)
2630       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2631       (goto-char (point-min))
2632       (setq gnus-group-mark-positions
2633             (list (cons 'process (and (search-forward "\200" nil t)
2634                                       (- (point) 2))))))))
2635
2636 (defvar gnus-mouse-face-0 'highlight)
2637 (defvar gnus-mouse-face-1 'highlight)
2638 (defvar gnus-mouse-face-2 'highlight)
2639 (defvar gnus-mouse-face-3 'highlight)
2640 (defvar gnus-mouse-face-4 'highlight)
2641
2642 (defun gnus-mouse-face-function (form type)
2643   `(gnus-put-text-property
2644     (point) (progn ,@form (point))
2645     gnus-mouse-face-prop
2646     ,(if (equal type 0)
2647          'gnus-mouse-face
2648        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2649
2650 (defvar gnus-face-0 'bold)
2651 (defvar gnus-face-1 'italic)
2652 (defvar gnus-face-2 'bold-italic)
2653 (defvar gnus-face-3 'bold)
2654 (defvar gnus-face-4 'bold)
2655
2656 (defun gnus-face-face-function (form type)
2657   `(gnus-put-text-property
2658     (point) (progn ,@form (point))
2659     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2660
2661 (defun gnus-max-width-function (el max-width)
2662   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2663   (if (symbolp el)
2664       `(if (> (length ,el) ,max-width)
2665            (substring ,el 0 ,max-width)
2666          ,el)
2667     `(let ((val (eval ,el)))
2668        (if (numberp val)
2669            (setq val (int-to-string val)))
2670        (if (> (length val) ,max-width)
2671            (substring val 0 ,max-width)
2672          val))))
2673
2674 (defun gnus-parse-format (format spec-alist &optional insert)
2675   ;; This function parses the FORMAT string with the help of the
2676   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2677   ;; string.  If the FORMAT string contains the specifiers %( and %)
2678   ;; the text between them will have the mouse-face text property.
2679   (if (string-match
2680        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2681        format)
2682       (gnus-parse-complex-format format spec-alist)
2683     ;; This is a simple format.
2684     (gnus-parse-simple-format format spec-alist insert)))
2685
2686 (defun gnus-parse-complex-format (format spec-alist)
2687   (save-excursion
2688     (gnus-set-work-buffer)
2689     (insert format)
2690     (goto-char (point-min))
2691     (while (re-search-forward "\"" nil t)
2692       (replace-match "\\\"" nil t))
2693     (goto-char (point-min))
2694     (insert "(\"")
2695     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2696       (let ((number (if (match-beginning 1)
2697                         (match-string 1) "0"))
2698             (delim (aref (match-string 2) 0)))
2699         (if (or (= delim ?\() (= delim ?\{))
2700             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2701                                    " " number " \""))
2702           (replace-match "\")\""))))
2703     (goto-char (point-max))
2704     (insert "\")")
2705     (goto-char (point-min))
2706     (let ((form (read (current-buffer))))
2707       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2708
2709 (defun gnus-complex-form-to-spec (form spec-alist)
2710   (delq nil
2711         (mapcar
2712          (lambda (sform)
2713            (if (stringp sform)
2714                (gnus-parse-simple-format sform spec-alist t)
2715              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2716                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2717                       (nth 1 sform))))
2718          form)))
2719
2720 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2721   ;; This function parses the FORMAT string with the help of the
2722   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2723   ;; string.
2724   (let ((max-width 0)
2725         spec flist fstring newspec elem beg result dontinsert)
2726     (save-excursion
2727       (gnus-set-work-buffer)
2728       (insert format)
2729       (goto-char (point-min))
2730       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2731                                 nil t)
2732         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2733               (setq newspec "%"
2734                     beg (1+ (match-beginning 0)))
2735           ;; First check if there are any specs that look anything like
2736           ;; "%12,12A", ie. with a "max width specification".  These have
2737           ;; to be treated specially.
2738           (if (setq beg (match-beginning 1))
2739               (setq max-width
2740                     (string-to-int
2741                      (buffer-substring
2742                       (1+ (match-beginning 1)) (match-end 1))))
2743             (setq max-width 0)
2744             (setq beg (match-beginning 2)))
2745           ;; Find the specification from `spec-alist'.
2746           (unless (setq elem (cdr (assq spec spec-alist)))
2747             (setq elem '("*" ?s)))
2748           ;; Treat user defined format specifiers specially.
2749           (when (eq (car elem) 'gnus-tmp-user-defined)
2750             (setq elem
2751                   (list
2752                    (list (intern (concat "gnus-user-format-function-"
2753                                          (match-string 3)))
2754                          'gnus-tmp-header) ?s))
2755             (delete-region (match-beginning 3) (match-end 3)))
2756           (if (not (zerop max-width))
2757               (let ((el (car elem)))
2758                 (cond ((= (cadr elem) ?c)
2759                        (setq el (list 'char-to-string el)))
2760                       ((= (cadr elem) ?d)
2761                        (setq el (list 'int-to-string el))))
2762                 (setq flist (cons (gnus-max-width-function el max-width)
2763                                   flist))
2764                 (setq newspec ?s))
2765             (progn
2766               (setq flist (cons (car elem) flist))
2767               (setq newspec (cadr elem)))))
2768         ;; Remove the old specification (and possibly a ",12" string).
2769         (delete-region beg (match-end 2))
2770         ;; Insert the new specification.
2771         (goto-char beg)
2772         (insert newspec))
2773       (setq fstring (buffer-substring 1 (point-max))))
2774     ;; Do some postprocessing to increase efficiency.
2775     (setq
2776      result
2777      (cond
2778       ;; Emptyness.
2779       ((string= fstring "")
2780        nil)
2781       ;; Not a format string.
2782       ((not (string-match "%" fstring))
2783        (list fstring))
2784       ;; A format string with just a single string spec.
2785       ((string= fstring "%s")
2786        (list (car flist)))
2787       ;; A single character.
2788       ((string= fstring "%c")
2789        (list (car flist)))
2790       ;; A single number.
2791       ((string= fstring "%d")
2792        (setq dontinsert)
2793        (if insert
2794            (list `(princ ,(car flist)))
2795          (list `(int-to-string ,(car flist)))))
2796       ;; Just lots of chars and strings.
2797       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2798        (nreverse flist))
2799       ;; A single string spec at the beginning of the spec.
2800       ((string-match "\\`%[sc][^%]+\\'" fstring)
2801        (list (car flist) (substring fstring 2)))
2802       ;; A single string spec in the middle of the spec.
2803       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2804        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2805       ;; A single string spec in the end of the spec.
2806       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2807        (list (match-string 1 fstring) (car flist)))
2808       ;; A more complex spec.
2809       (t
2810        (list (cons 'format (cons fstring (nreverse flist)))))))
2811
2812     (if insert
2813         (when result
2814           (if dontinsert
2815               result
2816             (cons 'insert result)))
2817       (cond ((stringp result)
2818              result)
2819             ((consp result)
2820              (cons 'concat result))
2821             (t "")))))
2822
2823 (defun gnus-eval-format (format &optional alist props)
2824   "Eval the format variable FORMAT, using ALIST.
2825 If PROPS, insert the result."
2826   (let ((form (gnus-parse-format format alist props)))
2827     (if props
2828         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
2829       (eval form))))
2830
2831 (defun gnus-remove-text-with-property (prop)
2832   "Delete all text in the current buffer with text property PROP."
2833   (save-excursion
2834     (goto-char (point-min))
2835     (while (not (eobp))
2836       (while (get-text-property (point) prop)
2837         (delete-char 1))
2838       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2839
2840 (defun gnus-set-work-buffer ()
2841   (if (get-buffer gnus-work-buffer)
2842       (progn
2843         (set-buffer gnus-work-buffer)
2844         (erase-buffer))
2845     (set-buffer (get-buffer-create gnus-work-buffer))
2846     (kill-all-local-variables)
2847     (buffer-disable-undo (current-buffer))
2848     (gnus-add-current-to-buffer-list)))
2849
2850 ;; Article file names when saving.
2851
2852 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2853   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2854 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2855 Otherwise, it is like ~/News/news/group/num."
2856   (let ((default
2857           (expand-file-name
2858            (concat (if (gnus-use-long-file-name 'not-save)
2859                        (gnus-capitalize-newsgroup newsgroup)
2860                      (gnus-newsgroup-directory-form newsgroup))
2861                    "/" (int-to-string (mail-header-number headers)))
2862            gnus-article-save-directory)))
2863     (if (and last-file
2864              (string-equal (file-name-directory default)
2865                            (file-name-directory last-file))
2866              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2867         default
2868       (or last-file default))))
2869
2870 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2871   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2872 If variable `gnus-use-long-file-name' is non-nil, it is
2873 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2874   (let ((default
2875           (expand-file-name
2876            (concat (if (gnus-use-long-file-name 'not-save)
2877                        newsgroup
2878                      (gnus-newsgroup-directory-form newsgroup))
2879                    "/" (int-to-string (mail-header-number headers)))
2880            gnus-article-save-directory)))
2881     (if (and last-file
2882              (string-equal (file-name-directory default)
2883                            (file-name-directory last-file))
2884              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2885         default
2886       (or last-file default))))
2887
2888 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2889   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2890 If variable `gnus-use-long-file-name' is non-nil, it is
2891 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2892   (or last-file
2893       (expand-file-name
2894        (if (gnus-use-long-file-name 'not-save)
2895            (gnus-capitalize-newsgroup newsgroup)
2896          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2897        gnus-article-save-directory)))
2898
2899 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2900   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2901 If variable `gnus-use-long-file-name' is non-nil, it is
2902 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2903   (or last-file
2904       (expand-file-name
2905        (if (gnus-use-long-file-name 'not-save)
2906            newsgroup
2907          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2908        gnus-article-save-directory)))
2909
2910 ;; For subscribing new newsgroup
2911
2912 (defun gnus-subscribe-hierarchical-interactive (groups)
2913   (let ((groups (sort groups 'string<))
2914         prefixes prefix start ans group starts)
2915     (while groups
2916       (setq prefixes (list "^"))
2917       (while (and groups prefixes)
2918         (while (not (string-match (car prefixes) (car groups)))
2919           (setq prefixes (cdr prefixes)))
2920         (setq prefix (car prefixes))
2921         (setq start (1- (length prefix)))
2922         (if (and (string-match "[^\\.]\\." (car groups) start)
2923                  (cdr groups)
2924                  (setq prefix
2925                        (concat "^" (substring (car groups) 0 (match-end 0))))
2926                  (string-match prefix (cadr groups)))
2927             (progn
2928               (setq prefixes (cons prefix prefixes))
2929               (message "Descend hierarchy %s? ([y]nsq): "
2930                        (substring prefix 1 (1- (length prefix))))
2931               (while (not (memq (setq ans (read-char)) '(?y ?\n ?n ?s ?q)))
2932                 (ding)
2933                 (message "Descend hierarchy %s? ([y]nsq): "
2934                          (substring prefix 1 (1- (length prefix)))))
2935               (cond ((= ans ?n)
2936                      (while (and groups
2937                                  (string-match prefix
2938                                                (setq group (car groups))))
2939                        (setq gnus-killed-list
2940                              (cons group gnus-killed-list))
2941                        (gnus-sethash group group gnus-killed-hashtb)
2942                        (setq groups (cdr groups)))
2943                      (setq starts (cdr starts)))
2944                     ((= ans ?s)
2945                      (while (and groups
2946                                  (string-match prefix
2947                                                (setq group (car groups))))
2948                        (gnus-sethash group group gnus-killed-hashtb)
2949                        (gnus-subscribe-alphabetically (car groups))
2950                        (setq groups (cdr groups)))
2951                      (setq starts (cdr starts)))
2952                     ((= ans ?q)
2953                      (while groups
2954                        (setq group (car groups))
2955                        (setq gnus-killed-list (cons group gnus-killed-list))
2956                        (gnus-sethash group group gnus-killed-hashtb)
2957                        (setq groups (cdr groups))))
2958                     (t nil)))
2959           (message "Subscribe %s? ([n]yq)" (car groups))
2960           (while (not (memq (setq ans (read-char)) '(?y ?\n ?q ?n)))
2961             (ding)
2962             (message "Subscribe %s? ([n]yq)" (car groups)))
2963           (setq group (car groups))
2964           (cond ((= ans ?y)
2965                  (gnus-subscribe-alphabetically (car groups))
2966                  (gnus-sethash group group gnus-killed-hashtb))
2967                 ((= ans ?q)
2968                  (while groups
2969                    (setq group (car groups))
2970                    (setq gnus-killed-list (cons group gnus-killed-list))
2971                    (gnus-sethash group group gnus-killed-hashtb)
2972                    (setq groups (cdr groups))))
2973                 (t
2974                  (setq gnus-killed-list (cons group gnus-killed-list))
2975                  (gnus-sethash group group gnus-killed-hashtb)))
2976           (setq groups (cdr groups)))))))
2977
2978 (defun gnus-subscribe-randomly (newsgroup)
2979   "Subscribe new NEWSGROUP by making it the first newsgroup."
2980   (gnus-subscribe-newsgroup newsgroup))
2981
2982 (defun gnus-subscribe-alphabetically (newgroup)
2983   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2984   (let ((groups (cdr gnus-newsrc-alist))
2985         before)
2986     (while (and (not before) groups)
2987       (if (string< newgroup (caar groups))
2988           (setq before (caar groups))
2989         (setq groups (cdr groups))))
2990     (gnus-subscribe-newsgroup newgroup before)))
2991
2992 (defun gnus-subscribe-hierarchically (newgroup)
2993   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2994   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2995   (save-excursion
2996     (set-buffer (find-file-noselect gnus-current-startup-file))
2997     (let ((groupkey newgroup)
2998           before)
2999       (while (and (not before) groupkey)
3000         (goto-char (point-min))
3001         (let ((groupkey-re
3002                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
3003           (while (and (re-search-forward groupkey-re nil t)
3004                       (progn
3005                         (setq before (match-string 1))
3006                         (string< before newgroup)))))
3007         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
3008         (setq groupkey
3009               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
3010                   (substring groupkey (match-beginning 1) (match-end 1)))))
3011       (gnus-subscribe-newsgroup newgroup before))))
3012
3013 (defun gnus-subscribe-interactively (group)
3014   "Subscribe the new GROUP interactively.
3015 It is inserted in hierarchical newsgroup order if subscribed.  If not,
3016 it is killed."
3017   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
3018       (gnus-subscribe-hierarchically group)
3019     (push group gnus-killed-list)))
3020
3021 (defun gnus-subscribe-zombies (group)
3022   "Make the new GROUP into a zombie group."
3023   (push group gnus-zombie-list))
3024
3025 (defun gnus-subscribe-killed (group)
3026   "Make the new GROUP a killed group."
3027   (push group gnus-killed-list))
3028
3029 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
3030   "Subscribe new NEWSGROUP.
3031 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
3032 the first newsgroup."
3033   ;; We subscribe the group by changing its level to `subscribed'.
3034   (gnus-group-change-level
3035    newsgroup gnus-level-default-subscribed
3036    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
3037   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
3038
3039 ;; For directories
3040
3041 (defun gnus-newsgroup-directory-form (newsgroup)
3042   "Make hierarchical directory name from NEWSGROUP name."
3043   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
3044         (len (length newsgroup))
3045         idx)
3046     ;; If this is a foreign group, we don't want to translate the
3047     ;; entire name.
3048     (if (setq idx (string-match ":" newsgroup))
3049         (aset newsgroup idx ?/)
3050       (setq idx 0))
3051     ;; Replace all occurrences of `.' with `/'.
3052     (while (< idx len)
3053       (if (= (aref newsgroup idx) ?.)
3054           (aset newsgroup idx ?/))
3055       (setq idx (1+ idx)))
3056     newsgroup))
3057
3058 (defun gnus-newsgroup-savable-name (group)
3059   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3060   ;; with dots.
3061   (nnheader-replace-chars-in-string group ?/ ?.))
3062
3063 (defun gnus-make-directory (dir)
3064   "Make DIRECTORY recursively."
3065   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3066   ;; of the many mysteries of the universe.
3067   (let* ((dir (expand-file-name dir default-directory))
3068          dirs err)
3069     (if (string-match "/$" dir)
3070         (setq dir (substring dir 0 (match-beginning 0))))
3071     ;; First go down the path until we find a directory that exists.
3072     (while (not (file-exists-p dir))
3073       (setq dirs (cons dir dirs))
3074       (string-match "/[^/]+$" dir)
3075       (setq dir (substring dir 0 (match-beginning 0))))
3076     ;; Then create all the subdirs.
3077     (while (and dirs (not err))
3078       (condition-case ()
3079           (make-directory (car dirs))
3080         (error (setq err t)))
3081       (setq dirs (cdr dirs)))
3082     ;; We return whether we were successful or not.
3083     (not dirs)))
3084
3085 (defun gnus-capitalize-newsgroup (newsgroup)
3086   "Capitalize NEWSGROUP name."
3087   (and (not (zerop (length newsgroup)))
3088        (concat (char-to-string (upcase (aref newsgroup 0)))
3089                (substring newsgroup 1))))
3090
3091 ;; Various... things.
3092
3093 (defun gnus-simplify-subject (subject &optional re-only)
3094   "Remove `Re:' and words in parentheses.
3095 If RE-ONLY is non-nil, strip leading `Re:'s only."
3096   (let ((case-fold-search t))           ;Ignore case.
3097     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3098     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3099       (setq subject (substring subject (match-end 0))))
3100     ;; Remove uninteresting prefixes.
3101     (if (and (not re-only)
3102              gnus-simplify-ignored-prefixes
3103              (string-match gnus-simplify-ignored-prefixes subject))
3104         (setq subject (substring subject (match-end 0))))
3105     ;; Remove words in parentheses from end.
3106     (unless re-only
3107       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3108         (setq subject (substring subject 0 (match-beginning 0)))))
3109     ;; Return subject string.
3110     subject))
3111
3112 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3113 ;; all whitespace.
3114 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3115 (defun gnus-simplify-buffer-fuzzy ()
3116   (let ((case-fold-search t))
3117     (goto-char (point-min))
3118     (while (search-forward "\t" nil t)
3119       (replace-match " " t t))
3120     (goto-char (point-min))
3121     (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3122     (goto-char (match-beginning 0))
3123     (while (or
3124             (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3125             (looking-at "^[[].*: .*[]]$"))
3126       (goto-char (point-min))
3127       (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3128                                 nil t)
3129         (replace-match "" t t))
3130       (goto-char (point-min))
3131       (while (re-search-forward "^[[].*: .*[]]$" nil t)
3132         (goto-char (match-end 0))
3133         (delete-char -1)
3134         (delete-region
3135          (progn (goto-char (match-beginning 0)))
3136          (re-search-forward ":"))))
3137     (goto-char (point-min))
3138     (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3139       (replace-match "" t t))
3140     (goto-char (point-min))
3141     (while (re-search-forward "  +" nil t)
3142       (replace-match " " t t))
3143     (goto-char (point-min))
3144     (while (re-search-forward " $" nil t)
3145       (replace-match "" t t))
3146     (goto-char (point-min))
3147     (while (re-search-forward "^ +" nil t)
3148       (replace-match "" t t))
3149     (goto-char (point-min))
3150     (when gnus-simplify-subject-fuzzy-regexp
3151       (if (listp gnus-simplify-subject-fuzzy-regexp)
3152           (let ((list gnus-simplify-subject-fuzzy-regexp))
3153             (while list
3154               (goto-char (point-min))
3155               (while (re-search-forward (car list) nil t)
3156                 (replace-match "" t t))
3157               (setq list (cdr list))))
3158         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3159           (replace-match "" t t))))))
3160
3161 (defun gnus-simplify-subject-fuzzy (subject)
3162   "Siplify a subject string fuzzily."
3163   (save-excursion
3164     (gnus-set-work-buffer)
3165     (let ((case-fold-search t))
3166       (insert subject)
3167       (inline (gnus-simplify-buffer-fuzzy))
3168       (buffer-string))))
3169
3170 ;; Add the current buffer to the list of buffers to be killed on exit.
3171 (defun gnus-add-current-to-buffer-list ()
3172   (or (memq (current-buffer) gnus-buffer-list)
3173       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3174
3175 (defun gnus-string> (s1 s2)
3176   (not (or (string< s1 s2)
3177            (string= s1 s2))))
3178
3179 (defun gnus-read-active-file-p ()
3180   "Say whether the active file has been read from `gnus-select-method'."
3181   (memq gnus-select-method gnus-have-read-active-file))
3182
3183 ;;; General various misc type functions.
3184
3185 (defun gnus-clear-system ()
3186   "Clear all variables and buffers."
3187   ;; Clear Gnus variables.
3188   (let ((variables gnus-variable-list))
3189     (while variables
3190       (set (car variables) nil)
3191       (setq variables (cdr variables))))
3192   ;; Clear other internal variables.
3193   (setq gnus-list-of-killed-groups nil
3194         gnus-have-read-active-file nil
3195         gnus-newsrc-alist nil
3196         gnus-newsrc-hashtb nil
3197         gnus-killed-list nil
3198         gnus-zombie-list nil
3199         gnus-killed-hashtb nil
3200         gnus-active-hashtb nil
3201         gnus-moderated-list nil
3202         gnus-description-hashtb nil
3203         gnus-current-headers nil
3204         gnus-thread-indent-array nil
3205         gnus-newsgroup-headers nil
3206         gnus-newsgroup-name nil
3207         gnus-server-alist nil
3208         gnus-group-list-mode nil
3209         gnus-opened-servers nil
3210         gnus-current-select-method nil)
3211   (gnus-shutdown 'gnus)
3212   ;; Kill the startup file.
3213   (and gnus-current-startup-file
3214        (get-file-buffer gnus-current-startup-file)
3215        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3216   ;; Clear the dribble buffer.
3217   (gnus-dribble-clear)
3218   ;; Kill global KILL file buffer.
3219   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3220     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3221   (gnus-kill-buffer nntp-server-buffer)
3222   ;; Kill Gnus buffers.
3223   (while gnus-buffer-list
3224     (gnus-kill-buffer (pop gnus-buffer-list)))
3225   ;; Remove Gnus frames.
3226   (while gnus-created-frames
3227     (when (frame-live-p (car gnus-created-frames))
3228       ;; We slap a condition-case around this `delete-frame' to ensure 
3229       ;; agains errors if we try do delete the single frame that's left.
3230       (condition-case ()
3231           (delete-frame (car gnus-created-frames))
3232         (error nil)))
3233     (pop gnus-created-frames)))
3234
3235 (defun gnus-windows-old-to-new (setting)
3236   ;; First we take care of the really, really old Gnus 3 actions.
3237   (when (symbolp setting)
3238     (setq setting
3239           ;; Take care of ooold GNUS 3.x values.
3240           (cond ((eq setting 'SelectArticle) 'article)
3241                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3242                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3243                 (t setting))))
3244   (if (or (listp setting)
3245           (not (and gnus-window-configuration
3246                     (memq setting '(group summary article)))))
3247       setting
3248     (let* ((setting (if (eq setting 'group)
3249                         (if (assq 'newsgroup gnus-window-configuration)
3250                             'newsgroup
3251                           'newsgroups) setting))
3252            (elem (cadr (assq setting gnus-window-configuration)))
3253            (total (apply '+ elem))
3254            (types '(group summary article))
3255            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3256            (i 0)
3257            perc
3258            out)
3259       (while (< i 3)
3260         (or (not (numberp (nth i elem)))
3261             (zerop (nth i elem))
3262             (progn
3263               (setq perc  (/ (float (nth 0 elem)) total))
3264               (setq out (cons (if (eq pbuf (nth i types))
3265                                   (vector (nth i types) perc 'point)
3266                                 (vector (nth i types) perc))
3267                               out))))
3268         (setq i (1+ i)))
3269       (list (nreverse out)))))
3270
3271 ;;;###autoload
3272 (defun gnus-add-configuration (conf)
3273   "Add the window configuration CONF to `gnus-buffer-configuration'."
3274   (setq gnus-buffer-configuration
3275         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3276                          gnus-buffer-configuration))))
3277
3278 (defvar gnus-frame-list nil)
3279
3280 (defun gnus-configure-frame (split &optional window)
3281   "Split WINDOW according to SPLIT."
3282   (unless window
3283     (setq window (get-buffer-window (current-buffer))))
3284   (select-window window)
3285   ;; This might be an old-stylee buffer config.
3286   (when (vectorp split)
3287     (setq split (append split nil)))
3288   (when (or (consp (car split))
3289             (vectorp (car split)))
3290     (push 1.0 split)
3291     (push 'vertical split))
3292   ;; The SPLIT might be something that is to be evaled to
3293   ;; return a new SPLIT.
3294   (while (and (not (assq (car split) gnus-window-to-buffer))
3295               (gnus-functionp (car split)))
3296     (setq split (eval split)))
3297   (let* ((type (car split))
3298          (subs (cddr split))
3299          (len (if (eq type 'horizontal) (window-width) (window-height)))
3300          (total 0)
3301          (window-min-width (or gnus-window-min-width window-min-width))
3302          (window-min-height (or gnus-window-min-height window-min-height))
3303          s result new-win rest comp-subs size sub)
3304     (cond
3305      ;; Nothing to do here.
3306      ((null split))
3307      ;; Don't switch buffers.
3308      ((null type)
3309       (and (memq 'point split) window))
3310      ;; This is a buffer to be selected.
3311      ((not (memq type '(frame horizontal vertical)))
3312       (let ((buffer (cond ((stringp type) type)
3313                           (t (cdr (assq type gnus-window-to-buffer)))))
3314             buf)
3315         (unless buffer
3316           (error "Illegal buffer type: %s" type))
3317         (unless (setq buf (get-buffer (if (symbolp buffer)
3318                                           (symbol-value buffer) buffer)))
3319           (setq buf (get-buffer-create (if (symbolp buffer)
3320                                            (symbol-value buffer) buffer))))
3321         (switch-to-buffer buf)
3322         ;; We return the window if it has the `point' spec.
3323         (and (memq 'point split) window)))
3324      ;; This is a frame split.
3325      ((eq type 'frame)
3326       (unless gnus-frame-list
3327         (setq gnus-frame-list (list (window-frame
3328                                      (get-buffer-window (current-buffer))))))
3329       (let ((i 0)
3330             params frame fresult)
3331         (while (< i (length subs))
3332           ;; Frame parameter is gotten from the sub-split.
3333           (setq params (cadr (elt subs i)))
3334           ;; It should be a list.
3335           (unless (listp params)
3336             (setq params nil))
3337           ;; Create a new frame?
3338           (unless (setq frame (elt gnus-frame-list i))
3339             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3340             (push frame gnus-created-frames))
3341           ;; Is the old frame still alive?
3342           (unless (frame-live-p frame)
3343             (setcar (nthcdr i gnus-frame-list)
3344                     (setq frame (make-frame params))))
3345           ;; Select the frame in question and do more splits there.
3346           (select-frame frame)
3347           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3348           (incf i))
3349         ;; Select the frame that has the selected buffer.
3350         (when fresult
3351           (select-frame (window-frame fresult)))))
3352      ;; This is a normal split.
3353      (t
3354       (when (> (length subs) 0)
3355         ;; First we have to compute the sizes of all new windows.
3356         (while subs
3357           (setq sub (append (pop subs) nil))
3358           (while (and (not (assq (car sub) gnus-window-to-buffer))
3359                       (gnus-functionp (car sub)))
3360             (setq sub (eval sub)))
3361           (when sub
3362             (push sub comp-subs)
3363             (setq size (cadar comp-subs))
3364             (cond ((equal size 1.0)
3365                    (setq rest (car comp-subs))
3366                    (setq s 0))
3367                   ((floatp size)
3368                    (setq s (floor (* size len))))
3369                   ((integerp size)
3370                    (setq s size))
3371                   (t
3372                    (error "Illegal size: %s" size)))
3373             ;; Try to make sure that we are inside the safe limits.
3374             (cond ((zerop s))
3375                   ((eq type 'horizontal)
3376                    (setq s (max s window-min-width)))
3377                   ((eq type 'vertical)
3378                    (setq s (max s window-min-height))))
3379             (setcar (cdar comp-subs) s)
3380             (incf total s)))
3381         ;; Take care of the "1.0" spec.
3382         (if rest
3383             (setcar (cdr rest) (- len total))
3384           (error "No 1.0 specs in %s" split))
3385         ;; The we do the actual splitting in a nice recursive
3386         ;; fashion.
3387         (setq comp-subs (nreverse comp-subs))
3388         (while comp-subs
3389           (if (null (cdr comp-subs))
3390               (setq new-win window)
3391             (setq new-win
3392                   (split-window window (cadar comp-subs)
3393                                 (eq type 'horizontal))))
3394           (setq result (or (gnus-configure-frame
3395                             (car comp-subs) window) result))
3396           (select-window new-win)
3397           (setq window new-win)
3398           (setq comp-subs (cdr comp-subs))))
3399       ;; Return the proper window, if any.
3400       (when result
3401         (select-window result))))))
3402
3403 (defvar gnus-frame-split-p nil)
3404
3405 (defun gnus-configure-windows (setting &optional force)
3406   (setq setting (gnus-windows-old-to-new setting))
3407   (let ((split (if (symbolp setting)
3408                    (cadr (assq setting gnus-buffer-configuration))
3409                  setting))
3410         all-visible)
3411
3412     (setq gnus-frame-split-p nil)
3413
3414     (unless split
3415       (error "No such setting: %s" setting))
3416
3417     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3418              (not force))
3419         ;; All the windows mentioned are already visible, so we just
3420         ;; put point in the assigned buffer, and do not touch the
3421         ;; winconf.
3422         (select-window all-visible)
3423
3424       ;; Either remove all windows or just remove all Gnus windows.
3425       (let ((frame (selected-frame)))
3426         (unwind-protect
3427             (if gnus-use-full-window
3428                 ;; We want to remove all other windows.
3429                 (if (not gnus-frame-split-p)
3430                     ;; This is not a `frame' split, so we ignore the
3431                     ;; other frames.  
3432                     (delete-other-windows)
3433                   ;; This is a `frame' split, so we delete all windows
3434                   ;; on all frames.
3435                   (mapcar 
3436                    (lambda (frame)
3437                      (unless (eq (cdr (assq 'minibuffer
3438                                             (frame-parameters frame)))
3439                                  'only)
3440                        (select-frame frame)
3441                        (delete-other-windows)))
3442                    (frame-list)))
3443               ;; Just remove some windows.
3444               (gnus-remove-some-windows)
3445               (switch-to-buffer nntp-server-buffer))
3446           (select-frame frame)))
3447
3448       (switch-to-buffer nntp-server-buffer)
3449       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3450
3451 (defun gnus-all-windows-visible-p (split)
3452   "Say whether all buffers in SPLIT are currently visible.
3453 In particular, the value returned will be the window that
3454 should have point."
3455   (let ((stack (list split))
3456         (all-visible t)
3457         type buffer win buf)
3458     (while (and (setq split (pop stack))
3459                 all-visible)
3460       ;; Be backwards compatible.
3461       (when (vectorp split)
3462         (setq split (append split nil)))
3463       (when (or (consp (car split))
3464                 (vectorp (car split)))
3465         (push 1.0 split)
3466         (push 'vertical split))
3467       ;; The SPLIT might be something that is to be evaled to
3468       ;; return a new SPLIT.
3469       (while (and (not (assq (car split) gnus-window-to-buffer))
3470                   (gnus-functionp (car split)))
3471         (setq split (eval split)))
3472
3473       (setq type (elt split 0))
3474       (cond
3475        ;; Nothing here.
3476        ((null split) t)
3477        ;; A buffer.
3478        ((not (memq type '(horizontal vertical frame)))
3479         (setq buffer (cond ((stringp type) type)
3480                            (t (cdr (assq type gnus-window-to-buffer)))))
3481         (unless buffer
3482           (error "Illegal buffer type: %s" type))
3483         (when (setq buf (get-buffer (if (symbolp buffer)
3484                                         (symbol-value buffer)
3485                                       buffer)))
3486           (setq win (get-buffer-window buf t)))
3487         (if win
3488             (when (memq 'point split)
3489                 (setq all-visible win))
3490           (setq all-visible nil)))
3491        (t
3492         (when (eq type 'frame)
3493           (setq gnus-frame-split-p t))
3494         (setq stack (append (cddr split) stack)))))
3495     (unless (eq all-visible t)
3496       all-visible)))
3497
3498 (defun gnus-window-top-edge (&optional window)
3499   (nth 1 (window-edges window)))
3500
3501 (defun gnus-remove-some-windows ()
3502   (let ((buffers gnus-window-to-buffer)
3503         buf bufs lowest-buf lowest)
3504     (save-excursion
3505       ;; Remove windows on all known Gnus buffers.
3506       (while buffers
3507         (setq buf (cdar buffers))
3508         (if (symbolp buf)
3509             (setq buf (and (boundp buf) (symbol-value buf))))
3510         (and buf
3511              (get-buffer-window buf)
3512              (progn
3513                (setq bufs (cons buf bufs))
3514                (pop-to-buffer buf)
3515                (if (or (not lowest)
3516                        (< (gnus-window-top-edge) lowest))
3517                    (progn
3518                      (setq lowest (gnus-window-top-edge))
3519                      (setq lowest-buf buf)))))
3520         (setq buffers (cdr buffers)))
3521       ;; Remove windows on *all* summary buffers.
3522       (walk-windows
3523        (lambda (win)
3524          (let ((buf (window-buffer win)))
3525            (if (string-match    "^\\*Summary" (buffer-name buf))
3526                (progn
3527                  (setq bufs (cons buf bufs))
3528                  (pop-to-buffer buf)
3529                  (if (or (not lowest)
3530                          (< (gnus-window-top-edge) lowest))
3531                      (progn
3532                        (setq lowest-buf buf)
3533                        (setq lowest (gnus-window-top-edge)))))))))
3534       (and lowest-buf
3535            (progn
3536              (pop-to-buffer lowest-buf)
3537              (switch-to-buffer nntp-server-buffer)))
3538       (while bufs
3539         (and (not (eq (car bufs) lowest-buf))
3540              (delete-windows-on (car bufs)))
3541         (setq bufs (cdr bufs))))))
3542
3543 (defun gnus-version (&optional arg)
3544   "Version number of this version of Gnus.
3545 If ARG, insert string at point."
3546   (interactive "P")
3547   (let ((methods gnus-valid-select-methods)
3548         (mess gnus-version)
3549         meth)
3550     ;; Go through all the legal select methods and add their version
3551     ;; numbers to the total version string.  Only the backends that are
3552     ;; currently in use will have their message numbers taken into
3553     ;; consideration.
3554     (while methods
3555       (setq meth (intern (concat (caar methods) "-version")))
3556       (and (boundp meth)
3557            (stringp (symbol-value meth))
3558            (setq mess (concat mess "; " (symbol-value meth))))
3559       (setq methods (cdr methods)))
3560     (if arg
3561         (insert (message mess))
3562       (message mess))))
3563
3564 (defun gnus-info-find-node ()
3565   "Find Info documentation of Gnus."
3566   (interactive)
3567   ;; Enlarge info window if needed.
3568   (let ((mode major-mode)
3569         gnus-info-buffer)
3570     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3571     (setq gnus-info-buffer (current-buffer))
3572     (gnus-configure-windows 'info)))
3573
3574 (defun gnus-days-between (date1 date2)
3575   ;; Return the number of days between date1 and date2.
3576   (- (gnus-day-number date1) (gnus-day-number date2)))
3577
3578 (defun gnus-day-number (date)
3579   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3580                      (timezone-parse-date date))))
3581     (timezone-absolute-from-gregorian
3582      (nth 1 dat) (nth 2 dat) (car dat))))
3583
3584 (defun gnus-encode-date (date)
3585   "Convert DATE to internal time."
3586   (let* ((parse (timezone-parse-date date))
3587          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3588          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3589     (encode-time (caddr time) (cadr time) (car time)
3590                  (caddr date) (cadr date) (car date) (nth 4 date))))
3591
3592 (defun gnus-time-minus (t1 t2)
3593   "Subtract two internal times."
3594   (let ((borrow (< (cadr t1) (cadr t2))))
3595     (list (- (car t1) (car t2) (if borrow 1 0))
3596           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3597
3598 (defun gnus-file-newer-than (file date)
3599   (let ((fdate (nth 5 (file-attributes file))))
3600     (or (> (car fdate) (car date))
3601         (and (= (car fdate) (car date))
3602              (> (nth 1 fdate) (nth 1 date))))))
3603
3604 (defmacro gnus-local-set-keys (&rest plist)
3605   "Set the keys in PLIST in the current keymap."
3606   `(gnus-define-keys-1 (current-local-map) ',plist))
3607
3608 (defmacro gnus-define-keys (keymap &rest plist)
3609   "Define all keys in PLIST in KEYMAP."
3610   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3611
3612 (put 'gnus-define-keys 'lisp-indent-function 1)
3613 (put 'gnus-define-keys 'lisp-indent-hook 1)
3614 (put 'gnus-define-keymap 'lisp-indent-function 1)
3615 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3616
3617 (defmacro gnus-define-keymap (keymap &rest plist)
3618   "Define all keys in PLIST in KEYMAP."
3619   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3620
3621 (defun gnus-define-keys-1 (keymap plist)
3622   (when (null keymap)
3623     (error "Can't set keys in a null keymap"))
3624   (cond ((symbolp keymap)
3625          (setq keymap (symbol-value keymap)))
3626         ((keymapp keymap))
3627         ((listp keymap)
3628          (set (car keymap) nil)
3629          (define-prefix-command (car keymap))
3630          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3631          (setq keymap (symbol-value (car keymap)))))
3632   (let (key)
3633     (while plist
3634       (when (symbolp (setq key (pop plist)))
3635         (setq key (symbol-value key)))
3636       (define-key keymap key (pop plist)))))
3637
3638 (defun gnus-group-read-only-p (&optional group)
3639   "Check whether GROUP supports editing or not.
3640 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3641 that that variable is buffer-local to the summary buffers."
3642   (let ((group (or group gnus-newsgroup-name)))
3643     (not (gnus-check-backend-function 'request-replace-article group))))
3644
3645 (defun gnus-group-total-expirable-p (group)
3646   "Check whether GROUP is total-expirable or not."
3647   (let ((params (gnus-info-params (gnus-get-info group))))
3648     (or (memq 'total-expire params)
3649         (cdr (assq 'total-expire params)) ; (total-expire . t)
3650         (and gnus-total-expirable-newsgroups ; Check var.
3651              (string-match gnus-total-expirable-newsgroups group)))))
3652
3653 (defun gnus-group-auto-expirable-p (group)
3654   "Check whether GROUP is total-expirable or not."
3655   (let ((params (gnus-info-params (gnus-get-info group))))
3656     (or (memq 'auto-expire params)
3657         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3658         (and gnus-auto-expirable-newsgroups ; Check var.
3659              (string-match gnus-auto-expirable-newsgroups group)))))
3660
3661 (defun gnus-virtual-group-p (group)
3662   "Say whether GROUP is virtual or not."
3663   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3664                         gnus-valid-select-methods)))
3665
3666 (defun gnus-news-group-p (group &optional article)
3667   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3668   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3669       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3670            (eq (gnus-request-type group article) 'news))))
3671
3672 (defsubst gnus-simplify-subject-fully (subject)
3673   "Simplify a subject string according to the user's wishes."
3674   (cond
3675    ((null gnus-summary-gather-subject-limit)
3676     (gnus-simplify-subject-re subject))
3677    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3678     (gnus-simplify-subject-fuzzy subject))
3679    ((numberp gnus-summary-gather-subject-limit)
3680     (gnus-limit-string (gnus-simplify-subject-re subject)
3681                        gnus-summary-gather-subject-limit))
3682    (t
3683     subject)))
3684
3685 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3686   "Check whether two subjects are equal.  If optional argument
3687 simple-first is t, first argument is already simplified."
3688   (cond
3689    ((null simple-first)
3690     (equal (gnus-simplify-subject-fully s1)
3691            (gnus-simplify-subject-fully s2)))
3692    (t
3693     (equal s1
3694            (gnus-simplify-subject-fully s2)))))
3695
3696 ;; Returns a list of writable groups.
3697 (defun gnus-writable-groups ()
3698   (let ((alist gnus-newsrc-alist)
3699         groups group)
3700     (while (setq group (car (pop alist)))
3701       (unless (gnus-group-read-only-p group)
3702         (push group groups)))
3703     (nreverse groups)))
3704
3705 (defun gnus-completing-read (default prompt &rest args)
3706   ;; Like `completing-read', except that DEFAULT is the default argument.
3707   (let* ((prompt (if default 
3708                      (concat prompt " (default " default ") ")
3709                    (concat prompt " ")))
3710          (answer (apply 'completing-read prompt args)))
3711     (if (or (null answer) (zerop (length answer)))
3712         default
3713       answer)))
3714
3715 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3716 ;; the echo area.
3717 (defun gnus-y-or-n-p (prompt)
3718   (prog1
3719       (y-or-n-p prompt)
3720     (message "")))
3721
3722 (defun gnus-yes-or-no-p (prompt)
3723   (prog1
3724       (yes-or-no-p prompt)
3725     (message "")))
3726
3727 ;; Check whether to use long file names.
3728 (defun gnus-use-long-file-name (symbol)
3729   ;; The variable has to be set...
3730   (and gnus-use-long-file-name
3731        ;; If it isn't a list, then we return t.
3732        (or (not (listp gnus-use-long-file-name))
3733            ;; If it is a list, and the list contains `symbol', we
3734            ;; return nil.
3735            (not (memq symbol gnus-use-long-file-name)))))
3736
3737 ;; I suspect there's a better way, but I haven't taken the time to do
3738 ;; it yet. -erik selberg@cs.washington.edu
3739 (defun gnus-dd-mmm (messy-date)
3740   "Return a string like DD-MMM from a big messy string"
3741   (let ((datevec (condition-case () (timezone-parse-date messy-date) 
3742                    (error nil))))
3743     (if (not datevec)
3744         "??-???"
3745       (format "%2s-%s"
3746               (condition-case ()
3747                   ;; Make sure leading zeroes are stripped.
3748                   (number-to-string (string-to-number (aref datevec 2)))
3749                 (error "??"))
3750               (capitalize
3751                (or (car
3752                     (nth (1- (string-to-number (aref datevec 1)))
3753                          timezone-months-assoc))
3754                    "???"))))))
3755
3756 (defun gnus-mode-string-quote (string)
3757   "Quote all \"%\" in STRING."
3758   (save-excursion
3759     (gnus-set-work-buffer)
3760     (insert string)
3761     (goto-char (point-min))
3762     (while (search-forward "%" nil t)
3763       (insert "%"))
3764     (buffer-string)))
3765
3766 ;; Make a hash table (default and minimum size is 255).
3767 ;; Optional argument HASHSIZE specifies the table size.
3768 (defun gnus-make-hashtable (&optional hashsize)
3769   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3770
3771 ;; Make a number that is suitable for hashing; bigger than MIN and one
3772 ;; less than 2^x.
3773 (defun gnus-create-hash-size (min)
3774   (let ((i 1))
3775     (while (< i min)
3776       (setq i (* 2 i)))
3777     (1- i)))
3778
3779 ;; Show message if message has a lower level than `gnus-verbose'.
3780 ;; Guideline for numbers:
3781 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3782 ;; for things that take a long time, 7 - not very important messages
3783 ;; on stuff, 9 - messages inside loops.
3784 (defun gnus-message (level &rest args)
3785   (if (<= level gnus-verbose)
3786       (apply 'message args)
3787     ;; We have to do this format thingy here even if the result isn't
3788     ;; shown - the return value has to be the same as the return value
3789     ;; from `message'.
3790     (apply 'format args)))
3791
3792 (defun gnus-error (level &rest args)
3793   "Beep an error if `gnus-verbose' is on LEVEL or less."
3794   (when (<= (floor level) gnus-verbose)
3795     (apply 'message args)
3796     (ding)
3797     (let (duration)
3798       (when (and (floatp level)
3799                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3800         (sit-for duration))))
3801   nil)
3802
3803 ;; Generate a unique new group name.
3804 (defun gnus-generate-new-group-name (leaf)
3805   (let ((name leaf)
3806         (num 0))
3807     (while (gnus-gethash name gnus-newsrc-hashtb)
3808       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3809     name))
3810
3811 (defsubst gnus-hide-text (b e props)
3812   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
3813   (gnus-add-text-properties b e props)
3814   (when (memq 'intangible props)
3815     (gnus-put-text-property (max (1- b) (point-min))
3816                        b 'intangible (cddr (memq 'intangible props)))))
3817
3818 (defsubst gnus-unhide-text (b e)
3819   "Remove hidden text properties from region between B and E."
3820   (remove-text-properties b e gnus-hidden-properties)
3821   (when (memq 'intangible gnus-hidden-properties)
3822     (gnus-put-text-property (max (1- b) (point-min))
3823                        b 'intangible nil)))
3824
3825 (defun gnus-hide-text-type (b e type)
3826   "Hide text of TYPE between B and E."
3827   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3828
3829 (defun gnus-parent-headers (headers &optional generation)
3830   "Return the headers of the GENERATIONeth parent of HEADERS."
3831   (unless generation 
3832     (setq generation 1))
3833   (let (references parent)
3834     (while (and headers (not (zerop generation)))
3835       (setq references (mail-header-references headers))
3836       (when (and references
3837                  (setq parent (gnus-parent-id references))
3838                  (setq headers (car (gnus-id-to-thread parent))))
3839         (decf generation)))
3840     headers))
3841
3842 (defun gnus-parent-id (references)
3843   "Return the last Message-ID in REFERENCES."
3844   (when (and references
3845              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3846     (substring references (match-beginning 1) (match-end 1))))
3847
3848 (defun gnus-split-references (references)
3849   "Return a list of Message-IDs in REFERENCES."
3850   (let ((beg 0)
3851         ids)
3852     (while (string-match "<[^>]+>" references beg)
3853       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3854             ids))
3855     (nreverse ids)))
3856
3857 (defun gnus-buffer-live-p (buffer)
3858   "Say whether BUFFER is alive or not."
3859   (and buffer
3860        (get-buffer buffer)
3861        (buffer-name (get-buffer buffer))))
3862
3863 (defun gnus-ephemeral-group-p (group)
3864   "Say whether GROUP is ephemeral or not."
3865   (gnus-group-get-parameter group 'quit-config))
3866
3867 (defun gnus-group-quit-config (group)
3868   "Return the quit-config of GROUP."
3869   (gnus-group-get-parameter group 'quit-config))
3870
3871 (defun gnus-simplify-mode-line ()
3872   "Make mode lines a bit simpler."
3873   (setq mode-line-modified "-- ")
3874   (when (listp mode-line-format)
3875     (make-local-variable 'mode-line-format)
3876     (setq mode-line-format (copy-sequence mode-line-format))
3877     (when (equal (nth 3 mode-line-format) "   ")
3878       (setcar (nthcdr 3 mode-line-format) " "))))
3879
3880 ;;; List and range functions
3881
3882 (defun gnus-last-element (list)
3883   "Return last element of LIST."
3884   (while (cdr list)
3885     (setq list (cdr list)))
3886   (car list))
3887
3888 (defun gnus-copy-sequence (list)
3889   "Do a complete, total copy of a list."
3890   (if (and (consp list) (not (consp (cdr list))))
3891       (cons (car list) (cdr list))
3892     (mapcar (lambda (elem) (if (consp elem)
3893                                (if (consp (cdr elem))
3894                                    (gnus-copy-sequence elem)
3895                                  (cons (car elem) (cdr elem)))
3896                              elem))
3897             list)))
3898
3899 (defun gnus-set-difference (list1 list2)
3900   "Return a list of elements of LIST1 that do not appear in LIST2."
3901   (let ((list1 (copy-sequence list1)))
3902     (while list2
3903       (setq list1 (delq (car list2) list1))
3904       (setq list2 (cdr list2)))
3905     list1))
3906
3907 (defun gnus-sorted-complement (list1 list2)
3908   "Return a list of elements of LIST1 that do not appear in LIST2.
3909 Both lists have to be sorted over <."
3910   (let (out)
3911     (if (or (null list1) (null list2))
3912         (or list1 list2)
3913       (while (and list1 list2)
3914         (cond ((= (car list1) (car list2))
3915                (setq list1 (cdr list1)
3916                      list2 (cdr list2)))
3917               ((< (car list1) (car list2))
3918                (setq out (cons (car list1) out))
3919                (setq list1 (cdr list1)))
3920               (t
3921                (setq out (cons (car list2) out))
3922                (setq list2 (cdr list2)))))
3923       (nconc (nreverse out) (or list1 list2)))))
3924
3925 (defun gnus-intersection (list1 list2)
3926   (let ((result nil))
3927     (while list2
3928       (if (memq (car list2) list1)
3929           (setq result (cons (car list2) result)))
3930       (setq list2 (cdr list2)))
3931     result))
3932
3933 (defun gnus-sorted-intersection (list1 list2)
3934   ;; LIST1 and LIST2 have to be sorted over <.
3935   (let (out)
3936     (while (and list1 list2)
3937       (cond ((= (car list1) (car list2))
3938              (setq out (cons (car list1) out)
3939                    list1 (cdr list1)
3940                    list2 (cdr list2)))
3941             ((< (car list1) (car list2))
3942              (setq list1 (cdr list1)))
3943             (t
3944              (setq list2 (cdr list2)))))
3945     (nreverse out)))
3946
3947 (defun gnus-set-sorted-intersection (list1 list2)
3948   ;; LIST1 and LIST2 have to be sorted over <.
3949   ;; This function modifies LIST1.
3950   (let* ((top (cons nil list1))
3951          (prev top))
3952     (while (and list1 list2)
3953       (cond ((= (car list1) (car list2))
3954              (setq prev list1
3955                    list1 (cdr list1)
3956                    list2 (cdr list2)))
3957             ((< (car list1) (car list2))
3958              (setcdr prev (cdr list1))
3959              (setq list1 (cdr list1)))
3960             (t
3961              (setq list2 (cdr list2)))))
3962     (setcdr prev nil)
3963     (cdr top)))
3964
3965 (defun gnus-compress-sequence (numbers &optional always-list)
3966   "Convert list of numbers to a list of ranges or a single range.
3967 If ALWAYS-LIST is non-nil, this function will always release a list of
3968 ranges."
3969   (let* ((first (car numbers))
3970          (last (car numbers))
3971          result)
3972     (if (null numbers)
3973         nil
3974       (if (not (listp (cdr numbers)))
3975           numbers
3976         (while numbers
3977           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3978                 ((= (1+ last) (car numbers)) ;Still in sequence
3979                  (setq last (car numbers)))
3980                 (t                      ;End of one sequence
3981                  (setq result
3982                        (cons (if (= first last) first
3983                                (cons first last)) result))
3984                  (setq first (car numbers))
3985                  (setq last  (car numbers))))
3986           (setq numbers (cdr numbers)))
3987         (if (and (not always-list) (null result))
3988             (if (= first last) (list first) (cons first last))
3989           (nreverse (cons (if (= first last) first (cons first last))
3990                           result)))))))
3991
3992 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3993 (defun gnus-uncompress-range (ranges)
3994   "Expand a list of ranges into a list of numbers.
3995 RANGES is either a single range on the form `(num . num)' or a list of
3996 these ranges."
3997   (let (first last result)
3998     (cond
3999      ((null ranges)
4000       nil)
4001      ((not (listp (cdr ranges)))
4002       (setq first (car ranges))
4003       (setq last (cdr ranges))
4004       (while (<= first last)
4005         (setq result (cons first result))
4006         (setq first (1+ first)))
4007       (nreverse result))
4008      (t
4009       (while ranges
4010         (if (atom (car ranges))
4011             (if (numberp (car ranges))
4012                 (setq result (cons (car ranges) result)))
4013           (setq first (caar ranges))
4014           (setq last  (cdar ranges))
4015           (while (<= first last)
4016             (setq result (cons first result))
4017             (setq first (1+ first))))
4018         (setq ranges (cdr ranges)))
4019       (nreverse result)))))
4020
4021 (defun gnus-add-to-range (ranges list)
4022   "Return a list of ranges that has all articles from both RANGES and LIST.
4023 Note: LIST has to be sorted over `<'."
4024   (if (not ranges)
4025       (gnus-compress-sequence list t)
4026     (setq list (copy-sequence list))
4027     (or (listp (cdr ranges))
4028         (setq ranges (list ranges)))
4029     (let ((out ranges)
4030           ilist lowest highest temp)
4031       (while (and ranges list)
4032         (setq ilist list)
4033         (setq lowest (or (and (atom (car ranges)) (car ranges))
4034                          (caar ranges)))
4035         (while (and list (cdr list) (< (cadr list) lowest))
4036           (setq list (cdr list)))
4037         (if (< (car ilist) lowest)
4038             (progn
4039               (setq temp list)
4040               (setq list (cdr list))
4041               (setcdr temp nil)
4042               (setq out (nconc (gnus-compress-sequence ilist t) out))))
4043         (setq highest (or (and (atom (car ranges)) (car ranges))
4044                           (cdar ranges)))
4045         (while (and list (<= (car list) highest))
4046           (setq list (cdr list)))
4047         (setq ranges (cdr ranges)))
4048       (if list
4049           (setq out (nconc (gnus-compress-sequence list t) out)))
4050       (setq out (sort out (lambda (r1 r2)
4051                             (< (or (and (atom r1) r1) (car r1))
4052                                (or (and (atom r2) r2) (car r2))))))
4053       (setq ranges out)
4054       (while ranges
4055         (if (atom (car ranges))
4056             (if (cdr ranges)
4057                 (if (atom (cadr ranges))
4058                     (if (= (1+ (car ranges)) (cadr ranges))
4059                         (progn
4060                           (setcar ranges (cons (car ranges)
4061                                                (cadr ranges)))
4062                           (setcdr ranges (cddr ranges))))
4063                   (if (= (1+ (car ranges)) (caadr ranges))
4064                       (progn
4065                         (setcar (cadr ranges) (car ranges))
4066                         (setcar ranges (cadr ranges))
4067                         (setcdr ranges (cddr ranges))))))
4068           (if (cdr ranges)
4069               (if (atom (cadr ranges))
4070                   (if (= (1+ (cdar ranges)) (cadr ranges))
4071                       (progn
4072                         (setcdr (car ranges) (cadr ranges))
4073                         (setcdr ranges (cddr ranges))))
4074                 (if (= (1+ (cdar ranges)) (caadr ranges))
4075                     (progn
4076                       (setcdr (car ranges) (cdadr ranges))
4077                       (setcdr ranges (cddr ranges)))))))
4078         (setq ranges (cdr ranges)))
4079       out)))
4080
4081 (defun gnus-remove-from-range (ranges list)
4082   "Return a list of ranges that has all articles from LIST removed from RANGES.
4083 Note: LIST has to be sorted over `<'."
4084   ;; !!! This function shouldn't look like this, but I've got a headache.
4085   (gnus-compress-sequence
4086    (gnus-sorted-complement
4087     (gnus-uncompress-range ranges) list)))
4088
4089 (defun gnus-member-of-range (number ranges)
4090   (if (not (listp (cdr ranges)))
4091       (and (>= number (car ranges))
4092            (<= number (cdr ranges)))
4093     (let ((not-stop t))
4094       (while (and ranges
4095                   (if (numberp (car ranges))
4096                       (>= number (car ranges))
4097                     (>= number (caar ranges)))
4098                   not-stop)
4099         (if (if (numberp (car ranges))
4100                 (= number (car ranges))
4101               (and (>= number (caar ranges))
4102                    (<= number (cdar ranges))))
4103             (setq not-stop nil))
4104         (setq ranges (cdr ranges)))
4105       (not not-stop))))
4106
4107 (defun gnus-range-length (range)
4108   "Return the length RANGE would have if uncompressed."
4109   (length (gnus-uncompress-range range)))
4110
4111 (defun gnus-sublist-p (list sublist)
4112   "Test whether all elements in SUBLIST are members of LIST."
4113   (let ((sublistp t))
4114     (while sublist
4115       (unless (memq (pop sublist) list)
4116         (setq sublistp nil
4117               sublist nil)))
4118     sublistp))
4119
4120 \f
4121 ;;;
4122 ;;; Gnus group mode
4123 ;;;
4124
4125 (defvar gnus-group-mode-map nil)
4126 (put 'gnus-group-mode 'mode-class 'special)
4127
4128 (unless gnus-group-mode-map
4129   (setq gnus-group-mode-map (make-keymap))
4130   (suppress-keymap gnus-group-mode-map)
4131
4132   (gnus-define-keys gnus-group-mode-map
4133     " " gnus-group-read-group
4134     "=" gnus-group-select-group
4135     "\r" gnus-group-select-group
4136     "\M-\r" gnus-group-quick-select-group
4137     "j" gnus-group-jump-to-group
4138     "n" gnus-group-next-unread-group
4139     "p" gnus-group-prev-unread-group
4140     "\177" gnus-group-prev-unread-group
4141     [delete] gnus-group-prev-unread-group
4142     "N" gnus-group-next-group
4143     "P" gnus-group-prev-group
4144     "\M-n" gnus-group-next-unread-group-same-level
4145     "\M-p" gnus-group-prev-unread-group-same-level
4146     "," gnus-group-best-unread-group
4147     "." gnus-group-first-unread-group
4148     "u" gnus-group-unsubscribe-current-group
4149     "U" gnus-group-unsubscribe-group
4150     "c" gnus-group-catchup-current
4151     "C" gnus-group-catchup-current-all
4152     "l" gnus-group-list-groups
4153     "L" gnus-group-list-all-groups
4154     "m" gnus-group-mail
4155     "g" gnus-group-get-new-news
4156     "\M-g" gnus-group-get-new-news-this-group
4157     "R" gnus-group-restart
4158     "r" gnus-group-read-init-file
4159     "B" gnus-group-browse-foreign-server
4160     "b" gnus-group-check-bogus-groups
4161     "F" gnus-find-new-newsgroups
4162     "\C-c\C-d" gnus-group-describe-group
4163     "\M-d" gnus-group-describe-all-groups
4164     "\C-c\C-a" gnus-group-apropos
4165     "\C-c\M-\C-a" gnus-group-description-apropos
4166     "a" gnus-group-post-news
4167     "\ek" gnus-group-edit-local-kill
4168     "\eK" gnus-group-edit-global-kill
4169     "\C-k" gnus-group-kill-group
4170     "\C-y" gnus-group-yank-group
4171     "\C-w" gnus-group-kill-region
4172     "\C-x\C-t" gnus-group-transpose-groups
4173     "\C-c\C-l" gnus-group-list-killed
4174     "\C-c\C-x" gnus-group-expire-articles
4175     "\C-c\M-\C-x" gnus-group-expire-all-groups
4176     "V" gnus-version
4177     "s" gnus-group-save-newsrc
4178     "z" gnus-group-suspend
4179     "Z" gnus-group-clear-dribble
4180     "q" gnus-group-exit
4181     "Q" gnus-group-quit
4182     "?" gnus-group-describe-briefly
4183     "\C-c\C-i" gnus-info-find-node
4184     "\M-e" gnus-group-edit-group-method
4185     "^" gnus-group-enter-server-mode
4186     gnus-mouse-2 gnus-mouse-pick-group
4187     "<" beginning-of-buffer
4188     ">" end-of-buffer
4189     "\C-c\C-b" gnus-bug
4190     "\C-c\C-s" gnus-group-sort-groups
4191     "t" gnus-topic-mode
4192     "\C-c\M-g" gnus-activate-all-groups
4193     "\M-&" gnus-group-universal-argument
4194     "#" gnus-group-mark-group
4195     "\M-#" gnus-group-unmark-group)
4196
4197   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4198     "m" gnus-group-mark-group
4199     "u" gnus-group-unmark-group
4200     "w" gnus-group-mark-region
4201     "m" gnus-group-mark-buffer
4202     "r" gnus-group-mark-regexp
4203     "U" gnus-group-unmark-all-groups)
4204
4205   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4206     "d" gnus-group-make-directory-group
4207     "h" gnus-group-make-help-group
4208     "a" gnus-group-make-archive-group
4209     "k" gnus-group-make-kiboze-group
4210     "m" gnus-group-make-group
4211     "E" gnus-group-edit-group
4212     "e" gnus-group-edit-group-method
4213     "p" gnus-group-edit-group-parameters
4214     "v" gnus-group-add-to-virtual
4215     "V" gnus-group-make-empty-virtual
4216     "D" gnus-group-enter-directory
4217     "f" gnus-group-make-doc-group
4218     "r" gnus-group-rename-group
4219     "\177" gnus-group-delete-group
4220     [delete] gnus-group-delete-group)
4221
4222    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4223      "b" gnus-group-brew-soup
4224      "w" gnus-soup-save-areas
4225      "s" gnus-soup-send-replies
4226      "p" gnus-soup-pack-packet
4227      "r" nnsoup-pack-replies)
4228
4229    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4230      "s" gnus-group-sort-groups
4231      "a" gnus-group-sort-groups-by-alphabet
4232      "u" gnus-group-sort-groups-by-unread
4233      "l" gnus-group-sort-groups-by-level
4234      "v" gnus-group-sort-groups-by-score
4235      "r" gnus-group-sort-groups-by-rank
4236      "m" gnus-group-sort-groups-by-method)
4237
4238    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4239      "k" gnus-group-list-killed
4240      "z" gnus-group-list-zombies
4241      "s" gnus-group-list-groups
4242      "u" gnus-group-list-all-groups
4243      "A" gnus-group-list-active
4244      "a" gnus-group-apropos
4245      "d" gnus-group-description-apropos
4246      "m" gnus-group-list-matching
4247      "M" gnus-group-list-all-matching
4248      "l" gnus-group-list-level)
4249
4250    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4251      "f" gnus-score-flush-cache)
4252
4253    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4254      "f" gnus-group-fetch-faq)
4255
4256    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4257      "l" gnus-group-set-current-level
4258      "t" gnus-group-unsubscribe-current-group
4259      "s" gnus-group-unsubscribe-group
4260      "k" gnus-group-kill-group
4261      "y" gnus-group-yank-group
4262      "w" gnus-group-kill-region
4263      "\C-k" gnus-group-kill-level
4264      "z" gnus-group-kill-all-zombies))
4265
4266 (defun gnus-group-mode ()
4267   "Major mode for reading news.
4268
4269 All normal editing commands are switched off.
4270 \\<gnus-group-mode-map>
4271 The group buffer lists (some of) the groups available.  For instance,
4272 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4273 lists all zombie groups.
4274
4275 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4276 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4277
4278 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4279
4280 The following commands are available:
4281
4282 \\{gnus-group-mode-map}"
4283   (interactive)
4284   (when (and menu-bar-mode
4285              (gnus-visual-p 'group-menu 'menu))
4286     (gnus-group-make-menu-bar))
4287   (kill-all-local-variables)
4288   (gnus-simplify-mode-line)
4289   (setq major-mode 'gnus-group-mode)
4290   (setq mode-name "Group")
4291   (gnus-group-set-mode-line)
4292   (setq mode-line-process nil)
4293   (use-local-map gnus-group-mode-map)
4294   (buffer-disable-undo (current-buffer))
4295   (setq truncate-lines t)
4296   (setq buffer-read-only t)
4297   (gnus-make-local-hook 'post-command-hook)
4298   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
4299   (run-hooks 'gnus-group-mode-hook))
4300
4301 (defun gnus-clear-inboxes-moved ()
4302   (setq nnmail-moved-inboxes nil))
4303
4304 (defun gnus-mouse-pick-group (e)
4305   "Enter the group under the mouse pointer."
4306   (interactive "e")
4307   (mouse-set-point e)
4308   (gnus-group-read-group nil))
4309
4310 ;; Look at LEVEL and find out what the level is really supposed to be.
4311 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4312 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4313 (defun gnus-group-default-level (&optional level number-or-nil)
4314   (cond
4315    (gnus-group-use-permanent-levels
4316     (or (setq gnus-group-use-permanent-levels
4317               (or level (if (numberp gnus-group-use-permanent-levels)
4318                             gnus-group-use-permanent-levels
4319                           (or gnus-group-default-list-level
4320                               gnus-level-subscribed))))
4321         gnus-group-default-list-level gnus-level-subscribed))
4322    (number-or-nil
4323     level)
4324    (t
4325     (or level gnus-group-default-list-level gnus-level-subscribed))))
4326
4327 ;;;###autoload
4328 (defun gnus-slave-no-server (&optional arg)
4329   "Read network news as a slave, without connecting to local server"
4330   (interactive "P")
4331   (gnus-no-server arg t))
4332
4333 ;;;###autoload
4334 (defun gnus-no-server (&optional arg slave)
4335   "Read network news.
4336 If ARG is a positive number, Gnus will use that as the
4337 startup level.  If ARG is nil, Gnus will be started at level 2.
4338 If ARG is non-nil and not a positive number, Gnus will
4339 prompt the user for the name of an NNTP server to use.
4340 As opposed to `gnus', this command will not connect to the local server."
4341   (interactive "P")
4342   (let ((val (or arg (1- gnus-level-default-subscribed))))
4343     (gnus val t slave)
4344     (make-local-variable 'gnus-group-use-permanent-levels)
4345     (setq gnus-group-use-permanent-levels val)))
4346
4347 ;;;###autoload
4348 (defun gnus-slave (&optional arg)
4349   "Read news as a slave."
4350   (interactive "P")
4351   (gnus arg nil 'slave))
4352
4353 ;;;###autoload
4354 (defun gnus-other-frame (&optional arg)
4355   "Pop up a frame to read news."
4356   (interactive "P")
4357   (if (get-buffer gnus-group-buffer)
4358       (let ((pop-up-frames t))
4359         (gnus arg))
4360     (select-frame (make-frame))
4361     (gnus arg)))
4362
4363 ;;;###autoload
4364 (defun gnus (&optional arg dont-connect slave)
4365   "Read network news.
4366 If ARG is non-nil and a positive number, Gnus will use that as the
4367 startup level.  If ARG is non-nil and not a positive number, Gnus will
4368 prompt the user for the name of an NNTP server to use."
4369   (interactive "P")
4370
4371   (if (get-buffer gnus-group-buffer)
4372       (progn
4373         (switch-to-buffer gnus-group-buffer)
4374         (gnus-group-get-new-news))
4375
4376     (gnus-clear-system)
4377     (nnheader-init-server-buffer)
4378     (gnus-read-init-file)
4379     (setq gnus-slave slave)
4380
4381     (gnus-group-setup-buffer)
4382     (let ((buffer-read-only nil))
4383       (erase-buffer)
4384       (if (not gnus-inhibit-startup-message)
4385           (progn
4386             (gnus-group-startup-message)
4387             (sit-for 0))))
4388
4389     (let ((level (and (numberp arg) (> arg 0) arg))
4390           did-connect)
4391       (unwind-protect
4392           (progn
4393             (or dont-connect
4394                 (setq did-connect
4395                       (gnus-start-news-server (and arg (not level))))))
4396         (if (and (not dont-connect)
4397                  (not did-connect))
4398             (gnus-group-quit)
4399           (run-hooks 'gnus-startup-hook)
4400           ;; NNTP server is successfully open.
4401
4402           ;; Find the current startup file name.
4403           (setq gnus-current-startup-file
4404                 (gnus-make-newsrc-file gnus-startup-file))
4405
4406           ;; Read the dribble file.
4407           (when (or gnus-slave gnus-use-dribble-file)
4408             (gnus-dribble-read-file))
4409
4410           ;; Allow using GroupLens predictions.
4411           (when gnus-use-grouplens
4412             (bbb-login)
4413             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4414
4415           (gnus-summary-make-display-table)
4416           ;; Do the actual startup.
4417           (gnus-setup-news nil level dont-connect)
4418           ;; Generate the group buffer.
4419           (gnus-group-list-groups level)
4420           (gnus-group-first-unread-group)
4421           (gnus-configure-windows 'group)
4422           (gnus-group-set-mode-line))))))
4423
4424 (defun gnus-unload ()
4425   "Unload all Gnus features."
4426   (interactive)
4427   (or (boundp 'load-history)
4428       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4429   (let ((history load-history)
4430         feature)
4431     (while history
4432       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4433            (setq feature (cdr (assq 'provide (car history))))
4434            (unload-feature feature 'force))
4435       (setq history (cdr history)))))
4436
4437 (defun gnus-compile ()
4438   "Byte-compile the user-defined format specs."
4439   (interactive)
4440   (let ((entries gnus-format-specs)
4441         entry gnus-tmp-func)
4442     (save-excursion
4443       (gnus-message 7 "Compiling format specs...")
4444
4445       (while entries
4446         (setq entry (pop entries))
4447         (if (eq (car entry) 'version)
4448             (setq gnus-format-specs (delq entry gnus-format-specs))
4449           (when (and (listp (caddr entry))
4450                      (not (eq 'byte-code (caaddr entry))))
4451             (fset 'gnus-tmp-func
4452                   `(lambda () ,(caddr entry)))
4453             (byte-compile 'gnus-tmp-func)
4454             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4455
4456       (push (cons 'version emacs-version) gnus-format-specs)
4457
4458       (gnus-message 7 "Compiling user specs...done"))))
4459
4460 (defun gnus-indent-rigidly (start end arg)
4461   "Indent rigidly using only spaces and no tabs."
4462   (save-excursion
4463     (save-restriction
4464       (narrow-to-region start end)
4465       (indent-rigidly start end arg)
4466       (goto-char (point-min))
4467       (while (search-forward "\t" nil t)
4468         (replace-match "        " t t)))))
4469
4470 (defun gnus-group-startup-message (&optional x y)
4471   "Insert startup message in current buffer."
4472   ;; Insert the message.
4473   (erase-buffer)
4474   (insert
4475    (format "              %s
4476           _    ___ _             _
4477           _ ___ __ ___  __    _ ___
4478           __   _     ___    __  ___
4479               _           ___     _
4480              _  _ __             _
4481              ___   __            _
4482                    __           _
4483                     _      _   _
4484                    _      _    _
4485                       _  _    _
4486                   __  ___
4487                  _   _ _     _
4488                 _   _
4489               _    _
4490              _    _
4491             _
4492           __
4493
4494 "
4495            ""))
4496   ;; And then hack it.
4497   (gnus-indent-rigidly (point-min) (point-max)
4498                        (/ (max (- (window-width) (or x 46)) 0) 2))
4499   (goto-char (point-min))
4500   (forward-line 1)
4501   (let* ((pheight (count-lines (point-min) (point-max)))
4502          (wheight (window-height))
4503          (rest (- wheight pheight)))
4504     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4505   ;; Fontify some.
4506   (goto-char (point-min))
4507   (and (search-forward "Praxis" nil t)
4508        (gnus-put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4509   (goto-char (point-min))
4510   (let* ((mode-string (gnus-group-set-mode-line)))
4511     (setq mode-line-buffer-identification
4512           (list (concat gnus-version (substring (car mode-string) 4))))
4513     (set-buffer-modified-p t)))
4514
4515 (defun gnus-group-setup-buffer ()
4516   (or (get-buffer gnus-group-buffer)
4517       (progn
4518         (switch-to-buffer gnus-group-buffer)
4519         (gnus-add-current-to-buffer-list)
4520         (gnus-group-mode)
4521         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4522
4523 (defun gnus-group-list-groups (&optional level unread lowest)
4524   "List newsgroups with level LEVEL or lower that have unread articles.
4525 Default is all subscribed groups.
4526 If argument UNREAD is non-nil, groups with no unread articles are also
4527 listed."
4528   (interactive (list (if current-prefix-arg
4529                          (prefix-numeric-value current-prefix-arg)
4530                        (or
4531                         (gnus-group-default-level nil t)
4532                         gnus-group-default-list-level
4533                         gnus-level-subscribed))))
4534   (or level
4535       (setq level (car gnus-group-list-mode)
4536             unread (cdr gnus-group-list-mode)))
4537   (setq level (gnus-group-default-level level))
4538   (gnus-group-setup-buffer)             ;May call from out of group buffer
4539   (gnus-update-format-specifications)
4540   (let ((case-fold-search nil)
4541         (props (text-properties-at (gnus-point-at-bol)))
4542         (group (gnus-group-group-name)))
4543     (set-buffer gnus-group-buffer)
4544     (funcall gnus-group-prepare-function level unread lowest)
4545     (if (zerop (buffer-size))
4546         (gnus-message 5 gnus-no-groups-message)
4547       (goto-char (point-max))
4548       (when (or (not gnus-group-goto-next-group-function)
4549                 (not (funcall gnus-group-goto-next-group-function 
4550                               group props)))
4551         (if (not group)
4552             ;; Go to the first group with unread articles.
4553             (gnus-group-search-forward t)
4554           ;; Find the right group to put point on.  If the current group
4555           ;; has disappeared in the new listing, try to find the next
4556           ;; one.        If no next one can be found, just leave point at the
4557           ;; first newsgroup in the buffer.
4558           (if (not (gnus-goto-char
4559                     (text-property-any
4560                      (point-min) (point-max)
4561                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4562               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4563                 (while (and newsrc
4564                             (not (gnus-goto-char
4565                                   (text-property-any
4566                                    (point-min) (point-max) 'gnus-group
4567                                    (gnus-intern-safe
4568                                     (caar newsrc) gnus-active-hashtb)))))
4569                   (setq newsrc (cdr newsrc)))
4570                 (or newsrc (progn (goto-char (point-max))
4571                                   (forward-line -1)))))))
4572       ;; Adjust cursor point.
4573       (gnus-group-position-point))))
4574
4575 (defun gnus-group-list-level (level &optional all)
4576   "List groups on LEVEL.
4577 If ALL (the prefix), also list groups that have no unread articles."
4578   (interactive "nList groups on level: \nP")
4579   (gnus-group-list-groups level all level))
4580
4581 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4582   "List all newsgroups with unread articles of level LEVEL or lower.
4583 If ALL is non-nil, list groups that have no unread articles.
4584 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4585 If REGEXP, only list groups matching REGEXP."
4586   (set-buffer gnus-group-buffer)
4587   (let ((buffer-read-only nil)
4588         (newsrc (cdr gnus-newsrc-alist))
4589         (lowest (or lowest 1))
4590         info clevel unread group params)
4591     (erase-buffer)
4592     (if (< lowest gnus-level-zombie)
4593         ;; List living groups.
4594         (while newsrc
4595           (setq info (car newsrc)
4596                 group (gnus-info-group info)
4597                 params (gnus-info-params info)
4598                 newsrc (cdr newsrc)
4599                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4600           (and unread                   ; This group might be bogus
4601                (or (not regexp)
4602                    (string-match regexp group))
4603                (<= (setq clevel (gnus-info-level info)) level)
4604                (>= clevel lowest)
4605                (or all                  ; We list all groups?
4606                    (if (eq unread t)    ; Unactivated?
4607                        gnus-group-list-inactive-groups ; We list unactivated 
4608                      (> unread 0))      ; We list groups with unread articles
4609                    (and gnus-list-groups-with-ticked-articles
4610                         (cdr (assq 'tick (gnus-info-marks info))))
4611                                         ; And groups with tickeds
4612                    ;; Check for permanent visibility.
4613                    (and gnus-permanently-visible-groups
4614                         (string-match gnus-permanently-visible-groups
4615                                       group))
4616                    (memq 'visible params)
4617                    (cdr (assq 'visible params)))
4618                (gnus-group-insert-group-line
4619                 group (gnus-info-level info)
4620                 (gnus-info-marks info) unread (gnus-info-method info)))))
4621
4622     ;; List dead groups.
4623     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4624          (gnus-group-prepare-flat-list-dead
4625           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4626           gnus-level-zombie ?Z
4627           regexp))
4628     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4629          (gnus-group-prepare-flat-list-dead
4630           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4631           gnus-level-killed ?K regexp))
4632
4633     (gnus-group-set-mode-line)
4634     (setq gnus-group-list-mode (cons level all))
4635     (run-hooks 'gnus-group-prepare-hook)))
4636
4637 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4638   ;; List zombies and killed lists somewhat faster, which was
4639   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4640   ;; this by ignoring the group format specification altogether.
4641   (let (group)
4642     (if regexp
4643         ;; This loop is used when listing groups that match some
4644         ;; regexp.
4645         (while groups
4646           (setq group (pop groups))
4647           (when (string-match regexp group)
4648             (gnus-add-text-properties
4649              (point) (prog1 (1+ (point))
4650                        (insert " " mark "     *: " group "\n"))
4651              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4652                    'gnus-unread t
4653                    'gnus-level level))))
4654       ;; This loop is used when listing all groups.
4655       (while groups
4656         (gnus-add-text-properties
4657          (point) (prog1 (1+ (point))
4658                    (insert " " mark "     *: "
4659                            (setq group (pop groups)) "\n"))
4660          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4661                'gnus-unread t
4662                'gnus-level level))))))
4663
4664 (defmacro gnus-group-real-name (group)
4665   "Find the real name of a foreign newsgroup."
4666   `(let ((gname ,group))
4667      (if (string-match ":[^:]+$" gname)
4668          (substring gname (1+ (match-beginning 0)))
4669        gname)))
4670
4671 (defsubst gnus-server-add-address (method)
4672   (let ((method-name (symbol-name (car method))))
4673     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4674              (not (assq (intern (concat method-name "-address")) method)))
4675         (append method (list (list (intern (concat method-name "-address"))
4676                                    (nth 1 method))))
4677       method)))
4678
4679 (defsubst gnus-server-get-method (group method)
4680   ;; Input either a server name, and extended server name, or a
4681   ;; select method, and return a select method.
4682   (cond ((stringp method)
4683          (gnus-server-to-method method))
4684         ((equal method gnus-select-method)
4685          gnus-select-method)
4686         ((and (stringp (car method)) group)
4687          (gnus-server-extend-method group method))
4688         ((and method (not group)
4689               (equal (cadr method) ""))
4690          method)
4691         (t
4692          (gnus-server-add-address method))))
4693
4694 (defun gnus-server-to-method (server)
4695   "Map virtual server names to select methods."
4696   (or 
4697    ;; Is this a method, perhaps?
4698    (and server (listp server) server)
4699    ;; Perhaps this is the native server?
4700    (and (equal server "native") gnus-select-method)
4701    ;; It should be in the server alist.
4702    (cdr (assoc server gnus-server-alist))
4703    ;; If not, we look through all the opened server
4704    ;; to see whether we can find it there.
4705    (let ((opened gnus-opened-servers))
4706      (while (and opened
4707                  (not (equal server (format "%s:%s" (caaar opened)
4708                                             (cadaar opened)))))
4709        (pop opened))
4710      (caar opened))))
4711
4712 (defmacro gnus-method-equal (ss1 ss2)
4713   "Say whether two servers are equal."
4714   `(let ((s1 ,ss1)
4715          (s2 ,ss2))
4716      (or (equal s1 s2)
4717          (and (= (length s1) (length s2))
4718               (progn
4719                 (while (and s1 (member (car s1) s2))
4720                   (setq s1 (cdr s1)))
4721                 (null s1))))))
4722
4723 (defun gnus-server-equal (m1 m2)
4724   "Say whether two methods are equal."
4725   (let ((m1 (cond ((null m1) gnus-select-method)
4726                   ((stringp m1) (gnus-server-to-method m1))
4727                   (t m1)))
4728         (m2 (cond ((null m2) gnus-select-method)
4729                   ((stringp m2) (gnus-server-to-method m2))
4730                   (t m2))))
4731     (gnus-method-equal m1 m2)))
4732
4733 (defun gnus-servers-using-backend (backend)
4734   "Return a list of known servers using BACKEND."
4735   (let ((opened gnus-opened-servers)
4736         out)
4737     (while opened
4738       (when (eq backend (caaar opened))
4739         (push (caar opened) out))
4740       (pop opened))
4741     out))
4742
4743 (defun gnus-group-prefixed-name (group method)
4744   "Return the whole name from GROUP and METHOD."
4745   (and (stringp method) (setq method (gnus-server-to-method method)))
4746   (concat (format "%s" (car method))
4747           (if (and
4748                (or (assoc (format "%s" (car method)) 
4749                           (gnus-methods-using 'address))
4750                    (gnus-server-equal method gnus-message-archive-method))
4751                (nth 1 method)
4752                (not (string= (nth 1 method) "")))
4753               (concat "+" (nth 1 method)))
4754           ":" group))
4755
4756 (defun gnus-group-real-prefix (group)
4757   "Return the prefix of the current group name."
4758   (if (string-match "^[^:]+:" group)
4759       (substring group 0 (match-end 0))
4760     ""))
4761
4762 (defun gnus-group-method (group)
4763   "Return the server or method used for selecting GROUP."
4764   (let ((prefix (gnus-group-real-prefix group)))
4765     (if (equal prefix "")
4766         gnus-select-method
4767       (let ((servers gnus-opened-servers)
4768             (server "")
4769             backend possible found)
4770         (if (string-match "^[^\\+]+\\+" prefix)
4771             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4772                   server (substring prefix (match-end 0) (1- (length prefix))))
4773           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4774         (while servers
4775           (when (eq (caaar servers) backend)
4776             (setq possible (caar servers))
4777             (when (equal (cadaar servers) server)
4778               (setq found (caar servers))))
4779           (pop servers))
4780         (or (car (rassoc found gnus-server-alist))
4781             found
4782             (car (rassoc possible gnus-server-alist))
4783             possible
4784             (list backend server))))))
4785
4786 (defsubst gnus-secondary-method-p (method)
4787   "Return whether METHOD is a secondary select method."
4788   (let ((methods gnus-secondary-select-methods)
4789         (gmethod (gnus-server-get-method nil method)))
4790     (while (and methods
4791                 (not (equal (gnus-server-get-method nil (car methods))
4792                             gmethod)))
4793       (setq methods (cdr methods)))
4794     methods))
4795
4796 (defun gnus-group-foreign-p (group)
4797   "Say whether a group is foreign or not."
4798   (and (not (gnus-group-native-p group))
4799        (not (gnus-group-secondary-p group))))
4800
4801 (defun gnus-group-native-p (group)
4802   "Say whether the group is native or not."
4803   (not (string-match ":" group)))
4804
4805 (defun gnus-group-secondary-p (group)
4806   "Say whether the group is secondary or not."
4807   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4808
4809 (defun gnus-group-get-parameter (group &optional symbol)
4810   "Returns the group parameters for GROUP.
4811 If SYMBOL, return the value of that symbol in the group parameters."
4812   (let ((params (gnus-info-params (gnus-get-info group))))
4813     (if symbol
4814         (gnus-group-parameter-value params symbol)
4815       params)))
4816
4817 (defun gnus-group-parameter-value (params symbol)
4818   "Return the value of SYMBOL in group PARAMS."
4819   (or (car (memq symbol params))        ; It's either a simple symbol
4820       (cdr (assq symbol params))))      ; or a cons.
4821
4822 (defun gnus-group-add-parameter (group param)
4823   "Add parameter PARAM to GROUP."
4824   (let ((info (gnus-get-info group)))
4825     (if (not info)
4826         () ; This is a dead group.  We just ignore it.
4827       ;; Cons the new param to the old one and update.
4828       (gnus-group-set-info (cons param (gnus-info-params info))
4829                            group 'params))))
4830
4831 (defun gnus-group-set-parameter (group name value)
4832   "Set parameter NAME to VALUE in GROUP."
4833   (let ((info (gnus-get-info group)))
4834     (if (not info)
4835         () ; This is a dead group.  We just ignore it.
4836       (let ((old-params (gnus-info-params info))
4837             (new-params (list (cons name value))))
4838         (while old-params
4839           (if (or (not (listp (car old-params)))
4840                   (not (eq (caar old-params) name)))
4841               (setq new-params (append new-params (list (car old-params)))))
4842           (setq old-params (cdr old-params)))
4843         (gnus-group-set-info new-params group 'params)))))
4844
4845 (defun gnus-group-add-score (group &optional score)
4846   "Add SCORE to the GROUP score.
4847 If SCORE is nil, add 1 to the score of GROUP."
4848   (let ((info (gnus-get-info group)))
4849     (when info
4850       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4851
4852 (defun gnus-summary-bubble-group ()
4853   "Increase the score of the current group.
4854 This is a handy function to add to `gnus-summary-exit-hook' to
4855 increase the score of each group you read."
4856   (gnus-group-add-score gnus-newsgroup-name))
4857
4858 (defun gnus-group-set-info (info &optional method-only-group part)
4859   (let* ((entry (gnus-gethash
4860                  (or method-only-group (gnus-info-group info))
4861                  gnus-newsrc-hashtb))
4862          (part-info info)
4863          (info (if method-only-group (nth 2 entry) info))
4864          method)
4865     (when method-only-group
4866       (unless entry
4867         (error "Trying to change non-existent group %s" method-only-group))
4868       ;; We have received parts of the actual group info - either the
4869       ;; select method or the group parameters.  We first check
4870       ;; whether we have to extend the info, and if so, do that.
4871       (let ((len (length info))
4872             (total (if (eq part 'method) 5 6)))
4873         (when (< len total)
4874           (setcdr (nthcdr (1- len) info)
4875                   (make-list (- total len) nil)))
4876         ;; Then we enter the new info.
4877         (setcar (nthcdr (1- total) info) part-info)))
4878     (unless entry
4879       ;; This is a new group, so we just create it.
4880       (save-excursion
4881         (set-buffer gnus-group-buffer)
4882         (setq method (gnus-info-method info))
4883         (when (gnus-server-equal method "native")
4884           (setq method nil))
4885         (save-excursion
4886           (set-buffer gnus-group-buffer)
4887           (if method
4888               ;; It's a foreign group...
4889               (gnus-group-make-group
4890                (gnus-group-real-name (gnus-info-group info))
4891                (if (stringp method) method
4892                  (prin1-to-string (car method)))
4893                (and (consp method)
4894                     (nth 1 (gnus-info-method info))))
4895             ;; It's a native group.
4896             (gnus-group-make-group (gnus-info-group info))))
4897         (gnus-message 6 "Note: New group created")
4898         (setq entry
4899               (gnus-gethash (gnus-group-prefixed-name
4900                              (gnus-group-real-name (gnus-info-group info))
4901                              (or (gnus-info-method info) gnus-select-method))
4902                             gnus-newsrc-hashtb))))
4903     ;; Whether it was a new group or not, we now have the entry, so we
4904     ;; can do the update.
4905     (if entry
4906         (progn
4907           (setcar (nthcdr 2 entry) info)
4908           (when (and (not (eq (car entry) t))
4909                      (gnus-active (gnus-info-group info)))
4910             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4911       (error "No such group: %s" (gnus-info-group info)))))
4912
4913 (defun gnus-group-set-method-info (group select-method)
4914   (gnus-group-set-info select-method group 'method))
4915
4916 (defun gnus-group-set-params-info (group params)
4917   (gnus-group-set-info params group 'params))
4918
4919 (defun gnus-group-update-group-line ()
4920   "Update the current line in the group buffer."
4921   (let* ((buffer-read-only nil)
4922          (group (gnus-group-group-name))
4923          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
4924          gnus-group-indentation)
4925     (and entry
4926          (not (gnus-ephemeral-group-p group))
4927          (gnus-dribble-enter
4928           (concat "(gnus-group-set-info '"
4929                   (prin1-to-string (nth 2 entry)) ")")))
4930     (setq gnus-group-indentation (gnus-group-group-indentation))
4931     (gnus-delete-line)
4932     (gnus-group-insert-group-line-info group)
4933     (forward-line -1)
4934     (gnus-group-position-point)))
4935
4936 (defun gnus-group-insert-group-line-info (group)
4937   "Insert GROUP on the current line."
4938   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4939         active info)
4940     (if entry
4941         (progn
4942           ;; (Un)subscribed group.
4943           (setq info (nth 2 entry))
4944           (gnus-group-insert-group-line
4945            group (gnus-info-level info) (gnus-info-marks info)
4946            (or (car entry) t) (gnus-info-method info)))
4947       ;; This group is dead.
4948       (gnus-group-insert-group-line
4949        group
4950        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4951        nil
4952        (if (setq active (gnus-active group))
4953            (- (1+ (cdr active)) (car active)) 0)
4954        nil))))
4955
4956 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4957                                                     gnus-tmp-marked number
4958                                                     gnus-tmp-method)
4959   "Insert a group line in the group buffer."
4960   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4961          (gnus-tmp-number-total
4962           (if gnus-tmp-active
4963               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4964             0))
4965          (gnus-tmp-number-of-unread
4966           (if (numberp number) (int-to-string (max 0 number))
4967             "*"))
4968          (gnus-tmp-number-of-read
4969           (if (numberp number)
4970               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4971             "*"))
4972          (gnus-tmp-subscribed
4973           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4974                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4975                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4976                 (t ?K)))
4977          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4978          (gnus-tmp-newsgroup-description
4979           (if gnus-description-hashtb
4980               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4981             ""))
4982          (gnus-tmp-moderated
4983           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4984          (gnus-tmp-moderated-string
4985           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4986          (gnus-tmp-method
4987           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4988          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4989          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4990          (gnus-tmp-news-method-string
4991           (if gnus-tmp-method
4992               (format "(%s:%s)" (car gnus-tmp-method)
4993                       (cadr gnus-tmp-method)) ""))
4994          (gnus-tmp-marked-mark
4995           (if (and (numberp number)
4996                    (zerop number)
4997                    (cdr (assq 'tick gnus-tmp-marked)))
4998               ?* ? ))
4999          (gnus-tmp-process-marked
5000           (if (member gnus-tmp-group gnus-group-marked)
5001               gnus-process-mark ? ))
5002          (gnus-tmp-grouplens
5003           (or (and gnus-use-grouplens
5004                    (bbb-grouplens-group-p gnus-tmp-group))
5005               ""))
5006          (buffer-read-only nil)
5007          header gnus-tmp-header)        ; passed as parameter to user-funcs.
5008     (beginning-of-line)
5009     (gnus-add-text-properties
5010      (point)
5011      (prog1 (1+ (point))
5012        ;; Insert the text.
5013        (eval gnus-group-line-format-spec))
5014      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
5015        gnus-unread ,(if (numberp number)
5016                         (string-to-int gnus-tmp-number-of-unread)
5017                       t)
5018        gnus-marked ,gnus-tmp-marked-mark
5019        gnus-indentation ,gnus-group-indentation
5020        gnus-level ,gnus-tmp-level))
5021     (when (inline (gnus-visual-p 'group-highlight 'highlight))
5022       (forward-line -1)
5023       (run-hooks 'gnus-group-update-hook)
5024       (forward-line))
5025     ;; Allow XEmacs to remove front-sticky text properties.
5026     (gnus-group-remove-excess-properties)))
5027
5028 (defun gnus-group-update-group (group &optional visible-only)
5029   "Update all lines where GROUP appear.
5030 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
5031 already."
5032   (save-excursion
5033     (set-buffer gnus-group-buffer)
5034     ;; The buffer may be narrowed.
5035     (save-restriction
5036       (widen)
5037       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
5038             (loc (point-min))
5039             found buffer-read-only)
5040         ;; Enter the current status into the dribble buffer.
5041         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
5042           (if (and entry (not (gnus-ephemeral-group-p group)))
5043               (gnus-dribble-enter
5044                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
5045                        ")"))))
5046         ;; Find all group instances.  If topics are in use, each group
5047         ;; may be listed in more than once.
5048         (while (setq loc (text-property-any
5049                           loc (point-max) 'gnus-group ident))
5050           (setq found t)
5051           (goto-char loc)
5052           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5053             (gnus-delete-line)
5054             (gnus-group-insert-group-line-info group)
5055             (save-excursion
5056               (forward-line -1)
5057               (run-hooks 'gnus-group-update-group-hook)))
5058           (setq loc (1+ loc)))
5059         (unless (or found visible-only)
5060           ;; No such line in the buffer, find out where it's supposed to
5061           ;; go, and insert it there (or at the end of the buffer).
5062           (if gnus-goto-missing-group-function
5063               (funcall gnus-goto-missing-group-function group)
5064             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
5065               (while (and entry (car entry)
5066                           (not
5067                            (gnus-goto-char
5068                             (text-property-any
5069                              (point-min) (point-max)
5070                              'gnus-group (gnus-intern-safe
5071                                           (caar entry) gnus-active-hashtb)))))
5072                 (setq entry (cdr entry)))
5073               (or entry (goto-char (point-max)))))
5074           ;; Finally insert the line.
5075           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5076             (gnus-group-insert-group-line-info group)
5077             (save-excursion
5078               (forward-line -1)
5079               (run-hooks 'gnus-group-update-group-hook))))
5080         (gnus-group-set-mode-line)))))
5081
5082 (defun gnus-group-set-mode-line ()
5083   "Update the mode line in the group buffer."
5084   (when (memq 'group gnus-updated-mode-lines)
5085     ;; Yes, we want to keep this mode line updated.
5086     (save-excursion
5087       (set-buffer gnus-group-buffer)
5088       (let* ((gformat (or gnus-group-mode-line-format-spec
5089                           (setq gnus-group-mode-line-format-spec
5090                                 (gnus-parse-format
5091                                  gnus-group-mode-line-format
5092                                  gnus-group-mode-line-format-alist))))
5093              (gnus-tmp-news-server (cadr gnus-select-method))
5094              (gnus-tmp-news-method (car gnus-select-method))
5095              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
5096              (max-len 60)
5097              gnus-tmp-header            ;Dummy binding for user-defined formats
5098              ;; Get the resulting string.
5099              (mode-string (eval gformat)))
5100         ;; Say whether the dribble buffer has been modified.
5101         (setq mode-line-modified
5102               (if (and gnus-dribble-buffer
5103                        (buffer-name gnus-dribble-buffer)
5104                        (buffer-modified-p gnus-dribble-buffer)
5105                        (save-excursion
5106                          (set-buffer gnus-dribble-buffer)
5107                          (not (zerop (buffer-size)))))
5108                   "---*- " "----- "))
5109         ;; If the line is too long, we chop it off.
5110         (when (> (length mode-string) max-len)
5111           (setq mode-string (substring mode-string 0 (- max-len 4))))
5112         (prog1
5113             (setq mode-line-buffer-identification 
5114                   (list mode-string))
5115           (set-buffer-modified-p t))))))
5116
5117 (defun gnus-group-group-name ()
5118   "Get the name of the newsgroup on the current line."
5119   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5120     (and group (symbol-name group))))
5121
5122 (defun gnus-group-group-level ()
5123   "Get the level of the newsgroup on the current line."
5124   (get-text-property (gnus-point-at-bol) 'gnus-level))
5125
5126 (defun gnus-group-group-indentation ()
5127   "Get the indentation of the newsgroup on the current line."
5128   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5129       (and gnus-group-indentation-function
5130            (funcall gnus-group-indentation-function))
5131       ""))
5132
5133 (defun gnus-group-group-unread ()
5134   "Get the number of unread articles of the newsgroup on the current line."
5135   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5136
5137 (defun gnus-group-search-forward (&optional backward all level first-too)
5138   "Find the next newsgroup with unread articles.
5139 If BACKWARD is non-nil, find the previous newsgroup instead.
5140 If ALL is non-nil, just find any newsgroup.
5141 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5142 group exists.
5143 If FIRST-TOO, the current line is also eligible as a target."
5144   (let ((way (if backward -1 1))
5145         (low gnus-level-killed)
5146         (beg (point))
5147         pos found lev)
5148     (if (and backward (progn (beginning-of-line)) (bobp))
5149         nil
5150       (or first-too (forward-line way))
5151       (while (and
5152               (not (eobp))
5153               (not (setq
5154                     found
5155                     (and (or all
5156                              (and
5157                               (let ((unread
5158                                      (get-text-property (point) 'gnus-unread)))
5159                                 (and (numberp unread) (> unread 0)))
5160                               (setq lev (get-text-property (point)
5161                                                            'gnus-level))
5162                               (<= lev gnus-level-subscribed)))
5163                          (or (not level)
5164                              (and (setq lev (get-text-property (point)
5165                                                                'gnus-level))
5166                                   (or (= lev level)
5167                                       (and (< lev low)
5168                                            (< level lev)
5169                                            (progn
5170                                              (setq low lev)
5171                                              (setq pos (point))
5172                                              nil))))))))
5173               (zerop (forward-line way)))))
5174     (if found
5175         (progn (gnus-group-position-point) t)
5176       (goto-char (or pos beg))
5177       (and pos t))))
5178
5179 ;;; Gnus group mode commands
5180
5181 ;; Group marking.
5182
5183 (defun gnus-group-mark-group (n &optional unmark no-advance)
5184   "Mark the current group."
5185   (interactive "p")
5186   (let ((buffer-read-only nil)
5187         group)
5188     (while (and (> n 0)
5189                 (not (eobp)))
5190       (when (setq group (gnus-group-group-name))
5191         ;; Update the mark.
5192         (beginning-of-line)
5193         (forward-char
5194          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5195         (delete-char 1)
5196         (if unmark
5197             (progn
5198               (insert " ")
5199               (setq gnus-group-marked (delete group gnus-group-marked)))
5200           (insert "#")
5201           (setq gnus-group-marked
5202                 (cons group (delete group gnus-group-marked)))))
5203       (or no-advance (gnus-group-next-group 1))
5204       (decf n))
5205     (gnus-summary-position-point)
5206     n))
5207
5208 (defun gnus-group-unmark-group (n)
5209   "Remove the mark from the current group."
5210   (interactive "p")
5211   (gnus-group-mark-group n 'unmark)
5212   (gnus-group-position-point))
5213
5214 (defun gnus-group-unmark-all-groups ()
5215   "Unmark all groups."
5216   (interactive)
5217   (let ((groups gnus-group-marked))
5218     (save-excursion
5219       (while groups
5220         (gnus-group-remove-mark (pop groups)))))
5221   (gnus-group-position-point))
5222
5223 (defun gnus-group-mark-region (unmark beg end)
5224   "Mark all groups between point and mark.
5225 If UNMARK, remove the mark instead."
5226   (interactive "P\nr")
5227   (let ((num (count-lines beg end)))
5228     (save-excursion
5229       (goto-char beg)
5230       (- num (gnus-group-mark-group num unmark)))))
5231
5232 (defun gnus-group-mark-buffer (&optional unmark)
5233   "Mark all groups in the buffer.
5234 If UNMARK, remove the mark instead."
5235   (interactive "P")
5236   (gnus-group-mark-region unmark (point-min) (point-max)))
5237
5238 (defun gnus-group-mark-regexp (regexp)
5239   "Mark all groups that match some regexp."
5240   (interactive "sMark (regexp): ")
5241   (let ((alist (cdr gnus-newsrc-alist))
5242         group)
5243     (while alist
5244       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5245         (gnus-group-set-mark group))))
5246   (gnus-group-position-point))
5247
5248 (defun gnus-group-remove-mark (group)
5249   "Remove the process mark from GROUP and move point there.
5250 Return nil if the group isn't displayed."
5251   (if (gnus-group-goto-group group)
5252       (save-excursion
5253         (gnus-group-mark-group 1 'unmark t)
5254         t)
5255     (setq gnus-group-marked
5256           (delete group gnus-group-marked))
5257     nil))
5258
5259 (defun gnus-group-set-mark (group)
5260   "Set the process mark on GROUP."
5261   (if (gnus-group-goto-group group) 
5262       (save-excursion
5263         (gnus-group-mark-group 1 nil t))
5264     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5265
5266 (defun gnus-group-universal-argument (arg &optional groups func)
5267   "Perform any command on all groups accoring to the process/prefix convention."
5268   (interactive "P")
5269   (let ((groups (or groups (gnus-group-process-prefix arg)))
5270         group func)
5271     (if (eq (setq func (or func
5272                            (key-binding
5273                             (read-key-sequence
5274                              (substitute-command-keys
5275                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5276             'undefined)
5277         (gnus-error 1 "Undefined key")
5278       (while groups
5279         (gnus-group-remove-mark (setq group (pop groups)))
5280         (command-execute func))))
5281   (gnus-group-position-point))
5282
5283 (defun gnus-group-process-prefix (n)
5284   "Return a list of groups to work on.
5285 Take into consideration N (the prefix) and the list of marked groups."
5286   (cond
5287    (n
5288     (setq n (prefix-numeric-value n))
5289     ;; There is a prefix, so we return a list of the N next
5290     ;; groups.
5291     (let ((way (if (< n 0) -1 1))
5292           (n (abs n))
5293           group groups)
5294       (save-excursion
5295         (while (and (> n 0)
5296                     (setq group (gnus-group-group-name)))
5297           (setq groups (cons group groups))
5298           (setq n (1- n))
5299           (gnus-group-next-group way)))
5300       (nreverse groups)))
5301    ((and (boundp 'transient-mark-mode)
5302          transient-mark-mode
5303          (boundp 'mark-active)
5304          mark-active)
5305     ;; Work on the region between point and mark.
5306     (let ((max (max (point) (mark)))
5307           groups)
5308       (save-excursion
5309         (goto-char (min (point) (mark)))
5310         (while
5311             (and
5312              (push (gnus-group-group-name) groups)
5313              (zerop (gnus-group-next-group 1))
5314              (< (point) max)))
5315         (nreverse groups))))
5316    (gnus-group-marked
5317     ;; No prefix, but a list of marked articles.
5318     (reverse gnus-group-marked))
5319    (t
5320     ;; Neither marked articles or a prefix, so we return the
5321     ;; current group.
5322     (let ((group (gnus-group-group-name)))
5323       (and group (list group))))))
5324
5325 ;; Selecting groups.
5326
5327 (defun gnus-group-read-group (&optional all no-article group)
5328   "Read news in this newsgroup.
5329 If the prefix argument ALL is non-nil, already read articles become
5330 readable.  IF ALL is a number, fetch this number of articles.  If the
5331 optional argument NO-ARTICLE is non-nil, no article will be
5332 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5333 group."
5334   (interactive "P")
5335   (let ((group (or group (gnus-group-group-name)))
5336         number active marked entry)
5337     (or group (error "No group on current line"))
5338     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5339                                             group gnus-newsrc-hashtb)))))
5340     ;; This group might be a dead group.  In that case we have to get
5341     ;; the number of unread articles from `gnus-active-hashtb'.
5342     (setq number
5343           (cond ((numberp all) all)
5344                 (entry (car entry))
5345                 ((setq active (gnus-active group))
5346                  (- (1+ (cdr active)) (car active)))))
5347     (gnus-summary-read-group
5348      group (or all (and (numberp number)
5349                         (zerop (+ number (length (cdr (assq 'tick marked)))
5350                                   (length (cdr (assq 'dormant marked)))))))
5351      no-article)))
5352
5353 (defun gnus-group-select-group (&optional all)
5354   "Select this newsgroup.
5355 No article is selected automatically.
5356 If ALL is non-nil, already read articles become readable.
5357 If ALL is a number, fetch this number of articles."
5358   (interactive "P")
5359   (gnus-group-read-group all t))
5360
5361 (defun gnus-group-quick-select-group (&optional all)
5362   "Select the current group \"quickly\".
5363 This means that no highlighting or scoring will be performed."
5364   (interactive "P")
5365   (let (gnus-visual
5366         gnus-score-find-score-files-function
5367         gnus-apply-kill-hook
5368         gnus-summary-expunge-below)
5369     (gnus-group-read-group all t)))
5370
5371 (defun gnus-group-visible-select-group (&optional all)
5372   "Select the current group without hiding any articles."
5373   (interactive "P")
5374   (let ((gnus-inhibit-limiting t))
5375     (gnus-group-read-group all t)))
5376
5377 ;;;###autoload
5378 (defun gnus-fetch-group (group)
5379   "Start Gnus if necessary and enter GROUP.
5380 Returns whether the fetching was successful or not."
5381   (interactive "sGroup name: ")
5382   (or (get-buffer gnus-group-buffer)
5383       (gnus))
5384   (gnus-group-read-group nil nil group))
5385
5386 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5387 ;; if selection was successful.
5388 (defun gnus-group-read-ephemeral-group
5389   (group method &optional activate quit-config)
5390   (let ((group (if (gnus-group-foreign-p group) group
5391                  (gnus-group-prefixed-name group method))))
5392     (gnus-sethash
5393      group
5394      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5395                      ((quit-config . ,(if quit-config quit-config
5396                                         (cons (current-buffer) 'summary))))))
5397      gnus-newsrc-hashtb)
5398     (set-buffer gnus-group-buffer)
5399     (or (gnus-check-server method)
5400         (error "Unable to contact server: %s" (gnus-status-message method)))
5401     (if activate (or (gnus-request-group group)
5402                      (error "Couldn't request group")))
5403     (condition-case ()
5404         (gnus-group-read-group t t group)
5405       (error nil)
5406       (quit nil))))
5407
5408 (defun gnus-group-jump-to-group (group)
5409   "Jump to newsgroup GROUP."
5410   (interactive
5411    (list (completing-read
5412           "Group: " gnus-active-hashtb nil
5413           (gnus-read-active-file-p)
5414           nil
5415           'gnus-group-history)))
5416
5417   (when (equal group "")
5418     (error "Empty group name"))
5419
5420   (when (string-match "[\000-\032]" group)
5421     (error "Control characters in group: %s" group))
5422
5423   (let ((b (text-property-any
5424             (point-min) (point-max)
5425             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5426     (unless (gnus-ephemeral-group-p group)
5427       (if b
5428           ;; Either go to the line in the group buffer...
5429           (goto-char b)
5430         ;; ... or insert the line.
5431         (or
5432          (gnus-active group)
5433          (gnus-activate-group group)
5434          (error "%s error: %s" group (gnus-status-message group)))
5435
5436         (gnus-group-update-group group)
5437         (goto-char (text-property-any
5438                     (point-min) (point-max)
5439                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5440     ;; Adjust cursor point.
5441     (gnus-group-position-point)))
5442
5443 (defun gnus-group-goto-group (group)
5444   "Goto to newsgroup GROUP."
5445   (when group
5446     (let ((b (text-property-any (point-min) (point-max)
5447                                 'gnus-group (gnus-intern-safe
5448                                              group gnus-active-hashtb))))
5449       (and b (goto-char b)))))
5450
5451 (defun gnus-group-next-group (n)
5452   "Go to next N'th newsgroup.
5453 If N is negative, search backward instead.
5454 Returns the difference between N and the number of skips actually
5455 done."
5456   (interactive "p")
5457   (gnus-group-next-unread-group n t))
5458
5459 (defun gnus-group-next-unread-group (n &optional all level)
5460   "Go to next N'th unread newsgroup.
5461 If N is negative, search backward instead.
5462 If ALL is non-nil, choose any newsgroup, unread or not.
5463 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5464 such group can be found, the next group with a level higher than
5465 LEVEL.
5466 Returns the difference between N and the number of skips actually
5467 made."
5468   (interactive "p")
5469   (let ((backward (< n 0))
5470         (n (abs n)))
5471     (while (and (> n 0)
5472                 (gnus-group-search-forward
5473                  backward (or (not gnus-group-goto-unread) all) level))
5474       (setq n (1- n)))
5475     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5476                                (if level " on this level or higher" "")))
5477     n))
5478
5479 (defun gnus-group-prev-group (n)
5480   "Go to previous N'th newsgroup.
5481 Returns the difference between N and the number of skips actually
5482 done."
5483   (interactive "p")
5484   (gnus-group-next-unread-group (- n) t))
5485
5486 (defun gnus-group-prev-unread-group (n)
5487   "Go to previous N'th unread newsgroup.
5488 Returns the difference between N and the number of skips actually
5489 done."
5490   (interactive "p")
5491   (gnus-group-next-unread-group (- n)))
5492
5493 (defun gnus-group-next-unread-group-same-level (n)
5494   "Go to next N'th unread newsgroup on the same level.
5495 If N is negative, search backward instead.
5496 Returns the difference between N and the number of skips actually
5497 done."
5498   (interactive "p")
5499   (gnus-group-next-unread-group n t (gnus-group-group-level))
5500   (gnus-group-position-point))
5501
5502 (defun gnus-group-prev-unread-group-same-level (n)
5503   "Go to next N'th unread newsgroup on the same level.
5504 Returns the difference between N and the number of skips actually
5505 done."
5506   (interactive "p")
5507   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5508   (gnus-group-position-point))
5509
5510 (defun gnus-group-best-unread-group (&optional exclude-group)
5511   "Go to the group with the highest level.
5512 If EXCLUDE-GROUP, do not go to that group."
5513   (interactive)
5514   (goto-char (point-min))
5515   (let ((best 100000)
5516         unread best-point)
5517     (while (not (eobp))
5518       (setq unread (get-text-property (point) 'gnus-unread))
5519       (if (and (numberp unread) (> unread 0))
5520           (progn
5521             (if (and (get-text-property (point) 'gnus-level)
5522                      (< (get-text-property (point) 'gnus-level) best)
5523                      (or (not exclude-group)
5524                          (not (equal exclude-group (gnus-group-group-name)))))
5525                 (progn
5526                   (setq best (get-text-property (point) 'gnus-level))
5527                   (setq best-point (point))))))
5528       (forward-line 1))
5529     (if best-point (goto-char best-point))
5530     (gnus-summary-position-point)
5531     (and best-point (gnus-group-group-name))))
5532
5533 (defun gnus-group-first-unread-group ()
5534   "Go to the first group with unread articles."
5535   (interactive)
5536   (prog1
5537       (let ((opoint (point))
5538             unread)
5539         (goto-char (point-min))
5540         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5541                 (and (numberp unread)   ; Not a topic.
5542                      (not (zerop unread))) ; Has unread articles.
5543                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5544             (point)                     ; Success.
5545           (goto-char opoint)
5546           nil))                         ; Not success.
5547     (gnus-group-position-point)))
5548
5549 (defun gnus-group-enter-server-mode ()
5550   "Jump to the server buffer."
5551   (interactive)
5552   (gnus-enter-server-buffer))
5553
5554 (defun gnus-group-make-group (name &optional method address)
5555   "Add a new newsgroup.
5556 The user will be prompted for a NAME, for a select METHOD, and an
5557 ADDRESS."
5558   (interactive
5559    (cons
5560     (read-string "Group name: ")
5561     (let ((method
5562            (completing-read
5563             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5564             nil t nil 'gnus-method-history)))
5565       (cond ((assoc method gnus-valid-select-methods)
5566              (list method
5567                    (if (memq 'prompt-address
5568                              (assoc method gnus-valid-select-methods))
5569                        (read-string "Address: ")
5570                      "")))
5571             ((assoc method gnus-server-alist)
5572              (list method))
5573             (t
5574              (list method ""))))))
5575
5576   (let* ((meth (and method (if address (list (intern method) address)
5577                              method)))
5578          (nname (if method (gnus-group-prefixed-name name meth) name))
5579          backend info)
5580     (when (gnus-gethash nname gnus-newsrc-hashtb)
5581       (error "Group %s already exists" nname))
5582     ;; Subscribe to the new group.
5583     (gnus-group-change-level
5584      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5585      gnus-level-default-subscribed gnus-level-killed
5586      (and (gnus-group-group-name)
5587           (gnus-gethash (gnus-group-group-name)
5588                         gnus-newsrc-hashtb))
5589      t)
5590     ;; Make it active.
5591     (gnus-set-active nname (cons 1 0))
5592     (or (gnus-ephemeral-group-p name)
5593         (gnus-dribble-enter
5594          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5595     ;; Insert the line.
5596     (gnus-group-insert-group-line-info nname)
5597     (forward-line -1)
5598     (gnus-group-position-point)
5599
5600     ;; Load the backend and try to make the backend create
5601     ;; the group as well.
5602     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5603                                                   nil meth))))
5604                  gnus-valid-select-methods)
5605       (require backend))
5606     (gnus-check-server meth)
5607     (and (gnus-check-backend-function 'request-create-group nname)
5608          (gnus-request-create-group nname))
5609     t))
5610
5611 (defun gnus-group-delete-group (group &optional force)
5612   "Delete the current group.
5613 If FORCE (the prefix) is non-nil, all the articles in the group will
5614 be deleted.  This is \"deleted\" as in \"removed forever from the face
5615 of the Earth\".  There is no undo."
5616   (interactive
5617    (list (gnus-group-group-name)
5618          current-prefix-arg))
5619   (or group (error "No group to rename"))
5620   (or (gnus-check-backend-function 'request-delete-group group)
5621       (error "This backend does not support group deletion"))
5622   (prog1
5623       (if (not (gnus-yes-or-no-p
5624                 (format
5625                  "Do you really want to delete %s%s? "
5626                  group (if force " and all its contents" ""))))
5627           () ; Whew!
5628         (gnus-message 6 "Deleting group %s..." group)
5629         (if (not (gnus-request-delete-group group force))
5630             (gnus-error 3 "Couldn't delete group %s" group)
5631           (gnus-message 6 "Deleting group %s...done" group)
5632           (gnus-group-goto-group group)
5633           (gnus-group-kill-group 1 t)
5634           (gnus-sethash group nil gnus-active-hashtb)
5635           t))
5636     (gnus-group-position-point)))
5637
5638 (defun gnus-group-rename-group (group new-name)
5639   (interactive
5640    (list
5641     (gnus-group-group-name)
5642     (progn
5643       (or (gnus-check-backend-function
5644            'request-rename-group (gnus-group-group-name))
5645           (error "This backend does not support renaming groups"))
5646       (read-string "New group name: "))))
5647
5648   (or (gnus-check-backend-function 'request-rename-group group)
5649       (error "This backend does not support renaming groups"))
5650
5651   (or group (error "No group to rename"))
5652   (and (string-match "^[ \t]*$" new-name)
5653        (error "Not a valid group name"))
5654
5655   ;; We find the proper prefixed name.
5656   (setq new-name
5657         (gnus-group-prefixed-name
5658          (gnus-group-real-name new-name)
5659          (gnus-info-method (gnus-get-info group))))
5660
5661   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5662   (prog1
5663       (if (not (gnus-request-rename-group group new-name))
5664           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5665         ;; We rename the group internally by killing it...
5666         (gnus-group-goto-group group)
5667         (gnus-group-kill-group)
5668         ;; ... changing its name ...
5669         (setcar (cdar gnus-list-of-killed-groups) new-name)
5670         ;; ... and then yanking it.  Magic!
5671         (gnus-group-yank-group)
5672         (gnus-set-active new-name (gnus-active group))
5673         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5674         new-name)
5675     (gnus-group-position-point)))
5676
5677 (defun gnus-group-edit-group (group &optional part)
5678   "Edit the group on the current line."
5679   (interactive (list (gnus-group-group-name)))
5680   (let* ((part (or part 'info))
5681          (done-func `(lambda ()
5682                        "Exit editing mode and update the information."
5683                        (interactive)
5684                        (gnus-group-edit-group-done ',part ,group)))
5685          (winconf (current-window-configuration))
5686          info)
5687     (or group (error "No group on current line"))
5688     (or (setq info (gnus-get-info group))
5689         (error "Killed group; can't be edited"))
5690     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5691     (gnus-configure-windows 'edit-group)
5692     (gnus-add-current-to-buffer-list)
5693     (emacs-lisp-mode)
5694     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5695     (use-local-map (copy-keymap emacs-lisp-mode-map))
5696     (local-set-key "\C-c\C-c" done-func)
5697     (make-local-variable 'gnus-prev-winconf)
5698     (setq gnus-prev-winconf winconf)
5699     (erase-buffer)
5700     (insert
5701      (cond
5702       ((eq part 'method)
5703        ";; Type `C-c C-c' after editing the select method.\n\n")
5704       ((eq part 'params)
5705        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5706       ((eq part 'info)
5707        ";; Type `C-c C-c' after editing the group info.\n\n")))
5708     (insert
5709      (pp-to-string
5710       (cond ((eq part 'method)
5711              (or (gnus-info-method info) "native"))
5712             ((eq part 'params)
5713              (gnus-info-params info))
5714             (t info)))
5715      "\n")))
5716
5717 (defun gnus-group-edit-group-method (group)
5718   "Edit the select method of GROUP."
5719   (interactive (list (gnus-group-group-name)))
5720   (gnus-group-edit-group group 'method))
5721
5722 (defun gnus-group-edit-group-parameters (group)
5723   "Edit the group parameters of GROUP."
5724   (interactive (list (gnus-group-group-name)))
5725   (gnus-group-edit-group group 'params))
5726
5727 (defun gnus-group-edit-group-done (part group)
5728   "Get info from buffer, update variables and jump to the group buffer."
5729   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5730   (goto-char (point-min))
5731   (let* ((form (read (current-buffer)))
5732          (winconf gnus-prev-winconf)
5733          (method (cond ((eq part 'info) (nth 4 form))
5734                        ((eq part 'method) form)
5735                        (t nil)))
5736          (info (cond ((eq part 'info) form)
5737                      ((eq part 'method) (gnus-get-info group))
5738                      (t nil)))
5739          (new-group (if info
5740                       (if (or (not method)
5741                               (gnus-server-equal
5742                                gnus-select-method method))
5743                           (gnus-group-real-name (car info))
5744                         (gnus-group-prefixed-name
5745                          (gnus-group-real-name (car info)) method))
5746                       nil)))
5747     (when (and new-group
5748                (not (equal new-group group)))
5749       (when (gnus-group-goto-group group)
5750         (gnus-group-kill-group 1))
5751       (gnus-activate-group new-group))
5752     ;; Set the info.
5753     (if (and info new-group)
5754         (progn
5755           (setq info (gnus-copy-sequence info))
5756           (setcar info new-group)
5757           (unless (gnus-server-equal method "native")
5758             (unless (nthcdr 3 info)
5759               (nconc info (list nil nil)))
5760             (unless (nthcdr 4 info)
5761               (nconc info (list nil)))
5762             (gnus-info-set-method info method))
5763           (gnus-group-set-info info))
5764       (gnus-group-set-info form (or new-group group) part))
5765     (kill-buffer (current-buffer))
5766     (and winconf (set-window-configuration winconf))
5767     (set-buffer gnus-group-buffer)
5768     (gnus-group-update-group (or new-group group))
5769     (gnus-group-position-point)))
5770
5771 (defun gnus-group-make-help-group ()
5772   "Create the Gnus documentation group."
5773   (interactive)
5774   (let ((path load-path)
5775         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5776         file dir)
5777     (and (gnus-gethash name gnus-newsrc-hashtb)
5778          (error "Documentation group already exists"))
5779     (while path
5780       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5781             file nil)
5782       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5783                 (file-exists-p
5784                  (setq file (concat (file-name-directory
5785                                      (directory-file-name dir))
5786                                     "etc/gnus-tut.txt"))))
5787         (setq path nil)))
5788     (if (not file)
5789         (gnus-message 1 "Couldn't find doc group")
5790       (gnus-group-make-group
5791        (gnus-group-real-name name)
5792        (list 'nndoc "gnus-help"
5793              (list 'nndoc-address file)
5794              (list 'nndoc-article-type 'mbox)))))
5795   (gnus-group-position-point))
5796
5797 (defun gnus-group-make-doc-group (file type)
5798   "Create a group that uses a single file as the source."
5799   (interactive
5800    (list (read-file-name "File name: ")
5801          (and current-prefix-arg 'ask)))
5802   (when (eq type 'ask)
5803     (let ((err "")
5804           char found)
5805       (while (not found)
5806         (message
5807          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5808          err)
5809         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5810                           ((= char ?b) 'babyl)
5811                           ((= char ?d) 'digest)
5812                           ((= char ?f) 'forward)
5813                           ((= char ?a) 'mmfd)
5814                           (t (setq err (format "%c unknown. " char))
5815                              nil))))
5816       (setq type found)))
5817   (let* ((file (expand-file-name file))
5818          (name (gnus-generate-new-group-name
5819                 (gnus-group-prefixed-name
5820                  (file-name-nondirectory file) '(nndoc "")))))
5821     (gnus-group-make-group
5822      (gnus-group-real-name name)
5823      (list 'nndoc (file-name-nondirectory file)
5824            (list 'nndoc-address file)
5825            (list 'nndoc-article-type (or type 'guess))))))
5826
5827 (defun gnus-group-make-archive-group (&optional all)
5828   "Create the (ding) Gnus archive group of the most recent articles.
5829 Given a prefix, create a full group."
5830   (interactive "P")
5831   (let ((group (gnus-group-prefixed-name
5832                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5833     (and (gnus-gethash group gnus-newsrc-hashtb)
5834          (error "Archive group already exists"))
5835     (gnus-group-make-group
5836      (gnus-group-real-name group)
5837      (list 'nndir (if all "hpc" "edu")
5838            (list 'nndir-directory
5839                  (if all gnus-group-archive-directory
5840                    gnus-group-recent-archive-directory))))))
5841
5842 (defun gnus-group-make-directory-group (dir)
5843   "Create an nndir group.
5844 The user will be prompted for a directory.  The contents of this
5845 directory will be used as a newsgroup.  The directory should contain
5846 mail messages or news articles in files that have numeric names."
5847   (interactive
5848    (list (read-file-name "Create group from directory: ")))
5849   (or (file-exists-p dir) (error "No such directory"))
5850   (or (file-directory-p dir) (error "Not a directory"))
5851   (let ((ext "")
5852         (i 0)
5853         group)
5854     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5855       (setq group
5856             (gnus-group-prefixed-name
5857              (concat (file-name-as-directory (directory-file-name dir))
5858                      ext)
5859              '(nndir "")))
5860       (setq ext (format "<%d>" (setq i (1+ i)))))
5861     (gnus-group-make-group
5862      (gnus-group-real-name group)
5863      (list 'nndir group (list 'nndir-directory dir)))))
5864
5865 (defun gnus-group-make-kiboze-group (group address scores)
5866   "Create an nnkiboze group.
5867 The user will be prompted for a name, a regexp to match groups, and
5868 score file entries for articles to include in the group."
5869   (interactive
5870    (list
5871     (read-string "nnkiboze group name: ")
5872     (read-string "Source groups (regexp): ")
5873     (let ((headers (mapcar (lambda (group) (list group))
5874                            '("subject" "from" "number" "date" "message-id"
5875                              "references" "chars" "lines" "xref"
5876                              "followup" "all" "body" "head")))
5877           scores header regexp regexps)
5878       (while (not (equal "" (setq header (completing-read
5879                                           "Match on header: " headers nil t))))
5880         (setq regexps nil)
5881         (while (not (equal "" (setq regexp (read-string
5882                                             (format "Match on %s (string): "
5883                                                     header)))))
5884           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5885         (setq scores (cons (cons header regexps) scores)))
5886       scores)))
5887   (gnus-group-make-group group "nnkiboze" address)
5888   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5889     (let (emacs-lisp-mode-hook)
5890       (pp scores (current-buffer)))))
5891
5892 (defun gnus-group-add-to-virtual (n vgroup)
5893   "Add the current group to a virtual group."
5894   (interactive
5895    (list current-prefix-arg
5896          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5897                           "nnvirtual:")))
5898   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5899       (error "%s is not an nnvirtual group" vgroup))
5900   (let* ((groups (gnus-group-process-prefix n))
5901          (method (gnus-info-method (gnus-get-info vgroup))))
5902     (setcar (cdr method)
5903             (concat
5904              (nth 1 method) "\\|"
5905              (mapconcat
5906               (lambda (s)
5907                 (gnus-group-remove-mark s)
5908                 (concat "\\(^" (regexp-quote s) "$\\)"))
5909               groups "\\|"))))
5910   (gnus-group-position-point))
5911
5912 (defun gnus-group-make-empty-virtual (group)
5913   "Create a new, fresh, empty virtual group."
5914   (interactive "sCreate new, empty virtual group: ")
5915   (let* ((method (list 'nnvirtual "^$"))
5916          (pgroup (gnus-group-prefixed-name group method)))
5917     ;; Check whether it exists already.
5918     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5919          (error "Group %s already exists." pgroup))
5920     ;; Subscribe the new group after the group on the current line.
5921     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5922     (gnus-group-update-group pgroup)
5923     (forward-line -1)
5924     (gnus-group-position-point)))
5925
5926 (defun gnus-group-enter-directory (dir)
5927   "Enter an ephemeral nneething group."
5928   (interactive "DDirectory to read: ")
5929   (let* ((method (list 'nneething dir))
5930          (leaf (gnus-group-prefixed-name
5931                 (file-name-nondirectory (directory-file-name dir))
5932                 method))
5933          (name (gnus-generate-new-group-name leaf)))
5934     (let ((nneething-read-only t))
5935       (or (gnus-group-read-ephemeral-group
5936            name method t
5937            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5938                                       'summary 'group)))
5939           (error "Couldn't enter %s" dir)))))
5940
5941 ;; Group sorting commands
5942 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5943
5944 (defun gnus-group-sort-groups (func &optional reverse)
5945   "Sort the group buffer according to FUNC.
5946 If REVERSE, reverse the sorting order."
5947   (interactive (list gnus-group-sort-function
5948                      current-prefix-arg))
5949   (let ((func (cond 
5950                ((not (listp func)) func)
5951                ((null func) func)
5952                ((= 1 (length func)) (car func))
5953                (t `(lambda (t1 t2)
5954                      ,(gnus-make-sort-function 
5955                        (reverse func)))))))
5956     ;; We peel off the dummy group from the alist.
5957     (when func
5958       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5959         (pop gnus-newsrc-alist))
5960       ;; Do the sorting.
5961       (setq gnus-newsrc-alist
5962             (sort gnus-newsrc-alist func))
5963       (when reverse
5964         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5965       ;; Regenerate the hash table.
5966       (gnus-make-hashtable-from-newsrc-alist)
5967       (gnus-group-list-groups))))
5968
5969 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5970   "Sort the group buffer alphabetically by group name.
5971 If REVERSE, sort in reverse order."
5972   (interactive "P")
5973   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5974
5975 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5976   "Sort the group buffer by number of unread articles.
5977 If REVERSE, sort in reverse order."
5978   (interactive "P")
5979   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5980
5981 (defun gnus-group-sort-groups-by-level (&optional reverse)
5982   "Sort the group buffer by group level.
5983 If REVERSE, sort in reverse order."
5984   (interactive "P")
5985   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5986
5987 (defun gnus-group-sort-groups-by-score (&optional reverse)
5988   "Sort the group buffer by group score.
5989 If REVERSE, sort in reverse order."
5990   (interactive "P")
5991   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5992
5993 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5994   "Sort the group buffer by group rank.
5995 If REVERSE, sort in reverse order."
5996   (interactive "P")
5997   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5998
5999 (defun gnus-group-sort-groups-by-method (&optional reverse)
6000   "Sort the group buffer alphabetically by backend name.
6001 If REVERSE, sort in reverse order."
6002   (interactive "P")
6003   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
6004
6005 (defun gnus-group-sort-by-alphabet (info1 info2)
6006   "Sort alphabetically."
6007   (string< (gnus-info-group info1) (gnus-info-group info2)))
6008
6009 (defun gnus-group-sort-by-unread (info1 info2)
6010   "Sort by number of unread articles."
6011   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
6012         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
6013     (< (or (and (numberp n1) n1) 0)
6014        (or (and (numberp n2) n2) 0))))
6015
6016 (defun gnus-group-sort-by-level (info1 info2)
6017   "Sort by level."
6018   (< (gnus-info-level info1) (gnus-info-level info2)))
6019
6020 (defun gnus-group-sort-by-method (info1 info2)
6021   "Sort alphabetically by backend name."
6022   (string< (symbol-name (car (gnus-find-method-for-group
6023                               (gnus-info-group info1) info1)))
6024            (symbol-name (car (gnus-find-method-for-group
6025                               (gnus-info-group info2) info2)))))
6026
6027 (defun gnus-group-sort-by-score (info1 info2)
6028   "Sort by group score."
6029   (< (gnus-info-score info1) (gnus-info-score info2)))
6030
6031 (defun gnus-group-sort-by-rank (info1 info2)
6032   "Sort by level and score."
6033   (let ((level1 (gnus-info-level info1))
6034         (level2 (gnus-info-level info2)))
6035     (or (< level1 level2)
6036         (and (= level1 level2)
6037              (> (gnus-info-score info1) (gnus-info-score info2))))))
6038
6039 ;; Group catching up.
6040
6041 (defun gnus-group-clear-data (n)
6042   "Clear all marks and read ranges from the current group."
6043   (interactive "P")
6044   (let ((groups (gnus-group-process-prefix n))
6045         group info)
6046     (while (setq group (pop groups))
6047       (setq info (gnus-get-info group))
6048       (gnus-info-set-read info nil)
6049       (when (gnus-info-marks info)
6050         (gnus-info-set-marks info nil))
6051       (gnus-get-unread-articles-in-group info (gnus-active group) t)
6052       (when (gnus-group-goto-group group)
6053         (gnus-group-remove-mark group)
6054         (gnus-group-update-group-line)))))
6055
6056 (defun gnus-group-catchup-current (&optional n all)
6057   "Mark all articles not marked as unread in current newsgroup as read.
6058 If prefix argument N is numeric, the ARG next newsgroups will be
6059 caught up.  If ALL is non-nil, marked articles will also be marked as
6060 read.  Cross references (Xref: header) of articles are ignored.
6061 The difference between N and actual number of newsgroups that were
6062 caught up is returned."
6063   (interactive "P")
6064   (unless (gnus-group-group-name)
6065     (error "No group on the current line"))
6066   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
6067                gnus-expert-user
6068                (gnus-y-or-n-p
6069                 (if all
6070                     "Do you really want to mark all articles as read? "
6071                   "Mark all unread articles as read? "))))
6072       n
6073     (let ((groups (gnus-group-process-prefix n))
6074           (ret 0))
6075       (while groups
6076         ;; Virtual groups have to be given special treatment.
6077         (let ((method (gnus-find-method-for-group (car groups))))
6078           (if (eq 'nnvirtual (car method))
6079               (nnvirtual-catchup-group
6080                (gnus-group-real-name (car groups)) (nth 1 method) all)))
6081         (gnus-group-remove-mark (car groups))
6082         (if (>= (gnus-group-group-level) gnus-level-zombie)
6083             (gnus-message 2 "Dead groups can't be caught up")
6084           (if (prog1
6085                   (gnus-group-goto-group (car groups))
6086                 (gnus-group-catchup (car groups) all))
6087               (gnus-group-update-group-line)
6088             (setq ret (1+ ret))))
6089         (setq groups (cdr groups)))
6090       (gnus-group-next-unread-group 1)
6091       ret)))
6092
6093 (defun gnus-group-catchup-current-all (&optional n)
6094   "Mark all articles in current newsgroup as read.
6095 Cross references (Xref: header) of articles are ignored."
6096   (interactive "P")
6097   (gnus-group-catchup-current n 'all))
6098
6099 (defun gnus-group-catchup (group &optional all)
6100   "Mark all articles in GROUP as read.
6101 If ALL is non-nil, all articles are marked as read.
6102 The return value is the number of articles that were marked as read,
6103 or nil if no action could be taken."
6104   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6105          (num (car entry)))
6106     ;; Do the updating only if the newsgroup isn't killed.
6107     (if (not (numberp (car entry)))
6108         (gnus-message 1 "Can't catch up; non-active group")
6109       ;; Do auto-expirable marks if that's required.
6110       (when (gnus-group-auto-expirable-p group)
6111         (gnus-add-marked-articles
6112          group 'expire (gnus-list-of-unread-articles group))
6113         (when all
6114           (let ((marks (nth 3 (nth 2 entry))))
6115             (gnus-add-marked-articles
6116              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6117             (gnus-add-marked-articles
6118              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6119       (when entry
6120         (gnus-update-read-articles group nil)
6121         ;; Also nix out the lists of marks and dormants.
6122         (when all
6123           (gnus-add-marked-articles group 'tick nil nil 'force)
6124           (gnus-add-marked-articles group 'dormant nil nil 'force))
6125         (run-hooks 'gnus-group-catchup-group-hook)
6126         num))))
6127
6128 (defun gnus-group-expire-articles (&optional n)
6129   "Expire all expirable articles in the current newsgroup."
6130   (interactive "P")
6131   (let ((groups (gnus-group-process-prefix n))
6132         group)
6133     (unless groups
6134       (error "No groups to expire"))
6135     (while (setq group (pop groups))
6136       (gnus-group-remove-mark group)
6137       (when (gnus-check-backend-function 'request-expire-articles group)
6138         (gnus-message 6 "Expiring articles in %s..." group)
6139         (let* ((info (gnus-get-info group))
6140                (expirable (if (gnus-group-total-expirable-p group)
6141                               (cons nil (gnus-list-of-read-articles group))
6142                             (assq 'expire (gnus-info-marks info))))
6143                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6144           (when expirable
6145             (setcdr
6146              expirable
6147              (gnus-compress-sequence
6148               (if expiry-wait
6149                   ;; We set the expiry variables to the groupp
6150                   ;; parameter. 
6151                   (let ((nnmail-expiry-wait-function nil)
6152                         (nnmail-expiry-wait expiry-wait))
6153                     (gnus-request-expire-articles
6154                      (gnus-uncompress-sequence (cdr expirable)) group))
6155                 ;; Just expire using the normal expiry values.
6156                 (gnus-request-expire-articles
6157                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6158           (gnus-message 6 "Expiring articles in %s...done" group)))
6159       (gnus-group-position-point))))
6160
6161 (defun gnus-group-expire-all-groups ()
6162   "Expire all expirable articles in all newsgroups."
6163   (interactive)
6164   (save-excursion
6165     (gnus-message 5 "Expiring...")
6166     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6167                                      (cdr gnus-newsrc-alist))))
6168       (gnus-group-expire-articles nil)))
6169   (gnus-group-position-point)
6170   (gnus-message 5 "Expiring...done"))
6171
6172 (defun gnus-group-set-current-level (n level)
6173   "Set the level of the next N groups to LEVEL."
6174   (interactive
6175    (list
6176     current-prefix-arg
6177     (string-to-int
6178      (let ((s (read-string
6179                (format "Level (default %s): "
6180                        (or (gnus-group-group-level) 
6181                            gnus-level-default-subscribed)))))
6182        (if (string-match "^\\s-*$" s)
6183            (int-to-string (or (gnus-group-group-level) 
6184                               gnus-level-default-subscribed))
6185          s)))))
6186   (or (and (>= level 1) (<= level gnus-level-killed))
6187       (error "Illegal level: %d" level))
6188   (let ((groups (gnus-group-process-prefix n))
6189         group)
6190     (while (setq group (pop groups))
6191       (gnus-group-remove-mark group)
6192       (gnus-message 6 "Changed level of %s from %d to %d"
6193                     group (or (gnus-group-group-level) gnus-level-killed)
6194                     level)
6195       (gnus-group-change-level
6196        group level (or (gnus-group-group-level) gnus-level-killed))
6197       (gnus-group-update-group-line)))
6198   (gnus-group-position-point))
6199
6200 (defun gnus-group-unsubscribe-current-group (&optional n)
6201   "Toggle subscription of the current group.
6202 If given numerical prefix, toggle the N next groups."
6203   (interactive "P")
6204   (let ((groups (gnus-group-process-prefix n))
6205         group)
6206     (while groups
6207       (setq group (car groups)
6208             groups (cdr groups))
6209       (gnus-group-remove-mark group)
6210       (gnus-group-unsubscribe-group
6211        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6212                  gnus-level-default-unsubscribed
6213                gnus-level-default-subscribed) t)
6214       (gnus-group-update-group-line))
6215     (gnus-group-next-group 1)))
6216
6217 (defun gnus-group-unsubscribe-group (group &optional level silent)
6218   "Toggle subscription to GROUP.
6219 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6220 group line."
6221   (interactive
6222    (list (completing-read
6223           "Group: " gnus-active-hashtb nil
6224           (gnus-read-active-file-p)
6225           nil 
6226           'gnus-group-history)))
6227   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6228     (cond
6229      ((string-match "^[ \t]$" group)
6230       (error "Empty group name"))
6231      (newsrc
6232       ;; Toggle subscription flag.
6233       (gnus-group-change-level
6234        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6235                                       gnus-level-subscribed)
6236                                   (1+ gnus-level-subscribed)
6237                                 gnus-level-default-subscribed)))
6238       (unless silent
6239         (gnus-group-update-group group)))
6240      ((and (stringp group)
6241            (or (not (gnus-read-active-file-p))
6242                (gnus-active group)))
6243       ;; Add new newsgroup.
6244       (gnus-group-change-level
6245        group
6246        (if level level gnus-level-default-subscribed)
6247        (or (and (member group gnus-zombie-list)
6248                 gnus-level-zombie)
6249            gnus-level-killed)
6250        (and (gnus-group-group-name)
6251             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6252       (unless silent
6253         (gnus-group-update-group group)))
6254      (t (error "No such newsgroup: %s" group)))
6255     (gnus-group-position-point)))
6256
6257 (defun gnus-group-transpose-groups (n)
6258   "Move the current newsgroup up N places.
6259 If given a negative prefix, move down instead.  The difference between
6260 N and the number of steps taken is returned."
6261   (interactive "p")
6262   (or (gnus-group-group-name)
6263       (error "No group on current line"))
6264   (gnus-group-kill-group 1)
6265   (prog1
6266       (forward-line (- n))
6267     (gnus-group-yank-group)
6268     (gnus-group-position-point)))
6269
6270 (defun gnus-group-kill-all-zombies ()
6271   "Kill all zombie newsgroups."
6272   (interactive)
6273   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6274   (setq gnus-zombie-list nil)
6275   (gnus-group-list-groups))
6276
6277 (defun gnus-group-kill-region (begin end)
6278   "Kill newsgroups in current region (excluding current point).
6279 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6280   (interactive "r")
6281   (let ((lines
6282          ;; Count lines.
6283          (save-excursion
6284            (count-lines
6285             (progn
6286               (goto-char begin)
6287               (beginning-of-line)
6288               (point))
6289             (progn
6290               (goto-char end)
6291               (beginning-of-line)
6292               (point))))))
6293     (goto-char begin)
6294     (beginning-of-line)                 ;Important when LINES < 1
6295     (gnus-group-kill-group lines)))
6296
6297 (defun gnus-group-kill-group (&optional n discard)
6298   "Kill the next N groups.
6299 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6300 However, only groups that were alive can be yanked; already killed
6301 groups or zombie groups can't be yanked.
6302 The return value is the name of the group that was killed, or a list
6303 of groups killed."
6304   (interactive "P")
6305   (let ((buffer-read-only nil)
6306         (groups (gnus-group-process-prefix n))
6307         group entry level out)
6308     (if (< (length groups) 10)
6309         ;; This is faster when there are few groups.
6310         (while groups
6311           (push (setq group (pop groups)) out)
6312           (gnus-group-remove-mark group)
6313           (setq level (gnus-group-group-level))
6314           (gnus-delete-line)
6315           (when (and (not discard)
6316                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6317             (push (cons (car entry) (nth 2 entry))
6318                   gnus-list-of-killed-groups))
6319           (gnus-group-change-level
6320            (if entry entry group) gnus-level-killed (if entry nil level)))
6321       ;; If there are lots and lots of groups to be killed, we use
6322       ;; this thing instead.
6323       (let (entry)
6324         (setq groups (nreverse groups))
6325         (while groups
6326           (gnus-group-remove-mark (setq group (pop groups)))
6327           (gnus-delete-line)
6328           (push group gnus-killed-list)
6329           (setq gnus-newsrc-alist
6330                 (delq (assoc group gnus-newsrc-alist)
6331                       gnus-newsrc-alist))
6332           (when gnus-group-change-level-function
6333             (funcall gnus-group-change-level-function group 9 3))
6334           (cond
6335            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6336             (push (cons (car entry) (nth 2 entry))
6337                   gnus-list-of-killed-groups)
6338             (setcdr (cdr entry) (cdddr entry)))
6339            ((member group gnus-zombie-list)
6340             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6341         (gnus-make-hashtable-from-newsrc-alist)))
6342
6343     (gnus-group-position-point)
6344     (if (< (length out) 2) (car out) (nreverse out))))
6345
6346 (defun gnus-group-yank-group (&optional arg)
6347   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6348 inserting it before the current newsgroup.  The numeric ARG specifies
6349 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6350 is returned, or (if several groups are yanked) a list of yanked groups
6351 is returned."
6352   (interactive "p")
6353   (setq arg (or arg 1))
6354   (let (info group prev out)
6355     (while (>= (decf arg) 0)
6356       (if (not (setq info (pop gnus-list-of-killed-groups)))
6357           (error "No more newsgroups to yank"))
6358       (push (setq group (nth 1 info)) out)
6359       ;; Find which newsgroup to insert this one before - search
6360       ;; backward until something suitable is found.  If there are no
6361       ;; other newsgroups in this buffer, just make this newsgroup the
6362       ;; first newsgroup.
6363       (setq prev (gnus-group-group-name))
6364       (gnus-group-change-level
6365        info (gnus-info-level (cdr info)) gnus-level-killed
6366        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6367        t)
6368       (gnus-group-insert-group-line-info group))
6369     (forward-line -1)
6370     (gnus-group-position-point)
6371     (if (< (length out) 2) (car out) (nreverse out))))
6372
6373 (defun gnus-group-kill-level (level)
6374   "Kill all groups that is on a certain LEVEL."
6375   (interactive "nKill all groups on level: ")
6376   (cond
6377    ((= level gnus-level-zombie)
6378     (setq gnus-killed-list
6379           (nconc gnus-zombie-list gnus-killed-list))
6380     (setq gnus-zombie-list nil))
6381    ((and (< level gnus-level-zombie)
6382          (> level 0)
6383          (or gnus-expert-user
6384              (gnus-yes-or-no-p
6385               (format
6386                "Do you really want to kill all groups on level %d? "
6387                level))))
6388     (let* ((prev gnus-newsrc-alist)
6389            (alist (cdr prev)))
6390       (while alist
6391         (if (= (gnus-info-level level) level)
6392             (setcdr prev (cdr alist))
6393           (setq prev alist))
6394         (setq alist (cdr alist)))
6395       (gnus-make-hashtable-from-newsrc-alist)
6396       (gnus-group-list-groups)))
6397    (t
6398     (error "Can't kill; illegal level: %d" level))))
6399
6400 (defun gnus-group-list-all-groups (&optional arg)
6401   "List all newsgroups with level ARG or lower.
6402 Default is gnus-level-unsubscribed, which lists all subscribed and most
6403 unsubscribed groups."
6404   (interactive "P")
6405   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6406
6407 ;; Redefine this to list ALL killed groups if prefix arg used.
6408 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6409 (defun gnus-group-list-killed (&optional arg)
6410   "List all killed newsgroups in the group buffer.
6411 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6412 entail asking the server for the groups."
6413   (interactive "P")
6414   ;; Find all possible killed newsgroups if arg.
6415   (when arg
6416     (gnus-get-killed-groups))
6417   (if (not gnus-killed-list)
6418       (gnus-message 6 "No killed groups")
6419     (let (gnus-group-list-mode)
6420       (funcall gnus-group-prepare-function
6421                gnus-level-killed t gnus-level-killed))
6422     (goto-char (point-min)))
6423   (gnus-group-position-point))
6424
6425 (defun gnus-group-list-zombies ()
6426   "List all zombie newsgroups in the group buffer."
6427   (interactive)
6428   (if (not gnus-zombie-list)
6429       (gnus-message 6 "No zombie groups")
6430     (let (gnus-group-list-mode)
6431       (funcall gnus-group-prepare-function
6432                gnus-level-zombie t gnus-level-zombie))
6433     (goto-char (point-min)))
6434   (gnus-group-position-point))
6435
6436 (defun gnus-group-list-active ()
6437   "List all groups that are available from the server(s)."
6438   (interactive)
6439   ;; First we make sure that we have really read the active file.
6440   (unless (gnus-read-active-file-p)
6441     (let ((gnus-read-active-file t))
6442       (gnus-read-active-file)))
6443   ;; Find all groups and sort them.
6444   (let ((groups
6445          (sort
6446           (let (list)
6447             (mapatoms
6448              (lambda (sym)
6449                (and (boundp sym)
6450                     (symbol-value sym)
6451                     (setq list (cons (symbol-name sym) list))))
6452              gnus-active-hashtb)
6453             list)
6454           'string<))
6455         (buffer-read-only nil))
6456     (erase-buffer)
6457     (while groups
6458       (gnus-group-insert-group-line-info (pop groups)))
6459     (goto-char (point-min))))
6460
6461 (defun gnus-activate-all-groups (level)
6462   "Activate absolutely all groups."
6463   (interactive (list 7))
6464   (let ((gnus-activate-level level)
6465         (gnus-activate-foreign-newsgroups level))
6466     (gnus-group-get-new-news)))
6467
6468 (defun gnus-group-get-new-news (&optional arg)
6469   "Get newly arrived articles.
6470 If ARG is a number, it specifies which levels you are interested in
6471 re-scanning.  If ARG is non-nil and not a number, this will force
6472 \"hard\" re-reading of the active files from all servers."
6473   (interactive "P")
6474   (run-hooks 'gnus-get-new-news-hook)
6475   ;; We might read in new NoCeM messages here.
6476   (when (and gnus-use-nocem 
6477              (null arg))
6478     (gnus-nocem-scan-groups))
6479   ;; If ARG is not a number, then we read the active file.
6480   (when (and arg (not (numberp arg)))
6481     (let ((gnus-read-active-file t))
6482       (gnus-read-active-file))
6483     (setq arg nil))
6484
6485   (setq arg (gnus-group-default-level arg t))
6486   (if (and gnus-read-active-file (not arg))
6487       (progn
6488         (gnus-read-active-file)
6489         (gnus-get-unread-articles arg))
6490     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6491       (gnus-get-unread-articles arg)))
6492   (run-hooks 'gnus-after-getting-new-news-hook)
6493   (gnus-group-list-groups))
6494
6495 (defun gnus-group-get-new-news-this-group (&optional n)
6496   "Check for newly arrived news in the current group (and the N-1 next groups).
6497 The difference between N and the number of newsgroup checked is returned.
6498 If N is negative, this group and the N-1 previous groups will be checked."
6499   (interactive "P")
6500   (let* ((groups (gnus-group-process-prefix n))
6501          (ret (if (numberp n) (- n (length groups)) 0))
6502          (beg (unless n (point)))
6503          group)
6504     (while (setq group (pop groups))
6505       (gnus-group-remove-mark group)
6506       (if (gnus-activate-group group 'scan)
6507           (progn
6508             (gnus-get-unread-articles-in-group
6509              (gnus-get-info group) (gnus-active group) t)
6510             (unless (gnus-virtual-group-p group)
6511               (gnus-close-group group))
6512             (gnus-group-update-group group))
6513         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6514     (when beg (goto-char beg))
6515     (when gnus-goto-next-group-when-activating
6516       (gnus-group-next-unread-group 1 t))
6517     (gnus-summary-position-point)
6518     ret))
6519
6520 (defun gnus-group-fetch-faq (group &optional faq-dir)
6521   "Fetch the FAQ for the current group."
6522   (interactive
6523    (list
6524     (and (gnus-group-group-name)
6525          (gnus-group-real-name (gnus-group-group-name)))
6526     (cond (current-prefix-arg
6527            (completing-read
6528             "Faq dir: " (and (listp gnus-group-faq-directory)
6529                              (mapcar (lambda (file) (list file))
6530                                      gnus-group-faq-directory)))))))
6531   (or faq-dir
6532       (setq faq-dir (if (listp gnus-group-faq-directory)
6533                         (car gnus-group-faq-directory)
6534                       gnus-group-faq-directory)))
6535   (or group (error "No group name given"))
6536   (let ((file (concat (file-name-as-directory faq-dir)
6537                       (gnus-group-real-name group))))
6538     (if (not (file-exists-p file))
6539         (error "No such file: %s" file)
6540       (find-file file))))
6541
6542 (defun gnus-group-describe-group (force &optional group)
6543   "Display a description of the current newsgroup."
6544   (interactive (list current-prefix-arg (gnus-group-group-name)))
6545   (and force (setq gnus-description-hashtb nil))
6546   (let ((method (gnus-find-method-for-group group))
6547         desc)
6548     (or group (error "No group name given"))
6549     (and (or (and gnus-description-hashtb
6550                   ;; We check whether this group's method has been
6551                   ;; queried for a description file.
6552                   (gnus-gethash
6553                    (gnus-group-prefixed-name "" method)
6554                    gnus-description-hashtb))
6555              (setq desc (gnus-group-get-description group))
6556              (gnus-read-descriptions-file method))
6557          (gnus-message 1
6558           (or desc (gnus-gethash group gnus-description-hashtb)
6559               "No description available")))))
6560
6561 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6562 (defun gnus-group-describe-all-groups (&optional force)
6563   "Pop up a buffer with descriptions of all newsgroups."
6564   (interactive "P")
6565   (and force (setq gnus-description-hashtb nil))
6566   (if (not (or gnus-description-hashtb
6567                (gnus-read-all-descriptions-files)))
6568       (error "Couldn't request descriptions file"))
6569   (let ((buffer-read-only nil)
6570         b)
6571     (erase-buffer)
6572     (mapatoms
6573      (lambda (group)
6574        (setq b (point))
6575        (insert (format "      *: %-20s %s\n" (symbol-name group)
6576                        (symbol-value group)))
6577        (gnus-add-text-properties
6578         b (1+ b) (list 'gnus-group group
6579                        'gnus-unread t 'gnus-marked nil
6580                        'gnus-level (1+ gnus-level-subscribed))))
6581      gnus-description-hashtb)
6582     (goto-char (point-min))
6583     (gnus-group-position-point)))
6584
6585 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6586 (defun gnus-group-apropos (regexp &optional search-description)
6587   "List all newsgroups that have names that match a regexp."
6588   (interactive "sGnus apropos (regexp): ")
6589   (let ((prev "")
6590         (obuf (current-buffer))
6591         groups des)
6592     ;; Go through all newsgroups that are known to Gnus.
6593     (mapatoms
6594      (lambda (group)
6595        (and (symbol-name group)
6596             (string-match regexp (symbol-name group))
6597             (setq groups (cons (symbol-name group) groups))))
6598      gnus-active-hashtb)
6599     ;; Also go through all descriptions that are known to Gnus.
6600     (when search-description
6601       (mapatoms
6602        (lambda (group)
6603          (and (string-match regexp (symbol-value group))
6604               (gnus-active (symbol-name group))
6605               (setq groups (cons (symbol-name group) groups))))
6606        gnus-description-hashtb))
6607     (if (not groups)
6608         (gnus-message 3 "No groups matched \"%s\"." regexp)
6609       ;; Print out all the groups.
6610       (save-excursion
6611         (pop-to-buffer "*Gnus Help*")
6612         (buffer-disable-undo (current-buffer))
6613         (erase-buffer)
6614         (setq groups (sort groups 'string<))
6615         (while groups
6616           ;; Groups may be entered twice into the list of groups.
6617           (if (not (string= (car groups) prev))
6618               (progn
6619                 (insert (setq prev (car groups)) "\n")
6620                 (if (and gnus-description-hashtb
6621                          (setq des (gnus-gethash (car groups)
6622                                                  gnus-description-hashtb)))
6623                     (insert "  " des "\n"))))
6624           (setq groups (cdr groups)))
6625         (goto-char (point-min))))
6626     (pop-to-buffer obuf)))
6627
6628 (defun gnus-group-description-apropos (regexp)
6629   "List all newsgroups that have names or descriptions that match a regexp."
6630   (interactive "sGnus description apropos (regexp): ")
6631   (if (not (or gnus-description-hashtb
6632                (gnus-read-all-descriptions-files)))
6633       (error "Couldn't request descriptions file"))
6634   (gnus-group-apropos regexp t))
6635
6636 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6637 (defun gnus-group-list-matching (level regexp &optional all lowest)
6638   "List all groups with unread articles that match REGEXP.
6639 If the prefix LEVEL is non-nil, it should be a number that says which
6640 level to cut off listing groups.
6641 If ALL, also list groups with no unread articles.
6642 If LOWEST, don't list groups with level lower than LOWEST.
6643
6644 This command may read the active file."
6645   (interactive "P\nsList newsgroups matching: ")
6646   ;; First make sure active file has been read.
6647   (when (and level
6648              (> (prefix-numeric-value level) gnus-level-killed))
6649     (gnus-get-killed-groups))
6650   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6651                            all (or lowest 1) regexp)
6652   (goto-char (point-min))
6653   (gnus-group-position-point))
6654
6655 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6656   "List all groups that match REGEXP.
6657 If the prefix LEVEL is non-nil, it should be a number that says which
6658 level to cut off listing groups.
6659 If LOWEST, don't list groups with level lower than LOWEST."
6660   (interactive "P\nsList newsgroups matching: ")
6661   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6662
6663 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6664 (defun gnus-group-save-newsrc (&optional force)
6665   "Save the Gnus startup files.
6666 If FORCE, force saving whether it is necessary or not."
6667   (interactive "P")
6668   (gnus-save-newsrc-file force))
6669
6670 (defun gnus-group-restart (&optional arg)
6671   "Force Gnus to read the .newsrc file."
6672   (interactive "P")
6673   (when (gnus-yes-or-no-p
6674          (format "Are you sure you want to read %s? "
6675                  gnus-current-startup-file))
6676     (gnus-save-newsrc-file)
6677     (gnus-setup-news 'force)
6678     (gnus-group-list-groups arg)))
6679
6680 (defun gnus-group-read-init-file ()
6681   "Read the Gnus elisp init file."
6682   (interactive)
6683   (gnus-read-init-file))
6684
6685 (defun gnus-group-check-bogus-groups (&optional silent)
6686   "Check bogus newsgroups.
6687 If given a prefix, don't ask for confirmation before removing a bogus
6688 group."
6689   (interactive "P")
6690   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6691   (gnus-group-list-groups))
6692
6693 (defun gnus-group-edit-global-kill (&optional article group)
6694   "Edit the global kill file.
6695 If GROUP, edit that local kill file instead."
6696   (interactive "P")
6697   (setq gnus-current-kill-article article)
6698   (gnus-kill-file-edit-file group)
6699   (gnus-message
6700    6
6701    (substitute-command-keys
6702     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6703             (if group "local" "global")))))
6704
6705 (defun gnus-group-edit-local-kill (article group)
6706   "Edit a local kill file."
6707   (interactive (list nil (gnus-group-group-name)))
6708   (gnus-group-edit-global-kill article group))
6709
6710 (defun gnus-group-force-update ()
6711   "Update `.newsrc' file."
6712   (interactive)
6713   (gnus-save-newsrc-file))
6714
6715 (defun gnus-group-suspend ()
6716   "Suspend the current Gnus session.
6717 In fact, cleanup buffers except for group mode buffer.
6718 The hook gnus-suspend-gnus-hook is called before actually suspending."
6719   (interactive)
6720   (run-hooks 'gnus-suspend-gnus-hook)
6721   ;; Kill Gnus buffers except for group mode buffer.
6722   (let ((group-buf (get-buffer gnus-group-buffer)))
6723     ;; Do this on a separate list in case the user does a ^G before we finish
6724     (let ((gnus-buffer-list
6725            (delq group-buf (delq gnus-dribble-buffer
6726                                  (append gnus-buffer-list nil)))))
6727       (while gnus-buffer-list
6728         (gnus-kill-buffer (car gnus-buffer-list))
6729         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6730     (if group-buf
6731         (progn
6732           (setq gnus-buffer-list (list group-buf))
6733           (bury-buffer group-buf)
6734           (delete-windows-on group-buf t)))))
6735
6736 (defun gnus-group-clear-dribble ()
6737   "Clear all information from the dribble buffer."
6738   (interactive)
6739   (gnus-dribble-clear)
6740   (gnus-message 7 "Cleared dribble buffer"))
6741
6742 (defun gnus-group-exit ()
6743   "Quit reading news after updating .newsrc.eld and .newsrc.
6744 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6745   (interactive)
6746   (when 
6747       (or noninteractive                ;For gnus-batch-kill
6748           (not gnus-interactive-exit)   ;Without confirmation
6749           gnus-expert-user
6750           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6751     (run-hooks 'gnus-exit-gnus-hook)
6752     ;; Offer to save data from non-quitted summary buffers.
6753     (gnus-offer-save-summaries)
6754     ;; Save the newsrc file(s).
6755     (gnus-save-newsrc-file)
6756     ;; Kill-em-all.
6757     (gnus-close-backends)
6758     ;; Reset everything.
6759     (gnus-clear-system)
6760     ;; Allow the user to do things after cleaning up.
6761     (run-hooks 'gnus-after-exiting-gnus-hook)))
6762
6763 (defun gnus-close-backends ()
6764   ;; Send a close request to all backends that support such a request.
6765   (let ((methods gnus-valid-select-methods)
6766         func)
6767     (while methods
6768       (if (fboundp (setq func (intern (concat (caar methods)
6769                                               "-request-close"))))
6770           (funcall func))
6771       (setq methods (cdr methods)))))
6772
6773 (defun gnus-group-quit ()
6774   "Quit reading news without updating .newsrc.eld or .newsrc.
6775 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6776   (interactive)
6777   (when (or noninteractive              ;For gnus-batch-kill
6778             (zerop (buffer-size))
6779             (not (gnus-server-opened gnus-select-method))
6780             gnus-expert-user
6781             (not gnus-current-startup-file)
6782             (gnus-yes-or-no-p
6783              (format "Quit reading news without saving %s? "
6784                      (file-name-nondirectory gnus-current-startup-file))))
6785     (run-hooks 'gnus-exit-gnus-hook)
6786     (if gnus-use-full-window
6787         (delete-other-windows)
6788       (gnus-remove-some-windows))
6789     (gnus-dribble-save)
6790     (gnus-close-backends)
6791     (gnus-clear-system)
6792     ;; Allow the user to do things after cleaning up.
6793     (run-hooks 'gnus-after-exiting-gnus-hook)))
6794
6795 (defun gnus-offer-save-summaries ()
6796   "Offer to save all active summary buffers."
6797   (save-excursion
6798     (let ((buflist (buffer-list))
6799           buffers bufname)
6800       ;; Go through all buffers and find all summaries.
6801       (while buflist
6802         (and (setq bufname (buffer-name (car buflist)))
6803              (string-match "Summary" bufname)
6804              (save-excursion
6805                (set-buffer bufname)
6806                ;; We check that this is, indeed, a summary buffer.
6807                (and (eq major-mode 'gnus-summary-mode)
6808                     ;; Also make sure this isn't bogus.
6809                     gnus-newsgroup-prepared))
6810              (push bufname buffers))
6811         (setq buflist (cdr buflist)))
6812       ;; Go through all these summary buffers and offer to save them.
6813       (when buffers
6814         (map-y-or-n-p
6815          "Update summary buffer %s? "
6816          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6817          buffers)))))
6818
6819 (defun gnus-group-describe-briefly ()
6820   "Give a one line description of the group mode commands."
6821   (interactive)
6822   (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")))
6823
6824 (defun gnus-group-browse-foreign-server (method)
6825   "Browse a foreign news server.
6826 If called interactively, this function will ask for a select method
6827  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6828 If not, METHOD should be a list where the first element is the method
6829 and the second element is the address."
6830   (interactive
6831    (list (let ((how (completing-read
6832                      "Which backend: "
6833                      (append gnus-valid-select-methods gnus-server-alist)
6834                      nil t (cons "nntp" 0) 'gnus-method-history)))
6835            ;; We either got a backend name or a virtual server name.
6836            ;; If the first, we also need an address.
6837            (if (assoc how gnus-valid-select-methods)
6838                (list (intern how)
6839                      ;; Suggested by mapjph@bath.ac.uk.
6840                      (completing-read
6841                       "Address: "
6842                       (mapcar (lambda (server) (list server))
6843                               gnus-secondary-servers)))
6844              ;; We got a server name, so we find the method.
6845              (gnus-server-to-method how)))))
6846   (gnus-browse-foreign-server method))
6847
6848 \f
6849 ;;;
6850 ;;; Gnus summary mode
6851 ;;;
6852
6853 (defvar gnus-summary-mode-map nil)
6854
6855 (put 'gnus-summary-mode 'mode-class 'special)
6856
6857 (unless gnus-summary-mode-map
6858   (setq gnus-summary-mode-map (make-keymap))
6859   (suppress-keymap gnus-summary-mode-map)
6860
6861   ;; Non-orthogonal keys
6862
6863   (gnus-define-keys gnus-summary-mode-map
6864     " " gnus-summary-next-page
6865     "\177" gnus-summary-prev-page
6866     [delete] gnus-summary-prev-page
6867     "\r" gnus-summary-scroll-up
6868     "n" gnus-summary-next-unread-article
6869     "p" gnus-summary-prev-unread-article
6870     "N" gnus-summary-next-article
6871     "P" gnus-summary-prev-article
6872     "\M-\C-n" gnus-summary-next-same-subject
6873     "\M-\C-p" gnus-summary-prev-same-subject
6874     "\M-n" gnus-summary-next-unread-subject
6875     "\M-p" gnus-summary-prev-unread-subject
6876     "." gnus-summary-first-unread-article
6877     "," gnus-summary-best-unread-article
6878     "\M-s" gnus-summary-search-article-forward
6879     "\M-r" gnus-summary-search-article-backward
6880     "<" gnus-summary-beginning-of-article
6881     ">" gnus-summary-end-of-article
6882     "j" gnus-summary-goto-article
6883     "^" gnus-summary-refer-parent-article
6884     "\M-^" gnus-summary-refer-article
6885     "u" gnus-summary-tick-article-forward
6886     "!" gnus-summary-tick-article-forward
6887     "U" gnus-summary-tick-article-backward
6888     "d" gnus-summary-mark-as-read-forward
6889     "D" gnus-summary-mark-as-read-backward
6890     "E" gnus-summary-mark-as-expirable
6891     "\M-u" gnus-summary-clear-mark-forward
6892     "\M-U" gnus-summary-clear-mark-backward
6893     "k" gnus-summary-kill-same-subject-and-select
6894     "\C-k" gnus-summary-kill-same-subject
6895     "\M-\C-k" gnus-summary-kill-thread
6896     "\M-\C-l" gnus-summary-lower-thread
6897     "e" gnus-summary-edit-article
6898     "#" gnus-summary-mark-as-processable
6899     "\M-#" gnus-summary-unmark-as-processable
6900     "\M-\C-t" gnus-summary-toggle-threads
6901     "\M-\C-s" gnus-summary-show-thread
6902     "\M-\C-h" gnus-summary-hide-thread
6903     "\M-\C-f" gnus-summary-next-thread
6904     "\M-\C-b" gnus-summary-prev-thread
6905     "\M-\C-u" gnus-summary-up-thread
6906     "\M-\C-d" gnus-summary-down-thread
6907     "&" gnus-summary-execute-command
6908     "c" gnus-summary-catchup-and-exit
6909     "\C-w" gnus-summary-mark-region-as-read
6910     "\C-t" gnus-summary-toggle-truncation
6911     "?" gnus-summary-mark-as-dormant
6912     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6913     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6914     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6915     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6916     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6917     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6918     "=" gnus-summary-expand-window
6919     "\C-x\C-s" gnus-summary-reselect-current-group
6920     "\M-g" gnus-summary-rescan-group
6921     "w" gnus-summary-stop-page-breaking
6922     "\C-c\C-r" gnus-summary-caesar-message
6923     "\M-t" gnus-summary-toggle-mime
6924     "f" gnus-summary-followup
6925     "F" gnus-summary-followup-with-original
6926     "C" gnus-summary-cancel-article
6927     "r" gnus-summary-reply
6928     "R" gnus-summary-reply-with-original
6929     "\C-c\C-f" gnus-summary-mail-forward
6930     "o" gnus-summary-save-article
6931     "\C-o" gnus-summary-save-article-mail
6932     "|" gnus-summary-pipe-output
6933     "\M-k" gnus-summary-edit-local-kill
6934     "\M-K" gnus-summary-edit-global-kill
6935     "V" gnus-version
6936     "\C-c\C-d" gnus-summary-describe-group
6937     "q" gnus-summary-exit
6938     "Q" gnus-summary-exit-no-update
6939     "\C-c\C-i" gnus-info-find-node
6940     gnus-mouse-2 gnus-mouse-pick-article
6941     "m" gnus-summary-mail-other-window
6942     "a" gnus-summary-post-news
6943     "x" gnus-summary-limit-to-unread
6944     "s" gnus-summary-isearch-article
6945     "t" gnus-article-hide-headers
6946     "g" gnus-summary-show-article
6947     "l" gnus-summary-goto-last-article
6948     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6949     "\C-d" gnus-summary-enter-digest-group
6950     "\C-c\C-b" gnus-bug
6951     "*" gnus-cache-enter-article
6952     "\M-*" gnus-cache-remove-article
6953     "\M-&" gnus-summary-universal-argument
6954     "\C-l" gnus-recenter
6955     "I" gnus-summary-increase-score
6956     "L" gnus-summary-lower-score
6957
6958     "V" gnus-summary-score-map
6959     "X" gnus-uu-extract-map
6960     "S" gnus-summary-send-map)
6961
6962   ;; Sort of orthogonal keymap
6963   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6964     "t" gnus-summary-tick-article-forward
6965     "!" gnus-summary-tick-article-forward
6966     "d" gnus-summary-mark-as-read-forward
6967     "r" gnus-summary-mark-as-read-forward
6968     "c" gnus-summary-clear-mark-forward
6969     " " gnus-summary-clear-mark-forward
6970     "e" gnus-summary-mark-as-expirable
6971     "x" gnus-summary-mark-as-expirable
6972     "?" gnus-summary-mark-as-dormant
6973     "b" gnus-summary-set-bookmark
6974     "B" gnus-summary-remove-bookmark
6975     "#" gnus-summary-mark-as-processable
6976     "\M-#" gnus-summary-unmark-as-processable
6977     "S" gnus-summary-limit-include-expunged
6978     "C" gnus-summary-catchup
6979     "H" gnus-summary-catchup-to-here
6980     "\C-c" gnus-summary-catchup-all
6981     "k" gnus-summary-kill-same-subject-and-select
6982     "K" gnus-summary-kill-same-subject
6983     "P" gnus-uu-mark-map)
6984
6985   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6986     "c" gnus-summary-clear-above
6987     "u" gnus-summary-tick-above
6988     "m" gnus-summary-mark-above
6989     "k" gnus-summary-kill-below)
6990
6991   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6992     "/" gnus-summary-limit-to-subject
6993     "n" gnus-summary-limit-to-articles
6994     "w" gnus-summary-pop-limit
6995     "s" gnus-summary-limit-to-subject
6996     "a" gnus-summary-limit-to-author
6997     "u" gnus-summary-limit-to-unread
6998     "m" gnus-summary-limit-to-marks
6999     "v" gnus-summary-limit-to-score
7000     "D" gnus-summary-limit-include-dormant
7001     "d" gnus-summary-limit-exclude-dormant
7002     ;;  "t" gnus-summary-limit-exclude-thread
7003     "E" gnus-summary-limit-include-expunged
7004     "c" gnus-summary-limit-exclude-childless-dormant
7005     "C" gnus-summary-limit-mark-excluded-as-read)
7006
7007   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
7008     "n" gnus-summary-next-unread-article
7009     "p" gnus-summary-prev-unread-article
7010     "N" gnus-summary-next-article
7011     "P" gnus-summary-prev-article
7012     "\C-n" gnus-summary-next-same-subject
7013     "\C-p" gnus-summary-prev-same-subject
7014     "\M-n" gnus-summary-next-unread-subject
7015     "\M-p" gnus-summary-prev-unread-subject
7016     "f" gnus-summary-first-unread-article
7017     "b" gnus-summary-best-unread-article
7018     "j" gnus-summary-goto-article
7019     "g" gnus-summary-goto-subject
7020     "l" gnus-summary-goto-last-article
7021     "p" gnus-summary-pop-article)
7022
7023   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
7024     "k" gnus-summary-kill-thread
7025     "l" gnus-summary-lower-thread
7026     "i" gnus-summary-raise-thread
7027     "T" gnus-summary-toggle-threads
7028     "t" gnus-summary-rethread-current
7029     "^" gnus-summary-reparent-thread
7030     "s" gnus-summary-show-thread
7031     "S" gnus-summary-show-all-threads
7032     "h" gnus-summary-hide-thread
7033     "H" gnus-summary-hide-all-threads
7034     "n" gnus-summary-next-thread
7035     "p" gnus-summary-prev-thread
7036     "u" gnus-summary-up-thread
7037     "o" gnus-summary-top-thread
7038     "d" gnus-summary-down-thread
7039     "#" gnus-uu-mark-thread
7040     "\M-#" gnus-uu-unmark-thread)
7041
7042   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
7043     "c" gnus-summary-catchup-and-exit
7044     "C" gnus-summary-catchup-all-and-exit
7045     "E" gnus-summary-exit-no-update
7046     "Q" gnus-summary-exit
7047     "Z" gnus-summary-exit
7048     "n" gnus-summary-catchup-and-goto-next-group
7049     "R" gnus-summary-reselect-current-group
7050     "G" gnus-summary-rescan-group
7051     "N" gnus-summary-next-group
7052     "P" gnus-summary-prev-group)
7053
7054   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
7055     " " gnus-summary-next-page
7056     "n" gnus-summary-next-page
7057     "\177" gnus-summary-prev-page
7058     [delete] gnus-summary-prev-page
7059     "p" gnus-summary-prev-page
7060     "\r" gnus-summary-scroll-up
7061     "<" gnus-summary-beginning-of-article
7062     ">" gnus-summary-end-of-article
7063     "b" gnus-summary-beginning-of-article
7064     "e" gnus-summary-end-of-article
7065     "^" gnus-summary-refer-parent-article
7066     "r" gnus-summary-refer-parent-article
7067     "R" gnus-summary-refer-references
7068     "g" gnus-summary-show-article
7069     "s" gnus-summary-isearch-article)
7070
7071   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
7072     "b" gnus-article-add-buttons
7073     "B" gnus-article-add-buttons-to-head
7074     "o" gnus-article-treat-overstrike
7075     ;;  "w" gnus-article-word-wrap
7076     "w" gnus-article-fill-cited-article
7077     "c" gnus-article-remove-cr
7078     "L" gnus-article-remove-trailing-blank-lines
7079     "q" gnus-article-de-quoted-unreadable
7080     "f" gnus-article-display-x-face
7081     "l" gnus-summary-stop-page-breaking
7082     "r" gnus-summary-caesar-message
7083     "t" gnus-article-hide-headers
7084     "v" gnus-summary-verbose-headers
7085     "m" gnus-summary-toggle-mime)
7086
7087   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
7088     "a" gnus-article-hide
7089     "h" gnus-article-hide-headers
7090     "b" gnus-article-hide-boring-headers
7091     "s" gnus-article-hide-signature
7092     "c" gnus-article-hide-citation
7093     "p" gnus-article-hide-pgp
7094     "\C-c" gnus-article-hide-citation-maybe)
7095
7096   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
7097     "a" gnus-article-highlight
7098     "h" gnus-article-highlight-headers
7099     "c" gnus-article-highlight-citation
7100     "s" gnus-article-highlight-signature)
7101
7102   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
7103     "z" gnus-article-date-ut
7104     "u" gnus-article-date-ut
7105     "l" gnus-article-date-local
7106     "e" gnus-article-date-lapsed
7107     "o" gnus-article-date-original)
7108
7109   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7110     "v" gnus-version
7111     "f" gnus-summary-fetch-faq
7112     "d" gnus-summary-describe-group
7113     "h" gnus-summary-describe-briefly
7114     "i" gnus-info-find-node)
7115
7116   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7117     "e" gnus-summary-expire-articles
7118     "\M-\C-e" gnus-summary-expire-articles-now
7119     "\177" gnus-summary-delete-article
7120     [delete] gnus-summary-delete-article
7121     "m" gnus-summary-move-article
7122     "r" gnus-summary-respool-article
7123     "w" gnus-summary-edit-article
7124     "c" gnus-summary-copy-article
7125     "B" gnus-summary-crosspost-article
7126     "q" gnus-summary-respool-query
7127     "i" gnus-summary-import-article)
7128
7129   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7130     "o" gnus-summary-save-article
7131     "m" gnus-summary-save-article-mail
7132     "r" gnus-summary-save-article-rmail
7133     "f" gnus-summary-save-article-file
7134     "b" gnus-summary-save-article-body-file
7135     "h" gnus-summary-save-article-folder
7136     "v" gnus-summary-save-article-vm
7137     "p" gnus-summary-pipe-output
7138     "s" gnus-soup-add-article)
7139   )
7140
7141 \f
7142
7143 (defun gnus-summary-mode (&optional group)
7144   "Major mode for reading articles.
7145
7146 All normal editing commands are switched off.
7147 \\<gnus-summary-mode-map>
7148 Each line in this buffer represents one article.  To read an
7149 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7150 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7151 respectively.
7152
7153 You can also post articles and send mail from this buffer.  To
7154 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7155 of an article, type `\\[gnus-summary-reply]'.
7156
7157 There are approx. one gazillion commands you can execute in this
7158 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7159
7160 The following commands are available:
7161
7162 \\{gnus-summary-mode-map}"
7163   (interactive)
7164   (when (and menu-bar-mode
7165              (gnus-visual-p 'summary-menu 'menu))
7166     (gnus-summary-make-menu-bar))
7167   (kill-all-local-variables)
7168   (gnus-summary-make-local-variables)
7169   (gnus-make-thread-indent-array)
7170   (gnus-simplify-mode-line)
7171   (setq major-mode 'gnus-summary-mode)
7172   (setq mode-name "Summary")
7173   (make-local-variable 'minor-mode-alist)
7174   (use-local-map gnus-summary-mode-map)
7175   (buffer-disable-undo (current-buffer))
7176   (setq buffer-read-only t)             ;Disable modification
7177   (setq truncate-lines t)
7178   (setq selective-display t)
7179   (setq selective-display-ellipses t)   ;Display `...'
7180   (setq buffer-display-table gnus-summary-display-table)
7181   (setq gnus-newsgroup-name group)
7182   (make-local-variable 'gnus-summary-line-format)
7183   (make-local-variable 'gnus-summary-line-format-spec)
7184   (make-local-variable 'gnus-summary-mark-positions)
7185   (run-hooks 'gnus-summary-mode-hook))
7186
7187 (defun gnus-summary-make-local-variables ()
7188   "Make all the local summary buffer variables."
7189   (let ((locals gnus-summary-local-variables)
7190         global local)
7191     (while (setq local (pop locals))
7192       (if (consp local)
7193           (progn
7194             (if (eq (cdr local) 'global)
7195                 ;; Copy the global value of the variable.
7196                 (setq global (symbol-value (car local)))
7197               ;; Use the value from the list.
7198               (setq global (eval (cdr local))))
7199             (make-local-variable (car local))
7200             (set (car local) global))
7201         ;; Simple nil-valued local variable.
7202         (make-local-variable local)
7203         (set local nil)))))
7204
7205 (defun gnus-summary-make-display-table ()
7206   ;; Change the display table.  Odd characters have a tendency to mess
7207   ;; up nicely formatted displays - we make all possible glyphs
7208   ;; display only a single character.
7209
7210   ;; We start from the standard display table, if any.
7211   (setq gnus-summary-display-table
7212         (or (copy-sequence standard-display-table)
7213             (make-display-table)))
7214   ;; Nix out all the control chars...
7215   (let ((i 32))
7216     (while (>= (setq i (1- i)) 0)
7217       (aset gnus-summary-display-table i [??])))
7218   ;; ... but not newline and cr, of course. (cr is necessary for the
7219   ;; selective display).
7220   (aset gnus-summary-display-table ?\n nil)
7221   (aset gnus-summary-display-table ?\r nil)
7222   ;; We nix out any glyphs over 126 that are not set already.
7223   (let ((i 256))
7224     (while (>= (setq i (1- i)) 127)
7225       ;; Only modify if the entry is nil.
7226       (or (aref gnus-summary-display-table i)
7227           (aset gnus-summary-display-table i [??])))))
7228
7229 (defun gnus-summary-clear-local-variables ()
7230   (let ((locals gnus-summary-local-variables))
7231     (while locals
7232       (if (consp (car locals))
7233           (and (vectorp (caar locals))
7234                (set (caar locals) nil))
7235         (and (vectorp (car locals))
7236              (set (car locals) nil)))
7237       (setq locals (cdr locals)))))
7238
7239 ;; Summary data functions.
7240
7241 (defmacro gnus-data-number (data)
7242   `(car ,data))
7243
7244 (defmacro gnus-data-set-number (data number)
7245   `(setcar ,data ,number))
7246
7247 (defmacro gnus-data-mark (data)
7248   `(nth 1 ,data))
7249
7250 (defmacro gnus-data-set-mark (data mark)
7251   `(setcar (nthcdr 1 ,data) ,mark))
7252
7253 (defmacro gnus-data-pos (data)
7254   `(nth 2 ,data))
7255
7256 (defmacro gnus-data-set-pos (data pos)
7257   `(setcar (nthcdr 2 ,data) ,pos))
7258
7259 (defmacro gnus-data-header (data)
7260   `(nth 3 ,data))
7261
7262 (defmacro gnus-data-level (data)
7263   `(nth 4 ,data))
7264
7265 (defmacro gnus-data-unread-p (data)
7266   `(= (nth 1 ,data) gnus-unread-mark))
7267
7268 (defmacro gnus-data-pseudo-p (data)
7269   `(consp (nth 3 ,data)))
7270
7271 (defmacro gnus-data-find (number)
7272   `(assq ,number gnus-newsgroup-data))
7273
7274 (defmacro gnus-data-find-list (number &optional data)
7275   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7276      (memq (assq ,number bdata)
7277            bdata)))
7278
7279 (defmacro gnus-data-make (number mark pos header level)
7280   `(list ,number ,mark ,pos ,header ,level))
7281
7282 (defun gnus-data-enter (after-article number mark pos header level offset)
7283   (let ((data (gnus-data-find-list after-article)))
7284     (or data (error "No such article: %d" after-article))
7285     (setcdr data (cons (gnus-data-make number mark pos header level)
7286                        (cdr data)))
7287     (setq gnus-newsgroup-data-reverse nil)
7288     (gnus-data-update-list (cddr data) offset)))
7289
7290 (defun gnus-data-enter-list (after-article list &optional offset)
7291   (when list
7292     (let ((data (and after-article (gnus-data-find-list after-article)))
7293           (ilist list))
7294       (or data (not after-article) (error "No such article: %d" after-article))
7295       ;; Find the last element in the list to be spliced into the main
7296       ;; list.
7297       (while (cdr list)
7298         (setq list (cdr list)))
7299       (if (not data)
7300           (progn
7301             (setcdr list gnus-newsgroup-data)
7302             (setq gnus-newsgroup-data ilist)
7303             (and offset (gnus-data-update-list (cdr list) offset)))
7304         (setcdr list (cdr data))
7305         (setcdr data ilist)
7306         (and offset (gnus-data-update-list (cdr data) offset)))
7307       (setq gnus-newsgroup-data-reverse nil))))
7308
7309 (defun gnus-data-remove (article &optional offset)
7310   (let ((data gnus-newsgroup-data))
7311     (if (= (gnus-data-number (car data)) article)
7312         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7313               gnus-newsgroup-data-reverse nil)
7314       (while (cdr data)
7315         (and (= (gnus-data-number (cadr data)) article)
7316              (progn
7317                (setcdr data (cddr data))
7318                (and offset (gnus-data-update-list (cdr data) offset))
7319                (setq data nil
7320                      gnus-newsgroup-data-reverse nil)))
7321         (setq data (cdr data))))))
7322
7323 (defmacro gnus-data-list (backward)
7324   `(if ,backward
7325        (or gnus-newsgroup-data-reverse
7326            (setq gnus-newsgroup-data-reverse
7327                  (reverse gnus-newsgroup-data)))
7328      gnus-newsgroup-data))
7329
7330 (defun gnus-data-update-list (data offset)
7331   "Add OFFSET to the POS of all data entries in DATA."
7332   (while data
7333     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7334     (setq data (cdr data))))
7335
7336 (defun gnus-data-compute-positions ()
7337   "Compute the positions of all articles."
7338   (let ((data gnus-newsgroup-data)
7339         pos)
7340     (while data
7341       (when (setq pos (text-property-any
7342                        (point-min) (point-max)
7343                        'gnus-number (gnus-data-number (car data))))
7344         (gnus-data-set-pos (car data) (+ pos 3)))
7345       (setq data (cdr data)))))
7346
7347 (defun gnus-summary-article-pseudo-p (article)
7348   "Say whether this article is a pseudo article or not."
7349   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7350
7351 (defun gnus-article-parent-p (number)
7352   "Say whether this article is a parent or not."
7353   (let ((data (gnus-data-find-list number)))
7354     (and (cdr data)                     ; There has to be an article after...
7355          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7356             (gnus-data-level (nth 1 data))))))
7357
7358 (defun gnus-article-children (number)
7359   "Return a list of all children to NUMBER."
7360   (let* ((data (gnus-data-find-list number))
7361          (level (gnus-data-level (car data)))
7362          children)
7363     (setq data (cdr data))
7364     (while (and data            
7365                 (= (gnus-data-level (car data)) (1+ level)))
7366       (push (gnus-data-number (car data)) children)
7367       (setq data (cdr data)))
7368     children))
7369
7370 (defmacro gnus-summary-skip-intangible ()
7371   "If the current article is intangible, then jump to a different article."
7372   '(let ((to (get-text-property (point) 'gnus-intangible)))
7373     (and to (gnus-summary-goto-subject to))))
7374
7375 (defmacro gnus-summary-article-intangible-p ()
7376   "Say whether this article is intangible or not."
7377   '(get-text-property (point) 'gnus-intangible))
7378
7379 ;; Some summary mode macros.
7380
7381 (defmacro gnus-summary-article-number ()
7382   "The article number of the article on the current line.
7383 If there isn's an article number here, then we return the current
7384 article number."
7385   '(progn
7386      (gnus-summary-skip-intangible)
7387      (or (get-text-property (point) 'gnus-number)
7388          (gnus-summary-last-subject))))
7389
7390 (defmacro gnus-summary-article-header (&optional number)
7391   `(gnus-data-header (gnus-data-find
7392                       ,(or number '(gnus-summary-article-number)))))
7393
7394 (defmacro gnus-summary-thread-level (&optional number)
7395   `(if (and (eq gnus-summary-make-false-root 'dummy)
7396             (get-text-property (point) 'gnus-intangible))
7397        0
7398      (gnus-data-level (gnus-data-find
7399                        ,(or number '(gnus-summary-article-number))))))
7400
7401 (defmacro gnus-summary-article-mark (&optional number)
7402   `(gnus-data-mark (gnus-data-find
7403                     ,(or number '(gnus-summary-article-number)))))
7404
7405 (defmacro gnus-summary-article-pos (&optional number)
7406   `(gnus-data-pos (gnus-data-find
7407                    ,(or number '(gnus-summary-article-number)))))
7408
7409 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7410 (defmacro gnus-summary-article-subject (&optional number)
7411   "Return current subject string or nil if nothing."
7412   `(let ((headers
7413           ,(if number
7414                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7415              '(gnus-data-header (assq (gnus-summary-article-number)
7416                                       gnus-newsgroup-data)))))
7417      (and headers
7418           (vectorp headers)
7419           (mail-header-subject headers))))
7420
7421 (defmacro gnus-summary-article-score (&optional number)
7422   "Return current article score."
7423   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7424                   gnus-newsgroup-scored))
7425        gnus-summary-default-score 0))
7426
7427 (defun gnus-summary-article-children (&optional number)
7428   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7429          (level (gnus-data-level (car data)))
7430          l children)
7431     (while (and (setq data (cdr data))
7432                 (> (setq l (gnus-data-level (car data))) level))
7433       (and (= (1+ level) l)
7434            (setq children (cons (gnus-data-number (car data))
7435                                 children))))
7436     (nreverse children)))
7437
7438 (defun gnus-summary-article-parent (&optional number)
7439   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7440                                     (gnus-data-list t)))
7441          (level (gnus-data-level (car data))))
7442     (if (zerop level)
7443         () ; This is a root.
7444       ;; We search until we find an article with a level less than
7445       ;; this one.  That function has to be the parent.
7446       (while (and (setq data (cdr data))
7447                   (not (< (gnus-data-level (car data)) level))))
7448       (and data (gnus-data-number (car data))))))
7449
7450 (defun gnus-unread-mark-p (mark)
7451   "Say whether MARK is the unread mark."
7452   (= mark gnus-unread-mark))
7453
7454 (defun gnus-read-mark-p (mark)
7455   "Say whether MARK is one of the marks that mark as read.
7456 This is all marks except unread, ticked, dormant, and expirable."
7457   (not (or (= mark gnus-unread-mark)
7458            (= mark gnus-ticked-mark)
7459            (= mark gnus-dormant-mark)
7460            (= mark gnus-expirable-mark))))
7461
7462 ;; Saving hidden threads.
7463
7464 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
7465 (put 'gnus-save-hidden-threads 'lisp-indent-hook 0)
7466 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
7467
7468 (defmacro gnus-save-hidden-threads (&rest forms)
7469   "Save hidden threads, eval FORMS, and restore the hidden threads."
7470   (let ((config (make-symbol "config")))
7471     `(let ((,config (gnus-hidden-threads-configuration)))
7472        (unwind-protect
7473            (progn
7474              ,@forms)
7475          (gnus-restore-hidden-threads-configuration ,config)))))
7476
7477 (defun gnus-hidden-threads-configuration ()
7478   "Return the current hidden threads configuration."
7479   (save-excursion
7480     (let (config)
7481       (goto-char (point-min))
7482       (while (search-forward "\r" nil t)
7483         (push (1- (point)) config))
7484       config)))
7485
7486 (defun gnus-restore-hidden-threads-configuration (config)
7487   "Restore hidden threads configuration from CONFIG."
7488   (let (point buffer-read-only)
7489     (while (setq point (pop config))
7490       (when (and (< point (point-max))
7491                  (goto-char point)
7492                  (= (following-char) ?\n))
7493         (subst-char-in-region point (1+ point) ?\n ?\r)))))
7494
7495 ;; Various summary mode internalish functions.
7496
7497 (defun gnus-mouse-pick-article (e)
7498   (interactive "e")
7499   (mouse-set-point e)
7500   (gnus-summary-next-page nil t))
7501
7502 (defun gnus-summary-setup-buffer (group)
7503   "Initialize summary buffer."
7504   (let ((buffer (concat "*Summary " group "*")))
7505     (if (get-buffer buffer)
7506         (progn
7507           (set-buffer buffer)
7508           (setq gnus-summary-buffer (current-buffer))
7509           (not gnus-newsgroup-prepared))
7510       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7511       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7512       (gnus-add-current-to-buffer-list)
7513       (gnus-summary-mode group)
7514       (when gnus-carpal
7515         (gnus-carpal-setup-buffer 'summary))
7516       (unless gnus-single-article-buffer
7517         (make-local-variable 'gnus-article-buffer)
7518         (make-local-variable 'gnus-article-current)
7519         (make-local-variable 'gnus-original-article-buffer))
7520       (setq gnus-newsgroup-name group)
7521       t)))
7522
7523 (defun gnus-set-global-variables ()
7524   ;; Set the global equivalents of the summary buffer-local variables
7525   ;; to the latest values they had.  These reflect the summary buffer
7526   ;; that was in action when the last article was fetched.
7527   (when (eq major-mode 'gnus-summary-mode)
7528     (setq gnus-summary-buffer (current-buffer))
7529     (let ((name gnus-newsgroup-name)
7530           (marked gnus-newsgroup-marked)
7531           (unread gnus-newsgroup-unreads)
7532           (headers gnus-current-headers)
7533           (data gnus-newsgroup-data)
7534           (summary gnus-summary-buffer)
7535           (article-buffer gnus-article-buffer)
7536           (original gnus-original-article-buffer)
7537           (gac gnus-article-current)
7538           (score-file gnus-current-score-file))
7539       (save-excursion
7540         (set-buffer gnus-group-buffer)
7541         (setq gnus-newsgroup-name name)
7542         (setq gnus-newsgroup-marked marked)
7543         (setq gnus-newsgroup-unreads unread)
7544         (setq gnus-current-headers headers)
7545         (setq gnus-newsgroup-data data)
7546         (setq gnus-article-current gac)
7547         (setq gnus-summary-buffer summary)
7548         (setq gnus-article-buffer article-buffer)
7549         (setq gnus-original-article-buffer original)
7550         (setq gnus-current-score-file score-file)))))
7551
7552 (defun gnus-summary-last-article-p (&optional article)
7553   "Return whether ARTICLE is the last article in the buffer."
7554   (if (not (setq article (or article (gnus-summary-article-number))))
7555       t ; All non-existant numbers are the last article. :-)
7556     (not (cdr (gnus-data-find-list article)))))
7557
7558 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7559   "Insert a dummy root in the summary buffer."
7560   (beginning-of-line)
7561   (gnus-add-text-properties
7562    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7563    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7564
7565 (defun gnus-make-thread-indent-array ()
7566   (let ((n 200))
7567     (unless (and gnus-thread-indent-array
7568                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7569       (setq gnus-thread-indent-array (make-vector 201 "")
7570             gnus-thread-indent-array-level gnus-thread-indent-level)
7571       (while (>= n 0)
7572         (aset gnus-thread-indent-array n
7573               (make-string (* n gnus-thread-indent-level) ? ))
7574         (setq n (1- n))))))
7575
7576 (defun gnus-summary-insert-line
7577   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7578                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7579                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7580   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7581          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7582          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7583          (gnus-tmp-score-char
7584           (if (or (null gnus-summary-default-score)
7585                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7586                       gnus-summary-zcore-fuzz)) ? 
7587             (if (< gnus-tmp-score gnus-summary-default-score)
7588                 gnus-score-below-mark gnus-score-over-mark)))
7589          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7590                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7591                                   gnus-cached-mark)
7592                                  (gnus-tmp-replied gnus-replied-mark)
7593                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7594                                   gnus-saved-mark)
7595                                  (t gnus-unread-mark)))
7596          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7597          (gnus-tmp-name
7598           (cond
7599            ((string-match "(.+)" gnus-tmp-from)
7600             (substring gnus-tmp-from
7601                        (1+ (match-beginning 0)) (1- (match-end 0))))
7602            ((string-match "<[^>]+> *$" gnus-tmp-from)
7603             (let ((beg (match-beginning 0)))
7604               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7605                        (substring gnus-tmp-from (1+ (match-beginning 0))
7606                                   (1- (match-end 0))))
7607                   (substring gnus-tmp-from 0 beg))))
7608            (t gnus-tmp-from)))
7609          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7610          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7611          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7612          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7613          (buffer-read-only nil))
7614     (when (string= gnus-tmp-name "")
7615       (setq gnus-tmp-name gnus-tmp-from))
7616     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7617     (gnus-put-text-property
7618      (point)
7619      (progn (eval gnus-summary-line-format-spec) (point))
7620      'gnus-number gnus-tmp-number)
7621     (when (gnus-visual-p 'summary-highlight 'highlight)
7622       (forward-line -1)
7623       (run-hooks 'gnus-summary-update-hook)
7624       (forward-line 1))))
7625
7626 (defun gnus-summary-update-line (&optional dont-update)
7627   ;; Update summary line after change.
7628   (when (and gnus-summary-default-score
7629              (not gnus-summary-inhibit-highlight))
7630     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7631            (article (gnus-summary-article-number))
7632            (score (gnus-summary-article-score article)))
7633       (unless dont-update
7634         (if (and gnus-summary-mark-below
7635                  (< (gnus-summary-article-score)
7636                     gnus-summary-mark-below))
7637             ;; This article has a low score, so we mark it as read.
7638             (when (memq article gnus-newsgroup-unreads)
7639               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7640           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7641             ;; This article was previously marked as read on account
7642             ;; of a low score, but now it has risen, so we mark it as
7643             ;; unread.
7644             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7645         (gnus-summary-update-mark
7646          (if (or (null gnus-summary-default-score)
7647                  (<= (abs (- score gnus-summary-default-score))
7648                      gnus-summary-zcore-fuzz)) ? 
7649            (if (< score gnus-summary-default-score)
7650                gnus-score-below-mark gnus-score-over-mark)) 'score))
7651       ;; Do visual highlighting.
7652       (when (gnus-visual-p 'summary-highlight 'highlight)
7653         (run-hooks 'gnus-summary-update-hook)))))
7654
7655 (defvar gnus-tmp-new-adopts nil)
7656
7657 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7658   ;; Sum up all elements (and sub-elements) in a list.
7659   (let* ((number
7660           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7661           (cond
7662            ((and (consp thread) (cdr thread))
7663             (apply
7664              '+ 1 (mapcar
7665                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7666            ((null thread)
7667             1)
7668            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7669             1)
7670            (t 0))))
7671     (when (and level (zerop level) gnus-tmp-new-adopts)
7672       (incf number
7673             (apply '+ (mapcar
7674                        'gnus-summary-number-of-articles-in-thread
7675                        gnus-tmp-new-adopts))))
7676     (if char
7677         (if (> number 1) gnus-not-empty-thread-mark
7678           gnus-empty-thread-mark)
7679       number)))
7680
7681 (defun gnus-summary-set-local-parameters (group)
7682  "Go through the local params of GROUP and set all variable specs in that list."
7683   (let ((params (gnus-info-params (gnus-get-info group)))
7684         elem)
7685     (while params
7686       (setq elem (car params)
7687             params (cdr params))
7688       (and (consp elem)                 ; Has to be a cons.
7689            (consp (cdr elem))           ; The cdr has to be a list.
7690            (symbolp (car elem))         ; Has to be a symbol in there.
7691            (not (memq (car elem) 
7692                       '(quit-config to-address to-list to-group)))
7693            (progn                       ; So we set it.
7694              (make-local-variable (car elem))
7695              (set (car elem) (eval (nth 1 elem))))))))
7696
7697 (defun gnus-summary-read-group (group &optional show-all no-article
7698                                       kill-buffer no-display)
7699   "Start reading news in newsgroup GROUP.
7700 If SHOW-ALL is non-nil, already read articles are also listed.
7701 If NO-ARTICLE is non-nil, no article is selected initially.
7702 If NO-DISPLAY, don't generate a summary buffer."
7703   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7704   (let* ((new-group (gnus-summary-setup-buffer group))
7705          (quit-config (gnus-group-quit-config group))
7706          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7707     (cond
7708      ;; This summary buffer exists already, so we just select it.
7709      ((not new-group)
7710       (gnus-set-global-variables)
7711       (when kill-buffer
7712         (gnus-kill-or-deaden-summary kill-buffer))
7713       (gnus-configure-windows 'summary 'force)
7714       (gnus-set-mode-line 'summary)
7715       (gnus-summary-position-point)
7716       (message "")
7717       t)
7718      ;; We couldn't select this group.
7719      ((null did-select)
7720       (when (and (eq major-mode 'gnus-summary-mode)
7721                  (not (equal (current-buffer) kill-buffer)))
7722         (kill-buffer (current-buffer))
7723         (if (not quit-config)
7724             (progn
7725               (set-buffer gnus-group-buffer)
7726               (gnus-group-jump-to-group group)
7727               (gnus-group-next-unread-group 1))
7728           (if (not (buffer-name (car quit-config)))
7729               (gnus-configure-windows 'group 'force)
7730             (set-buffer (car quit-config))
7731             (and (eq major-mode 'gnus-summary-mode)
7732                  (gnus-set-global-variables))
7733             (gnus-configure-windows (cdr quit-config)))))
7734       (gnus-message 3 "Can't select group")
7735       nil)
7736      ;; The user did a `C-g' while prompting for number of articles,
7737      ;; so we exit this group.
7738      ((eq did-select 'quit)
7739       (and (eq major-mode 'gnus-summary-mode)
7740            (not (equal (current-buffer) kill-buffer))
7741            (kill-buffer (current-buffer)))
7742       (when kill-buffer
7743         (gnus-kill-or-deaden-summary kill-buffer))
7744       (if (not quit-config)
7745           (progn
7746             (set-buffer gnus-group-buffer)
7747             (gnus-group-jump-to-group group)
7748             (gnus-group-next-unread-group 1)
7749             (gnus-configure-windows 'group 'force))
7750         (if (not (buffer-name (car quit-config)))
7751             (gnus-configure-windows 'group 'force)
7752           (set-buffer (car quit-config))
7753           (and (eq major-mode 'gnus-summary-mode)
7754                (gnus-set-global-variables))
7755           (gnus-configure-windows (cdr quit-config))))
7756       ;; Finally signal the quit.
7757       (signal 'quit nil))
7758      ;; The group was successfully selected.
7759      (t
7760       (gnus-set-global-variables)
7761       ;; Save the active value in effect when the group was entered.
7762       (setq gnus-newsgroup-active
7763             (gnus-copy-sequence
7764              (gnus-active gnus-newsgroup-name)))
7765       ;; You can change the summary buffer in some way with this hook.
7766       (run-hooks 'gnus-select-group-hook)
7767       ;; Set any local variables in the group parameters.
7768       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7769       (gnus-update-format-specifications)
7770       ;; Do score processing.
7771       (when gnus-use-scoring
7772         (gnus-possibly-score-headers))
7773       ;; Check whether to fill in the gaps in the threads.
7774       (when gnus-build-sparse-threads
7775         (gnus-build-sparse-threads))
7776       ;; Find the initial limit.
7777       (if gnus-show-threads
7778           (if show-all
7779               (let ((gnus-newsgroup-dormant nil))
7780                 (gnus-summary-initial-limit show-all))
7781             (gnus-summary-initial-limit show-all))
7782         (setq gnus-newsgroup-limit 
7783               (mapcar 
7784                (lambda (header) (mail-header-number header))
7785                gnus-newsgroup-headers)))
7786       ;; Generate the summary buffer.
7787       (unless no-display
7788         (gnus-summary-prepare))
7789       (when gnus-use-trees
7790         (gnus-tree-open group)
7791         (setq gnus-summary-highlight-line-function
7792               'gnus-tree-highlight-article))
7793       ;; If the summary buffer is empty, but there are some low-scored
7794       ;; articles or some excluded dormants, we include these in the
7795       ;; buffer.
7796       (when (and (zerop (buffer-size))
7797                  (not no-display))
7798         (cond (gnus-newsgroup-dormant
7799                (gnus-summary-limit-include-dormant))
7800               ((and gnus-newsgroup-scored show-all)
7801                (gnus-summary-limit-include-expunged))))
7802       ;; Function `gnus-apply-kill-file' must be called in this hook.
7803       (run-hooks 'gnus-apply-kill-hook)
7804       (if (and (zerop (buffer-size))
7805                (not no-display))
7806           (progn
7807             ;; This newsgroup is empty.
7808             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7809             (gnus-message 6 "No unread news")
7810             (when kill-buffer
7811               (gnus-kill-or-deaden-summary kill-buffer))
7812             ;; Return nil from this function.
7813             nil)
7814         ;; Hide conversation thread subtrees.  We cannot do this in
7815         ;; gnus-summary-prepare-hook since kill processing may not
7816         ;; work with hidden articles.
7817         (and gnus-show-threads
7818              gnus-thread-hide-subtree
7819              (gnus-summary-hide-all-threads))
7820         ;; Show first unread article if requested.
7821         (if (and (not no-article)
7822                  (not no-display)
7823                  gnus-newsgroup-unreads
7824                  gnus-auto-select-first)
7825             (unless (if (eq gnus-auto-select-first 'best)
7826                         (gnus-summary-best-unread-article)
7827                       (gnus-summary-first-unread-article))
7828               (gnus-configure-windows 'summary))
7829           ;; Don't select any articles, just move point to the first
7830           ;; article in the group.
7831           (goto-char (point-min))
7832           (gnus-summary-position-point)
7833           (gnus-set-mode-line 'summary)
7834           (gnus-configure-windows 'summary 'force))
7835         ;; If we are in async mode, we send some info to the backend.
7836         (when gnus-newsgroup-async
7837           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7838         (when kill-buffer
7839           (gnus-kill-or-deaden-summary kill-buffer))
7840         (when (get-buffer-window gnus-group-buffer t)
7841           ;; Gotta use windows, because recenter does wierd stuff if
7842           ;; the current buffer ain't the displayed window.
7843           (let ((owin (selected-window)))
7844             (select-window (get-buffer-window gnus-group-buffer t))
7845             (when (gnus-group-goto-group group)
7846               (recenter))
7847             (select-window owin))))
7848       ;; Mark this buffer as "prepared".
7849       (setq gnus-newsgroup-prepared t)
7850       t))))
7851
7852 (defun gnus-summary-prepare ()
7853   "Generate the summary buffer."
7854   (let ((buffer-read-only nil))
7855     (erase-buffer)
7856     (setq gnus-newsgroup-data nil
7857           gnus-newsgroup-data-reverse nil)
7858     (run-hooks 'gnus-summary-generate-hook)
7859     ;; Generate the buffer, either with threads or without.
7860     (when gnus-newsgroup-headers
7861       (gnus-summary-prepare-threads
7862        (if gnus-show-threads
7863            (gnus-sort-gathered-threads
7864             (funcall gnus-summary-thread-gathering-function
7865                      (gnus-sort-threads
7866                       (gnus-cut-threads (gnus-make-threads)))))
7867          ;; Unthreaded display.
7868          (gnus-sort-articles gnus-newsgroup-headers))))
7869     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7870     ;; Call hooks for modifying summary buffer.
7871     (goto-char (point-min))
7872     (run-hooks 'gnus-summary-prepare-hook)))
7873
7874 (defun gnus-gather-threads-by-subject (threads)
7875   "Gather threads by looking at Subject headers."
7876   (if (not gnus-summary-make-false-root)
7877       threads
7878     (let ((hashtb (gnus-make-hashtable 1023))
7879           (prev threads)
7880           (result threads)
7881           subject hthread whole-subject)
7882       (while threads
7883         (setq whole-subject (mail-header-subject (caar threads)))
7884         (setq subject
7885               (cond
7886                ;; Truncate the subject.
7887                ((numberp gnus-summary-gather-subject-limit)
7888                 (setq subject (gnus-simplify-subject-re whole-subject))
7889                 (if (> (length subject) gnus-summary-gather-subject-limit)
7890                     (substring subject 0 gnus-summary-gather-subject-limit)
7891                   subject))
7892                ;; Fuzzily simplify it.
7893                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7894                 (gnus-simplify-subject-fuzzy whole-subject))
7895                ;; Just remove the leading "Re:".
7896                (t
7897                 (gnus-simplify-subject-re whole-subject))))
7898
7899         (if (and gnus-summary-gather-exclude-subject
7900                  (string-match gnus-summary-gather-exclude-subject
7901                                subject))
7902             ()          ; We don't want to do anything with this article.
7903           ;; We simplify the subject before looking it up in the
7904           ;; hash table.
7905
7906           (if (setq hthread (gnus-gethash subject hashtb))
7907               (progn
7908                 ;; We enter a dummy root into the thread, if we
7909                 ;; haven't done that already.
7910                 (unless (stringp (caar hthread))
7911                   (setcar hthread (list whole-subject (car hthread))))
7912                 ;; We add this new gathered thread to this gathered
7913                 ;; thread.
7914                 (setcdr (car hthread)
7915                         (nconc (cdar hthread) (list (car threads))))
7916                 ;; Remove it from the list of threads.
7917                 (setcdr prev (cdr threads))
7918                 (setq threads prev))
7919             ;; Enter this thread into the hash table.
7920             (gnus-sethash subject threads hashtb)))
7921         (setq prev threads)
7922         (setq threads (cdr threads)))
7923       result)))
7924
7925 (defun gnus-gather-threads-by-references (threads)
7926   "Gather threads by looking at References headers."
7927   (let ((idhashtb (gnus-make-hashtable 1023))
7928         (thhashtb (gnus-make-hashtable 1023))
7929         (prev threads)
7930         (result threads)
7931         ids references id gthread gid entered)
7932     (while threads
7933       (when (setq references (mail-header-references (caar threads)))
7934         (setq id (mail-header-id (caar threads)))
7935         (setq ids (gnus-split-references references))
7936         (setq entered nil)
7937         (while ids
7938           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7939               (progn
7940                 (gnus-sethash (car ids) id idhashtb)
7941                 (gnus-sethash id threads thhashtb))
7942             (setq gthread (gnus-gethash gid thhashtb))
7943             (unless entered
7944               ;; We enter a dummy root into the thread, if we
7945               ;; haven't done that already.
7946               (unless (stringp (caar gthread))
7947                 (setcar gthread (list (mail-header-subject (caar gthread))
7948                                       (car gthread))))
7949               ;; We add this new gathered thread to this gathered
7950               ;; thread.
7951               (setcdr (car gthread)
7952                       (nconc (cdar gthread) (list (car threads)))))
7953             ;; Add it into the thread hash table.
7954             (gnus-sethash id gthread thhashtb)
7955             (setq entered t)
7956             ;; Remove it from the list of threads.
7957             (setcdr prev (cdr threads))
7958             (setq threads prev))
7959           (setq ids (cdr ids))))
7960       (setq prev threads)
7961       (setq threads (cdr threads)))
7962     result))
7963
7964 (defun gnus-sort-gathered-threads (threads)
7965   "Sort subtreads inside each gathered thread by article number."
7966   (let ((result threads))
7967     (while threads
7968       (when (stringp (caar threads))
7969         (setcdr (car threads)
7970                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7971       (setq threads (cdr threads)))
7972     result))
7973
7974 (defun gnus-make-threads ()
7975   "Go through the dependency hashtb and find the roots.  Return all threads."
7976   (let (threads)
7977     (mapatoms
7978      (lambda (refs)
7979        (unless (car (symbol-value refs))
7980          ;; These threads do not refer back to any other articles,
7981          ;; so they're roots.
7982          (setq threads (append (cdr (symbol-value refs)) threads))))
7983      gnus-newsgroup-dependencies)
7984     threads))
7985
7986 (defun gnus-build-sparse-threads ()
7987   (let ((headers gnus-newsgroup-headers)
7988         (deps gnus-newsgroup-dependencies)
7989         header references generation relations 
7990         cthread subject child end pthread relation)
7991     ;; First we create an alist of generations/relations, where 
7992     ;; generations is how much we trust the ralation, and the relation
7993     ;; is parent/child.
7994     (gnus-message 7 "Making sparse threads...")
7995     (save-excursion
7996       (nnheader-set-temp-buffer " *gnus sparse threads*")
7997       (while (setq header (pop headers))
7998         (when (and (setq references (mail-header-references header))
7999                    (not (string= references "")))
8000           (insert references)
8001           (setq child (mail-header-id header)
8002                 subject (mail-header-subject header))
8003           (setq generation 0)
8004           (while (search-backward ">" nil t)
8005             (setq end (1+ (point)))
8006             (when (search-backward "<" nil t)
8007               (push (list (incf generation) 
8008                           child (setq child (buffer-substring (point) end))
8009                           subject)
8010                     relations)))
8011           (push (list (1+ generation) child nil subject) relations)
8012           (erase-buffer)))
8013       (kill-buffer (current-buffer)))
8014     ;; Sort over trustworthiness.
8015     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
8016     (while (setq relation (pop relations))
8017       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
8018                 (unless (car (symbol-value cthread))
8019                   ;; Make this article the parent of these threads.
8020                   (setcar (symbol-value cthread)
8021                           (vector gnus-reffed-article-number 
8022                                   (cadddr relation) 
8023                                   "" ""
8024                                   (cadr relation) 
8025                                   (or (caddr relation) "") 0 0 "")))
8026               (set cthread (list (vector gnus-reffed-article-number
8027                                          (cadddr relation) 
8028                                          "" "" (cadr relation) 
8029                                          (or (caddr relation) "") 0 0 ""))))
8030         (push gnus-reffed-article-number gnus-newsgroup-limit)
8031         (push gnus-reffed-article-number gnus-newsgroup-sparse)
8032         (push (cons gnus-reffed-article-number gnus-sparse-mark)
8033               gnus-newsgroup-reads)
8034         (decf gnus-reffed-article-number)
8035         ;; Make this new thread the child of its parent.
8036         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
8037             (setcdr (symbol-value pthread)
8038                     (nconc (cdr (symbol-value pthread))
8039                            (list (symbol-value cthread))))
8040           (set pthread (list nil (symbol-value cthread))))))
8041     (gnus-message 7 "Making sparse threads...done")))
8042
8043 (defun gnus-build-old-threads ()
8044   ;; Look at all the articles that refer back to old articles, and
8045   ;; fetch the headers for the articles that aren't there.  This will
8046   ;; build complete threads - if the roots haven't been expired by the
8047   ;; server, that is.
8048   (let (id heads)
8049     (mapatoms
8050      (lambda (refs)
8051        (when (not (car (symbol-value refs)))
8052          (setq heads (cdr (symbol-value refs)))
8053          (while heads
8054            (if (memq (mail-header-number (caar heads))
8055                      gnus-newsgroup-dormant)
8056                (setq heads (cdr heads))
8057              (setq id (symbol-name refs))
8058              (while (and (setq id (gnus-build-get-header id))
8059                          (not (car (gnus-gethash
8060                                     id gnus-newsgroup-dependencies)))))
8061              (setq heads nil)))))
8062      gnus-newsgroup-dependencies)))
8063
8064 (defun gnus-build-get-header (id)
8065   ;; Look through the buffer of NOV lines and find the header to
8066   ;; ID.  Enter this line into the dependencies hash table, and return
8067   ;; the id of the parent article (if any).
8068   (let ((deps gnus-newsgroup-dependencies)
8069         found header)
8070     (prog1
8071         (save-excursion
8072           (set-buffer nntp-server-buffer)
8073           (goto-char (point-min))
8074           (while (and (not found) (search-forward id nil t))
8075             (beginning-of-line)
8076             (setq found (looking-at
8077                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
8078                                  (regexp-quote id))))
8079             (or found (beginning-of-line 2)))
8080           (when found
8081             (beginning-of-line)
8082             (and
8083              (setq header (gnus-nov-parse-line
8084                            (read (current-buffer)) deps))
8085              (gnus-parent-id (mail-header-references header)))))
8086       (when header
8087         (let ((number (mail-header-number header)))
8088           (push number gnus-newsgroup-limit)
8089           (push header gnus-newsgroup-headers)
8090           (if (memq number gnus-newsgroup-unselected)
8091               (progn
8092                 (push number gnus-newsgroup-unreads)
8093                 (setq gnus-newsgroup-unselected
8094                       (delq number gnus-newsgroup-unselected)))
8095             (push number gnus-newsgroup-ancient)))))))
8096
8097 (defun gnus-summary-update-article (article &optional iheader)
8098   "Update ARTICLE in the summary buffer."
8099   (set-buffer gnus-summary-buffer)
8100   (let* ((header (or iheader (gnus-summary-article-header article)))
8101          (id (mail-header-id header))
8102          (data (gnus-data-find article))
8103          (thread (gnus-id-to-thread id))
8104          (references (mail-header-references header))
8105          (parent
8106           (gnus-id-to-thread
8107            (or (gnus-parent-id 
8108                 (if (and references
8109                          (not (equal "" references)))
8110                     references))
8111                "none")))
8112          (buffer-read-only nil)
8113          (old (car thread))
8114          (number (mail-header-number header))
8115          pos)
8116     (when thread
8117       ;; !!! Should this be in or not?
8118       (unless iheader
8119         (setcar thread nil))
8120       (when parent
8121         (delq thread parent))
8122       (if (gnus-summary-insert-subject id header iheader)
8123           ;; Set the (possibly) new article number in the data structure.
8124           (gnus-data-set-number data (gnus-id-to-article id))
8125         (setcar thread old)
8126         nil))))
8127
8128 (defun gnus-rebuild-thread (id)
8129   "Rebuild the thread containing ID."
8130   (let ((buffer-read-only nil)
8131         current thread data)
8132     (if (not gnus-show-threads)
8133         (setq thread (list (car (gnus-id-to-thread id))))
8134       ;; Get the thread this article is part of.
8135       (setq thread (gnus-remove-thread id)))
8136     (setq current (save-excursion
8137                     (and (zerop (forward-line -1))
8138                          (gnus-summary-article-number))))
8139     ;; If this is a gathered thread, we have to go some re-gathering.
8140     (when (stringp (car thread))
8141       (let ((subject (car thread))
8142             roots thr)
8143         (setq thread (cdr thread))
8144         (while thread
8145           (unless (memq (setq thr (gnus-id-to-thread
8146                                       (gnus-root-id
8147                                        (mail-header-id (caar thread)))))
8148                         roots)
8149             (push thr roots))
8150           (setq thread (cdr thread)))
8151         ;; We now have all (unique) roots.
8152         (if (= (length roots) 1)
8153             ;; All the loose roots are now one solid root.
8154             (setq thread (car roots))
8155           (setq thread (cons subject (gnus-sort-threads roots))))))
8156     (let (threads)
8157       ;; We then insert this thread into the summary buffer.
8158       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8159         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8160         (setq data (nreverse gnus-newsgroup-data))
8161         (setq threads gnus-newsgroup-threads))
8162       ;; We splice the new data into the data structure.
8163       (gnus-data-enter-list current data)
8164       (gnus-data-compute-positions)
8165       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8166
8167 (defun gnus-number-to-header (number)
8168   "Return the header for article NUMBER."
8169   (let ((headers gnus-newsgroup-headers))
8170     (while (and headers
8171                 (not (= number (mail-header-number (car headers)))))
8172       (pop headers))
8173     (when headers
8174       (car headers))))
8175
8176 (defun gnus-id-to-thread (id)
8177   "Return the (sub-)thread where ID appears."
8178   (gnus-gethash id gnus-newsgroup-dependencies))
8179
8180 (defun gnus-id-to-article (id)
8181   "Return the article number of ID."
8182   (let ((thread (gnus-id-to-thread id)))
8183     (when (and thread
8184                (car thread))
8185       (mail-header-number (car thread)))))
8186
8187 (defun gnus-id-to-header (id)
8188   "Return the article headers of ID."
8189   (car (gnus-id-to-thread id)))
8190
8191 (defun gnus-article-displayed-root-p (article)
8192   "Say whether ARTICLE is a root(ish) article."
8193   (let ((level (gnus-summary-thread-level article))
8194         (refs (mail-header-references  (gnus-summary-article-header article)))
8195         particle)
8196     (cond 
8197      ((null level) nil)
8198      ((zerop level) t)
8199      ((null refs) t)
8200      ((null (gnus-parent-id refs)) t)
8201      ((and (= 1 level)
8202            (null (setq particle (gnus-id-to-article
8203                                  (gnus-parent-id refs))))
8204            (null (gnus-summary-thread-level particle)))))))
8205
8206 (defun gnus-root-id (id)
8207   "Return the id of the root of the thread where ID appears."
8208   (let (last-id prev)
8209     (while (and id (setq prev (car (gnus-gethash 
8210                                     id gnus-newsgroup-dependencies))))
8211       (setq last-id id
8212             id (gnus-parent-id (mail-header-references prev))))
8213     last-id))
8214
8215 (defun gnus-remove-thread (id &optional dont-remove)
8216   "Remove the thread that has ID in it."
8217   (let ((dep gnus-newsgroup-dependencies)
8218         headers thread last-id)
8219     ;; First go up in this thread until we find the root.
8220     (setq last-id (gnus-root-id id))
8221     (setq headers (list (car (gnus-id-to-thread last-id))
8222                         (caadr (gnus-id-to-thread last-id))))
8223     ;; We have now found the real root of this thread.  It might have
8224     ;; been gathered into some loose thread, so we have to search
8225     ;; through the threads to find the thread we wanted.
8226     (let ((threads gnus-newsgroup-threads)
8227           sub)
8228       (while threads
8229         (setq sub (car threads))
8230         (if (stringp (car sub))
8231             ;; This is a gathered threads, so we look at the roots
8232             ;; below it to find whether this article in in this
8233             ;; gathered root.
8234             (progn
8235               (setq sub (cdr sub))
8236               (while sub
8237                 (when (member (caar sub) headers)
8238                   (setq thread (car threads)
8239                         threads nil
8240                         sub nil))
8241                 (setq sub (cdr sub))))
8242           ;; It's an ordinary thread, so we check it.
8243           (when (eq (car sub) (car headers))
8244             (setq thread sub
8245                   threads nil)))
8246         (setq threads (cdr threads)))
8247       ;; If this article is in no thread, then it's a root.
8248       (if thread
8249           (unless dont-remove
8250             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8251         (setq thread (gnus-gethash last-id dep)))
8252       (when thread
8253         (prog1
8254             thread ; We return this thread.
8255           (unless dont-remove
8256             (if (stringp (car thread))
8257                 (progn
8258                   ;; If we use dummy roots, then we have to remove the
8259                   ;; dummy root as well.
8260                   (when (eq gnus-summary-make-false-root 'dummy)
8261                     ;; Uhm.
8262                     )
8263                   (setq thread (cdr thread))
8264                   (while thread
8265                     (gnus-remove-thread-1 (car thread))
8266                     (setq thread (cdr thread))))
8267               (gnus-remove-thread-1 thread))))))))
8268
8269 (defun gnus-remove-thread-1 (thread)
8270   "Remove the thread THREAD recursively."
8271   (let ((number (mail-header-number (car thread)))
8272         pos)
8273     (when (setq pos (text-property-any
8274                      (point-min) (point-max) 'gnus-number number))
8275       (goto-char pos)
8276       (gnus-delete-line)
8277       (gnus-data-remove number))
8278     (setq thread (cdr thread))
8279     (while thread
8280       (gnus-remove-thread-1 (pop thread)))))
8281
8282 (defun gnus-sort-threads (threads)
8283   "Sort THREADS."
8284   (if (not gnus-thread-sort-functions)
8285       threads
8286     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8287                     (car gnus-thread-sort-functions)
8288                   `(lambda (t1 t2)
8289                      ,(gnus-make-sort-function 
8290                        (reverse gnus-thread-sort-functions))))))
8291       (gnus-message 7 "Sorting threads...")
8292       (prog1
8293           (sort threads func)
8294         (gnus-message 7 "Sorting threads...done")))))
8295
8296 (defun gnus-sort-articles (articles)
8297   "Sort ARTICLES."
8298   (when gnus-article-sort-functions
8299     (let ((func (if (= 1 (length gnus-article-sort-functions))
8300                     (car gnus-article-sort-functions)
8301                   `(lambda (t1 t2)
8302                      ,(gnus-make-sort-function 
8303                        (reverse gnus-article-sort-functions))))))
8304       (gnus-message 7 "Sorting articles...")
8305       (prog1
8306           (setq gnus-newsgroup-headers (sort articles func))
8307         (gnus-message 7 "Sorting articles...done")))))
8308
8309 (defun gnus-make-sort-function (funs)
8310   "Return a composite sort condition based on the functions in FUNC."
8311   (if (cdr funs)
8312       `(or (,(car funs) t1 t2)
8313            (and (not (,(car funs) t2 t1))
8314                 ,(gnus-make-sort-function (cdr funs))))
8315     `(,(car funs) t1 t2)))
8316                  
8317 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8318 (defmacro gnus-thread-header (thread)
8319   ;; Return header of first article in THREAD.
8320   ;; Note that THREAD must never, ever be anything else than a variable -
8321   ;; using some other form will lead to serious barfage.
8322   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8323   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8324   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8325         (vector thread) 2))
8326
8327 (defsubst gnus-article-sort-by-number (h1 h2)
8328   "Sort articles by article number."
8329   (< (mail-header-number h1)
8330      (mail-header-number h2)))
8331
8332 (defun gnus-thread-sort-by-number (h1 h2)
8333   "Sort threads by root article number."
8334   (gnus-article-sort-by-number
8335    (gnus-thread-header h1) (gnus-thread-header h2)))
8336
8337 (defsubst gnus-article-sort-by-author (h1 h2)
8338   "Sort articles by root author."
8339   (string-lessp
8340    (let ((extract (funcall
8341                    gnus-extract-address-components
8342                    (mail-header-from h1))))
8343      (or (car extract) (cdr extract)))
8344    (let ((extract (funcall
8345                    gnus-extract-address-components
8346                    (mail-header-from h2))))
8347      (or (car extract) (cdr extract)))))
8348
8349 (defun gnus-thread-sort-by-author (h1 h2)
8350   "Sort threads by root author."
8351   (gnus-article-sort-by-author
8352    (gnus-thread-header h1)  (gnus-thread-header h2)))
8353
8354 (defsubst gnus-article-sort-by-subject (h1 h2)
8355   "Sort articles by root subject."
8356   (string-lessp
8357    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8358    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8359
8360 (defun gnus-thread-sort-by-subject (h1 h2)
8361   "Sort threads by root subject."
8362   (gnus-article-sort-by-subject
8363    (gnus-thread-header h1) (gnus-thread-header h2)))
8364
8365 (defsubst gnus-article-sort-by-date (h1 h2)
8366   "Sort articles by root article date."
8367   (string-lessp
8368    (inline (gnus-sortable-date (mail-header-date h1)))
8369    (inline (gnus-sortable-date (mail-header-date h2)))))
8370
8371 (defun gnus-thread-sort-by-date (h1 h2)
8372   "Sort threads by root article date."
8373   (gnus-article-sort-by-date
8374    (gnus-thread-header h1) (gnus-thread-header h2)))
8375
8376 (defsubst gnus-article-sort-by-score (h1 h2)
8377   "Sort articles by root article score.
8378 Unscored articles will be counted as having a score of zero."
8379   (> (or (cdr (assq (mail-header-number h1)
8380                     gnus-newsgroup-scored))
8381          gnus-summary-default-score 0)
8382      (or (cdr (assq (mail-header-number h2)
8383                     gnus-newsgroup-scored))
8384          gnus-summary-default-score 0)))
8385
8386 (defun gnus-thread-sort-by-score (h1 h2)
8387   "Sort threads by root article score."
8388   (gnus-article-sort-by-score
8389    (gnus-thread-header h1) (gnus-thread-header h2)))
8390
8391 (defun gnus-thread-sort-by-total-score (h1 h2)
8392   "Sort threads by the sum of all scores in the thread.
8393 Unscored articles will be counted as having a score of zero."
8394   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8395
8396 (defun gnus-thread-total-score (thread)
8397   ;;  This function find the total score of THREAD.
8398   (cond ((null thread)
8399          0)
8400         ((consp thread)
8401          (if (stringp (car thread))
8402              (apply gnus-thread-score-function 0
8403                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8404            (gnus-thread-total-score-1 thread)))
8405         (t
8406          (gnus-thread-total-score-1 (list thread)))))
8407
8408 (defun gnus-thread-total-score-1 (root)
8409   ;; This function find the total score of the thread below ROOT.
8410   (setq root (car root))
8411   (apply gnus-thread-score-function
8412          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8413              gnus-summary-default-score 0)
8414          (mapcar 'gnus-thread-total-score
8415                  (cdr (gnus-gethash (mail-header-id root)
8416                                     gnus-newsgroup-dependencies)))))
8417
8418 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8419 (defvar gnus-tmp-prev-subject nil)
8420 (defvar gnus-tmp-false-parent nil)
8421 (defvar gnus-tmp-root-expunged nil)
8422 (defvar gnus-tmp-dummy-line nil)
8423
8424 (defun gnus-summary-prepare-threads (threads)
8425   "Prepare summary buffer from THREADS and indentation LEVEL.
8426 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8427 or a straight list of headers."
8428   (gnus-message 7 "Generating summary...")
8429
8430   (setq gnus-newsgroup-threads threads)
8431   (beginning-of-line)
8432
8433   (let ((gnus-tmp-level 0)
8434         (default-score (or gnus-summary-default-score 0))
8435         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8436         thread number subject stack state gnus-tmp-gathered beg-match
8437         new-roots gnus-tmp-new-adopts thread-end
8438         gnus-tmp-header gnus-tmp-unread
8439         gnus-tmp-replied gnus-tmp-subject-or-nil
8440         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8441         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8442         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8443
8444     (setq gnus-tmp-prev-subject nil)
8445
8446     (if (vectorp (car threads))
8447         ;; If this is a straight (sic) list of headers, then a
8448         ;; threaded summary display isn't required, so we just create
8449         ;; an unthreaded one.
8450         (gnus-summary-prepare-unthreaded threads)
8451
8452       ;; Do the threaded display.
8453
8454       (while (or threads stack gnus-tmp-new-adopts new-roots)
8455
8456         (if (and (= gnus-tmp-level 0)
8457                  (not (setq gnus-tmp-dummy-line nil))
8458                  (or (not stack)
8459                      (= (caar stack) 0))
8460                  (not gnus-tmp-false-parent)
8461                  (or gnus-tmp-new-adopts new-roots))
8462             (if gnus-tmp-new-adopts
8463                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8464                       thread (list (car gnus-tmp-new-adopts))
8465                       gnus-tmp-header (caar thread)
8466                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8467               (if new-roots
8468                   (setq thread (list (car new-roots))
8469                         gnus-tmp-header (caar thread)
8470                         new-roots (cdr new-roots))))
8471
8472           (if threads
8473               ;; If there are some threads, we do them before the
8474               ;; threads on the stack.
8475               (setq thread threads
8476                     gnus-tmp-header (caar thread))
8477             ;; There were no current threads, so we pop something off
8478             ;; the stack.
8479             (setq state (car stack)
8480                   gnus-tmp-level (car state)
8481                   thread (cdr state)
8482                   stack (cdr stack)
8483                   gnus-tmp-header (caar thread))))
8484
8485         (setq gnus-tmp-false-parent nil)
8486         (setq gnus-tmp-root-expunged nil)
8487         (setq thread-end nil)
8488
8489         (if (stringp gnus-tmp-header)
8490             ;; The header is a dummy root.
8491             (cond
8492              ((eq gnus-summary-make-false-root 'adopt)
8493               ;; We let the first article adopt the rest.
8494               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8495                                                (cddar thread)))
8496               (setq gnus-tmp-gathered
8497                     (nconc (mapcar
8498                             (lambda (h) (mail-header-number (car h)))
8499                             (cddar thread))
8500                            gnus-tmp-gathered))
8501               (setq thread (cons (list (caar thread)
8502                                        (cadar thread))
8503                                  (cdr thread)))
8504               (setq gnus-tmp-level -1
8505                     gnus-tmp-false-parent t))
8506              ((eq gnus-summary-make-false-root 'empty)
8507               ;; We print adopted articles with empty subject fields.
8508               (setq gnus-tmp-gathered
8509                     (nconc (mapcar
8510                             (lambda (h) (mail-header-number (car h)))
8511                             (cddar thread))
8512                            gnus-tmp-gathered))
8513               (setq gnus-tmp-level -1))
8514              ((eq gnus-summary-make-false-root 'dummy)
8515               ;; We remember that we probably want to output a dummy
8516               ;; root.
8517               (setq gnus-tmp-dummy-line gnus-tmp-header)
8518               (setq gnus-tmp-prev-subject gnus-tmp-header))
8519              (t
8520               ;; We do not make a root for the gathered
8521               ;; sub-threads at all.
8522               (setq gnus-tmp-level -1)))
8523
8524           (setq number (mail-header-number gnus-tmp-header)
8525                 subject (mail-header-subject gnus-tmp-header))
8526
8527           (cond
8528            ;; If the thread has changed subject, we might want to make
8529            ;; this subthread into a root.
8530            ((and (null gnus-thread-ignore-subject)
8531                  (not (zerop gnus-tmp-level))
8532                  gnus-tmp-prev-subject
8533                  (not (inline
8534                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8535             (setq new-roots (nconc new-roots (list (car thread)))
8536                   thread-end t
8537                   gnus-tmp-header nil))
8538            ;; If the article lies outside the current limit,
8539            ;; then we do not display it.
8540            ((and (not (memq number gnus-newsgroup-limit))
8541                  (not gnus-tmp-dummy-line))
8542             (setq gnus-tmp-gathered
8543                   (nconc (mapcar
8544                           (lambda (h) (mail-header-number (car h)))
8545                           (cdar thread))
8546                          gnus-tmp-gathered))
8547             (setq gnus-tmp-new-adopts (if (cdar thread)
8548                                           (append gnus-tmp-new-adopts
8549                                                   (cdar thread))
8550                                         gnus-tmp-new-adopts)
8551                   thread-end t
8552                   gnus-tmp-header nil)
8553             (when (zerop gnus-tmp-level)
8554               (setq gnus-tmp-root-expunged t)))
8555            ;; Perhaps this article is to be marked as read?
8556            ((and gnus-summary-mark-below
8557                  (< (or (cdr (assq number gnus-newsgroup-scored))
8558                         default-score)
8559                     gnus-summary-mark-below)
8560                  ;; Don't touch sparse articles.
8561                  (not (memq number gnus-newsgroup-sparse))
8562                  (not (memq number gnus-newsgroup-ancient)))
8563             (setq gnus-newsgroup-unreads
8564                   (delq number gnus-newsgroup-unreads))
8565             (if gnus-newsgroup-auto-expire
8566                 (push number gnus-newsgroup-expirable)
8567               (push (cons number gnus-low-score-mark)
8568                     gnus-newsgroup-reads))))
8569
8570           (when gnus-tmp-header
8571             ;; We may have an old dummy line to output before this
8572             ;; article.
8573             (when gnus-tmp-dummy-line
8574               (gnus-summary-insert-dummy-line
8575                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8576               (setq gnus-tmp-dummy-line nil))
8577
8578             ;; Compute the mark.
8579             (setq
8580              gnus-tmp-unread
8581              (cond
8582               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8583               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8584               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8585               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8586               (t (or (cdr (assq number gnus-newsgroup-reads))
8587                      gnus-ancient-mark))))
8588
8589             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8590                                   gnus-tmp-header gnus-tmp-level)
8591                   gnus-newsgroup-data)
8592
8593             ;; Actually insert the line.
8594             (setq
8595              gnus-tmp-subject-or-nil
8596              (cond
8597               ((and gnus-thread-ignore-subject
8598                     gnus-tmp-prev-subject
8599                     (not (inline (gnus-subject-equal
8600                                   gnus-tmp-prev-subject subject))))
8601                subject)
8602               ((zerop gnus-tmp-level)
8603                (if (and (eq gnus-summary-make-false-root 'empty)
8604                         (memq number gnus-tmp-gathered)
8605                         gnus-tmp-prev-subject
8606                         (inline (gnus-subject-equal
8607                                  gnus-tmp-prev-subject subject)))
8608                    gnus-summary-same-subject
8609                  subject))
8610               (t gnus-summary-same-subject)))
8611             (if (and (eq gnus-summary-make-false-root 'adopt)
8612                      (= gnus-tmp-level 1)
8613                      (memq number gnus-tmp-gathered))
8614                 (setq gnus-tmp-opening-bracket ?\<
8615                       gnus-tmp-closing-bracket ?\>)
8616               (setq gnus-tmp-opening-bracket ?\[
8617                     gnus-tmp-closing-bracket ?\]))
8618             (setq
8619              gnus-tmp-indentation
8620              (aref gnus-thread-indent-array gnus-tmp-level)
8621              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8622              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8623                                 gnus-summary-default-score 0)
8624              gnus-tmp-score-char
8625              (if (or (null gnus-summary-default-score)
8626                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8627                          gnus-summary-zcore-fuzz)) ? 
8628                (if (< gnus-tmp-score gnus-summary-default-score)
8629                    gnus-score-below-mark gnus-score-over-mark))
8630              gnus-tmp-replied
8631              (cond ((memq number gnus-newsgroup-processable)
8632                     gnus-process-mark)
8633                    ((memq number gnus-newsgroup-cached)
8634                     gnus-cached-mark)
8635                    ((memq number gnus-newsgroup-replied)
8636                     gnus-replied-mark)
8637                    (t gnus-unread-mark))
8638              gnus-tmp-from (mail-header-from gnus-tmp-header)
8639              gnus-tmp-name
8640              (cond
8641               ((string-match "(.+)" gnus-tmp-from)
8642                (substring gnus-tmp-from
8643                           (1+ (match-beginning 0)) (1- (match-end 0))))
8644               ((string-match "<[^>]+> *$" gnus-tmp-from)
8645                (setq beg-match (match-beginning 0))
8646                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8647                         (substring gnus-tmp-from (1+ (match-beginning 0))
8648                                    (1- (match-end 0))))
8649                    (substring gnus-tmp-from 0 beg-match)))
8650               (t gnus-tmp-from)))
8651             (when (string= gnus-tmp-name "")
8652               (setq gnus-tmp-name gnus-tmp-from))
8653             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8654             (gnus-put-text-property
8655              (point)
8656              (progn (eval gnus-summary-line-format-spec) (point))
8657              'gnus-number number)
8658             (when gnus-visual-p
8659               (forward-line -1)
8660               (run-hooks 'gnus-summary-update-hook)
8661               (forward-line 1))
8662
8663             (setq gnus-tmp-prev-subject subject)))
8664
8665         (when (nth 1 thread)
8666           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8667         (incf gnus-tmp-level)
8668         (setq threads (if thread-end nil (cdar thread)))
8669         (unless threads
8670           (setq gnus-tmp-level 0)))))
8671   (gnus-message 7 "Generating summary...done"))
8672
8673 (defun gnus-summary-prepare-unthreaded (headers)
8674   "Generate an unthreaded summary buffer based on HEADERS."
8675   (let (header number mark)
8676
8677     (while headers
8678       ;; We may have to root out some bad articles...
8679       (when (memq (setq number (mail-header-number
8680                                 (setq header (pop headers))))
8681                   gnus-newsgroup-limit)
8682         ;; Mark article as read when it has a low score.
8683         (when (and gnus-summary-mark-below
8684                    (< (or (cdr (assq number gnus-newsgroup-scored))
8685                           gnus-summary-default-score 0)
8686                       gnus-summary-mark-below)
8687                    (not (memq number gnus-newsgroup-ancient)))
8688           (setq gnus-newsgroup-unreads
8689                 (delq number gnus-newsgroup-unreads))
8690           (if gnus-newsgroup-auto-expire
8691               (push number gnus-newsgroup-expirable)
8692             (push (cons number gnus-low-score-mark)
8693                   gnus-newsgroup-reads)))
8694
8695         (setq mark
8696               (cond
8697                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8698                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8699                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8700                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8701                (t (or (cdr (assq number gnus-newsgroup-reads))
8702                       gnus-ancient-mark))))
8703         (setq gnus-newsgroup-data
8704               (cons (gnus-data-make number mark (1+ (point)) header 0)
8705                     gnus-newsgroup-data))
8706         (gnus-summary-insert-line
8707          header 0 nil mark (memq number gnus-newsgroup-replied)
8708          (memq number gnus-newsgroup-expirable)
8709          (mail-header-subject header) nil
8710          (cdr (assq number gnus-newsgroup-scored))
8711          (memq number gnus-newsgroup-processable))))))
8712
8713 (defun gnus-select-newsgroup (group &optional read-all)
8714   "Select newsgroup GROUP.
8715 If READ-ALL is non-nil, all articles in the group are selected."
8716   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8717          (info (nth 2 entry))
8718          articles fetched-articles cached)
8719
8720     (or (gnus-check-server
8721          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8722         (error "Couldn't open server"))
8723
8724     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8725         (gnus-activate-group group)     ; Or we can activate it...
8726         (progn                          ; Or we bug out.
8727           (when (equal major-mode 'gnus-summary-mode)
8728             (kill-buffer (current-buffer)))
8729           (error "Couldn't request group %s: %s"
8730                  group (gnus-status-message group))))
8731
8732     (unless (gnus-request-group group t)
8733       (when (equal major-mode 'gnus-summary-mode)
8734         (kill-buffer (current-buffer)))
8735       (error "Couldn't request group %s: %s"
8736              group (gnus-status-message group)))      
8737
8738     (setq gnus-newsgroup-name group)
8739     (setq gnus-newsgroup-unselected nil)
8740     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8741
8742     (and gnus-asynchronous
8743          (gnus-check-backend-function
8744           'request-asynchronous gnus-newsgroup-name)
8745          (setq gnus-newsgroup-async
8746                (gnus-request-asynchronous gnus-newsgroup-name)))
8747
8748     ;; Adjust and set lists of article marks.
8749     (when info
8750       (gnus-adjust-marked-articles info))
8751
8752     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8753     (when (gnus-virtual-group-p group)
8754       (setq cached gnus-newsgroup-cached))
8755
8756     (setq gnus-newsgroup-unreads
8757           (gnus-set-difference
8758            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8759            gnus-newsgroup-dormant))
8760
8761     (setq gnus-newsgroup-processable nil)
8762
8763     (setq articles (gnus-articles-to-read group read-all))
8764
8765     (cond
8766      ((null articles)
8767       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8768       'quit)
8769      ((eq articles 0) nil)
8770      (t
8771       ;; Init the dependencies hash table.
8772       (setq gnus-newsgroup-dependencies
8773             (gnus-make-hashtable (length articles)))
8774       ;; Retrieve the headers and read them in.
8775       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8776       (setq gnus-newsgroup-headers
8777             (if (eq 'nov
8778                     (setq gnus-headers-retrieved-by
8779                           (gnus-retrieve-headers
8780                            articles gnus-newsgroup-name
8781                            ;; We might want to fetch old headers, but
8782                            ;; not if there is only 1 article.
8783                            (and gnus-fetch-old-headers
8784                                 (or (and
8785                                      (not (eq gnus-fetch-old-headers 'some))
8786                                      (not (numberp gnus-fetch-old-headers)))
8787                                     (> (length articles) 1))))))
8788                 (gnus-get-newsgroup-headers-xover articles)
8789               (gnus-get-newsgroup-headers)))
8790       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8791
8792       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8793       (when cached
8794         (setq gnus-newsgroup-cached cached))
8795
8796       ;; Set the initial limit.
8797       (setq gnus-newsgroup-limit (copy-sequence articles))
8798       ;; Remove canceled articles from the list of unread articles.
8799       (setq gnus-newsgroup-unreads
8800             (gnus-set-sorted-intersection
8801              gnus-newsgroup-unreads
8802              (setq fetched-articles
8803                    (mapcar (lambda (headers) (mail-header-number headers))
8804                            gnus-newsgroup-headers))))
8805       ;; Removed marked articles that do not exist.
8806       (gnus-update-missing-marks
8807        (gnus-sorted-complement fetched-articles articles))
8808       ;; We might want to build some more threads first.
8809       (and gnus-fetch-old-headers
8810            (eq gnus-headers-retrieved-by 'nov)
8811            (gnus-build-old-threads))
8812       ;; Check whether auto-expire is to be done in this group.
8813       (setq gnus-newsgroup-auto-expire
8814             (gnus-group-auto-expirable-p group))
8815       ;; Set up the article buffer now, if necessary.
8816       (unless gnus-single-article-buffer
8817         (gnus-article-setup-buffer))
8818       ;; First and last article in this newsgroup.
8819       (when gnus-newsgroup-headers
8820         (setq gnus-newsgroup-begin
8821               (mail-header-number (car gnus-newsgroup-headers))
8822               gnus-newsgroup-end
8823               (mail-header-number
8824                (gnus-last-element gnus-newsgroup-headers))))
8825       (setq gnus-reffed-article-number -1)
8826       ;; GROUP is successfully selected.
8827       (or gnus-newsgroup-headers t)))))
8828
8829 (defun gnus-articles-to-read (group read-all)
8830   ;; Find out what articles the user wants to read.
8831   (let* ((articles
8832           ;; Select all articles if `read-all' is non-nil, or if there
8833           ;; are no unread articles.
8834           (if (or read-all
8835                   (and (zerop (length gnus-newsgroup-marked))
8836                        (zerop (length gnus-newsgroup-unreads))))
8837               (gnus-uncompress-range (gnus-active group))
8838             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8839                           (copy-sequence gnus-newsgroup-unreads))
8840                   '<)))
8841          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8842          (scored (length scored-list))
8843          (number (length articles))
8844          (marked (+ (length gnus-newsgroup-marked)
8845                     (length gnus-newsgroup-dormant)))
8846          (select
8847           (cond
8848            ((numberp read-all)
8849             read-all)
8850            (t
8851             (condition-case ()
8852                 (cond
8853                  ((and (or (<= scored marked) (= scored number))
8854                        (numberp gnus-large-newsgroup)
8855                        (> number gnus-large-newsgroup))
8856                   (let ((input
8857                          (read-string
8858                           (format
8859                            "How many articles from %s (default %d): "
8860                            gnus-newsgroup-name number))))
8861                     (if (string-match "^[ \t]*$" input) number input)))
8862                  ((and (> scored marked) (< scored number)
8863                        (> (- scored number) 20))
8864                   (let ((input
8865                          (read-string
8866                           (format "%s %s (%d scored, %d total): "
8867                                   "How many articles from"
8868                                   group scored number))))
8869                     (if (string-match "^[ \t]*$" input)
8870                         number input)))
8871                  (t number))
8872               (quit nil))))))
8873     (setq select (if (stringp select) (string-to-number select) select))
8874     (if (or (null select) (zerop select))
8875         select
8876       (if (and (not (zerop scored)) (<= (abs select) scored))
8877           (progn
8878             (setq articles (sort scored-list '<))
8879             (setq number (length articles)))
8880         (setq articles (copy-sequence articles)))
8881
8882       (if (< (abs select) number)
8883           (if (< select 0)
8884               ;; Select the N oldest articles.
8885               (setcdr (nthcdr (1- (abs select)) articles) nil)
8886             ;; Select the N most recent articles.
8887             (setq articles (nthcdr (- number select) articles))))
8888       (setq gnus-newsgroup-unselected
8889             (gnus-sorted-intersection
8890              gnus-newsgroup-unreads
8891              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8892       articles)))
8893
8894 (defun gnus-killed-articles (killed articles)
8895   (let (out)
8896     (while articles
8897       (if (inline (gnus-member-of-range (car articles) killed))
8898           (setq out (cons (car articles) out)))
8899       (setq articles (cdr articles)))
8900     out))
8901
8902 (defun gnus-uncompress-marks (marks)
8903   "Uncompress the mark ranges in MARKS."
8904   (let ((uncompressed '(score bookmark))
8905         out)
8906     (while marks
8907       (if (memq (caar marks) uncompressed)
8908           (push (car marks) out)
8909         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8910       (setq marks (cdr marks)))
8911     out))
8912
8913 (defun gnus-adjust-marked-articles (info)
8914   "Set all article lists and remove all marks that are no longer legal."
8915   (let* ((marked-lists (gnus-info-marks info))
8916          (active (gnus-active (gnus-info-group info)))
8917          (min (car active))
8918          (max (cdr active))
8919          (types gnus-article-mark-lists)
8920          (uncompressed '(score bookmark))
8921          marks var articles article mark)
8922
8923     (while marked-lists
8924       (setq marks (pop marked-lists))
8925       (set (setq var (intern (format "gnus-newsgroup-%s"
8926                                      (car (rassq (setq mark (car marks))
8927                                                  types)))))
8928            (if (memq (car marks) uncompressed) (cdr marks)
8929              (gnus-uncompress-range (cdr marks))))
8930
8931       (setq articles (symbol-value var))
8932
8933       ;; All articles have to be subsets of the active articles.
8934       (cond
8935        ;; Adjust "simple" lists.
8936        ((memq mark '(tick dormant expirable reply killed save))
8937         (while articles
8938           (when (or (< (setq article (pop articles)) min) (> article max))
8939             (set var (delq article (symbol-value var))))))
8940        ;; Adjust assocs.
8941        ((memq mark '(score bookmark))
8942         (while articles
8943           (when (or (< (car (setq article (pop articles))) min)
8944                     (> (car article) max))
8945             (set var (delq article (symbol-value var))))))))))
8946
8947 (defun gnus-update-missing-marks (missing)
8948   "Go through the list of MISSING articles and remove them mark lists."
8949   (when missing
8950     (let ((types gnus-article-mark-lists)
8951           var m)
8952       ;; Go through all types.
8953       (while types
8954         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8955         (when (symbol-value var)
8956           ;; This list has articles.  So we delete all missing articles
8957           ;; from it.
8958           (setq m missing)
8959           (while m
8960             (set var (delq (pop m) (symbol-value var)))))))))
8961
8962 (defun gnus-update-marks ()
8963   "Enter the various lists of marked articles into the newsgroup info list."
8964   (let ((types gnus-article-mark-lists)
8965         (info (gnus-get-info gnus-newsgroup-name))
8966         (uncompressed '(score bookmark killed))
8967         type list newmarked symbol)
8968     (when info
8969       ;; Add all marks lists that are non-nil to the list of marks lists.
8970       (while types
8971         (setq type (pop types))
8972         (when (setq list (symbol-value
8973                           (setq symbol
8974                                 (intern (format "gnus-newsgroup-%s"
8975                                                 (car type))))))
8976           (push (cons (cdr type)
8977                       (if (memq (cdr type) uncompressed) list
8978                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8979                 newmarked)))
8980
8981       ;; Enter these new marks into the info of the group.
8982       (if (nthcdr 3 info)
8983           (setcar (nthcdr 3 info) newmarked)
8984         ;; Add the marks lists to the end of the info.
8985         (when newmarked
8986           (setcdr (nthcdr 2 info) (list newmarked))))
8987
8988       ;; Cut off the end of the info if there's nothing else there.
8989       (let ((i 5))
8990         (while (and (> i 2)
8991                     (not (nth i info)))
8992           (when (nthcdr (decf i) info)
8993             (setcdr (nthcdr i info) nil)))))))
8994
8995 (defun gnus-add-marked-articles (group type articles &optional info force)
8996   ;; Add ARTICLES of TYPE to the info of GROUP.
8997   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8998   ;; add, but replace marked articles of TYPE with ARTICLES.
8999   (let ((info (or info (gnus-get-info group)))
9000         (uncompressed '(score bookmark killed))
9001         marked m)
9002     (or (not info)
9003         (and (not (setq marked (nthcdr 3 info)))
9004              (or (null articles)
9005                  (setcdr (nthcdr 2 info)
9006                          (list (list (cons type (gnus-compress-sequence
9007                                                  articles t)))))))
9008         (and (not (setq m (assq type (car marked))))
9009              (or (null articles)
9010                  (setcar marked
9011                          (cons (cons type (gnus-compress-sequence articles t) )
9012                                (car marked)))))
9013         (if force
9014             (if (null articles)
9015                 (setcar (nthcdr 3 info)
9016                         (delq (assq type (car marked)) (car marked)))
9017               (setcdr m (gnus-compress-sequence articles t)))
9018           (setcdr m (gnus-compress-sequence
9019                      (sort (nconc (gnus-uncompress-range (cdr m))
9020                                   (copy-sequence articles)) '<) t))))))
9021
9022 (defun gnus-set-mode-line (where)
9023   "This function sets the mode line of the article or summary buffers.
9024 If WHERE is `summary', the summary mode line format will be used."
9025   ;; Is this mode line one we keep updated?
9026   (when (memq where gnus-updated-mode-lines)
9027     (let (mode-string)
9028       (save-excursion
9029         ;; We evaluate this in the summary buffer since these
9030         ;; variables are buffer-local to that buffer.
9031         (set-buffer gnus-summary-buffer)
9032         ;; We bind all these variables that are used in the `eval' form
9033         ;; below.
9034         (let* ((mformat (symbol-value
9035                          (intern
9036                           (format "gnus-%s-mode-line-format-spec" where))))
9037                (gnus-tmp-group-name gnus-newsgroup-name)
9038                (gnus-tmp-article-number (or gnus-current-article 0))
9039                (gnus-tmp-unread gnus-newsgroup-unreads)
9040                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
9041                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
9042                (gnus-tmp-unread-and-unselected
9043                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
9044                             (zerop gnus-tmp-unselected)) "")
9045                       ((zerop gnus-tmp-unselected)
9046                        (format "{%d more}" gnus-tmp-unread-and-unticked))
9047                       (t (format "{%d(+%d) more}"
9048                                  gnus-tmp-unread-and-unticked
9049                                  gnus-tmp-unselected))))
9050                (gnus-tmp-subject
9051                 (if (and gnus-current-headers
9052                          (vectorp gnus-current-headers))
9053                     (gnus-mode-string-quote
9054                      (mail-header-subject gnus-current-headers)) ""))
9055                max-len
9056                gnus-tmp-header);; passed as argument to any user-format-funcs
9057           (setq mode-string (eval mformat))
9058           (setq max-len (max 4 (if gnus-mode-non-string-length
9059                                    (- (window-width)
9060                                       gnus-mode-non-string-length)
9061                                  (length mode-string))))
9062           ;; We might have to chop a bit of the string off...
9063           (when (> (length mode-string) max-len)
9064             (setq mode-string
9065                   (concat (gnus-truncate-string mode-string (- max-len 3))
9066                           "...")))
9067           ;; Pad the mode string a bit.
9068           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
9069       ;; Update the mode line.
9070       (setq mode-line-buffer-identification (list mode-string))
9071       (set-buffer-modified-p t))))
9072
9073 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
9074   "Go through the HEADERS list and add all Xrefs to a hash table.
9075 The resulting hash table is returned, or nil if no Xrefs were found."
9076   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
9077          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
9078          (xref-hashtb (make-vector 63 0))
9079          start group entry number xrefs header)
9080     (while headers
9081       (setq header (pop headers))
9082       (when (and (setq xrefs (mail-header-xref header))
9083                  (not (memq (setq number (mail-header-number header))
9084                             unreads)))
9085         (setq start 0)
9086         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
9087           (setq start (match-end 0))
9088           (setq group (if prefix
9089                           (concat prefix (substring xrefs (match-beginning 1)
9090                                                     (match-end 1)))
9091                         (substring xrefs (match-beginning 1) (match-end 1))))
9092           (setq number
9093                 (string-to-int (substring xrefs (match-beginning 2)
9094                                           (match-end 2))))
9095           (if (setq entry (gnus-gethash group xref-hashtb))
9096               (setcdr entry (cons number (cdr entry)))
9097             (gnus-sethash group (cons number nil) xref-hashtb)))))
9098     (and start xref-hashtb)))
9099
9100 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
9101   "Look through all the headers and mark the Xrefs as read."
9102   (let ((virtual (gnus-virtual-group-p from-newsgroup))
9103         name entry info xref-hashtb idlist method nth4)
9104     (save-excursion
9105       (set-buffer gnus-group-buffer)
9106       (when (setq xref-hashtb
9107                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
9108         (mapatoms
9109          (lambda (group)
9110            (unless (string= from-newsgroup (setq name (symbol-name group)))
9111              (setq idlist (symbol-value group))
9112              ;; Dead groups are not updated.
9113              (and (prog1
9114                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
9115                             info (nth 2 entry))
9116                     (if (stringp (setq nth4 (gnus-info-method info)))
9117                         (setq nth4 (gnus-server-to-method nth4))))
9118                   ;; Only do the xrefs if the group has the same
9119                   ;; select method as the group we have just read.
9120                   (or (gnus-methods-equal-p
9121                        nth4 (gnus-find-method-for-group from-newsgroup))
9122                       virtual
9123                       (equal nth4 (setq method (gnus-find-method-for-group
9124                                                 from-newsgroup)))
9125                       (and (equal (car nth4) (car method))
9126                            (equal (nth 1 nth4) (nth 1 method))))
9127                   gnus-use-cross-reference
9128                   (or (not (eq gnus-use-cross-reference t))
9129                       virtual
9130                       ;; Only do cross-references on subscribed
9131                       ;; groups, if that is what is wanted.
9132                       (<= (gnus-info-level info) gnus-level-subscribed))
9133                   (gnus-group-make-articles-read name idlist))))
9134          xref-hashtb)))))
9135
9136 (defun gnus-group-make-articles-read (group articles)
9137   (let* ((num 0)
9138          (entry (gnus-gethash group gnus-newsrc-hashtb))
9139          (info (nth 2 entry))
9140          (active (gnus-active group))
9141          range)
9142     ;; First peel off all illegal article numbers.
9143     (if active
9144         (let ((ids articles)
9145               id first)
9146           (while ids
9147             (setq id (car ids))
9148             (if (and first (> id (cdr active)))
9149                 (progn
9150                   ;; We'll end up in this situation in one particular
9151                   ;; obscure situation.  If you re-scan a group and get
9152                   ;; a new article that is cross-posted to a different
9153                   ;; group that has not been re-scanned, you might get
9154                   ;; crossposted article that has a higher number than
9155                   ;; Gnus believes possible.  So we re-activate this
9156                   ;; group as well.  This might mean doing the
9157                   ;; crossposting thingy will *increase* the number
9158                   ;; of articles in some groups.  Tsk, tsk.
9159                   (setq active (or (gnus-activate-group group) active))))
9160             (if (or (> id (cdr active))
9161                     (< id (car active)))
9162                 (setq articles (delq id articles)))
9163             (setq ids (cdr ids)))))
9164     ;; If the read list is nil, we init it.
9165     (and active
9166          (null (gnus-info-read info))
9167          (> (car active) 1)
9168          (gnus-info-set-read info (cons 1 (1- (car active)))))
9169     ;; Then we add the read articles to the range.
9170     (gnus-info-set-read
9171      info
9172      (setq range
9173            (gnus-add-to-range
9174             (gnus-info-read info) (setq articles (sort articles '<)))))
9175     ;; Then we have to re-compute how many unread
9176     ;; articles there are in this group.
9177     (if active
9178         (progn
9179           (cond
9180            ((not range)
9181             (setq num (- (1+ (cdr active)) (car active))))
9182            ((not (listp (cdr range)))
9183             (setq num (- (cdr active) (- (1+ (cdr range))
9184                                          (car range)))))
9185            (t
9186             (while range
9187               (if (numberp (car range))
9188                   (setq num (1+ num))
9189                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9190               (setq range (cdr range)))
9191             (setq num (- (cdr active) num))))
9192           ;; Update the number of unread articles.
9193           (setcar entry num)
9194           ;; Update the group buffer.
9195           (gnus-group-update-group group t)))))
9196
9197 (defun gnus-methods-equal-p (m1 m2)
9198   (let ((m1 (or m1 gnus-select-method))
9199         (m2 (or m2 gnus-select-method)))
9200     (or (equal m1 m2)
9201         (and (eq (car m1) (car m2))
9202              (or (not (memq 'address (assoc (symbol-name (car m1))
9203                                             gnus-valid-select-methods)))
9204                  (equal (nth 1 m1) (nth 1 m2)))))))
9205
9206 (defsubst gnus-header-value ()
9207   (buffer-substring 
9208    (match-end 0) 
9209    (if (re-search-forward "^[^ \t]" nil t)
9210        (progn
9211          (backward-char 2)
9212          (point))
9213      (gnus-point-at-eol))))
9214
9215 (defvar gnus-newsgroup-none-id 0)
9216
9217 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9218   (let ((cur nntp-server-buffer)
9219         (dependencies
9220          (or dependencies
9221              (save-excursion (set-buffer gnus-summary-buffer)
9222                              gnus-newsgroup-dependencies)))
9223         headers id id-dep ref-dep end ref)
9224     (save-excursion
9225       (set-buffer nntp-server-buffer)
9226       (run-hooks 'gnus-parse-headers-hook)
9227       (let ((case-fold-search t)
9228             in-reply-to header p lines)
9229         (goto-char (point-min))
9230         ;; Search to the beginning of the next header.  Error messages
9231         ;; do not begin with 2 or 3.
9232         (while (re-search-forward "^[23][0-9]+ " nil t)
9233           (setq id nil
9234                 ref nil)
9235           ;; This implementation of this function, with nine
9236           ;; search-forwards instead of the one re-search-forward and
9237           ;; a case (which basically was the old function) is actually
9238           ;; about twice as fast, even though it looks messier.  You
9239           ;; can't have everything, I guess.  Speed and elegance
9240           ;; doesn't always go hand in hand.
9241           (setq
9242            header
9243            (vector
9244             ;; Number.
9245             (prog1
9246                 (read cur)
9247               (end-of-line)
9248               (setq p (point))
9249               (narrow-to-region (point)
9250                                 (or (and (search-forward "\n.\n" nil t)
9251                                          (- (point) 2))
9252                                     (point))))
9253             ;; Subject.
9254             (progn
9255               (goto-char p)
9256               (if (search-forward "\nsubject: " nil t)
9257                   (gnus-header-value) "(none)"))
9258             ;; From.
9259             (progn
9260               (goto-char p)
9261               (if (search-forward "\nfrom: " nil t)
9262                   (gnus-header-value) "(nobody)"))
9263             ;; Date.
9264             (progn
9265               (goto-char p)
9266               (if (search-forward "\ndate: " nil t)
9267                   (gnus-header-value) ""))
9268             ;; Message-ID.
9269             (progn
9270               (goto-char p)
9271               (if (search-forward "\nmessage-id: " nil t)
9272                   (setq id (gnus-header-value))
9273                 ;; If there was no message-id, we just fake one to make
9274                 ;; subsequent routines simpler.
9275                 (setq id (concat "none+"
9276                                  (int-to-string
9277                                   (setq gnus-newsgroup-none-id
9278                                         (1+ gnus-newsgroup-none-id)))))))
9279             ;; References.
9280             (progn
9281               (goto-char p)
9282               (if (search-forward "\nreferences: " nil t)
9283                   (progn
9284                     (setq end (point))
9285                     (prog1
9286                         (gnus-header-value)
9287                       (setq ref
9288                             (buffer-substring
9289                              (progn
9290                                (end-of-line)
9291                                (search-backward ">" end t)
9292                                (1+ (point)))
9293                              (progn
9294                                (search-backward "<" end t)
9295                                (point))))))
9296                 ;; Get the references from the in-reply-to header if there
9297                 ;; were no references and the in-reply-to header looks
9298                 ;; promising.
9299                 (if (and (search-forward "\nin-reply-to: " nil t)
9300                          (setq in-reply-to (gnus-header-value))
9301                          (string-match "<[^>]+>" in-reply-to))
9302                     (setq ref (substring in-reply-to (match-beginning 0)
9303                                          (match-end 0)))
9304                   (setq ref ""))))
9305             ;; Chars.
9306             0
9307             ;; Lines.
9308             (progn
9309               (goto-char p)
9310               (if (search-forward "\nlines: " nil t)
9311                   (if (numberp (setq lines (read cur)))
9312                       lines 0)
9313                 0))
9314             ;; Xref.
9315             (progn
9316               (goto-char p)
9317               (and (search-forward "\nxref: " nil t)
9318                    (gnus-header-value)))))
9319           ;; We do the threading while we read the headers.  The
9320           ;; message-id and the last reference are both entered into
9321           ;; the same hash table.  Some tippy-toeing around has to be
9322           ;; done in case an article has arrived before the article
9323           ;; which it refers to.
9324           (if (boundp (setq id-dep (intern id dependencies)))
9325               (if (and (car (symbol-value id-dep))
9326                        (not force-new))
9327                   ;; An article with this Message-ID has already
9328                   ;; been seen, so we ignore this one, except we add
9329                   ;; any additional Xrefs (in case the two articles
9330                   ;; came from different servers).
9331                   (progn
9332                     (mail-header-set-xref
9333                      (car (symbol-value id-dep))
9334                      (concat (or (mail-header-xref
9335                                   (car (symbol-value id-dep))) "")
9336                              (or (mail-header-xref header) "")))
9337                     (setq header nil))
9338                 (setcar (symbol-value id-dep) header))
9339             (set id-dep (list header)))
9340           (when header
9341             (if (boundp (setq ref-dep (intern ref dependencies)))
9342                 (setcdr (symbol-value ref-dep)
9343                         (nconc (cdr (symbol-value ref-dep))
9344                                (list (symbol-value id-dep))))
9345               (set ref-dep (list nil (symbol-value id-dep))))
9346             (setq headers (cons header headers)))
9347           (goto-char (point-max))
9348           (widen))
9349         (nreverse headers)))))
9350
9351 ;; The following macros and functions were written by Felix Lee
9352 ;; <flee@cse.psu.edu>.
9353
9354 (defmacro gnus-nov-read-integer ()
9355   '(prog1
9356        (if (= (following-char) ?\t)
9357            0
9358          (let ((num (condition-case nil (read buffer) (error nil))))
9359            (if (numberp num) num 0)))
9360      (or (eobp) (forward-char 1))))
9361
9362 (defmacro gnus-nov-skip-field ()
9363   '(search-forward "\t" eol 'move))
9364
9365 (defmacro gnus-nov-field ()
9366   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9367
9368 ;; Goes through the xover lines and returns a list of vectors
9369 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9370                                                   force-new dependencies)
9371   "Parse the news overview data in the server buffer, and return a
9372 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9373   ;; Get the Xref when the users reads the articles since most/some
9374   ;; NNTP servers do not include Xrefs when using XOVER.
9375   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9376   (let ((cur nntp-server-buffer)
9377         (dependencies (or dependencies gnus-newsgroup-dependencies))
9378         number headers header)
9379     (save-excursion
9380       (set-buffer nntp-server-buffer)
9381       ;; Allow the user to mangle the headers before parsing them.
9382       (run-hooks 'gnus-parse-headers-hook)
9383       (goto-char (point-min))
9384       (while (and sequence (not (eobp)))
9385         (setq number (read cur))
9386         (while (and sequence (< (car sequence) number))
9387           (setq sequence (cdr sequence)))
9388         (and sequence
9389              (eq number (car sequence))
9390              (progn
9391                (setq sequence (cdr sequence))
9392                (if (setq header
9393                          (inline (gnus-nov-parse-line
9394                                   number dependencies force-new)))
9395                    (setq headers (cons header headers)))))
9396         (forward-line 1))
9397       (setq headers (nreverse headers)))
9398     headers))
9399
9400 ;; This function has to be called with point after the article number
9401 ;; on the beginning of the line.
9402 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9403   (let ((none 0)
9404         (eol (gnus-point-at-eol))
9405         (buffer (current-buffer))
9406         header ref id id-dep ref-dep)
9407
9408     ;; overview: [num subject from date id refs chars lines misc]
9409     (narrow-to-region (point) eol)
9410     (or (eobp) (forward-char))
9411
9412     (condition-case nil
9413         (setq header
9414               (vector
9415                number                   ; number
9416                (gnus-nov-field)         ; subject
9417                (gnus-nov-field)         ; from
9418                (gnus-nov-field)         ; date
9419                (setq id (or (gnus-nov-field)
9420                             (concat "none+"
9421                                     (int-to-string
9422                                      (setq none (1+ none)))))) ; id
9423                (progn
9424                  (save-excursion
9425                    (let ((beg (point)))
9426                      (search-forward "\t" eol)
9427                      (if (search-backward ">" beg t)
9428                          (setq ref
9429                                (buffer-substring
9430                                 (1+ (point))
9431                                 (search-backward "<" beg t)))
9432                        (setq ref nil))))
9433                  (gnus-nov-field))      ; refs
9434                (gnus-nov-read-integer)  ; chars
9435                (gnus-nov-read-integer)  ; lines
9436                (if (= (following-char) ?\n)
9437                    nil
9438                  (gnus-nov-field))      ; misc
9439                ))
9440       (error (progn
9441                (gnus-error 4 "Strange nov line")
9442                (setq header nil)
9443                (goto-char eol))))
9444
9445     (widen)
9446
9447     ;; We build the thread tree.
9448     (when header
9449       (if (boundp (setq id-dep (intern id dependencies)))
9450           (if (and (car (symbol-value id-dep))
9451                    (not force-new))
9452               ;; An article with this Message-ID has already been seen,
9453               ;; so we ignore this one, except we add any additional
9454               ;; Xrefs (in case the two articles came from different
9455               ;; servers.
9456               (progn
9457                 (mail-header-set-xref
9458                  (car (symbol-value id-dep))
9459                  (concat (or (mail-header-xref
9460                               (car (symbol-value id-dep))) "")
9461                          (or (mail-header-xref header) "")))
9462                 (setq header nil))
9463             (setcar (symbol-value id-dep) header))
9464         (set id-dep (list header))))
9465     (when header
9466       (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9467           (setcdr (symbol-value ref-dep)
9468                   (nconc (cdr (symbol-value ref-dep))
9469                          (list (symbol-value id-dep))))
9470         (set ref-dep (list nil (symbol-value id-dep)))))
9471     header))
9472
9473 (defun gnus-article-get-xrefs ()
9474   "Fill in the Xref value in `gnus-current-headers', if necessary.
9475 This is meant to be called in `gnus-article-internal-prepare-hook'."
9476   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9477                                  gnus-current-headers)))
9478     (or (not gnus-use-cross-reference)
9479         (not headers)
9480         (and (mail-header-xref headers)
9481              (not (string= (mail-header-xref headers) "")))
9482         (let ((case-fold-search t)
9483               xref)
9484           (save-restriction
9485             (nnheader-narrow-to-headers)
9486             (goto-char (point-min))
9487             (if (or (and (eq (downcase (following-char)) ?x)
9488                          (looking-at "Xref:"))
9489                     (search-forward "\nXref:" nil t))
9490                 (progn
9491                   (goto-char (1+ (match-end 0)))
9492                   (setq xref (buffer-substring (point)
9493                                                (progn (end-of-line) (point))))
9494                   (mail-header-set-xref headers xref))))))))
9495
9496 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
9497   "Find article ID and insert the summary line for that article."
9498   (let ((header (if (and old-header use-old-header)
9499                     old-header (gnus-read-header id)))
9500         (number (and (numberp id) id))
9501         pos)
9502     (when header
9503       ;; Rebuild the thread that this article is part of and go to the
9504       ;; article we have fetched.
9505       (when (and (not gnus-show-threads)
9506                  old-header)
9507         (when (setq pos (text-property-any
9508                          (point-min) (point-max) 'gnus-number 
9509                          (mail-header-number old-header)))
9510           (goto-char pos)
9511           (gnus-delete-line)
9512           (gnus-data-remove (mail-header-number old-header))))
9513       (when old-header
9514         (mail-header-set-number header (mail-header-number old-header)))
9515       (setq gnus-newsgroup-sparse
9516             (delq (setq number (mail-header-number header)) 
9517                   gnus-newsgroup-sparse))
9518       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
9519       (gnus-rebuild-thread (mail-header-id header))
9520       (gnus-summary-goto-subject number nil t))
9521     (when (and (numberp number)
9522                (> number 0))
9523       ;; We have to update the boundaries even if we can't fetch the
9524       ;; article if ID is a number -- so that the next `P' or `N'
9525       ;; command will fetch the previous (or next) article even
9526       ;; if the one we tried to fetch this time has been canceled.
9527       (and (> number gnus-newsgroup-end)
9528            (setq gnus-newsgroup-end number))
9529       (and (< number gnus-newsgroup-begin)
9530            (setq gnus-newsgroup-begin number))
9531       (setq gnus-newsgroup-unselected
9532             (delq number gnus-newsgroup-unselected)))
9533     ;; Report back a success?
9534     (and header (mail-header-number header))))
9535
9536 (defun gnus-summary-work-articles (n)
9537   "Return a list of articles to be worked upon.  The prefix argument,
9538 the list of process marked articles, and the current article will be
9539 taken into consideration."
9540   (cond
9541    ((and n (numberp n))
9542     ;; A numerical prefix has been given.
9543     (let ((backward (< n 0))
9544           (n (abs n))
9545           articles article)
9546       (save-excursion
9547         (while
9548             (and (> n 0)
9549                  (push (setq article (gnus-summary-article-number))
9550                        articles)
9551                  (if backward
9552                      (gnus-summary-find-prev nil article)
9553                    (gnus-summary-find-next nil article)))
9554           (decf n)))
9555       (nreverse articles)))
9556    ((and (boundp 'transient-mark-mode)
9557          transient-mark-mode
9558          mark-active)
9559     ;; Work on the region between point and mark.
9560     (let ((max (max (point) (mark)))
9561           articles article)
9562       (save-excursion
9563         (goto-char (min (point) (mark)))
9564         (while
9565             (and
9566              (push (setq article (gnus-summary-article-number)) articles)
9567              (gnus-summary-find-next nil article)
9568              (< (point) max)))
9569         (nreverse articles))))
9570    (gnus-newsgroup-processable
9571     ;; There are process-marked articles present.
9572     (reverse gnus-newsgroup-processable))
9573    (t
9574     ;; Just return the current article.
9575     (list (gnus-summary-article-number)))))
9576
9577 (defun gnus-summary-search-group (&optional backward use-level)
9578   "Search for next unread newsgroup.
9579 If optional argument BACKWARD is non-nil, search backward instead."
9580   (save-excursion
9581     (set-buffer gnus-group-buffer)
9582     (if (gnus-group-search-forward
9583          backward nil (if use-level (gnus-group-group-level) nil))
9584         (gnus-group-group-name))))
9585
9586 (defun gnus-summary-best-group (&optional exclude-group)
9587   "Find the name of the best unread group.
9588 If EXCLUDE-GROUP, do not go to this group."
9589   (save-excursion
9590     (set-buffer gnus-group-buffer)
9591     (save-excursion
9592       (gnus-group-best-unread-group exclude-group))))
9593
9594 (defun gnus-summary-find-next (&optional unread article backward)
9595   (if backward (gnus-summary-find-prev)
9596     (let* ((dummy (gnus-summary-article-intangible-p))
9597            (article (or article (gnus-summary-article-number)))
9598            (arts (gnus-data-find-list article))
9599            result)
9600       (when (and (not dummy)
9601                  (or (not gnus-summary-check-current)
9602                      (not unread)
9603                      (not (gnus-data-unread-p (car arts)))))
9604         (setq arts (cdr arts)))
9605       (when (setq result
9606                   (if unread
9607                       (progn
9608                         (while arts
9609                           (when (gnus-data-unread-p (car arts))
9610                             (setq result (car arts)
9611                                   arts nil))
9612                           (setq arts (cdr arts)))
9613                         result)
9614                     (car arts)))
9615         (goto-char (gnus-data-pos result))
9616         (gnus-data-number result)))))
9617
9618 (defun gnus-summary-find-prev (&optional unread article)
9619   (let* ((eobp (eobp))
9620          (article (or article (gnus-summary-article-number)))
9621          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9622          result)
9623     (when (and (not eobp)
9624                (or (not gnus-summary-check-current)
9625                    (not unread)
9626                    (not (gnus-data-unread-p (car arts)))))
9627       (setq arts (cdr arts)))
9628     (if (setq result
9629               (if unread
9630                   (progn
9631                     (while arts
9632                       (and (gnus-data-unread-p (car arts))
9633                            (setq result (car arts)
9634                                  arts nil))
9635                       (setq arts (cdr arts)))
9636                     result)
9637                 (car arts)))
9638         (progn
9639           (goto-char (gnus-data-pos result))
9640           (gnus-data-number result)))))
9641
9642 (defun gnus-summary-find-subject (subject &optional unread backward article)
9643   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9644          (article (or article (gnus-summary-article-number)))
9645          (articles (gnus-data-list backward))
9646          (arts (gnus-data-find-list article articles))
9647          result)
9648     (when (or (not gnus-summary-check-current)
9649               (not unread)
9650               (not (gnus-data-unread-p (car arts))))
9651       (setq arts (cdr arts)))
9652     (while arts
9653       (and (or (not unread)
9654                (gnus-data-unread-p (car arts)))
9655            (vectorp (gnus-data-header (car arts)))
9656            (gnus-subject-equal
9657             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9658            (setq result (car arts)
9659                  arts nil))
9660       (setq arts (cdr arts)))
9661     (and result
9662          (goto-char (gnus-data-pos result))
9663          (gnus-data-number result))))
9664
9665 (defun gnus-summary-search-forward (&optional unread subject backward)
9666   "Search forward for an article.
9667 If UNREAD, look for unread articles.  If SUBJECT, look for
9668 articles with that subject.  If BACKWARD, search backward instead."
9669   (cond (subject (gnus-summary-find-subject subject unread backward))
9670         (backward (gnus-summary-find-prev unread))
9671         (t (gnus-summary-find-next unread))))
9672
9673 (defun gnus-recenter (&optional n)
9674   "Center point in window and redisplay frame.
9675 Also do horizontal recentering."
9676   (interactive "P")
9677   (when (and gnus-auto-center-summary
9678              (not (eq gnus-auto-center-summary 'vertical)))
9679     (gnus-horizontal-recenter))
9680   (recenter n))
9681
9682 (defun gnus-summary-recenter ()
9683   "Center point in the summary window.
9684 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9685 displayed, no centering will be performed."
9686   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9687   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9688   (let* ((top (cond ((< (window-height) 4) 0)
9689                     ((< (window-height) 7) 1)
9690                     (t 2)))
9691          (height (1- (window-height)))
9692          (bottom (save-excursion (goto-char (point-max))
9693                                  (forward-line (- height))
9694                                  (point)))
9695          (window (get-buffer-window (current-buffer))))
9696     ;; The user has to want it.
9697     (when gnus-auto-center-summary
9698       (when (get-buffer-window gnus-article-buffer)
9699        ;; Only do recentering when the article buffer is displayed,
9700        ;; Set the window start to either `bottom', which is the biggest
9701        ;; possible valid number, or the second line from the top,
9702        ;; whichever is the least.
9703        (set-window-start
9704         window (min bottom (save-excursion 
9705                              (forward-line (- top)) (point)))))
9706       ;; Do horizontal recentering while we're at it.
9707       (when (and (get-buffer-window (current-buffer) t)
9708                  (not (eq gnus-auto-center-summary 'vertical)))
9709         (let ((selected (selected-window)))
9710           (select-window (get-buffer-window (current-buffer) t))
9711           (gnus-summary-position-point)
9712           (gnus-horizontal-recenter)
9713           (select-window selected))))))
9714
9715 (defun gnus-horizontal-recenter ()
9716   "Recenter the current buffer horizontally."
9717   (if (< (current-column) (/ (window-width) 2))
9718       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9719     (let* ((orig (point))
9720            (end (window-end (get-buffer-window (current-buffer) t)))
9721            (max 0))
9722       ;; Find the longest line currently displayed in the window.
9723       (goto-char (window-start))
9724       (while (and (not (eobp)) 
9725                   (< (point) end))
9726         (end-of-line)
9727         (setq max (max max (current-column)))
9728         (forward-line 1))
9729       (goto-char orig)
9730       ;; Scroll horizontally to center (sort of) the point.
9731       (if (> max (window-width))
9732           (set-window-hscroll 
9733            (get-buffer-window (current-buffer) t)
9734            (min (- (current-column) (/ (window-width) 3))
9735                 (+ 2 (- max (window-width)))))
9736         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9737       max)))
9738
9739 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9740 (defun gnus-short-group-name (group &optional levels)
9741   "Collapse GROUP name LEVELS."
9742   (let* ((name "") 
9743          (foreign "")
9744          (depth 0) 
9745          (skip 1)
9746          (levels (or levels
9747                      (progn
9748                        (while (string-match "\\." group skip)
9749                          (setq skip (match-end 0)
9750                                depth (+ depth 1)))
9751                        depth))))
9752     (if (string-match ":" group)
9753         (setq foreign (substring group 0 (match-end 0))
9754               group (substring group (match-end 0))))
9755     (while group
9756       (if (and (string-match "\\." group)
9757                (> levels (- gnus-group-uncollapsed-levels 1)))
9758           (setq name (concat name (substring group 0 1))
9759                 group (substring group (match-end 0))
9760                 levels (- levels 1)
9761                 name (concat name "."))
9762         (setq name (concat foreign name group)
9763               group nil)))
9764     name))
9765
9766 (defun gnus-summary-jump-to-group (newsgroup)
9767   "Move point to NEWSGROUP in group mode buffer."
9768   ;; Keep update point of group mode buffer if visible.
9769   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9770       (save-window-excursion
9771         ;; Take care of tree window mode.
9772         (if (get-buffer-window gnus-group-buffer)
9773             (pop-to-buffer gnus-group-buffer))
9774         (gnus-group-jump-to-group newsgroup))
9775     (save-excursion
9776       ;; Take care of tree window mode.
9777       (if (get-buffer-window gnus-group-buffer)
9778           (pop-to-buffer gnus-group-buffer)
9779         (set-buffer gnus-group-buffer))
9780       (gnus-group-jump-to-group newsgroup))))
9781
9782 ;; This function returns a list of article numbers based on the
9783 ;; difference between the ranges of read articles in this group and
9784 ;; the range of active articles.
9785 (defun gnus-list-of-unread-articles (group)
9786   (let* ((read (gnus-info-read (gnus-get-info group)))
9787          (active (gnus-active group))
9788          (last (cdr active))
9789          first nlast unread)
9790     ;; If none are read, then all are unread.
9791     (if (not read)
9792         (setq first (car active))
9793       ;; If the range of read articles is a single range, then the
9794       ;; first unread article is the article after the last read
9795       ;; article.  Sounds logical, doesn't it?
9796       (if (not (listp (cdr read)))
9797           (setq first (1+ (cdr read)))
9798         ;; `read' is a list of ranges.
9799         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9800                                 (caar read))) 1)
9801             (setq first 1))
9802         (while read
9803           (if first
9804               (while (< first nlast)
9805                 (setq unread (cons first unread))
9806                 (setq first (1+ first))))
9807           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9808           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9809           (setq read (cdr read)))))
9810     ;; And add the last unread articles.
9811     (while (<= first last)
9812       (setq unread (cons first unread))
9813       (setq first (1+ first)))
9814     ;; Return the list of unread articles.
9815     (nreverse unread)))
9816
9817 (defun gnus-list-of-read-articles (group)
9818   "Return a list of unread, unticked and non-dormant articles."
9819   (let* ((info (gnus-get-info group))
9820          (marked (gnus-info-marks info))
9821          (active (gnus-active group)))
9822     (and info active
9823          (gnus-set-difference
9824           (gnus-sorted-complement
9825            (gnus-uncompress-range active)
9826            (gnus-list-of-unread-articles group))
9827           (append
9828            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9829            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9830
9831 ;; Various summary commands
9832
9833 (defun gnus-summary-universal-argument (arg)
9834   "Perform any operation on all articles that are process/prefixed."
9835   (interactive "P")
9836   (gnus-set-global-variables)
9837   (let ((articles (gnus-summary-work-articles arg))
9838         func article)
9839     (if (eq
9840          (setq
9841           func
9842           (key-binding
9843            (read-key-sequence
9844             (substitute-command-keys
9845              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9846              ))))
9847          'undefined)
9848         (gnus-error 1 "Undefined key")
9849       (save-excursion
9850         (while articles
9851           (gnus-summary-goto-subject (setq article (pop articles)))
9852           (command-execute func)
9853           (gnus-summary-remove-process-mark article)))))
9854   (gnus-summary-position-point))
9855
9856 (defun gnus-summary-toggle-truncation (&optional arg)
9857   "Toggle truncation of summary lines.
9858 With arg, turn line truncation on iff arg is positive."
9859   (interactive "P")
9860   (setq truncate-lines
9861         (if (null arg) (not truncate-lines)
9862           (> (prefix-numeric-value arg) 0)))
9863   (redraw-display))
9864
9865 (defun gnus-summary-reselect-current-group (&optional all rescan)
9866   "Exit and then reselect the current newsgroup.
9867 The prefix argument ALL means to select all articles."
9868   (interactive "P")
9869   (gnus-set-global-variables)
9870   (let ((current-subject (gnus-summary-article-number))
9871         (group gnus-newsgroup-name))
9872     (setq gnus-newsgroup-begin nil)
9873     (gnus-summary-exit)
9874     ;; We have to adjust the point of group mode buffer because the
9875     ;; current point was moved to the next unread newsgroup by
9876     ;; exiting.
9877     (gnus-summary-jump-to-group group)
9878     (when rescan
9879       (save-excursion
9880         (gnus-group-get-new-news-this-group 1)))
9881     (gnus-group-read-group all t)
9882     (gnus-summary-goto-subject current-subject)))
9883
9884 (defun gnus-summary-rescan-group (&optional all)
9885   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9886   (interactive "P")
9887   (gnus-summary-reselect-current-group all t))
9888
9889 (defun gnus-summary-update-info ()
9890   (let* ((group gnus-newsgroup-name))
9891     (when gnus-newsgroup-kill-headers
9892       (setq gnus-newsgroup-killed
9893             (gnus-compress-sequence
9894              (nconc
9895               (gnus-set-sorted-intersection
9896                (gnus-uncompress-range gnus-newsgroup-killed)
9897                (setq gnus-newsgroup-unselected
9898                      (sort gnus-newsgroup-unselected '<)))
9899               (setq gnus-newsgroup-unreads
9900                     (sort gnus-newsgroup-unreads '<))) t)))
9901     (unless (listp (cdr gnus-newsgroup-killed))
9902       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9903     (let ((headers gnus-newsgroup-headers))
9904       (run-hooks 'gnus-exit-group-hook)
9905       (unless gnus-save-score
9906         (setq gnus-newsgroup-scored nil))
9907       ;; Set the new ranges of read articles.
9908       (gnus-update-read-articles
9909        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9910       ;; Set the current article marks.
9911       (gnus-update-marks)
9912       ;; Do the cross-ref thing.
9913       (when gnus-use-cross-reference
9914         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9915       ;; Do adaptive scoring, and possibly save score files.
9916       (when gnus-newsgroup-adaptive
9917         (gnus-score-adaptive))
9918       (when gnus-use-scoring
9919         (gnus-score-save))
9920       ;; Do not switch windows but change the buffer to work.
9921       (set-buffer gnus-group-buffer)
9922       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9923           (gnus-group-update-group group)))))
9924
9925 (defun gnus-summary-exit (&optional temporary)
9926   "Exit reading current newsgroup, and then return to group selection mode.
9927 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9928   (interactive)
9929   (gnus-set-global-variables)
9930   (gnus-kill-save-kill-buffer)
9931   (let* ((group gnus-newsgroup-name)
9932          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9933          (mode major-mode)
9934          (buf (current-buffer)))
9935     (run-hooks 'gnus-summary-prepare-exit-hook)
9936     ;; If we have several article buffers, we kill them at exit.
9937     (unless gnus-single-article-buffer
9938       (gnus-kill-buffer gnus-original-article-buffer)
9939       (setq gnus-article-current nil))
9940     (when gnus-use-cache
9941       (gnus-cache-possibly-remove-articles)
9942       (gnus-cache-save-buffers))
9943     (when gnus-use-trees
9944       (gnus-tree-close group))
9945     ;; Make all changes in this group permanent.
9946     (unless quit-config
9947       (gnus-summary-update-info))
9948     (gnus-close-group group)
9949     ;; Make sure where I was, and go to next newsgroup.
9950     (set-buffer gnus-group-buffer)
9951     (unless quit-config
9952       (gnus-group-jump-to-group group))
9953     (run-hooks 'gnus-summary-exit-hook)
9954     (unless quit-config
9955       (gnus-group-next-unread-group 1))
9956     (if temporary
9957         nil                             ;Nothing to do.
9958       ;; If we have several article buffers, we kill them at exit.
9959       (unless gnus-single-article-buffer
9960         (gnus-kill-buffer gnus-article-buffer)
9961         (gnus-kill-buffer gnus-original-article-buffer)
9962         (setq gnus-article-current nil))
9963       (set-buffer buf)
9964       (if (not gnus-kill-summary-on-exit)
9965           (gnus-deaden-summary)
9966         ;; We set all buffer-local variables to nil.  It is unclear why
9967         ;; this is needed, but if we don't, buffer-local variables are
9968         ;; not garbage-collected, it seems.  This would the lead to en
9969         ;; ever-growing Emacs.
9970         (gnus-summary-clear-local-variables)
9971         (when (get-buffer gnus-article-buffer)
9972           (bury-buffer gnus-article-buffer))
9973         ;; We clear the global counterparts of the buffer-local
9974         ;; variables as well, just to be on the safe side.
9975         (gnus-configure-windows 'group 'force)
9976         (gnus-summary-clear-local-variables)
9977         ;; Return to group mode buffer.
9978         (if (eq mode 'gnus-summary-mode)
9979             (gnus-kill-buffer buf)))
9980       (setq gnus-current-select-method gnus-select-method)
9981       (pop-to-buffer gnus-group-buffer)
9982       ;; Clear the current group name.
9983       (if (not quit-config)
9984           (progn
9985             (gnus-group-jump-to-group group)
9986             (gnus-group-next-unread-group 1)
9987             (gnus-configure-windows 'group 'force))
9988         (if (not (buffer-name (car quit-config)))
9989             (gnus-configure-windows 'group 'force)
9990           (set-buffer (car quit-config))
9991           (and (eq major-mode 'gnus-summary-mode)
9992                (gnus-set-global-variables))
9993           (gnus-configure-windows (cdr quit-config))))
9994       (unless quit-config
9995         (setq gnus-newsgroup-name nil)))))
9996
9997 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9998 (defun gnus-summary-exit-no-update (&optional no-questions)
9999   "Quit reading current newsgroup without updating read article info."
10000   (interactive)
10001   (gnus-set-global-variables)
10002   (let* ((group gnus-newsgroup-name)
10003          (quit-config (gnus-group-quit-config group)))
10004     (when (or no-questions
10005               gnus-expert-user
10006               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
10007       ;; If we have several article buffers, we kill them at exit.
10008       (unless gnus-single-article-buffer
10009         (gnus-kill-buffer gnus-article-buffer)
10010         (gnus-kill-buffer gnus-original-article-buffer)
10011         (setq gnus-article-current nil))
10012       (if (not gnus-kill-summary-on-exit)
10013           (gnus-deaden-summary)
10014         (gnus-close-group group)
10015         (gnus-summary-clear-local-variables)
10016         (set-buffer gnus-group-buffer)
10017         (gnus-summary-clear-local-variables)
10018         (when (get-buffer gnus-summary-buffer)
10019           (kill-buffer gnus-summary-buffer)))
10020       (unless gnus-single-article-buffer
10021         (setq gnus-article-current nil))
10022       (when gnus-use-trees
10023         (gnus-tree-close group))
10024       (when (get-buffer gnus-article-buffer)
10025         (bury-buffer gnus-article-buffer))
10026       ;; Return to the group buffer.
10027       (gnus-configure-windows 'group 'force)
10028       ;; Clear the current group name.
10029       (setq gnus-newsgroup-name nil)
10030       (when (equal (gnus-group-group-name) group)
10031         (gnus-group-next-unread-group 1))
10032       (when quit-config
10033         (if (not (buffer-name (car quit-config)))
10034             (gnus-configure-windows 'group 'force)
10035           (set-buffer (car quit-config))
10036           (when (eq major-mode 'gnus-summary-mode)
10037             (gnus-set-global-variables))
10038           (gnus-configure-windows (cdr quit-config)))))))
10039
10040 ;;; Dead summaries.
10041
10042 (defvar gnus-dead-summary-mode-map nil)
10043
10044 (if gnus-dead-summary-mode-map
10045     nil
10046   (setq gnus-dead-summary-mode-map (make-keymap))
10047   (suppress-keymap gnus-dead-summary-mode-map)
10048   (substitute-key-definition
10049    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
10050   (let ((keys '("\C-d" "\r" "\177")))
10051     (while keys
10052       (define-key gnus-dead-summary-mode-map
10053         (pop keys) 'gnus-summary-wake-up-the-dead))))
10054
10055 (defvar gnus-dead-summary-mode nil
10056   "Minor mode for Gnus summary buffers.")
10057
10058 (defun gnus-dead-summary-mode (&optional arg)
10059   "Minor mode for Gnus summary buffers."
10060   (interactive "P")
10061   (when (eq major-mode 'gnus-summary-mode)
10062     (make-local-variable 'gnus-dead-summary-mode)
10063     (setq gnus-dead-summary-mode
10064           (if (null arg) (not gnus-dead-summary-mode)
10065             (> (prefix-numeric-value arg) 0)))
10066     (when gnus-dead-summary-mode
10067       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
10068         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
10069       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
10070         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
10071               minor-mode-map-alist)))))
10072
10073 (defun gnus-deaden-summary ()
10074   "Make the current summary buffer into a dead summary buffer."
10075   ;; Kill any previous dead summary buffer.
10076   (when (and gnus-dead-summary
10077              (buffer-name gnus-dead-summary))
10078     (save-excursion
10079       (set-buffer gnus-dead-summary)
10080       (when gnus-dead-summary-mode
10081         (kill-buffer (current-buffer)))))
10082   ;; Make this the current dead summary.
10083   (setq gnus-dead-summary (current-buffer))
10084   (gnus-dead-summary-mode 1)
10085   (let ((name (buffer-name)))
10086     (when (string-match "Summary" name)
10087       (rename-buffer
10088        (concat (substring name 0 (match-beginning 0)) "Dead "
10089                (substring name (match-beginning 0))) t))))
10090
10091 (defun gnus-kill-or-deaden-summary (buffer)
10092   "Kill or deaden the summary BUFFER."
10093   (when (and (buffer-name buffer)
10094              (not gnus-single-article-buffer))
10095     (save-excursion
10096       (set-buffer buffer)
10097       (gnus-kill-buffer gnus-article-buffer)
10098       (gnus-kill-buffer gnus-original-article-buffer)))
10099   (cond (gnus-kill-summary-on-exit
10100          (when (and gnus-use-trees
10101                     (and (get-buffer buffer)
10102                          (buffer-name (get-buffer buffer))))
10103            (save-excursion
10104              (set-buffer (get-buffer buffer))
10105              (gnus-tree-close gnus-newsgroup-name)))
10106          (gnus-kill-buffer buffer))
10107         ((and (get-buffer buffer)
10108               (buffer-name (get-buffer buffer)))
10109          (save-excursion
10110            (set-buffer buffer)
10111            (gnus-deaden-summary)))))
10112
10113 (defun gnus-summary-wake-up-the-dead (&rest args)
10114   "Wake up the dead summary buffer."
10115   (interactive)
10116   (gnus-dead-summary-mode -1)
10117   (let ((name (buffer-name)))
10118     (when (string-match "Dead " name)
10119       (rename-buffer
10120        (concat (substring name 0 (match-beginning 0))
10121                (substring name (match-end 0))) t)))
10122   (gnus-message 3 "This dead summary is now alive again"))
10123
10124 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
10125 (defun gnus-summary-fetch-faq (&optional faq-dir)
10126   "Fetch the FAQ for the current group.
10127 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
10128 in."
10129   (interactive
10130    (list
10131     (if current-prefix-arg
10132         (completing-read
10133          "Faq dir: " (and (listp gnus-group-faq-directory)
10134                           gnus-group-faq-directory)))))
10135   (let (gnus-faq-buffer)
10136     (and (setq gnus-faq-buffer
10137                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
10138          (gnus-configure-windows 'summary-faq))))
10139
10140 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10141 (defun gnus-summary-describe-group (&optional force)
10142   "Describe the current newsgroup."
10143   (interactive "P")
10144   (gnus-group-describe-group force gnus-newsgroup-name))
10145
10146 (defun gnus-summary-describe-briefly ()
10147   "Describe summary mode commands briefly."
10148   (interactive)
10149   (gnus-message 6
10150                 (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")))
10151
10152 ;; Walking around group mode buffer from summary mode.
10153
10154 (defun gnus-summary-next-group (&optional no-article target-group backward)
10155   "Exit current newsgroup and then select next unread newsgroup.
10156 If prefix argument NO-ARTICLE is non-nil, no article is selected
10157 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
10158 previous group instead."
10159   (interactive "P")
10160   (gnus-set-global-variables)
10161   (let ((current-group gnus-newsgroup-name)
10162         (current-buffer (current-buffer))
10163         entered)
10164     ;; First we semi-exit this group to update Xrefs and all variables.
10165     ;; We can't do a real exit, because the window conf must remain
10166     ;; the same in case the user is prompted for info, and we don't
10167     ;; want the window conf to change before that...
10168     (gnus-summary-exit t)
10169     (while (not entered)
10170       ;; Then we find what group we are supposed to enter.
10171       (set-buffer gnus-group-buffer)
10172       (gnus-group-jump-to-group current-group)
10173       (setq target-group
10174             (or target-group
10175                 (if (eq gnus-keep-same-level 'best)
10176                     (gnus-summary-best-group gnus-newsgroup-name)
10177                   (gnus-summary-search-group backward gnus-keep-same-level))))
10178       (if (not target-group)
10179           ;; There are no further groups, so we return to the group
10180           ;; buffer.
10181           (progn
10182             (gnus-message 5 "Returning to the group buffer")
10183             (setq entered t)
10184             (set-buffer current-buffer)
10185             (gnus-summary-exit))
10186         ;; We try to enter the target group.
10187         (gnus-group-jump-to-group target-group)
10188         (let ((unreads (gnus-group-group-unread)))
10189           (if (and (or (eq t unreads)
10190                        (and unreads (not (zerop unreads))))
10191                    (gnus-summary-read-group
10192                     target-group nil no-article current-buffer))
10193               (setq entered t)
10194             (setq current-group target-group
10195                   target-group nil)))))))
10196
10197 (defun gnus-summary-prev-group (&optional no-article)
10198   "Exit current newsgroup and then select previous unread newsgroup.
10199 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10200   (interactive "P")
10201   (gnus-summary-next-group no-article nil t))
10202
10203 ;; Walking around summary lines.
10204
10205 (defun gnus-summary-first-subject (&optional unread)
10206   "Go to the first unread subject.
10207 If UNREAD is non-nil, go to the first unread article.
10208 Returns the article selected or nil if there are no unread articles."
10209   (interactive "P")
10210   (prog1
10211       (cond
10212        ;; Empty summary.
10213        ((null gnus-newsgroup-data)
10214         (gnus-message 3 "No articles in the group")
10215         nil)
10216        ;; Pick the first article.
10217        ((not unread)
10218         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10219         (gnus-data-number (car gnus-newsgroup-data)))
10220        ;; No unread articles.
10221        ((null gnus-newsgroup-unreads)
10222         (gnus-message 3 "No more unread articles")
10223         nil)
10224        ;; Find the first unread article.
10225        (t
10226         (let ((data gnus-newsgroup-data))
10227           (while (and data
10228                       (not (gnus-data-unread-p (car data))))
10229             (setq data (cdr data)))
10230           (if data
10231               (progn
10232                 (goto-char (gnus-data-pos (car data)))
10233                 (gnus-data-number (car data)))))))
10234     (gnus-summary-position-point)))
10235
10236 (defun gnus-summary-next-subject (n &optional unread dont-display)
10237   "Go to next N'th summary line.
10238 If N is negative, go to the previous N'th subject line.
10239 If UNREAD is non-nil, only unread articles are selected.
10240 The difference between N and the actual number of steps taken is
10241 returned."
10242   (interactive "p")
10243   (let ((backward (< n 0))
10244         (n (abs n)))
10245     (while (and (> n 0)
10246                 (if backward
10247                     (gnus-summary-find-prev unread)
10248                   (gnus-summary-find-next unread)))
10249       (setq n (1- n)))
10250     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10251                                (if unread " unread" "")))
10252     (unless dont-display
10253       (gnus-summary-recenter)
10254       (gnus-summary-position-point))
10255     n))
10256
10257 (defun gnus-summary-next-unread-subject (n)
10258   "Go to next N'th unread summary line."
10259   (interactive "p")
10260   (gnus-summary-next-subject n t))
10261
10262 (defun gnus-summary-prev-subject (n &optional unread)
10263   "Go to previous N'th summary line.
10264 If optional argument UNREAD is non-nil, only unread article is selected."
10265   (interactive "p")
10266   (gnus-summary-next-subject (- n) unread))
10267
10268 (defun gnus-summary-prev-unread-subject (n)
10269   "Go to previous N'th unread summary line."
10270   (interactive "p")
10271   (gnus-summary-next-subject (- n) t))
10272
10273 (defun gnus-summary-goto-subject (article &optional force silent)
10274   "Go the subject line of ARTICLE.
10275 If FORCE, also allow jumping to articles not currently shown."
10276   (let ((b (point))
10277         (data (gnus-data-find article)))
10278     ;; We read in the article if we have to.
10279     (and (not data)
10280          force
10281          (gnus-summary-insert-subject article (and (vectorp force) force) t)
10282          (setq data (gnus-data-find article)))
10283     (goto-char b)
10284     (if (not data)
10285         (progn
10286           (unless silent
10287             (gnus-message 3 "Can't find article %d" article))
10288           nil)
10289       (goto-char (gnus-data-pos data))
10290       article)))
10291
10292 ;; Walking around summary lines with displaying articles.
10293
10294 (defun gnus-summary-expand-window (&optional arg)
10295   "Make the summary buffer take up the entire Emacs frame.
10296 Given a prefix, will force an `article' buffer configuration."
10297   (interactive "P")
10298   (gnus-set-global-variables)
10299   (if arg
10300       (gnus-configure-windows 'article 'force)
10301     (gnus-configure-windows 'summary 'force)))
10302
10303 (defun gnus-summary-display-article (article &optional all-header)
10304   "Display ARTICLE in article buffer."
10305   (gnus-set-global-variables)
10306   (if (null article)
10307       nil
10308     (prog1
10309         (if gnus-summary-display-article-function
10310             (funcall gnus-summary-display-article-function article all-header)
10311           (gnus-article-prepare article all-header))
10312       (run-hooks 'gnus-select-article-hook)
10313       (unless (zerop gnus-current-article)
10314         (gnus-summary-goto-subject gnus-current-article))
10315       (gnus-summary-recenter)
10316       (when gnus-use-trees
10317         (gnus-possibly-generate-tree article)
10318         (gnus-highlight-selected-tree article))
10319       ;; Successfully display article.
10320       (gnus-article-set-window-start
10321        (cdr (assq article gnus-newsgroup-bookmarks))))))
10322
10323 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10324   "Select the current article.
10325 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10326 non-nil, the article will be re-fetched even if it already present in
10327 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10328 be displayed."
10329   ;; Make sure we are in the summary buffer to work around bbdb bug.
10330   (unless (eq major-mode 'gnus-summary-mode)
10331     (set-buffer gnus-summary-buffer))
10332   (let ((article (or article (gnus-summary-article-number)))
10333         (all-headers (not (not all-headers))) ;Must be T or NIL.
10334         gnus-summary-display-article-function
10335         did)
10336     (and (not pseudo)
10337          (gnus-summary-article-pseudo-p article)
10338          (error "This is a pseudo-article."))
10339     (prog1
10340         (save-excursion
10341           (set-buffer gnus-summary-buffer)
10342           (if (or (and gnus-single-article-buffer
10343                        (or (null gnus-current-article)
10344                            (null gnus-article-current)
10345                            (null (get-buffer gnus-article-buffer))
10346                            (not (eq article (cdr gnus-article-current)))
10347                            (not (equal (car gnus-article-current)
10348                                        gnus-newsgroup-name))))
10349                   (and (not gnus-single-article-buffer)
10350                        (or (null gnus-current-article)
10351                            (not (eq gnus-current-article article))))
10352                   force)
10353               ;; The requested article is different from the current article.
10354               (prog1
10355                   (gnus-summary-display-article article all-headers)
10356                 (setq did article))
10357             (if (or all-headers gnus-show-all-headers)
10358                 (gnus-article-show-all-headers))
10359             'old))
10360       (if did
10361           (gnus-article-set-window-start
10362            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10363
10364 (defun gnus-summary-set-current-mark (&optional current-mark)
10365   "Obsolete function."
10366   nil)
10367
10368 (defun gnus-summary-next-article (&optional unread subject backward push)
10369   "Select the next article.
10370 If UNREAD, only unread articles are selected.
10371 If SUBJECT, only articles with SUBJECT are selected.
10372 If BACKWARD, the previous article is selected instead of the next."
10373   (interactive "P")
10374   (gnus-set-global-variables)
10375   (cond
10376    ;; Is there such an article?
10377    ((and (gnus-summary-search-forward unread subject backward)
10378          (or (gnus-summary-display-article (gnus-summary-article-number))
10379              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10380     (gnus-summary-position-point))
10381    ;; If not, we try the first unread, if that is wanted.
10382    ((and subject
10383          gnus-auto-select-same
10384          (or (gnus-summary-first-unread-article)
10385              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10386     (gnus-summary-position-point)
10387     (gnus-message 6 "Wrapped"))
10388    ;; Try to get next/previous article not displayed in this group.
10389    ((and gnus-auto-extend-newsgroup
10390          (not unread) (not subject))
10391     (gnus-summary-goto-article
10392      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10393      nil t))
10394    ;; Go to next/previous group.
10395    (t
10396     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10397         (gnus-summary-jump-to-group gnus-newsgroup-name))
10398     (let ((cmd last-command-char)
10399           (group
10400            (if (eq gnus-keep-same-level 'best)
10401                (gnus-summary-best-group gnus-newsgroup-name)
10402              (gnus-summary-search-group backward gnus-keep-same-level))))
10403       ;; For some reason, the group window gets selected.  We change
10404       ;; it back.
10405       (select-window (get-buffer-window (current-buffer)))
10406       ;; Select next unread newsgroup automagically.
10407       (cond
10408        ((or (not gnus-auto-select-next)
10409             (not cmd))
10410         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10411        ((or (eq gnus-auto-select-next 'quietly)
10412             (and (eq gnus-auto-select-next 'slightly-quietly)
10413                  push)
10414             (and (eq gnus-auto-select-next 'almost-quietly)
10415                  (gnus-summary-last-article-p)))
10416         ;; Select quietly.
10417         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10418             (gnus-summary-exit)
10419           (gnus-message 7 "No more%s articles (%s)..."
10420                         (if unread " unread" "")
10421                         (if group (concat "selecting " group)
10422                           "exiting"))
10423           (gnus-summary-next-group nil group backward)))
10424        (t
10425         (gnus-summary-walk-group-buffer
10426          gnus-newsgroup-name cmd unread backward)))))))
10427
10428 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10429   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10430                       (?\C-p (gnus-group-prev-unread-group 1))))
10431         keve key group ended)
10432     (save-excursion
10433       (set-buffer gnus-group-buffer)
10434       (gnus-summary-jump-to-group from-group)
10435       (setq group
10436             (if (eq gnus-keep-same-level 'best)
10437                 (gnus-summary-best-group gnus-newsgroup-name)
10438               (gnus-summary-search-group backward gnus-keep-same-level))))
10439     (while (not ended)
10440       (gnus-message
10441        5 "No more%s articles%s" (if unread " unread" "")
10442        (if (and group
10443                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10444            (format " (Type %s for %s [%s])"
10445                    (single-key-description cmd) group
10446                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10447          (format " (Type %s to exit %s)"
10448                  (single-key-description cmd)
10449                  gnus-newsgroup-name)))
10450       ;; Confirm auto selection.
10451       (setq key (car (setq keve (gnus-read-event-char))))
10452       (setq ended t)
10453       (cond
10454        ((assq key keystrokes)
10455         (let ((obuf (current-buffer)))
10456           (switch-to-buffer gnus-group-buffer)
10457           (and group
10458                (gnus-group-jump-to-group group))
10459           (eval (cadr (assq key keystrokes)))
10460           (setq group (gnus-group-group-name))
10461           (switch-to-buffer obuf))
10462         (setq ended nil))
10463        ((equal key cmd)
10464         (if (or (not group)
10465                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10466             (gnus-summary-exit)
10467           (gnus-summary-next-group nil group backward)))
10468        (t
10469         (push (cdr keve) unread-command-events))))))
10470
10471 (defun gnus-read-event-char ()
10472   "Get the next event."
10473   (let ((event (read-event)))
10474     (cons (and (numberp event) event) event)))
10475
10476 (defun gnus-summary-next-unread-article ()
10477   "Select unread article after current one."
10478   (interactive)
10479   (gnus-summary-next-article t (and gnus-auto-select-same
10480                                     (gnus-summary-article-subject))))
10481
10482 (defun gnus-summary-prev-article (&optional unread subject)
10483   "Select the article after the current one.
10484 If UNREAD is non-nil, only unread articles are selected."
10485   (interactive "P")
10486   (gnus-summary-next-article unread subject t))
10487
10488 (defun gnus-summary-prev-unread-article ()
10489   "Select unred article before current one."
10490   (interactive)
10491   (gnus-summary-prev-article t (and gnus-auto-select-same
10492                                     (gnus-summary-article-subject))))
10493
10494 (defun gnus-summary-next-page (&optional lines circular)
10495   "Show next page of the selected article.
10496 If at the end of the current article, select the next article.
10497 LINES says how many lines should be scrolled up.
10498
10499 If CIRCULAR is non-nil, go to the start of the article instead of
10500 selecting the next article when reaching the end of the current
10501 article."
10502   (interactive "P")
10503   (setq gnus-summary-buffer (current-buffer))
10504   (gnus-set-global-variables)
10505   (let ((article (gnus-summary-article-number))
10506         (endp nil))
10507     (gnus-configure-windows 'article)
10508     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10509         (if (and (eq gnus-summary-goto-unread 'never)
10510                  (not (gnus-summary-last-article-p article)))
10511             (gnus-summary-next-article)
10512           (gnus-summary-next-unread-article))
10513       (if (or (null gnus-current-article)
10514               (null gnus-article-current)
10515               (/= article (cdr gnus-article-current))
10516               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10517           ;; Selected subject is different from current article's.
10518           (gnus-summary-display-article article)
10519         (gnus-eval-in-buffer-window gnus-article-buffer
10520           (setq endp (gnus-article-next-page lines)))
10521         (if endp
10522             (cond (circular
10523                    (gnus-summary-beginning-of-article))
10524                   (lines
10525                    (gnus-message 3 "End of message"))
10526                   ((null lines)
10527                    (if (and (eq gnus-summary-goto-unread 'never)
10528                             (not (gnus-summary-last-article-p article)))
10529                        (gnus-summary-next-article)
10530                      (gnus-summary-next-unread-article)))))))
10531     (gnus-summary-recenter)
10532     (gnus-summary-position-point)))
10533
10534 (defun gnus-summary-prev-page (&optional lines)
10535   "Show previous page of selected article.
10536 Argument LINES specifies lines to be scrolled down."
10537   (interactive "P")
10538   (gnus-set-global-variables)
10539   (let ((article (gnus-summary-article-number)))
10540     (gnus-configure-windows 'article)
10541     (if (or (null gnus-current-article)
10542             (null gnus-article-current)
10543             (/= article (cdr gnus-article-current))
10544             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10545         ;; Selected subject is different from current article's.
10546         (gnus-summary-display-article article)
10547       (gnus-summary-recenter)
10548       (gnus-eval-in-buffer-window gnus-article-buffer
10549         (gnus-article-prev-page lines))))
10550   (gnus-summary-position-point))
10551
10552 (defun gnus-summary-scroll-up (lines)
10553   "Scroll up (or down) one line current article.
10554 Argument LINES specifies lines to be scrolled up (or down if negative)."
10555   (interactive "p")
10556   (gnus-set-global-variables)
10557   (gnus-configure-windows 'article)
10558   (gnus-summary-show-thread)
10559   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10560     (gnus-eval-in-buffer-window gnus-article-buffer
10561       (cond ((> lines 0)
10562              (if (gnus-article-next-page lines)
10563                  (gnus-message 3 "End of message")))
10564             ((< lines 0)
10565              (gnus-article-prev-page (- lines))))))
10566   (gnus-summary-recenter)
10567   (gnus-summary-position-point))
10568
10569 (defun gnus-summary-next-same-subject ()
10570   "Select next article which has the same subject as current one."
10571   (interactive)
10572   (gnus-set-global-variables)
10573   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10574
10575 (defun gnus-summary-prev-same-subject ()
10576   "Select previous article which has the same subject as current one."
10577   (interactive)
10578   (gnus-set-global-variables)
10579   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10580
10581 (defun gnus-summary-next-unread-same-subject ()
10582   "Select next unread article which has the same subject as current one."
10583   (interactive)
10584   (gnus-set-global-variables)
10585   (gnus-summary-next-article t (gnus-summary-article-subject)))
10586
10587 (defun gnus-summary-prev-unread-same-subject ()
10588   "Select previous unread article which has the same subject as current one."
10589   (interactive)
10590   (gnus-set-global-variables)
10591   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10592
10593 (defun gnus-summary-first-unread-article ()
10594   "Select the first unread article.
10595 Return nil if there are no unread articles."
10596   (interactive)
10597   (gnus-set-global-variables)
10598   (prog1
10599       (if (gnus-summary-first-subject t)
10600           (progn
10601             (gnus-summary-show-thread)
10602             (gnus-summary-first-subject t)
10603             (gnus-summary-display-article (gnus-summary-article-number))))
10604     (gnus-summary-position-point)))
10605
10606 (defun gnus-summary-best-unread-article ()
10607   "Select the unread article with the highest score."
10608   (interactive)
10609   (gnus-set-global-variables)
10610   (let ((best -1000000)
10611         (data gnus-newsgroup-data)
10612         article score)
10613     (while data
10614       (and (gnus-data-unread-p (car data))
10615            (> (setq score
10616                     (gnus-summary-article-score (gnus-data-number (car data))))
10617               best)
10618            (setq best score
10619                  article (gnus-data-number (car data))))
10620       (setq data (cdr data)))
10621     (prog1
10622         (if article
10623             (gnus-summary-goto-article article)
10624           (error "No unread articles"))
10625       (gnus-summary-position-point))))
10626
10627 (defun gnus-summary-last-subject ()
10628   "Go to the last displayed subject line in the group."
10629   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10630     (when article
10631       (gnus-summary-goto-subject article))))
10632
10633 (defun gnus-summary-goto-article (article &optional all-headers force)
10634   "Fetch ARTICLE and display it if it exists.
10635 If ALL-HEADERS is non-nil, no header lines are hidden."
10636   (interactive
10637    (list
10638     (string-to-int
10639      (completing-read
10640       "Article number: "
10641       (mapcar (lambda (number) (list (int-to-string number)))
10642               gnus-newsgroup-limit)))
10643     current-prefix-arg
10644     t))
10645   (prog1
10646       (if (gnus-summary-goto-subject article force)
10647           (gnus-summary-display-article article all-headers)
10648         (gnus-message 4 "Couldn't go to article %s" article) nil)
10649     (gnus-summary-position-point)))
10650
10651 (defun gnus-summary-goto-last-article ()
10652   "Go to the previously read article."
10653   (interactive)
10654   (prog1
10655       (and gnus-last-article
10656            (gnus-summary-goto-article gnus-last-article))
10657     (gnus-summary-position-point)))
10658
10659 (defun gnus-summary-pop-article (number)
10660   "Pop one article off the history and go to the previous.
10661 NUMBER articles will be popped off."
10662   (interactive "p")
10663   (let (to)
10664     (setq gnus-newsgroup-history
10665           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10666     (if to
10667         (gnus-summary-goto-article (car to))
10668       (error "Article history empty")))
10669   (gnus-summary-position-point))
10670
10671 ;; Summary commands and functions for limiting the summary buffer.
10672
10673 (defun gnus-summary-limit-to-articles (n)
10674   "Limit the summary buffer to the next N articles.
10675 If not given a prefix, use the process marked articles instead."
10676   (interactive "P")
10677   (gnus-set-global-variables)
10678   (prog1
10679       (let ((articles (gnus-summary-work-articles n)))
10680         (setq gnus-newsgroup-processable nil)
10681         (gnus-summary-limit articles))
10682     (gnus-summary-position-point)))
10683
10684 (defun gnus-summary-pop-limit (&optional total)
10685   "Restore the previous limit.
10686 If given a prefix, remove all limits."
10687   (interactive "P")
10688   (gnus-set-global-variables)
10689   (when total 
10690     (setq gnus-newsgroup-limits
10691           (list (mapcar (lambda (h) (mail-header-number h))
10692                         gnus-newsgroup-headers))))
10693   (unless gnus-newsgroup-limits
10694     (error "No limit to pop"))
10695   (prog1
10696       (gnus-summary-limit nil 'pop)
10697     (gnus-summary-position-point)))
10698
10699 (defun gnus-summary-limit-to-subject (subject &optional header)
10700   "Limit the summary buffer to articles that have subjects that match a regexp."
10701   (interactive "sRegexp: ")
10702   (unless header
10703     (setq header "subject"))
10704   (when (not (equal "" subject))
10705     (prog1
10706         (let ((articles (gnus-summary-find-matching
10707                          (or header "subject") subject 'all)))
10708           (or articles (error "Found no matches for \"%s\"" subject))
10709           (gnus-summary-limit articles))
10710       (gnus-summary-position-point))))
10711
10712 (defun gnus-summary-limit-to-author (from)
10713   "Limit the summary buffer to articles that have authors that match a regexp."
10714   (interactive "sRegexp: ")
10715   (gnus-summary-limit-to-subject from "from"))
10716
10717 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10718 (make-obsolete
10719  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10720
10721 (defun gnus-summary-limit-to-unread (&optional all)
10722   "Limit the summary buffer to articles that are not marked as read.
10723 If ALL is non-nil, limit strictly to unread articles."
10724   (interactive "P")
10725   (if all
10726       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10727     (gnus-summary-limit-to-marks
10728      ;; Concat all the marks that say that an article is read and have
10729      ;; those removed.
10730      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10731            gnus-killed-mark gnus-kill-file-mark
10732            gnus-low-score-mark gnus-expirable-mark
10733            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10734      'reverse)))
10735
10736 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10737 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10738
10739 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10740   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10741 If REVERSE, limit the summary buffer to articles that are not marked
10742 with MARKS.  MARKS can either be a string of marks or a list of marks.
10743 Returns how many articles were removed."
10744   (interactive "sMarks: ")
10745   (gnus-set-global-variables)
10746   (prog1
10747       (let ((data gnus-newsgroup-data)
10748             (marks (if (listp marks) marks
10749                      (append marks nil))) ; Transform to list.
10750             articles)
10751         (while data
10752           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10753                  (memq (gnus-data-mark (car data)) marks))
10754                (setq articles (cons (gnus-data-number (car data)) articles)))
10755           (setq data (cdr data)))
10756         (gnus-summary-limit articles))
10757     (gnus-summary-position-point)))
10758
10759 (defun gnus-summary-limit-to-score (&optional score)
10760   "Limit to articles with score at or above SCORE."
10761   (interactive "P")
10762   (gnus-set-global-variables)
10763   (setq score (if score
10764                   (prefix-numeric-value score)
10765                 (or gnus-summary-default-score 0)))
10766   (let ((data gnus-newsgroup-data)
10767         articles)
10768     (while data
10769       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10770                 score)
10771         (push (gnus-data-number (car data)) articles))
10772       (setq data (cdr data)))
10773     (prog1
10774         (gnus-summary-limit articles)
10775       (gnus-summary-position-point))))
10776
10777 (defun gnus-summary-limit-include-dormant ()
10778   "Display all the hidden articles that are marked as dormant."
10779   (interactive)
10780   (gnus-set-global-variables)
10781   (or gnus-newsgroup-dormant
10782       (error "There are no dormant articles in this group"))
10783   (prog1
10784       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10785     (gnus-summary-position-point)))
10786
10787 (defun gnus-summary-limit-exclude-dormant ()
10788   "Hide all dormant articles."
10789   (interactive)
10790   (gnus-set-global-variables)
10791   (prog1
10792       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10793     (gnus-summary-position-point)))
10794
10795 (defun gnus-summary-limit-exclude-childless-dormant ()
10796   "Hide all dormant articles that have no children."
10797   (interactive)
10798   (gnus-set-global-variables)
10799   (let ((data (gnus-data-list t))
10800         articles d children)
10801     ;; Find all articles that are either not dormant or have
10802     ;; children.
10803     (while (setq d (pop data))
10804       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10805                 (and (setq children 
10806                            (gnus-article-children (gnus-data-number d)))
10807                      (let (found)
10808                        (while children
10809                          (when (memq (car children) articles)
10810                            (setq children nil
10811                                  found t))
10812                          (pop children))
10813                        found)))
10814         (push (gnus-data-number d) articles)))
10815     ;; Do the limiting.
10816     (prog1
10817         (gnus-summary-limit articles)
10818       (gnus-summary-position-point))))
10819
10820 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10821   "Mark all unread excluded articles as read.
10822 If ALL, mark even excluded ticked and dormants as read."
10823   (interactive "P")
10824   (let ((articles (gnus-sorted-complement
10825                    (sort
10826                     (mapcar (lambda (h) (mail-header-number h))
10827                             gnus-newsgroup-headers)
10828                     '<)
10829                    (sort gnus-newsgroup-limit '<)))
10830         article)
10831     (setq gnus-newsgroup-unreads nil)
10832     (if all
10833         (setq gnus-newsgroup-dormant nil
10834               gnus-newsgroup-marked nil
10835               gnus-newsgroup-reads
10836               (nconc
10837                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10838                gnus-newsgroup-reads))
10839       (while (setq article (pop articles))
10840         (unless (or (memq article gnus-newsgroup-dormant)
10841                     (memq article gnus-newsgroup-marked))
10842           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10843
10844 (defun gnus-summary-limit (articles &optional pop)
10845   (if pop
10846       ;; We pop the previous limit off the stack and use that.
10847       (setq articles (car gnus-newsgroup-limits)
10848             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10849     ;; We use the new limit, so we push the old limit on the stack.
10850     (setq gnus-newsgroup-limits
10851           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10852   ;; Set the limit.
10853   (setq gnus-newsgroup-limit articles)
10854   (let ((total (length gnus-newsgroup-data))
10855         (data (gnus-data-find-list (gnus-summary-article-number)))
10856         found)
10857     ;; This will do all the work of generating the new summary buffer
10858     ;; according to the new limit.
10859     (gnus-summary-prepare)
10860     ;; Hide any threads, possibly.
10861     (and gnus-show-threads
10862          gnus-thread-hide-subtree
10863          (gnus-summary-hide-all-threads))
10864     ;; Try to return to the article you were at, or one in the
10865     ;; neighborhood.
10866     (if data
10867         ;; We try to find some article after the current one.
10868         (while data
10869           (and (gnus-summary-goto-subject
10870                 (gnus-data-number (car data)) nil t)
10871                (setq data nil
10872                      found t))
10873           (setq data (cdr data))))
10874     (or found
10875         ;; If there is no data, that means that we were after the last
10876         ;; article.  The same goes when we can't find any articles
10877         ;; after the current one.
10878         (progn
10879           (goto-char (point-max))
10880           (gnus-summary-find-prev)))
10881     ;; We return how many articles were removed from the summary
10882     ;; buffer as a result of the new limit.
10883     (- total (length gnus-newsgroup-data))))
10884
10885 (defsubst gnus-invisible-cut-children (threads)
10886   (let ((num 0))
10887     (while threads
10888       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
10889         (incf num))
10890       (pop threads))
10891     (< num 2)))
10892
10893 (defsubst gnus-cut-thread (thread)
10894   "Go forwards in the thread until we find an article that we want to display."
10895   (when (or (eq gnus-fetch-old-headers 'some)
10896             (eq gnus-build-sparse-threads 'some)
10897             (eq gnus-build-sparse-threads 'more))
10898     ;; Deal with old-fetched headers and sparse threads.
10899     (while (and
10900             thread
10901             (or
10902              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10903              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
10904             (or (<= (length (cdr thread)) 1)
10905                 (gnus-invisible-cut-children (cdr thread))))
10906       (setq thread (cadr thread))))
10907   thread)
10908
10909 (defun gnus-cut-threads (threads)
10910   "Cut off all uninteresting articles from the beginning of threads."
10911   (when (or (eq gnus-fetch-old-headers 'some)
10912             (eq gnus-build-sparse-threads 'some)
10913             (eq gnus-build-sparse-threads 'more))
10914     (let ((th threads))
10915       (while th
10916         (setcar th (gnus-cut-thread (car th)))
10917         (setq th (cdr th)))))
10918   ;; Remove nixed out threads.
10919   (delq nil threads))
10920
10921 (defun gnus-summary-initial-limit (&optional show-if-empty)
10922   "Figure out what the initial limit is supposed to be on group entry.
10923 This entails weeding out unwanted dormants, low-scored articles,
10924 fetch-old-headers verbiage, and so on."
10925   ;; Most groups have nothing to remove.
10926   (if (or gnus-inhibit-limiting
10927           (and (null gnus-newsgroup-dormant)
10928                (not (eq gnus-fetch-old-headers 'some))
10929                (null gnus-summary-expunge-below)
10930                (not (eq gnus-build-sparse-threads 'some))
10931                (not (eq gnus-build-sparse-threads 'more))
10932                (null gnus-thread-expunge-below)
10933                (not gnus-use-nocem)))
10934       () ; Do nothing.
10935     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10936     (setq gnus-newsgroup-limit nil)
10937     (mapatoms
10938      (lambda (node)
10939        (unless (car (symbol-value node))
10940          ;; These threads have no parents -- they are roots.
10941          (let ((nodes (cdr (symbol-value node)))
10942                thread)
10943            (while nodes
10944              (if (and gnus-thread-expunge-below
10945                       (< (gnus-thread-total-score (car nodes))
10946                          gnus-thread-expunge-below))
10947                  (gnus-expunge-thread (pop nodes))
10948                (setq thread (pop nodes))
10949                (gnus-summary-limit-children thread))))))
10950      gnus-newsgroup-dependencies)
10951     ;; If this limitation resulted in an empty group, we might
10952     ;; pop the previous limit and use it instead.
10953     (when (and (not gnus-newsgroup-limit)
10954                show-if-empty)
10955       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10956     gnus-newsgroup-limit))
10957
10958 (defun gnus-summary-limit-children (thread)
10959   "Return 1 if this subthread is visible and 0 if it is not."
10960   ;; First we get the number of visible children to this thread.  This
10961   ;; is done by recursing down the thread using this function, so this
10962   ;; will really go down to a leaf article first, before slowly
10963   ;; working its way up towards the root.
10964   (when thread
10965     (let ((children
10966            (if (cdr thread)
10967                (apply '+ (mapcar 'gnus-summary-limit-children
10968                                  (cdr thread)))
10969              0))
10970           (number (mail-header-number (car thread)))
10971           score)
10972       (if (or
10973            ;; If this article is dormant and has absolutely no visible
10974            ;; children, then this article isn't visible.
10975            (and (memq number gnus-newsgroup-dormant)
10976                 (= children 0))
10977            ;; If this is "fetch-old-headered" and there is only one
10978            ;; visible child (or less), then we don't want this article.
10979            (and (eq gnus-fetch-old-headers 'some)
10980                 (memq number gnus-newsgroup-ancient)
10981                 (zerop children))
10982            ;; If this is a sparsely inserted article with no children,
10983            ;; we don't want it.
10984            (and (eq gnus-build-sparse-threads 'some)
10985                 (memq number gnus-newsgroup-sparse)
10986                 (zerop children))
10987            ;; If we use expunging, and this article is really
10988            ;; low-scored, then we don't want this article.
10989            (when (and gnus-summary-expunge-below
10990                       (< (setq score
10991                                (or (cdr (assq number gnus-newsgroup-scored))
10992                                    gnus-summary-default-score))
10993                          gnus-summary-expunge-below))
10994              ;; We increase the expunge-tally here, but that has
10995              ;; nothing to do with the limits, really.
10996              (incf gnus-newsgroup-expunged-tally)
10997              ;; We also mark as read here, if that's wanted.
10998              (when (and gnus-summary-mark-below
10999                         (< score gnus-summary-mark-below))
11000                (setq gnus-newsgroup-unreads
11001                      (delq number gnus-newsgroup-unreads))
11002                (if gnus-newsgroup-auto-expire
11003                    (push number gnus-newsgroup-expirable)
11004                  (push (cons number gnus-low-score-mark)
11005                        gnus-newsgroup-reads)))
11006              t)
11007            (and gnus-use-nocem
11008                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
11009           ;; Nope, invisible article.
11010           0
11011         ;; Ok, this article is to be visible, so we add it to the limit
11012         ;; and return 1.
11013         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
11014         1))))
11015
11016 (defun gnus-expunge-thread (thread)
11017   "Mark all articles in THREAD as read."
11018   (let* ((number (mail-header-number (car thread))))
11019     (incf gnus-newsgroup-expunged-tally)
11020     ;; We also mark as read here, if that's wanted.
11021     (setq gnus-newsgroup-unreads
11022           (delq number gnus-newsgroup-unreads))
11023     (if gnus-newsgroup-auto-expire
11024         (push number gnus-newsgroup-expirable)
11025       (push (cons number gnus-low-score-mark)
11026             gnus-newsgroup-reads)))
11027   ;; Go recursively through all subthreads.
11028   (mapcar 'gnus-expunge-thread (cdr thread)))
11029
11030 ;; Summary article oriented commands
11031
11032 (defun gnus-summary-refer-parent-article (n)
11033   "Refer parent article N times.
11034 The difference between N and the number of articles fetched is returned."
11035   (interactive "p")
11036   (gnus-set-global-variables)
11037   (while
11038       (and
11039        (> n 0)
11040        (let* ((header (gnus-summary-article-header))
11041               (ref
11042                ;; If we try to find the parent of the currently
11043                ;; displayed article, then we take a look at the actual
11044                ;; References header, since this is slightly more
11045                ;; reliable than the References field we got from the
11046                ;; server.
11047                (if (and (eq (mail-header-number header)
11048                             (cdr gnus-article-current))
11049                         (equal gnus-newsgroup-name
11050                                (car gnus-article-current)))
11051                    (save-excursion
11052                      (set-buffer gnus-original-article-buffer)
11053                      (nnheader-narrow-to-headers)
11054                      (prog1
11055                          (message-fetch-field "references")
11056                        (widen)))
11057                  ;; It's not the current article, so we take a bet on
11058                  ;; the value we got from the server.
11059                  (mail-header-references header))))
11060          (if (setq ref (or ref (mail-header-references header)))
11061              (or (gnus-summary-refer-article (gnus-parent-id ref))
11062                  (gnus-message 1 "Couldn't find parent"))
11063            (gnus-message 1 "No references in article %d"
11064                          (gnus-summary-article-number))
11065            nil)))
11066     (setq n (1- n)))
11067   (gnus-summary-position-point)
11068   n)
11069
11070 (defun gnus-summary-refer-references ()
11071   "Fetch all articles mentioned in the References header.
11072 Return how many articles were fetched."
11073   (interactive)
11074   (gnus-set-global-variables)
11075   (let ((ref (mail-header-references (gnus-summary-article-header)))
11076         (current (gnus-summary-article-number))
11077         (n 0))
11078     ;; For each Message-ID in the References header...
11079     (while (string-match "<[^>]*>" ref)
11080       (incf n)
11081       ;; ... fetch that article.
11082       (gnus-summary-refer-article
11083        (prog1 (match-string 0 ref)
11084          (setq ref (substring ref (match-end 0))))))
11085     (gnus-summary-goto-subject current)
11086     (gnus-summary-position-point)
11087     n))
11088
11089 (defun gnus-summary-refer-article (message-id)
11090   "Fetch an article specified by MESSAGE-ID."
11091   (interactive "sMessage-ID: ")
11092   (when (and (stringp message-id)
11093              (not (zerop (length message-id))))
11094     ;; Construct the correct Message-ID if necessary.
11095     ;; Suggested by tale@pawl.rpi.edu.
11096     (unless (string-match "^<" message-id)
11097       (setq message-id (concat "<" message-id)))
11098     (unless (string-match ">$" message-id)
11099       (setq message-id (concat message-id ">")))
11100     (let* ((header (gnus-id-to-header message-id))
11101            (sparse (and header
11102                         (memq (mail-header-number header)
11103                               gnus-newsgroup-sparse))))
11104       (if header
11105           (prog1
11106               ;; The article is present in the buffer, to we just go to it.
11107               (gnus-summary-goto-article 
11108                (mail-header-number header) nil header)
11109             (when sparse
11110               (gnus-summary-update-article (mail-header-number header))))
11111         ;; We fetch the article
11112         (let ((gnus-override-method 
11113                (and (gnus-news-group-p gnus-newsgroup-name)
11114                     gnus-refer-article-method))
11115               number)
11116           ;; Start the special refer-article method, if necessary.
11117           (when (and gnus-refer-article-method
11118                      (gnus-news-group-p gnus-newsgroup-name))
11119             (gnus-check-server gnus-refer-article-method))
11120           ;; Fetch the header, and display the article.
11121           (if (setq number (gnus-summary-insert-subject message-id))
11122               (gnus-summary-select-article nil nil nil number)
11123             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
11124
11125 (defun gnus-summary-enter-digest-group (&optional force)
11126   "Enter a digest group based on the current article."
11127   (interactive "P")
11128   (gnus-set-global-variables)
11129   (gnus-summary-select-article)
11130   (let ((name (format "%s-%d"
11131                       (gnus-group-prefixed-name
11132                        gnus-newsgroup-name (list 'nndoc ""))
11133                       gnus-current-article))
11134         (ogroup gnus-newsgroup-name)
11135         (case-fold-search t)
11136         (buf (current-buffer))
11137         dig)
11138     (save-excursion
11139       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
11140       (insert-buffer-substring gnus-original-article-buffer)
11141       (narrow-to-region
11142        (goto-char (point-min))
11143        (or (search-forward "\n\n" nil t) (point)))
11144       (goto-char (point-min))
11145       (delete-matching-lines "^\\(Path\\):\\|^From ")
11146       (widen))
11147     (unwind-protect
11148         (if (gnus-group-read-ephemeral-group
11149              name `(nndoc ,name (nndoc-address
11150                                  ,(get-buffer dig))
11151                           (nndoc-article-type ,(if force 'digest 'guess))) t)
11152             ;; Make all postings to this group go to the parent group.
11153             (nconc (gnus-info-params (gnus-get-info name))
11154                    (list (cons 'to-group ogroup)))
11155           ;; Couldn't select this doc group.
11156           (switch-to-buffer buf)
11157           (gnus-set-global-variables)
11158           (gnus-configure-windows 'summary)
11159           (gnus-message 3 "Article couldn't be entered?"))
11160       (kill-buffer dig))))
11161
11162 (defun gnus-summary-isearch-article (&optional regexp-p)
11163   "Do incremental search forward on the current article.
11164 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
11165   (interactive "P")
11166   (gnus-set-global-variables)
11167   (gnus-summary-select-article)
11168   (gnus-configure-windows 'article)
11169   (gnus-eval-in-buffer-window gnus-article-buffer
11170     (goto-char (point-min))
11171     (isearch-forward regexp-p)))
11172
11173 (defun gnus-summary-search-article-forward (regexp &optional backward)
11174   "Search for an article containing REGEXP forward.
11175 If BACKWARD, search backward instead."
11176   (interactive
11177    (list (read-string
11178           (format "Search article %s (regexp%s): "
11179                   (if current-prefix-arg "backward" "forward")
11180                   (if gnus-last-search-regexp
11181                       (concat ", default " gnus-last-search-regexp)
11182                     "")))
11183          current-prefix-arg))
11184   (gnus-set-global-variables)
11185   (if (string-equal regexp "")
11186       (setq regexp (or gnus-last-search-regexp ""))
11187     (setq gnus-last-search-regexp regexp))
11188   (unless (gnus-summary-search-article regexp backward)
11189     (error "Search failed: \"%s\"" regexp)))
11190
11191 (defun gnus-summary-search-article-backward (regexp)
11192   "Search for an article containing REGEXP backward."
11193   (interactive
11194    (list (read-string
11195           (format "Search article backward (regexp%s): "
11196                   (if gnus-last-search-regexp
11197                       (concat ", default " gnus-last-search-regexp)
11198                     "")))))
11199   (gnus-summary-search-article-forward regexp 'backward))
11200
11201 (defun gnus-summary-search-article (regexp &optional backward)
11202   "Search for an article containing REGEXP.
11203 Optional argument BACKWARD means do search for backward.
11204 `gnus-select-article-hook' is not called during the search."
11205   (let ((gnus-select-article-hook nil)  ;Disable hook.
11206         (gnus-article-display-hook nil)
11207         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11208         (re-search
11209          (if backward
11210              're-search-backward 're-search-forward))
11211         (sum (current-buffer))
11212         (found nil))
11213     (gnus-save-hidden-threads
11214       (gnus-summary-select-article)
11215       (set-buffer gnus-article-buffer)
11216       (when backward
11217         (forward-line -1))
11218       (while (not found)
11219         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
11220         (if (if backward
11221                 (re-search-backward regexp nil t)
11222               (re-search-forward regexp nil t))
11223             ;; We found the regexp.
11224             (progn
11225               (setq found 'found)
11226               (beginning-of-line)
11227               (set-window-start
11228                (get-buffer-window (current-buffer))
11229                (point))
11230               (forward-line 1)
11231               (set-buffer sum))
11232           ;; We didn't find it, so we go to the next article.
11233           (set-buffer sum)
11234           (if (not (if backward (gnus-summary-find-prev)
11235                      (gnus-summary-find-next)))
11236               ;; No more articles.
11237               (setq found t)
11238             ;; Select the next article and adjust point.
11239             (gnus-summary-select-article)
11240             (set-buffer gnus-article-buffer)
11241             (widen)
11242             (goto-char (if backward (point-max) (point-min))))))
11243       (gnus-message 7 ""))
11244     ;; Return whether we found the regexp.
11245     (when (eq found 'found)
11246       (gnus-summary-show-thread)
11247       (gnus-summary-goto-subject gnus-current-article)
11248       (gnus-summary-position-point)
11249       t)))
11250
11251 (defun gnus-summary-find-matching (header regexp &optional backward unread
11252                                           not-case-fold)
11253   "Return a list of all articles that match REGEXP on HEADER.
11254 The search stars on the current article and goes forwards unless
11255 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11256 If UNREAD is non-nil, only unread articles will
11257 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11258 in the comparisons."
11259   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11260                 (gnus-data-find-list
11261                  (gnus-summary-article-number) (gnus-data-list backward))))
11262         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11263         (case-fold-search (not not-case-fold))
11264         articles d)
11265     (or (fboundp (intern (concat "mail-header-" header)))
11266         (error "%s is not a valid header" header))
11267     (while data
11268       (setq d (car data))
11269       (and (or (not unread)             ; We want all articles...
11270                (gnus-data-unread-p d))  ; Or just unreads.
11271            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11272            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11273            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11274       (setq data (cdr data)))
11275     (nreverse articles)))
11276
11277 (defun gnus-summary-execute-command (header regexp command &optional backward)
11278   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11279 If HEADER is an empty string (or nil), the match is done on the entire
11280 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11281   (interactive
11282    (list (let ((completion-ignore-case t))
11283            (completing-read
11284             "Header name: "
11285             (mapcar (lambda (string) (list string))
11286                     '("Number" "Subject" "From" "Lines" "Date"
11287                       "Message-ID" "Xref" "References" "Body"))
11288             nil 'require-match))
11289          (read-string "Regexp: ")
11290          (read-key-sequence "Command: ")
11291          current-prefix-arg))
11292   (when (equal header "Body")
11293     (setq header ""))
11294   (gnus-set-global-variables)
11295   ;; Hidden thread subtrees must be searched as well.
11296   (gnus-summary-show-all-threads)
11297   ;; We don't want to change current point nor window configuration.
11298   (save-excursion
11299     (save-window-excursion
11300       (gnus-message 6 "Executing %s..." (key-description command))
11301       ;; We'd like to execute COMMAND interactively so as to give arguments.
11302       (gnus-execute header regexp
11303                     `(lambda () (call-interactively ',(key-binding command)))
11304                     backward)
11305       (gnus-message 6 "Executing %s...done" (key-description command)))))
11306
11307 (defun gnus-summary-beginning-of-article ()
11308   "Scroll the article back to the beginning."
11309   (interactive)
11310   (gnus-set-global-variables)
11311   (gnus-summary-select-article)
11312   (gnus-configure-windows 'article)
11313   (gnus-eval-in-buffer-window gnus-article-buffer
11314     (widen)
11315     (goto-char (point-min))
11316     (and gnus-break-pages (gnus-narrow-to-page))))
11317
11318 (defun gnus-summary-end-of-article ()
11319   "Scroll to the end of the article."
11320   (interactive)
11321   (gnus-set-global-variables)
11322   (gnus-summary-select-article)
11323   (gnus-configure-windows 'article)
11324   (gnus-eval-in-buffer-window gnus-article-buffer
11325     (widen)
11326     (goto-char (point-max))
11327     (recenter -3)
11328     (and gnus-break-pages (gnus-narrow-to-page))))
11329
11330 (defun gnus-summary-show-article (&optional arg)
11331   "Force re-fetching of the current article.
11332 If ARG (the prefix) is non-nil, show the raw article without any
11333 article massaging functions being run."
11334   (interactive "P")
11335   (gnus-set-global-variables)
11336   (if (not arg)
11337       ;; Select the article the normal way.
11338       (gnus-summary-select-article nil 'force)
11339     ;; Bind the article treatment functions to nil.
11340     (let ((gnus-have-all-headers t)
11341           gnus-article-display-hook
11342           gnus-article-prepare-hook
11343           gnus-break-pages
11344           gnus-visual)
11345       (gnus-summary-select-article nil 'force)))
11346   (gnus-summary-goto-subject gnus-current-article)
11347 ;  (gnus-configure-windows 'article)
11348   (gnus-summary-position-point))
11349
11350 (defun gnus-summary-verbose-headers (&optional arg)
11351   "Toggle permanent full header display.
11352 If ARG is a positive number, turn header display on.
11353 If ARG is a negative number, turn header display off."
11354   (interactive "P")
11355   (gnus-set-global-variables)
11356   (gnus-summary-toggle-header arg)
11357   (setq gnus-show-all-headers
11358         (cond ((or (not (numberp arg))
11359                    (zerop arg))
11360                (not gnus-show-all-headers))
11361               ((natnump arg)
11362                t))))
11363
11364 (defun gnus-summary-toggle-header (&optional arg)
11365   "Show the headers if they are hidden, or hide them if they are shown.
11366 If ARG is a positive number, show the entire header.
11367 If ARG is a negative number, hide the unwanted header lines."
11368   (interactive "P")
11369   (gnus-set-global-variables)
11370   (save-excursion
11371     (set-buffer gnus-article-buffer)
11372     (let* ((buffer-read-only nil)
11373            (inhibit-point-motion-hooks t)
11374            (hidden (text-property-any
11375                     (goto-char (point-min)) (search-forward "\n\n")
11376                     'invisible t))
11377            e)
11378       (goto-char (point-min))
11379       (when (search-forward "\n\n" nil t)
11380         (delete-region (point-min) (1- (point))))
11381       (goto-char (point-min))
11382       (save-excursion
11383         (set-buffer gnus-original-article-buffer)
11384         (goto-char (point-min))
11385         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11386       (insert-buffer-substring gnus-original-article-buffer 1 e)
11387       (let ((gnus-inhibit-hiding t))
11388         (run-hooks 'gnus-article-display-hook))
11389       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11390           (gnus-article-hide-headers)))))
11391
11392 (defun gnus-summary-show-all-headers ()
11393   "Make all header lines visible."
11394   (interactive)
11395   (gnus-set-global-variables)
11396   (gnus-article-show-all-headers))
11397
11398 (defun gnus-summary-toggle-mime (&optional arg)
11399   "Toggle MIME processing.
11400 If ARG is a positive number, turn MIME processing on."
11401   (interactive "P")
11402   (gnus-set-global-variables)
11403   (setq gnus-show-mime
11404         (if (null arg) (not gnus-show-mime)
11405           (> (prefix-numeric-value arg) 0)))
11406   (gnus-summary-select-article t 'force))
11407
11408 (defun gnus-summary-caesar-message (&optional arg)
11409   "Caesar rotate the current article by 13.
11410 The numerical prefix specifies how manu places to rotate each letter
11411 forward."
11412   (interactive "P")
11413   (gnus-set-global-variables)
11414   (gnus-summary-select-article)
11415   (let ((mail-header-separator ""))
11416     (gnus-eval-in-buffer-window gnus-article-buffer
11417       (save-restriction
11418         (widen)
11419         (let ((start (window-start))
11420               buffer-read-only)
11421           (message-caesar-buffer-body arg)
11422           (set-window-start (get-buffer-window (current-buffer)) start))))))
11423
11424 (defun gnus-summary-stop-page-breaking ()
11425   "Stop page breaking in the current article."
11426   (interactive)
11427   (gnus-set-global-variables)
11428   (gnus-summary-select-article)
11429   (gnus-eval-in-buffer-window gnus-article-buffer
11430     (widen)))
11431
11432 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11433   "Move the current article to a different newsgroup.
11434 If N is a positive number, move the N next articles.
11435 If N is a negative number, move the N previous articles.
11436 If N is nil and any articles have been marked with the process mark,
11437 move those articles instead.
11438 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11439 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11440 re-spool using this method.
11441
11442 For this function to work, both the current newsgroup and the
11443 newsgroup that you want to move to have to support the `request-move'
11444 and `request-accept' functions."
11445   (interactive "P")
11446   (unless action (setq action 'move))
11447   (gnus-set-global-variables)
11448   ;; Check whether the source group supports the required functions.
11449   (cond ((and (eq action 'move)
11450               (not (gnus-check-backend-function
11451                     'request-move-article gnus-newsgroup-name)))
11452          (error "The current group does not support article moving"))
11453         ((and (eq action 'crosspost)
11454               (not (gnus-check-backend-function
11455                     'request-replace-article gnus-newsgroup-name)))
11456          (error "The current group does not support article editing")))
11457   (let ((articles (gnus-summary-work-articles n))
11458         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11459         (names '((move "Move" "Moving")
11460                  (copy "Copy" "Copying")
11461                  (crosspost "Crosspost" "Crossposting")))
11462         (copy-buf (save-excursion
11463                     (nnheader-set-temp-buffer " *copy article*")))
11464         art-group to-method new-xref article to-groups)
11465     (unless (assq action names)
11466       (error "Unknown action %s" action))
11467     ;; Read the newsgroup name.
11468     (when (and (not to-newsgroup)
11469                (not select-method))
11470       (setq to-newsgroup
11471             (gnus-read-move-group-name
11472              (cadr (assq action names))
11473              (symbol-value (intern (format "gnus-current-%s-group" action)))
11474              articles prefix))
11475       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11476     (setq to-method (or select-method 
11477                         (gnus-find-method-for-group to-newsgroup)))
11478     ;; Check the method we are to move this article to...
11479     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11480         (error "%s does not support article copying" (car to-method)))
11481     (or (gnus-check-server to-method)
11482         (error "Can't open server %s" (car to-method)))
11483     (gnus-message 6 "%s to %s: %s..."
11484                   (caddr (assq action names))
11485                   (or (car select-method) to-newsgroup) articles)
11486     (while articles
11487       (setq article (pop articles))
11488       (setq
11489        art-group
11490        (cond
11491         ;; Move the article.
11492         ((eq action 'move)
11493          (gnus-request-move-article
11494           article                       ; Article to move
11495           gnus-newsgroup-name           ; From newsgrouo
11496           (nth 1 (gnus-find-method-for-group
11497                   gnus-newsgroup-name)) ; Server
11498           (list 'gnus-request-accept-article
11499                 to-newsgroup (list 'quote select-method)
11500                 (not articles))         ; Accept form
11501           (not articles)))              ; Only save nov last time
11502         ;; Copy the article.
11503         ((eq action 'copy)
11504          (save-excursion
11505            (set-buffer copy-buf)
11506            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11507            (gnus-request-accept-article
11508             to-newsgroup select-method (not articles))))
11509         ;; Crosspost the article.
11510         ((eq action 'crosspost)
11511          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11512            (setq new-xref (concat gnus-newsgroup-name ":" article))
11513            (if (and xref (not (string= xref "")))
11514                (progn
11515                  (when (string-match "^Xref: " xref)
11516                    (setq xref (substring xref (match-end 0))))
11517                  (setq new-xref (concat xref " " new-xref)))
11518              (setq new-xref (concat (system-name) " " new-xref)))
11519            (save-excursion
11520              (set-buffer copy-buf)
11521              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11522              (nnheader-replace-header "xref" new-xref)
11523              (gnus-request-accept-article
11524               to-newsgroup select-method (not articles)))))))
11525       (if (not art-group)
11526           (gnus-message 1 "Couldn't %s article %s"
11527                         (cadr (assq action names)) article)
11528         (let* ((entry
11529                 (or
11530                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11531                  (gnus-gethash
11532                   (gnus-group-prefixed-name
11533                    (car art-group)
11534                    (or select-method 
11535                        (gnus-find-method-for-group to-newsgroup)))
11536                   gnus-newsrc-hashtb)))
11537                (info (nth 2 entry))
11538                (to-group (gnus-info-group info)))
11539           ;; Update the group that has been moved to.
11540           (when (and info
11541                      (memq action '(move copy)))
11542             (unless (member to-group to-groups)
11543               (push to-group to-groups))
11544
11545             (unless (memq article gnus-newsgroup-unreads)
11546               (gnus-info-set-read
11547                info (gnus-add-to-range (gnus-info-read info)
11548                                        (list (cdr art-group)))))
11549
11550             ;; Copy any marks over to the new group.
11551             (let ((marks gnus-article-mark-lists)
11552                   (to-article (cdr art-group)))
11553
11554               ;; See whether the article is to be put in the cache.
11555               (when gnus-use-cache
11556                 (gnus-cache-possibly-enter-article
11557                  to-group to-article
11558                  (let ((header (copy-sequence
11559                                 (gnus-summary-article-header article))))
11560                    (mail-header-set-number header to-article)
11561                    header)
11562                  (memq article gnus-newsgroup-marked)
11563                  (memq article gnus-newsgroup-dormant)
11564                  (memq article gnus-newsgroup-unreads)))
11565
11566               (while marks
11567                 (when (memq article (symbol-value
11568                                      (intern (format "gnus-newsgroup-%s"
11569                                                      (caar marks)))))
11570                   ;; If the other group is the same as this group,
11571                   ;; then we have to add the mark to the list.
11572                   (when (equal to-group gnus-newsgroup-name)
11573                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11574                          (cons to-article
11575                                (symbol-value
11576                                 (intern (format "gnus-newsgroup-%s"
11577                                                 (caar marks)))))))
11578                   ;; Copy mark to other group.
11579                   (gnus-add-marked-articles
11580                    to-group (cdar marks) (list to-article) info))
11581                 (setq marks (cdr marks)))))
11582
11583           ;; Update the Xref header in this article to point to
11584           ;; the new crossposted article we have just created.
11585           (when (eq action 'crosspost)
11586             (save-excursion
11587               (set-buffer copy-buf)
11588               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11589               (nnheader-replace-header
11590                "xref" (concat new-xref " " (gnus-group-prefixed-name
11591                                             (car art-group) to-method)
11592                               ":" (cdr art-group)))
11593               (gnus-request-replace-article
11594                article gnus-newsgroup-name (current-buffer)))))
11595
11596         (gnus-summary-goto-subject article)
11597         (when (eq action 'move)
11598           (gnus-summary-mark-article article gnus-canceled-mark)))
11599       (gnus-summary-remove-process-mark article))
11600     ;; Re-activate all groups that have been moved to.
11601     (while to-groups
11602       (gnus-activate-group (pop to-groups)))
11603     
11604     (gnus-kill-buffer copy-buf)
11605     (gnus-summary-position-point)
11606     (gnus-set-mode-line 'summary)))
11607
11608 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11609   "Move the current article to a different newsgroup.
11610 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11611 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11612 re-spool using this method."
11613   (interactive "P")
11614   (gnus-summary-move-article n nil select-method 'copy))
11615
11616 (defun gnus-summary-crosspost-article (&optional n)
11617   "Crosspost the current article to some other group."
11618   (interactive "P")
11619   (gnus-summary-move-article n nil nil 'crosspost))
11620
11621 (defvar gnus-summary-respool-default-method nil
11622   "Default method for respooling an article.  
11623 If nil, use to the current newsgroup method.")
11624
11625 (defun gnus-summary-respool-article (&optional n method)
11626   "Respool the current article.
11627 The article will be squeezed through the mail spooling process again,
11628 which means that it will be put in some mail newsgroup or other
11629 depending on `nnmail-split-methods'.
11630 If N is a positive number, respool the N next articles.
11631 If N is a negative number, respool the N previous articles.
11632 If N is nil and any articles have been marked with the process mark,
11633 respool those articles instead.
11634
11635 Respooling can be done both from mail groups and \"real\" newsgroups.
11636 In the former case, the articles in question will be moved from the
11637 current group into whatever groups they are destined to.  In the
11638 latter case, they will be copied into the relevant groups."
11639   (interactive 
11640    (list current-prefix-arg
11641          (let* ((methods (gnus-methods-using 'respool))
11642                 (methname
11643                  (symbol-name (or gnus-summary-respool-default-method
11644                                   (car (gnus-find-method-for-group
11645                                         gnus-newsgroup-name)))))
11646                 (method
11647                  (gnus-completing-read 
11648                   methname "What backend do you want to use when? "
11649                   methods nil t nil 'gnus-method-history))
11650                 ms)
11651            (cond
11652             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11653              (list (intern method) ""))
11654             ((= 1 (length ms))
11655              (car ms))
11656             (t
11657              (cdr (completing-read 
11658                    "Server name: "
11659                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11660   (gnus-set-global-variables)
11661   (unless method
11662     (error "No method given for respooling"))
11663   (if (assoc (symbol-name
11664               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11665              (gnus-methods-using 'respool))
11666       (gnus-summary-move-article n nil method)
11667     (gnus-summary-copy-article n nil method)))
11668
11669 (defun gnus-summary-import-article (file)
11670   "Import a random file into a mail newsgroup."
11671   (interactive "fImport file: ")
11672   (gnus-set-global-variables)
11673   (let ((group gnus-newsgroup-name)
11674         (now (current-time))
11675         atts lines)
11676     (or (gnus-check-backend-function 'request-accept-article group)
11677         (error "%s does not support article importing" group))
11678     (or (file-readable-p file)
11679         (not (file-regular-p file))
11680         (error "Can't read %s" file))
11681     (save-excursion
11682       (set-buffer (get-buffer-create " *import file*"))
11683       (buffer-disable-undo (current-buffer))
11684       (erase-buffer)
11685       (insert-file-contents file)
11686       (goto-char (point-min))
11687       (unless (nnheader-article-p)
11688         ;; This doesn't look like an article, so we fudge some headers.
11689         (setq atts (file-attributes file)
11690               lines (count-lines (point-min) (point-max)))
11691         (insert "From: " (read-string "From: ") "\n"
11692                 "Subject: " (read-string "Subject: ") "\n"
11693                 "Date: " (timezone-make-date-arpa-standard
11694                           (current-time-string (nth 5 atts))
11695                           (current-time-zone now)
11696                           (current-time-zone now)) "\n"
11697                 "Message-ID: " (message-make-message-id) "\n"
11698                 "Lines: " (int-to-string lines) "\n"
11699                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11700       (gnus-request-accept-article group nil t)
11701       (kill-buffer (current-buffer)))))
11702
11703 (defun gnus-summary-expire-articles (&optional now)
11704   "Expire all articles that are marked as expirable in the current group."
11705   (interactive)
11706   (gnus-set-global-variables)
11707   (when (gnus-check-backend-function
11708          'request-expire-articles gnus-newsgroup-name)
11709     ;; This backend supports expiry.
11710     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11711            (expirable (if total
11712                           (gnus-list-of-read-articles gnus-newsgroup-name)
11713                         (setq gnus-newsgroup-expirable
11714                               (sort gnus-newsgroup-expirable '<))))
11715            (expiry-wait (if now 'immediate
11716                           (gnus-group-get-parameter
11717                            gnus-newsgroup-name 'expiry-wait)))
11718            es)
11719       (when expirable
11720         ;; There are expirable articles in this group, so we run them
11721         ;; through the expiry process.
11722         (gnus-message 6 "Expiring articles...")
11723         ;; The list of articles that weren't expired is returned.
11724         (if expiry-wait
11725             (let ((nnmail-expiry-wait-function nil)
11726                   (nnmail-expiry-wait expiry-wait))
11727               (setq es (gnus-request-expire-articles
11728                         expirable gnus-newsgroup-name)))
11729           (setq es (gnus-request-expire-articles
11730                     expirable gnus-newsgroup-name)))
11731         (or total (setq gnus-newsgroup-expirable es))
11732         ;; We go through the old list of expirable, and mark all
11733         ;; really expired articles as nonexistent.
11734         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11735           (let ((gnus-use-cache nil))
11736             (while expirable
11737               (unless (memq (car expirable) es)
11738                 (when (gnus-data-find (car expirable))
11739                   (gnus-summary-mark-article
11740                    (car expirable) gnus-canceled-mark)))
11741               (setq expirable (cdr expirable)))))
11742         (gnus-message 6 "Expiring articles...done")))))
11743
11744 (defun gnus-summary-expire-articles-now ()
11745   "Expunge all expirable articles in the current group.
11746 This means that *all* articles that are marked as expirable will be
11747 deleted forever, right now."
11748   (interactive)
11749   (gnus-set-global-variables)
11750   (or gnus-expert-user
11751       (gnus-y-or-n-p
11752        "Are you really, really, really sure you want to delete all these messages? ")
11753       (error "Phew!"))
11754   (gnus-summary-expire-articles t))
11755
11756 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11757 (defun gnus-summary-delete-article (&optional n)
11758   "Delete the N next (mail) articles.
11759 This command actually deletes articles.  This is not a marking
11760 command.  The article will disappear forever from your life, never to
11761 return.
11762 If N is negative, delete backwards.
11763 If N is nil and articles have been marked with the process mark,
11764 delete these instead."
11765   (interactive "P")
11766   (gnus-set-global-variables)
11767   (or (gnus-check-backend-function 'request-expire-articles
11768                                    gnus-newsgroup-name)
11769       (error "The current newsgroup does not support article deletion."))
11770   ;; Compute the list of articles to delete.
11771   (let ((articles (gnus-summary-work-articles n))
11772         not-deleted)
11773     (if (and gnus-novice-user
11774              (not (gnus-y-or-n-p
11775                    (format "Do you really want to delete %s forever? "
11776                            (if (> (length articles) 1) 
11777                                (format "these %s articles" (length articles))
11778                              "this article")))))
11779         ()
11780       ;; Delete the articles.
11781       (setq not-deleted (gnus-request-expire-articles
11782                          articles gnus-newsgroup-name 'force))
11783       (while articles
11784         (gnus-summary-remove-process-mark (car articles))
11785         ;; The backend might not have been able to delete the article
11786         ;; after all.
11787         (or (memq (car articles) not-deleted)
11788             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11789         (setq articles (cdr articles))))
11790     (gnus-summary-position-point)
11791     (gnus-set-mode-line 'summary)
11792     not-deleted))
11793
11794 (defun gnus-summary-edit-article (&optional force)
11795   "Enter into a buffer and edit the current article.
11796 This will have permanent effect only in mail groups.
11797 If FORCE is non-nil, allow editing of articles even in read-only
11798 groups."
11799   (interactive "P")
11800   (save-excursion
11801     (set-buffer gnus-summary-buffer)
11802     (gnus-set-global-variables)
11803     (when (and (not force)
11804                (gnus-group-read-only-p))
11805       (error "The current newsgroup does not support article editing."))
11806     (gnus-summary-select-article t nil t)
11807     (gnus-configure-windows 'article)
11808     (select-window (get-buffer-window gnus-article-buffer))
11809     (gnus-message 6 "C-c C-c to end edits")
11810     (setq buffer-read-only nil)
11811     (text-mode)
11812     (use-local-map (copy-keymap (current-local-map)))
11813     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11814     (buffer-enable-undo)
11815     (widen)
11816     (goto-char (point-min))
11817     (search-forward "\n\n" nil t)))
11818
11819 (defun gnus-summary-edit-article-done ()
11820   "Make edits to the current article permanent."
11821   (interactive)
11822   (if (gnus-group-read-only-p)
11823       (progn
11824         (gnus-summary-edit-article-postpone)
11825         (gnus-error
11826          1 "The current newsgroup does not support article editing."))
11827     (let ((buf (format "%s" (buffer-string))))
11828       (erase-buffer)
11829       (insert buf)
11830       (if (not (gnus-request-replace-article
11831                 (cdr gnus-article-current) (car gnus-article-current)
11832                 (current-buffer)))
11833           (error "Couldn't replace article.")
11834         (gnus-article-mode)
11835         (use-local-map gnus-article-mode-map)
11836         (setq buffer-read-only t)
11837         (buffer-disable-undo (current-buffer))
11838         (gnus-configure-windows 'summary)
11839         (gnus-summary-update-article (cdr gnus-article-current))
11840         (when gnus-use-cache
11841           (gnus-cache-update-article    
11842            (car gnus-article-current) (cdr gnus-article-current)))
11843         (when gnus-keep-backlog
11844           (gnus-backlog-remove-article 
11845            (car gnus-article-current) (cdr gnus-article-current))))
11846       (save-excursion
11847         (when (get-buffer gnus-original-article-buffer)
11848           (set-buffer gnus-original-article-buffer)
11849           (setq gnus-original-article nil)))
11850       (setq gnus-article-current nil
11851             gnus-current-article nil)
11852       (run-hooks 'gnus-article-display-hook)
11853       (and (gnus-visual-p 'summary-highlight 'highlight)
11854            (run-hooks 'gnus-visual-mark-article-hook)))))
11855
11856 (defun gnus-summary-edit-article-postpone ()
11857   "Postpone changes to the current article."
11858   (interactive)
11859   (gnus-article-mode)
11860   (use-local-map gnus-article-mode-map)
11861   (setq buffer-read-only t)
11862   (buffer-disable-undo (current-buffer))
11863   (gnus-configure-windows 'summary)
11864   (and (gnus-visual-p 'summary-highlight 'highlight)
11865        (run-hooks 'gnus-visual-mark-article-hook)))
11866
11867 (defun gnus-summary-respool-query ()
11868   "Query where the respool algorithm would put this article."
11869   (interactive)
11870   (gnus-set-global-variables)
11871   (gnus-summary-select-article)
11872   (save-excursion
11873     (set-buffer gnus-article-buffer)
11874     (save-restriction
11875       (goto-char (point-min))
11876       (search-forward "\n\n")
11877       (narrow-to-region (point-min) (point))
11878       (pp-eval-expression
11879        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11880
11881 ;; Summary marking commands.
11882
11883 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11884   "Mark articles which has the same subject as read, and then select the next.
11885 If UNMARK is positive, remove any kind of mark.
11886 If UNMARK is negative, tick articles."
11887   (interactive "P")
11888   (gnus-set-global-variables)
11889   (if unmark
11890       (setq unmark (prefix-numeric-value unmark)))
11891   (let ((count
11892          (gnus-summary-mark-same-subject
11893           (gnus-summary-article-subject) unmark)))
11894     ;; Select next unread article.  If auto-select-same mode, should
11895     ;; select the first unread article.
11896     (gnus-summary-next-article t (and gnus-auto-select-same
11897                                       (gnus-summary-article-subject)))
11898     (gnus-message 7 "%d article%s marked as %s"
11899                   count (if (= count 1) " is" "s are")
11900                   (if unmark "unread" "read"))))
11901
11902 (defun gnus-summary-kill-same-subject (&optional unmark)
11903   "Mark articles which has the same subject as read.
11904 If UNMARK is positive, remove any kind of mark.
11905 If UNMARK is negative, tick articles."
11906   (interactive "P")
11907   (gnus-set-global-variables)
11908   (if unmark
11909       (setq unmark (prefix-numeric-value unmark)))
11910   (let ((count
11911          (gnus-summary-mark-same-subject
11912           (gnus-summary-article-subject) unmark)))
11913     ;; If marked as read, go to next unread subject.
11914     (if (null unmark)
11915         ;; Go to next unread subject.
11916         (gnus-summary-next-subject 1 t))
11917     (gnus-message 7 "%d articles are marked as %s"
11918                   count (if unmark "unread" "read"))))
11919
11920 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11921   "Mark articles with same SUBJECT as read, and return marked number.
11922 If optional argument UNMARK is positive, remove any kinds of marks.
11923 If optional argument UNMARK is negative, mark articles as unread instead."
11924   (let ((count 1))
11925     (save-excursion
11926       (cond
11927        ((null unmark)                   ; Mark as read.
11928         (while (and
11929                 (progn
11930                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11931                   (gnus-summary-show-thread) t)
11932                 (gnus-summary-find-subject subject))
11933           (setq count (1+ count))))
11934        ((> unmark 0)                    ; Tick.
11935         (while (and
11936                 (progn
11937                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11938                   (gnus-summary-show-thread) t)
11939                 (gnus-summary-find-subject subject))
11940           (setq count (1+ count))))
11941        (t                               ; Mark as unread.
11942         (while (and
11943                 (progn
11944                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11945                   (gnus-summary-show-thread) t)
11946                 (gnus-summary-find-subject subject))
11947           (setq count (1+ count)))))
11948       (gnus-set-mode-line 'summary)
11949       ;; Return the number of marked articles.
11950       count)))
11951
11952 (defun gnus-summary-mark-as-processable (n &optional unmark)
11953   "Set the process mark on the next N articles.
11954 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11955 the process mark instead.  The difference between N and the actual
11956 number of articles marked is returned."
11957   (interactive "p")
11958   (gnus-set-global-variables)
11959   (let ((backward (< n 0))
11960         (n (abs n)))
11961     (while (and
11962             (> n 0)
11963             (if unmark
11964                 (gnus-summary-remove-process-mark
11965                  (gnus-summary-article-number))
11966               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11967             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11968       (setq n (1- n)))
11969     (if (/= 0 n) (gnus-message 7 "No more articles"))
11970     (gnus-summary-recenter)
11971     (gnus-summary-position-point)
11972     n))
11973
11974 (defun gnus-summary-unmark-as-processable (n)
11975   "Remove the process mark from the next N articles.
11976 If N is negative, mark backward instead.  The difference between N and
11977 the actual number of articles marked is returned."
11978   (interactive "p")
11979   (gnus-set-global-variables)
11980   (gnus-summary-mark-as-processable n t))
11981
11982 (defun gnus-summary-unmark-all-processable ()
11983   "Remove the process mark from all articles."
11984   (interactive)
11985   (gnus-set-global-variables)
11986   (save-excursion
11987     (while gnus-newsgroup-processable
11988       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11989   (gnus-summary-position-point))
11990
11991 (defun gnus-summary-mark-as-expirable (n)
11992   "Mark N articles forward as expirable.
11993 If N is negative, mark backward instead.  The difference between N and
11994 the actual number of articles marked is returned."
11995   (interactive "p")
11996   (gnus-set-global-variables)
11997   (gnus-summary-mark-forward n gnus-expirable-mark))
11998
11999 (defun gnus-summary-mark-article-as-replied (article)
12000   "Mark ARTICLE replied and update the summary line."
12001   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
12002   (let ((buffer-read-only nil))
12003     (when (gnus-summary-goto-subject article)
12004       (gnus-summary-update-secondary-mark article))))
12005
12006 (defun gnus-summary-set-bookmark (article)
12007   "Set a bookmark in current article."
12008   (interactive (list (gnus-summary-article-number)))
12009   (gnus-set-global-variables)
12010   (if (or (not (get-buffer gnus-article-buffer))
12011           (not gnus-current-article)
12012           (not gnus-article-current)
12013           (not (equal gnus-newsgroup-name (car gnus-article-current))))
12014       (error "No current article selected"))
12015   ;; Remove old bookmark, if one exists.
12016   (let ((old (assq article gnus-newsgroup-bookmarks)))
12017     (if old (setq gnus-newsgroup-bookmarks
12018                   (delq old gnus-newsgroup-bookmarks))))
12019   ;; Set the new bookmark, which is on the form
12020   ;; (article-number . line-number-in-body).
12021   (setq gnus-newsgroup-bookmarks
12022         (cons
12023          (cons article
12024                (save-excursion
12025                  (set-buffer gnus-article-buffer)
12026                  (count-lines
12027                   (min (point)
12028                        (save-excursion
12029                          (goto-char (point-min))
12030                          (search-forward "\n\n" nil t)
12031                          (point)))
12032                   (point))))
12033          gnus-newsgroup-bookmarks))
12034   (gnus-message 6 "A bookmark has been added to the current article."))
12035
12036 (defun gnus-summary-remove-bookmark (article)
12037   "Remove the bookmark from the current article."
12038   (interactive (list (gnus-summary-article-number)))
12039   (gnus-set-global-variables)
12040   ;; Remove old bookmark, if one exists.
12041   (let ((old (assq article gnus-newsgroup-bookmarks)))
12042     (if old
12043         (progn
12044           (setq gnus-newsgroup-bookmarks
12045                 (delq old gnus-newsgroup-bookmarks))
12046           (gnus-message 6 "Removed bookmark."))
12047       (gnus-message 6 "No bookmark in current article."))))
12048
12049 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12050 (defun gnus-summary-mark-as-dormant (n)
12051   "Mark N articles forward as dormant.
12052 If N is negative, mark backward instead.  The difference between N and
12053 the actual number of articles marked is returned."
12054   (interactive "p")
12055   (gnus-set-global-variables)
12056   (gnus-summary-mark-forward n gnus-dormant-mark))
12057
12058 (defun gnus-summary-set-process-mark (article)
12059   "Set the process mark on ARTICLE and update the summary line."
12060   (setq gnus-newsgroup-processable
12061         (cons article
12062               (delq article gnus-newsgroup-processable)))
12063   (when (gnus-summary-goto-subject article)
12064     (gnus-summary-show-thread)
12065     (gnus-summary-update-secondary-mark article)))
12066
12067 (defun gnus-summary-remove-process-mark (article)
12068   "Remove the process mark from ARTICLE and update the summary line."
12069   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
12070   (when (gnus-summary-goto-subject article)
12071     (gnus-summary-show-thread)
12072     (gnus-summary-update-secondary-mark article)))
12073
12074 (defun gnus-summary-set-saved-mark (article)
12075   "Set the process mark on ARTICLE and update the summary line."
12076   (push article gnus-newsgroup-saved)
12077   (when (gnus-summary-goto-subject article)
12078     (gnus-summary-update-secondary-mark article)))
12079
12080 (defun gnus-summary-mark-forward (n &optional mark no-expire)
12081   "Mark N articles as read forwards.
12082 If N is negative, mark backwards instead.
12083 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
12084 marked as unread.
12085 The difference between N and the actual number of articles marked is
12086 returned."
12087   (interactive "p")
12088   (gnus-set-global-variables)
12089   (let ((backward (< n 0))
12090         (gnus-summary-goto-unread
12091          (and gnus-summary-goto-unread
12092               (not (eq gnus-summary-goto-unread 'never))
12093               (not (memq mark (list gnus-unread-mark
12094                                     gnus-ticked-mark gnus-dormant-mark)))))
12095         (n (abs n))
12096         (mark (or mark gnus-del-mark)))
12097     (while (and (> n 0)
12098                 (gnus-summary-mark-article nil mark no-expire)
12099                 (zerop (gnus-summary-next-subject
12100                         (if backward -1 1)
12101                         (and gnus-summary-goto-unread
12102                              (not (eq gnus-summary-goto-unread 'never)))
12103                         t)))
12104       (setq n (1- n)))
12105     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12106     (gnus-summary-recenter)
12107     (gnus-summary-position-point)
12108     (gnus-set-mode-line 'summary)
12109     n))
12110
12111 (defun gnus-summary-mark-article-as-read (mark)
12112   "Mark the current article quickly as read with MARK."
12113   (let ((article (gnus-summary-article-number)))
12114     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12115     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12116     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12117     (setq gnus-newsgroup-reads
12118           (cons (cons article mark) gnus-newsgroup-reads))
12119     ;; Possibly remove from cache, if that is used.
12120     (and gnus-use-cache (gnus-cache-enter-remove-article article))
12121     ;; Allow the backend to change the mark.
12122     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
12123     ;; Check for auto-expiry.
12124     (when (and gnus-newsgroup-auto-expire
12125                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12126                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12127                    (= mark gnus-ancient-mark)
12128                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
12129       (setq mark gnus-expirable-mark)
12130       (push article gnus-newsgroup-expirable))
12131     ;; Set the mark in the buffer.
12132     (gnus-summary-update-mark mark 'unread)
12133     t))
12134
12135 (defun gnus-summary-mark-article-as-unread (mark)
12136   "Mark the current article quickly as unread with MARK."
12137   (let ((article (gnus-summary-article-number)))
12138     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12139     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12140     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12141     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
12142     (cond ((= mark gnus-ticked-mark)
12143            (push article gnus-newsgroup-marked))
12144           ((= mark gnus-dormant-mark)
12145            (push article gnus-newsgroup-dormant))
12146           (t
12147            (push article gnus-newsgroup-unreads)))
12148     (setq gnus-newsgroup-reads
12149           (delq (assq article gnus-newsgroup-reads)
12150                 gnus-newsgroup-reads))
12151
12152     ;; See whether the article is to be put in the cache.
12153     (and gnus-use-cache
12154          (vectorp (gnus-summary-article-header article))
12155          (save-excursion
12156            (gnus-cache-possibly-enter-article
12157             gnus-newsgroup-name article
12158             (gnus-summary-article-header article)
12159             (= mark gnus-ticked-mark)
12160             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12161
12162     ;; Fix the mark.
12163     (gnus-summary-update-mark mark 'unread)
12164     t))
12165
12166 (defun gnus-summary-mark-article (&optional article mark no-expire)
12167   "Mark ARTICLE with MARK.  MARK can be any character.
12168 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12169 `??' (dormant) and `?E' (expirable).
12170 If MARK is nil, then the default character `?D' is used.
12171 If ARTICLE is nil, then the article on the current line will be
12172 marked."
12173   ;; The mark might be a string.
12174   (and (stringp mark)
12175        (setq mark (aref mark 0)))
12176   ;; If no mark is given, then we check auto-expiring.
12177   (and (not no-expire)
12178        gnus-newsgroup-auto-expire
12179        (or (not mark)
12180            (and (numberp mark)
12181                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12182                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12183                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12184        (setq mark gnus-expirable-mark))
12185   (let* ((mark (or mark gnus-del-mark))
12186          (article (or article (gnus-summary-article-number))))
12187     (or article (error "No article on current line"))
12188     (if (or (= mark gnus-unread-mark)
12189             (= mark gnus-ticked-mark)
12190             (= mark gnus-dormant-mark))
12191         (gnus-mark-article-as-unread article mark)
12192       (gnus-mark-article-as-read article mark))
12193
12194     ;; See whether the article is to be put in the cache.
12195     (and gnus-use-cache
12196          (not (= mark gnus-canceled-mark))
12197          (vectorp (gnus-summary-article-header article))
12198          (save-excursion
12199            (gnus-cache-possibly-enter-article
12200             gnus-newsgroup-name article
12201             (gnus-summary-article-header article)
12202             (= mark gnus-ticked-mark)
12203             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12204
12205     (if (gnus-summary-goto-subject article nil t)
12206         (let ((buffer-read-only nil))
12207           (gnus-summary-show-thread)
12208           ;; Fix the mark.
12209           (gnus-summary-update-mark mark 'unread)
12210           t))))
12211
12212 (defun gnus-summary-update-secondary-mark (article)
12213   "Update the secondary (read, process, cache) mark."
12214   (gnus-summary-update-mark
12215    (cond ((memq article gnus-newsgroup-processable)
12216           gnus-process-mark)
12217          ((memq article gnus-newsgroup-cached)
12218           gnus-cached-mark)
12219          ((memq article gnus-newsgroup-replied)
12220           gnus-replied-mark)
12221          ((memq article gnus-newsgroup-saved)
12222           gnus-saved-mark)
12223          (t gnus-unread-mark))
12224    'replied)
12225   (when (gnus-visual-p 'summary-highlight 'highlight)
12226     (run-hooks 'gnus-summary-update-hook))
12227   t)
12228
12229 (defun gnus-summary-update-mark (mark type)
12230   (beginning-of-line)
12231   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12232         (buffer-read-only nil))
12233     (when (and forward
12234                (<= (+ forward (point)) (point-max)))
12235       ;; Go to the right position on the line.
12236       (goto-char (+ forward (point)))
12237       ;; Replace the old mark with the new mark.
12238       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12239       ;; Optionally update the marks by some user rule.
12240       (when (eq type 'unread)
12241         (gnus-data-set-mark
12242          (gnus-data-find (gnus-summary-article-number)) mark)
12243         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12244
12245 (defun gnus-mark-article-as-read (article &optional mark)
12246   "Enter ARTICLE in the pertinent lists and remove it from others."
12247   ;; Make the article expirable.
12248   (let ((mark (or mark gnus-del-mark)))
12249     (if (= mark gnus-expirable-mark)
12250         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12251       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12252     ;; Remove from unread and marked lists.
12253     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12254     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12255     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12256     (push (cons article mark) gnus-newsgroup-reads)
12257     ;; Possibly remove from cache, if that is used.
12258     (when gnus-use-cache
12259       (gnus-cache-enter-remove-article article))))
12260
12261 (defun gnus-mark-article-as-unread (article &optional mark)
12262   "Enter ARTICLE in the pertinent lists and remove it from others."
12263   (let ((mark (or mark gnus-ticked-mark)))
12264     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12265     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12266     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12267     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12268     (cond ((= mark gnus-ticked-mark)
12269            (push article gnus-newsgroup-marked))
12270           ((= mark gnus-dormant-mark)
12271            (push article gnus-newsgroup-dormant))
12272           (t
12273            (push article gnus-newsgroup-unreads)))
12274     (setq gnus-newsgroup-reads
12275           (delq (assq article gnus-newsgroup-reads)
12276                 gnus-newsgroup-reads))))
12277
12278 (defalias 'gnus-summary-mark-as-unread-forward
12279   'gnus-summary-tick-article-forward)
12280 (make-obsolete 'gnus-summary-mark-as-unread-forward
12281                'gnus-summary-tick-article-forward)
12282 (defun gnus-summary-tick-article-forward (n)
12283   "Tick N articles forwards.
12284 If N is negative, tick backwards instead.
12285 The difference between N and the number of articles ticked is returned."
12286   (interactive "p")
12287   (gnus-summary-mark-forward n gnus-ticked-mark))
12288
12289 (defalias 'gnus-summary-mark-as-unread-backward
12290   'gnus-summary-tick-article-backward)
12291 (make-obsolete 'gnus-summary-mark-as-unread-backward
12292                'gnus-summary-tick-article-backward)
12293 (defun gnus-summary-tick-article-backward (n)
12294   "Tick N articles backwards.
12295 The difference between N and the number of articles ticked is returned."
12296   (interactive "p")
12297   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12298
12299 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12300 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12301 (defun gnus-summary-tick-article (&optional article clear-mark)
12302   "Mark current article as unread.
12303 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12304 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12305   (interactive)
12306   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12307                                        gnus-ticked-mark)))
12308
12309 (defun gnus-summary-mark-as-read-forward (n)
12310   "Mark N articles as read forwards.
12311 If N is negative, mark backwards instead.
12312 The difference between N and the actual number of articles marked is
12313 returned."
12314   (interactive "p")
12315   (gnus-summary-mark-forward n gnus-del-mark t))
12316
12317 (defun gnus-summary-mark-as-read-backward (n)
12318   "Mark the N articles as read backwards.
12319 The difference between N and the actual number of articles marked is
12320 returned."
12321   (interactive "p")
12322   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12323
12324 (defun gnus-summary-mark-as-read (&optional article mark)
12325   "Mark current article as read.
12326 ARTICLE specifies the article to be marked as read.
12327 MARK specifies a string to be inserted at the beginning of the line."
12328   (gnus-summary-mark-article article mark))
12329
12330 (defun gnus-summary-clear-mark-forward (n)
12331   "Clear marks from N articles forward.
12332 If N is negative, clear backward instead.
12333 The difference between N and the number of marks cleared is returned."
12334   (interactive "p")
12335   (gnus-summary-mark-forward n gnus-unread-mark))
12336
12337 (defun gnus-summary-clear-mark-backward (n)
12338   "Clear marks from N articles backward.
12339 The difference between N and the number of marks cleared is returned."
12340   (interactive "p")
12341   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12342
12343 (defun gnus-summary-mark-unread-as-read ()
12344   "Intended to be used by `gnus-summary-mark-article-hook'."
12345   (when (memq gnus-current-article gnus-newsgroup-unreads)
12346     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12347
12348 (defun gnus-summary-mark-read-and-unread-as-read ()
12349   "Intended to be used by `gnus-summary-mark-article-hook'."
12350   (let ((mark (gnus-summary-article-mark)))
12351     (when (or (gnus-unread-mark-p mark)
12352               (gnus-read-mark-p mark))
12353       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12354
12355 (defun gnus-summary-mark-region-as-read (point mark all)
12356   "Mark all unread articles between point and mark as read.
12357 If given a prefix, mark all articles between point and mark as read,
12358 even ticked and dormant ones."
12359   (interactive "r\nP")
12360   (save-excursion
12361     (let (article)
12362       (goto-char point)
12363       (beginning-of-line)
12364       (while (and
12365               (< (point) mark)
12366               (progn
12367                 (when (or all
12368                           (memq (setq article (gnus-summary-article-number))
12369                                 gnus-newsgroup-unreads))
12370                   (gnus-summary-mark-article article gnus-del-mark))
12371                 t)
12372               (gnus-summary-find-next))))))
12373
12374 (defun gnus-summary-mark-below (score mark)
12375   "Mark articles with score less than SCORE with MARK."
12376   (interactive "P\ncMark: ")
12377   (gnus-set-global-variables)
12378   (setq score (if score
12379                   (prefix-numeric-value score)
12380                 (or gnus-summary-default-score 0)))
12381   (save-excursion
12382     (set-buffer gnus-summary-buffer)
12383     (goto-char (point-min))
12384     (while 
12385         (progn
12386           (and (< (gnus-summary-article-score) score)
12387                (gnus-summary-mark-article nil mark))
12388           (gnus-summary-find-next)))))
12389
12390 (defun gnus-summary-kill-below (&optional score)
12391   "Mark articles with score below SCORE as read."
12392   (interactive "P")
12393   (gnus-set-global-variables)
12394   (gnus-summary-mark-below score gnus-killed-mark))
12395
12396 (defun gnus-summary-clear-above (&optional score)
12397   "Clear all marks from articles with score above SCORE."
12398   (interactive "P")
12399   (gnus-set-global-variables)
12400   (gnus-summary-mark-above score gnus-unread-mark))
12401
12402 (defun gnus-summary-tick-above (&optional score)
12403   "Tick all articles with score above SCORE."
12404   (interactive "P")
12405   (gnus-set-global-variables)
12406   (gnus-summary-mark-above score gnus-ticked-mark))
12407
12408 (defun gnus-summary-mark-above (score mark)
12409   "Mark articles with score over SCORE with MARK."
12410   (interactive "P\ncMark: ")
12411   (gnus-set-global-variables)
12412   (setq score (if score
12413                   (prefix-numeric-value score)
12414                 (or gnus-summary-default-score 0)))
12415   (save-excursion
12416     (set-buffer gnus-summary-buffer)
12417     (goto-char (point-min))
12418     (while (and (progn
12419                   (if (> (gnus-summary-article-score) score)
12420                       (gnus-summary-mark-article nil mark))
12421                   t)
12422                 (gnus-summary-find-next)))))
12423
12424 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12425 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12426 (defun gnus-summary-limit-include-expunged ()
12427   "Display all the hidden articles that were expunged for low scores."
12428   (interactive)
12429   (gnus-set-global-variables)
12430   (let ((buffer-read-only nil))
12431     (let ((scored gnus-newsgroup-scored)
12432           headers h)
12433       (while scored
12434         (or (gnus-summary-goto-subject (caar scored))
12435             (and (setq h (gnus-summary-article-header (caar scored)))
12436                  (< (cdar scored) gnus-summary-expunge-below)
12437                  (setq headers (cons h headers))))
12438         (setq scored (cdr scored)))
12439       (or headers (error "No expunged articles hidden."))
12440       (goto-char (point-min))
12441       (gnus-summary-prepare-unthreaded (nreverse headers)))
12442     (goto-char (point-min))
12443     (gnus-summary-position-point)))
12444
12445 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12446   "Mark all articles not marked as unread in this newsgroup as read.
12447 If prefix argument ALL is non-nil, all articles are marked as read.
12448 If QUIETLY is non-nil, no questions will be asked.
12449 If TO-HERE is non-nil, it should be a point in the buffer.  All
12450 articles before this point will be marked as read.
12451 The number of articles marked as read is returned."
12452   (interactive "P")
12453   (gnus-set-global-variables)
12454   (prog1
12455       (if (or quietly
12456               (not gnus-interactive-catchup) ;Without confirmation?
12457               gnus-expert-user
12458               (gnus-y-or-n-p
12459                (if all
12460                    "Mark absolutely all articles as read? "
12461                  "Mark all unread articles as read? ")))
12462           (if (and not-mark
12463                    (not gnus-newsgroup-adaptive)
12464                    (not gnus-newsgroup-auto-expire))
12465               (progn
12466                 (when all
12467                   (setq gnus-newsgroup-marked nil
12468                         gnus-newsgroup-dormant nil))
12469                 (setq gnus-newsgroup-unreads nil))
12470             ;; We actually mark all articles as canceled, which we
12471             ;; have to do when using auto-expiry or adaptive scoring.
12472             (gnus-summary-show-all-threads)
12473             (if (gnus-summary-first-subject (not all))
12474                 (while (and
12475                         (if to-here (< (point) to-here) t)
12476                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12477                         (gnus-summary-find-next (not all)))))
12478             (unless to-here
12479               (setq gnus-newsgroup-unreads nil))
12480             (gnus-set-mode-line 'summary)))
12481     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12482       (if (and (not to-here) (eq 'nnvirtual (car method)))
12483           (nnvirtual-catchup-group
12484            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12485     (gnus-summary-position-point)))
12486
12487 (defun gnus-summary-catchup-to-here (&optional all)
12488   "Mark all unticked articles before the current one as read.
12489 If ALL is non-nil, also mark ticked and dormant articles as read."
12490   (interactive "P")
12491   (gnus-set-global-variables)
12492   (save-excursion
12493     (gnus-save-hidden-threads
12494       (let ((beg (point)))
12495         ;; We check that there are unread articles.
12496         (when (or all (gnus-summary-find-prev))
12497           (gnus-summary-catchup all t beg)))))
12498   (gnus-summary-position-point))
12499
12500 (defun gnus-summary-catchup-all (&optional quietly)
12501   "Mark all articles in this newsgroup as read."
12502   (interactive "P")
12503   (gnus-set-global-variables)
12504   (gnus-summary-catchup t quietly))
12505
12506 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12507   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12508 If prefix argument ALL is non-nil, all articles are marked as read."
12509   (interactive "P")
12510   (gnus-set-global-variables)
12511   (gnus-summary-catchup all quietly nil 'fast)
12512   ;; Select next newsgroup or exit.
12513   (if (eq gnus-auto-select-next 'quietly)
12514       (gnus-summary-next-group nil)
12515     (gnus-summary-exit)))
12516
12517 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12518   "Mark all articles in this newsgroup as read, and then exit."
12519   (interactive "P")
12520   (gnus-set-global-variables)
12521   (gnus-summary-catchup-and-exit t quietly))
12522
12523 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12524 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12525   "Mark all articles in this group as read and select the next group.
12526 If given a prefix, mark all articles, unread as well as ticked, as
12527 read."
12528   (interactive "P")
12529   (gnus-set-global-variables)
12530   (save-excursion
12531     (gnus-summary-catchup all))
12532   (gnus-summary-next-article t nil nil t))
12533
12534 ;; Thread-based commands.
12535
12536 (defun gnus-summary-articles-in-thread (&optional article)
12537   "Return a list of all articles in the current thread.
12538 If ARTICLE is non-nil, return all articles in the thread that starts
12539 with that article."
12540   (let* ((article (or article (gnus-summary-article-number)))
12541          (data (gnus-data-find-list article))
12542          (top-level (gnus-data-level (car data)))
12543          (top-subject
12544           (cond ((null gnus-thread-operation-ignore-subject)
12545                  (gnus-simplify-subject-re
12546                   (mail-header-subject (gnus-data-header (car data)))))
12547                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12548                  (gnus-simplify-subject-fuzzy
12549                   (mail-header-subject (gnus-data-header (car data)))))
12550                 (t nil)))
12551          (end-point (save-excursion
12552                       (if (gnus-summary-go-to-next-thread) 
12553                           (point) (point-max))))
12554          articles)
12555     (while (and data
12556                 (< (gnus-data-pos (car data)) end-point))
12557       (when (or (not top-subject)
12558                 (string= top-subject
12559                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12560                              (gnus-simplify-subject-fuzzy
12561                               (mail-header-subject
12562                                (gnus-data-header (car data))))
12563                            (gnus-simplify-subject-re
12564                             (mail-header-subject
12565                              (gnus-data-header (car data)))))))
12566         (push (gnus-data-number (car data)) articles))
12567       (unless (and (setq data (cdr data))
12568                    (> (gnus-data-level (car data)) top-level))
12569         (setq data nil)))
12570     ;; Return the list of articles.
12571     (nreverse articles)))
12572
12573 (defun gnus-summary-rethread-current ()
12574   "Rethread the thread the current article is part of."
12575   (interactive)
12576   (gnus-set-global-variables)
12577   (let* ((gnus-show-threads t)
12578          (article (gnus-summary-article-number))
12579          (id (mail-header-id (gnus-summary-article-header)))
12580          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12581     (unless id
12582       (error "No article on the current line"))
12583     (gnus-rebuild-thread id)
12584     (gnus-summary-goto-subject article)))
12585
12586 (defun gnus-summary-reparent-thread ()
12587   "Make current article child of the marked (or previous) article.
12588
12589 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12590 is non-nil or the Subject: of both articles are the same."
12591   (interactive)
12592   (or (not (gnus-group-read-only-p))
12593       (error "The current newsgroup does not support article editing."))
12594   (or (<= (length gnus-newsgroup-processable) 1)
12595       (error "No more than one article may be marked."))
12596   (save-window-excursion
12597     (let ((gnus-article-buffer " *reparent*")
12598           (current-article (gnus-summary-article-number))
12599           ; first grab the marked article, otherwise one line up.
12600           (parent-article (if (not (null gnus-newsgroup-processable))
12601                               (car gnus-newsgroup-processable)
12602                             (save-excursion
12603                               (if (eq (forward-line -1) 0)
12604                                   (gnus-summary-article-number)
12605                                 (error "Beginning of summary buffer."))))))
12606       (or (not (eq current-article parent-article))
12607           (error "An article may not be self-referential."))
12608       (let ((message-id (mail-header-id 
12609                          (gnus-summary-article-header parent-article))))
12610         (or (and message-id (not (equal message-id "")))
12611             (error "No message-id in desired parent."))
12612         (gnus-summary-select-article t t nil current-article)
12613         (set-buffer gnus-article-buffer)
12614         (setq buffer-read-only nil)
12615         (let ((buf (format "%s" (buffer-string))))
12616           (erase-buffer)
12617           (insert buf))
12618         (goto-char (point-min))
12619         (if (search-forward-regexp "^References: " nil t)
12620             (insert message-id " " )
12621           (insert "References: " message-id "\n"))
12622         (or (gnus-request-replace-article current-article
12623                                           (car gnus-article-current)
12624                                           gnus-article-buffer)
12625             (error "Couldn't replace article."))
12626         (set-buffer gnus-summary-buffer)
12627         (gnus-summary-unmark-all-processable)
12628         (gnus-summary-rethread-current)
12629         (gnus-message 3 "Article %d is now the child of article %d."
12630                       current-article parent-article)))))
12631
12632 (defun gnus-summary-toggle-threads (&optional arg)
12633   "Toggle showing conversation threads.
12634 If ARG is positive number, turn showing conversation threads on."
12635   (interactive "P")
12636   (gnus-set-global-variables)
12637   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12638     (setq gnus-show-threads
12639           (if (null arg) (not gnus-show-threads)
12640             (> (prefix-numeric-value arg) 0)))
12641     (gnus-summary-prepare)
12642     (gnus-summary-goto-subject current)
12643     (gnus-summary-position-point)))
12644
12645 (defun gnus-summary-show-all-threads ()
12646   "Show all threads."
12647   (interactive)
12648   (gnus-set-global-variables)
12649   (save-excursion
12650     (let ((buffer-read-only nil))
12651       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12652   (gnus-summary-position-point))
12653
12654 (defun gnus-summary-show-thread ()
12655   "Show thread subtrees.
12656 Returns nil if no thread was there to be shown."
12657   (interactive)
12658   (gnus-set-global-variables)
12659   (let ((buffer-read-only nil)
12660         (orig (point))
12661         ;; first goto end then to beg, to have point at beg after let
12662         (end (progn (end-of-line) (point)))
12663         (beg (progn (beginning-of-line) (point))))
12664     (prog1
12665         ;; Any hidden lines here?
12666         (search-forward "\r" end t)
12667       (subst-char-in-region beg end ?\^M ?\n t)
12668       (goto-char orig)
12669       (gnus-summary-position-point))))
12670
12671 (defun gnus-summary-hide-all-threads ()
12672   "Hide all thread subtrees."
12673   (interactive)
12674   (gnus-set-global-variables)
12675   (save-excursion
12676     (goto-char (point-min))
12677     (gnus-summary-hide-thread)
12678     (while (zerop (gnus-summary-next-thread 1 t))
12679       (gnus-summary-hide-thread)))
12680   (gnus-summary-position-point))
12681
12682 (defun gnus-summary-hide-thread ()
12683   "Hide thread subtrees.
12684 Returns nil if no threads were there to be hidden."
12685   (interactive)
12686   (gnus-set-global-variables)
12687   (let ((buffer-read-only nil)
12688         (start (point))
12689         (article (gnus-summary-article-number)))
12690     (goto-char start)
12691     ;; Go forward until either the buffer ends or the subthread
12692     ;; ends.
12693     (when (and (not (eobp))
12694                (or (zerop (gnus-summary-next-thread 1 t))
12695                    (goto-char (point-max))))
12696       (prog1
12697           (if (and (> (point) start)
12698                    (search-backward "\n" start t))
12699               (progn
12700                 (subst-char-in-region start (point) ?\n ?\^M)
12701                 (gnus-summary-goto-subject article))
12702             (goto-char start)
12703             nil)
12704         ;;(gnus-summary-position-point)
12705         ))))
12706
12707 (defun gnus-summary-go-to-next-thread (&optional previous)
12708   "Go to the same level (or less) next thread.
12709 If PREVIOUS is non-nil, go to previous thread instead.
12710 Return the article number moved to, or nil if moving was impossible."
12711   (let ((level (gnus-summary-thread-level))
12712         (way (if previous -1 1))
12713         (beg (point)))
12714     (forward-line way)
12715     (while (and (not (eobp))
12716                 (< level (gnus-summary-thread-level)))
12717       (forward-line way))
12718     (if (eobp)
12719         (progn
12720           (goto-char beg)
12721           nil)
12722       (setq beg (point))
12723       (prog1
12724           (gnus-summary-article-number)
12725         (goto-char beg)))))
12726
12727 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12728   "Go to the same level (or less) next thread.
12729 If PREVIOUS is non-nil, go to previous thread instead.
12730 Return the article number moved to, or nil if moving was impossible."
12731   (if (and (eq gnus-summary-make-false-root 'dummy)
12732            (gnus-summary-article-intangible-p))
12733       (let ((beg (point)))
12734         (while (and (zerop (forward-line 1))
12735                     (not (gnus-summary-article-intangible-p))
12736                     (not (zerop (save-excursion 
12737                                   (gnus-summary-thread-level))))))
12738         (if (eobp)
12739             (progn
12740               (goto-char beg)
12741               nil)
12742           (point)))
12743     (let* ((level (gnus-summary-thread-level))
12744            (article (gnus-summary-article-number))
12745            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12746            oart)
12747       (while data
12748         (if (<= (gnus-data-level (car data)) level)
12749             (setq oart (gnus-data-number (car data))
12750                   data nil)
12751           (setq data (cdr data))))
12752       (and oart
12753            (gnus-summary-goto-subject oart)))))
12754
12755 (defun gnus-summary-next-thread (n &optional silent)
12756   "Go to the same level next N'th thread.
12757 If N is negative, search backward instead.
12758 Returns the difference between N and the number of skips actually
12759 done.
12760
12761 If SILENT, don't output messages."
12762   (interactive "p")
12763   (gnus-set-global-variables)
12764   (let ((backward (< n 0))
12765         (n (abs n))
12766         old dum int)
12767     (while (and (> n 0)
12768                 (gnus-summary-go-to-next-thread backward))
12769       (decf n))
12770     (unless silent 
12771       (gnus-summary-position-point))
12772     (when (and (not silent) (/= 0 n))
12773       (gnus-message 7 "No more threads"))
12774     n))
12775
12776 (defun gnus-summary-prev-thread (n)
12777   "Go to the same level previous N'th thread.
12778 Returns the difference between N and the number of skips actually
12779 done."
12780   (interactive "p")
12781   (gnus-set-global-variables)
12782   (gnus-summary-next-thread (- n)))
12783
12784 (defun gnus-summary-go-down-thread ()
12785   "Go down one level in the current thread."
12786   (let ((children (gnus-summary-article-children)))
12787     (and children
12788          (gnus-summary-goto-subject (car children)))))
12789
12790 (defun gnus-summary-go-up-thread ()
12791   "Go up one level in the current thread."
12792   (let ((parent (gnus-summary-article-parent)))
12793     (and parent
12794          (gnus-summary-goto-subject parent))))
12795
12796 (defun gnus-summary-down-thread (n)
12797   "Go down thread N steps.
12798 If N is negative, go up instead.
12799 Returns the difference between N and how many steps down that were
12800 taken."
12801   (interactive "p")
12802   (gnus-set-global-variables)
12803   (let ((up (< n 0))
12804         (n (abs n)))
12805     (while (and (> n 0)
12806                 (if up (gnus-summary-go-up-thread)
12807                   (gnus-summary-go-down-thread)))
12808       (setq n (1- n)))
12809     (gnus-summary-position-point)
12810     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12811     n))
12812
12813 (defun gnus-summary-up-thread (n)
12814   "Go up thread N steps.
12815 If N is negative, go up instead.
12816 Returns the difference between N and how many steps down that were
12817 taken."
12818   (interactive "p")
12819   (gnus-set-global-variables)
12820   (gnus-summary-down-thread (- n)))
12821
12822 (defun gnus-summary-top-thread ()
12823   "Go to the top of the thread."
12824   (interactive)
12825   (gnus-set-global-variables)
12826   (while (gnus-summary-go-up-thread))
12827   (gnus-summary-article-number))
12828
12829 (defun gnus-summary-kill-thread (&optional unmark)
12830   "Mark articles under current thread as read.
12831 If the prefix argument is positive, remove any kinds of marks.
12832 If the prefix argument is negative, tick articles instead."
12833   (interactive "P")
12834   (gnus-set-global-variables)
12835   (if unmark
12836       (setq unmark (prefix-numeric-value unmark)))
12837   (let ((articles (gnus-summary-articles-in-thread)))
12838     (save-excursion
12839       ;; Expand the thread.
12840       (gnus-summary-show-thread)
12841       ;; Mark all the articles.
12842       (while articles
12843         (gnus-summary-goto-subject (car articles))
12844         (cond ((null unmark)
12845                (gnus-summary-mark-article-as-read gnus-killed-mark))
12846               ((> unmark 0)
12847                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12848               (t
12849                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12850         (setq articles (cdr articles))))
12851     ;; Hide killed subtrees.
12852     (and (null unmark)
12853          gnus-thread-hide-killed
12854          (gnus-summary-hide-thread))
12855     ;; If marked as read, go to next unread subject.
12856     (if (null unmark)
12857         ;; Go to next unread subject.
12858         (gnus-summary-next-subject 1 t)))
12859   (gnus-set-mode-line 'summary))
12860
12861 ;; Summary sorting commands
12862
12863 (defun gnus-summary-sort-by-number (&optional reverse)
12864   "Sort summary buffer by article number.
12865 Argument REVERSE means reverse order."
12866   (interactive "P")
12867   (gnus-summary-sort 'number reverse))
12868
12869 (defun gnus-summary-sort-by-author (&optional reverse)
12870   "Sort summary buffer by author name alphabetically.
12871 If case-fold-search is non-nil, case of letters is ignored.
12872 Argument REVERSE means reverse order."
12873   (interactive "P")
12874   (gnus-summary-sort 'author reverse))
12875
12876 (defun gnus-summary-sort-by-subject (&optional reverse)
12877   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12878 If case-fold-search is non-nil, case of letters is ignored.
12879 Argument REVERSE means reverse order."
12880   (interactive "P")
12881   (gnus-summary-sort 'subject reverse))
12882
12883 (defun gnus-summary-sort-by-date (&optional reverse)
12884   "Sort summary buffer by date.
12885 Argument REVERSE means reverse order."
12886   (interactive "P")
12887   (gnus-summary-sort 'date reverse))
12888
12889 (defun gnus-summary-sort-by-score (&optional reverse)
12890   "Sort summary buffer by score.
12891 Argument REVERSE means reverse order."
12892   (interactive "P")
12893   (gnus-summary-sort 'score reverse))
12894
12895 (defun gnus-summary-sort (predicate reverse)
12896   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12897   (gnus-set-global-variables)
12898   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12899          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12900          (gnus-thread-sort-functions
12901           (list
12902            (if (not reverse)
12903                thread
12904              `(lambda (t1 t2)
12905                 (,thread t2 t1)))))
12906          (gnus-article-sort-functions
12907           (list
12908            (if (not reverse)
12909                article
12910              `(lambda (t1 t2)
12911                 (,article t2 t1)))))
12912          (buffer-read-only)
12913          (gnus-summary-prepare-hook nil))
12914     ;; We do the sorting by regenerating the threads.
12915     (gnus-summary-prepare)
12916     ;; Hide subthreads if needed.
12917     (when (and gnus-show-threads gnus-thread-hide-subtree)
12918       (gnus-summary-hide-all-threads)))
12919   ;; If in async mode, we send some info to the backend.
12920   (when gnus-newsgroup-async
12921     (gnus-request-asynchronous
12922      gnus-newsgroup-name gnus-newsgroup-data)))
12923
12924 (defun gnus-sortable-date (date)
12925   "Make sortable string by string-lessp from DATE.
12926 Timezone package is used."
12927   (condition-case ()
12928       (progn
12929         (setq date (inline (timezone-fix-time 
12930                             date nil 
12931                             (aref (inline (timezone-parse-date date)) 4))))
12932         (inline
12933           (timezone-make-sortable-date
12934            (aref date 0) (aref date 1) (aref date 2)
12935            (inline
12936              (timezone-make-time-string
12937               (aref date 3) (aref date 4) (aref date 5))))))
12938     (error "")))
12939   
12940 ;; Summary saving commands.
12941
12942 (defun gnus-summary-save-article (&optional n not-saved)
12943   "Save the current article using the default saver function.
12944 If N is a positive number, save the N next articles.
12945 If N is a negative number, save the N previous articles.
12946 If N is nil and any articles have been marked with the process mark,
12947 save those articles instead.
12948 The variable `gnus-default-article-saver' specifies the saver function."
12949   (interactive "P")
12950   (gnus-set-global-variables)
12951   (let ((articles (gnus-summary-work-articles n))
12952         (save-buffer (save-excursion 
12953                        (nnheader-set-temp-buffer " *Gnus Save*")))
12954         file header article)
12955     (while articles
12956       (setq header (gnus-summary-article-header
12957                     (setq article (pop articles))))
12958       (if (not (vectorp header))
12959           ;; This is a pseudo-article.
12960           (if (assq 'name header)
12961               (gnus-copy-file (cdr (assq 'name header)))
12962             (gnus-message 1 "Article %d is unsaveable" article))
12963         ;; This is a real article.
12964         (save-window-excursion
12965           (gnus-summary-select-article t nil nil article))
12966         (save-excursion
12967           (set-buffer save-buffer)
12968           (erase-buffer)
12969           (insert-buffer-substring gnus-original-article-buffer))
12970         (unless gnus-save-all-headers
12971           ;; Remove headers accoring to `gnus-saved-headers'.
12972           (let ((gnus-visible-headers
12973                  (or gnus-saved-headers gnus-visible-headers))
12974                 (gnus-article-buffer save-buffer))
12975             (gnus-article-hide-headers 1 t)))
12976         (save-window-excursion
12977           (if (not gnus-default-article-saver)
12978               (error "No default saver is defined.")
12979             ;; !!! Magic!  The saving functions all save
12980             ;; `gnus-original-article-buffer' (or so they think),
12981             ;; but we bind that variable to our save-buffer.
12982             (set-buffer gnus-article-buffer)
12983             (let ((gnus-original-article-buffer save-buffer))
12984               (set-buffer gnus-summary-buffer)
12985               (setq file (funcall
12986                           gnus-default-article-saver
12987                           (cond
12988                            ((not gnus-prompt-before-saving)
12989                             'default)
12990                            ((eq gnus-prompt-before-saving 'always)
12991                             nil)
12992                            (t file)))))))
12993         (gnus-summary-remove-process-mark article)
12994         (unless not-saved
12995           (gnus-summary-set-saved-mark article))))
12996     (gnus-kill-buffer save-buffer)
12997     (gnus-summary-position-point)
12998     n))
12999
13000 (defun gnus-summary-pipe-output (&optional arg)
13001   "Pipe the current article to a subprocess.
13002 If N is a positive number, pipe the N next articles.
13003 If N is a negative number, pipe the N previous articles.
13004 If N is nil and any articles have been marked with the process mark,
13005 pipe those articles instead."
13006   (interactive "P")
13007   (gnus-set-global-variables)
13008   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
13009     (gnus-summary-save-article arg t))
13010   (gnus-configure-windows 'pipe))
13011
13012 (defun gnus-summary-save-article-mail (&optional arg)
13013   "Append the current article to an mail file.
13014 If N is a positive number, save the N next articles.
13015 If N is a negative number, save the N previous articles.
13016 If N is nil and any articles have been marked with the process mark,
13017 save those articles instead."
13018   (interactive "P")
13019   (gnus-set-global-variables)
13020   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
13021     (gnus-summary-save-article arg)))
13022
13023 (defun gnus-summary-save-article-rmail (&optional arg)
13024   "Append the current article to an rmail file.
13025 If N is a positive number, save the N next articles.
13026 If N is a negative number, save the N previous articles.
13027 If N is nil and any articles have been marked with the process mark,
13028 save those articles instead."
13029   (interactive "P")
13030   (gnus-set-global-variables)
13031   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
13032     (gnus-summary-save-article arg)))
13033
13034 (defun gnus-summary-save-article-file (&optional arg)
13035   "Append the current article to a file.
13036 If N is a positive number, save the N next articles.
13037 If N is a negative number, save the N previous articles.
13038 If N is nil and any articles have been marked with the process mark,
13039 save those articles instead."
13040   (interactive "P")
13041   (gnus-set-global-variables)
13042   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
13043     (gnus-summary-save-article arg)))
13044
13045 (defun gnus-summary-save-article-body-file (&optional arg)
13046   "Append the current article body to a file.
13047 If N is a positive number, save the N next articles.
13048 If N is a negative number, save the N previous articles.
13049 If N is nil and any articles have been marked with the process mark,
13050 save those articles instead."
13051   (interactive "P")
13052   (gnus-set-global-variables)
13053   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
13054     (gnus-summary-save-article arg)))
13055
13056 (defun gnus-get-split-value (methods)
13057   "Return a value based on the split METHODS."
13058   (let (split-name method result match)
13059     (when methods
13060       (save-excursion
13061         (set-buffer gnus-original-article-buffer)
13062         (save-restriction
13063           (nnheader-narrow-to-headers)
13064           (while methods
13065             (goto-char (point-min))
13066             (setq method (pop methods))
13067             (setq match (car method))
13068             (when (cond
13069                    ((stringp match)
13070                     ;; Regular expression.
13071                     (condition-case ()
13072                         (re-search-forward match nil t)
13073                       (error nil)))
13074                    ((gnus-functionp match)
13075                     ;; Function.
13076                     (save-restriction
13077                       (widen)
13078                       (setq result (funcall match gnus-newsgroup-name))))
13079                    ((consp match)
13080                     ;; Form.
13081                     (save-restriction
13082                       (widen)
13083                       (setq result (eval match)))))
13084               (setq split-name (append (cdr method) split-name))
13085               (cond ((stringp result)
13086                      (push result split-name))
13087                     ((consp result)
13088                      (setq split-name (append result split-name)))))))))
13089     split-name))
13090
13091 (defun gnus-read-move-group-name (prompt default articles prefix)
13092   "Read a group name."
13093   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
13094          (minibuffer-confirm-incomplete nil) ; XEmacs
13095          group-map
13096          (dum (mapatoms
13097                (lambda (g) 
13098                  (and (boundp g)
13099                       (symbol-name g)
13100                       (memq 'respool
13101                             (assoc (symbol-name
13102                                     (car (gnus-find-method-for-group
13103                                           (symbol-name g))))
13104                                    gnus-valid-select-methods))
13105                       (push (list (symbol-name g)) group-map)))
13106                gnus-active-hashtb))
13107          (prom
13108           (format "%s %s to:"
13109                   prompt
13110                   (if (> (length articles) 1)
13111                       (format "these %d articles" (length articles))
13112                     "this article")))
13113          (to-newsgroup
13114           (cond
13115            ((null split-name)
13116             (gnus-completing-read default prom
13117                                   group-map nil nil prefix
13118                                   'gnus-group-history))
13119            ((= 1 (length split-name))
13120             (gnus-completing-read (car split-name) prom group-map
13121                                   nil nil nil
13122                                   'gnus-group-history))
13123            (t
13124             (gnus-completing-read nil prom 
13125                                   (mapcar (lambda (el) (list el))
13126                                           (nreverse split-name))
13127                                   nil nil nil
13128                                   'gnus-group-history)))))
13129     (when to-newsgroup
13130       (if (or (string= to-newsgroup "")
13131               (string= to-newsgroup prefix))
13132           (setq to-newsgroup (or default "")))
13133       (or (gnus-active to-newsgroup)
13134           (gnus-activate-group to-newsgroup)
13135           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
13136                                      to-newsgroup))
13137               (or (gnus-request-create-group to-newsgroup)
13138                   (error "Couldn't create group %s" to-newsgroup)))
13139           (error "No such group: %s" to-newsgroup)))
13140     to-newsgroup))
13141
13142 (defun gnus-read-save-file-name (prompt default-name)
13143   (let* ((split-name (gnus-get-split-value gnus-split-methods))
13144          (file
13145           ;; Let the split methods have their say.
13146           (cond
13147            ;; No split name was found.
13148            ((null split-name)
13149             (read-file-name
13150              (concat prompt " (default "
13151                      (file-name-nondirectory default-name) ") ")
13152              (file-name-directory default-name)
13153              default-name))
13154            ;; A single split name was found
13155            ((= 1 (length split-name))
13156             (read-file-name
13157              (concat prompt " (default " (car split-name) ") ")
13158              gnus-article-save-directory
13159              (concat gnus-article-save-directory (car split-name))))
13160            ;; A list of splits was found.
13161            (t
13162             (setq split-name (nreverse split-name))
13163             (let (result)
13164               (let ((file-name-history (nconc split-name file-name-history)))
13165                 (setq result
13166                       (read-file-name
13167                        (concat prompt " (`M-p' for defaults) ")
13168                        gnus-article-save-directory
13169                        (car split-name))))
13170               (car (push result file-name-history)))))))
13171     ;; If we have read a directory, we append the default file name.
13172     (when (file-directory-p file)
13173       (setq file (concat (file-name-as-directory file)
13174                          (file-name-nondirectory default-name))))
13175     ;; Possibly translate some charaters.
13176     (nnheader-translate-file-chars file)))
13177
13178 (defun gnus-article-archive-name (group)
13179   "Return the first instance of an \"Archive-name\" in the current buffer."
13180   (let ((case-fold-search t))
13181     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13182       (match-string 1))))
13183
13184 (defun gnus-summary-save-in-rmail (&optional filename)
13185   "Append this article to Rmail file.
13186 Optional argument FILENAME specifies file name.
13187 Directory to save to is default to `gnus-article-save-directory'."
13188   (interactive)
13189   (gnus-set-global-variables)
13190   (let ((default-name
13191           (funcall gnus-rmail-save-name gnus-newsgroup-name
13192                    gnus-current-headers gnus-newsgroup-last-rmail)))
13193     (setq filename
13194           (cond ((eq filename 'default)
13195                  default-name)
13196                 (filename filename)
13197                 (t (gnus-read-save-file-name
13198                     "Save in rmail file:" default-name))))
13199     (gnus-make-directory (file-name-directory filename))
13200     (gnus-eval-in-buffer-window gnus-original-article-buffer
13201       (save-excursion
13202         (save-restriction
13203           (widen)
13204           (gnus-output-to-rmail filename))))
13205     ;; Remember the directory name to save articles
13206     (setq gnus-newsgroup-last-rmail filename)))
13207
13208 (defun gnus-summary-save-in-mail (&optional filename)
13209   "Append this article to Unix mail file.
13210 Optional argument FILENAME specifies file name.
13211 Directory to save to is default to `gnus-article-save-directory'."
13212   (interactive)
13213   (gnus-set-global-variables)
13214   (let ((default-name
13215           (funcall gnus-mail-save-name gnus-newsgroup-name
13216                    gnus-current-headers gnus-newsgroup-last-mail)))
13217     (setq filename
13218           (cond ((eq filename 'default)
13219                  default-name)
13220                 (filename filename)
13221                 (t (gnus-read-save-file-name
13222                     "Save in Unix mail file:" default-name))))
13223     (setq filename
13224           (expand-file-name filename
13225                             (and default-name
13226                                  (file-name-directory default-name))))
13227     (gnus-make-directory (file-name-directory filename))
13228     (gnus-eval-in-buffer-window gnus-original-article-buffer
13229       (save-excursion
13230         (save-restriction
13231           (widen)
13232           (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13233               (gnus-output-to-rmail filename)
13234             (let ((mail-use-rfc822 t))
13235               (rmail-output filename 1 t t))))))
13236     ;; Remember the directory name to save articles.
13237     (setq gnus-newsgroup-last-mail filename)))
13238
13239 (defun gnus-summary-save-in-file (&optional filename)
13240   "Append this article to file.
13241 Optional argument FILENAME specifies file name.
13242 Directory to save to is default to `gnus-article-save-directory'."
13243   (interactive)
13244   (gnus-set-global-variables)
13245   (let ((default-name
13246           (funcall gnus-file-save-name gnus-newsgroup-name
13247                    gnus-current-headers gnus-newsgroup-last-file)))
13248     (setq filename
13249           (cond ((eq filename 'default)
13250                  default-name)
13251                 (filename filename)
13252                 (t (gnus-read-save-file-name
13253                     "Save in file:" default-name))))
13254     (gnus-make-directory (file-name-directory filename))
13255     (gnus-eval-in-buffer-window gnus-original-article-buffer
13256       (save-excursion
13257         (save-restriction
13258           (widen)
13259           (gnus-output-to-file filename))))
13260     ;; Remember the directory name to save articles.
13261     (setq gnus-newsgroup-last-file filename)))
13262
13263 (defun gnus-summary-save-body-in-file (&optional filename)
13264   "Append this article body to a file.
13265 Optional argument FILENAME specifies file name.
13266 The directory to save in defaults to `gnus-article-save-directory'."
13267   (interactive)
13268   (gnus-set-global-variables)
13269   (let ((default-name
13270           (funcall gnus-file-save-name gnus-newsgroup-name
13271                    gnus-current-headers gnus-newsgroup-last-file)))
13272     (setq filename
13273           (cond ((eq filename 'default)
13274                  default-name)
13275                 (filename filename)
13276                 (t (gnus-read-save-file-name
13277                     "Save body in file:" default-name))))
13278     (gnus-make-directory (file-name-directory filename))
13279     (gnus-eval-in-buffer-window gnus-article-buffer
13280       (save-excursion
13281         (save-restriction
13282           (widen)
13283           (goto-char (point-min))
13284           (and (search-forward "\n\n" nil t)
13285                (narrow-to-region (point) (point-max)))
13286           (gnus-output-to-file filename))))
13287     ;; Remember the directory name to save articles.
13288     (setq gnus-newsgroup-last-file filename)))
13289
13290 (defun gnus-summary-save-in-pipe (&optional command)
13291   "Pipe this article to subprocess."
13292   (interactive)
13293   (gnus-set-global-variables)
13294   (setq command
13295         (cond ((eq command 'default)
13296                gnus-last-shell-command)
13297               (command command)
13298               (t (read-string "Shell command on article: "
13299                               gnus-last-shell-command))))
13300   (if (string-equal command "")
13301       (setq command gnus-last-shell-command))
13302   (gnus-eval-in-buffer-window gnus-article-buffer
13303     (save-restriction
13304       (widen)
13305       (shell-command-on-region (point-min) (point-max) command nil)))
13306   (setq gnus-last-shell-command command))
13307
13308 ;; Summary extract commands
13309
13310 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13311   (let ((buffer-read-only nil)
13312         (article (gnus-summary-article-number))
13313         after-article b e)
13314     (or (gnus-summary-goto-subject article)
13315         (error (format "No such article: %d" article)))
13316     (gnus-summary-position-point)
13317     ;; If all commands are to be bunched up on one line, we collect
13318     ;; them here.
13319     (if gnus-view-pseudos-separately
13320         ()
13321       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13322             files action)
13323         (while ps
13324           (setq action (cdr (assq 'action (car ps))))
13325           (setq files (list (cdr (assq 'name (car ps)))))
13326           (while (and ps (cdr ps)
13327                       (string= (or action "1")
13328                                (or (cdr (assq 'action (cadr ps))) "2")))
13329             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13330             (setcdr ps (cddr ps)))
13331           (if (not files)
13332               ()
13333             (if (not (string-match "%s" action))
13334                 (setq files (cons " " files)))
13335             (setq files (cons " " files))
13336             (and (assq 'execute (car ps))
13337                  (setcdr (assq 'execute (car ps))
13338                          (funcall (if (string-match "%s" action)
13339                                       'format 'concat)
13340                                   action
13341                                   (mapconcat (lambda (f) f) files " ")))))
13342           (setq ps (cdr ps)))))
13343     (if (and gnus-view-pseudos (not not-view))
13344         (while pslist
13345           (and (assq 'execute (car pslist))
13346                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13347                                      (eq gnus-view-pseudos 'not-confirm)))
13348           (setq pslist (cdr pslist)))
13349       (save-excursion
13350         (while pslist
13351           (setq after-article (or (cdr (assq 'article (car pslist)))
13352                                   (gnus-summary-article-number)))
13353           (gnus-summary-goto-subject after-article)
13354           (forward-line 1)
13355           (setq b (point))
13356           (insert "    " (file-name-nondirectory
13357                                 (cdr (assq 'name (car pslist))))
13358                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13359           (setq e (point))
13360           (forward-line -1)             ; back to `b'
13361           (gnus-add-text-properties
13362            b (1- e) (list 'gnus-number gnus-reffed-article-number
13363                           gnus-mouse-face-prop gnus-mouse-face))
13364           (gnus-data-enter
13365            after-article gnus-reffed-article-number
13366            gnus-unread-mark b (car pslist) 0 (- e b))
13367           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13368           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13369           (setq pslist (cdr pslist)))))))
13370
13371 (defun gnus-pseudos< (p1 p2)
13372   (let ((c1 (cdr (assq 'action p1)))
13373         (c2 (cdr (assq 'action p2))))
13374     (and c1 c2 (string< c1 c2))))
13375
13376 (defun gnus-request-pseudo-article (props)
13377   (cond ((assq 'execute props)
13378          (gnus-execute-command (cdr (assq 'execute props)))))
13379   (let ((gnus-current-article (gnus-summary-article-number)))
13380     (run-hooks 'gnus-mark-article-hook)))
13381
13382 (defun gnus-execute-command (command &optional automatic)
13383   (save-excursion
13384     (gnus-article-setup-buffer)
13385     (set-buffer gnus-article-buffer)
13386     (setq buffer-read-only nil)
13387     (let ((command (if automatic command (read-string "Command: " command)))
13388           ;; Just binding this here doesn't help, because there might
13389           ;; be output from the process after exiting the scope of 
13390           ;; this `let'.
13391           ;; (buffer-read-only nil)
13392           )
13393       (erase-buffer)
13394       (insert "$ " command "\n\n")
13395       (if gnus-view-pseudo-asynchronously
13396           (start-process "gnus-execute" nil "sh" "-c" command)
13397         (call-process "sh" nil t nil "-c" command)))))
13398
13399 (defun gnus-copy-file (file &optional to)
13400   "Copy FILE to TO."
13401   (interactive
13402    (list (read-file-name "Copy file: " default-directory)
13403          (read-file-name "Copy file to: " default-directory)))
13404   (gnus-set-global-variables)
13405   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13406   (and (file-directory-p to)
13407        (setq to (concat (file-name-as-directory to)
13408                         (file-name-nondirectory file))))
13409   (copy-file file to))
13410
13411 ;; Summary kill commands.
13412
13413 (defun gnus-summary-edit-global-kill (article)
13414   "Edit the \"global\" kill file."
13415   (interactive (list (gnus-summary-article-number)))
13416   (gnus-set-global-variables)
13417   (gnus-group-edit-global-kill article))
13418
13419 (defun gnus-summary-edit-local-kill ()
13420   "Edit a local kill file applied to the current newsgroup."
13421   (interactive)
13422   (gnus-set-global-variables)
13423   (setq gnus-current-headers (gnus-summary-article-header))
13424   (gnus-set-global-variables)
13425   (gnus-group-edit-local-kill
13426    (gnus-summary-article-number) gnus-newsgroup-name))
13427
13428 \f
13429 ;;;
13430 ;;; Gnus article mode
13431 ;;;
13432
13433 (put 'gnus-article-mode 'mode-class 'special)
13434
13435 (if gnus-article-mode-map
13436     nil
13437   (setq gnus-article-mode-map (make-keymap))
13438   (suppress-keymap gnus-article-mode-map)
13439
13440   (gnus-define-keys gnus-article-mode-map
13441     " " gnus-article-goto-next-page
13442     "\177" gnus-article-goto-prev-page
13443     [delete] gnus-article-goto-prev-page
13444     "\C-c^" gnus-article-refer-article
13445     "h" gnus-article-show-summary
13446     "s" gnus-article-show-summary
13447     "\C-c\C-m" gnus-article-mail
13448     "?" gnus-article-describe-briefly
13449     gnus-mouse-2 gnus-article-push-button
13450     "\r" gnus-article-press-button
13451     "\t" gnus-article-next-button
13452     "\M-\t" gnus-article-prev-button
13453     "\C-c\C-b" gnus-bug)
13454
13455   (substitute-key-definition
13456    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13457
13458 (defun gnus-article-mode ()
13459   "Major mode for displaying an article.
13460
13461 All normal editing commands are switched off.
13462
13463 The following commands are available:
13464
13465 \\<gnus-article-mode-map>
13466 \\[gnus-article-next-page]\t Scroll the article one page forwards
13467 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13468 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13469 \\[gnus-article-show-summary]\t Display the summary buffer
13470 \\[gnus-article-mail]\t Send a reply to the address near point
13471 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13472 \\[gnus-info-find-node]\t Go to the Gnus info node"
13473   (interactive)
13474   (when (and menu-bar-mode
13475              (gnus-visual-p 'article-menu 'menu))
13476     (gnus-article-make-menu-bar))
13477   (kill-all-local-variables)
13478   (gnus-simplify-mode-line)
13479   (setq mode-name "Article")
13480   (setq major-mode 'gnus-article-mode)
13481   (make-local-variable 'minor-mode-alist)
13482   (or (assq 'gnus-show-mime minor-mode-alist)
13483       (setq minor-mode-alist
13484             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13485   (use-local-map gnus-article-mode-map)
13486   (make-local-variable 'page-delimiter)
13487   (setq page-delimiter gnus-page-delimiter)
13488   (buffer-disable-undo (current-buffer))
13489   (setq buffer-read-only t)             ;Disable modification
13490   (run-hooks 'gnus-article-mode-hook))
13491
13492 (defun gnus-article-setup-buffer ()
13493   "Initialize the article buffer."
13494   (let* ((name (if gnus-single-article-buffer "*Article*"
13495                  (concat "*Article " gnus-newsgroup-name "*")))
13496          (original
13497           (progn (string-match "\\*Article" name)
13498                  (concat " *Original Article"
13499                          (substring name (match-end 0))))))
13500     (setq gnus-article-buffer name)
13501     (setq gnus-original-article-buffer original)
13502     ;; This might be a variable local to the summary buffer.
13503     (unless gnus-single-article-buffer
13504       (save-excursion
13505         (set-buffer gnus-summary-buffer)
13506         (setq gnus-article-buffer name)
13507         (setq gnus-original-article-buffer original)
13508         (gnus-set-global-variables))
13509       (make-local-variable 'gnus-summary-buffer))
13510     ;; Init original article buffer.
13511     (save-excursion
13512       (set-buffer (get-buffer-create gnus-original-article-buffer))
13513       (buffer-disable-undo (current-buffer))
13514       (setq major-mode 'gnus-original-article-mode)
13515       (make-local-variable 'gnus-original-article))
13516     (if (get-buffer name)
13517         (save-excursion
13518           (set-buffer name)
13519           (buffer-disable-undo (current-buffer))
13520           (setq buffer-read-only t)
13521           (gnus-add-current-to-buffer-list)
13522           (or (eq major-mode 'gnus-article-mode)
13523               (gnus-article-mode))
13524           (current-buffer))
13525       (save-excursion
13526         (set-buffer (get-buffer-create name))
13527         (gnus-add-current-to-buffer-list)
13528         (gnus-article-mode)
13529         (current-buffer)))))
13530
13531 ;; Set article window start at LINE, where LINE is the number of lines
13532 ;; from the head of the article.
13533 (defun gnus-article-set-window-start (&optional line)
13534   (set-window-start
13535    (get-buffer-window gnus-article-buffer t)
13536    (save-excursion
13537      (set-buffer gnus-article-buffer)
13538      (goto-char (point-min))
13539      (if (not line)
13540          (point-min)
13541        (gnus-message 6 "Moved to bookmark")
13542        (search-forward "\n\n" nil t)
13543        (forward-line line)
13544        (point)))))
13545
13546 (defun gnus-kill-all-overlays ()
13547   "Delete all overlays in the current buffer."
13548   (when (fboundp 'overlay-lists)
13549     (let* ((overlayss (overlay-lists))
13550            (buffer-read-only nil)
13551            (overlays (nconc (car overlayss) (cdr overlayss))))
13552       (while overlays
13553         (delete-overlay (pop overlays))))))
13554
13555 (defun gnus-request-article-this-buffer (article group)
13556   "Get an article and insert it into this buffer."
13557   (let (do-update-line)
13558     (prog1
13559         (save-excursion
13560           (erase-buffer)
13561           (gnus-kill-all-overlays)
13562           (setq group (or group gnus-newsgroup-name))
13563
13564           ;; Open server if it has closed.
13565           (gnus-check-server (gnus-find-method-for-group group))
13566
13567           ;; Using `gnus-request-article' directly will insert the article into
13568           ;; `nntp-server-buffer' - so we'll save some time by not having to
13569           ;; copy it from the server buffer into the article buffer.
13570
13571           ;; We only request an article by message-id when we do not have the
13572           ;; headers for it, so we'll have to get those.
13573           (when (stringp article)
13574             (let ((gnus-override-method gnus-refer-article-method))
13575               (gnus-read-header article)))
13576
13577           ;; If the article number is negative, that means that this article
13578           ;; doesn't belong in this newsgroup (possibly), so we find its
13579           ;; message-id and request it by id instead of number.
13580           (when (and (numberp article)
13581                      gnus-summary-buffer
13582                      (get-buffer gnus-summary-buffer)
13583                      (buffer-name (get-buffer gnus-summary-buffer)))
13584             (save-excursion
13585               (set-buffer gnus-summary-buffer)
13586               (let ((header (gnus-summary-article-header article)))
13587                 (if (< article 0)
13588                     (cond 
13589                      ((memq article gnus-newsgroup-sparse)
13590                       ;; This is a sparse gap article.
13591                       (setq do-update-line article)
13592                       (setq article (mail-header-id header))
13593                       (let ((gnus-override-method gnus-refer-article-method))
13594                         (gnus-read-header article))
13595                       (setq gnus-newsgroup-sparse
13596                             (delq article gnus-newsgroup-sparse)))
13597                      ((vectorp header)
13598                       ;; It's a real article.
13599                       (setq article (mail-header-id header)))
13600                      (t
13601                       ;; It is an extracted pseudo-article.
13602                       (setq article 'pseudo)
13603                       (gnus-request-pseudo-article header))))
13604                 
13605                 (let ((method (gnus-find-method-for-group 
13606                                gnus-newsgroup-name)))
13607                   (if (not (eq (car method) 'nneething))
13608                       ()
13609                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13610                                        (mail-header-subject header))))
13611                       (if (file-directory-p dir)
13612                           (progn
13613                             (setq article 'nneething)
13614                             (gnus-group-enter-directory dir)))))))))
13615
13616           (cond
13617            ;; Refuse to select canceled articles.
13618            ((and (numberp article)
13619                  gnus-summary-buffer
13620                  (get-buffer gnus-summary-buffer)
13621                  (buffer-name (get-buffer gnus-summary-buffer))
13622                  (eq (cdr (save-excursion
13623                             (set-buffer gnus-summary-buffer)
13624                             (assq article gnus-newsgroup-reads)))
13625                      gnus-canceled-mark))
13626             nil)
13627            ;; We first check `gnus-original-article-buffer'.
13628            ((and (get-buffer gnus-original-article-buffer)
13629                  (numberp article)
13630                  (save-excursion
13631                    (set-buffer gnus-original-article-buffer)
13632                    (and (equal (car gnus-original-article) group)
13633                         (eq (cdr gnus-original-article) article))))
13634             (insert-buffer-substring gnus-original-article-buffer)
13635             'article)
13636            ;; Check the backlog.
13637            ((and gnus-keep-backlog
13638                  (gnus-backlog-request-article group article (current-buffer)))
13639             'article)
13640            ;; Check the cache.
13641            ((and gnus-use-cache
13642                  (numberp article)
13643                  (gnus-cache-request-article article group))
13644             'article)
13645            ;; Get the article and put into the article buffer.
13646            ((or (stringp article) (numberp article))
13647             (let ((gnus-override-method
13648                    (and (stringp article) gnus-refer-article-method))
13649                   (buffer-read-only nil))
13650               (erase-buffer)
13651               (gnus-kill-all-overlays)
13652               (if (gnus-request-article article group (current-buffer))
13653                   (progn
13654                     (and gnus-keep-backlog
13655                          (numberp article)
13656                          (gnus-backlog-enter-article
13657                           group article (current-buffer)))
13658                     'article))))
13659            ;; It was a pseudo.
13660            (t article)))
13661
13662       ;; Take the article from the original article buffer
13663       ;; and place it in the buffer it's supposed to be in.
13664       (when (and (get-buffer gnus-article-buffer)
13665                  ;;(numberp article)
13666                  (equal (buffer-name (current-buffer))
13667                         (buffer-name (get-buffer gnus-article-buffer))))
13668         (save-excursion
13669           (if (get-buffer gnus-original-article-buffer)
13670               (set-buffer (get-buffer gnus-original-article-buffer))
13671             (set-buffer (get-buffer-create gnus-original-article-buffer))
13672             (buffer-disable-undo (current-buffer))
13673             (setq major-mode 'gnus-original-article-mode)
13674             (setq buffer-read-only t)
13675             (gnus-add-current-to-buffer-list))
13676           (let (buffer-read-only)
13677             (erase-buffer)
13678             (insert-buffer-substring gnus-article-buffer))
13679           (setq gnus-original-article (cons group article))))
13680     
13681       ;; Update sparse articles.
13682       (when (and do-update-line
13683                  (or (numberp article)
13684                      (stringp article)))
13685         (let ((buf (current-buffer)))
13686           (set-buffer gnus-summary-buffer)
13687           (gnus-summary-update-article do-update-line)
13688           (gnus-summary-goto-subject do-update-line nil t)
13689           (set-window-point (get-buffer-window (current-buffer) t)
13690                             (point))
13691           (set-buffer buf))))))
13692
13693 (defun gnus-read-header (id &optional header)
13694   "Read the headers of article ID and enter them into the Gnus system."
13695   (let ((group gnus-newsgroup-name)
13696         (gnus-override-method 
13697          (and (gnus-news-group-p gnus-newsgroup-name)
13698               gnus-refer-article-method))       
13699         where)
13700     ;; First we check to see whether the header in question is already
13701     ;; fetched.
13702     (if (stringp id)
13703         ;; This is a Message-ID.
13704         (setq header (or header (gnus-id-to-header id)))
13705       ;; This is an article number.
13706       (setq header (or header (gnus-summary-article-header id))))
13707     (if (and header
13708              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13709         ;; We have found the header.
13710         header
13711       ;; We have to really fetch the header to this article.
13712       (when (setq where
13713                   (if (gnus-check-backend-function 'request-head group)
13714                       (gnus-request-head id group)
13715                     (gnus-request-article id group)))
13716         (save-excursion
13717           (set-buffer nntp-server-buffer)
13718           (and (search-forward "\n\n" nil t)
13719                (delete-region (1- (point)) (point-max)))
13720           (goto-char (point-max))
13721           (insert ".\n")
13722           (goto-char (point-min))
13723           (insert "211 ")
13724           (princ (cond
13725                   ((numberp id) id)
13726                   ((cdr where) (cdr where))
13727                   (header (mail-header-number header))
13728                   (t gnus-reffed-article-number))
13729                  (current-buffer))
13730           (insert " Article retrieved.\n"))
13731         ;(when (and header
13732         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13733         ;  (setcar (gnus-id-to-thread id) nil))
13734         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13735             ()                          ; Malformed head.
13736           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13737             (if (and (stringp id)
13738                      (not (string= (gnus-group-real-name group)
13739                                    (car where))))
13740                 ;; If we fetched by Message-ID and the article came
13741                 ;; from a different group, we fudge some bogus article
13742                 ;; numbers for this article.
13743                 (mail-header-set-number header gnus-reffed-article-number))
13744             (decf gnus-reffed-article-number)
13745             (gnus-remove-header (mail-header-number header))
13746             (push header gnus-newsgroup-headers)
13747             (setq gnus-current-headers header)
13748             (push (mail-header-number header) gnus-newsgroup-limit))
13749           header)))))
13750
13751 (defun gnus-remove-header (number)
13752   "Remove header NUMBER from `gnus-newsgroup-headers'."
13753   (if (and gnus-newsgroup-headers
13754            (= number (mail-header-number (car gnus-newsgroup-headers))))
13755       (pop gnus-newsgroup-headers)
13756     (let ((headers gnus-newsgroup-headers))
13757       (while (and (cdr headers)
13758                   (not (= number (mail-header-number (cadr headers)))))
13759         (pop headers))
13760       (when (cdr headers)
13761         (setcdr headers (cddr headers))))))
13762
13763 (defun gnus-article-prepare (article &optional all-headers header)
13764   "Prepare ARTICLE in article mode buffer.
13765 ARTICLE should either be an article number or a Message-ID.
13766 If ARTICLE is an id, HEADER should be the article headers.
13767 If ALL-HEADERS is non-nil, no headers are hidden."
13768   (save-excursion
13769     ;; Make sure we start in a summary buffer.
13770     (unless (eq major-mode 'gnus-summary-mode)
13771       (set-buffer gnus-summary-buffer))
13772     (setq gnus-summary-buffer (current-buffer))
13773     ;; Make sure the connection to the server is alive.
13774     (unless (gnus-server-opened
13775              (gnus-find-method-for-group gnus-newsgroup-name))
13776       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13777       (gnus-request-group gnus-newsgroup-name t))
13778     (let* ((article (if header (mail-header-number header) article))
13779            (summary-buffer (current-buffer))
13780            (internal-hook gnus-article-internal-prepare-hook)
13781            (group gnus-newsgroup-name)
13782            result)
13783       (save-excursion
13784         (gnus-article-setup-buffer)
13785         (set-buffer gnus-article-buffer)
13786         ;; Deactivate active regions.
13787         (when (and (boundp 'transient-mark-mode)
13788                    transient-mark-mode)
13789           (setq mark-active nil))
13790         (if (not (setq result (let ((buffer-read-only nil))
13791                                 (gnus-request-article-this-buffer
13792                                  article group))))
13793             ;; There is no such article.
13794             (save-excursion
13795               (when (and (numberp article)
13796                          (not (memq article gnus-newsgroup-sparse)))
13797                 (setq gnus-article-current
13798                       (cons gnus-newsgroup-name article))
13799                 (set-buffer gnus-summary-buffer)
13800                 (setq gnus-current-article article)
13801                 (gnus-summary-mark-article article gnus-canceled-mark))
13802               (unless (memq article gnus-newsgroup-sparse)
13803                 (gnus-error
13804                  1 "No such article (may have expired or been canceled)")))
13805           (if (or (eq result 'pseudo) (eq result 'nneething))
13806               (progn
13807                 (save-excursion
13808                   (set-buffer summary-buffer)
13809                   (setq gnus-last-article gnus-current-article
13810                         gnus-newsgroup-history (cons gnus-current-article
13811                                                      gnus-newsgroup-history)
13812                         gnus-current-article 0
13813                         gnus-current-headers nil
13814                         gnus-article-current nil)
13815                   (if (eq result 'nneething)
13816                       (gnus-configure-windows 'summary)
13817                     (gnus-configure-windows 'article))
13818                   (gnus-set-global-variables))
13819                 (gnus-set-mode-line 'article))
13820             ;; The result from the `request' was an actual article -
13821             ;; or at least some text that is now displayed in the
13822             ;; article buffer.
13823             (if (and (numberp article)
13824                      (not (eq article gnus-current-article)))
13825                 ;; Seems like a new article has been selected.
13826                 ;; `gnus-current-article' must be an article number.
13827                 (save-excursion
13828                   (set-buffer summary-buffer)
13829                   (setq gnus-last-article gnus-current-article
13830                         gnus-newsgroup-history (cons gnus-current-article
13831                                                      gnus-newsgroup-history)
13832                         gnus-current-article article
13833                         gnus-current-headers
13834                         (gnus-summary-article-header gnus-current-article)
13835                         gnus-article-current
13836                         (cons gnus-newsgroup-name gnus-current-article))
13837                   (unless (vectorp gnus-current-headers)
13838                     (setq gnus-current-headers nil))
13839                   (gnus-summary-show-thread)
13840                   (run-hooks 'gnus-mark-article-hook)
13841                   (gnus-set-mode-line 'summary)
13842                   (and (gnus-visual-p 'article-highlight 'highlight)
13843                        (run-hooks 'gnus-visual-mark-article-hook))
13844                   ;; Set the global newsgroup variables here.
13845                   ;; Suggested by Jim Sisolak
13846                   ;; <sisolak@trans4.neep.wisc.edu>.
13847                   (gnus-set-global-variables)
13848                   (setq gnus-have-all-headers
13849                         (or all-headers gnus-show-all-headers))
13850                   (and gnus-use-cache
13851                        (vectorp (gnus-summary-article-header article))
13852                        (gnus-cache-possibly-enter-article
13853                         group article
13854                         (gnus-summary-article-header article)
13855                         (memq article gnus-newsgroup-marked)
13856                         (memq article gnus-newsgroup-dormant)
13857                         (memq article gnus-newsgroup-unreads)))))
13858             (when (or (numberp article)
13859                       (stringp article))
13860               ;; Hooks for getting information from the article.
13861               ;; This hook must be called before being narrowed.
13862               (let (buffer-read-only)
13863                 (run-hooks 'internal-hook)
13864                 (run-hooks 'gnus-article-prepare-hook)
13865                 ;; Decode MIME message.
13866                 (if gnus-show-mime
13867                     (if (or (not gnus-strict-mime)
13868                             (gnus-fetch-field "Mime-Version"))
13869                         (funcall gnus-show-mime-method)
13870                       (funcall gnus-decode-encoded-word-method)))
13871                 ;; Perform the article display hooks.
13872                 (run-hooks 'gnus-article-display-hook))
13873               ;; Do page break.
13874               (goto-char (point-min))
13875               (and gnus-break-pages (gnus-narrow-to-page)))
13876             (gnus-set-mode-line 'article)
13877             (gnus-configure-windows 'article)
13878             (goto-char (point-min))
13879             t))))))
13880
13881 (defun gnus-article-show-all-headers ()
13882   "Show all article headers in article mode buffer."
13883   (save-excursion
13884     (gnus-article-setup-buffer)
13885     (set-buffer gnus-article-buffer)
13886     (let ((buffer-read-only nil))
13887       (gnus-unhide-text (point-min) (point-max)))))
13888
13889 (defun gnus-article-hide-headers-if-wanted ()
13890   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13891 Provided for backwards compatibility."
13892   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13893       gnus-inhibit-hiding
13894       (gnus-article-hide-headers)))
13895
13896 (defsubst gnus-article-header-rank ()
13897   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13898   (let ((list gnus-sorted-header-list)
13899         (i 0))
13900     (while list
13901       (when (looking-at (car list))
13902         (setq list nil))
13903       (setq list (cdr list))
13904       (incf i))
13905     i))
13906
13907 (defun gnus-article-hide-headers (&optional arg delete)
13908   "Toggle whether to hide unwanted headers and possibly sort them as well.
13909 If given a negative prefix, always show; if given a positive prefix,
13910 always hide."
13911   (interactive (gnus-hidden-arg))
13912   (if (gnus-article-check-hidden-text 'headers arg)
13913       ;; Show boring headers as well.
13914       (gnus-article-show-hidden-text 'boring-headers)
13915     ;; This function might be inhibited.
13916     (unless gnus-inhibit-hiding
13917       (save-excursion
13918         (set-buffer gnus-article-buffer)
13919         (save-restriction
13920           (let ((buffer-read-only nil)
13921                 (props (nconc (list 'gnus-type 'headers)
13922                               gnus-hidden-properties))
13923                 (max (1+ (length gnus-sorted-header-list)))
13924                 (ignored (when (not (stringp gnus-visible-headers))
13925                            (cond ((stringp gnus-ignored-headers)
13926                                   gnus-ignored-headers)
13927                                  ((listp gnus-ignored-headers)
13928                                   (mapconcat 'identity gnus-ignored-headers
13929                                              "\\|")))))
13930                 (visible
13931                  (cond ((stringp gnus-visible-headers)
13932                         gnus-visible-headers)
13933                        ((and gnus-visible-headers
13934                              (listp gnus-visible-headers))
13935                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13936                 (inhibit-point-motion-hooks t)
13937                 want-list beg)
13938             ;; First we narrow to just the headers.
13939             (widen)
13940             (goto-char (point-min))
13941             ;; Hide any "From " lines at the beginning of (mail) articles.
13942             (while (looking-at "From ")
13943               (forward-line 1))
13944             (unless (bobp)
13945               (if delete
13946                   (delete-region (point-min) (point))
13947                 (gnus-hide-text (point-min) (point) props)))
13948             ;; Then treat the rest of the header lines.
13949             (narrow-to-region
13950              (point)
13951              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13952             ;; Then we use the two regular expressions
13953             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13954             ;; select which header lines is to remain visible in the
13955             ;; article buffer.
13956             (goto-char (point-min))
13957             (while (re-search-forward "^[^ \t]*:" nil t)
13958               (beginning-of-line)
13959               ;; We add the headers we want to keep to a list and delete
13960               ;; them from the buffer.
13961               (gnus-put-text-property 
13962                (point) (1+ (point)) 'message-rank
13963                (if (or (and visible (looking-at visible))
13964                        (and ignored
13965                             (not (looking-at ignored))))
13966                    (gnus-article-header-rank) 
13967                  (+ 2 max)))
13968               (forward-line 1))
13969             (message-sort-headers-1)
13970             (when (setq beg (text-property-any 
13971                              (point-min) (point-max) 'message-rank (+ 2 max)))
13972               ;; We make the unwanted headers invisible.
13973               (if delete
13974                   (delete-region beg (point-max))
13975                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13976                 (gnus-hide-text-type beg (point-max) 'headers))
13977               ;; Work around XEmacs lossage.
13978               (gnus-put-text-property (point-min) beg 'invisible nil))))))))
13979
13980 (defun gnus-article-hide-boring-headers (&optional arg)
13981   "Toggle hiding of headers that aren't very interesting.
13982 If given a negative prefix, always show; if given a positive prefix,
13983 always hide."
13984   (interactive (gnus-hidden-arg))
13985   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13986     (save-excursion
13987       (set-buffer gnus-article-buffer)
13988       (save-restriction
13989         (let ((buffer-read-only nil)
13990               (list gnus-boring-article-headers)
13991               (inhibit-point-motion-hooks t)
13992               elem)
13993           (nnheader-narrow-to-headers)
13994           (while list
13995             (setq elem (pop list))
13996             (goto-char (point-min))
13997             (cond
13998              ;; Hide empty headers.
13999              ((eq elem 'empty)
14000               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
14001                 (forward-line -1)
14002                 (gnus-hide-text-type
14003                  (progn (beginning-of-line) (point))
14004                  (progn 
14005                    (end-of-line)
14006                    (if (re-search-forward "^[^ \t]" nil t)
14007                        (match-beginning 0)
14008                      (point-max)))
14009                  'boring-headers)))
14010              ;; Hide boring Newsgroups header.
14011              ((eq elem 'newsgroups)
14012               (when (equal (message-fetch-field "newsgroups")
14013                            (gnus-group-real-name gnus-newsgroup-name))
14014                 (gnus-article-hide-header "newsgroups")))
14015              ((eq elem 'followup-to)
14016               (when (equal (message-fetch-field "followup-to")
14017                            (message-fetch-field "newsgroups"))
14018                 (gnus-article-hide-header "followup-to")))
14019              ((eq elem 'reply-to)
14020               (let ((from (message-fetch-field "from"))
14021                     (reply-to (message-fetch-field "reply-to")))
14022                 (when (and
14023                        from reply-to
14024                        (equal 
14025                         (nth 1 (funcall gnus-extract-address-components from))
14026                         (nth 1 (funcall gnus-extract-address-components
14027                                         reply-to))))
14028                   (gnus-article-hide-header "reply-to"))))
14029              ((eq elem 'date)
14030               (let ((date (message-fetch-field "date")))
14031                 (when (and date
14032                            (< (gnus-days-between date (current-time-string))
14033                               4))
14034                   (gnus-article-hide-header "date")))))))))))
14035
14036 (defun gnus-article-hide-header (header)
14037   (save-excursion
14038     (goto-char (point-min))
14039     (when (re-search-forward (concat "^" header ":") nil t)
14040       (gnus-hide-text-type
14041        (progn (beginning-of-line) (point))
14042        (progn 
14043          (end-of-line)
14044          (if (re-search-forward "^[^ \t]" nil t)
14045              (match-beginning 0)
14046            (point-max)))
14047        'boring-headers))))
14048
14049 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
14050 (defun gnus-article-treat-overstrike ()
14051   "Translate overstrikes into bold text."
14052   (interactive)
14053   (save-excursion
14054     (set-buffer gnus-article-buffer)
14055     (let ((buffer-read-only nil))
14056       (while (search-forward "\b" nil t)
14057         (let ((next (following-char))
14058               (previous (char-after (- (point) 2))))
14059           (cond 
14060            ((eq next previous)
14061             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14062             (gnus-put-text-property (point) (1+ (point)) 'face 'bold))
14063            ((eq next ?_)
14064             (gnus-put-text-property (1- (point)) (1+ (point)) 'invisible t)
14065             (gnus-put-text-property
14066              (- (point) 2) (1- (point)) 'face 'underline))
14067            ((eq previous ?_)
14068             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14069             (gnus-put-text-property
14070              (point) (1+ (point))       'face 'underline))))))))
14071
14072 (defun gnus-article-word-wrap ()
14073   "Format too long lines."
14074   (interactive)
14075   (save-excursion
14076     (set-buffer gnus-article-buffer)
14077     (let ((buffer-read-only nil))
14078       (widen)
14079       (goto-char (point-min))
14080       (search-forward "\n\n" nil t)
14081       (end-of-line 1)
14082       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
14083             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
14084             (adaptive-fill-mode t))
14085         (while (not (eobp))
14086           (and (>= (current-column) (min fill-column (window-width)))
14087                (/= (preceding-char) ?:)
14088                (fill-paragraph nil))
14089           (end-of-line 2))))))
14090
14091 (defun gnus-article-remove-cr ()
14092   "Remove carriage returns from an article."
14093   (interactive)
14094   (save-excursion
14095     (set-buffer gnus-article-buffer)
14096     (let ((buffer-read-only nil))
14097       (goto-char (point-min))
14098       (while (search-forward "\r" nil t)
14099         (replace-match "" t t)))))
14100
14101 (defun gnus-article-remove-trailing-blank-lines ()
14102   "Remove all trailing blank lines from the article."
14103   (interactive)
14104   (save-excursion
14105     (set-buffer gnus-article-buffer)
14106     (let ((buffer-read-only nil))
14107       (goto-char (point-max))
14108       (delete-region
14109        (point)
14110        (progn
14111          (while (looking-at "^[ \t]*$")
14112            (forward-line -1))
14113          (forward-line 1)
14114          (point))))))
14115
14116 (defun gnus-article-display-x-face (&optional force)
14117   "Look for an X-Face header and display it if present."
14118   (interactive (list 'force))
14119   (save-excursion
14120     (set-buffer gnus-article-buffer)
14121     ;; Delete the old process, if any.
14122     (when (process-status "gnus-x-face")
14123       (delete-process "gnus-x-face"))
14124     (let ((inhibit-point-motion-hooks t)
14125           (case-fold-search nil)
14126           from)
14127       (save-restriction
14128         (nnheader-narrow-to-headers)
14129         (setq from (message-fetch-field "from"))
14130         (goto-char (point-min))
14131         (when (and gnus-article-x-face-command
14132                    (or force
14133                        ;; Check whether this face is censored.
14134                        (not gnus-article-x-face-too-ugly)
14135                        (and gnus-article-x-face-too-ugly from
14136                             (not (string-match gnus-article-x-face-too-ugly
14137                                                from))))
14138                    ;; Has to be present.
14139                    (re-search-forward "^X-Face: " nil t))
14140           ;; We now have the area of the buffer where the X-Face is stored.
14141           (let ((beg (point))
14142                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
14143             ;; We display the face.
14144             (if (symbolp gnus-article-x-face-command)
14145                 ;; The command is a lisp function, so we call it.
14146                 (if (gnus-functionp gnus-article-x-face-command)
14147                     (funcall gnus-article-x-face-command beg end)
14148                   (error "%s is not a function" gnus-article-x-face-command))
14149               ;; The command is a string, so we interpret the command
14150               ;; as a, well, command, and fork it off.
14151               (let ((process-connection-type nil))
14152                 (process-kill-without-query
14153                  (start-process
14154                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
14155                 (process-send-region "gnus-x-face" beg end)
14156                 (process-send-eof "gnus-x-face")))))))))
14157
14158 (defalias 'gnus-headers-decode-quoted-printable 'gnus-decode-rfc1522)
14159 (defun gnus-decode-rfc1522 ()
14160   "Hack to remove QP encoding from headers."
14161   (let ((case-fold-search t)
14162         (inhibit-point-motion-hooks t)
14163         (buffer-read-only nil)
14164         string)
14165     (save-restriction
14166       (narrow-to-region
14167        (goto-char (point-min))
14168        (or (search-forward "\n\n" nil t) (point-max)))
14169
14170       (while (re-search-forward 
14171               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
14172         (setq string (match-string 1))
14173         (narrow-to-region (match-beginning 0) (match-end 0))
14174         (delete-region (point-min) (point-max))
14175         (insert string)
14176         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
14177         (subst-char-in-region (point-min) (point-max) ?_ ? )
14178         (widen)
14179         (goto-char (point-min))))))
14180
14181 (defun gnus-article-de-quoted-unreadable (&optional force)
14182   "Do a naive translation of a quoted-printable-encoded article.
14183 This is in no way, shape or form meant as a replacement for real MIME
14184 processing, but is simply a stop-gap measure until MIME support is
14185 written.
14186 If FORCE, decode the article whether it is marked as quoted-printable
14187 or not."
14188   (interactive (list 'force))
14189   (save-excursion
14190     (set-buffer gnus-article-buffer)
14191     (let ((case-fold-search t)
14192           (buffer-read-only nil)
14193           (type (gnus-fetch-field "content-transfer-encoding")))
14194       (gnus-decode-rfc1522)
14195       (when (or force
14196                 (and type (string-match "quoted-printable" (downcase type))))
14197         (goto-char (point-min))
14198         (search-forward "\n\n" nil 'move)
14199         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14200
14201 (defun gnus-mime-decode-quoted-printable (from to)
14202   "Decode Quoted-Printable in the region between FROM and TO."
14203   (interactive "r")
14204   (goto-char from)
14205   (while (search-forward "=" to t)
14206     (cond ((eq (following-char) ?\n)
14207            (delete-char -1)
14208            (delete-char 1))
14209           ((looking-at "[0-9A-F][0-9A-F]")
14210            (subst-char-in-region
14211             (1- (point)) (point) ?=
14212             (hexl-hex-string-to-integer
14213              (buffer-substring (point) (+ 2 (point)))))
14214            (delete-char 2))
14215           ((looking-at "=")
14216            (delete-char 1))
14217           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14218
14219 (defun gnus-article-hide-pgp (&optional arg)
14220   "Toggle hiding of any PGP headers and signatures in the current article.
14221 If given a negative prefix, always show; if given a positive prefix,
14222 always hide."
14223   (interactive (gnus-hidden-arg))
14224   (unless (gnus-article-check-hidden-text 'pgp arg)
14225     (save-excursion
14226       (set-buffer gnus-article-buffer)
14227       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14228             buffer-read-only beg end)
14229         (widen)
14230         (goto-char (point-min))
14231         ;; Hide the "header".
14232         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14233              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14234         (setq beg (point))
14235         ;; Hide the actual signature.
14236         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14237              (setq end (1+ (match-beginning 0)))
14238              (gnus-hide-text
14239               end
14240               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14241                   (match-end 0)
14242                 ;; Perhaps we shouldn't hide to the end of the buffer
14243                 ;; if there is no end to the signature?
14244                 (point-max))
14245               props))
14246         ;; Hide "- " PGP quotation markers.
14247         (when (and beg end)
14248           (narrow-to-region beg end)
14249           (goto-char (point-min))
14250           (while (re-search-forward "^- " nil t)
14251             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14252           (widen))))))
14253
14254 (defun gnus-article-hide-signature (&optional arg)
14255   "Hide the signature in the current article.
14256 If given a negative prefix, always show; if given a positive prefix,
14257 always hide."
14258   (interactive (gnus-hidden-arg))
14259   (unless (gnus-article-check-hidden-text 'signature arg)
14260     (save-excursion
14261       (set-buffer gnus-article-buffer)
14262       (save-restriction
14263         (let ((buffer-read-only nil))
14264           (when (gnus-narrow-to-signature)
14265             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14266
14267 (defun gnus-article-strip-leading-blank-lines ()
14268   "Remove all blank lines from the beginning of the article."
14269   (interactive)
14270   (save-excursion
14271     (set-buffer gnus-article-buffer)
14272     (let (buffer-read-only)
14273       (goto-char (point-min))
14274       (when (search-forward "\n\n" nil t)
14275         (while (looking-at "[ \t]$")
14276           (gnus-delete-line))))))
14277
14278 (defvar mime::preview/content-list)
14279 (defvar mime::preview-content-info/point-min)
14280 (defun gnus-narrow-to-signature ()
14281   "Narrow to the signature."
14282   (widen)
14283   (if (and (boundp 'mime::preview/content-list)
14284            mime::preview/content-list)
14285       (let ((pcinfo (car (last mime::preview/content-list))))
14286         (condition-case ()
14287             (narrow-to-region
14288              (funcall (intern "mime::preview-content-info/point-min") pcinfo)
14289              (point-max))
14290           (error nil))))
14291   (goto-char (point-max))
14292   (when (re-search-backward gnus-signature-separator nil t)
14293     (forward-line 1)
14294     (when (or (null gnus-signature-limit)
14295               (and (numberp gnus-signature-limit)
14296                    (< (- (point-max) (point)) gnus-signature-limit))
14297               (and (gnus-functionp gnus-signature-limit)
14298                    (funcall gnus-signature-limit))
14299               (and (stringp gnus-signature-limit)
14300                    (not (re-search-forward gnus-signature-limit nil t))))
14301       (narrow-to-region (point) (point-max))
14302       t)))
14303
14304 (defun gnus-hidden-arg ()
14305   "Return the current prefix arg as a number, or 0 if no prefix."
14306   (list (if current-prefix-arg
14307             (prefix-numeric-value current-prefix-arg)
14308           0)))
14309
14310 (defun gnus-article-check-hidden-text (type arg)
14311   "Return nil if hiding is necessary.
14312 Arg can be nil or a number.  Nil and positive means hide, negative
14313 means show, 0 means toggle."
14314   (save-excursion
14315     (set-buffer gnus-article-buffer)
14316     (let ((hide (gnus-article-hidden-text-p type)))
14317       (cond
14318        ((or (null arg)
14319             (> arg 0))
14320         nil)
14321        ((< arg 0)
14322         (gnus-article-show-hidden-text type))
14323        (t
14324         (if (eq hide 'hidden)
14325             (gnus-article-show-hidden-text type)
14326           nil))))))
14327
14328 (defun gnus-article-hidden-text-p (type)
14329   "Say whether the current buffer contains hidden text of type TYPE."
14330   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14331     (when pos
14332       (if (get-text-property pos 'invisible)
14333           'hidden
14334         'shown))))
14335
14336 (defun gnus-article-hide (&optional arg force)
14337   "Hide all the gruft in the current article.
14338 This means that PGP stuff, signatures, cited text and (some)
14339 headers will be hidden.
14340 If given a prefix, show the hidden text instead."
14341   (interactive (list current-prefix-arg 'force))
14342   (gnus-article-hide-headers arg)
14343   (gnus-article-hide-pgp arg)
14344   (gnus-article-hide-citation-maybe arg force)
14345   (gnus-article-hide-signature arg))
14346
14347 (defun gnus-article-show-hidden-text (type &optional hide)
14348   "Show all hidden text of type TYPE.
14349 If HIDE, hide the text instead."
14350   (save-excursion
14351     (set-buffer gnus-article-buffer)
14352     (let ((buffer-read-only nil)
14353           (inhibit-point-motion-hooks t)
14354           (beg (point-min)))
14355       (while (gnus-goto-char (text-property-any
14356                               beg (point-max) 'gnus-type type))
14357         (setq beg (point))
14358         (forward-char)
14359         (if hide
14360             (gnus-hide-text beg (point) gnus-hidden-properties)
14361           (gnus-unhide-text beg (point)))
14362         (setq beg (point)))
14363       t)))
14364
14365 (defvar gnus-article-time-units
14366   `((year . ,(* 365.25 24 60 60))
14367     (week . ,(* 7 24 60 60))
14368     (day . ,(* 24 60 60))
14369     (hour . ,(* 60 60))
14370     (minute . 60)
14371     (second . 1))
14372   "Mapping from time units to seconds.")
14373
14374 (defun gnus-article-date-ut (&optional type highlight)
14375   "Convert DATE date to universal time in the current article.
14376 If TYPE is `local', convert to local time; if it is `lapsed', output
14377 how much time has lapsed since DATE."
14378   (interactive (list 'ut t))
14379   (let* ((header (or gnus-current-headers
14380                      (gnus-summary-article-header) ""))
14381          (date (and (vectorp header) (mail-header-date header)))
14382          (date-regexp "^Date: \\|^X-Sent: ")
14383          (now (current-time))
14384          (inhibit-point-motion-hooks t)
14385          bface eface)
14386     (when (and date (not (string= date "")))
14387       (save-excursion
14388         (set-buffer gnus-article-buffer)
14389         (save-restriction
14390           (nnheader-narrow-to-headers)
14391           (let ((buffer-read-only nil))
14392             ;; Delete any old Date headers.
14393             (if (re-search-forward date-regexp nil t)
14394                 (progn
14395                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14396                         eface (get-text-property (1- (gnus-point-at-eol))
14397                                                  'face))
14398                   (message-remove-header date-regexp t)
14399                   (beginning-of-line))
14400               (goto-char (point-max)))
14401             (insert (gnus-make-date-line date type))
14402             ;; Do highlighting.
14403             (forward-line -1)
14404             (when (and (gnus-visual-p 'article-highlight 'highlight)
14405                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14406               (gnus-put-text-property (match-beginning 1) (match-end 1)
14407                                  'face bface)
14408               (gnus-put-text-property (match-beginning 2) (match-end 2)
14409                                  'face eface))))))))
14410
14411 (defun gnus-make-date-line (date type)
14412   "Return a DATE line of TYPE."
14413   (cond
14414    ;; Convert to the local timezone.  We have to slap a
14415    ;; `condition-case' round the calls to the timezone
14416    ;; functions since they aren't particularly resistant to
14417    ;; buggy dates.
14418    ((eq type 'local)
14419     (concat "Date: " (condition-case ()
14420                          (timezone-make-date-arpa-standard date)
14421                        (error date))
14422             "\n"))
14423    ;; Convert to Universal Time.
14424    ((eq type 'ut)
14425     (concat "Date: "
14426             (condition-case ()
14427                 (timezone-make-date-arpa-standard date nil "UT")
14428               (error date))
14429             "\n"))
14430    ;; Get the original date from the article.
14431    ((eq type 'original)
14432     (concat "Date: " date "\n"))
14433    ;; Do an X-Sent lapsed format.
14434    ((eq type 'lapsed)
14435     ;; If the date is seriously mangled, the timezone
14436     ;; functions are liable to bug out, so we condition-case
14437     ;; the entire thing.
14438     (let* ((now (current-time))
14439            (real-time
14440             (condition-case ()
14441                 (gnus-time-minus
14442                  (gnus-encode-date
14443                   (timezone-make-date-arpa-standard
14444                    (current-time-string now)
14445                    (current-time-zone now) "UT"))
14446                  (gnus-encode-date
14447                   (timezone-make-date-arpa-standard
14448                    date nil "UT")))
14449               (error '(0 0))))
14450            (real-sec (+ (* (float (car real-time)) 65536)
14451                         (cadr real-time)))
14452            (sec (abs real-sec))
14453            num prev)
14454       (cond
14455        ((equal real-time '(0 0))
14456         "X-Sent: Unknown\n")
14457        ((zerop sec)
14458         "X-Sent: Now\n")
14459        (t
14460         (concat
14461          "X-Sent: "
14462          ;; This is a bit convoluted, but basically we go
14463          ;; through the time units for years, weeks, etc,
14464          ;; and divide things to see whether that results
14465          ;; in positive answers.
14466          (mapconcat
14467           (lambda (unit)
14468             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14469                 ;; The (remaining) seconds are too few to
14470                 ;; be divided into this time unit.
14471                 ""
14472               ;; It's big enough, so we output it.
14473               (setq sec (- sec (* num (cdr unit))))
14474               (prog1
14475                   (concat (if prev ", " "") (int-to-string
14476                                              (floor num))
14477                           " " (symbol-name (car unit)) 
14478                           (if (> num 1) "s" ""))
14479                 (setq prev t))))
14480           gnus-article-time-units "")
14481          ;; If dates are odd, then it might appear like the
14482          ;; article was sent in the future.
14483          (if (> real-sec 0)
14484              " ago\n"
14485            " in the future\n"))))))
14486    (t
14487     (error "Unknown conversion type: %s" type))))
14488
14489 (defun gnus-article-date-local (&optional highlight)
14490   "Convert the current article date to the local timezone."
14491   (interactive (list t))
14492   (gnus-article-date-ut 'local highlight))
14493
14494 (defun gnus-article-date-original (&optional highlight)
14495   "Convert the current article date to what it was originally.
14496 This is only useful if you have used some other date conversion
14497 function and want to see what the date was before converting."
14498   (interactive (list t))
14499   (gnus-article-date-ut 'original highlight))
14500
14501 (defun gnus-article-date-lapsed (&optional highlight)
14502   "Convert the current article date to time lapsed since it was sent."
14503   (interactive (list t))
14504   (gnus-article-date-ut 'lapsed highlight))
14505
14506 (defun gnus-article-maybe-highlight ()
14507   "Do some article highlighting if `gnus-visual' is non-nil."
14508   (if (gnus-visual-p 'article-highlight 'highlight)
14509       (gnus-article-highlight-some)))
14510
14511 ;;; Article savers.
14512
14513 (defun gnus-output-to-rmail (file-name)
14514   "Append the current article to an Rmail file named FILE-NAME."
14515   (require 'rmail)
14516   ;; Most of these codes are borrowed from rmailout.el.
14517   (setq file-name (expand-file-name file-name))
14518   (setq rmail-default-rmail-file file-name)
14519   (let ((artbuf (current-buffer))
14520         (tmpbuf (get-buffer-create " *Gnus-output*")))
14521     (save-excursion
14522       (or (get-file-buffer file-name)
14523           (file-exists-p file-name)
14524           (if (gnus-yes-or-no-p
14525                (concat "\"" file-name "\" does not exist, create it? "))
14526               (let ((file-buffer (create-file-buffer file-name)))
14527                 (save-excursion
14528                   (set-buffer file-buffer)
14529                   (rmail-insert-rmail-file-header)
14530                   (let ((require-final-newline nil))
14531                     (write-region (point-min) (point-max) file-name t 1)))
14532                 (kill-buffer file-buffer))
14533             (error "Output file does not exist")))
14534       (set-buffer tmpbuf)
14535       (buffer-disable-undo (current-buffer))
14536       (erase-buffer)
14537       (insert-buffer-substring artbuf)
14538       (gnus-convert-article-to-rmail)
14539       ;; Decide whether to append to a file or to an Emacs buffer.
14540       (let ((outbuf (get-file-buffer file-name)))
14541         (if (not outbuf)
14542             (append-to-file (point-min) (point-max) file-name)
14543           ;; File has been visited, in buffer OUTBUF.
14544           (set-buffer outbuf)
14545           (let ((buffer-read-only nil)
14546                 (msg (and (boundp 'rmail-current-message)
14547                           (symbol-value 'rmail-current-message))))
14548             ;; If MSG is non-nil, buffer is in RMAIL mode.
14549             (if msg
14550                 (progn (widen)
14551                        (narrow-to-region (point-max) (point-max))))
14552             (insert-buffer-substring tmpbuf)
14553             (if msg
14554                 (progn
14555                   (goto-char (point-min))
14556                   (widen)
14557                   (search-backward "\^_")
14558                   (narrow-to-region (point) (point-max))
14559                   (goto-char (1+ (point-min)))
14560                   (rmail-count-new-messages t)
14561                   (rmail-show-message msg)))))))
14562     (kill-buffer tmpbuf)))
14563
14564 (defun gnus-output-to-file (file-name)
14565   "Append the current article to a file named FILE-NAME."
14566   (let ((artbuf (current-buffer)))
14567     (nnheader-temp-write nil
14568       (insert-buffer-substring artbuf)
14569       ;; Append newline at end of the buffer as separator, and then
14570       ;; save it to file.
14571       (goto-char (point-max))
14572       (insert "\n")
14573       (append-to-file (point-min) (point-max) file-name))))
14574
14575 (defun gnus-convert-article-to-rmail ()
14576   "Convert article in current buffer to Rmail message format."
14577   (let ((buffer-read-only nil))
14578     ;; Convert article directly into Babyl format.
14579     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14580     (goto-char (point-min))
14581     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14582     (while (search-forward "\n\^_" nil t) ;single char
14583       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14584     (goto-char (point-max))
14585     (insert "\^_")))
14586
14587 (defun gnus-narrow-to-page (&optional arg)
14588   "Narrow the article buffer to a page.
14589 If given a numerical ARG, move forward ARG pages."
14590   (interactive "P")
14591   (setq arg (if arg (prefix-numeric-value arg) 0))
14592   (save-excursion
14593     (set-buffer gnus-article-buffer)
14594     (goto-char (point-min))
14595     (widen)
14596     (when (gnus-visual-p 'page-marker)
14597       (let ((buffer-read-only nil))
14598         (gnus-remove-text-with-property 'gnus-prev)
14599         (gnus-remove-text-with-property 'gnus-next)))
14600     (when
14601         (cond ((< arg 0)
14602                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14603               ((> arg 0)
14604                (re-search-forward page-delimiter nil 'move arg)))
14605       (goto-char (match-end 0)))
14606     (narrow-to-region
14607      (point)
14608      (if (re-search-forward page-delimiter nil 'move)
14609          (match-beginning 0)
14610        (point)))
14611     (when (and (gnus-visual-p 'page-marker)
14612                (not (= (point-min) 1)))
14613       (save-excursion
14614         (goto-char (point-min))
14615         (gnus-insert-prev-page-button)))
14616     (when (and (gnus-visual-p 'page-marker)
14617                (not (= (1- (point-max)) (buffer-size))))
14618       (save-excursion
14619         (goto-char (point-max))
14620         (gnus-insert-next-page-button)))))
14621
14622 ;; Article mode commands
14623
14624 (defun gnus-article-goto-next-page ()
14625   "Show the next page of the article."
14626   (interactive)
14627   (when (gnus-article-next-page)
14628     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14629
14630 (defun gnus-article-goto-prev-page ()
14631   "Show the next page of the article."
14632   (interactive)
14633   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14634     (gnus-article-prev-page nil)))
14635
14636 (defun gnus-article-next-page (&optional lines)
14637   "Show the next page of the current article.
14638 If end of article, return non-nil.  Otherwise return nil.
14639 Argument LINES specifies lines to be scrolled up."
14640   (interactive "p")
14641   (move-to-window-line -1)
14642   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14643   (if (save-excursion
14644         (end-of-line)
14645         (and (pos-visible-in-window-p)  ;Not continuation line.
14646              (eobp)))
14647       ;; Nothing in this page.
14648       (if (or (not gnus-break-pages)
14649               (save-excursion
14650                 (save-restriction
14651                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14652           t                             ;Nothing more.
14653         (gnus-narrow-to-page 1)         ;Go to next page.
14654         nil)
14655     ;; More in this page.
14656     (condition-case ()
14657         (scroll-up lines)
14658       (end-of-buffer
14659        ;; Long lines may cause an end-of-buffer error.
14660        (goto-char (point-max))))
14661     (move-to-window-line 0)
14662     nil))
14663
14664 (defun gnus-article-prev-page (&optional lines)
14665   "Show previous page of current article.
14666 Argument LINES specifies lines to be scrolled down."
14667   (interactive "p")
14668   (move-to-window-line 0)
14669   (if (and gnus-break-pages
14670            (bobp)
14671            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14672       (progn
14673         (gnus-narrow-to-page -1)        ;Go to previous page.
14674         (goto-char (point-max))
14675         (recenter -1))
14676     (prog1
14677         (condition-case ()
14678             (scroll-down lines)
14679           (error nil))
14680       (move-to-window-line 0))))
14681
14682 (defun gnus-article-refer-article ()
14683   "Read article specified by message-id around point."
14684   (interactive)
14685   (let ((point (point)))
14686     (search-forward ">" nil t)          ;Move point to end of "<....>".
14687     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14688         (let ((message-id (match-string 1)))
14689           (goto-char point)
14690           (set-buffer gnus-summary-buffer)
14691           (gnus-summary-refer-article message-id))
14692       (goto-char (point))
14693       (error "No references around point"))))
14694
14695 (defun gnus-article-show-summary ()
14696   "Reconfigure windows to show summary buffer."
14697   (interactive)
14698   (gnus-configure-windows 'article)
14699   (gnus-summary-goto-subject gnus-current-article))
14700
14701 (defun gnus-article-describe-briefly ()
14702   "Describe article mode commands briefly."
14703   (interactive)
14704   (gnus-message 6
14705                 (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")))
14706
14707 (defun gnus-article-summary-command ()
14708   "Execute the last keystroke in the summary buffer."
14709   (interactive)
14710   (let ((obuf (current-buffer))
14711         (owin (current-window-configuration))
14712         func)
14713     (switch-to-buffer gnus-summary-buffer 'norecord)
14714     (setq func (lookup-key (current-local-map) (this-command-keys)))
14715     (call-interactively func)
14716     (set-buffer obuf)
14717     (set-window-configuration owin)
14718     (set-window-point (get-buffer-window (current-buffer)) (point))))
14719
14720 (defun gnus-article-summary-command-nosave ()
14721   "Execute the last keystroke in the summary buffer."
14722   (interactive)
14723   (let (func)
14724     (pop-to-buffer gnus-summary-buffer 'norecord)
14725     (setq func (lookup-key (current-local-map) (this-command-keys)))
14726     (call-interactively func)))
14727
14728 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14729   "Read a summary buffer key sequence and execute it from the article buffer."
14730   (interactive "P")
14731   (let ((nosaves
14732          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14733            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14734            "=" "^" "\M-^" "|"))
14735         keys)
14736     (save-excursion
14737       (set-buffer gnus-summary-buffer)
14738       (push (or key last-command-event) unread-command-events)
14739       (setq keys (read-key-sequence nil)))
14740     (message "")
14741
14742     (if (member keys nosaves)
14743         (let (func)
14744           (pop-to-buffer gnus-summary-buffer 'norecord)
14745           (if (setq func (lookup-key (current-local-map) keys))
14746               (call-interactively func)
14747             (ding)))
14748       (let ((obuf (current-buffer))
14749             (owin (current-window-configuration))
14750             (opoint (point))
14751             func in-buffer)
14752         (if not-restore-window
14753             (pop-to-buffer gnus-summary-buffer 'norecord)
14754           (switch-to-buffer gnus-summary-buffer 'norecord))
14755         (setq in-buffer (current-buffer))
14756         (if (setq func (lookup-key (current-local-map) keys))
14757             (call-interactively func)
14758           (ding))
14759         (when (eq in-buffer (current-buffer))
14760           (set-buffer obuf)
14761           (unless not-restore-window
14762             (set-window-configuration owin))
14763           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14764
14765 \f
14766 ;;;
14767 ;;; Kill file handling.
14768 ;;;
14769
14770 ;;;###autoload
14771 (defalias 'gnus-batch-kill 'gnus-batch-score)
14772 ;;;###autoload
14773 (defun gnus-batch-score ()
14774   "Run batched scoring.
14775 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14776 Newsgroups is a list of strings in Bnews format.  If you want to score
14777 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14778 score the alt hierarchy, you'd say \"!alt.all\"."
14779   (interactive)
14780   (let* ((yes-and-no
14781           (gnus-newsrc-parse-options
14782            (apply (function concat)
14783                   (mapcar (lambda (g) (concat g " "))
14784                           command-line-args-left))))
14785          (gnus-expert-user t)
14786          (nnmail-spool-file nil)
14787          (gnus-use-dribble-file nil)
14788          (yes (car yes-and-no))
14789          (no (cdr yes-and-no))
14790          group newsrc entry
14791          ;; Disable verbose message.
14792          gnus-novice-user gnus-large-newsgroup)
14793     ;; Eat all arguments.
14794     (setq command-line-args-left nil)
14795     ;; Start Gnus.
14796     (gnus)
14797     ;; Apply kills to specified newsgroups in command line arguments.
14798     (setq newsrc (cdr gnus-newsrc-alist))
14799     (while newsrc
14800       (setq group (caar newsrc))
14801       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14802       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14803                (and (car entry)
14804                     (or (eq (car entry) t)
14805                         (not (zerop (car entry)))))
14806                (if yes (string-match yes group) t)
14807                (or (null no) (not (string-match no group))))
14808           (progn
14809             (gnus-summary-read-group group nil t nil t)
14810             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14811                  (gnus-summary-exit))))
14812       (setq newsrc (cdr newsrc)))
14813     ;; Exit Emacs.
14814     (switch-to-buffer gnus-group-buffer)
14815     (gnus-group-save-newsrc)))
14816
14817 (defun gnus-apply-kill-file ()
14818   "Apply a kill file to the current newsgroup.
14819 Returns the number of articles marked as read."
14820   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14821           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14822       (gnus-apply-kill-file-internal)
14823     0))
14824
14825 (defun gnus-kill-save-kill-buffer ()
14826   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14827     (when (get-file-buffer file)
14828       (save-excursion
14829         (set-buffer (get-file-buffer file))
14830         (and (buffer-modified-p) (save-buffer))
14831         (kill-buffer (current-buffer))))))
14832
14833 (defvar gnus-kill-file-name "KILL"
14834   "Suffix of the kill files.")
14835
14836 (defun gnus-newsgroup-kill-file (newsgroup)
14837   "Return the name of a kill file name for NEWSGROUP.
14838 If NEWSGROUP is nil, return the global kill file name instead."
14839   (cond 
14840    ;; The global KILL file is placed at top of the directory.
14841    ((or (null newsgroup)
14842         (string-equal newsgroup ""))
14843     (expand-file-name gnus-kill-file-name
14844                       gnus-kill-files-directory))
14845    ;; Append ".KILL" to newsgroup name.
14846    ((gnus-use-long-file-name 'not-kill)
14847     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14848                               "." gnus-kill-file-name)
14849                       gnus-kill-files-directory))
14850    ;; Place "KILL" under the hierarchical directory.
14851    (t
14852     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14853                               "/" gnus-kill-file-name)
14854                       gnus-kill-files-directory))))
14855
14856 \f
14857 ;;;
14858 ;;; Dribble file
14859 ;;;
14860
14861 (defvar gnus-dribble-ignore nil)
14862 (defvar gnus-dribble-eval-file nil)
14863
14864 (defun gnus-dribble-file-name ()
14865   "Return the dribble file for the current .newsrc."
14866   (concat
14867    (if gnus-dribble-directory
14868        (concat (file-name-as-directory gnus-dribble-directory)
14869                (file-name-nondirectory gnus-current-startup-file))
14870      gnus-current-startup-file)
14871    "-dribble"))
14872
14873 (defun gnus-dribble-enter (string)
14874   "Enter STRING into the dribble buffer."
14875   (if (and (not gnus-dribble-ignore)
14876            gnus-dribble-buffer
14877            (buffer-name gnus-dribble-buffer))
14878       (let ((obuf (current-buffer)))
14879         (set-buffer gnus-dribble-buffer)
14880         (insert string "\n")
14881         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14882         (set-buffer obuf))))
14883
14884 (defun gnus-dribble-read-file ()
14885   "Read the dribble file from disk."
14886   (let ((dribble-file (gnus-dribble-file-name)))
14887     (save-excursion
14888       (set-buffer (setq gnus-dribble-buffer
14889                         (get-buffer-create
14890                          (file-name-nondirectory dribble-file))))
14891       (gnus-add-current-to-buffer-list)
14892       (erase-buffer)
14893       (setq buffer-file-name dribble-file)
14894       (auto-save-mode t)
14895       (buffer-disable-undo (current-buffer))
14896       (bury-buffer (current-buffer))
14897       (set-buffer-modified-p nil)
14898       (let ((auto (make-auto-save-file-name))
14899             (gnus-dribble-ignore t)
14900             modes)
14901         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14902           ;; Load whichever file is newest -- the auto save file
14903           ;; or the "real" file.
14904           (if (file-newer-than-file-p auto dribble-file)
14905               (insert-file-contents auto)
14906             (insert-file-contents dribble-file))
14907           (unless (zerop (buffer-size))
14908             (set-buffer-modified-p t))
14909           ;; Set the file modes to reflect the .newsrc file modes.
14910           (save-buffer)
14911           (when (and (file-exists-p gnus-current-startup-file)
14912                      (setq modes (file-modes gnus-current-startup-file)))
14913             (set-file-modes dribble-file modes))
14914           ;; Possibly eval the file later.
14915           (when (gnus-y-or-n-p
14916                  "Auto-save file exists.  Do you want to read it? ")
14917             (setq gnus-dribble-eval-file t)))))))
14918
14919 (defun gnus-dribble-eval-file ()
14920   (when gnus-dribble-eval-file
14921     (setq gnus-dribble-eval-file nil)
14922     (save-excursion
14923       (let ((gnus-dribble-ignore t))
14924         (set-buffer gnus-dribble-buffer)
14925         (eval-buffer (current-buffer))))))
14926
14927 (defun gnus-dribble-delete-file ()
14928   (when (file-exists-p (gnus-dribble-file-name))
14929     (delete-file (gnus-dribble-file-name)))
14930   (when gnus-dribble-buffer
14931     (save-excursion
14932       (set-buffer gnus-dribble-buffer)
14933       (let ((auto (make-auto-save-file-name)))
14934         (if (file-exists-p auto)
14935             (delete-file auto))
14936         (erase-buffer)
14937         (set-buffer-modified-p nil)))))
14938
14939 (defun gnus-dribble-save ()
14940   (when (and gnus-dribble-buffer
14941              (buffer-name gnus-dribble-buffer))
14942     (save-excursion
14943       (set-buffer gnus-dribble-buffer)
14944       (save-buffer))))
14945
14946 (defun gnus-dribble-clear ()
14947   (when (gnus-buffer-exists-p gnus-dribble-buffer)
14948     (save-excursion
14949       (set-buffer gnus-dribble-buffer)
14950       (erase-buffer)
14951       (set-buffer-modified-p nil)
14952       (setq buffer-saved-size (buffer-size)))))
14953
14954 \f
14955 ;;;
14956 ;;; Server Communication
14957 ;;;
14958
14959 (defun gnus-start-news-server (&optional confirm)
14960   "Open a method for getting news.
14961 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14962   (let (how)
14963     (if gnus-current-select-method
14964         ;; Stream is already opened.
14965         nil
14966       ;; Open NNTP server.
14967       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14968       (if confirm
14969           (progn
14970             ;; Read server name with completion.
14971             (setq gnus-nntp-server
14972                   (completing-read "NNTP server: "
14973                                    (mapcar (lambda (server) (list server))
14974                                            (cons (list gnus-nntp-server)
14975                                                  gnus-secondary-servers))
14976                                    nil nil gnus-nntp-server))))
14977
14978       (if (and gnus-nntp-server
14979                (stringp gnus-nntp-server)
14980                (not (string= gnus-nntp-server "")))
14981           (setq gnus-select-method
14982                 (cond ((or (string= gnus-nntp-server "")
14983                            (string= gnus-nntp-server "::"))
14984                        (list 'nnspool (system-name)))
14985                       ((string-match "^:" gnus-nntp-server)
14986                        (list 'nnmh gnus-nntp-server
14987                              (list 'nnmh-directory
14988                                    (file-name-as-directory
14989                                     (expand-file-name
14990                                      (concat "~/" (substring
14991                                                    gnus-nntp-server 1)))))
14992                              (list 'nnmh-get-new-mail nil)))
14993                       (t
14994                        (list 'nntp gnus-nntp-server)))))
14995
14996       (setq how (car gnus-select-method))
14997       (cond ((eq how 'nnspool)
14998              (require 'nnspool)
14999              (gnus-message 5 "Looking up local news spool..."))
15000             ((eq how 'nnmh)
15001              (require 'nnmh)
15002              (gnus-message 5 "Looking up mh spool..."))
15003             (t
15004              (require 'nntp)))
15005       (setq gnus-current-select-method gnus-select-method)
15006       (run-hooks 'gnus-open-server-hook)
15007       (or
15008        ;; gnus-open-server-hook might have opened it
15009        (gnus-server-opened gnus-select-method)
15010        (gnus-open-server gnus-select-method)
15011        (gnus-y-or-n-p
15012         (format
15013          "%s (%s) open error: '%s'.     Continue? "
15014          (car gnus-select-method) (cadr gnus-select-method)
15015          (gnus-status-message gnus-select-method)))
15016        (gnus-error 1 "Couldn't open server on %s"
15017                    (nth 1 gnus-select-method))))))
15018
15019 (defun gnus-check-group (group)
15020   "Try to make sure that the server where GROUP exists is alive."
15021   (let ((method (gnus-find-method-for-group group)))
15022     (or (gnus-server-opened method)
15023         (gnus-open-server method))))
15024
15025 (defun gnus-check-server (&optional method silent)
15026   "Check whether the connection to METHOD is down.
15027 If METHOD is nil, use `gnus-select-method'.
15028 If it is down, start it up (again)."
15029   (let ((method (or method gnus-select-method)))
15030     ;; Transform virtual server names into select methods.
15031     (when (stringp method)
15032       (setq method (gnus-server-to-method method)))
15033     (if (gnus-server-opened method)
15034         ;; The stream is already opened.
15035         t
15036       ;; Open the server.
15037       (unless silent
15038         (gnus-message 5 "Opening %s server%s..." (car method)
15039                       (if (equal (nth 1 method) "") ""
15040                         (format " on %s" (nth 1 method)))))
15041       (run-hooks 'gnus-open-server-hook)
15042       (prog1
15043           (gnus-open-server method)
15044         (unless silent
15045           (message ""))))))
15046
15047 (defun gnus-get-function (method function)
15048   "Return a function symbol based on METHOD and FUNCTION."
15049   ;; Translate server names into methods.
15050   (unless method
15051     (error "Attempted use of a nil select method"))
15052   (when (stringp method)
15053     (setq method (gnus-server-to-method method)))
15054   (let ((func (intern (format "%s-%s" (car method) function))))
15055     ;; If the functions isn't bound, we require the backend in
15056     ;; question.
15057     (unless (fboundp func)
15058       (require (car method))
15059       (unless (fboundp func)
15060         ;; This backend doesn't implement this function.
15061         (error "No such function: %s" func)))
15062     func))
15063
15064 \f
15065 ;;;
15066 ;;; Interface functions to the backends.
15067 ;;;
15068
15069 (defun gnus-open-server (method)
15070   "Open a connection to METHOD."
15071   (when (stringp method)
15072     (setq method (gnus-server-to-method method)))
15073   (let ((elem (assoc method gnus-opened-servers)))
15074     ;; If this method was previously denied, we just return nil.
15075     (if (eq (nth 1 elem) 'denied)
15076         (progn
15077           (gnus-message 1 "Denied server")
15078           nil)
15079       ;; Open the server.
15080       (let ((result
15081              (funcall (gnus-get-function method 'open-server)
15082                       (nth 1 method) (nthcdr 2 method))))
15083         ;; If this hasn't been opened before, we add it to the list.
15084         (unless elem
15085           (setq elem (list method nil)
15086                 gnus-opened-servers (cons elem gnus-opened-servers)))
15087         ;; Set the status of this server.
15088         (setcar (cdr elem) (if result 'ok 'denied))
15089         ;; Return the result from the "open" call.
15090         result))))
15091
15092 (defun gnus-close-server (method)
15093   "Close the connection to METHOD."
15094   (when (stringp method)
15095     (setq method (gnus-server-to-method method)))
15096   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
15097
15098 (defun gnus-request-list (method)
15099   "Request the active file from METHOD."
15100   (when (stringp method)
15101     (setq method (gnus-server-to-method method)))
15102   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
15103
15104 (defun gnus-request-list-newsgroups (method)
15105   "Request the newsgroups file from METHOD."
15106   (when (stringp method)
15107     (setq method (gnus-server-to-method method)))
15108   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
15109
15110 (defun gnus-request-newgroups (date method)
15111   "Request all new groups since DATE from METHOD."
15112   (when (stringp method)
15113     (setq method (gnus-server-to-method method)))
15114   (funcall (gnus-get-function method 'request-newgroups)
15115            date (nth 1 method)))
15116
15117 (defun gnus-server-opened (method)
15118   "Check whether a connection to METHOD has been opened."
15119   (when (stringp method)
15120     (setq method (gnus-server-to-method method)))
15121   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
15122
15123 (defun gnus-status-message (method)
15124   "Return the status message from METHOD.
15125 If METHOD is a string, it is interpreted as a group name.   The method
15126 this group uses will be queried."
15127   (let ((method (if (stringp method) (gnus-find-method-for-group method)
15128                   method)))
15129     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
15130
15131 (defun gnus-request-group (group &optional dont-check method)
15132   "Request GROUP.  If DONT-CHECK, no information is required."
15133   (let ((method (or method (gnus-find-method-for-group group))))
15134     (when (stringp method)
15135       (setq method (gnus-server-to-method method)))
15136     (funcall (gnus-get-function method 'request-group)
15137              (gnus-group-real-name group) (nth 1 method) dont-check)))
15138
15139 (defun gnus-request-asynchronous (group &optional articles)
15140   "Request that GROUP behave asynchronously.
15141 ARTICLES is the `data' of the group."
15142   (let ((method (gnus-find-method-for-group group)))
15143     (funcall (gnus-get-function method 'request-asynchronous)
15144              (gnus-group-real-name group) (nth 1 method) articles)))
15145
15146 (defun gnus-list-active-group (group)
15147   "Request active information on GROUP."
15148   (let ((method (gnus-find-method-for-group group))
15149         (func 'list-active-group))
15150     (when (gnus-check-backend-function func group)
15151       (funcall (gnus-get-function method func)
15152                (gnus-group-real-name group) (nth 1 method)))))
15153
15154 (defun gnus-request-group-description (group)
15155   "Request a description of GROUP."
15156   (let ((method (gnus-find-method-for-group group))
15157         (func 'request-group-description))
15158     (when (gnus-check-backend-function func group)
15159       (funcall (gnus-get-function method func)
15160                (gnus-group-real-name group) (nth 1 method)))))
15161
15162 (defun gnus-close-group (group)
15163   "Request the GROUP be closed."
15164   (let ((method (gnus-find-method-for-group group)))
15165     (funcall (gnus-get-function method 'close-group)
15166              (gnus-group-real-name group) (nth 1 method))))
15167
15168 (defun gnus-retrieve-headers (articles group &optional fetch-old)
15169   "Request headers for ARTICLES in GROUP.
15170 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
15171   (let ((method (gnus-find-method-for-group group)))
15172     (if (and gnus-use-cache (numberp (car articles)))
15173         (gnus-cache-retrieve-headers articles group fetch-old)
15174       (funcall (gnus-get-function method 'retrieve-headers)
15175                articles (gnus-group-real-name group) (nth 1 method)
15176                fetch-old))))
15177
15178 (defun gnus-retrieve-groups (groups method)
15179   "Request active information on GROUPS from METHOD."
15180   (when (stringp method)
15181     (setq method (gnus-server-to-method method)))
15182   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
15183
15184 (defun gnus-request-type (group &optional article)
15185   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15186   (let ((method (gnus-find-method-for-group group)))
15187     (if (not (gnus-check-backend-function 'request-type (car method)))
15188         'unknown
15189       (funcall (gnus-get-function method 'request-type)
15190                (gnus-group-real-name group) article))))
15191
15192 (defun gnus-request-update-mark (group article mark)
15193   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15194   (let ((method (gnus-find-method-for-group group)))
15195     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
15196         mark
15197       (funcall (gnus-get-function method 'request-update-mark)
15198                (gnus-group-real-name group) article mark))))
15199
15200 (defun gnus-request-article (article group &optional buffer)
15201   "Request the ARTICLE in GROUP.
15202 ARTICLE can either be an article number or an article Message-ID.
15203 If BUFFER, insert the article in that group."
15204   (let ((method (gnus-find-method-for-group group)))
15205     (funcall (gnus-get-function method 'request-article)
15206              article (gnus-group-real-name group) (nth 1 method) buffer)))
15207
15208 (defun gnus-request-head (article group)
15209   "Request the head of ARTICLE in GROUP."
15210   (let ((method (gnus-find-method-for-group group)))
15211     (funcall (gnus-get-function method 'request-head)
15212              article (gnus-group-real-name group) (nth 1 method))))
15213
15214 (defun gnus-request-body (article group)
15215   "Request the body of ARTICLE in GROUP."
15216   (let ((method (gnus-find-method-for-group group)))
15217     (funcall (gnus-get-function method 'request-body)
15218              article (gnus-group-real-name group) (nth 1 method))))
15219
15220 (defun gnus-request-post (method)
15221   "Post the current buffer using METHOD."
15222   (when (stringp method)
15223     (setq method (gnus-server-to-method method)))
15224   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
15225
15226 (defun gnus-request-scan (group method)
15227   "Request a SCAN being performed in GROUP from METHOD.
15228 If GROUP is nil, all groups on METHOD are scanned."
15229   (let ((method (if group (gnus-find-method-for-group group) method)))
15230     (funcall (gnus-get-function method 'request-scan)
15231              (and group (gnus-group-real-name group)) (nth 1 method))))
15232
15233 (defsubst gnus-request-update-info (info method)
15234   "Request that METHOD update INFO."
15235   (when (stringp method)
15236     (setq method (gnus-server-to-method method)))
15237   (when (gnus-check-backend-function 'request-update-info (car method))
15238     (funcall (gnus-get-function method 'request-update-info)
15239              (gnus-group-real-name (gnus-info-group info))
15240              info (nth 1 method))))
15241
15242 (defun gnus-request-expire-articles (articles group &optional force)
15243   (let ((method (gnus-find-method-for-group group)))
15244     (funcall (gnus-get-function method 'request-expire-articles)
15245              articles (gnus-group-real-name group) (nth 1 method)
15246              force)))
15247
15248 (defun gnus-request-move-article
15249   (article group server accept-function &optional last)
15250   (let ((method (gnus-find-method-for-group group)))
15251     (funcall (gnus-get-function method 'request-move-article)
15252              article (gnus-group-real-name group)
15253              (nth 1 method) accept-function last)))
15254
15255 (defun gnus-request-accept-article (group method &optional last)
15256   ;; Make sure there's a newline at the end of the article.
15257   (when (stringp method)
15258     (setq method (gnus-server-to-method method)))
15259   (when (and (not method)
15260              (stringp group))
15261     (setq method (gnus-find-method-for-group group)))
15262   (goto-char (point-max))
15263   (unless (bolp)
15264     (insert "\n"))
15265   (let ((func (car (or method (gnus-find-method-for-group group)))))
15266     (funcall (intern (format "%s-request-accept-article" func))
15267              (if (stringp group) (gnus-group-real-name group) group)
15268              (cadr method)
15269              last)))
15270
15271 (defun gnus-request-replace-article (article group buffer)
15272   (let ((func (car (gnus-find-method-for-group group))))
15273     (funcall (intern (format "%s-request-replace-article" func))
15274              article (gnus-group-real-name group) buffer)))
15275
15276 (defun gnus-request-associate-buffer (group)
15277   (let ((method (gnus-find-method-for-group group)))
15278     (funcall (gnus-get-function method 'request-associate-buffer)
15279              (gnus-group-real-name group))))
15280
15281 (defun gnus-request-restore-buffer (article group)
15282   "Request a new buffer restored to the state of ARTICLE."
15283   (let ((method (gnus-find-method-for-group group)))
15284     (funcall (gnus-get-function method 'request-restore-buffer)
15285              article (gnus-group-real-name group) (nth 1 method))))
15286
15287 (defun gnus-request-create-group (group &optional method)
15288   (when (stringp method)
15289     (setq method (gnus-server-to-method method)))
15290   (let ((method (or method (gnus-find-method-for-group group))))
15291     (funcall (gnus-get-function method 'request-create-group)
15292              (gnus-group-real-name group) (nth 1 method))))
15293
15294 (defun gnus-request-delete-group (group &optional force)
15295   (let ((method (gnus-find-method-for-group group)))
15296     (funcall (gnus-get-function method 'request-delete-group)
15297              (gnus-group-real-name group) force (nth 1 method))))
15298
15299 (defun gnus-request-rename-group (group new-name)
15300   (let ((method (gnus-find-method-for-group group)))
15301     (funcall (gnus-get-function method 'request-rename-group)
15302              (gnus-group-real-name group)
15303              (gnus-group-real-name new-name) (nth 1 method))))
15304
15305 (defun gnus-member-of-valid (symbol group)
15306   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15307   (memq symbol (assoc
15308                 (symbol-name (car (gnus-find-method-for-group group)))
15309                 gnus-valid-select-methods)))
15310
15311 (defun gnus-method-option-p (method option)
15312   "Return non-nil if select METHOD has OPTION as a parameter."
15313   (when (stringp method)
15314     (setq method (gnus-server-to-method method)))
15315   (memq option (assoc (format "%s" (car method))
15316                       gnus-valid-select-methods)))
15317
15318 (defun gnus-server-extend-method (group method)
15319   ;; This function "extends" a virtual server.  If the server is
15320   ;; "hello", and the select method is ("hello" (my-var "something"))
15321   ;; in the group "alt.alt", this will result in a new virtual server
15322   ;; called "hello+alt.alt".
15323   (let ((entry
15324          (gnus-copy-sequence
15325           (if (equal (car method) "native") gnus-select-method
15326             (cdr (assoc (car method) gnus-server-alist))))))
15327     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15328     (nconc entry (cdr method))))
15329
15330 (defun gnus-find-method-for-group (group &optional info)
15331   "Find the select method that GROUP uses."
15332   (or gnus-override-method
15333       (and (not group)
15334            gnus-select-method)
15335       (let ((info (or info (gnus-get-info group)))
15336             method)
15337         (if (or (not info)
15338                 (not (setq method (gnus-info-method info)))
15339                 (equal method "native"))
15340             gnus-select-method
15341           (setq method
15342                 (cond ((stringp method)
15343                        (gnus-server-to-method method))
15344                       ((stringp (car method))
15345                        (gnus-server-extend-method group method))
15346                       (t
15347                        method)))
15348           (cond ((equal (cadr method) "")
15349                  method)
15350                 ((null (cadr method))
15351                  (list (car method) ""))
15352                 (t
15353                  (gnus-server-add-address method)))))))
15354
15355 (defun gnus-check-backend-function (func group)
15356   "Check whether GROUP supports function FUNC."
15357   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15358                   group)))
15359     (fboundp (intern (format "%s-%s" method func)))))
15360
15361 (defun gnus-methods-using (feature)
15362   "Find all methods that have FEATURE."
15363   (let ((valids gnus-valid-select-methods)
15364         outs)
15365     (while valids
15366       (if (memq feature (car valids))
15367           (setq outs (cons (car valids) outs)))
15368       (setq valids (cdr valids)))
15369     outs))
15370
15371 \f
15372 ;;;
15373 ;;; Active & Newsrc File Handling
15374 ;;;
15375
15376 (defun gnus-setup-news (&optional rawfile level dont-connect)
15377   "Setup news information.
15378 If RAWFILE is non-nil, the .newsrc file will also be read.
15379 If LEVEL is non-nil, the news will be set up at level LEVEL."
15380   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15381
15382     (when init 
15383       ;; Clear some variables to re-initialize news information.
15384       (setq gnus-newsrc-alist nil
15385             gnus-active-hashtb nil)
15386       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15387       (gnus-read-newsrc-file rawfile))
15388
15389     (when (and (not (assoc "archive" gnus-server-alist))
15390                gnus-message-archive-method)
15391       (push (cons "archive" gnus-message-archive-method)
15392             gnus-server-alist))
15393
15394     ;; If we don't read the complete active file, we fill in the
15395     ;; hashtb here.
15396     (if (or (null gnus-read-active-file)
15397             (eq gnus-read-active-file 'some))
15398         (gnus-update-active-hashtb-from-killed))
15399
15400     ;; Read the active file and create `gnus-active-hashtb'.
15401     ;; If `gnus-read-active-file' is nil, then we just create an empty
15402     ;; hash table.  The partial filling out of the hash table will be
15403     ;; done in `gnus-get-unread-articles'.
15404     (and gnus-read-active-file
15405          (not level)
15406          (gnus-read-active-file))
15407
15408     (or gnus-active-hashtb
15409         (setq gnus-active-hashtb (make-vector 4095 0)))
15410
15411     ;; Initialize the cache.
15412     (when gnus-use-cache
15413       (gnus-cache-open))
15414
15415     ;; Possibly eval the dribble file.
15416     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15417
15418     ;; Slave Gnusii should then clear the dribble buffer.
15419     (when (and init gnus-slave)
15420       (gnus-dribble-clear))
15421
15422     (gnus-update-format-specifications)
15423
15424     ;; See whether we need to read the description file.
15425     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15426              (not gnus-description-hashtb)
15427              (not dont-connect)
15428              gnus-read-active-file)
15429         (gnus-read-all-descriptions-files))
15430
15431     ;; Find new newsgroups and treat them.
15432     (if (and init gnus-check-new-newsgroups (not level)
15433              (gnus-check-server gnus-select-method))
15434         (gnus-find-new-newsgroups))
15435
15436     ;; We might read in new NoCeM messages here.
15437     (when (and gnus-use-nocem 
15438                (not level)
15439                (not dont-connect))
15440       (gnus-nocem-scan-groups))
15441
15442     ;; Find the number of unread articles in each non-dead group.
15443     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15444       (gnus-get-unread-articles level))
15445
15446     (if (and init gnus-check-bogus-newsgroups
15447              gnus-read-active-file (not level)
15448              (gnus-server-opened gnus-select-method))
15449         (gnus-check-bogus-newsgroups))))
15450
15451 (defun gnus-find-new-newsgroups (&optional arg)
15452   "Search for new newsgroups and add them.
15453 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15454 The `-n' option line from .newsrc is respected.
15455 If ARG (the prefix), use the `ask-server' method to query
15456 the server for new groups."
15457   (interactive "P")
15458   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15459                        (null gnus-read-active-file)
15460                        (eq gnus-read-active-file 'some))
15461                    'ask-server gnus-check-new-newsgroups)))
15462     (unless (gnus-check-first-time-used)
15463       (if (or (consp check)
15464               (eq check 'ask-server))
15465           ;; Ask the server for new groups.
15466           (gnus-ask-server-for-new-groups)
15467         ;; Go through the active hashtb and look for new groups.
15468         (let ((groups 0)
15469               group new-newsgroups)
15470           (gnus-message 5 "Looking for new newsgroups...")
15471           (unless gnus-have-read-active-file
15472             (gnus-read-active-file))
15473           (setq gnus-newsrc-last-checked-date (current-time-string))
15474           (unless gnus-killed-hashtb
15475             (gnus-make-hashtable-from-killed))
15476           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15477           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15478           (mapatoms
15479            (lambda (sym)
15480              (if (or (null (setq group (symbol-name sym)))
15481                      (not (boundp sym))
15482                      (null (symbol-value sym))
15483                      (gnus-gethash group gnus-killed-hashtb)
15484                      (gnus-gethash group gnus-newsrc-hashtb))
15485                  ()
15486                (let ((do-sub (gnus-matches-options-n group)))
15487                  (cond
15488                   ((eq do-sub 'subscribe)
15489                    (setq groups (1+ groups))
15490                    (gnus-sethash group group gnus-killed-hashtb)
15491                    (funcall gnus-subscribe-options-newsgroup-method group))
15492                   ((eq do-sub 'ignore)
15493                    nil)
15494                   (t
15495                    (setq groups (1+ groups))
15496                    (gnus-sethash group group gnus-killed-hashtb)
15497                    (if gnus-subscribe-hierarchical-interactive
15498                        (setq new-newsgroups (cons group new-newsgroups))
15499                      (funcall gnus-subscribe-newsgroup-method group)))))))
15500            gnus-active-hashtb)
15501           (when new-newsgroups
15502             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15503           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15504           (if (> groups 0)
15505               (gnus-message 6 "%d new newsgroup%s arrived."
15506                             groups (if (> groups 1) "s have" " has"))
15507             (gnus-message 6 "No new newsgroups.")))))))
15508
15509 (defun gnus-matches-options-n (group)
15510   ;; Returns `subscribe' if the group is to be unconditionally
15511   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15512   ;; no match for the group.
15513
15514   ;; First we check the two user variables.
15515   (cond
15516    ((and gnus-options-subscribe
15517          (string-match gnus-options-subscribe group))
15518     'subscribe)
15519    ((and gnus-auto-subscribed-groups
15520          (string-match gnus-auto-subscribed-groups group))
15521     'subscribe)
15522    ((and gnus-options-not-subscribe
15523          (string-match gnus-options-not-subscribe group))
15524     'ignore)
15525    ;; Then we go through the list that was retrieved from the .newsrc
15526    ;; file.  This list has elements on the form
15527    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15528    ;; is in the reverse order of the options line) is returned.
15529    (t
15530     (let ((regs gnus-newsrc-options-n))
15531       (while (and regs
15532                   (not (string-match (caar regs) group)))
15533         (setq regs (cdr regs)))
15534       (and regs (cdar regs))))))
15535
15536 (defun gnus-ask-server-for-new-groups ()
15537   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15538          (methods (cons gnus-select-method
15539                         (nconc
15540                          (when gnus-message-archive-method
15541                            (list "archive"))
15542                          (append
15543                           (and (consp gnus-check-new-newsgroups)
15544                                gnus-check-new-newsgroups)
15545                           gnus-secondary-select-methods))))
15546          (groups 0)
15547          (new-date (current-time-string))
15548          group new-newsgroups got-new method hashtb
15549          gnus-override-subscribe-method)
15550     ;; Go through both primary and secondary select methods and
15551     ;; request new newsgroups.
15552     (while (setq method (gnus-server-get-method nil (pop methods)))
15553       (setq new-newsgroups nil)
15554       (setq gnus-override-subscribe-method method)
15555       (when (and (gnus-check-server method)
15556                  (gnus-request-newgroups date method))
15557         (save-excursion
15558           (setq got-new t)
15559           (setq hashtb (gnus-make-hashtable 100))
15560           (set-buffer nntp-server-buffer)
15561           ;; Enter all the new groups into a hashtable.
15562           (gnus-active-to-gnus-format method hashtb 'ignore))
15563         ;; Now all new groups from `method' are in `hashtb'.
15564         (mapatoms
15565          (lambda (group-sym)
15566            (if (or (null (setq group (symbol-name group-sym)))
15567                    (not (boundp group-sym))
15568                    (null (symbol-value group-sym))
15569                    (gnus-gethash group gnus-newsrc-hashtb)
15570                    (member group gnus-zombie-list)
15571                    (member group gnus-killed-list))
15572                ;; The group is already known.
15573                ()
15574              ;; Make this group active.
15575              (when (symbol-value group-sym)
15576                (gnus-set-active group (symbol-value group-sym)))
15577              ;; Check whether we want it or not.
15578              (let ((do-sub (gnus-matches-options-n group)))
15579                (cond
15580                 ((eq do-sub 'subscribe)
15581                  (incf groups)
15582                  (gnus-sethash group group gnus-killed-hashtb)
15583                  (funcall gnus-subscribe-options-newsgroup-method group))
15584                 ((eq do-sub 'ignore)
15585                  nil)
15586                 (t
15587                  (incf groups)
15588                  (gnus-sethash group group gnus-killed-hashtb)
15589                  (if gnus-subscribe-hierarchical-interactive
15590                      (push group new-newsgroups)
15591                    (funcall gnus-subscribe-newsgroup-method group)))))))
15592          hashtb))
15593       (when new-newsgroups
15594         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15595     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15596     (when (> groups 0)
15597       (gnus-message 6 "%d new newsgroup%s arrived."
15598                     groups (if (> groups 1) "s have" " has")))
15599     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15600     got-new))
15601
15602 (defun gnus-check-first-time-used ()
15603   (if (or (> (length gnus-newsrc-alist) 1)
15604           (file-exists-p gnus-startup-file)
15605           (file-exists-p (concat gnus-startup-file ".el"))
15606           (file-exists-p (concat gnus-startup-file ".eld")))
15607       nil
15608     (gnus-message 6 "First time user; subscribing you to default groups")
15609     (unless (gnus-read-active-file-p)
15610       (gnus-read-active-file))
15611     (setq gnus-newsrc-last-checked-date (current-time-string))
15612     (let ((groups gnus-default-subscribed-newsgroups)
15613           group)
15614       (if (eq groups t)
15615           nil
15616         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15617         (mapatoms
15618          (lambda (sym)
15619            (if (null (setq group (symbol-name sym)))
15620                ()
15621              (let ((do-sub (gnus-matches-options-n group)))
15622                (cond
15623                 ((eq do-sub 'subscribe)
15624                  (gnus-sethash group group gnus-killed-hashtb)
15625                  (funcall gnus-subscribe-options-newsgroup-method group))
15626                 ((eq do-sub 'ignore)
15627                  nil)
15628                 (t
15629                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15630          gnus-active-hashtb)
15631         (while groups
15632           (if (gnus-active (car groups))
15633               (gnus-group-change-level
15634                (car groups) gnus-level-default-subscribed gnus-level-killed))
15635           (setq groups (cdr groups)))
15636         (gnus-group-make-help-group)
15637         (and gnus-novice-user
15638              (gnus-message 7 "`A k' to list killed groups"))))))
15639
15640 (defun gnus-subscribe-group (group previous &optional method)
15641   (gnus-group-change-level
15642    (if method
15643        (list t group gnus-level-default-subscribed nil nil method)
15644      group)
15645    gnus-level-default-subscribed gnus-level-killed previous t))
15646
15647 ;; `gnus-group-change-level' is the fundamental function for changing
15648 ;; subscription levels of newsgroups.  This might mean just changing
15649 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15650 ;; again, which subscribes/unsubscribes a group, which is equally
15651 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15652 ;; from 8-9 to 1-7 means that you remove the group from the list of
15653 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15654 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15655 ;; which is trivial.
15656 ;; ENTRY can either be a string (newsgroup name) or a list (if
15657 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15658 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15659 ;; entries.
15660 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15661 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15662 ;; after.
15663 (defun gnus-group-change-level (entry level &optional oldlevel
15664                                       previous fromkilled)
15665   (let (group info active num)
15666     ;; Glean what info we can from the arguments
15667     (if (consp entry)
15668         (if fromkilled (setq group (nth 1 entry))
15669           (setq group (car (nth 2 entry))))
15670       (setq group entry))
15671     (if (and (stringp entry)
15672              oldlevel
15673              (< oldlevel gnus-level-zombie))
15674         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15675     (if (and (not oldlevel)
15676              (consp entry))
15677         (setq oldlevel (gnus-info-level (nth 2 entry)))
15678       (setq oldlevel (or oldlevel 9)))
15679     (if (stringp previous)
15680         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15681
15682     (if (and (>= oldlevel gnus-level-zombie)
15683              (gnus-gethash group gnus-newsrc-hashtb))
15684         ;; We are trying to subscribe a group that is already
15685         ;; subscribed.
15686         ()                              ; Do nothing.
15687
15688       (or (gnus-ephemeral-group-p group)
15689           (gnus-dribble-enter
15690            (format "(gnus-group-change-level %S %S %S %S %S)"
15691                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15692
15693       ;; Then we remove the newgroup from any old structures, if needed.
15694       ;; If the group was killed, we remove it from the killed or zombie
15695       ;; list.  If not, and it is in fact going to be killed, we remove
15696       ;; it from the newsrc hash table and assoc.
15697       (cond
15698        ((>= oldlevel gnus-level-zombie)
15699         (if (= oldlevel gnus-level-zombie)
15700             (setq gnus-zombie-list (delete group gnus-zombie-list))
15701           (setq gnus-killed-list (delete group gnus-killed-list))))
15702        (t
15703         (if (and (>= level gnus-level-zombie)
15704                  entry)
15705             (progn
15706               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15707               (if (nth 3 entry)
15708                   (setcdr (gnus-gethash (car (nth 3 entry))
15709                                         gnus-newsrc-hashtb)
15710                           (cdr entry)))
15711               (setcdr (cdr entry) (cdddr entry))))))
15712
15713       ;; Finally we enter (if needed) the list where it is supposed to
15714       ;; go, and change the subscription level.  If it is to be killed,
15715       ;; we enter it into the killed or zombie list.
15716       (cond 
15717        ((>= level gnus-level-zombie)
15718         ;; Remove from the hash table.
15719         (gnus-sethash group nil gnus-newsrc-hashtb)
15720         ;; We do not enter foreign groups into the list of dead
15721         ;; groups.
15722         (unless (gnus-group-foreign-p group)
15723           (if (= level gnus-level-zombie)
15724               (setq gnus-zombie-list (cons group gnus-zombie-list))
15725             (setq gnus-killed-list (cons group gnus-killed-list)))))
15726        (t
15727         ;; If the list is to be entered into the newsrc assoc, and
15728         ;; it was killed, we have to create an entry in the newsrc
15729         ;; hashtb format and fix the pointers in the newsrc assoc.
15730         (if (< oldlevel gnus-level-zombie)
15731             ;; It was alive, and it is going to stay alive, so we
15732             ;; just change the level and don't change any pointers or
15733             ;; hash table entries.
15734             (setcar (cdaddr entry) level)
15735           (if (listp entry)
15736               (setq info (cdr entry)
15737                     num (car entry))
15738             (setq active (gnus-active group))
15739             (setq num
15740                   (if active (- (1+ (cdr active)) (car active)) t))
15741             ;; Check whether the group is foreign.  If so, the
15742             ;; foreign select method has to be entered into the
15743             ;; info.
15744             (let ((method (or gnus-override-subscribe-method
15745                               (gnus-group-method group))))
15746               (if (eq method gnus-select-method)
15747                   (setq info (list group level nil))
15748                 (setq info (list group level nil nil method)))))
15749           (unless previous
15750             (setq previous
15751                   (let ((p gnus-newsrc-alist))
15752                     (while (cddr p)
15753                       (setq p (cdr p)))
15754                     p)))
15755           (setq entry (cons info (cddr previous)))
15756           (if (cdr previous)
15757               (progn
15758                 (setcdr (cdr previous) entry)
15759                 (gnus-sethash group (cons num (cdr previous))
15760                               gnus-newsrc-hashtb))
15761             (setcdr previous entry)
15762             (gnus-sethash group (cons num previous)
15763                           gnus-newsrc-hashtb))
15764           (when (cdr entry)
15765             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15766       (when gnus-group-change-level-function
15767         (funcall gnus-group-change-level-function group level oldlevel)))))
15768
15769 (defun gnus-kill-newsgroup (newsgroup)
15770   "Obsolete function.  Kills a newsgroup."
15771   (gnus-group-change-level
15772    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15773
15774 (defun gnus-check-bogus-newsgroups (&optional confirm)
15775   "Remove bogus newsgroups.
15776 If CONFIRM is non-nil, the user has to confirm the deletion of every
15777 newsgroup."
15778   (let ((newsrc (cdr gnus-newsrc-alist))
15779         bogus group entry info)
15780     (gnus-message 5 "Checking bogus newsgroups...")
15781     (unless (gnus-read-active-file-p)
15782       (gnus-read-active-file))
15783     (when (gnus-read-active-file-p)
15784       ;; Find all bogus newsgroup that are subscribed.
15785       (while newsrc
15786         (setq info (pop newsrc)
15787               group (gnus-info-group info))
15788         (unless (or (gnus-active group) ; Active
15789                     (gnus-info-method info) ; Foreign
15790                     (and confirm
15791                          (not (gnus-y-or-n-p
15792                                (format "Remove bogus newsgroup: %s " group)))))
15793           ;; Found a bogus newsgroup.
15794           (push group bogus)))
15795       ;; Remove all bogus subscribed groups by first killing them, and
15796       ;; then removing them from the list of killed groups.
15797       (while bogus
15798         (when (setq entry (gnus-gethash (setq group (pop bogus))
15799                                         gnus-newsrc-hashtb))
15800           (gnus-group-change-level entry gnus-level-killed)
15801           (setq gnus-killed-list (delete group gnus-killed-list))))
15802       ;; Then we remove all bogus groups from the list of killed and
15803       ;; zombie groups.  They are are removed without confirmation.
15804       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15805             killed)
15806         (while dead-lists
15807           (setq killed (symbol-value (car dead-lists)))
15808           (while killed
15809             (unless (gnus-active (setq group (pop killed)))
15810               ;; The group is bogus.
15811               ;; !!!Slow as hell.
15812               (set (car dead-lists)
15813                    (delete group (symbol-value (car dead-lists))))))
15814           (setq dead-lists (cdr dead-lists))))
15815       (gnus-message 5 "Checking bogus newsgroups...done"))))
15816
15817 (defun gnus-check-duplicate-killed-groups ()
15818   "Remove duplicates from the list of killed groups."
15819   (interactive)
15820   (let ((killed gnus-killed-list))
15821     (while killed
15822       (gnus-message 9 "%d" (length killed))
15823       (setcdr killed (delete (car killed) (cdr killed)))
15824       (setq killed (cdr killed)))))
15825
15826 ;; We want to inline a function from gnus-cache, so we cheat here:
15827 (eval-when-compile
15828   (provide 'gnus)
15829   (require 'gnus-cache))
15830
15831 (defun gnus-get-unread-articles-in-group (info active &optional update)
15832   (when active
15833     ;; Allow the backend to update the info in the group.
15834     (when (and update 
15835                (gnus-request-update-info
15836                 info (gnus-find-method-for-group (gnus-info-group info))))
15837       (gnus-activate-group (gnus-info-group info) nil t))
15838     (let* ((range (gnus-info-read info))
15839            (num 0))
15840       ;; If a cache is present, we may have to alter the active info.
15841       (when (and gnus-use-cache info)
15842         (inline (gnus-cache-possibly-alter-active 
15843                  (gnus-info-group info) active)))
15844       ;; Modify the list of read articles according to what articles
15845       ;; are available; then tally the unread articles and add the
15846       ;; number to the group hash table entry.
15847       (cond
15848        ((zerop (cdr active))
15849         (setq num 0))
15850        ((not range)
15851         (setq num (- (1+ (cdr active)) (car active))))
15852        ((not (listp (cdr range)))
15853         ;; Fix a single (num . num) range according to the
15854         ;; active hash table.
15855         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15856         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15857         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15858         ;; Compute number of unread articles.
15859         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15860        (t
15861         ;; The read list is a list of ranges.  Fix them according to
15862         ;; the active hash table.
15863         ;; First peel off any elements that are below the lower
15864         ;; active limit.
15865         (while (and (cdr range)
15866                     (>= (car active)
15867                         (or (and (atom (cadr range)) (cadr range))
15868                             (caadr range))))
15869           (if (numberp (car range))
15870               (setcar range
15871                       (cons (car range)
15872                             (or (and (numberp (cadr range))
15873                                      (cadr range))
15874                                 (cdadr range))))
15875             (setcdr (car range)
15876                     (or (and (numberp (nth 1 range)) (nth 1 range))
15877                         (cdadr range))))
15878           (setcdr range (cddr range)))
15879         ;; Adjust the first element to be the same as the lower limit.
15880         (if (and (not (atom (car range)))
15881                  (< (cdar range) (car active)))
15882             (setcdr (car range) (1- (car active))))
15883         ;; Then we want to peel off any elements that are higher
15884         ;; than the upper active limit.
15885         (let ((srange range))
15886           ;; Go past all legal elements.
15887           (while (and (cdr srange)
15888                       (<= (or (and (atom (cadr srange))
15889                                    (cadr srange))
15890                               (caadr srange)) (cdr active)))
15891             (setq srange (cdr srange)))
15892           (if (cdr srange)
15893               ;; Nuke all remaining illegal elements.
15894               (setcdr srange nil))
15895
15896           ;; Adjust the final element.
15897           (if (and (not (atom (car srange)))
15898                    (> (cdar srange) (cdr active)))
15899               (setcdr (car srange) (cdr active))))
15900         ;; Compute the number of unread articles.
15901         (while range
15902           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15903                                       (cdar range)))
15904                               (or (and (atom (car range)) (car range))
15905                                   (caar range)))))
15906           (setq range (cdr range)))
15907         (setq num (max 0 (- (cdr active) num)))))
15908       ;; Set the number of unread articles.
15909       (when info
15910         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15911       num)))
15912
15913 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15914 ;; and compute how many unread articles there are in each group.
15915 (defun gnus-get-unread-articles (&optional level)
15916   (let* ((newsrc (cdr gnus-newsrc-alist))
15917          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15918          (foreign-level
15919           (min
15920            (cond ((and gnus-activate-foreign-newsgroups
15921                        (not (numberp gnus-activate-foreign-newsgroups)))
15922                   (1+ gnus-level-subscribed))
15923                  ((numberp gnus-activate-foreign-newsgroups)
15924                   gnus-activate-foreign-newsgroups)
15925                  (t 0))
15926            level))
15927          info group active method)
15928     (gnus-message 5 "Checking new news...")
15929
15930     (while newsrc
15931       (setq active (gnus-active (setq group (gnus-info-group
15932                                              (setq info (pop newsrc))))))
15933
15934       ;; Check newsgroups.  If the user doesn't want to check them, or
15935       ;; they can't be checked (for instance, if the news server can't
15936       ;; be reached) we just set the number of unread articles in this
15937       ;; newsgroup to t.  This means that Gnus thinks that there are
15938       ;; unread articles, but it has no idea how many.
15939       (if (and (setq method (gnus-info-method info))
15940                (not (gnus-server-equal
15941                      gnus-select-method
15942                      (setq method (gnus-server-get-method nil method))))
15943                (not (gnus-secondary-method-p method)))
15944           ;; These groups are foreign.  Check the level.
15945           (when (<= (gnus-info-level info) foreign-level)
15946             (setq active (gnus-activate-group group 'scan))
15947             (unless (inline (gnus-virtual-group-p group))
15948               (inline (gnus-close-group group)))
15949             (when (fboundp (intern (concat (symbol-name (car method))
15950                                            "-request-update-info")))
15951               (inline (gnus-request-update-info info method))))
15952         ;; These groups are native or secondary.
15953         (when (and (<= (gnus-info-level info) level)
15954                    (not gnus-read-active-file))
15955           (setq active (gnus-activate-group group 'scan))
15956           (inline (gnus-close-group group))))
15957
15958       ;; Get the number of unread articles in the group.
15959       (if active
15960           (inline (gnus-get-unread-articles-in-group info active))
15961         ;; The group couldn't be reached, so we nix out the number of
15962         ;; unread articles and stuff.
15963         (gnus-set-active group nil)
15964         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15965
15966     (gnus-message 5 "Checking new news...done")))
15967
15968 ;; Create a hash table out of the newsrc alist.  The `car's of the
15969 ;; alist elements are used as keys.
15970 (defun gnus-make-hashtable-from-newsrc-alist ()
15971   (let ((alist gnus-newsrc-alist)
15972         (ohashtb gnus-newsrc-hashtb)
15973         prev)
15974     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15975     (setq alist
15976           (setq prev (setq gnus-newsrc-alist
15977                            (if (equal (caar gnus-newsrc-alist)
15978                                       "dummy.group")
15979                                gnus-newsrc-alist
15980                              (cons (list "dummy.group" 0 nil) alist)))))
15981     (while alist
15982       (gnus-sethash
15983        (caar alist)
15984        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15985              prev)
15986        gnus-newsrc-hashtb)
15987       (setq prev alist
15988             alist (cdr alist)))))
15989
15990 (defun gnus-make-hashtable-from-killed ()
15991   "Create a hash table from the killed and zombie lists."
15992   (let ((lists '(gnus-killed-list gnus-zombie-list))
15993         list)
15994     (setq gnus-killed-hashtb
15995           (gnus-make-hashtable
15996            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15997     (while (setq list (pop lists))
15998       (setq list (symbol-value list))
15999       (while list
16000         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
16001
16002 (defun gnus-activate-group (group &optional scan dont-check)
16003   ;; Check whether a group has been activated or not.
16004   ;; If SCAN, request a scan of that group as well.
16005   (let ((method (gnus-find-method-for-group group))
16006         active)
16007     (and (gnus-check-server method)
16008          ;; We escape all bugs and quit here to make it possible to
16009          ;; continue if a group is so out-there that it reports bugs
16010          ;; and stuff.
16011          (progn
16012            (and scan
16013                 (gnus-check-backend-function 'request-scan (car method))
16014                 (gnus-request-scan group method))
16015            t)
16016          (condition-case ()
16017              (gnus-request-group group dont-check)
16018         ;   (error nil)
16019            (quit nil))
16020          (save-excursion
16021            (set-buffer nntp-server-buffer)
16022            (goto-char (point-min))
16023            ;; Parse the result we got from `gnus-request-group'.
16024            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
16025                 (progn
16026                   (goto-char (match-beginning 1))
16027                   (gnus-set-active
16028                    group (setq active (cons (read (current-buffer))
16029                                             (read (current-buffer)))))
16030                   ;; Return the new active info.
16031                   active))))))
16032
16033 (defun gnus-update-read-articles (group unread)
16034   "Update the list of read and ticked articles in GROUP using the
16035 UNREAD and TICKED lists.
16036 Note: UNSELECTED has to be sorted over `<'.
16037 Returns whether the updating was successful."
16038   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
16039          (entry (gnus-gethash group gnus-newsrc-hashtb))
16040          (info (nth 2 entry))
16041          (prev 1)
16042          (unread (sort (copy-sequence unread) '<))
16043          read)
16044     (if (or (not info) (not active))
16045         ;; There is no info on this group if it was, in fact,
16046         ;; killed.  Gnus stores no information on killed groups, so
16047         ;; there's nothing to be done.
16048         ;; One could store the information somewhere temporarily,
16049         ;; perhaps...  Hmmm...
16050         ()
16051       ;; Remove any negative articles numbers.
16052       (while (and unread (< (car unread) 0))
16053         (setq unread (cdr unread)))
16054       ;; Remove any expired article numbers
16055       (while (and unread (< (car unread) (car active)))
16056         (setq unread (cdr unread)))
16057       ;; Compute the ranges of read articles by looking at the list of
16058       ;; unread articles.
16059       (while unread
16060         (if (/= (car unread) prev)
16061             (setq read (cons (if (= prev (1- (car unread))) prev
16062                                (cons prev (1- (car unread)))) read)))
16063         (setq prev (1+ (car unread)))
16064         (setq unread (cdr unread)))
16065       (when (<= prev (cdr active))
16066         (setq read (cons (cons prev (cdr active)) read)))
16067       ;; Enter this list into the group info.
16068       (gnus-info-set-read
16069        info (if (> (length read) 1) (nreverse read) read))
16070       ;; Set the number of unread articles in gnus-newsrc-hashtb.
16071       (gnus-get-unread-articles-in-group info (gnus-active group))
16072       t)))
16073
16074 (defun gnus-make-articles-unread (group articles)
16075   "Mark ARTICLES in GROUP as unread."
16076   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
16077                           (gnus-gethash (gnus-group-real-name group)
16078                                         gnus-newsrc-hashtb))))
16079          (ranges (gnus-info-read info))
16080          news article)
16081     (while articles
16082       (when (gnus-member-of-range
16083              (setq article (pop articles)) ranges)
16084         (setq news (cons article news))))
16085     (when news
16086       (gnus-info-set-read
16087        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
16088       (gnus-group-update-group group t))))
16089
16090 ;; Enter all dead groups into the hashtb.
16091 (defun gnus-update-active-hashtb-from-killed ()
16092   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
16093         (lists (list gnus-killed-list gnus-zombie-list))
16094         killed)
16095     (while lists
16096       (setq killed (car lists))
16097       (while killed
16098         (gnus-sethash (car killed) nil hashtb)
16099         (setq killed (cdr killed)))
16100       (setq lists (cdr lists)))))
16101
16102 (defun gnus-get-killed-groups ()
16103   "Go through the active hashtb and all all unknown groups as killed."
16104   ;; First make sure active file has been read.
16105   (unless (gnus-read-active-file-p)
16106     (let ((gnus-read-active-file t))
16107       (gnus-read-active-file)))
16108   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
16109   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
16110   (mapatoms
16111    (lambda (sym)
16112      (let ((groups 0)
16113            (group (symbol-name sym)))
16114        (if (or (null group)
16115                (gnus-gethash group gnus-killed-hashtb)
16116                (gnus-gethash group gnus-newsrc-hashtb))
16117            ()
16118          (let ((do-sub (gnus-matches-options-n group)))
16119            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
16120                ()
16121              (setq groups (1+ groups))
16122              (setq gnus-killed-list
16123                    (cons group gnus-killed-list))
16124              (gnus-sethash group group gnus-killed-hashtb))))))
16125    gnus-active-hashtb))
16126
16127 ;; Get the active file(s) from the backend(s).
16128 (defun gnus-read-active-file ()
16129   (gnus-group-set-mode-line)
16130   (let ((methods 
16131          (append
16132           (if (gnus-check-server gnus-select-method)
16133               ;; The native server is available.
16134               (cons gnus-select-method gnus-secondary-select-methods)
16135             ;; The native server is down, so we just do the
16136             ;; secondary ones.
16137             gnus-secondary-select-methods)
16138           ;; Also read from the archive server.
16139           (when gnus-message-archive-method
16140             (list "archive"))))
16141         list-type)
16142     (setq gnus-have-read-active-file nil)
16143     (save-excursion
16144       (set-buffer nntp-server-buffer)
16145       (while methods
16146         (let* ((method (if (stringp (car methods))
16147                            (gnus-server-get-method nil (car methods))
16148                          (car methods)))
16149                (where (nth 1 method))
16150                (mesg (format "Reading active file%s via %s..."
16151                              (if (and where (not (zerop (length where))))
16152                                  (concat " from " where) "")
16153                              (car method))))
16154           (gnus-message 5 mesg)
16155           (when (gnus-check-server method)
16156             ;; Request that the backend scan its incoming messages.
16157             (and (gnus-check-backend-function 'request-scan (car method))
16158                  (gnus-request-scan nil method))
16159             (cond
16160              ((and (eq gnus-read-active-file 'some)
16161                    (gnus-check-backend-function 'retrieve-groups (car method)))
16162               (let ((newsrc (cdr gnus-newsrc-alist))
16163                     (gmethod (gnus-server-get-method nil method))
16164                     groups info)
16165                 (while (setq info (pop newsrc))
16166                   (when (gnus-server-equal
16167                          (gnus-find-method-for-group 
16168                           (gnus-info-group info) info)
16169                          gmethod)
16170                     (push (gnus-group-real-name (gnus-info-group info)) 
16171                           groups)))
16172                 (when groups
16173                   (gnus-check-server method)
16174                   (setq list-type (gnus-retrieve-groups groups method))
16175                   (cond
16176                    ((not list-type)
16177                     (gnus-error
16178                      1.2 "Cannot read partial active file from %s server."
16179                      (car method)))
16180                    ((eq list-type 'active)
16181                     (gnus-active-to-gnus-format method gnus-active-hashtb))
16182                    (t
16183                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
16184              (t
16185               (if (not (gnus-request-list method))
16186                   (unless (equal method gnus-message-archive-method)
16187                     (gnus-error 1 "Cannot read active file from %s server."
16188                                 (car method)))
16189                 (gnus-message 5 mesg)
16190                 (gnus-active-to-gnus-format method gnus-active-hashtb)
16191                 ;; We mark this active file as read.
16192                 (push method gnus-have-read-active-file)
16193                 (gnus-message 5 "%sdone" mesg))))))
16194         (setq methods (cdr methods))))))
16195
16196 ;; Read an active file and place the results in `gnus-active-hashtb'.
16197 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
16198   (unless method
16199     (setq method gnus-select-method))
16200   (let ((cur (current-buffer))
16201         (hashtb (or hashtb
16202                     (if (and gnus-active-hashtb
16203                              (not (equal method gnus-select-method)))
16204                         gnus-active-hashtb
16205                       (setq gnus-active-hashtb
16206                             (if (equal method gnus-select-method)
16207                                 (gnus-make-hashtable
16208                                  (count-lines (point-min) (point-max)))
16209                               (gnus-make-hashtable 4096)))))))
16210     ;; Delete unnecessary lines.
16211     (goto-char (point-min))
16212     (while (search-forward "\nto." nil t)
16213       (delete-region (1+ (match-beginning 0))
16214                      (progn (forward-line 1) (point))))
16215     (or (string= gnus-ignored-newsgroups "")
16216         (progn
16217           (goto-char (point-min))
16218           (delete-matching-lines gnus-ignored-newsgroups)))
16219     ;; Make the group names readable as a lisp expression even if they
16220     ;; contain special characters.
16221     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
16222     (goto-char (point-max))
16223     (while (re-search-backward "[][';?()#]" nil t)
16224       (insert ?\\))
16225     ;; If these are groups from a foreign select method, we insert the
16226     ;; group prefix in front of the group names.
16227     (and method (not (gnus-server-equal
16228                       (gnus-server-get-method nil method)
16229                       (gnus-server-get-method nil gnus-select-method)))
16230          (let ((prefix (gnus-group-prefixed-name "" method)))
16231            (goto-char (point-min))
16232            (while (and (not (eobp))
16233                        (progn (insert prefix)
16234                               (zerop (forward-line 1)))))))
16235     ;; Store the active file in a hash table.
16236     (goto-char (point-min))
16237     (if (string-match "%[oO]" gnus-group-line-format)
16238         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16239         ;; If we want information on moderated groups, we use this
16240         ;; loop...
16241         (let* ((mod-hashtb (make-vector 7 0))
16242                (m (intern "m" mod-hashtb))
16243                group max min)
16244           (while (not (eobp))
16245             (condition-case nil
16246                 (progn
16247                   (narrow-to-region (point) (gnus-point-at-eol))
16248                   (setq group (let ((obarray hashtb)) (read cur)))
16249                   (if (and (numberp (setq max (read cur)))
16250                            (numberp (setq min (read cur)))
16251                            (progn
16252                              (skip-chars-forward " \t")
16253                              (not
16254                               (or (= (following-char) ?=)
16255                                   (= (following-char) ?x)
16256                                   (= (following-char) ?j)))))
16257                       (set group (cons min max))
16258                     (set group nil))
16259                   ;; Enter moderated groups into a list.
16260                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16261                       (setq gnus-moderated-list
16262                             (cons (symbol-name group) gnus-moderated-list))))
16263               (error
16264                (and group
16265                     (symbolp group)
16266                     (set group nil))))
16267             (widen)
16268             (forward-line 1)))
16269       ;; And if we do not care about moderation, we use this loop,
16270       ;; which is faster.
16271       (let (group max min)
16272         (while (not (eobp))
16273           (condition-case ()
16274               (progn
16275                 (narrow-to-region (point) (gnus-point-at-eol))
16276                 ;; group gets set to a symbol interned in the hash table
16277                 ;; (what a hack!!) - jwz
16278                 (setq group (let ((obarray hashtb)) (read cur)))
16279                 (if (and (numberp (setq max (read cur)))
16280                          (numberp (setq min (read cur)))
16281                          (progn
16282                            (skip-chars-forward " \t")
16283                            (not
16284                             (or (= (following-char) ?=)
16285                                 (= (following-char) ?x)
16286                                 (= (following-char) ?j)))))
16287                     (set group (cons min max))
16288                   (set group nil)))
16289             (error
16290              (progn
16291                (and group
16292                     (symbolp group)
16293                     (set group nil))
16294                (or ignore-errors
16295                    (gnus-message 3 "Warning - illegal active: %s"
16296                                  (buffer-substring
16297                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16298           (widen)
16299           (forward-line 1))))))
16300
16301 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16302   ;; Parse a "groups" active file.
16303   (let ((cur (current-buffer))
16304         (hashtb (or hashtb
16305                     (if (and method gnus-active-hashtb)
16306                         gnus-active-hashtb
16307                       (setq gnus-active-hashtb
16308                             (gnus-make-hashtable
16309                              (count-lines (point-min) (point-max)))))))
16310         (prefix (and method
16311                      (not (gnus-server-equal
16312                            (gnus-server-get-method nil method)
16313                            (gnus-server-get-method nil gnus-select-method)))
16314                      (gnus-group-prefixed-name "" method))))
16315
16316     (goto-char (point-min))
16317     ;; We split this into to separate loops, one with the prefix
16318     ;; and one without to speed the reading up somewhat.
16319     (if prefix
16320         (let (min max opoint group)
16321           (while (not (eobp))
16322             (condition-case ()
16323                 (progn
16324                   (read cur) (read cur)
16325                   (setq min (read cur)
16326                         max (read cur)
16327                         opoint (point))
16328                   (skip-chars-forward " \t")
16329                   (insert prefix)
16330                   (goto-char opoint)
16331                   (set (let ((obarray hashtb)) (read cur))
16332                        (cons min max)))
16333               (error (and group (symbolp group) (set group nil))))
16334             (forward-line 1)))
16335       (let (min max group)
16336         (while (not (eobp))
16337           (condition-case ()
16338               (if (= (following-char) ?2)
16339                   (progn
16340                     (read cur) (read cur)
16341                     (setq min (read cur)
16342                           max (read cur))
16343                     (set (setq group (let ((obarray hashtb)) (read cur)))
16344                          (cons min max))))
16345             (error (and group (symbolp group) (set group nil))))
16346           (forward-line 1))))))
16347
16348 (defun gnus-read-newsrc-file (&optional force)
16349   "Read startup file.
16350 If FORCE is non-nil, the .newsrc file is read."
16351   ;; Reset variables that might be defined in the .newsrc.eld file.
16352   (let ((variables gnus-variable-list))
16353     (while variables
16354       (set (car variables) nil)
16355       (setq variables (cdr variables))))
16356   (let* ((newsrc-file gnus-current-startup-file)
16357          (quick-file (concat newsrc-file ".el")))
16358     (save-excursion
16359       ;; We always load the .newsrc.eld file.  If always contains
16360       ;; much information that can not be gotten from the .newsrc
16361       ;; file (ticked articles, killed groups, foreign methods, etc.)
16362       (gnus-read-newsrc-el-file quick-file)
16363
16364       (if (and (file-exists-p gnus-current-startup-file)
16365                (or force
16366                    (and (file-newer-than-file-p newsrc-file quick-file)
16367                         (file-newer-than-file-p newsrc-file
16368                                                 (concat quick-file "d")))
16369                    (not gnus-newsrc-alist)))
16370           ;; We read the .newsrc file.  Note that if there if a
16371           ;; .newsrc.eld file exists, it has already been read, and
16372           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16373           ;; the .newsrc file, Gnus will only use the information it
16374           ;; can find there for changing the data already read -
16375           ;; ie. reading the .newsrc file will not trash the data
16376           ;; already read (except for read articles).
16377           (save-excursion
16378             (gnus-message 5 "Reading %s..." newsrc-file)
16379             (set-buffer (find-file-noselect newsrc-file))
16380             (buffer-disable-undo (current-buffer))
16381             (gnus-newsrc-to-gnus-format)
16382             (kill-buffer (current-buffer))
16383             (gnus-message 5 "Reading %s...done" newsrc-file)))
16384
16385       ;; Read any slave files.
16386       (unless gnus-slave
16387         (gnus-master-read-slave-newsrc))
16388       
16389       ;; Convert old to new.
16390       (gnus-convert-old-newsrc))))
16391
16392 (defun gnus-continuum-version (version)
16393   "Return VERSION as a floating point number."
16394   (when (or (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16395             (string-match "^\\(.?\\)gnus-\\([0-9.]+\\)$" version))
16396     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16397            (number (match-string 2 version))
16398            major minor least)
16399       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16400       (setq major (string-to-number (match-string 1 number)))
16401       (setq minor (string-to-number (match-string 2 number)))
16402       (setq least (if (match-beginning 3)
16403                       (string-to-number (match-string 3 number))
16404                     0))
16405       (string-to-number
16406        (if (zerop major)
16407            (format "%s00%02d%02d"
16408                    (cond 
16409                     ((member alpha '("(ding)" "d")) "4.99")
16410                     ((member alpha '("September" "s")) "5.01")
16411                     ((member alpha '("Red" "r")) "5.03"))
16412                    minor least)
16413          (format "%d.%02d%02d" major minor least))))))
16414
16415 (defun gnus-convert-old-newsrc ()
16416   "Convert old newsrc into the new format, if needed."
16417   (let ((fcv (and gnus-newsrc-file-version
16418                   (gnus-continuum-version gnus-newsrc-file-version))))
16419     (cond
16420      ;; No .newsrc.eld file was loaded.
16421      ((null fcv) nil)
16422      ;; Gnus 5 .newsrc.eld was loaded.
16423      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16424       (gnus-convert-old-ticks)))))
16425
16426 (defun gnus-convert-old-ticks ()
16427   (let ((newsrc (cdr gnus-newsrc-alist))
16428         marks info dormant ticked)
16429     (while (setq info (pop newsrc))
16430       (when (setq marks (gnus-info-marks info))
16431         (setq dormant (cdr (assq 'dormant marks))
16432               ticked (cdr (assq 'tick marks)))
16433         (when (or dormant ticked)
16434           (gnus-info-set-read
16435            info
16436            (gnus-add-to-range
16437             (gnus-info-read info)
16438             (nconc (gnus-uncompress-range dormant)
16439                    (gnus-uncompress-range ticked)))))))))
16440
16441 (defun gnus-read-newsrc-el-file (file)
16442   (let ((ding-file (concat file "d")))
16443     ;; We always, always read the .eld file.
16444     (gnus-message 5 "Reading %s..." ding-file)
16445     (let (gnus-newsrc-assoc)
16446       (condition-case nil
16447           (load ding-file t t t)
16448         (error
16449          (gnus-error 1 "Error in %s" ding-file)))
16450       (when gnus-newsrc-assoc
16451         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16452     (gnus-make-hashtable-from-newsrc-alist)
16453     (when (file-newer-than-file-p file ding-file)
16454       ;; Old format quick file
16455       (gnus-message 5 "Reading %s..." file)
16456       ;; The .el file is newer than the .eld file, so we read that one
16457       ;; as well.
16458       (gnus-read-old-newsrc-el-file file))))
16459
16460 ;; Parse the old-style quick startup file
16461 (defun gnus-read-old-newsrc-el-file (file)
16462   (let (newsrc killed marked group m info)
16463     (prog1
16464         (let ((gnus-killed-assoc nil)
16465               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16466           (prog1
16467               (condition-case nil
16468                   (load file t t t)
16469                 (error nil))
16470             (setq newsrc gnus-newsrc-assoc
16471                   killed gnus-killed-assoc
16472                   marked gnus-marked-assoc)))
16473       (setq gnus-newsrc-alist nil)
16474       (while (setq group (pop newsrc))
16475         (if (setq info (gnus-get-info (car group)))
16476             (progn
16477               (gnus-info-set-read info (cddr group))
16478               (gnus-info-set-level
16479                info (if (nth 1 group) gnus-level-default-subscribed
16480                       gnus-level-default-unsubscribed))
16481               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16482           (push (setq info
16483                       (list (car group)
16484                             (if (nth 1 group) gnus-level-default-subscribed
16485                               gnus-level-default-unsubscribed)
16486                             (cddr group)))
16487                 gnus-newsrc-alist))
16488         ;; Copy marks into info.
16489         (when (setq m (assoc (car group) marked))
16490           (unless (nthcdr 3 info)
16491             (nconc info (list nil)))
16492           (gnus-info-set-marks
16493            info (list (cons 'tick (gnus-compress-sequence 
16494                                    (sort (cdr m) '<) t))))))
16495       (setq newsrc killed)
16496       (while newsrc
16497         (setcar newsrc (caar newsrc))
16498         (setq newsrc (cdr newsrc)))
16499       (setq gnus-killed-list killed))
16500     ;; The .el file version of this variable does not begin with
16501     ;; "options", while the .eld version does, so we just add it if it
16502     ;; isn't there.
16503     (and
16504      gnus-newsrc-options
16505      (progn
16506        (and (not (string-match "^ *options" gnus-newsrc-options))
16507             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16508        (and (not (string-match "\n$" gnus-newsrc-options))
16509             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16510        ;; Finally, if we read some options lines, we parse them.
16511        (or (string= gnus-newsrc-options "")
16512            (gnus-newsrc-parse-options gnus-newsrc-options))))
16513
16514     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16515     (gnus-make-hashtable-from-newsrc-alist)))
16516
16517 (defun gnus-make-newsrc-file (file)
16518   "Make server dependent file name by catenating FILE and server host name."
16519   (let* ((file (expand-file-name file nil))
16520          (real-file (concat file "-" (nth 1 gnus-select-method))))
16521     (if (or (file-exists-p real-file)
16522             (file-exists-p (concat real-file ".el"))
16523             (file-exists-p (concat real-file ".eld")))
16524         real-file file)))
16525
16526 (defun gnus-newsrc-to-gnus-format ()
16527   (setq gnus-newsrc-options "")
16528   (setq gnus-newsrc-options-n nil)
16529
16530   (or gnus-active-hashtb
16531       (setq gnus-active-hashtb (make-vector 4095 0)))
16532   (let ((buf (current-buffer))
16533         (already-read (> (length gnus-newsrc-alist) 1))
16534         group subscribed options-symbol newsrc Options-symbol
16535         symbol reads num1)
16536     (goto-char (point-min))
16537     ;; We intern the symbol `options' in the active hashtb so that we
16538     ;; can `eq' against it later.
16539     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16540     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16541
16542     (while (not (eobp))
16543       ;; We first read the first word on the line by narrowing and
16544       ;; then reading into `gnus-active-hashtb'.  Most groups will
16545       ;; already exist in that hashtb, so this will save some string
16546       ;; space.
16547       (narrow-to-region
16548        (point)
16549        (progn (skip-chars-forward "^ \t!:\n") (point)))
16550       (goto-char (point-min))
16551       (setq symbol
16552             (and (/= (point-min) (point-max))
16553                  (let ((obarray gnus-active-hashtb)) (read buf))))
16554       (widen)
16555       ;; Now, the symbol we have read is either `options' or a group
16556       ;; name.  If it is an options line, we just add it to a string.
16557       (cond
16558        ((or (eq symbol options-symbol)
16559             (eq symbol Options-symbol))
16560         (setq gnus-newsrc-options
16561               ;; This concating is quite inefficient, but since our
16562               ;; thorough studies show that approx 99.37% of all
16563               ;; .newsrc files only contain a single options line, we
16564               ;; don't give a damn, frankly, my dear.
16565               (concat gnus-newsrc-options
16566                       (buffer-substring
16567                        (gnus-point-at-bol)
16568                        ;; Options may continue on the next line.
16569                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16570                                 (progn (beginning-of-line) (point)))
16571                            (point)))))
16572         (forward-line -1))
16573        (symbol
16574         ;; Group names can be just numbers.  
16575         (when (numberp symbol) 
16576           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16577         (or (boundp symbol) (set symbol nil))
16578         ;; It was a group name.
16579         (setq subscribed (= (following-char) ?:)
16580               group (symbol-name symbol)
16581               reads nil)
16582         (if (eolp)
16583             ;; If the line ends here, this is clearly a buggy line, so
16584             ;; we put point a the beginning of line and let the cond
16585             ;; below do the error handling.
16586             (beginning-of-line)
16587           ;; We skip to the beginning of the ranges.
16588           (skip-chars-forward "!: \t"))
16589         ;; We are now at the beginning of the list of read articles.
16590         ;; We read them range by range.
16591         (while
16592             (cond
16593              ((looking-at "[0-9]+")
16594               ;; We narrow and read a number instead of buffer-substring/
16595               ;; string-to-int because it's faster.  narrow/widen is
16596               ;; faster than save-restriction/narrow, and save-restriction
16597               ;; produces a garbage object.
16598               (setq num1 (progn
16599                            (narrow-to-region (match-beginning 0) (match-end 0))
16600                            (read buf)))
16601               (widen)
16602               ;; If the next character is a dash, then this is a range.
16603               (if (= (following-char) ?-)
16604                   (progn
16605                     ;; We read the upper bound of the range.
16606                     (forward-char 1)
16607                     (if (not (looking-at "[0-9]+"))
16608                         ;; This is a buggy line, by we pretend that
16609                         ;; it's kinda OK.  Perhaps the user should be
16610                         ;; dinged?
16611                         (setq reads (cons num1 reads))
16612                       (setq reads
16613                             (cons
16614                              (cons num1
16615                                    (progn
16616                                      (narrow-to-region (match-beginning 0)
16617                                                        (match-end 0))
16618                                      (read buf)))
16619                              reads))
16620                       (widen)))
16621                 ;; It was just a simple number, so we add it to the
16622                 ;; list of ranges.
16623                 (setq reads (cons num1 reads)))
16624               ;; If the next char in ?\n, then we have reached the end
16625               ;; of the line and return nil.
16626               (/= (following-char) ?\n))
16627              ((= (following-char) ?\n)
16628               ;; End of line, so we end.
16629               nil)
16630              (t
16631               ;; Not numbers and not eol, so this might be a buggy
16632               ;; line...
16633               (or (eobp)
16634                   ;; If it was eob instead of ?\n, we allow it.
16635                   (progn
16636                     ;; The line was buggy.
16637                     (setq group nil)
16638                     (gnus-error 3.1 "Mangled line: %s"
16639                                 (buffer-substring (gnus-point-at-bol)
16640                                                   (gnus-point-at-eol)))))
16641               nil))
16642           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16643           ;; we allow them, because it's a common mistake to put a
16644           ;; space after the comma.
16645           (skip-chars-forward ", "))
16646
16647         ;; We have already read .newsrc.eld, so we gently update the
16648         ;; data in the hash table with the information we have just
16649         ;; read.
16650         (when group
16651           (let ((info (gnus-get-info group))
16652                 level)
16653             (if info
16654                 ;; There is an entry for this file in the alist.
16655                 (progn
16656                   (gnus-info-set-read info (nreverse reads))
16657                   ;; We update the level very gently.  In fact, we
16658                   ;; only change it if there's been a status change
16659                   ;; from subscribed to unsubscribed, or vice versa.
16660                   (setq level (gnus-info-level info))
16661                   (cond ((and (<= level gnus-level-subscribed)
16662                               (not subscribed))
16663                          (setq level (if reads
16664                                          gnus-level-default-unsubscribed
16665                                        (1+ gnus-level-default-unsubscribed))))
16666                         ((and (> level gnus-level-subscribed) subscribed)
16667                          (setq level gnus-level-default-subscribed)))
16668                   (gnus-info-set-level info level))
16669               ;; This is a new group.
16670               (setq info (list group
16671                                (if subscribed
16672                                    gnus-level-default-subscribed
16673                                  (if reads
16674                                      (1+ gnus-level-subscribed)
16675                                    gnus-level-default-unsubscribed))
16676                                (nreverse reads))))
16677             (setq newsrc (cons info newsrc))))))
16678       (forward-line 1))
16679
16680     (setq newsrc (nreverse newsrc))
16681
16682     (if (not already-read)
16683         ()
16684       ;; We now have two newsrc lists - `newsrc', which is what we
16685       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16686       ;; what we've read from .newsrc.eld.  We have to merge these
16687       ;; lists.  We do this by "attaching" any (foreign) groups in the
16688       ;; gnus-newsrc-alist to the (native) group that precedes them.
16689       (let ((rc (cdr gnus-newsrc-alist))
16690             (prev gnus-newsrc-alist)
16691             entry mentry)
16692         (while rc
16693           (or (null (nth 4 (car rc)))   ; It's a native group.
16694               (assoc (caar rc) newsrc) ; It's already in the alist.
16695               (if (setq entry (assoc (caar prev) newsrc))
16696                   (setcdr (setq mentry (memq entry newsrc))
16697                           (cons (car rc) (cdr mentry)))
16698                 (setq newsrc (cons (car rc) newsrc))))
16699           (setq prev rc
16700                 rc (cdr rc)))))
16701
16702     (setq gnus-newsrc-alist newsrc)
16703     ;; We make the newsrc hashtb.
16704     (gnus-make-hashtable-from-newsrc-alist)
16705
16706     ;; Finally, if we read some options lines, we parse them.
16707     (or (string= gnus-newsrc-options "")
16708         (gnus-newsrc-parse-options gnus-newsrc-options))))
16709
16710 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16711 ;; The return value will be a list on the form
16712 ;; ((regexp1 . ignore)
16713 ;;  (regexp2 . subscribe)...)
16714 ;; When handling new newsgroups, groups that match a `ignore' regexp
16715 ;; will be ignored, and groups that match a `subscribe' regexp will be
16716 ;; subscribed.  A line like
16717 ;; options -n !all rec.all
16718 ;; will lead to a list that looks like
16719 ;; (("^rec\\..+" . subscribe)
16720 ;;  ("^.+" . ignore))
16721 ;; So all "rec.*" groups will be subscribed, while all the other
16722 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16723 ;; different from "options -n rec.all !all".
16724 (defun gnus-newsrc-parse-options (options)
16725   (let (out eol)
16726     (save-excursion
16727       (gnus-set-work-buffer)
16728       (insert (regexp-quote options))
16729       ;; First we treat all continuation lines.
16730       (goto-char (point-min))
16731       (while (re-search-forward "\n[ \t]+" nil t)
16732         (replace-match " " t t))
16733       ;; Then we transform all "all"s into ".+"s.
16734       (goto-char (point-min))
16735       (while (re-search-forward "\\ball\\b" nil t)
16736         (replace-match ".+" t t))
16737       (goto-char (point-min))
16738       ;; We remove all other options than the "-n" ones.
16739       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16740         (replace-match " ")
16741         (forward-char -1))
16742       (goto-char (point-min))
16743
16744       ;; We are only interested in "options -n" lines - we
16745       ;; ignore the other option lines.
16746       (while (re-search-forward "[ \t]-n" nil t)
16747         (setq eol
16748               (or (save-excursion
16749                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16750                          (- (point) 2)))
16751                   (gnus-point-at-eol)))
16752         ;; Search for all "words"...
16753         (while (re-search-forward "[^ \t,\n]+" eol t)
16754           (if (= (char-after (match-beginning 0)) ?!)
16755               ;; If the word begins with a bang (!), this is a "not"
16756               ;; spec.  We put this spec (minus the bang) and the
16757               ;; symbol `ignore' into the list.
16758               (setq out (cons (cons (concat
16759                                      "^" (buffer-substring
16760                                           (1+ (match-beginning 0))
16761                                           (match-end 0)))
16762                                     'ignore) out))
16763             ;; There was no bang, so this is a "yes" spec.
16764             (setq out (cons (cons (concat "^" (match-string 0))
16765                                   'subscribe) out)))))
16766
16767       (setq gnus-newsrc-options-n out))))
16768
16769 (defun gnus-save-newsrc-file (&optional force)
16770   "Save .newsrc file."
16771   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16772   ;; from the variable gnus-newsrc-alist.
16773   (when (and (or gnus-newsrc-alist gnus-killed-list)
16774              gnus-current-startup-file)
16775     (save-excursion
16776       (if (and (or gnus-use-dribble-file gnus-slave)
16777                (not force)
16778                (or (not gnus-dribble-buffer)
16779                    (not (buffer-name gnus-dribble-buffer))
16780                    (zerop (save-excursion
16781                             (set-buffer gnus-dribble-buffer)
16782                             (buffer-size)))))
16783           (gnus-message 4 "(No changes need to be saved)")
16784         (run-hooks 'gnus-save-newsrc-hook)
16785         (if gnus-slave
16786             (gnus-slave-save-newsrc)
16787           ;; Save .newsrc.
16788           (when gnus-save-newsrc-file
16789             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16790             (gnus-gnus-to-newsrc-format)
16791             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16792           ;; Save .newsrc.eld.
16793           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16794           (make-local-variable 'version-control)
16795           (setq version-control 'never)
16796           (setq buffer-file-name
16797                 (concat gnus-current-startup-file ".eld"))
16798           (setq default-directory (file-name-directory buffer-file-name))
16799           (gnus-add-current-to-buffer-list)
16800           (buffer-disable-undo (current-buffer))
16801           (erase-buffer)
16802           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16803           (gnus-gnus-to-quick-newsrc-format)
16804           (run-hooks 'gnus-save-quick-newsrc-hook)
16805           (save-buffer)
16806           (kill-buffer (current-buffer))
16807           (gnus-message
16808            5 "Saving %s.eld...done" gnus-current-startup-file))
16809         (gnus-dribble-delete-file)
16810         (gnus-group-set-mode-line)))))
16811
16812 (defun gnus-gnus-to-quick-newsrc-format ()
16813   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16814   (insert ";; Gnus startup file.\n")
16815   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16816   (insert ";; to read .newsrc.\n")
16817   (insert "(setq gnus-newsrc-file-version "
16818           (prin1-to-string gnus-version) ")\n")
16819   (let ((variables
16820          (if gnus-save-killed-list gnus-variable-list
16821            ;; Remove the `gnus-killed-list' from the list of variables
16822            ;; to be saved, if required.
16823            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16824         ;; Peel off the "dummy" group.
16825         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16826         variable)
16827     ;; Insert the variables into the file.
16828     (while variables
16829       (when (and (boundp (setq variable (pop variables)))
16830                  (symbol-value variable))
16831         (insert "(setq " (symbol-name variable) " '")
16832         (prin1 (symbol-value variable) (current-buffer))
16833         (insert ")\n")))))
16834
16835 (defun gnus-gnus-to-newsrc-format ()
16836   ;; Generate and save the .newsrc file.
16837   (save-excursion
16838     (set-buffer (create-file-buffer gnus-current-startup-file))
16839     (let ((newsrc (cdr gnus-newsrc-alist))
16840           (standard-output (current-buffer))
16841           info ranges range method)
16842       (setq buffer-file-name gnus-current-startup-file)
16843       (setq default-directory (file-name-directory buffer-file-name))
16844       (buffer-disable-undo (current-buffer))
16845       (erase-buffer)
16846       ;; Write options.
16847       (if gnus-newsrc-options (insert gnus-newsrc-options))
16848       ;; Write subscribed and unsubscribed.
16849       (while (setq info (pop newsrc))
16850         ;; Don't write foreign groups to .newsrc.
16851         (when (or (null (setq method (gnus-info-method info)))
16852                   (equal method "native")
16853                   (gnus-server-equal method gnus-select-method))
16854           (insert (gnus-info-group info)
16855                   (if (> (gnus-info-level info) gnus-level-subscribed)
16856                       "!" ":"))
16857           (when (setq ranges (gnus-info-read info))
16858             (insert " ")
16859             (if (not (listp (cdr ranges)))
16860                 (if (= (car ranges) (cdr ranges))
16861                     (princ (car ranges))
16862                   (princ (car ranges))
16863                   (insert "-")
16864                   (princ (cdr ranges)))
16865               (while (setq range (pop ranges))
16866                 (if (or (atom range) (= (car range) (cdr range)))
16867                     (princ (or (and (atom range) range) (car range)))
16868                   (princ (car range))
16869                   (insert "-")
16870                   (princ (cdr range)))
16871                 (if ranges (insert ",")))))
16872           (insert "\n")))
16873       (make-local-variable 'version-control)
16874       (setq version-control 'never)
16875       ;; It has been reported that sometime the modtime on the .newsrc
16876       ;; file seems to be off.  We really do want to overwrite it, so
16877       ;; we clear the modtime here before saving.  It's a bit odd,
16878       ;; though...
16879       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16880       ;; delete the silly thing entirely first.  but this fails to provide
16881       ;; such niceties as .newsrc~ creation.
16882       (if gnus-modtime-botch
16883           (delete-file gnus-startup-file)
16884         (clear-visited-file-modtime))
16885       (run-hooks 'gnus-save-standard-newsrc-hook)
16886       (save-buffer)
16887       (kill-buffer (current-buffer)))))
16888
16889 \f
16890 ;;;
16891 ;;; Slave functions.
16892 ;;;
16893
16894 (defun gnus-slave-save-newsrc ()
16895   (save-excursion
16896     (set-buffer gnus-dribble-buffer)
16897     (let ((slave-name
16898            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16899       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16900
16901 (defun gnus-master-read-slave-newsrc ()
16902   (let ((slave-files
16903          (directory-files
16904           (file-name-directory gnus-current-startup-file)
16905           t (concat
16906              "^" (regexp-quote
16907                   (concat
16908                    (file-name-nondirectory gnus-current-startup-file)
16909                    "-slave-")))
16910           t))
16911         file)
16912     (if (not slave-files)
16913         ()                              ; There are no slave files to read.
16914       (gnus-message 7 "Reading slave newsrcs...")
16915       (save-excursion
16916         (set-buffer (get-buffer-create " *gnus slave*"))
16917         (buffer-disable-undo (current-buffer))
16918         (setq slave-files
16919               (sort (mapcar (lambda (file)
16920                               (list (nth 5 (file-attributes file)) file))
16921                             slave-files)
16922                     (lambda (f1 f2)
16923                       (or (< (caar f1) (caar f2))
16924                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16925         (while slave-files
16926           (erase-buffer)
16927           (setq file (nth 1 (car slave-files)))
16928           (insert-file-contents file)
16929           (if (condition-case ()
16930                   (progn
16931                     (eval-buffer (current-buffer))
16932                     t)
16933                 (error
16934                  (gnus-error 3.2 "Possible error in %s" file)
16935                  nil))
16936               (or gnus-slave ; Slaves shouldn't delete these files.
16937                   (condition-case ()
16938                       (delete-file file)
16939                     (error nil))))
16940           (setq slave-files (cdr slave-files))))
16941       (gnus-message 7 "Reading slave newsrcs...done"))))
16942
16943 \f
16944 ;;;
16945 ;;; Group description.
16946 ;;;
16947
16948 (defun gnus-read-all-descriptions-files ()
16949   (let ((methods (cons gnus-select-method 
16950                        (nconc
16951                         (when gnus-message-archive-method
16952                           (list "archive"))
16953                         gnus-secondary-select-methods))))
16954     (while methods
16955       (gnus-read-descriptions-file (car methods))
16956       (setq methods (cdr methods)))
16957     t))
16958
16959 (defun gnus-read-descriptions-file (&optional method)
16960   (let ((method (or method gnus-select-method)))
16961     (when (stringp method)
16962       (setq method (gnus-server-to-method method)))
16963     ;; We create the hashtable whether we manage to read the desc file
16964     ;; to avoid trying to re-read after a failed read.
16965     (or gnus-description-hashtb
16966         (setq gnus-description-hashtb
16967               (gnus-make-hashtable (length gnus-active-hashtb))))
16968     ;; Mark this method's desc file as read.
16969     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16970                   gnus-description-hashtb)
16971
16972     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16973     (cond
16974      ((not (gnus-check-server method))
16975       (gnus-message 1 "Couldn't open server")
16976       nil)
16977      ((not (gnus-request-list-newsgroups method))
16978       (gnus-message 1 "Couldn't read newsgroups descriptions")
16979       nil)
16980      (t
16981       (let (group)
16982         (save-excursion
16983           (save-restriction
16984             (set-buffer nntp-server-buffer)
16985             (goto-char (point-min))
16986             (if (or (search-forward "\n.\n" nil t)
16987                     (goto-char (point-max)))
16988                 (progn
16989                   (beginning-of-line)
16990                   (narrow-to-region (point-min) (point))))
16991             (goto-char (point-min))
16992             (while (not (eobp))
16993               ;; If we get an error, we set group to 0, which is not a
16994               ;; symbol...
16995               (setq group
16996                     (condition-case ()
16997                         (let ((obarray gnus-description-hashtb))
16998                           ;; Group is set to a symbol interned in this
16999                           ;; hash table.
17000                           (read nntp-server-buffer))
17001                       (error 0)))
17002               (skip-chars-forward " \t")
17003               ;; ...  which leads to this line being effectively ignored.
17004               (and (symbolp group)
17005                    (set group (buffer-substring
17006                                (point) (progn (end-of-line) (point)))))
17007               (forward-line 1))))
17008         (gnus-message 5 "Reading descriptions file...done")
17009         t)))))
17010
17011 (defun gnus-group-get-description (group)
17012   "Get the description of a group by sending XGTITLE to the server."
17013   (when (gnus-request-group-description group)
17014     (save-excursion
17015       (set-buffer nntp-server-buffer)
17016       (goto-char (point-min))
17017       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
17018         (match-string 1)))))
17019
17020 \f
17021 ;;;
17022 ;;; Buffering of read articles.
17023 ;;;
17024
17025 (defvar gnus-backlog-buffer " *Gnus Backlog*")
17026 (defvar gnus-backlog-articles nil)
17027 (defvar gnus-backlog-hashtb nil)
17028
17029 (defun gnus-backlog-buffer ()
17030   "Return the backlog buffer."
17031   (or (get-buffer gnus-backlog-buffer)
17032       (save-excursion
17033         (set-buffer (get-buffer-create gnus-backlog-buffer))
17034         (buffer-disable-undo (current-buffer))
17035         (setq buffer-read-only t)
17036         (gnus-add-current-to-buffer-list)
17037         (get-buffer gnus-backlog-buffer))))
17038
17039 (defun gnus-backlog-setup ()
17040   "Initialize backlog variables."
17041   (unless gnus-backlog-hashtb
17042     (setq gnus-backlog-hashtb (make-vector 1023 0))))
17043
17044 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
17045
17046 (defun gnus-backlog-shutdown ()
17047   "Clear all backlog variables and buffers."
17048   (when (get-buffer gnus-backlog-buffer)
17049     (kill-buffer gnus-backlog-buffer))
17050   (setq gnus-backlog-hashtb nil
17051         gnus-backlog-articles nil))
17052
17053 (defun gnus-backlog-enter-article (group number buffer)
17054   (gnus-backlog-setup)
17055   (let ((ident (intern (concat group ":" (int-to-string number))
17056                        gnus-backlog-hashtb))
17057         b)
17058     (if (memq ident gnus-backlog-articles)
17059         () ; It's already kept.
17060       ;; Remove the oldest article, if necessary.
17061       (and (numberp gnus-keep-backlog)
17062            (>= (length gnus-backlog-articles) gnus-keep-backlog)
17063            (gnus-backlog-remove-oldest-article))
17064       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
17065       ;; Insert the new article.
17066       (save-excursion
17067         (set-buffer (gnus-backlog-buffer))
17068         (let (buffer-read-only)
17069           (goto-char (point-max))
17070           (or (bolp) (insert "\n"))
17071           (setq b (point))
17072           (insert-buffer-substring buffer)
17073           ;; Tag the beginning of the article with the ident.
17074           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
17075
17076 (defun gnus-backlog-remove-oldest-article ()
17077   (save-excursion
17078     (set-buffer (gnus-backlog-buffer))
17079     (goto-char (point-min))
17080     (if (zerop (buffer-size))
17081         () ; The buffer is empty.
17082       (let ((ident (get-text-property (point) 'gnus-backlog))
17083             buffer-read-only)
17084         ;; Remove the ident from the list of articles.
17085         (when ident
17086           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17087         ;; Delete the article itself.
17088         (delete-region
17089          (point) (next-single-property-change
17090                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
17091
17092 (defun gnus-backlog-remove-article (group number)
17093   "Remove article NUMBER in GROUP from the backlog."
17094   (when (numberp number)
17095     (gnus-backlog-setup)
17096     (let ((ident (intern (concat group ":" (int-to-string number))
17097                          gnus-backlog-hashtb))
17098           beg end)
17099       (when (memq ident gnus-backlog-articles)
17100         ;; It was in the backlog.
17101         (save-excursion
17102           (set-buffer (gnus-backlog-buffer))
17103           (let (buffer-read-only)
17104             (when (setq beg (text-property-any
17105                              (point-min) (point-max) 'gnus-backlog
17106                              ident))
17107               ;; Find the end (i. e., the beginning of the next article).
17108               (setq end
17109                     (next-single-property-change
17110                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
17111               (delete-region beg end)
17112               ;; Return success.
17113               t)))))))
17114
17115 (defun gnus-backlog-request-article (group number buffer)
17116   (when (numberp number)
17117     (gnus-backlog-setup)
17118     (let ((ident (intern (concat group ":" (int-to-string number))
17119                          gnus-backlog-hashtb))
17120           beg end)
17121       (when (memq ident gnus-backlog-articles)
17122         ;; It was in the backlog.
17123         (save-excursion
17124           (set-buffer (gnus-backlog-buffer))
17125           (if (not (setq beg (text-property-any
17126                               (point-min) (point-max) 'gnus-backlog
17127                               ident)))
17128               ;; It wasn't in the backlog after all.
17129               (ignore
17130                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17131             ;; Find the end (i. e., the beginning of the next article).
17132             (setq end
17133                   (next-single-property-change
17134                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
17135         (let ((buffer-read-only nil))
17136           (erase-buffer)
17137           (insert-buffer-substring gnus-backlog-buffer beg end)
17138           t)))))
17139
17140 ;; Allow redefinition of Gnus functions.
17141
17142 (gnus-ems-redefine)
17143
17144 (provide 'gnus)
17145
17146 ;;; gnus.el ends here