*** 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@sunsite.doc.ic.ac.uk:/pub/usenet/news-faqs/"
195     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
196     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
197     "/ftp@rtfm.mit.edu:/pub/usenet/"
198     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
199     "/ftp@ftp.sunet.se:/pub/usenet/"
200     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
201     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
202     "/ftp@ftp.hk.super.net:/mirror/faqs/")
203   "*Directory where the group FAQs are stored.
204 This will most commonly be on a remote machine, and the file will be
205 fetched by ange-ftp.
206
207 This variable can also be a list of directories.  In that case, the
208 first element in the list will be used by default, and the others will
209 be used as backup sites.
210
211 Note that Gnus uses an aol machine as the default directory.  If this
212 feels fundamentally unclean, just think of it as a way to finally get
213 something of value back from them.
214
215 If the default site is too slow, try one of these:
216
217    North America: mirrors.aol.com                /pub/rtfm/usenet
218                   ftp.seas.gwu.edu               /pub/rtfm
219                   rtfm.mit.edu                   /pub/usenet
220    Europe:        ftp.uni-paderborn.de           /pub/FAQ
221                   src.doc.ic.ac.uk               /usenet/news-FAQS
222                   ftp.sunet.se                   /pub/usenet
223                   sunsite.auc.dk                 /pub/usenet
224    Asia:          nctuccca.edu.tw                /USENET/FAQ
225                   hwarang.postech.ac.kr          /pub/usenet
226                   ftp.hk.super.net               /mirror/faqs")
227
228 (defvar gnus-group-archive-directory
229   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
230   "*The address of the (ding) archives.")
231
232 (defvar gnus-group-recent-archive-directory
233   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
234   "*The address of the most recent (ding) articles.")
235
236 (defvar gnus-default-subscribed-newsgroups nil
237   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
238 It should be a list of strings.
239 If it is `t', Gnus will not do anything special the first time it is
240 started; it'll just use the normal newsgroups subscription methods.")
241
242 (defvar gnus-use-cross-reference t
243   "*Non-nil means that cross referenced articles will be marked as read.
244 If nil, ignore cross references.  If t, mark articles as read in
245 subscribed newsgroups.  If neither t nor nil, mark as read in all
246 newsgroups.")
247
248 (defvar gnus-single-article-buffer t
249   "*If non-nil, display all articles in the same buffer.
250 If nil, each group will get its own article buffer.")
251
252 (defvar gnus-use-dribble-file t
253   "*Non-nil means that Gnus will use a dribble file to store user updates.
254 If Emacs should crash without saving the .newsrc files, complete
255 information can be restored from the dribble file.")
256
257 (defvar gnus-dribble-directory nil
258   "*The directory where dribble files will be saved.
259 If this variable is nil, the directory where the .newsrc files are
260 saved will be used.")
261
262 (defvar gnus-asynchronous nil
263   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
264
265 (defvar gnus-kill-summary-on-exit t
266   "*If non-nil, kill the summary buffer when you exit from it.
267 If nil, the summary will become a \"*Dead Summary*\" buffer, and
268 it will be killed sometime later.")
269
270 (defvar gnus-large-newsgroup 200
271   "*The number of articles which indicates a large newsgroup.
272 If the number of articles in a newsgroup is greater than this value,
273 confirmation is required for selecting the newsgroup.")
274
275 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
276 (defvar gnus-no-groups-message "No news is horrible news"
277   "*Message displayed by Gnus when no groups are available.")
278
279 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
280   "*Non-nil means that the default name of a file to save articles in is the group name.
281 If it's nil, the directory form of the group name is used instead.
282
283 If this variable is a list, and the list contains the element
284 `not-score', long file names will not be used for score files; if it
285 contains the element `not-save', long file names will not be used for
286 saving; and if it contains the element `not-kill', long file names
287 will not be used for kill files.
288
289 Note that the default for this variable varies according to what system
290 type you're using.  On `usg-unix-v' and `xenix' this variable defaults
291 to nil while on all other systems it defaults to t.")
292
293 (defvar gnus-article-save-directory gnus-directory
294   "*Name of the directory articles will be saved in (default \"~/News\").")
295
296 (defvar gnus-kill-files-directory gnus-directory
297   "*Name of the directory where kill files will be stored (default \"~/News\").")
298
299 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
300   "*A function to save articles in your favorite format.
301 The function must be interactively callable (in other words, it must
302 be an Emacs command).
303
304 Gnus provides the following functions:
305
306 * gnus-summary-save-in-rmail (Rmail format)
307 * gnus-summary-save-in-mail (Unix mail format)
308 * gnus-summary-save-in-folder (MH folder)
309 * gnus-summary-save-in-file (article format).
310 * gnus-summary-save-in-vm (use VM's folder format).")
311
312 (defvar gnus-prompt-before-saving 'always
313   "*This variable says how much prompting is to be done when saving articles.
314 If it is nil, no prompting will be done, and the articles will be
315 saved to the default files.  If this variable is `always', each and
316 every article that is saved will be preceded by a prompt, even when
317 saving large batches of articles.  If this variable is neither nil not
318 `always', there the user will be prompted once for a file name for
319 each invocation of the saving commands.")
320
321 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
322   "*A function generating a file name to save articles in Rmail format.
323 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
324
325 (defvar gnus-mail-save-name (function gnus-plain-save-name)
326   "*A function generating a file name to save articles in Unix mail format.
327 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
328
329 (defvar gnus-folder-save-name (function gnus-folder-save-name)
330   "*A function generating a file name to save articles in MH folder.
331 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
332
333 (defvar gnus-file-save-name (function gnus-numeric-save-name)
334   "*A function generating a file name to save articles in article format.
335 The function is called with NEWSGROUP, HEADERS, and optional
336 LAST-FILE.")
337
338 (defvar gnus-split-methods
339   '((gnus-article-archive-name))
340   "*Variable used to suggest where articles are to be saved.
341 For instance, if you would like to save articles related to Gnus in
342 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
343 you could set this variable to something like:
344
345  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
346    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
347
348 This variable is an alist where the where the key is the match and the
349 value is a list of possible files to save in if the match is non-nil.
350
351 If the match is a string, it is used as a regexp match on the
352 article.  If the match is a symbol, that symbol will be funcalled
353 from the buffer of the article to be saved with the newsgroup as the
354 parameter.  If it is a list, it will be evaled in the same buffer.
355
356 If this form or function returns a string, this string will be used as
357 a possible file name; and if it returns a non-nil list, that list will
358 be used as possible file names.")
359
360 (defvar gnus-move-split-methods nil
361   "*Variable used to suggest where articles are to be moved to.
362 It uses the same syntax as the `gnus-split-methods' variable.")
363
364 (defvar gnus-save-score nil
365   "*If non-nil, save group scoring info.")
366
367 (defvar gnus-use-adaptive-scoring nil
368   "*If non-nil, use some adaptive scoring scheme.")
369
370 (defvar gnus-use-cache 'passive
371   "*If nil, Gnus will ignore the article cache.
372 If `passive', it will allow entering (and reading) articles
373 explicitly entered into the cache.  If anything else, use the
374 cache to the full extent of the law.")
375
376 (defvar gnus-use-trees nil
377   "*If non-nil, display a thread tree buffer.")
378
379 (defvar gnus-use-grouplens nil
380   "*If non-nil, use GroupLens ratings.")
381
382 (defvar gnus-keep-backlog nil
383   "*If non-nil, Gnus will keep read articles for later re-retrieval.
384 If it is a number N, then Gnus will only keep the last N articles
385 read.  If it is neither nil nor a number, Gnus will keep all read
386 articles.  This is not a good idea.")
387
388 (defvar gnus-use-nocem nil
389   "*If non-nil, Gnus will read NoCeM cancel messages.")
390
391 (defvar gnus-use-demon nil
392   "If non-nil, Gnus might use some demons.")
393
394 (defvar gnus-use-scoring t
395   "*If non-nil, enable scoring.")
396
397 (defvar gnus-use-picons nil
398   "*If non-nil, display picons.")
399
400 (defvar gnus-fetch-old-headers nil
401   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
402 If an unread article in the group refers to an older, already read (or
403 just marked as read) article, the old article will not normally be
404 displayed in the Summary buffer.  If this variable is non-nil, Gnus
405 will attempt to grab the headers to the old articles, and thereby
406 build complete threads.  If it has the value `some', only enough
407 headers to connect otherwise loose threads will be displayed.
408 This variable can also be a number.  In that case, no more than that
409 number of old headers will be fetched.
410
411 The server has to support NOV for any of this to work.")
412
413 ;see gnus-cus.el
414 ;(defvar gnus-visual t
415 ;  "*If non-nil, will do various highlighting.
416 ;If nil, no mouse highlights (or any other highlights) will be
417 ;performed.  This might speed up Gnus some when generating large group
418 ;and summary buffers.")
419
420 (defvar gnus-novice-user t
421   "*Non-nil means that you are a usenet novice.
422 If non-nil, verbose messages may be displayed and confirmations may be
423 required.")
424
425 (defvar gnus-expert-user nil
426   "*Non-nil means that you will never be asked for confirmation about anything.
427 And that means *anything*.")
428
429 (defvar gnus-verbose 7
430   "*Integer that says how verbose Gnus should be.
431 The higher the number, the more messages Gnus will flash to say what
432 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
433 display most important messages; and at ten, Gnus will keep on
434 jabbering all the time.")
435
436 (defvar gnus-keep-same-level nil
437   "*Non-nil means that the next newsgroup after the current will be on the same level.
438 When you type, for instance, `n' after reading the last article in the
439 current newsgroup, you will go to the next newsgroup.  If this variable
440 is nil, the next newsgroup will be the next from the group
441 buffer.
442 If this variable is non-nil, Gnus will either put you in the
443 next newsgroup with the same level, or, if no such newsgroup is
444 available, the next newsgroup with the lowest possible level higher
445 than the current level.
446 If this variable is `best', Gnus will make the next newsgroup the one
447 with the best level.")
448
449 (defvar gnus-summary-make-false-root 'adopt
450   "*nil means that Gnus won't gather loose threads.
451 If the root of a thread has expired or been read in a previous
452 session, the information necessary to build a complete thread has been
453 lost.  Instead of having many small sub-threads from this original thread
454 scattered all over the summary buffer, Gnus can gather them.
455
456 If non-nil, Gnus will try to gather all loose sub-threads from an
457 original thread into one large thread.
458
459 If this variable is non-nil, it should be one of `none', `adopt',
460 `dummy' or `empty'.
461
462 If this variable is `none', Gnus will not make a false root, but just
463 present the sub-threads after another.
464 If this variable is `dummy', Gnus will create a dummy root that will
465 have all the sub-threads as children.
466 If this variable is `adopt', Gnus will make one of the \"children\"
467 the parent and mark all the step-children as such.
468 If this variable is `empty', the \"children\" are printed with empty
469 subject fields.  (Or rather, they will be printed with a string
470 given by the `gnus-summary-same-subject' variable.)")
471
472 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
473   "*A regexp to match subjects to be excluded from loose thread gathering.
474 As loose thread gathering is done on subjects only, that means that
475 there can be many false gatherings performed.  By rooting out certain
476 common subjects, gathering might become saner.")
477
478 (defvar gnus-summary-gather-subject-limit nil
479   "*Maximum length of subject comparisons when gathering loose threads.
480 Use nil to compare full subjects.  Setting this variable to a low
481 number will help gather threads that have been corrupted by
482 newsreaders chopping off subject lines, but it might also mean that
483 unrelated articles that have subject that happen to begin with the
484 same few characters will be incorrectly gathered.
485
486 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
487 comparing subjects.")
488
489 (defvar gnus-simplify-ignored-prefixes nil
490   "*Regexp, matches for which are removed from subject lines when simplifying.")
491
492 (defvar gnus-build-sparse-threads nil
493   "*If non-nil, fill in the gaps in threads.
494 If `some', only fill in the gaps that are needed to tie loose threads
495 together.  If `more', fill in all leaf nodes that Gnus can find.  If
496 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
497
498 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
499   "Function used for gathering loose threads.
500 There are two pre-defined functions: `gnus-gather-threads-by-subject',
501 which only takes Subjects into consideration; and
502 `gnus-gather-threads-by-references', which compared the References
503 headers of the articles to find matches.")
504
505 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
506 (defvar gnus-summary-same-subject ""
507   "*String indicating that the current article has the same subject as the previous.
508 This variable will only be used if the value of
509 `gnus-summary-make-false-root' is `empty'.")
510
511 (defvar gnus-summary-goto-unread t
512   "*If non-nil, marking commands will go to the next unread article.
513 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
514 whether it is read or not.")
515
516 (defvar gnus-group-goto-unread t
517   "*If non-nil, movement commands will go to the next unread and subscribed group.")
518
519 (defvar gnus-goto-next-group-when-activating t
520   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
521
522 (defvar gnus-check-new-newsgroups t
523   "*Non-nil means that Gnus will add new newsgroups at startup.
524 If this variable is `ask-server', Gnus will ask the server for new
525 groups since the last time it checked.  This means that the killed list
526 is no longer necessary, so you could set `gnus-save-killed-list' to
527 nil.
528
529 A variant is to have this variable be a list of select methods.  Gnus
530 will then use the `ask-server' method on all these select methods to
531 query for new groups from all those servers.
532
533 Eg.
534   (setq gnus-check-new-newsgroups
535         '((nntp \"some.server\") (nntp \"other.server\")))
536
537 If this variable is nil, then you have to tell Gnus explicitly to
538 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
539
540 (defvar gnus-check-bogus-newsgroups nil
541   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
542 If this variable is nil, then you have to tell Gnus explicitly to
543 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
544
545 (defvar gnus-read-active-file t
546   "*Non-nil means that Gnus will read the entire active file at startup.
547 If this variable is nil, Gnus will only know about the groups in your
548 `.newsrc' file.
549
550 If this variable is `some', Gnus will try to only read the relevant
551 parts of the active file from the server.  Not all servers support
552 this, and it might be quite slow with other servers, but this should
553 generally be faster than both the t and nil value.
554
555 If you set this variable to nil or `some', you probably still want to
556 be told about new newsgroups that arrive.  To do that, set
557 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
558 properly with all servers.")
559
560 (defvar gnus-level-subscribed 5
561   "*Groups with levels less than or equal to this variable are subscribed.")
562
563 (defvar gnus-level-unsubscribed 7
564   "*Groups with levels less than or equal to this variable are unsubscribed.
565 Groups with levels less than `gnus-level-subscribed', which should be
566 less than this variable, are subscribed.")
567
568 (defvar gnus-level-zombie 8
569   "*Groups with this level are zombie groups.")
570
571 (defvar gnus-level-killed 9
572   "*Groups with this level are killed.")
573
574 (defvar gnus-level-default-subscribed 3
575   "*New subscribed groups will be subscribed at this level.")
576
577 (defvar gnus-level-default-unsubscribed 6
578   "*New unsubscribed groups will be unsubscribed at this level.")
579
580 (defvar gnus-activate-level (1+ gnus-level-subscribed)
581   "*Groups higher than this level won't be activated on startup.
582 Setting this variable to something log might save lots of time when
583 you have many groups that you aren't interested in.")
584
585 (defvar gnus-activate-foreign-newsgroups 4
586   "*If nil, Gnus will not check foreign newsgroups at startup.
587 If it is non-nil, it should be a number between one and nine.  Foreign
588 newsgroups that have a level lower or equal to this number will be
589 activated on startup.  For instance, if you want to active all
590 subscribed newsgroups, but not the rest, you'd set this variable to
591 `gnus-level-subscribed'.
592
593 If you subscribe to lots of newsgroups from different servers, startup
594 might take a while.  By setting this variable to nil, you'll save time,
595 but you won't be told how many unread articles there are in the
596 groups.")
597
598 (defvar gnus-save-newsrc-file t
599   "*Non-nil means that Gnus will save the `.newsrc' file.
600 Gnus always saves its own startup file, which is called
601 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
602 be readily understood by other newsreaders.  If you don't plan on
603 using other newsreaders, set this variable to nil to save some time on
604 exit.")
605
606 (defvar gnus-save-killed-list t
607   "*If non-nil, save the list of killed groups to the startup file.
608 If you set this variable to nil, you'll save both time (when starting
609 and quitting) and space (both memory and disk), but it will also mean
610 that Gnus has no record of which groups are new and which are old, so
611 the automatic new newsgroups subscription methods become meaningless.
612
613 You should always set `gnus-check-new-newsgroups' to `ask-server' or
614 nil if you set this variable to nil.")
615
616 (defvar gnus-interactive-catchup t
617   "*If non-nil, require your confirmation when catching up a group.")
618
619 (defvar gnus-interactive-exit t
620   "*If non-nil, require your confirmation when exiting Gnus.")
621
622 (defvar gnus-kill-killed t
623   "*If non-nil, Gnus will apply kill files to already killed articles.
624 If it is nil, Gnus will never apply kill files to articles that have
625 already been through the scoring process, which might very well save lots
626 of time.")
627
628 (defvar gnus-extract-address-components 'gnus-extract-address-components
629   "*Function for extracting address components from a From header.
630 Two pre-defined function exist: `gnus-extract-address-components',
631 which is the default, quite fast, and too simplistic solution, and
632 `mail-extract-address-components', which works much better, but is
633 slower.")
634
635 (defvar gnus-summary-default-score 0
636   "*Default article score level.
637 If this variable is nil, scoring will be disabled.")
638
639 (defvar gnus-summary-zcore-fuzz 0
640   "*Fuzziness factor for the zcore in the summary buffer.
641 Articles with scores closer than this to `gnus-summary-default-score'
642 will not be marked.")
643
644 (defvar gnus-simplify-subject-fuzzy-regexp nil
645   "*Strings to be removed when doing fuzzy matches.
646 This can either be a regular expression or list of regular expressions
647 that will be removed from subject strings if fuzzy subject
648 simplification is selected.")
649
650 (defvar gnus-permanently-visible-groups nil
651   "*Regexp to match groups that should always be listed in the group buffer.
652 This means that they will still be listed when there are no unread
653 articles in the groups.")
654
655 (defvar gnus-list-groups-with-ticked-articles t
656   "*If non-nil, list groups that have only ticked articles.
657 If nil, only list groups that have unread articles.")
658
659 (defvar gnus-group-default-list-level gnus-level-subscribed
660   "*Default listing level.
661 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
662
663 (defvar gnus-group-use-permanent-levels nil
664   "*If non-nil, once you set a level, Gnus will use this level.")
665
666 (defvar gnus-group-list-inactive-groups t
667   "*If non-nil, inactive groups will be listed.")
668
669 (defvar gnus-show-mime nil
670   "*If non-nil, do mime processing of articles.
671 The articles will simply be fed to the function given by
672 `gnus-show-mime-method'.")
673
674 (defvar gnus-strict-mime t
675   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
676
677 (defvar gnus-show-mime-method 'metamail-buffer
678   "*Function to process a MIME message.
679 The function is called from the article buffer.")
680
681 (defvar gnus-decode-encoded-word-method (lambda ())
682   "*Function to decode a MIME encoded-words.
683 The function is called from the article buffer.")
684
685 (defvar gnus-show-threads t
686   "*If non-nil, display threads in summary mode.")
687
688 (defvar gnus-thread-hide-subtree nil
689   "*If non-nil, hide all threads initially.
690 If threads are hidden, you have to run the command
691 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
692 to expose hidden threads.")
693
694 (defvar gnus-thread-hide-killed t
695   "*If non-nil, hide killed threads automatically.")
696
697 (defvar gnus-thread-ignore-subject nil
698   "*If non-nil, ignore subjects and do all threading based on the Reference header.
699 If nil, which is the default, articles that have different subjects
700 from their parents will start separate threads.")
701
702 (defvar gnus-thread-operation-ignore-subject t
703   "*If non-nil, subjects will be ignored when doing thread commands.
704 This affects commands like `gnus-summary-kill-thread' and
705 `gnus-summary-lower-thread'.
706
707 If this variable is nil, articles in the same thread with different
708 subjects will not be included in the operation in question.  If this
709 variable is `fuzzy', only articles that have subjects that are fuzzily
710 equal will be included.")
711
712 (defvar gnus-thread-indent-level 4
713   "*Number that says how much each sub-thread should be indented.")
714
715 (defvar gnus-ignored-newsgroups
716   (purecopy (mapconcat 'identity
717                        '("^to\\."       ; not "real" groups
718                          "^[0-9. \t]+ " ; all digits in name
719                          "[][\"#'()]"   ; bogus characters
720                          )
721                        "\\|"))
722   "*A regexp to match uninteresting newsgroups in the active file.
723 Any lines in the active file matching this regular expression are
724 removed from the newsgroup list before anything else is done to it,
725 thus making them effectively non-existent.")
726
727 (defvar gnus-ignored-headers
728   "^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:"
729   "*All headers that match this regexp will be hidden.
730 This variable can also be a list of regexps of headers to be ignored.
731 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
732
733 (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-"
734   "*All headers that do not match this regexp will be hidden.
735 This variable can also be a list of regexp of headers to remain visible.
736 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
737
738 (defvar gnus-sorted-header-list
739   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
740     "^Cc:" "^Date:" "^Organization:")
741   "*This variable is a list of regular expressions.
742 If it is non-nil, headers that match the regular expressions will
743 be placed first in the article buffer in the sequence specified by
744 this list.")
745
746 (defvar gnus-boring-article-headers
747   '(empty followup-to reply-to)
748   "*Headers that are only to be displayed if they have interesting data.
749 Possible values in this list are `empty', `newsgroups', `followup-to',
750 `reply-to', and `date'.")
751
752 (defvar gnus-show-all-headers nil
753   "*If non-nil, don't hide any headers.")
754
755 (defvar gnus-save-all-headers t
756   "*If non-nil, don't remove any headers before saving.")
757
758 (defvar gnus-saved-headers gnus-visible-headers
759   "*Headers to keep if `gnus-save-all-headers' is nil.
760 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
761 If that variable is nil, however, all headers that match this regexp
762 will be kept while the rest will be deleted before saving.")
763
764 (defvar gnus-inhibit-startup-message nil
765   "*If non-nil, the startup message will not be displayed.")
766
767 (defvar gnus-signature-separator "^-- *$"
768   "Regexp matching signature separator.")
769
770 (defvar gnus-signature-limit nil
771   "Provide a limit to what is considered a signature.
772 If it is a number, no signature may not be longer (in characters) than
773 that number.  If it is a function, the function will be called without
774 any parameters, and if it returns nil, there is no signature in the
775 buffer.  If it is a string, it will be used as a regexp.  If it
776 matches, the text in question is not a signature.")
777
778 (defvar gnus-auto-extend-newsgroup t
779   "*If non-nil, extend newsgroup forward and backward when requested.")
780
781 (defvar gnus-auto-select-first t
782   "*If nil, don't select the first unread article when entering a group.
783 If this variable is `best', select the highest-scored unread article
784 in the group.  If neither nil nor `best', select the first unread
785 article.
786
787 If you want to prevent automatic selection of the first unread article
788 in some newsgroups, set the variable to nil in
789 `gnus-select-group-hook'.")
790
791 (defvar gnus-auto-select-next t
792   "*If non-nil, offer to go to the next group from the end of the previous.
793 If the value is t and the next newsgroup is empty, Gnus will exit
794 summary mode and go back to group mode.  If the value is neither nil
795 nor t, Gnus will select the following unread newsgroup.  In
796 particular, if the value is the symbol `quietly', the next unread
797 newsgroup will be selected without any confirmation, and if it is
798 `almost-quietly', the next group will be selected without any
799 confirmation if you are located on the last article in the group.
800 Finally, if this variable is `slightly-quietly', the `Z n' command
801 will go to the next group without confirmation.")
802
803 (defvar gnus-auto-select-same nil
804   "*If non-nil, select the next article with the same subject.")
805
806 (defvar gnus-summary-check-current nil
807   "*If non-nil, consider the current article when moving.
808 The \"unread\" movement commands will stay on the same line if the
809 current article is unread.")
810
811 (defvar gnus-auto-center-summary t
812   "*If non-nil, always center the current summary buffer.
813 In particular, if `vertical' do only vertical recentering.  If non-nil
814 and non-`vertical', do both horizontal and vertical recentering.")
815
816 (defvar gnus-break-pages t
817   "*If non-nil, do page breaking on articles.
818 The page delimiter is specified by the `gnus-page-delimiter'
819 variable.")
820
821 (defvar gnus-page-delimiter "^\^L"
822   "*Regexp describing what to use as article page delimiters.
823 The default value is \"^\^L\", which is a form linefeed at the
824 beginning of a line.")
825
826 (defvar gnus-use-full-window t
827   "*If non-nil, use the entire Emacs screen.")
828
829 (defvar gnus-window-configuration nil
830   "Obsolete variable.  See `gnus-buffer-configuration'.")
831
832 (defvar gnus-window-min-width 2
833   "*Minimum width of Gnus buffers.")
834
835 (defvar gnus-window-min-height 1
836   "*Minimum height of Gnus buffers.")
837
838 (defvar gnus-buffer-configuration
839   '((group
840      (vertical 1.0
841                (group 1.0 point)
842                (if gnus-carpal '(group-carpal 4))))
843     (summary
844      (vertical 1.0
845                (summary 1.0 point)
846                (if gnus-carpal '(summary-carpal 4))))
847     (article
848      (cond 
849       (gnus-use-picons
850        '(frame 1.0
851                (vertical 1.0
852                          (summary 0.25 point)
853                          (if gnus-carpal '(summary-carpal 4))
854                          (article 1.0))
855                (vertical ((height . 5) (width . 15)
856                           (user-position . t)
857                           (left . -1) (top . 1))
858                          (picons 1.0))))
859       (gnus-use-trees
860        '(vertical 1.0
861                   (summary 0.25 point)
862                   (tree 0.25)
863                   (article 1.0)))
864       (t
865        '(vertical 1.0
866                  (summary 0.25 point)
867                  (if gnus-carpal '(summary-carpal 4))
868                  (if gnus-use-trees '(tree 0.25))
869                  (article 1.0)))))
870     (server
871      (vertical 1.0
872                (server 1.0 point)
873                (if gnus-carpal '(server-carpal 2))))
874     (browse
875      (vertical 1.0
876                (browse 1.0 point)
877                (if gnus-carpal '(browse-carpal 2))))
878     (message
879      (vertical 1.0
880                (message 1.0 point)))
881     (pick
882      (vertical 1.0
883                (article 1.0 point)))
884     (info
885      (vertical 1.0
886                (info 1.0 point)))
887     (summary-faq
888      (vertical 1.0
889                (summary 0.25)
890                (faq 1.0 point)))
891     (edit-group
892      (vertical 1.0
893                (group 0.5)
894                (edit-group 1.0 point)))
895     (edit-server
896      (vertical 1.0
897                (server 0.5)
898                (edit-server 1.0 point)))
899     (edit-score
900      (vertical 1.0
901                (summary 0.25)
902                (edit-score 1.0 point)))
903     (post
904      (vertical 1.0
905                (post 1.0 point)))
906     (reply
907      (vertical 1.0
908                (article-copy 0.5)
909                (message 1.0 point)))
910     (forward
911      (vertical 1.0
912                (message 1.0 point)))
913     (reply-yank
914      (vertical 1.0
915                (message 1.0 point)))
916     (mail-bounce
917      (vertical 1.0
918                (article 0.5)
919                (message 1.0 point)))
920     (draft
921      (vertical 1.0
922                (draft 1.0 point)))
923     (pipe
924      (vertical 1.0
925                (summary 0.25 point)
926                (if gnus-carpal '(summary-carpal 4))
927                ("*Shell Command Output*" 1.0)))
928     (bug
929      (vertical 1.0
930                ("*Gnus Help Bug*" 0.5)
931                ("*Gnus Bug*" 1.0 point)))
932     (compose-bounce
933      (vertical 1.0
934                (article 0.5)
935                (message 1.0 point))))
936   "Window configuration for all possible Gnus buffers.
937 This variable is a list of lists.  Each of these lists has a NAME and
938 a RULE.  The NAMEs are commonsense names like `group', which names a
939 rule used when displaying the group buffer; `summary', which names a
940 rule for what happens when you enter a group and do not display an
941 article buffer; and so on.  See the value of this variable for a
942 complete list of NAMEs.
943
944 Each RULE is a list of vectors.  The first element in this vector is
945 the name of the buffer to be displayed; the second element is the
946 percentage of the screen this buffer is to occupy (a number in the
947 0.0-0.99 range); the optional third element is `point', which should
948 be present to denote which buffer point is to go to after making this
949 buffer configuration.")
950
951 (defvar gnus-window-to-buffer
952   '((group . gnus-group-buffer)
953     (summary . gnus-summary-buffer)
954     (article . gnus-article-buffer)
955     (server . gnus-server-buffer)
956     (browse . "*Gnus Browse Server*")
957     (edit-group . gnus-group-edit-buffer)
958     (edit-server . gnus-server-edit-buffer)
959     (group-carpal . gnus-carpal-group-buffer)
960     (summary-carpal . gnus-carpal-summary-buffer)
961     (server-carpal . gnus-carpal-server-buffer)
962     (browse-carpal . gnus-carpal-browse-buffer)
963     (edit-score . gnus-score-edit-buffer)
964     (message . gnus-message-buffer)
965     (mail . gnus-message-buffer)
966     (post-news . gnus-message-buffer)
967     (faq . gnus-faq-buffer)
968     (picons . "*Picons*")
969     (tree . gnus-tree-buffer)
970     (info . gnus-info-buffer)
971     (article-copy . gnus-article-copy)
972     (draft . gnus-draft-buffer))
973   "Mapping from short symbols to buffer names or buffer variables.")
974
975 (defvar gnus-carpal nil
976   "*If non-nil, display clickable icons.")
977
978 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
979   "*Function called with a group name when new group is detected.
980 A few pre-made functions are supplied: `gnus-subscribe-randomly'
981 inserts new groups at the beginning of the list of groups;
982 `gnus-subscribe-alphabetically' inserts new groups in strict
983 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
984 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
985 for your decision; `gnus-subscribe-killed' kills all new groups;
986 `gnus-subscribe-zombies' will make all new groups into zombies.")
987
988 ;; Suggested by a bug report by Hallvard B Furuseth.
989 ;; <h.b.furuseth@usit.uio.no>.
990 (defvar gnus-subscribe-options-newsgroup-method
991   (function gnus-subscribe-alphabetically)
992   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
993 If, for instance, you want to subscribe to all newsgroups in the
994 \"no\" and \"alt\" hierarchies, you'd put the following in your
995 .newsrc file:
996
997 options -n no.all alt.all
998
999 Gnus will the subscribe all new newsgroups in these hierarchies with
1000 the subscription method in this variable.")
1001
1002 (defvar gnus-subscribe-hierarchical-interactive nil
1003   "*If non-nil, Gnus will offer to subscribe hierarchically.
1004 When a new hierarchy appears, Gnus will ask the user:
1005
1006 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
1007
1008 If the user pressed `d', Gnus will descend the hierarchy, `y' will
1009 subscribe to all newsgroups in the hierarchy and `s' will skip this
1010 hierarchy in its entirety.")
1011
1012 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1013   "*Function used for sorting the group buffer.
1014 This function will be called with group info entries as the arguments
1015 for the groups to be sorted.  Pre-made functions include
1016 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1017 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1018 `gnus-group-sort-by-rank'.
1019
1020 This variable can also be a list of sorting functions.  In that case,
1021 the most significant sort function should be the last function in the
1022 list.")
1023
1024 ;; Mark variables suggested by Thomas Michanek
1025 ;; <Thomas.Michanek@telelogic.se>.
1026 (defvar gnus-unread-mark ? 
1027   "*Mark used for unread articles.")
1028 (defvar gnus-ticked-mark ?!
1029   "*Mark used for ticked articles.")
1030 (defvar gnus-dormant-mark ??
1031   "*Mark used for dormant articles.")
1032 (defvar gnus-del-mark ?r
1033   "*Mark used for del'd articles.")
1034 (defvar gnus-read-mark ?R
1035   "*Mark used for read articles.")
1036 (defvar gnus-expirable-mark ?E
1037   "*Mark used for expirable articles.")
1038 (defvar gnus-killed-mark ?K
1039   "*Mark used for killed articles.")
1040 (defvar gnus-souped-mark ?F
1041   "*Mark used for killed articles.")
1042 (defvar gnus-kill-file-mark ?X
1043   "*Mark used for articles killed by kill files.")
1044 (defvar gnus-low-score-mark ?Y
1045   "*Mark used for articles with a low score.")
1046 (defvar gnus-catchup-mark ?C
1047   "*Mark used for articles that are caught up.")
1048 (defvar gnus-replied-mark ?A
1049   "*Mark used for articles that have been replied to.")
1050 (defvar gnus-cached-mark ?*
1051   "*Mark used for articles that are in the cache.")
1052 (defvar gnus-saved-mark ?S
1053   "*Mark used for articles that have been saved to.")
1054 (defvar gnus-process-mark ?#
1055   "*Process mark.")
1056 (defvar gnus-ancient-mark ?O
1057   "*Mark used for ancient articles.")
1058 (defvar gnus-sparse-mark ?Q
1059   "*Mark used for sparsely reffed articles.")
1060 (defvar gnus-canceled-mark ?G
1061   "*Mark used for canceled articles.")
1062 (defvar gnus-score-over-mark ?+
1063   "*Score mark used for articles with high scores.")
1064 (defvar gnus-score-below-mark ?-
1065   "*Score mark used for articles with low scores.")
1066 (defvar gnus-empty-thread-mark ? 
1067   "*There is no thread under the article.")
1068 (defvar gnus-not-empty-thread-mark ?=
1069   "*There is a thread under the article.")
1070
1071 (defvar gnus-view-pseudo-asynchronously nil
1072   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1073
1074 (defvar gnus-view-pseudos nil
1075   "*If `automatic', pseudo-articles will be viewed automatically.
1076 If `not-confirm', pseudos will be viewed automatically, and the user
1077 will not be asked to confirm the command.")
1078
1079 (defvar gnus-view-pseudos-separately t
1080   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1081 If nil, all files that use the same viewing command will be given as a
1082 list of parameters to that command.")
1083
1084 (defvar gnus-insert-pseudo-articles t
1085   "*If non-nil, insert pseudo-articles when decoding articles.")
1086
1087 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1088   "*Format of group lines.
1089 It works along the same lines as a normal formatting string,
1090 with some simple extensions.
1091
1092 %M    Only marked articles (character, \"*\" or \" \")
1093 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1094 %L    Level of subscribedness (integer)
1095 %N    Number of unread articles (integer)
1096 %I    Number of dormant articles (integer)
1097 %i    Number of ticked and dormant (integer)
1098 %T    Number of ticked articles (integer)
1099 %R    Number of read articles (integer)
1100 %t    Total number of articles (integer)
1101 %y    Number of unread, unticked articles (integer)
1102 %G    Group name (string)
1103 %g    Qualified group name (string)
1104 %D    Group description (string)
1105 %s    Select method (string)
1106 %o    Moderated group (char, \"m\")
1107 %p    Process mark (char)
1108 %O    Moderated group (string, \"(m)\" or \"\")
1109 %P    Topic indentation (string)
1110 %l    Whether there are GroupLens predictions for this group (string)
1111 %n    Select from where (string)
1112 %z    A string that look like `<%s:%n>' if a foreign select method is used
1113 %u    User defined specifier.  The next character in the format string should
1114       be a letter.  Gnus will call the function gnus-user-format-function-X,
1115       where X is the letter following %u.  The function will be passed the
1116       current header as argument.  The function should return a string, which
1117       will be inserted into the buffer just like information from any other
1118       group specifier.
1119
1120 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1121 the mouse point move inside the area.  There can only be one such area.
1122
1123 Note that this format specification is not always respected.  For
1124 reasons of efficiency, when listing killed groups, this specification
1125 is ignored altogether.  If the spec is changed considerably, your
1126 output may end up looking strange when listing both alive and killed
1127 groups.
1128
1129 If you use %o or %O, reading the active file will be slower and quite
1130 a bit of extra memory will be used. %D will also worsen performance.
1131 Also note that if you change the format specification to include any
1132 of these specs, you must probably re-start Gnus to see them go into
1133 effect.")
1134
1135 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1136   "*The format specification of the lines in the summary buffer.
1137
1138 It works along the same lines as a normal formatting string,
1139 with some simple extensions.
1140
1141 %N   Article number, left padded with spaces (string)
1142 %S   Subject (string)
1143 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1144 %n   Name of the poster (string)
1145 %a   Extracted name of the poster (string)
1146 %A   Extracted address of the poster (string)
1147 %F   Contents of the From: header (string)
1148 %x   Contents of the Xref: header (string)
1149 %D   Date of the article (string)
1150 %d   Date of the article (string) in DD-MMM format
1151 %M   Message-id of the article (string)
1152 %r   References of the article (string)
1153 %c   Number of characters in the article (integer)
1154 %L   Number of lines in the article (integer)
1155 %I   Indentation based on thread level (a string of spaces)
1156 %T   A string with two possible values: 80 spaces if the article
1157      is on thread level two or larger and 0 spaces on level one
1158 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1159 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1160 %[   Opening bracket (character, \"[\" or \"<\")
1161 %]   Closing bracket (character, \"]\" or \">\")
1162 %>   Spaces of length thread-level (string)
1163 %<   Spaces of length (- 20 thread-level) (string)
1164 %i   Article score (number)
1165 %z   Article zcore (character)
1166 %t   Number of articles under the current thread (number).
1167 %e   Whether the thread is empty or not (character).
1168 %l   GroupLens score (string).
1169 %u   User defined specifier.  The next character in the format string should
1170      be a letter.  Gnus will call the function gnus-user-format-function-X,
1171      where X is the letter following %u.  The function will be passed the
1172      current header as argument.  The function should return a string, which
1173      will be inserted into the summary just like information from any other
1174      summary specifier.
1175
1176 Text between %( and %) will be highlighted with `gnus-mouse-face'
1177 when the mouse point is placed inside the area.  There can only be one
1178 such area.
1179
1180 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1181 with care.  For reasons of efficiency, Gnus will compute what column
1182 these characters will end up in, and \"hard-code\" that.  This means that
1183 it is illegal to have these specs after a variable-length spec.  Well,
1184 you might not be arrested, but your summary buffer will look strange,
1185 which is bad enough.
1186
1187 The smart choice is to have these specs as for to the left as
1188 possible.
1189
1190 This restriction may disappear in later versions of Gnus.")
1191
1192 (defvar gnus-summary-dummy-line-format
1193   "*  %(:                          :%) %S\n"
1194   "*The format specification for the dummy roots in the summary buffer.
1195 It works along the same lines as a normal formatting string,
1196 with some simple extensions.
1197
1198 %S  The subject")
1199
1200 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1201   "*The format specification for the summary mode line.
1202 It works along the same lines as a normal formatting string,
1203 with some simple extensions:
1204
1205 %G  Group name
1206 %p  Unprefixed group name
1207 %A  Current article number
1208 %V  Gnus version
1209 %U  Number of unread articles in the group
1210 %e  Number of unselected articles in the group
1211 %Z  A string with unread/unselected article counts
1212 %g  Shortish group name
1213 %S  Subject of the current article
1214 %u  User-defined spec
1215 %s  Current score file name
1216 %d  Number of dormant articles
1217 %r  Number of articles that have been marked as read in this session
1218 %E  Number of articles expunged by the score files")
1219
1220 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1221   "*The format specification for the article mode line.
1222 See `gnus-summary-mode-line-format' for a closer description.")
1223
1224 (defvar gnus-group-mode-line-format "Gnus: %%b {%M%:%S}"
1225   "*The format specification for the group mode line.
1226 It works along the same lines as a normal formatting string,
1227 with some simple extensions:
1228
1229 %S   The native news server.
1230 %M   The native select method.
1231 %:   \":\" if %S isn't \"\".")
1232
1233 (defvar gnus-valid-select-methods
1234   '(("nntp" post address prompt-address)
1235     ("nnspool" post address)
1236     ("nnvirtual" post-mail virtual prompt-address)
1237     ("nnmbox" mail respool address)
1238     ("nnml" mail respool address)
1239     ("nnmh" mail respool address)
1240     ("nndir" post-mail prompt-address address)
1241     ("nneething" none address prompt-address)
1242     ("nndoc" none address prompt-address)
1243     ("nnbabyl" mail address respool)
1244     ("nnkiboze" post virtual)
1245     ("nnsoup" post-mail address)
1246     ("nndraft" post-mail)
1247     ("nnfolder" mail respool address))
1248   "An alist of valid select methods.
1249 The first element of each list lists should be a string with the name
1250 of the select method.  The other elements may be be the category of
1251 this method (ie. `post', `mail', `none' or whatever) or other
1252 properties that this method has (like being respoolable).
1253 If you implement a new select method, all you should have to change is
1254 this variable.  I think.")
1255
1256 (defvar gnus-updated-mode-lines '(group article summary tree)
1257   "*List of buffers that should update their mode lines.
1258 The list may contain the symbols `group', `article' and `summary'.  If
1259 the corresponding symbol is present, Gnus will keep that mode line
1260 updated with information that may be pertinent.
1261 If this variable is nil, screen refresh may be quicker.")
1262
1263 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1264 (defvar gnus-mode-non-string-length nil
1265   "*Max length of mode-line non-string contents.
1266 If this is nil, Gnus will take space as is needed, leaving the rest
1267 of the modeline intact.")
1268
1269 ;see gnus-cus.el
1270 ;(defvar gnus-mouse-face 'highlight
1271 ;  "*Face used for mouse highlighting in Gnus.
1272 ;No mouse highlights will be done if `gnus-visual' is nil.")
1273
1274 (defvar gnus-summary-mark-below 0
1275   "*Mark all articles with a score below this variable as read.
1276 This variable is local to each summary buffer and usually set by the
1277 score file.")
1278
1279 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1280   "*List of functions used for sorting articles in the summary buffer.
1281 This variable is only used when not using a threaded display.")
1282
1283 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1284   "*List of functions used for sorting threads in the summary buffer.
1285 By default, threads are sorted by article number.
1286
1287 Each function takes two threads and return non-nil if the first thread
1288 should be sorted before the other.  If you use more than one function,
1289 the primary sort function should be the last.  You should probably
1290 always include `gnus-thread-sort-by-number' in the list of sorting
1291 functions -- preferably first.
1292
1293 Ready-mady functions include `gnus-thread-sort-by-number',
1294 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1295 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1296 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1297
1298 (defvar gnus-thread-score-function '+
1299   "*Function used for calculating the total score of a thread.
1300
1301 The function is called with the scores of the article and each
1302 subthread and should then return the score of the thread.
1303
1304 Some functions you can use are `+', `max', or `min'.")
1305
1306 (defvar gnus-summary-expunge-below nil
1307   "All articles that have a score less than this variable will be expunged.")
1308
1309 (defvar gnus-thread-expunge-below nil
1310   "All threads that have a total score less than this variable will be expunged.
1311 See `gnus-thread-score-function' for en explanation of what a
1312 \"thread score\" is.")
1313
1314 (defvar gnus-auto-subscribed-groups
1315   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1316   "*All new groups that match this regexp will be subscribed automatically.
1317 Note that this variable only deals with new groups.  It has no effect
1318 whatsoever on old groups.")
1319
1320 (defvar gnus-options-subscribe nil
1321   "*All new groups matching this regexp will be subscribed unconditionally.
1322 Note that this variable deals only with new newsgroups.  This variable
1323 does not affect old newsgroups.")
1324
1325 (defvar gnus-options-not-subscribe nil
1326   "*All new groups matching this regexp will be ignored.
1327 Note that this variable deals only with new newsgroups.  This variable
1328 does not affect old (already subscribed) newsgroups.")
1329
1330 (defvar gnus-auto-expirable-newsgroups nil
1331   "*Groups in which to automatically mark read articles as expirable.
1332 If non-nil, this should be a regexp that should match all groups in
1333 which to perform auto-expiry.  This only makes sense for mail groups.")
1334
1335 (defvar gnus-total-expirable-newsgroups nil
1336   "*Groups in which to perform expiry of all read articles.
1337 Use with extreme caution.  All groups that match this regexp will be
1338 expiring - which means that all read articles will be deleted after
1339 (say) one week.  (This only goes for mail groups and the like, of
1340 course.)")
1341
1342 (defvar gnus-group-uncollapsed-levels 1
1343   "Number of group name elements to leave alone when making a short group name.")
1344
1345 (defvar gnus-hidden-properties '(invisible t intangible t)
1346   "Property list to use for hiding text.")
1347
1348 (defvar gnus-modtime-botch nil
1349   "*Non-nil means .newsrc should be deleted prior to save.  
1350 Its use is due to the bogus appearance that .newsrc was modified on
1351 disc.")
1352
1353 ;; Hooks.
1354
1355 (defvar gnus-group-mode-hook nil
1356   "*A hook for Gnus group mode.")
1357
1358 (defvar gnus-summary-mode-hook nil
1359   "*A hook for Gnus summary mode.
1360 This hook is run before any variables are set in the summary buffer.")
1361
1362 (defvar gnus-article-mode-hook nil
1363   "*A hook for Gnus article mode.")
1364
1365 (defvar gnus-summary-prepare-exit-hook nil
1366   "*A hook called when preparing to exit from the summary buffer.
1367 It calls `gnus-summary-expire-articles' by default.")
1368 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1369
1370 (defvar gnus-summary-exit-hook nil
1371   "*A hook called on exit from the summary buffer.")
1372
1373 (defvar gnus-group-catchup-group-hook nil
1374   "*A hook run when catching up a group from the group buffer.")
1375
1376 (defvar gnus-group-update-group-hook nil
1377   "*A hook called when updating group lines.")
1378
1379 (defvar gnus-open-server-hook nil
1380   "*A hook called just before opening connection to the news server.")
1381
1382 (defvar gnus-load-hook nil
1383   "*A hook run while Gnus is loaded.")
1384
1385 (defvar gnus-startup-hook nil
1386   "*A hook called at startup.
1387 This hook is called after Gnus is connected to the NNTP server.")
1388
1389 (defvar gnus-get-new-news-hook nil
1390   "*A hook run just before Gnus checks for new news.")
1391
1392 (defvar gnus-after-getting-new-news-hook nil
1393   "*A hook run after Gnus checks for new news.")
1394
1395 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1396   "*A function that is called to generate the group buffer.
1397 The function is called with three arguments: The first is a number;
1398 all group with a level less or equal to that number should be listed,
1399 if the second is non-nil, empty groups should also be displayed.  If
1400 the third is non-nil, it is a number.  No groups with a level lower
1401 than this number should be displayed.
1402
1403 The only current function implemented is `gnus-group-prepare-flat'.")
1404
1405 (defvar gnus-group-prepare-hook nil
1406   "*A hook called after the group buffer has been generated.
1407 If you want to modify the group buffer, you can use this hook.")
1408
1409 (defvar gnus-summary-prepare-hook nil
1410   "*A hook called after the summary buffer has been generated.
1411 If you want to modify the summary buffer, you can use this hook.")
1412
1413 (defvar gnus-summary-generate-hook nil
1414   "*A hook run just before generating the summary buffer.
1415 This hook is commonly used to customize threading variables and the
1416 like.")
1417
1418 (defvar gnus-article-prepare-hook nil
1419   "*A hook called after an article has been prepared in the article buffer.
1420 If you want to run a special decoding program like nkf, use this hook.")
1421
1422 ;(defvar gnus-article-display-hook nil
1423 ;  "*A hook called after the article is displayed in the article buffer.
1424 ;The hook is designed to change the contents of the article
1425 ;buffer.  Typical functions that this hook may contain are
1426 ;`gnus-article-hide-headers' (hide selected headers),
1427 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1428 ;`gnus-article-hide-signature' (hide signature) and
1429 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1430 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1431 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1432 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1433
1434 (defvar gnus-article-x-face-too-ugly nil
1435   "Regexp matching posters whose face shouldn't be shown automatically.")
1436
1437 (defvar gnus-select-group-hook nil
1438   "*A hook called when a newsgroup is selected.
1439
1440 If you'd like to simplify subjects like the
1441 `gnus-summary-next-same-subject' command does, you can use the
1442 following hook:
1443
1444  (setq gnus-select-group-hook
1445       (list
1446         (lambda ()
1447           (mapcar (lambda (header)
1448                      (mail-header-set-subject
1449                       header
1450                       (gnus-simplify-subject
1451                        (mail-header-subject header) 're-only)))
1452                   gnus-newsgroup-headers))))")
1453
1454 (defvar gnus-select-article-hook nil
1455   "*A hook called when an article is selected.")
1456
1457 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1458   "*A hook called to apply kill files to a group.
1459 This hook is intended to apply a kill file to the selected newsgroup.
1460 The function `gnus-apply-kill-file' is called by default.
1461
1462 Since a general kill file is too heavy to use only for a few
1463 newsgroups, I recommend you to use a lighter hook function.  For
1464 example, if you'd like to apply a kill file to articles which contains
1465 a string `rmgroup' in subject in newsgroup `control', you can use the
1466 following hook:
1467
1468  (setq gnus-apply-kill-hook
1469       (list
1470         (lambda ()
1471           (cond ((string-match \"control\" gnus-newsgroup-name)
1472                  (gnus-kill \"Subject\" \"rmgroup\")
1473                  (gnus-expunge \"X\"))))))")
1474
1475 (defvar gnus-visual-mark-article-hook
1476   (list 'gnus-highlight-selected-summary)
1477   "*Hook run after selecting an article in the summary buffer.
1478 It is meant to be used for highlighting the article in some way.  It
1479 is not run if `gnus-visual' is nil.")
1480
1481 (defvar gnus-parse-headers-hook nil
1482   "*A hook called before parsing the headers.")
1483 (add-hook 'gnus-parse-headers-hook 'gnus-decode-rfc1522)
1484
1485 (defvar gnus-exit-group-hook nil
1486   "*A hook called when exiting (not quitting) summary mode.")
1487
1488 (defvar gnus-suspend-gnus-hook nil
1489   "*A hook called when suspending (not exiting) Gnus.")
1490
1491 (defvar gnus-exit-gnus-hook nil
1492   "*A hook called when exiting Gnus.")
1493
1494 (defvar gnus-after-exiting-gnus-hook nil
1495   "*A hook called after exiting Gnus.")
1496
1497 (defvar gnus-save-newsrc-hook nil
1498   "*A hook called before saving any of the newsrc files.")
1499
1500 (defvar gnus-save-quick-newsrc-hook nil
1501   "*A hook called just before saving the quick newsrc file.
1502 Can be used to turn version control on or off.")
1503
1504 (defvar gnus-save-standard-newsrc-hook nil
1505   "*A hook called just before saving the standard newsrc file.
1506 Can be used to turn version control on or off.")
1507
1508 (defvar gnus-summary-update-hook
1509   (list 'gnus-summary-highlight-line)
1510   "*A hook called when a summary line is changed.
1511 The hook will not be called if `gnus-visual' is nil.
1512
1513 The default function `gnus-summary-highlight-line' will
1514 highlight the line according to the `gnus-summary-highlight'
1515 variable.")
1516
1517 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1518   "*A hook called when a group line is changed.
1519 The hook will not be called if `gnus-visual' is nil.
1520
1521 The default function `gnus-group-highlight-line' will
1522 highlight the line according to the `gnus-group-highlight'
1523 variable.")
1524
1525 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1526   "*A hook called when an article is selected for the first time.
1527 The hook is intended to mark an article as read (or unread)
1528 automatically when it is selected.")
1529
1530 (defvar gnus-group-change-level-function nil
1531   "Function run when a group level is changed.
1532 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1533
1534 ;; Remove any hilit infestation.
1535 (add-hook 'gnus-startup-hook
1536           (lambda ()
1537             (remove-hook 'gnus-summary-prepare-hook
1538                          'hilit-rehighlight-buffer-quietly)
1539             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1540             (setq gnus-mark-article-hook
1541                   '(gnus-summary-mark-read-and-unread-as-read))
1542             (remove-hook 'gnus-article-prepare-hook
1543                          'hilit-rehighlight-buffer-quietly)))
1544
1545 \f
1546 ;; Internal variables
1547
1548 (defvar gnus-tree-buffer "*Tree*"
1549   "Buffer where Gnus thread trees are displayed.")
1550
1551 ;; Dummy variable.
1552 (defvar gnus-use-generic-from nil)
1553
1554 (defvar gnus-thread-indent-array nil)
1555 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1556
1557 (defvar gnus-newsrc-file-version nil)
1558
1559 (defvar gnus-method-history nil)
1560 ;; Variable holding the user answers to all method prompts.
1561
1562 (defvar gnus-group-history nil)
1563 ;; Variable holding the user answers to all group prompts.
1564
1565 (defvar gnus-server-alist nil
1566   "List of available servers.")
1567
1568 (defvar gnus-group-indentation-function nil)
1569
1570 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1571
1572 (defvar gnus-goto-missing-group-function nil)
1573
1574 (defvar gnus-override-subscribe-method nil)
1575
1576 (defvar gnus-group-goto-next-group-function nil
1577   "Function to override finding the next group after listing groups.")
1578
1579 (defconst gnus-article-mark-lists
1580   '((marked . tick) (replied . reply)
1581     (expirable . expire) (killed . killed)
1582     (bookmarks . bookmark) (dormant . dormant)
1583     (scored . score) (saved . save)
1584     (cached . cache)
1585     ))
1586
1587 ;; Avoid highlighting in kill files.
1588 (defvar gnus-summary-inhibit-highlight nil)
1589 (defvar gnus-newsgroup-selected-overlay nil)
1590
1591 (defvar gnus-inhibit-hiding nil)
1592 (defvar gnus-group-indentation "")
1593 (defvar gnus-inhibit-limiting nil)
1594 (defvar gnus-created-frames nil)
1595
1596 (defvar gnus-article-mode-map nil)
1597 (defvar gnus-dribble-buffer nil)
1598 (defvar gnus-headers-retrieved-by nil)
1599 (defvar gnus-article-reply nil)
1600 (defvar gnus-override-method nil)
1601 (defvar gnus-article-check-size nil)
1602
1603 (defvar gnus-current-score-file nil)
1604 (defvar gnus-newsgroup-adaptive-score-file nil)
1605 (defvar gnus-scores-exclude-files nil)
1606
1607 (defvar gnus-opened-servers nil)
1608
1609 (defvar gnus-current-move-group nil)
1610 (defvar gnus-current-copy-group nil)
1611 (defvar gnus-current-crosspost-group nil)
1612
1613 (defvar gnus-newsgroup-dependencies nil)
1614 (defvar gnus-newsgroup-async nil)
1615 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1616
1617 (defvar gnus-newsgroup-adaptive nil)
1618
1619 (defvar gnus-summary-display-table nil)
1620 (defvar gnus-summary-display-article-function nil)
1621
1622 (defvar gnus-summary-highlight-line-function nil
1623   "Function called after highlighting a summary line.")
1624
1625 (defvar gnus-group-line-format-alist
1626   `((?M gnus-tmp-marked-mark ?c)
1627     (?S gnus-tmp-subscribed ?c)
1628     (?L gnus-tmp-level ?d)
1629     (?N (cond ((eq number t) "*" )
1630               ((numberp number) 
1631                (int-to-string
1632                 (+ number
1633                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1634                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1635               (t number)) ?s)
1636     (?R gnus-tmp-number-of-read ?s)
1637     (?t gnus-tmp-number-total ?d)
1638     (?y gnus-tmp-number-of-unread ?s)
1639     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1640     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1641     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1642            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1643     (?g gnus-tmp-group ?s)
1644     (?G gnus-tmp-qualified-group ?s)
1645     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1646     (?D gnus-tmp-newsgroup-description ?s)
1647     (?o gnus-tmp-moderated ?c)
1648     (?O gnus-tmp-moderated-string ?s)
1649     (?p gnus-tmp-process-marked ?c)
1650     (?s gnus-tmp-news-server ?s)
1651     (?n gnus-tmp-news-method ?s)
1652     (?P gnus-group-indentation ?s)
1653     (?l gnus-tmp-grouplens ?s)
1654     (?z gnus-tmp-news-method-string ?s)
1655     (?u gnus-tmp-user-defined ?s)))
1656
1657 (defvar gnus-summary-line-format-alist
1658   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1659     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1660     (?s gnus-tmp-subject-or-nil ?s)
1661     (?n gnus-tmp-name ?s)
1662     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1663         ?s)
1664     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1665             gnus-tmp-from) ?s)
1666     (?F gnus-tmp-from ?s)
1667     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1668     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1669     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1670     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1671     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1672     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1673     (?L gnus-tmp-lines ?d)
1674     (?I gnus-tmp-indentation ?s)
1675     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1676     (?R gnus-tmp-replied ?c)
1677     (?\[ gnus-tmp-opening-bracket ?c)
1678     (?\] gnus-tmp-closing-bracket ?c)
1679     (?\> (make-string gnus-tmp-level ? ) ?s)
1680     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1681     (?i gnus-tmp-score ?d)
1682     (?z gnus-tmp-score-char ?c)
1683     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1684     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1685     (?U gnus-tmp-unread ?c)
1686     (?t (gnus-summary-number-of-articles-in-thread
1687          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1688         ?d)
1689     (?e (gnus-summary-number-of-articles-in-thread
1690          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1691         ?c)
1692     (?u gnus-tmp-user-defined ?s))
1693   "An alist of format specifications that can appear in summary lines,
1694 and what variables they correspond with, along with the type of the
1695 variable (string, integer, character, etc).")
1696
1697 (defvar gnus-summary-dummy-line-format-alist
1698   `((?S gnus-tmp-subject ?s)
1699     (?N gnus-tmp-number ?d)
1700     (?u gnus-tmp-user-defined ?s)))
1701
1702 (defvar gnus-summary-mode-line-format-alist
1703   `((?G gnus-tmp-group-name ?s)
1704     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1705     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1706     (?A gnus-tmp-article-number ?d)
1707     (?Z gnus-tmp-unread-and-unselected ?s)
1708     (?V gnus-version ?s)
1709     (?U gnus-tmp-unread-and-unticked ?d)
1710     (?S gnus-tmp-subject ?s)
1711     (?e gnus-tmp-unselected ?d)
1712     (?u gnus-tmp-user-defined ?s)
1713     (?d (length gnus-newsgroup-dormant) ?d)
1714     (?t (length gnus-newsgroup-marked) ?d)
1715     (?r (length gnus-newsgroup-reads) ?d)
1716     (?E gnus-newsgroup-expunged-tally ?d)
1717     (?s (gnus-current-score-file-nondirectory) ?s)))
1718
1719 (defvar gnus-article-mode-line-format-alist
1720   gnus-summary-mode-line-format-alist)
1721
1722 (defvar gnus-group-mode-line-format-alist
1723   `((?S gnus-tmp-news-server ?s)
1724     (?M gnus-tmp-news-method ?s)
1725     (?u gnus-tmp-user-defined ?s)
1726     (?: gnus-tmp-colon ?s)))
1727
1728 (defvar gnus-have-read-active-file nil)
1729
1730 (defconst gnus-maintainer
1731   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1732   "The mail address of the Gnus maintainers.")
1733
1734 (defconst gnus-version-number "5.2.23"
1735   "Version number for this version of Gnus.")
1736
1737 (defconst gnus-version (format "Gnus v%s" gnus-version-number)
1738   "Version string for this version of Gnus.")
1739
1740 (defvar gnus-info-nodes
1741   '((gnus-group-mode "(gnus)The Group Buffer")
1742     (gnus-summary-mode "(gnus)The Summary Buffer")
1743     (gnus-article-mode "(gnus)The Article Buffer")
1744     (gnus-server-mode "(gnus)The Server Buffer")
1745     (gnus-browse-mode "(gnus)Browse Foreign Server")
1746     (gnus-tree-mode "(gnus)Tree Display")
1747     )
1748   "Alist of major modes and related Info nodes.")
1749
1750 (defvar gnus-group-buffer "*Group*")
1751 (defvar gnus-summary-buffer "*Summary*")
1752 (defvar gnus-article-buffer "*Article*")
1753 (defvar gnus-server-buffer "*Server*")
1754
1755 (defvar gnus-work-buffer " *gnus work*")
1756
1757 (defvar gnus-original-article-buffer " *Original Article*")
1758 (defvar gnus-original-article nil)
1759
1760 (defvar gnus-buffer-list nil
1761   "Gnus buffers that should be killed on exit.")
1762
1763 (defvar gnus-slave nil
1764   "Whether this Gnus is a slave or not.")
1765
1766 (defvar gnus-variable-list
1767   '(gnus-newsrc-options gnus-newsrc-options-n
1768     gnus-newsrc-last-checked-date
1769     gnus-newsrc-alist gnus-server-alist
1770     gnus-killed-list gnus-zombie-list
1771     gnus-topic-topology gnus-topic-alist
1772     gnus-format-specs)
1773   "Gnus variables saved in the quick startup file.")
1774
1775 (defvar gnus-newsrc-options nil
1776   "Options line in the .newsrc file.")
1777
1778 (defvar gnus-newsrc-options-n nil
1779   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1780
1781 (defvar gnus-newsrc-last-checked-date nil
1782   "Date Gnus last asked server for new newsgroups.")
1783
1784 (defvar gnus-topic-topology nil
1785   "The complete topic hierarchy.")
1786
1787 (defvar gnus-topic-alist nil
1788   "The complete topic-group alist.")
1789
1790 (defvar gnus-newsrc-alist nil
1791   "Assoc list of read articles.
1792 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1793
1794 (defvar gnus-newsrc-hashtb nil
1795   "Hashtable of gnus-newsrc-alist.")
1796
1797 (defvar gnus-killed-list nil
1798   "List of killed newsgroups.")
1799
1800 (defvar gnus-killed-hashtb nil
1801   "Hash table equivalent of gnus-killed-list.")
1802
1803 (defvar gnus-zombie-list nil
1804   "List of almost dead newsgroups.")
1805
1806 (defvar gnus-description-hashtb nil
1807   "Descriptions of newsgroups.")
1808
1809 (defvar gnus-list-of-killed-groups nil
1810   "List of newsgroups that have recently been killed by the user.")
1811
1812 (defvar gnus-active-hashtb nil
1813   "Hashtable of active articles.")
1814
1815 (defvar gnus-moderated-list nil
1816   "List of moderated newsgroups.")
1817
1818 (defvar gnus-group-marked nil)
1819
1820 (defvar gnus-current-startup-file nil
1821   "Startup file for the current host.")
1822
1823 (defvar gnus-last-search-regexp nil
1824   "Default regexp for article search command.")
1825
1826 (defvar gnus-last-shell-command nil
1827   "Default shell command on article.")
1828
1829 (defvar gnus-current-select-method nil
1830   "The current method for selecting a newsgroup.")
1831
1832 (defvar gnus-group-list-mode nil)
1833
1834 (defvar gnus-article-internal-prepare-hook nil)
1835
1836 (defvar gnus-newsgroup-name nil)
1837 (defvar gnus-newsgroup-begin nil)
1838 (defvar gnus-newsgroup-end nil)
1839 (defvar gnus-newsgroup-last-rmail nil)
1840 (defvar gnus-newsgroup-last-mail nil)
1841 (defvar gnus-newsgroup-last-folder nil)
1842 (defvar gnus-newsgroup-last-file nil)
1843 (defvar gnus-newsgroup-auto-expire nil)
1844 (defvar gnus-newsgroup-active nil)
1845
1846 (defvar gnus-newsgroup-data nil)
1847 (defvar gnus-newsgroup-data-reverse nil)
1848 (defvar gnus-newsgroup-limit nil)
1849 (defvar gnus-newsgroup-limits nil)
1850
1851 (defvar gnus-newsgroup-unreads nil
1852   "List of unread articles in the current newsgroup.")
1853
1854 (defvar gnus-newsgroup-unselected nil
1855   "List of unselected unread articles in the current newsgroup.")
1856
1857 (defvar gnus-newsgroup-reads nil
1858   "Alist of read articles and article marks in the current newsgroup.")
1859
1860 (defvar gnus-newsgroup-expunged-tally nil)
1861
1862 (defvar gnus-newsgroup-marked nil
1863   "List of ticked articles in the current newsgroup (a subset of unread art).")
1864
1865 (defvar gnus-newsgroup-killed nil
1866   "List of ranges of articles that have been through the scoring process.")
1867
1868 (defvar gnus-newsgroup-cached nil
1869   "List of articles that come from the article cache.")
1870
1871 (defvar gnus-newsgroup-saved nil
1872   "List of articles that have been saved.")
1873
1874 (defvar gnus-newsgroup-kill-headers nil)
1875
1876 (defvar gnus-newsgroup-replied nil
1877   "List of articles that have been replied to in the current newsgroup.")
1878
1879 (defvar gnus-newsgroup-expirable nil
1880   "List of articles in the current newsgroup that can be expired.")
1881
1882 (defvar gnus-newsgroup-processable nil
1883   "List of articles in the current newsgroup that can be processed.")
1884
1885 (defvar gnus-newsgroup-bookmarks nil
1886   "List of articles in the current newsgroup that have bookmarks.")
1887
1888 (defvar gnus-newsgroup-dormant nil
1889   "List of dormant articles in the current newsgroup.")
1890
1891 (defvar gnus-newsgroup-scored nil
1892   "List of scored articles in the current newsgroup.")
1893
1894 (defvar gnus-newsgroup-headers nil
1895   "List of article headers in the current newsgroup.")
1896
1897 (defvar gnus-newsgroup-threads nil)
1898
1899 (defvar gnus-newsgroup-prepared nil
1900   "Whether the current group has been prepared properly.")
1901
1902 (defvar gnus-newsgroup-ancient nil
1903   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1904
1905 (defvar gnus-newsgroup-sparse nil)
1906
1907 (defvar gnus-current-article nil)
1908 (defvar gnus-article-current nil)
1909 (defvar gnus-current-headers nil)
1910 (defvar gnus-have-all-headers nil)
1911 (defvar gnus-last-article nil)
1912 (defvar gnus-newsgroup-history nil)
1913 (defvar gnus-current-kill-article nil)
1914
1915 ;; Save window configuration.
1916 (defvar gnus-prev-winconf nil)
1917
1918 (defvar gnus-summary-mark-positions nil)
1919 (defvar gnus-group-mark-positions nil)
1920
1921 (defvar gnus-reffed-article-number nil)
1922
1923 ;;; Let the byte-compiler know that we know about this variable.
1924 (defvar rmail-default-rmail-file)
1925
1926 (defvar gnus-cache-removable-articles nil)
1927
1928 (defvar gnus-dead-summary nil)
1929
1930 (defconst gnus-summary-local-variables
1931   '(gnus-newsgroup-name
1932     gnus-newsgroup-begin gnus-newsgroup-end
1933     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1934     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1935     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1936     gnus-newsgroup-unselected gnus-newsgroup-marked
1937     gnus-newsgroup-reads gnus-newsgroup-saved
1938     gnus-newsgroup-replied gnus-newsgroup-expirable
1939     gnus-newsgroup-processable gnus-newsgroup-killed
1940     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1941     gnus-newsgroup-headers gnus-newsgroup-threads
1942     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1943     gnus-current-article gnus-current-headers gnus-have-all-headers
1944     gnus-last-article gnus-article-internal-prepare-hook
1945     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1946     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1947     gnus-newsgroup-async gnus-thread-expunge-below
1948     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1949     (gnus-summary-mark-below . global)
1950     gnus-newsgroup-active gnus-scores-exclude-files
1951     gnus-newsgroup-history gnus-newsgroup-ancient
1952     gnus-newsgroup-sparse
1953     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1954     gnus-newsgroup-adaptive-score-file
1955     (gnus-newsgroup-expunged-tally . 0)
1956     gnus-cache-removable-articles gnus-newsgroup-cached
1957     gnus-newsgroup-data gnus-newsgroup-data-reverse
1958     gnus-newsgroup-limit gnus-newsgroup-limits)
1959   "Variables that are buffer-local to the summary buffers.")
1960
1961 (defconst gnus-bug-message
1962   "Sending a bug report to the Gnus Towers.
1963 ========================================
1964
1965 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1966 be sent to the Gnus Bug Exterminators.
1967
1968 At the bottom of the buffer you'll see lots of variable settings.
1969 Please do not delete those.  They will tell the Bug People what your
1970 environment is, so that it will be easier to locate the bugs.
1971
1972 If you have found a bug that makes Emacs go \"beep\", set
1973 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1974 and include the backtrace in your bug report.
1975
1976 Please describe the bug in annoying, painstaking detail.
1977
1978 Thank you for your help in stamping out bugs.
1979 ")
1980
1981 ;;; End of variables.
1982
1983 ;; Define some autoload functions Gnus might use.
1984 (eval-and-compile
1985
1986   ;; This little mapcar goes through the list below and marks the
1987   ;; symbols in question as autoloaded functions.
1988   (mapcar
1989    (lambda (package)
1990      (let ((interactive (nth 1 (memq ':interactive package))))
1991        (mapcar
1992         (lambda (function)
1993           (let (keymap)
1994             (when (consp function)
1995               (setq keymap (car (memq 'keymap function)))
1996               (setq function (car function)))
1997             (autoload function (car package) nil interactive keymap)))
1998         (if (eq (nth 1 package) ':interactive)
1999             (cdddr package)
2000           (cdr package)))))
2001    '(("metamail" metamail-buffer)
2002      ("info" Info-goto-node)
2003      ("hexl" hexl-hex-string-to-integer)
2004      ("pp" pp pp-to-string pp-eval-expression)
2005      ("mail-extr" mail-extract-address-components)
2006      ("nnmail" nnmail-split-fancy nnmail-article-group)
2007      ("nnvirtual" nnvirtual-catchup-group)
2008      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
2009       timezone-make-sortable-date timezone-make-time-string)
2010      ("rmailout" rmail-output)
2011      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
2012       rmail-show-message)
2013      ("gnus-soup" :interactive t
2014       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
2015       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
2016      ("nnsoup" nnsoup-pack-replies)
2017      ("gnus-scomo" :interactive t gnus-score-mode)
2018      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2019       gnus-Folder-save-name gnus-folder-save-name)
2020      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2021      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2022       gnus-server-make-menu-bar gnus-article-make-menu-bar
2023       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2024       gnus-summary-highlight-line gnus-carpal-setup-buffer
2025       gnus-group-highlight-line
2026       gnus-article-add-button gnus-insert-next-page-button
2027       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2028      ("gnus-vis" :interactive t
2029       gnus-article-push-button gnus-article-press-button
2030       gnus-article-highlight gnus-article-highlight-some
2031       gnus-article-highlight-headers gnus-article-highlight-signature
2032       gnus-article-add-buttons gnus-article-add-buttons-to-head
2033       gnus-article-next-button gnus-article-prev-button)
2034      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2035       gnus-demon-add-disconnection gnus-demon-add-handler
2036       gnus-demon-remove-handler)
2037      ("gnus-demon" :interactive t
2038       gnus-demon-init gnus-demon-cancel)
2039      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2040       gnus-tree-open gnus-tree-close)
2041      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2042       gnus-nocem-unwanted-article-p)
2043      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2044      ("gnus-srvr" gnus-browse-foreign-server)
2045      ("gnus-cite" :interactive t
2046       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2047       gnus-article-hide-citation gnus-article-fill-cited-article
2048       gnus-article-hide-citation-in-followups)
2049      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2050       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2051       gnus-execute gnus-expunge)
2052      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2053       gnus-cache-possibly-remove-articles gnus-cache-request-article
2054       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2055       gnus-cache-enter-remove-article gnus-cached-article-p
2056       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2057      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2058       gnus-cache-remove-article)
2059      ("gnus-score" :interactive t
2060       gnus-summary-increase-score gnus-summary-lower-score
2061       gnus-score-flush-cache gnus-score-close
2062       gnus-score-raise-same-subject-and-select
2063       gnus-score-raise-same-subject gnus-score-default
2064       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2065       gnus-score-lower-same-subject gnus-score-lower-thread
2066       gnus-possibly-score-headers gnus-summary-raise-score 
2067       gnus-summary-set-score gnus-summary-current-score)
2068      ("gnus-score"
2069       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2070       gnus-current-score-file-nondirectory gnus-score-adaptive
2071       gnus-score-find-trace gnus-score-file-name)
2072      ("gnus-edit" :interactive t gnus-score-customize)
2073      ("gnus-topic" :interactive t gnus-topic-mode)
2074      ("gnus-topic" gnus-topic-remove-group)
2075      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2076      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2077      ("gnus-uu" :interactive t
2078       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2079       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2080       gnus-uu-mark-by-regexp gnus-uu-mark-all
2081       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2082       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2083       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2084       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2085       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2086       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2087       gnus-uu-decode-binhex-view)
2088      ("gnus-msg" (gnus-summary-send-map keymap)
2089       gnus-mail-yank-original gnus-mail-send-and-exit
2090       gnus-article-mail gnus-new-mail gnus-mail-reply)
2091      ("gnus-msg" :interactive t
2092       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2093       gnus-summary-followup gnus-summary-followup-with-original
2094       gnus-summary-cancel-article gnus-summary-supersede-article
2095       gnus-post-news gnus-inews-news 
2096       gnus-summary-reply gnus-summary-reply-with-original
2097       gnus-summary-mail-forward gnus-summary-mail-other-window
2098       gnus-bug)
2099      ("gnus-picon" :interactive t gnus-article-display-picons
2100       gnus-group-display-picons gnus-picons-article-display-x-face)
2101      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2102       gnus-grouplens-mode)
2103      ("smiley" :interactive t gnus-smiley-display)
2104      ("gnus-vm" gnus-vm-mail-setup)
2105      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2106       gnus-summary-save-article-vm))))
2107
2108 \f
2109
2110 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2111 ;; If you want the cursor to go somewhere else, set these two
2112 ;; functions in some startup hook to whatever you want.
2113 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2114 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2115
2116 ;;; Various macros and substs.
2117
2118 (defun gnus-header-from (header)
2119   (mail-header-from header))
2120
2121 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2122   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2123   (let ((tempvar (make-symbol "GnusStartBufferWindow"))
2124         (w (make-symbol "w"))
2125         (buf (make-symbol "buf")))
2126     `(let* ((,tempvar (selected-window))
2127             (,buf ,buffer)
2128             (,w (get-buffer-window ,buf 'visible)))
2129        (unwind-protect
2130            (progn
2131              (if ,w
2132                  (select-window ,w)
2133                (pop-to-buffer ,buf))
2134              ,@forms)
2135          (select-window ,tempvar)))))
2136
2137 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
2138 (put 'gnus-eval-in-buffer-window 'lisp-indent-hook 1)
2139 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
2140
2141 (defmacro gnus-gethash (string hashtable)
2142   "Get hash value of STRING in HASHTABLE."
2143   `(symbol-value (intern-soft ,string ,hashtable)))
2144
2145 (defmacro gnus-sethash (string value hashtable)
2146   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2147   `(set (intern ,string ,hashtable) ,value))
2148
2149 (defmacro gnus-intern-safe (string hashtable)
2150   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2151   `(let ((symbol (intern ,string ,hashtable)))
2152      (or (boundp symbol)
2153          (set symbol nil))
2154      symbol))
2155
2156 (defmacro gnus-group-unread (group)
2157   "Get the currently computed number of unread articles in GROUP."
2158   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2159
2160 (defmacro gnus-group-entry (group)
2161   "Get the newsrc entry for GROUP."
2162   `(gnus-gethash ,group gnus-newsrc-hashtb))
2163
2164 (defmacro gnus-active (group)
2165   "Get active info on GROUP."
2166   `(gnus-gethash ,group gnus-active-hashtb))
2167
2168 (defmacro gnus-set-active (group active)
2169   "Set GROUP's active info."
2170   `(gnus-sethash ,group ,active gnus-active-hashtb))
2171
2172 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2173 ;;   function `substring' might cut on a middle of multi-octet
2174 ;;   character.
2175 (defun gnus-truncate-string (str width)
2176   (substring str 0 width))
2177
2178 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2179 ;; to limit the length of a string.  This function is necessary since
2180 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2181 (defsubst gnus-limit-string (str width)
2182   (if (> (length str) width)
2183       (substring str 0 width)
2184     str))
2185
2186 (defsubst gnus-simplify-subject-re (subject)
2187   "Remove \"Re:\" from subject lines."
2188   (if (string-match "^[Rr][Ee]: *" subject)
2189       (substring subject (match-end 0))
2190     subject))
2191
2192 (defsubst gnus-functionp (form)
2193   "Return non-nil if FORM is funcallable."
2194   (or (and (symbolp form) (fboundp form))
2195       (and (listp form) (eq (car form) 'lambda))))
2196
2197 (defsubst gnus-goto-char (point)
2198   (and point (goto-char point)))
2199
2200 (defmacro gnus-buffer-exists-p (buffer)
2201   `(let ((buffer ,buffer))
2202      (and buffer
2203           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2204                    buffer))))
2205
2206 (defmacro gnus-kill-buffer (buffer)
2207   `(let ((buf ,buffer))
2208      (if (gnus-buffer-exists-p buf)
2209          (kill-buffer buf))))
2210
2211 (defsubst gnus-point-at-bol ()
2212   "Return point at the beginning of the line."
2213   (let ((p (point)))
2214     (beginning-of-line)
2215     (prog1
2216         (point)
2217       (goto-char p))))
2218
2219 (defsubst gnus-point-at-eol ()
2220   "Return point at the end of the line."
2221   (let ((p (point)))
2222     (end-of-line)
2223     (prog1
2224         (point)
2225       (goto-char p))))
2226
2227 (defun gnus-alive-p ()
2228   "Say whether Gnus is running or not."
2229   (and gnus-group-buffer
2230        (get-buffer gnus-group-buffer)))
2231
2232 (defun gnus-delete-first (elt list)
2233   "Delete by side effect the first occurrence of ELT as a member of LIST."
2234   (if (equal (car list) elt)
2235       (cdr list)
2236     (let ((total list))
2237       (while (and (cdr list)
2238                   (not (equal (cadr list) elt)))
2239         (setq list (cdr list)))
2240       (when (cdr list)
2241         (setcdr list (cddr list)))
2242       total)))
2243
2244 ;; Delete the current line (and the next N lines.);
2245 (defmacro gnus-delete-line (&optional n)
2246   `(delete-region (progn (beginning-of-line) (point))
2247                   (progn (forward-line ,(or n 1)) (point))))
2248
2249 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2250 (defvar gnus-init-inhibit nil)
2251 (defun gnus-read-init-file (&optional inhibit-next)
2252   (if gnus-init-inhibit
2253       (setq gnus-init-inhibit nil)
2254     (setq gnus-init-inhibit inhibit-next)
2255     (and gnus-init-file
2256          (or (and (file-exists-p gnus-init-file)
2257                   ;; Don't try to load a directory.
2258                   (not (file-directory-p gnus-init-file)))
2259              (file-exists-p (concat gnus-init-file ".el"))
2260              (file-exists-p (concat gnus-init-file ".elc")))
2261          (condition-case var
2262              (load gnus-init-file nil t)
2263            (error
2264             (error "Error in %s: %s" gnus-init-file var))))))
2265
2266 ;; Info access macros.
2267
2268 (defmacro gnus-info-group (info)
2269   `(nth 0 ,info))
2270 (defmacro gnus-info-rank (info)
2271   `(nth 1 ,info))
2272 (defmacro gnus-info-read (info)
2273   `(nth 2 ,info))
2274 (defmacro gnus-info-marks (info)
2275   `(nth 3 ,info))
2276 (defmacro gnus-info-method (info)
2277   `(nth 4 ,info))
2278 (defmacro gnus-info-params (info)
2279   `(nth 5 ,info))
2280
2281 (defmacro gnus-info-level (info)
2282   `(let ((rank (gnus-info-rank ,info)))
2283      (if (consp rank)
2284          (car rank)
2285        rank)))
2286 (defmacro gnus-info-score (info)
2287   `(let ((rank (gnus-info-rank ,info)))
2288      (or (and (consp rank) (cdr rank)) 0)))
2289
2290 (defmacro gnus-info-set-group (info group)
2291   `(setcar ,info ,group))
2292 (defmacro gnus-info-set-rank (info rank)
2293   `(setcar (nthcdr 1 ,info) ,rank))
2294 (defmacro gnus-info-set-read (info read)
2295   `(setcar (nthcdr 2 ,info) ,read))
2296 (defmacro gnus-info-set-marks (info marks)
2297   `(setcar (nthcdr 3 ,info) ,marks))
2298 (defmacro gnus-info-set-method (info method)
2299   `(setcar (nthcdr 4 ,info) ,method))
2300 (defmacro gnus-info-set-params (info params)
2301   `(setcar (nthcdr 5 ,info) ,params))
2302
2303 (defmacro gnus-info-set-level (info level)
2304   `(let ((rank (cdr ,info)))
2305      (if (consp (car rank))
2306          (setcar (car rank) ,level)
2307        (setcar rank ,level))))
2308 (defmacro gnus-info-set-score (info score)
2309   `(let ((rank (cdr ,info)))
2310      (if (consp (car rank))
2311          (setcdr (car rank) ,score)
2312        (setcar rank (cons (car rank) ,score)))))
2313
2314 (defmacro gnus-get-info (group)
2315   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2316
2317 (defun gnus-byte-code (func)
2318   "Return a form that can be `eval'ed based on FUNC."
2319   (let ((fval (symbol-function func)))
2320     (if (byte-code-function-p fval)
2321         (let ((flist (append fval nil)))
2322           (setcar flist 'byte-code)
2323           flist)
2324       (cons 'progn (cddr fval)))))
2325
2326 ;; Find out whether the gnus-visual TYPE is wanted.
2327 (defun gnus-visual-p (&optional type class)
2328   (and gnus-visual                      ; Has to be non-nil, at least.
2329        (if (not type)                   ; We don't care about type.
2330            gnus-visual
2331          (if (listp gnus-visual)        ; It's a list, so we check it.
2332              (or (memq type gnus-visual)
2333                  (memq class gnus-visual))
2334            t))))
2335
2336 ;;; Load the compatability functions.
2337
2338 (require 'gnus-cus)
2339 (require 'gnus-ems)
2340
2341 \f
2342 ;;;
2343 ;;; Shutdown
2344 ;;;
2345
2346 (defvar gnus-shutdown-alist nil)
2347
2348 (defun gnus-add-shutdown (function &rest symbols)
2349   "Run FUNCTION whenever one of SYMBOLS is shut down."
2350   (push (cons function symbols) gnus-shutdown-alist))
2351
2352 (defun gnus-shutdown (symbol)
2353   "Shut down everything that waits for SYMBOL."
2354   (let ((alist gnus-shutdown-alist)
2355         entry)
2356     (while (setq entry (pop alist))
2357       (when (memq symbol (cdr entry))
2358         (funcall (car entry))))))
2359
2360 \f
2361
2362 ;; Format specs.  The chunks below are the machine-generated forms
2363 ;; that are to be evaled as the result of the default format strings.
2364 ;; We write them in here to get them byte-compiled.  That way the
2365 ;; default actions will be quite fast, while still retaining the full
2366 ;; flexibility of the user-defined format specs.
2367
2368 ;; First we have lots of dummy defvars to let the compiler know these
2369 ;; are really dynamic variables.
2370
2371 (defvar gnus-tmp-unread)
2372 (defvar gnus-tmp-replied)
2373 (defvar gnus-tmp-score-char)
2374 (defvar gnus-tmp-indentation)
2375 (defvar gnus-tmp-opening-bracket)
2376 (defvar gnus-tmp-lines)
2377 (defvar gnus-tmp-name)
2378 (defvar gnus-tmp-closing-bracket)
2379 (defvar gnus-tmp-subject-or-nil)
2380 (defvar gnus-tmp-subject)
2381 (defvar gnus-tmp-marked)
2382 (defvar gnus-tmp-marked-mark)
2383 (defvar gnus-tmp-subscribed)
2384 (defvar gnus-tmp-process-marked)
2385 (defvar gnus-tmp-number-of-unread)
2386 (defvar gnus-tmp-group-name)
2387 (defvar gnus-tmp-group)
2388 (defvar gnus-tmp-article-number)
2389 (defvar gnus-tmp-unread-and-unselected)
2390 (defvar gnus-tmp-news-method)
2391 (defvar gnus-tmp-news-server)
2392 (defvar gnus-tmp-article-number)
2393 (defvar gnus-mouse-face)
2394 (defvar gnus-mouse-face-prop)
2395
2396 (defun gnus-summary-line-format-spec ()
2397   (insert gnus-tmp-unread gnus-tmp-replied
2398           gnus-tmp-score-char gnus-tmp-indentation)
2399   (gnus-put-text-property
2400    (point)
2401    (progn
2402      (insert
2403       gnus-tmp-opening-bracket
2404       (format "%4d: %-20s"
2405               gnus-tmp-lines
2406               (if (> (length gnus-tmp-name) 20)
2407                   (substring gnus-tmp-name 0 20)
2408                 gnus-tmp-name))
2409       gnus-tmp-closing-bracket)
2410      (point))
2411    gnus-mouse-face-prop gnus-mouse-face)
2412   (insert " " gnus-tmp-subject-or-nil "\n"))
2413
2414 (defvar gnus-summary-line-format-spec
2415   (gnus-byte-code 'gnus-summary-line-format-spec))
2416
2417 (defun gnus-summary-dummy-line-format-spec ()
2418   (insert "*  ")
2419   (gnus-put-text-property
2420    (point)
2421    (progn
2422      (insert ":                          :")
2423      (point))
2424    gnus-mouse-face-prop gnus-mouse-face)
2425   (insert " " gnus-tmp-subject "\n"))
2426
2427 (defvar gnus-summary-dummy-line-format-spec
2428   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2429
2430 (defun gnus-group-line-format-spec ()
2431   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2432           gnus-tmp-process-marked
2433           gnus-group-indentation
2434           (format "%5s: " gnus-tmp-number-of-unread))
2435   (gnus-put-text-property
2436    (point)
2437    (progn
2438      (insert gnus-tmp-group "\n")
2439      (1- (point)))
2440    gnus-mouse-face-prop gnus-mouse-face))
2441 (defvar gnus-group-line-format-spec
2442   (gnus-byte-code 'gnus-group-line-format-spec))
2443
2444 (defvar gnus-format-specs
2445   `((version . ,emacs-version)
2446     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2447     (summary-dummy ,gnus-summary-dummy-line-format
2448                    ,gnus-summary-dummy-line-format-spec)
2449     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2450
2451 (defvar gnus-article-mode-line-format-spec nil)
2452 (defvar gnus-summary-mode-line-format-spec nil)
2453 (defvar gnus-group-mode-line-format-spec nil)
2454
2455 ;;; Phew.  All that gruft is over, fortunately.
2456
2457 \f
2458 ;;;
2459 ;;; Gnus Utility Functions
2460 ;;;
2461
2462 (defun gnus-extract-address-components (from)
2463   (let (name address)
2464     ;; First find the address - the thing with the @ in it.  This may
2465     ;; not be accurate in mail addresses, but does the trick most of
2466     ;; the time in news messages.
2467     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2468         (setq address (substring from (match-beginning 0) (match-end 0))))
2469     ;; Then we check whether the "name <address>" format is used.
2470     (and address
2471          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2472          ;; Linear white space is not required.
2473          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2474          (and (setq name (substring from 0 (match-beginning 0)))
2475               ;; Strip any quotes from the name.
2476               (string-match "\".*\"" name)
2477               (setq name (substring name 1 (1- (match-end 0))))))
2478     ;; If not, then "address (name)" is used.
2479     (or name
2480         (and (string-match "(.+)" from)
2481              (setq name (substring from (1+ (match-beginning 0))
2482                                    (1- (match-end 0)))))
2483         (and (string-match "()" from)
2484              (setq name address))
2485         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2486         ;; XOVER might not support folded From headers.
2487         (and (string-match "(.*" from)
2488              (setq name (substring from (1+ (match-beginning 0))
2489                                    (match-end 0)))))
2490     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2491     (list (or name from) (or address from))))
2492
2493 (defun gnus-fetch-field (field)
2494   "Return the value of the header FIELD of current article."
2495   (save-excursion
2496     (save-restriction
2497       (let ((case-fold-search t)
2498             (inhibit-point-motion-hooks t))
2499         (nnheader-narrow-to-headers)
2500         (message-fetch-field field)))))
2501
2502 (defun gnus-goto-colon ()
2503   (beginning-of-line)
2504   (search-forward ":" (gnus-point-at-eol) t))
2505
2506 ;;;###autoload
2507 (defun gnus-update-format (var)
2508   "Update the format specification near point."
2509   (interactive
2510    (list
2511     (save-excursion
2512       (eval-defun nil)
2513       ;; Find the end of the current word.
2514       (re-search-forward "[ \t\n]" nil t)
2515       ;; Search backward.
2516       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2517         (match-string 1)))))
2518   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2519                               (match-string 1 var))))
2520          (entry (assq type gnus-format-specs))
2521          value spec)
2522     (when entry
2523       (setq gnus-format-specs (delq entry gnus-format-specs)))
2524     (set
2525      (intern (format "%s-spec" var))
2526      (gnus-parse-format (setq value (symbol-value (intern var)))
2527                         (symbol-value (intern (format "%s-alist" var)))
2528                         (not (string-match "mode" var))))
2529     (setq spec (symbol-value (intern (format "%s-spec" var))))
2530     (push (list type value spec) gnus-format-specs)
2531
2532     (pop-to-buffer "*Gnus Format*")
2533     (erase-buffer)
2534     (lisp-interaction-mode)
2535     (insert (pp-to-string spec))))
2536
2537 (defun gnus-update-format-specifications (&optional force)
2538   "Update all (necessary) format specifications."
2539   ;; Make the indentation array.
2540   (gnus-make-thread-indent-array)
2541
2542   ;; See whether all the stored info needs to be flushed.
2543   (when (or force
2544             (not (equal emacs-version
2545                         (cdr (assq 'version gnus-format-specs)))))
2546     (setq gnus-format-specs nil))
2547
2548   ;; Go through all the formats and see whether they need updating.
2549   (let ((types '(summary summary-dummy group
2550                          summary-mode group-mode article-mode))
2551         new-format entry type val)
2552     (while (setq type (pop types))
2553       ;; Jump to the proper buffer to find out the value of
2554       ;; the variable, if possible.  (It may be buffer-local.)
2555       (save-excursion
2556         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2557               val)
2558           (when (and (boundp buffer)
2559                      (setq val (symbol-value buffer))
2560                      (get-buffer val)
2561                      (buffer-name (get-buffer val)))
2562             (set-buffer (get-buffer val)))
2563           (setq new-format (symbol-value
2564                             (intern (format "gnus-%s-line-format" type))))))
2565       (setq entry (cdr (assq type gnus-format-specs)))
2566       (if (and entry
2567                (equal (car entry) new-format))
2568           ;; Use the old format.
2569           (set (intern (format "gnus-%s-line-format-spec" type))
2570                (cadr entry))
2571         ;; This is a new format.
2572         (setq val
2573               (if (not (stringp new-format))
2574                   ;; This is a function call or something.
2575                   new-format
2576                 ;; This is a "real" format.
2577                 (gnus-parse-format
2578                  new-format
2579                  (symbol-value
2580                   (intern (format "gnus-%s-line-format-alist"
2581                                   (if (eq type 'article-mode)
2582                                       'summary-mode type))))
2583                  (not (string-match "mode$" (symbol-name type))))))
2584         ;; Enter the new format spec into the list.
2585         (if entry
2586             (progn
2587               (setcar (cdr entry) val)
2588               (setcar entry new-format))
2589           (push (list type new-format val) gnus-format-specs))
2590         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2591
2592   (unless (assq 'version gnus-format-specs)
2593     (push (cons 'version emacs-version) gnus-format-specs))
2594
2595   (gnus-update-group-mark-positions)
2596   (gnus-update-summary-mark-positions))
2597
2598 (defun gnus-update-summary-mark-positions ()
2599   "Compute where the summary marks are to go."
2600   (save-excursion
2601     (when (and gnus-summary-buffer
2602                (get-buffer gnus-summary-buffer)
2603                (buffer-name (get-buffer gnus-summary-buffer)))
2604       (set-buffer gnus-summary-buffer))
2605     (let ((gnus-replied-mark 129)
2606           (gnus-score-below-mark 130)
2607           (gnus-score-over-mark 130)
2608           (thread nil)
2609           (gnus-visual nil)
2610           (spec gnus-summary-line-format-spec)
2611           pos)
2612       (save-excursion
2613         (gnus-set-work-buffer)
2614         (let ((gnus-summary-line-format-spec spec))
2615           (gnus-summary-insert-line
2616            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2617           (goto-char (point-min))
2618           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2619                                              (- (point) 2)))))
2620           (goto-char (point-min))
2621           (push (cons 'replied (and (search-forward "\201" nil t) 
2622                                     (- (point) 2)))
2623                 pos)
2624           (goto-char (point-min))
2625           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2626                 pos)))
2627       (setq gnus-summary-mark-positions pos))))
2628
2629 (defun gnus-update-group-mark-positions ()
2630   (save-excursion
2631     (let ((gnus-process-mark 128)
2632           (gnus-group-marked '("dummy.group"))
2633           (gnus-active-hashtb (make-vector 10 0)))
2634       (gnus-set-active "dummy.group" '(0 . 0))
2635       (gnus-set-work-buffer)
2636       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2637       (goto-char (point-min))
2638       (setq gnus-group-mark-positions
2639             (list (cons 'process (and (search-forward "\200" nil t)
2640                                       (- (point) 2))))))))
2641
2642 (defvar gnus-mouse-face-0 'highlight)
2643 (defvar gnus-mouse-face-1 'highlight)
2644 (defvar gnus-mouse-face-2 'highlight)
2645 (defvar gnus-mouse-face-3 'highlight)
2646 (defvar gnus-mouse-face-4 'highlight)
2647
2648 (defun gnus-mouse-face-function (form type)
2649   `(gnus-put-text-property
2650     (point) (progn ,@form (point))
2651     gnus-mouse-face-prop
2652     ,(if (equal type 0)
2653          'gnus-mouse-face
2654        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2655
2656 (defvar gnus-face-0 'bold)
2657 (defvar gnus-face-1 'italic)
2658 (defvar gnus-face-2 'bold-italic)
2659 (defvar gnus-face-3 'bold)
2660 (defvar gnus-face-4 'bold)
2661
2662 (defun gnus-face-face-function (form type)
2663   `(gnus-put-text-property
2664     (point) (progn ,@form (point))
2665     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2666
2667 (defun gnus-max-width-function (el max-width)
2668   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2669   (if (symbolp el)
2670       `(if (> (length ,el) ,max-width)
2671            (substring ,el 0 ,max-width)
2672          ,el)
2673     `(let ((val (eval ,el)))
2674        (if (numberp val)
2675            (setq val (int-to-string val)))
2676        (if (> (length val) ,max-width)
2677            (substring val 0 ,max-width)
2678          val))))
2679
2680 (defun gnus-parse-format (format spec-alist &optional insert)
2681   ;; This function parses the FORMAT string with the help of the
2682   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2683   ;; string.  If the FORMAT string contains the specifiers %( and %)
2684   ;; the text between them will have the mouse-face text property.
2685   (if (string-match
2686        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2687        format)
2688       (gnus-parse-complex-format format spec-alist)
2689     ;; This is a simple format.
2690     (gnus-parse-simple-format format spec-alist insert)))
2691
2692 (defun gnus-parse-complex-format (format spec-alist)
2693   (save-excursion
2694     (gnus-set-work-buffer)
2695     (insert format)
2696     (goto-char (point-min))
2697     (while (re-search-forward "\"" nil t)
2698       (replace-match "\\\"" nil t))
2699     (goto-char (point-min))
2700     (insert "(\"")
2701     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2702       (let ((number (if (match-beginning 1)
2703                         (match-string 1) "0"))
2704             (delim (aref (match-string 2) 0)))
2705         (if (or (= delim ?\() (= delim ?\{))
2706             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2707                                    " " number " \""))
2708           (replace-match "\")\""))))
2709     (goto-char (point-max))
2710     (insert "\")")
2711     (goto-char (point-min))
2712     (let ((form (read (current-buffer))))
2713       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2714
2715 (defun gnus-complex-form-to-spec (form spec-alist)
2716   (delq nil
2717         (mapcar
2718          (lambda (sform)
2719            (if (stringp sform)
2720                (gnus-parse-simple-format sform spec-alist t)
2721              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2722                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2723                       (nth 1 sform))))
2724          form)))
2725
2726 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2727   ;; This function parses the FORMAT string with the help of the
2728   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2729   ;; string.
2730   (let ((max-width 0)
2731         spec flist fstring newspec elem beg result dontinsert)
2732     (save-excursion
2733       (gnus-set-work-buffer)
2734       (insert format)
2735       (goto-char (point-min))
2736       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2737                                 nil t)
2738         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2739               (setq newspec "%"
2740                     beg (1+ (match-beginning 0)))
2741           ;; First check if there are any specs that look anything like
2742           ;; "%12,12A", ie. with a "max width specification".  These have
2743           ;; to be treated specially.
2744           (if (setq beg (match-beginning 1))
2745               (setq max-width
2746                     (string-to-int
2747                      (buffer-substring
2748                       (1+ (match-beginning 1)) (match-end 1))))
2749             (setq max-width 0)
2750             (setq beg (match-beginning 2)))
2751           ;; Find the specification from `spec-alist'.
2752           (unless (setq elem (cdr (assq spec spec-alist)))
2753             (setq elem '("*" ?s)))
2754           ;; Treat user defined format specifiers specially.
2755           (when (eq (car elem) 'gnus-tmp-user-defined)
2756             (setq elem
2757                   (list
2758                    (list (intern (concat "gnus-user-format-function-"
2759                                          (match-string 3)))
2760                          'gnus-tmp-header) ?s))
2761             (delete-region (match-beginning 3) (match-end 3)))
2762           (if (not (zerop max-width))
2763               (let ((el (car elem)))
2764                 (cond ((= (cadr elem) ?c)
2765                        (setq el (list 'char-to-string el)))
2766                       ((= (cadr elem) ?d)
2767                        (setq el (list 'int-to-string el))))
2768                 (setq flist (cons (gnus-max-width-function el max-width)
2769                                   flist))
2770                 (setq newspec ?s))
2771             (progn
2772               (setq flist (cons (car elem) flist))
2773               (setq newspec (cadr elem)))))
2774         ;; Remove the old specification (and possibly a ",12" string).
2775         (delete-region beg (match-end 2))
2776         ;; Insert the new specification.
2777         (goto-char beg)
2778         (insert newspec))
2779       (setq fstring (buffer-substring 1 (point-max))))
2780     ;; Do some postprocessing to increase efficiency.
2781     (setq
2782      result
2783      (cond
2784       ;; Emptyness.
2785       ((string= fstring "")
2786        nil)
2787       ;; Not a format string.
2788       ((not (string-match "%" fstring))
2789        (list fstring))
2790       ;; A format string with just a single string spec.
2791       ((string= fstring "%s")
2792        (list (car flist)))
2793       ;; A single character.
2794       ((string= fstring "%c")
2795        (list (car flist)))
2796       ;; A single number.
2797       ((string= fstring "%d")
2798        (setq dontinsert)
2799        (if insert
2800            (list `(princ ,(car flist)))
2801          (list `(int-to-string ,(car flist)))))
2802       ;; Just lots of chars and strings.
2803       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2804        (nreverse flist))
2805       ;; A single string spec at the beginning of the spec.
2806       ((string-match "\\`%[sc][^%]+\\'" fstring)
2807        (list (car flist) (substring fstring 2)))
2808       ;; A single string spec in the middle of the spec.
2809       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2810        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2811       ;; A single string spec in the end of the spec.
2812       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2813        (list (match-string 1 fstring) (car flist)))
2814       ;; A more complex spec.
2815       (t
2816        (list (cons 'format (cons fstring (nreverse flist)))))))
2817
2818     (if insert
2819         (when result
2820           (if dontinsert
2821               result
2822             (cons 'insert result)))
2823       (cond ((stringp result)
2824              result)
2825             ((consp result)
2826              (cons 'concat result))
2827             (t "")))))
2828
2829 (defun gnus-eval-format (format &optional alist props)
2830   "Eval the format variable FORMAT, using ALIST.
2831 If PROPS, insert the result."
2832   (let ((form (gnus-parse-format format alist props)))
2833     (if props
2834         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
2835       (eval form))))
2836
2837 (defun gnus-remove-text-with-property (prop)
2838   "Delete all text in the current buffer with text property PROP."
2839   (save-excursion
2840     (goto-char (point-min))
2841     (while (not (eobp))
2842       (while (get-text-property (point) prop)
2843         (delete-char 1))
2844       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2845
2846 (defun gnus-set-work-buffer ()
2847   (if (get-buffer gnus-work-buffer)
2848       (progn
2849         (set-buffer gnus-work-buffer)
2850         (erase-buffer))
2851     (set-buffer (get-buffer-create gnus-work-buffer))
2852     (kill-all-local-variables)
2853     (buffer-disable-undo (current-buffer))
2854     (gnus-add-current-to-buffer-list)))
2855
2856 ;; Article file names when saving.
2857
2858 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2859   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2860 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2861 Otherwise, it is like ~/News/news/group/num."
2862   (let ((default
2863           (expand-file-name
2864            (concat (if (gnus-use-long-file-name 'not-save)
2865                        (gnus-capitalize-newsgroup newsgroup)
2866                      (gnus-newsgroup-directory-form newsgroup))
2867                    "/" (int-to-string (mail-header-number headers)))
2868            gnus-article-save-directory)))
2869     (if (and last-file
2870              (string-equal (file-name-directory default)
2871                            (file-name-directory last-file))
2872              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2873         default
2874       (or last-file default))))
2875
2876 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2877   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2878 If variable `gnus-use-long-file-name' is non-nil, it is
2879 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2880   (let ((default
2881           (expand-file-name
2882            (concat (if (gnus-use-long-file-name 'not-save)
2883                        newsgroup
2884                      (gnus-newsgroup-directory-form newsgroup))
2885                    "/" (int-to-string (mail-header-number headers)))
2886            gnus-article-save-directory)))
2887     (if (and last-file
2888              (string-equal (file-name-directory default)
2889                            (file-name-directory last-file))
2890              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2891         default
2892       (or last-file default))))
2893
2894 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2895   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2896 If variable `gnus-use-long-file-name' is non-nil, it is
2897 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2898   (or last-file
2899       (expand-file-name
2900        (if (gnus-use-long-file-name 'not-save)
2901            (gnus-capitalize-newsgroup newsgroup)
2902          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2903        gnus-article-save-directory)))
2904
2905 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2906   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2907 If variable `gnus-use-long-file-name' is non-nil, it is
2908 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2909   (or last-file
2910       (expand-file-name
2911        (if (gnus-use-long-file-name 'not-save)
2912            newsgroup
2913          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2914        gnus-article-save-directory)))
2915
2916 ;; For subscribing new newsgroup
2917
2918 (defun gnus-subscribe-hierarchical-interactive (groups)
2919   (let ((groups (sort groups 'string<))
2920         prefixes prefix start ans group starts)
2921     (while groups
2922       (setq prefixes (list "^"))
2923       (while (and groups prefixes)
2924         (while (not (string-match (car prefixes) (car groups)))
2925           (setq prefixes (cdr prefixes)))
2926         (setq prefix (car prefixes))
2927         (setq start (1- (length prefix)))
2928         (if (and (string-match "[^\\.]\\." (car groups) start)
2929                  (cdr groups)
2930                  (setq prefix
2931                        (concat "^" (substring (car groups) 0 (match-end 0))))
2932                  (string-match prefix (cadr groups)))
2933             (progn
2934               (setq prefixes (cons prefix prefixes))
2935               (message "Descend hierarchy %s? ([y]nsq): "
2936                        (substring prefix 1 (1- (length prefix))))
2937               (while (not (memq (setq ans (read-char)) '(?y ?\n ?n ?s ?q)))
2938                 (ding)
2939                 (message "Descend hierarchy %s? ([y]nsq): "
2940                          (substring prefix 1 (1- (length prefix)))))
2941               (cond ((= ans ?n)
2942                      (while (and groups
2943                                  (string-match prefix
2944                                                (setq group (car groups))))
2945                        (setq gnus-killed-list
2946                              (cons group gnus-killed-list))
2947                        (gnus-sethash group group gnus-killed-hashtb)
2948                        (setq groups (cdr groups)))
2949                      (setq starts (cdr starts)))
2950                     ((= ans ?s)
2951                      (while (and groups
2952                                  (string-match prefix
2953                                                (setq group (car groups))))
2954                        (gnus-sethash group group gnus-killed-hashtb)
2955                        (gnus-subscribe-alphabetically (car groups))
2956                        (setq groups (cdr groups)))
2957                      (setq starts (cdr starts)))
2958                     ((= ans ?q)
2959                      (while groups
2960                        (setq group (car groups))
2961                        (setq gnus-killed-list (cons group gnus-killed-list))
2962                        (gnus-sethash group group gnus-killed-hashtb)
2963                        (setq groups (cdr groups))))
2964                     (t nil)))
2965           (message "Subscribe %s? ([n]yq)" (car groups))
2966           (while (not (memq (setq ans (read-char)) '(?y ?\n ?q ?n)))
2967             (ding)
2968             (message "Subscribe %s? ([n]yq)" (car groups)))
2969           (setq group (car groups))
2970           (cond ((= ans ?y)
2971                  (gnus-subscribe-alphabetically (car groups))
2972                  (gnus-sethash group group gnus-killed-hashtb))
2973                 ((= ans ?q)
2974                  (while groups
2975                    (setq group (car groups))
2976                    (setq gnus-killed-list (cons group gnus-killed-list))
2977                    (gnus-sethash group group gnus-killed-hashtb)
2978                    (setq groups (cdr groups))))
2979                 (t
2980                  (setq gnus-killed-list (cons group gnus-killed-list))
2981                  (gnus-sethash group group gnus-killed-hashtb)))
2982           (setq groups (cdr groups)))))))
2983
2984 (defun gnus-subscribe-randomly (newsgroup)
2985   "Subscribe new NEWSGROUP by making it the first newsgroup."
2986   (gnus-subscribe-newsgroup newsgroup))
2987
2988 (defun gnus-subscribe-alphabetically (newgroup)
2989   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2990   (let ((groups (cdr gnus-newsrc-alist))
2991         before)
2992     (while (and (not before) groups)
2993       (if (string< newgroup (caar groups))
2994           (setq before (caar groups))
2995         (setq groups (cdr groups))))
2996     (gnus-subscribe-newsgroup newgroup before)))
2997
2998 (defun gnus-subscribe-hierarchically (newgroup)
2999   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
3000   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
3001   (save-excursion
3002     (set-buffer (find-file-noselect gnus-current-startup-file))
3003     (let ((groupkey newgroup)
3004           before)
3005       (while (and (not before) groupkey)
3006         (goto-char (point-min))
3007         (let ((groupkey-re
3008                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
3009           (while (and (re-search-forward groupkey-re nil t)
3010                       (progn
3011                         (setq before (match-string 1))
3012                         (string< before newgroup)))))
3013         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
3014         (setq groupkey
3015               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
3016                   (substring groupkey (match-beginning 1) (match-end 1)))))
3017       (gnus-subscribe-newsgroup newgroup before))))
3018
3019 (defun gnus-subscribe-interactively (group)
3020   "Subscribe the new GROUP interactively.
3021 It is inserted in hierarchical newsgroup order if subscribed.  If not,
3022 it is killed."
3023   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
3024       (gnus-subscribe-hierarchically group)
3025     (push group gnus-killed-list)))
3026
3027 (defun gnus-subscribe-zombies (group)
3028   "Make the new GROUP into a zombie group."
3029   (push group gnus-zombie-list))
3030
3031 (defun gnus-subscribe-killed (group)
3032   "Make the new GROUP a killed group."
3033   (push group gnus-killed-list))
3034
3035 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
3036   "Subscribe new NEWSGROUP.
3037 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
3038 the first newsgroup."
3039   ;; We subscribe the group by changing its level to `subscribed'.
3040   (gnus-group-change-level
3041    newsgroup gnus-level-default-subscribed
3042    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
3043   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
3044
3045 ;; For directories
3046
3047 (defun gnus-newsgroup-directory-form (newsgroup)
3048   "Make hierarchical directory name from NEWSGROUP name."
3049   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
3050         (len (length newsgroup))
3051         idx)
3052     ;; If this is a foreign group, we don't want to translate the
3053     ;; entire name.
3054     (if (setq idx (string-match ":" newsgroup))
3055         (aset newsgroup idx ?/)
3056       (setq idx 0))
3057     ;; Replace all occurrences of `.' with `/'.
3058     (while (< idx len)
3059       (if (= (aref newsgroup idx) ?.)
3060           (aset newsgroup idx ?/))
3061       (setq idx (1+ idx)))
3062     newsgroup))
3063
3064 (defun gnus-newsgroup-savable-name (group)
3065   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3066   ;; with dots.
3067   (nnheader-replace-chars-in-string group ?/ ?.))
3068
3069 (defun gnus-make-directory (dir)
3070   "Make DIRECTORY recursively."
3071   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3072   ;; of the many mysteries of the universe.
3073   (let* ((dir (expand-file-name dir default-directory))
3074          dirs err)
3075     (if (string-match "/$" dir)
3076         (setq dir (substring dir 0 (match-beginning 0))))
3077     ;; First go down the path until we find a directory that exists.
3078     (while (not (file-exists-p dir))
3079       (setq dirs (cons dir dirs))
3080       (string-match "/[^/]+$" dir)
3081       (setq dir (substring dir 0 (match-beginning 0))))
3082     ;; Then create all the subdirs.
3083     (while (and dirs (not err))
3084       (condition-case ()
3085           (make-directory (car dirs))
3086         (error (setq err t)))
3087       (setq dirs (cdr dirs)))
3088     ;; We return whether we were successful or not.
3089     (not dirs)))
3090
3091 (defun gnus-capitalize-newsgroup (newsgroup)
3092   "Capitalize NEWSGROUP name."
3093   (and (not (zerop (length newsgroup)))
3094        (concat (char-to-string (upcase (aref newsgroup 0)))
3095                (substring newsgroup 1))))
3096
3097 ;; Various... things.
3098
3099 (defun gnus-simplify-subject (subject &optional re-only)
3100   "Remove `Re:' and words in parentheses.
3101 If RE-ONLY is non-nil, strip leading `Re:'s only."
3102   (let ((case-fold-search t))           ;Ignore case.
3103     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3104     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3105       (setq subject (substring subject (match-end 0))))
3106     ;; Remove uninteresting prefixes.
3107     (if (and (not re-only)
3108              gnus-simplify-ignored-prefixes
3109              (string-match gnus-simplify-ignored-prefixes subject))
3110         (setq subject (substring subject (match-end 0))))
3111     ;; Remove words in parentheses from end.
3112     (unless re-only
3113       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3114         (setq subject (substring subject 0 (match-beginning 0)))))
3115     ;; Return subject string.
3116     subject))
3117
3118 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3119 ;; all whitespace.
3120 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3121 (defun gnus-simplify-buffer-fuzzy ()
3122   (let ((case-fold-search t))
3123     (goto-char (point-min))
3124     (while (search-forward "\t" nil t)
3125       (replace-match " " t t))
3126     (goto-char (point-min))
3127     (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3128     (goto-char (match-beginning 0))
3129     (while (or
3130             (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3131             (looking-at "^[[].*: .*[]]$"))
3132       (goto-char (point-min))
3133       (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3134                                 nil t)
3135         (replace-match "" t t))
3136       (goto-char (point-min))
3137       (while (re-search-forward "^[[].*: .*[]]$" nil t)
3138         (goto-char (match-end 0))
3139         (delete-char -1)
3140         (delete-region
3141          (progn (goto-char (match-beginning 0)))
3142          (re-search-forward ":"))))
3143     (goto-char (point-min))
3144     (while (re-search-forward " *[[{(][^()\n]*[]})] *$" 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     (while (re-search-forward " $" nil t)
3151       (replace-match "" t t))
3152     (goto-char (point-min))
3153     (while (re-search-forward "^ +" nil t)
3154       (replace-match "" t t))
3155     (goto-char (point-min))
3156     (when gnus-simplify-subject-fuzzy-regexp
3157       (if (listp gnus-simplify-subject-fuzzy-regexp)
3158           (let ((list gnus-simplify-subject-fuzzy-regexp))
3159             (while list
3160               (goto-char (point-min))
3161               (while (re-search-forward (car list) nil t)
3162                 (replace-match "" t t))
3163               (setq list (cdr list))))
3164         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3165           (replace-match "" t t))))))
3166
3167 (defun gnus-simplify-subject-fuzzy (subject)
3168   "Siplify a subject string fuzzily."
3169   (save-excursion
3170     (gnus-set-work-buffer)
3171     (let ((case-fold-search t))
3172       (insert subject)
3173       (inline (gnus-simplify-buffer-fuzzy))
3174       (buffer-string))))
3175
3176 ;; Add the current buffer to the list of buffers to be killed on exit.
3177 (defun gnus-add-current-to-buffer-list ()
3178   (or (memq (current-buffer) gnus-buffer-list)
3179       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3180
3181 (defun gnus-string> (s1 s2)
3182   (not (or (string< s1 s2)
3183            (string= s1 s2))))
3184
3185 (defun gnus-read-active-file-p ()
3186   "Say whether the active file has been read from `gnus-select-method'."
3187   (memq gnus-select-method gnus-have-read-active-file))
3188
3189 ;;; General various misc type functions.
3190
3191 (defun gnus-clear-system ()
3192   "Clear all variables and buffers."
3193   ;; Clear Gnus variables.
3194   (let ((variables gnus-variable-list))
3195     (while variables
3196       (set (car variables) nil)
3197       (setq variables (cdr variables))))
3198   ;; Clear other internal variables.
3199   (setq gnus-list-of-killed-groups nil
3200         gnus-have-read-active-file nil
3201         gnus-newsrc-alist nil
3202         gnus-newsrc-hashtb nil
3203         gnus-killed-list nil
3204         gnus-zombie-list nil
3205         gnus-killed-hashtb nil
3206         gnus-active-hashtb nil
3207         gnus-moderated-list nil
3208         gnus-description-hashtb nil
3209         gnus-current-headers nil
3210         gnus-thread-indent-array nil
3211         gnus-newsgroup-headers nil
3212         gnus-newsgroup-name nil
3213         gnus-server-alist nil
3214         gnus-group-list-mode nil
3215         gnus-opened-servers nil
3216         gnus-current-select-method nil)
3217   (gnus-shutdown 'gnus)
3218   ;; Kill the startup file.
3219   (and gnus-current-startup-file
3220        (get-file-buffer gnus-current-startup-file)
3221        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3222   ;; Clear the dribble buffer.
3223   (gnus-dribble-clear)
3224   ;; Kill global KILL file buffer.
3225   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3226     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3227   (gnus-kill-buffer nntp-server-buffer)
3228   ;; Kill Gnus buffers.
3229   (while gnus-buffer-list
3230     (gnus-kill-buffer (pop gnus-buffer-list)))
3231   ;; Remove Gnus frames.
3232   (gnus-kill-gnus-frames))
3233
3234 (defun gnus-kill-gnus-frames ()
3235   "Kill all frames Gnus has created."
3236   (while gnus-created-frames
3237     (when (frame-live-p (car gnus-created-frames))
3238       ;; We slap a condition-case around this `delete-frame' to ensure 
3239       ;; against errors if we try do delete the single frame that's left.
3240       (condition-case ()
3241           (delete-frame (car gnus-created-frames))
3242         (error nil)))
3243     (pop gnus-created-frames)))
3244
3245 (defun gnus-windows-old-to-new (setting)
3246   ;; First we take care of the really, really old Gnus 3 actions.
3247   (when (symbolp setting)
3248     (setq setting
3249           ;; Take care of ooold GNUS 3.x values.
3250           (cond ((eq setting 'SelectArticle) 'article)
3251                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3252                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3253                 (t setting))))
3254   (if (or (listp setting)
3255           (not (and gnus-window-configuration
3256                     (memq setting '(group summary article)))))
3257       setting
3258     (let* ((setting (if (eq setting 'group)
3259                         (if (assq 'newsgroup gnus-window-configuration)
3260                             'newsgroup
3261                           'newsgroups) setting))
3262            (elem (cadr (assq setting gnus-window-configuration)))
3263            (total (apply '+ elem))
3264            (types '(group summary article))
3265            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3266            (i 0)
3267            perc
3268            out)
3269       (while (< i 3)
3270         (or (not (numberp (nth i elem)))
3271             (zerop (nth i elem))
3272             (progn
3273               (setq perc (if (= i 2)
3274                              1.0
3275                            (/ (float (nth 0 elem)) total)))
3276               (setq out (cons (if (eq pbuf (nth i types))
3277                                   (list (nth i types) perc 'point)
3278                                 (list (nth i types) perc))
3279                               out))))
3280         (setq i (1+ i)))
3281       `(vertical 1.0 ,@(nreverse out)))))
3282
3283 ;;;###autoload
3284 (defun gnus-add-configuration (conf)
3285   "Add the window configuration CONF to `gnus-buffer-configuration'."
3286   (setq gnus-buffer-configuration
3287         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3288                          gnus-buffer-configuration))))
3289
3290 (defvar gnus-frame-list nil)
3291
3292 (defun gnus-configure-frame (split &optional window)
3293   "Split WINDOW according to SPLIT."
3294   (unless window
3295     (setq window (get-buffer-window (current-buffer))))
3296   (select-window window)
3297   ;; This might be an old-stylee buffer config.
3298   (when (vectorp split)
3299     (setq split (append split nil)))
3300   (when (or (consp (car split))
3301             (vectorp (car split)))
3302     (push 1.0 split)
3303     (push 'vertical split))
3304   ;; The SPLIT might be something that is to be evaled to
3305   ;; return a new SPLIT.
3306   (while (and (not (assq (car split) gnus-window-to-buffer))
3307               (gnus-functionp (car split)))
3308     (setq split (eval split)))
3309   (let* ((type (car split))
3310          (subs (cddr split))
3311          (len (if (eq type 'horizontal) (window-width) (window-height)))
3312          (total 0)
3313          (window-min-width (or gnus-window-min-width window-min-width))
3314          (window-min-height (or gnus-window-min-height window-min-height))
3315          s result new-win rest comp-subs size sub)
3316     (cond
3317      ;; Nothing to do here.
3318      ((null split))
3319      ;; Don't switch buffers.
3320      ((null type)
3321       (and (memq 'point split) window))
3322      ;; This is a buffer to be selected.
3323      ((not (memq type '(frame horizontal vertical)))
3324       (let ((buffer (cond ((stringp type) type)
3325                           (t (cdr (assq type gnus-window-to-buffer)))))
3326             buf)
3327         (unless buffer
3328           (error "Illegal buffer type: %s" type))
3329         (unless (setq buf (get-buffer (if (symbolp buffer)
3330                                           (symbol-value buffer) buffer)))
3331           (setq buf (get-buffer-create (if (symbolp buffer)
3332                                            (symbol-value buffer) buffer))))
3333         (switch-to-buffer buf)
3334         ;; We return the window if it has the `point' spec.
3335         (and (memq 'point split) window)))
3336      ;; This is a frame split.
3337      ((eq type 'frame)
3338       (unless gnus-frame-list
3339         (setq gnus-frame-list (list (window-frame
3340                                      (get-buffer-window (current-buffer))))))
3341       (let ((i 0)
3342             params frame fresult)
3343         (while (< i (length subs))
3344           ;; Frame parameter is gotten from the sub-split.
3345           (setq params (cadr (elt subs i)))
3346           ;; It should be a list.
3347           (unless (listp params)
3348             (setq params nil))
3349           ;; Create a new frame?
3350           (unless (setq frame (elt gnus-frame-list i))
3351             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3352             (push frame gnus-created-frames))
3353           ;; Is the old frame still alive?
3354           (unless (frame-live-p frame)
3355             (setcar (nthcdr i gnus-frame-list)
3356                     (setq frame (make-frame params))))
3357           ;; Select the frame in question and do more splits there.
3358           (select-frame frame)
3359           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3360           (incf i))
3361         ;; Select the frame that has the selected buffer.
3362         (when fresult
3363           (select-frame (window-frame fresult)))))
3364      ;; This is a normal split.
3365      (t
3366       (when (> (length subs) 0)
3367         ;; First we have to compute the sizes of all new windows.
3368         (while subs
3369           (setq sub (append (pop subs) nil))
3370           (while (and (not (assq (car sub) gnus-window-to-buffer))
3371                       (gnus-functionp (car sub)))
3372             (setq sub (eval sub)))
3373           (when sub
3374             (push sub comp-subs)
3375             (setq size (cadar comp-subs))
3376             (cond ((equal size 1.0)
3377                    (setq rest (car comp-subs))
3378                    (setq s 0))
3379                   ((floatp size)
3380                    (setq s (floor (* size len))))
3381                   ((integerp size)
3382                    (setq s size))
3383                   (t
3384                    (error "Illegal size: %s" size)))
3385             ;; Try to make sure that we are inside the safe limits.
3386             (cond ((zerop s))
3387                   ((eq type 'horizontal)
3388                    (setq s (max s window-min-width)))
3389                   ((eq type 'vertical)
3390                    (setq s (max s window-min-height))))
3391             (setcar (cdar comp-subs) s)
3392             (incf total s)))
3393         ;; Take care of the "1.0" spec.
3394         (if rest
3395             (setcar (cdr rest) (- len total))
3396           (error "No 1.0 specs in %s" split))
3397         ;; The we do the actual splitting in a nice recursive
3398         ;; fashion.
3399         (setq comp-subs (nreverse comp-subs))
3400         (while comp-subs
3401           (if (null (cdr comp-subs))
3402               (setq new-win window)
3403             (setq new-win
3404                   (split-window window (cadar comp-subs)
3405                                 (eq type 'horizontal))))
3406           (setq result (or (gnus-configure-frame
3407                             (car comp-subs) window) result))
3408           (select-window new-win)
3409           (setq window new-win)
3410           (setq comp-subs (cdr comp-subs))))
3411       ;; Return the proper window, if any.
3412       (when result
3413         (select-window result))))))
3414
3415 (defvar gnus-frame-split-p nil)
3416
3417 (defun gnus-configure-windows (setting &optional force)
3418   (setq setting (gnus-windows-old-to-new setting))
3419   (let ((split (if (symbolp setting)
3420                    (cadr (assq setting gnus-buffer-configuration))
3421                  setting))
3422         all-visible)
3423
3424     (setq gnus-frame-split-p nil)
3425
3426     (unless split
3427       (error "No such setting: %s" setting))
3428
3429     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3430              (not force))
3431         ;; All the windows mentioned are already visible, so we just
3432         ;; put point in the assigned buffer, and do not touch the
3433         ;; winconf.
3434         (select-window all-visible)
3435
3436       ;; Either remove all windows or just remove all Gnus windows.
3437       (let ((frame (selected-frame)))
3438         (unwind-protect
3439             (if gnus-use-full-window
3440                 ;; We want to remove all other windows.
3441                 (if (not gnus-frame-split-p)
3442                     ;; This is not a `frame' split, so we ignore the
3443                     ;; other frames.  
3444                     (delete-other-windows)
3445                   ;; This is a `frame' split, so we delete all windows
3446                   ;; on all frames.
3447                   (mapcar 
3448                    (lambda (frame)
3449                      (unless (eq (cdr (assq 'minibuffer
3450                                             (frame-parameters frame)))
3451                                  'only)
3452                        (select-frame frame)
3453                        (delete-other-windows)))
3454                    (frame-list)))
3455               ;; Just remove some windows.
3456               (gnus-remove-some-windows)
3457               (switch-to-buffer nntp-server-buffer))
3458           (select-frame frame)))
3459
3460       (switch-to-buffer nntp-server-buffer)
3461       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3462
3463 (defun gnus-all-windows-visible-p (split)
3464   "Say whether all buffers in SPLIT are currently visible.
3465 In particular, the value returned will be the window that
3466 should have point."
3467   (let ((stack (list split))
3468         (all-visible t)
3469         type buffer win buf)
3470     (while (and (setq split (pop stack))
3471                 all-visible)
3472       ;; Be backwards compatible.
3473       (when (vectorp split)
3474         (setq split (append split nil)))
3475       (when (or (consp (car split))
3476                 (vectorp (car split)))
3477         (push 1.0 split)
3478         (push 'vertical split))
3479       ;; The SPLIT might be something that is to be evaled to
3480       ;; return a new SPLIT.
3481       (while (and (not (assq (car split) gnus-window-to-buffer))
3482                   (gnus-functionp (car split)))
3483         (setq split (eval split)))
3484
3485       (setq type (elt split 0))
3486       (cond
3487        ;; Nothing here.
3488        ((null split) t)
3489        ;; A buffer.
3490        ((not (memq type '(horizontal vertical frame)))
3491         (setq buffer (cond ((stringp type) type)
3492                            (t (cdr (assq type gnus-window-to-buffer)))))
3493         (unless buffer
3494           (error "Illegal buffer type: %s" type))
3495         (when (setq buf (get-buffer (if (symbolp buffer)
3496                                         (symbol-value buffer)
3497                                       buffer)))
3498           (setq win (get-buffer-window buf t)))
3499         (if win
3500             (when (memq 'point split)
3501                 (setq all-visible win))
3502           (setq all-visible nil)))
3503        (t
3504         (when (eq type 'frame)
3505           (setq gnus-frame-split-p t))
3506         (setq stack (append (cddr split) stack)))))
3507     (unless (eq all-visible t)
3508       all-visible)))
3509
3510 (defun gnus-window-top-edge (&optional window)
3511   (nth 1 (window-edges window)))
3512
3513 (defun gnus-remove-some-windows ()
3514   (let ((buffers gnus-window-to-buffer)
3515         buf bufs lowest-buf lowest)
3516     (save-excursion
3517       ;; Remove windows on all known Gnus buffers.
3518       (while buffers
3519         (setq buf (cdar buffers))
3520         (if (symbolp buf)
3521             (setq buf (and (boundp buf) (symbol-value buf))))
3522         (and buf
3523              (get-buffer-window buf)
3524              (progn
3525                (setq bufs (cons buf bufs))
3526                (pop-to-buffer buf)
3527                (if (or (not lowest)
3528                        (< (gnus-window-top-edge) lowest))
3529                    (progn
3530                      (setq lowest (gnus-window-top-edge))
3531                      (setq lowest-buf buf)))))
3532         (setq buffers (cdr buffers)))
3533       ;; Remove windows on *all* summary buffers.
3534       (walk-windows
3535        (lambda (win)
3536          (let ((buf (window-buffer win)))
3537            (if (string-match    "^\\*Summary" (buffer-name buf))
3538                (progn
3539                  (setq bufs (cons buf bufs))
3540                  (pop-to-buffer buf)
3541                  (if (or (not lowest)
3542                          (< (gnus-window-top-edge) lowest))
3543                      (progn
3544                        (setq lowest-buf buf)
3545                        (setq lowest (gnus-window-top-edge)))))))))
3546       (and lowest-buf
3547            (progn
3548              (pop-to-buffer lowest-buf)
3549              (switch-to-buffer nntp-server-buffer)))
3550       (while bufs
3551         (and (not (eq (car bufs) lowest-buf))
3552              (delete-windows-on (car bufs)))
3553         (setq bufs (cdr bufs))))))
3554
3555 (defun gnus-version (&optional arg)
3556   "Version number of this version of Gnus.
3557 If ARG, insert string at point."
3558   (interactive "P")
3559   (let ((methods gnus-valid-select-methods)
3560         (mess gnus-version)
3561         meth)
3562     ;; Go through all the legal select methods and add their version
3563     ;; numbers to the total version string.  Only the backends that are
3564     ;; currently in use will have their message numbers taken into
3565     ;; consideration.
3566     (while methods
3567       (setq meth (intern (concat (caar methods) "-version")))
3568       (and (boundp meth)
3569            (stringp (symbol-value meth))
3570            (setq mess (concat mess "; " (symbol-value meth))))
3571       (setq methods (cdr methods)))
3572     (if arg
3573         (insert (message mess))
3574       (message mess))))
3575
3576 (defun gnus-info-find-node ()
3577   "Find Info documentation of Gnus."
3578   (interactive)
3579   ;; Enlarge info window if needed.
3580   (let ((mode major-mode)
3581         gnus-info-buffer)
3582     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3583     (setq gnus-info-buffer (current-buffer))
3584     (gnus-configure-windows 'info)))
3585
3586 (defun gnus-days-between (date1 date2)
3587   ;; Return the number of days between date1 and date2.
3588   (- (gnus-day-number date1) (gnus-day-number date2)))
3589
3590 (defun gnus-day-number (date)
3591   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3592                      (timezone-parse-date date))))
3593     (timezone-absolute-from-gregorian
3594      (nth 1 dat) (nth 2 dat) (car dat))))
3595
3596 (defun gnus-encode-date (date)
3597   "Convert DATE to internal time."
3598   (let* ((parse (timezone-parse-date date))
3599          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3600          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3601     (encode-time (caddr time) (cadr time) (car time)
3602                  (caddr date) (cadr date) (car date) (nth 4 date))))
3603
3604 (defun gnus-time-minus (t1 t2)
3605   "Subtract two internal times."
3606   (let ((borrow (< (cadr t1) (cadr t2))))
3607     (list (- (car t1) (car t2) (if borrow 1 0))
3608           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3609
3610 (defun gnus-file-newer-than (file date)
3611   (let ((fdate (nth 5 (file-attributes file))))
3612     (or (> (car fdate) (car date))
3613         (and (= (car fdate) (car date))
3614              (> (nth 1 fdate) (nth 1 date))))))
3615
3616 (defmacro gnus-local-set-keys (&rest plist)
3617   "Set the keys in PLIST in the current keymap."
3618   `(gnus-define-keys-1 (current-local-map) ',plist))
3619
3620 (defmacro gnus-define-keys (keymap &rest plist)
3621   "Define all keys in PLIST in KEYMAP."
3622   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3623
3624 (put 'gnus-define-keys 'lisp-indent-function 1)
3625 (put 'gnus-define-keys 'lisp-indent-hook 1)
3626 (put 'gnus-define-keymap 'lisp-indent-function 1)
3627 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3628
3629 (defmacro gnus-define-keymap (keymap &rest plist)
3630   "Define all keys in PLIST in KEYMAP."
3631   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3632
3633 (defun gnus-define-keys-1 (keymap plist)
3634   (when (null keymap)
3635     (error "Can't set keys in a null keymap"))
3636   (cond ((symbolp keymap)
3637          (setq keymap (symbol-value keymap)))
3638         ((keymapp keymap))
3639         ((listp keymap)
3640          (set (car keymap) nil)
3641          (define-prefix-command (car keymap))
3642          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3643          (setq keymap (symbol-value (car keymap)))))
3644   (let (key)
3645     (while plist
3646       (when (symbolp (setq key (pop plist)))
3647         (setq key (symbol-value key)))
3648       (define-key keymap key (pop plist)))))
3649
3650 (defun gnus-group-read-only-p (&optional group)
3651   "Check whether GROUP supports editing or not.
3652 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3653 that that variable is buffer-local to the summary buffers."
3654   (let ((group (or group gnus-newsgroup-name)))
3655     (not (gnus-check-backend-function 'request-replace-article group))))
3656
3657 (defun gnus-group-total-expirable-p (group)
3658   "Check whether GROUP is total-expirable or not."
3659   (let ((params (gnus-info-params (gnus-get-info group))))
3660     (or (memq 'total-expire params)
3661         (cdr (assq 'total-expire params)) ; (total-expire . t)
3662         (and gnus-total-expirable-newsgroups ; Check var.
3663              (string-match gnus-total-expirable-newsgroups group)))))
3664
3665 (defun gnus-group-auto-expirable-p (group)
3666   "Check whether GROUP is total-expirable or not."
3667   (let ((params (gnus-info-params (gnus-get-info group))))
3668     (or (memq 'auto-expire params)
3669         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3670         (and gnus-auto-expirable-newsgroups ; Check var.
3671              (string-match gnus-auto-expirable-newsgroups group)))))
3672
3673 (defun gnus-virtual-group-p (group)
3674   "Say whether GROUP is virtual or not."
3675   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3676                         gnus-valid-select-methods)))
3677
3678 (defun gnus-news-group-p (group &optional article)
3679   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3680   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3681       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3682            (eq (gnus-request-type group article) 'news))))
3683
3684 (defsubst gnus-simplify-subject-fully (subject)
3685   "Simplify a subject string according to the user's wishes."
3686   (cond
3687    ((null gnus-summary-gather-subject-limit)
3688     (gnus-simplify-subject-re subject))
3689    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3690     (gnus-simplify-subject-fuzzy subject))
3691    ((numberp gnus-summary-gather-subject-limit)
3692     (gnus-limit-string (gnus-simplify-subject-re subject)
3693                        gnus-summary-gather-subject-limit))
3694    (t
3695     subject)))
3696
3697 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3698   "Check whether two subjects are equal.  If optional argument
3699 simple-first is t, first argument is already simplified."
3700   (cond
3701    ((null simple-first)
3702     (equal (gnus-simplify-subject-fully s1)
3703            (gnus-simplify-subject-fully s2)))
3704    (t
3705     (equal s1
3706            (gnus-simplify-subject-fully s2)))))
3707
3708 ;; Returns a list of writable groups.
3709 (defun gnus-writable-groups ()
3710   (let ((alist gnus-newsrc-alist)
3711         groups group)
3712     (while (setq group (car (pop alist)))
3713       (unless (gnus-group-read-only-p group)
3714         (push group groups)))
3715     (nreverse groups)))
3716
3717 (defun gnus-completing-read (default prompt &rest args)
3718   ;; Like `completing-read', except that DEFAULT is the default argument.
3719   (let* ((prompt (if default 
3720                      (concat prompt " (default " default ") ")
3721                    (concat prompt " ")))
3722          (answer (apply 'completing-read prompt args)))
3723     (if (or (null answer) (zerop (length answer)))
3724         default
3725       answer)))
3726
3727 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3728 ;; the echo area.
3729 (defun gnus-y-or-n-p (prompt)
3730   (prog1
3731       (y-or-n-p prompt)
3732     (message "")))
3733
3734 (defun gnus-yes-or-no-p (prompt)
3735   (prog1
3736       (yes-or-no-p prompt)
3737     (message "")))
3738
3739 ;; Check whether to use long file names.
3740 (defun gnus-use-long-file-name (symbol)
3741   ;; The variable has to be set...
3742   (and gnus-use-long-file-name
3743        ;; If it isn't a list, then we return t.
3744        (or (not (listp gnus-use-long-file-name))
3745            ;; If it is a list, and the list contains `symbol', we
3746            ;; return nil.
3747            (not (memq symbol gnus-use-long-file-name)))))
3748
3749 ;; I suspect there's a better way, but I haven't taken the time to do
3750 ;; it yet. -erik selberg@cs.washington.edu
3751 (defun gnus-dd-mmm (messy-date)
3752   "Return a string like DD-MMM from a big messy string"
3753   (let ((datevec (condition-case () (timezone-parse-date messy-date) 
3754                    (error nil))))
3755     (if (not datevec)
3756         "??-???"
3757       (format "%2s-%s"
3758               (condition-case ()
3759                   ;; Make sure leading zeroes are stripped.
3760                   (number-to-string (string-to-number (aref datevec 2)))
3761                 (error "??"))
3762               (capitalize
3763                (or (car
3764                     (nth (1- (string-to-number (aref datevec 1)))
3765                          timezone-months-assoc))
3766                    "???"))))))
3767
3768 (defun gnus-mode-string-quote (string)
3769   "Quote all \"%\" in STRING."
3770   (save-excursion
3771     (gnus-set-work-buffer)
3772     (insert string)
3773     (goto-char (point-min))
3774     (while (search-forward "%" nil t)
3775       (insert "%"))
3776     (buffer-string)))
3777
3778 ;; Make a hash table (default and minimum size is 255).
3779 ;; Optional argument HASHSIZE specifies the table size.
3780 (defun gnus-make-hashtable (&optional hashsize)
3781   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3782
3783 ;; Make a number that is suitable for hashing; bigger than MIN and one
3784 ;; less than 2^x.
3785 (defun gnus-create-hash-size (min)
3786   (let ((i 1))
3787     (while (< i min)
3788       (setq i (* 2 i)))
3789     (1- i)))
3790
3791 ;; Show message if message has a lower level than `gnus-verbose'.
3792 ;; Guideline for numbers:
3793 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3794 ;; for things that take a long time, 7 - not very important messages
3795 ;; on stuff, 9 - messages inside loops.
3796 (defun gnus-message (level &rest args)
3797   (if (<= level gnus-verbose)
3798       (apply 'message args)
3799     ;; We have to do this format thingy here even if the result isn't
3800     ;; shown - the return value has to be the same as the return value
3801     ;; from `message'.
3802     (apply 'format args)))
3803
3804 (defun gnus-error (level &rest args)
3805   "Beep an error if `gnus-verbose' is on LEVEL or less."
3806   (when (<= (floor level) gnus-verbose)
3807     (apply 'message args)
3808     (ding)
3809     (let (duration)
3810       (when (and (floatp level)
3811                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3812         (sit-for duration))))
3813   nil)
3814
3815 ;; Generate a unique new group name.
3816 (defun gnus-generate-new-group-name (leaf)
3817   (let ((name leaf)
3818         (num 0))
3819     (while (gnus-gethash name gnus-newsrc-hashtb)
3820       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3821     name))
3822
3823 (defsubst gnus-hide-text (b e props)
3824   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
3825   (gnus-add-text-properties b e props)
3826   (when (memq 'intangible props)
3827     (gnus-put-text-property (max (1- b) (point-min))
3828                        b 'intangible (cddr (memq 'intangible props)))))
3829
3830 (defsubst gnus-unhide-text (b e)
3831   "Remove hidden text properties from region between B and E."
3832   (remove-text-properties b e gnus-hidden-properties)
3833   (when (memq 'intangible gnus-hidden-properties)
3834     (gnus-put-text-property (max (1- b) (point-min))
3835                             b 'intangible nil)))
3836
3837 (defun gnus-hide-text-type (b e type)
3838   "Hide text of TYPE between B and E."
3839   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3840
3841 (defun gnus-parent-headers (headers &optional generation)
3842   "Return the headers of the GENERATIONeth parent of HEADERS."
3843   (unless generation 
3844     (setq generation 1))
3845   (let (references parent)
3846     (while (and headers (not (zerop generation)))
3847       (setq references (mail-header-references headers))
3848       (when (and references
3849                  (setq parent (gnus-parent-id references))
3850                  (setq headers (car (gnus-id-to-thread parent))))
3851         (decf generation)))
3852     headers))
3853
3854 (defun gnus-parent-id (references)
3855   "Return the last Message-ID in REFERENCES."
3856   (when (and references
3857              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3858     (substring references (match-beginning 1) (match-end 1))))
3859
3860 (defun gnus-split-references (references)
3861   "Return a list of Message-IDs in REFERENCES."
3862   (let ((beg 0)
3863         ids)
3864     (while (string-match "<[^>]+>" references beg)
3865       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3866             ids))
3867     (nreverse ids)))
3868
3869 (defun gnus-buffer-live-p (buffer)
3870   "Say whether BUFFER is alive or not."
3871   (and buffer
3872        (get-buffer buffer)
3873        (buffer-name (get-buffer buffer))))
3874
3875 (defun gnus-ephemeral-group-p (group)
3876   "Say whether GROUP is ephemeral or not."
3877   (gnus-group-get-parameter group 'quit-config))
3878
3879 (defun gnus-group-quit-config (group)
3880   "Return the quit-config of GROUP."
3881   (gnus-group-get-parameter group 'quit-config))
3882
3883 (defun gnus-simplify-mode-line ()
3884   "Make mode lines a bit simpler."
3885   (setq mode-line-modified "-- ")
3886   (when (listp mode-line-format)
3887     (make-local-variable 'mode-line-format)
3888     (setq mode-line-format (copy-sequence mode-line-format))
3889     (when (equal (nth 3 mode-line-format) "   ")
3890       (setcar (nthcdr 3 mode-line-format) " "))))
3891
3892 ;;; List and range functions
3893
3894 (defun gnus-last-element (list)
3895   "Return last element of LIST."
3896   (while (cdr list)
3897     (setq list (cdr list)))
3898   (car list))
3899
3900 (defun gnus-copy-sequence (list)
3901   "Do a complete, total copy of a list."
3902   (if (and (consp list) (not (consp (cdr list))))
3903       (cons (car list) (cdr list))
3904     (mapcar (lambda (elem) (if (consp elem)
3905                                (if (consp (cdr elem))
3906                                    (gnus-copy-sequence elem)
3907                                  (cons (car elem) (cdr elem)))
3908                              elem))
3909             list)))
3910
3911 (defun gnus-set-difference (list1 list2)
3912   "Return a list of elements of LIST1 that do not appear in LIST2."
3913   (let ((list1 (copy-sequence list1)))
3914     (while list2
3915       (setq list1 (delq (car list2) list1))
3916       (setq list2 (cdr list2)))
3917     list1))
3918
3919 (defun gnus-sorted-complement (list1 list2)
3920   "Return a list of elements of LIST1 that do not appear in LIST2.
3921 Both lists have to be sorted over <."
3922   (let (out)
3923     (if (or (null list1) (null list2))
3924         (or list1 list2)
3925       (while (and list1 list2)
3926         (cond ((= (car list1) (car list2))
3927                (setq list1 (cdr list1)
3928                      list2 (cdr list2)))
3929               ((< (car list1) (car list2))
3930                (setq out (cons (car list1) out))
3931                (setq list1 (cdr list1)))
3932               (t
3933                (setq out (cons (car list2) out))
3934                (setq list2 (cdr list2)))))
3935       (nconc (nreverse out) (or list1 list2)))))
3936
3937 (defun gnus-intersection (list1 list2)
3938   (let ((result nil))
3939     (while list2
3940       (if (memq (car list2) list1)
3941           (setq result (cons (car list2) result)))
3942       (setq list2 (cdr list2)))
3943     result))
3944
3945 (defun gnus-sorted-intersection (list1 list2)
3946   ;; LIST1 and LIST2 have to be sorted over <.
3947   (let (out)
3948     (while (and list1 list2)
3949       (cond ((= (car list1) (car list2))
3950              (setq out (cons (car list1) out)
3951                    list1 (cdr list1)
3952                    list2 (cdr list2)))
3953             ((< (car list1) (car list2))
3954              (setq list1 (cdr list1)))
3955             (t
3956              (setq list2 (cdr list2)))))
3957     (nreverse out)))
3958
3959 (defun gnus-set-sorted-intersection (list1 list2)
3960   ;; LIST1 and LIST2 have to be sorted over <.
3961   ;; This function modifies LIST1.
3962   (let* ((top (cons nil list1))
3963          (prev top))
3964     (while (and list1 list2)
3965       (cond ((= (car list1) (car list2))
3966              (setq prev list1
3967                    list1 (cdr list1)
3968                    list2 (cdr list2)))
3969             ((< (car list1) (car list2))
3970              (setcdr prev (cdr list1))
3971              (setq list1 (cdr list1)))
3972             (t
3973              (setq list2 (cdr list2)))))
3974     (setcdr prev nil)
3975     (cdr top)))
3976
3977 (defun gnus-compress-sequence (numbers &optional always-list)
3978   "Convert list of numbers to a list of ranges or a single range.
3979 If ALWAYS-LIST is non-nil, this function will always release a list of
3980 ranges."
3981   (let* ((first (car numbers))
3982          (last (car numbers))
3983          result)
3984     (if (null numbers)
3985         nil
3986       (if (not (listp (cdr numbers)))
3987           numbers
3988         (while numbers
3989           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3990                 ((= (1+ last) (car numbers)) ;Still in sequence
3991                  (setq last (car numbers)))
3992                 (t                      ;End of one sequence
3993                  (setq result
3994                        (cons (if (= first last) first
3995                                (cons first last)) result))
3996                  (setq first (car numbers))
3997                  (setq last  (car numbers))))
3998           (setq numbers (cdr numbers)))
3999         (if (and (not always-list) (null result))
4000             (if (= first last) (list first) (cons first last))
4001           (nreverse (cons (if (= first last) first (cons first last))
4002                           result)))))))
4003
4004 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
4005 (defun gnus-uncompress-range (ranges)
4006   "Expand a list of ranges into a list of numbers.
4007 RANGES is either a single range on the form `(num . num)' or a list of
4008 these ranges."
4009   (let (first last result)
4010     (cond
4011      ((null ranges)
4012       nil)
4013      ((not (listp (cdr ranges)))
4014       (setq first (car ranges))
4015       (setq last (cdr ranges))
4016       (while (<= first last)
4017         (setq result (cons first result))
4018         (setq first (1+ first)))
4019       (nreverse result))
4020      (t
4021       (while ranges
4022         (if (atom (car ranges))
4023             (if (numberp (car ranges))
4024                 (setq result (cons (car ranges) result)))
4025           (setq first (caar ranges))
4026           (setq last  (cdar ranges))
4027           (while (<= first last)
4028             (setq result (cons first result))
4029             (setq first (1+ first))))
4030         (setq ranges (cdr ranges)))
4031       (nreverse result)))))
4032
4033 (defun gnus-add-to-range (ranges list)
4034   "Return a list of ranges that has all articles from both RANGES and LIST.
4035 Note: LIST has to be sorted over `<'."
4036   (if (not ranges)
4037       (gnus-compress-sequence list t)
4038     (setq list (copy-sequence list))
4039     (or (listp (cdr ranges))
4040         (setq ranges (list ranges)))
4041     (let ((out ranges)
4042           ilist lowest highest temp)
4043       (while (and ranges list)
4044         (setq ilist list)
4045         (setq lowest (or (and (atom (car ranges)) (car ranges))
4046                          (caar ranges)))
4047         (while (and list (cdr list) (< (cadr list) lowest))
4048           (setq list (cdr list)))
4049         (if (< (car ilist) lowest)
4050             (progn
4051               (setq temp list)
4052               (setq list (cdr list))
4053               (setcdr temp nil)
4054               (setq out (nconc (gnus-compress-sequence ilist t) out))))
4055         (setq highest (or (and (atom (car ranges)) (car ranges))
4056                           (cdar ranges)))
4057         (while (and list (<= (car list) highest))
4058           (setq list (cdr list)))
4059         (setq ranges (cdr ranges)))
4060       (if list
4061           (setq out (nconc (gnus-compress-sequence list t) out)))
4062       (setq out (sort out (lambda (r1 r2)
4063                             (< (or (and (atom r1) r1) (car r1))
4064                                (or (and (atom r2) r2) (car r2))))))
4065       (setq ranges out)
4066       (while ranges
4067         (if (atom (car ranges))
4068             (if (cdr ranges)
4069                 (if (atom (cadr ranges))
4070                     (if (= (1+ (car ranges)) (cadr ranges))
4071                         (progn
4072                           (setcar ranges (cons (car ranges)
4073                                                (cadr ranges)))
4074                           (setcdr ranges (cddr ranges))))
4075                   (if (= (1+ (car ranges)) (caadr ranges))
4076                       (progn
4077                         (setcar (cadr ranges) (car ranges))
4078                         (setcar ranges (cadr ranges))
4079                         (setcdr ranges (cddr ranges))))))
4080           (if (cdr ranges)
4081               (if (atom (cadr ranges))
4082                   (if (= (1+ (cdar ranges)) (cadr ranges))
4083                       (progn
4084                         (setcdr (car ranges) (cadr ranges))
4085                         (setcdr ranges (cddr ranges))))
4086                 (if (= (1+ (cdar ranges)) (caadr ranges))
4087                     (progn
4088                       (setcdr (car ranges) (cdadr ranges))
4089                       (setcdr ranges (cddr ranges)))))))
4090         (setq ranges (cdr ranges)))
4091       out)))
4092
4093 (defun gnus-remove-from-range (ranges list)
4094   "Return a list of ranges that has all articles from LIST removed from RANGES.
4095 Note: LIST has to be sorted over `<'."
4096   ;; !!! This function shouldn't look like this, but I've got a headache.
4097   (gnus-compress-sequence
4098    (gnus-sorted-complement
4099     (gnus-uncompress-range ranges) list)))
4100
4101 (defun gnus-member-of-range (number ranges)
4102   (if (not (listp (cdr ranges)))
4103       (and (>= number (car ranges))
4104            (<= number (cdr ranges)))
4105     (let ((not-stop t))
4106       (while (and ranges
4107                   (if (numberp (car ranges))
4108                       (>= number (car ranges))
4109                     (>= number (caar ranges)))
4110                   not-stop)
4111         (if (if (numberp (car ranges))
4112                 (= number (car ranges))
4113               (and (>= number (caar ranges))
4114                    (<= number (cdar ranges))))
4115             (setq not-stop nil))
4116         (setq ranges (cdr ranges)))
4117       (not not-stop))))
4118
4119 (defun gnus-range-length (range)
4120   "Return the length RANGE would have if uncompressed."
4121   (length (gnus-uncompress-range range)))
4122
4123 (defun gnus-sublist-p (list sublist)
4124   "Test whether all elements in SUBLIST are members of LIST."
4125   (let ((sublistp t))
4126     (while sublist
4127       (unless (memq (pop sublist) list)
4128         (setq sublistp nil
4129               sublist nil)))
4130     sublistp))
4131
4132 \f
4133 ;;;
4134 ;;; Gnus group mode
4135 ;;;
4136
4137 (defvar gnus-group-mode-map nil)
4138 (put 'gnus-group-mode 'mode-class 'special)
4139
4140 (unless gnus-group-mode-map
4141   (setq gnus-group-mode-map (make-keymap))
4142   (suppress-keymap gnus-group-mode-map)
4143
4144   (gnus-define-keys gnus-group-mode-map
4145     " " gnus-group-read-group
4146     "=" gnus-group-select-group
4147     "\r" gnus-group-select-group
4148     "\M-\r" gnus-group-quick-select-group
4149     "j" gnus-group-jump-to-group
4150     "n" gnus-group-next-unread-group
4151     "p" gnus-group-prev-unread-group
4152     "\177" gnus-group-prev-unread-group
4153     [delete] gnus-group-prev-unread-group
4154     "N" gnus-group-next-group
4155     "P" gnus-group-prev-group
4156     "\M-n" gnus-group-next-unread-group-same-level
4157     "\M-p" gnus-group-prev-unread-group-same-level
4158     "," gnus-group-best-unread-group
4159     "." gnus-group-first-unread-group
4160     "u" gnus-group-unsubscribe-current-group
4161     "U" gnus-group-unsubscribe-group
4162     "c" gnus-group-catchup-current
4163     "C" gnus-group-catchup-current-all
4164     "l" gnus-group-list-groups
4165     "L" gnus-group-list-all-groups
4166     "m" gnus-group-mail
4167     "g" gnus-group-get-new-news
4168     "\M-g" gnus-group-get-new-news-this-group
4169     "R" gnus-group-restart
4170     "r" gnus-group-read-init-file
4171     "B" gnus-group-browse-foreign-server
4172     "b" gnus-group-check-bogus-groups
4173     "F" gnus-find-new-newsgroups
4174     "\C-c\C-d" gnus-group-describe-group
4175     "\M-d" gnus-group-describe-all-groups
4176     "\C-c\C-a" gnus-group-apropos
4177     "\C-c\M-\C-a" gnus-group-description-apropos
4178     "a" gnus-group-post-news
4179     "\ek" gnus-group-edit-local-kill
4180     "\eK" gnus-group-edit-global-kill
4181     "\C-k" gnus-group-kill-group
4182     "\C-y" gnus-group-yank-group
4183     "\C-w" gnus-group-kill-region
4184     "\C-x\C-t" gnus-group-transpose-groups
4185     "\C-c\C-l" gnus-group-list-killed
4186     "\C-c\C-x" gnus-group-expire-articles
4187     "\C-c\M-\C-x" gnus-group-expire-all-groups
4188     "V" gnus-version
4189     "s" gnus-group-save-newsrc
4190     "z" gnus-group-suspend
4191     "Z" gnus-group-clear-dribble
4192     "q" gnus-group-exit
4193     "Q" gnus-group-quit
4194     "?" gnus-group-describe-briefly
4195     "\C-c\C-i" gnus-info-find-node
4196     "\M-e" gnus-group-edit-group-method
4197     "^" gnus-group-enter-server-mode
4198     gnus-mouse-2 gnus-mouse-pick-group
4199     "<" beginning-of-buffer
4200     ">" end-of-buffer
4201     "\C-c\C-b" gnus-bug
4202     "\C-c\C-s" gnus-group-sort-groups
4203     "t" gnus-topic-mode
4204     "\C-c\M-g" gnus-activate-all-groups
4205     "\M-&" gnus-group-universal-argument
4206     "#" gnus-group-mark-group
4207     "\M-#" gnus-group-unmark-group)
4208
4209   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4210     "m" gnus-group-mark-group
4211     "u" gnus-group-unmark-group
4212     "w" gnus-group-mark-region
4213     "m" gnus-group-mark-buffer
4214     "r" gnus-group-mark-regexp
4215     "U" gnus-group-unmark-all-groups)
4216
4217   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4218     "d" gnus-group-make-directory-group
4219     "h" gnus-group-make-help-group
4220     "a" gnus-group-make-archive-group
4221     "k" gnus-group-make-kiboze-group
4222     "m" gnus-group-make-group
4223     "E" gnus-group-edit-group
4224     "e" gnus-group-edit-group-method
4225     "p" gnus-group-edit-group-parameters
4226     "v" gnus-group-add-to-virtual
4227     "V" gnus-group-make-empty-virtual
4228     "D" gnus-group-enter-directory
4229     "f" gnus-group-make-doc-group
4230     "r" gnus-group-rename-group
4231     "\177" gnus-group-delete-group
4232     [delete] gnus-group-delete-group)
4233
4234    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4235      "b" gnus-group-brew-soup
4236      "w" gnus-soup-save-areas
4237      "s" gnus-soup-send-replies
4238      "p" gnus-soup-pack-packet
4239      "r" nnsoup-pack-replies)
4240
4241    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4242      "s" gnus-group-sort-groups
4243      "a" gnus-group-sort-groups-by-alphabet
4244      "u" gnus-group-sort-groups-by-unread
4245      "l" gnus-group-sort-groups-by-level
4246      "v" gnus-group-sort-groups-by-score
4247      "r" gnus-group-sort-groups-by-rank
4248      "m" gnus-group-sort-groups-by-method)
4249
4250    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4251      "k" gnus-group-list-killed
4252      "z" gnus-group-list-zombies
4253      "s" gnus-group-list-groups
4254      "u" gnus-group-list-all-groups
4255      "A" gnus-group-list-active
4256      "a" gnus-group-apropos
4257      "d" gnus-group-description-apropos
4258      "m" gnus-group-list-matching
4259      "M" gnus-group-list-all-matching
4260      "l" gnus-group-list-level)
4261
4262    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4263      "f" gnus-score-flush-cache)
4264
4265    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4266      "f" gnus-group-fetch-faq)
4267
4268    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4269      "l" gnus-group-set-current-level
4270      "t" gnus-group-unsubscribe-current-group
4271      "s" gnus-group-unsubscribe-group
4272      "k" gnus-group-kill-group
4273      "y" gnus-group-yank-group
4274      "w" gnus-group-kill-region
4275      "\C-k" gnus-group-kill-level
4276      "z" gnus-group-kill-all-zombies))
4277
4278 (defun gnus-group-mode ()
4279   "Major mode for reading news.
4280
4281 All normal editing commands are switched off.
4282 \\<gnus-group-mode-map>
4283 The group buffer lists (some of) the groups available.  For instance,
4284 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4285 lists all zombie groups.
4286
4287 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4288 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4289
4290 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4291
4292 The following commands are available:
4293
4294 \\{gnus-group-mode-map}"
4295   (interactive)
4296   (when (and menu-bar-mode
4297              (gnus-visual-p 'group-menu 'menu))
4298     (gnus-group-make-menu-bar))
4299   (kill-all-local-variables)
4300   (gnus-simplify-mode-line)
4301   (setq major-mode 'gnus-group-mode)
4302   (setq mode-name "Group")
4303   (gnus-group-set-mode-line)
4304   (setq mode-line-process nil)
4305   (use-local-map gnus-group-mode-map)
4306   (buffer-disable-undo (current-buffer))
4307   (setq truncate-lines t)
4308   (setq buffer-read-only t)
4309   (gnus-make-local-hook 'post-command-hook)
4310   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
4311   (run-hooks 'gnus-group-mode-hook))
4312
4313 (defun gnus-clear-inboxes-moved ()
4314   (setq nnmail-moved-inboxes nil))
4315
4316 (defun gnus-mouse-pick-group (e)
4317   "Enter the group under the mouse pointer."
4318   (interactive "e")
4319   (mouse-set-point e)
4320   (gnus-group-read-group nil))
4321
4322 ;; Look at LEVEL and find out what the level is really supposed to be.
4323 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4324 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4325 (defun gnus-group-default-level (&optional level number-or-nil)
4326   (cond
4327    (gnus-group-use-permanent-levels
4328     (or (setq gnus-group-use-permanent-levels
4329               (or level (if (numberp gnus-group-use-permanent-levels)
4330                             gnus-group-use-permanent-levels
4331                           (or gnus-group-default-list-level
4332                               gnus-level-subscribed))))
4333         gnus-group-default-list-level gnus-level-subscribed))
4334    (number-or-nil
4335     level)
4336    (t
4337     (or level gnus-group-default-list-level gnus-level-subscribed))))
4338
4339 ;;;###autoload
4340 (defun gnus-slave-no-server (&optional arg)
4341   "Read network news as a slave, without connecting to local server"
4342   (interactive "P")
4343   (gnus-no-server arg t))
4344
4345 ;;;###autoload
4346 (defun gnus-no-server (&optional arg slave)
4347   "Read network news.
4348 If ARG is a positive number, Gnus will use that as the
4349 startup level.  If ARG is nil, Gnus will be started at level 2.
4350 If ARG is non-nil and not a positive number, Gnus will
4351 prompt the user for the name of an NNTP server to use.
4352 As opposed to `gnus', this command will not connect to the local server."
4353   (interactive "P")
4354   (let ((val (or arg (1- gnus-level-default-subscribed))))
4355     (gnus val t slave)
4356     (make-local-variable 'gnus-group-use-permanent-levels)
4357     (setq gnus-group-use-permanent-levels val)))
4358
4359 ;;;###autoload
4360 (defun gnus-slave (&optional arg)
4361   "Read news as a slave."
4362   (interactive "P")
4363   (gnus arg nil 'slave))
4364
4365 ;;;###autoload
4366 (defun gnus-other-frame (&optional arg)
4367   "Pop up a frame to read news."
4368   (interactive "P")
4369   (if (get-buffer gnus-group-buffer)
4370       (let ((pop-up-frames t))
4371         (gnus arg))
4372     (select-frame (make-frame))
4373     (gnus arg)))
4374
4375 ;;;###autoload
4376 (defun gnus (&optional arg dont-connect slave)
4377   "Read network news.
4378 If ARG is non-nil and a positive number, Gnus will use that as the
4379 startup level.  If ARG is non-nil and not a positive number, Gnus will
4380 prompt the user for the name of an NNTP server to use."
4381   (interactive "P")
4382
4383   (if (get-buffer gnus-group-buffer)
4384       (progn
4385         (switch-to-buffer gnus-group-buffer)
4386         (gnus-group-get-new-news))
4387
4388     (gnus-clear-system)
4389     (nnheader-init-server-buffer)
4390     (gnus-read-init-file)
4391     (setq gnus-slave slave)
4392
4393     (gnus-group-setup-buffer)
4394     (let ((buffer-read-only nil))
4395       (erase-buffer)
4396       (if (not gnus-inhibit-startup-message)
4397           (progn
4398             (gnus-group-startup-message)
4399             (sit-for 0))))
4400
4401     (let ((level (and (numberp arg) (> arg 0) arg))
4402           did-connect)
4403       (unwind-protect
4404           (progn
4405             (or dont-connect
4406                 (setq did-connect
4407                       (gnus-start-news-server (and arg (not level))))))
4408         (if (and (not dont-connect)
4409                  (not did-connect))
4410             (gnus-group-quit)
4411           (run-hooks 'gnus-startup-hook)
4412           ;; NNTP server is successfully open.
4413
4414           ;; Find the current startup file name.
4415           (setq gnus-current-startup-file
4416                 (gnus-make-newsrc-file gnus-startup-file))
4417
4418           ;; Read the dribble file.
4419           (when (or gnus-slave gnus-use-dribble-file)
4420             (gnus-dribble-read-file))
4421
4422           ;; Allow using GroupLens predictions.
4423           (when gnus-use-grouplens
4424             (bbb-login)
4425             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4426
4427           (gnus-summary-make-display-table)
4428           ;; Do the actual startup.
4429           (gnus-setup-news nil level dont-connect)
4430           ;; Generate the group buffer.
4431           (gnus-group-list-groups level)
4432           (gnus-group-first-unread-group)
4433           (gnus-configure-windows 'group)
4434           (gnus-group-set-mode-line))))))
4435
4436 (defun gnus-unload ()
4437   "Unload all Gnus features."
4438   (interactive)
4439   (or (boundp 'load-history)
4440       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4441   (let ((history load-history)
4442         feature)
4443     (while history
4444       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4445            (setq feature (cdr (assq 'provide (car history))))
4446            (unload-feature feature 'force))
4447       (setq history (cdr history)))))
4448
4449 (defun gnus-compile ()
4450   "Byte-compile the user-defined format specs."
4451   (interactive)
4452   (let ((entries gnus-format-specs)
4453         entry gnus-tmp-func)
4454     (save-excursion
4455       (gnus-message 7 "Compiling format specs...")
4456
4457       (while entries
4458         (setq entry (pop entries))
4459         (if (eq (car entry) 'version)
4460             (setq gnus-format-specs (delq entry gnus-format-specs))
4461           (when (and (listp (caddr entry))
4462                      (not (eq 'byte-code (caaddr entry))))
4463             (fset 'gnus-tmp-func
4464                   `(lambda () ,(caddr entry)))
4465             (byte-compile 'gnus-tmp-func)
4466             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4467
4468       (push (cons 'version emacs-version) gnus-format-specs)
4469
4470       (gnus-message 7 "Compiling user specs...done"))))
4471
4472 (defun gnus-indent-rigidly (start end arg)
4473   "Indent rigidly using only spaces and no tabs."
4474   (save-excursion
4475     (save-restriction
4476       (narrow-to-region start end)
4477       (indent-rigidly start end arg)
4478       (goto-char (point-min))
4479       (while (search-forward "\t" nil t)
4480         (replace-match "        " t t)))))
4481
4482 (defun gnus-group-startup-message (&optional x y)
4483   "Insert startup message in current buffer."
4484   ;; Insert the message.
4485   (erase-buffer)
4486   (insert
4487    (format "              %s
4488           _    ___ _             _
4489           _ ___ __ ___  __    _ ___
4490           __   _     ___    __  ___
4491               _           ___     _
4492              _  _ __             _
4493              ___   __            _
4494                    __           _
4495                     _      _   _
4496                    _      _    _
4497                       _  _    _
4498                   __  ___
4499                  _   _ _     _
4500                 _   _
4501               _    _
4502              _    _
4503             _
4504           __
4505
4506 "
4507            ""))
4508   ;; And then hack it.
4509   (gnus-indent-rigidly (point-min) (point-max)
4510                        (/ (max (- (window-width) (or x 46)) 0) 2))
4511   (goto-char (point-min))
4512   (forward-line 1)
4513   (let* ((pheight (count-lines (point-min) (point-max)))
4514          (wheight (window-height))
4515          (rest (- wheight pheight)))
4516     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4517   ;; Fontify some.
4518   (goto-char (point-min))
4519   (and (search-forward "Praxis" nil t)
4520        (gnus-put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4521   (goto-char (point-min))
4522   (let* ((mode-string (gnus-group-set-mode-line)))
4523     (setq mode-line-buffer-identification
4524           (list (concat gnus-version (substring (car mode-string) 4))))
4525     (set-buffer-modified-p t)))
4526
4527 (defun gnus-group-setup-buffer ()
4528   (or (get-buffer gnus-group-buffer)
4529       (progn
4530         (switch-to-buffer gnus-group-buffer)
4531         (gnus-add-current-to-buffer-list)
4532         (gnus-group-mode)
4533         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4534
4535 (defun gnus-group-list-groups (&optional level unread lowest)
4536   "List newsgroups with level LEVEL or lower that have unread articles.
4537 Default is all subscribed groups.
4538 If argument UNREAD is non-nil, groups with no unread articles are also
4539 listed."
4540   (interactive (list (if current-prefix-arg
4541                          (prefix-numeric-value current-prefix-arg)
4542                        (or
4543                         (gnus-group-default-level nil t)
4544                         gnus-group-default-list-level
4545                         gnus-level-subscribed))))
4546   (or level
4547       (setq level (car gnus-group-list-mode)
4548             unread (cdr gnus-group-list-mode)))
4549   (setq level (gnus-group-default-level level))
4550   (gnus-group-setup-buffer)             ;May call from out of group buffer
4551   (gnus-update-format-specifications)
4552   (let ((case-fold-search nil)
4553         (props (text-properties-at (gnus-point-at-bol)))
4554         (group (gnus-group-group-name)))
4555     (set-buffer gnus-group-buffer)
4556     (funcall gnus-group-prepare-function level unread lowest)
4557     (if (zerop (buffer-size))
4558         (gnus-message 5 gnus-no-groups-message)
4559       (goto-char (point-max))
4560       (when (or (not gnus-group-goto-next-group-function)
4561                 (not (funcall gnus-group-goto-next-group-function 
4562                               group props)))
4563         (if (not group)
4564             ;; Go to the first group with unread articles.
4565             (gnus-group-search-forward t)
4566           ;; Find the right group to put point on.  If the current group
4567           ;; has disappeared in the new listing, try to find the next
4568           ;; one.        If no next one can be found, just leave point at the
4569           ;; first newsgroup in the buffer.
4570           (if (not (gnus-goto-char
4571                     (text-property-any
4572                      (point-min) (point-max)
4573                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4574               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4575                 (while (and newsrc
4576                             (not (gnus-goto-char
4577                                   (text-property-any
4578                                    (point-min) (point-max) 'gnus-group
4579                                    (gnus-intern-safe
4580                                     (caar newsrc) gnus-active-hashtb)))))
4581                   (setq newsrc (cdr newsrc)))
4582                 (or newsrc (progn (goto-char (point-max))
4583                                   (forward-line -1)))))))
4584       ;; Adjust cursor point.
4585       (gnus-group-position-point))))
4586
4587 (defun gnus-group-list-level (level &optional all)
4588   "List groups on LEVEL.
4589 If ALL (the prefix), also list groups that have no unread articles."
4590   (interactive "nList groups on level: \nP")
4591   (gnus-group-list-groups level all level))
4592
4593 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4594   "List all newsgroups with unread articles of level LEVEL or lower.
4595 If ALL is non-nil, list groups that have no unread articles.
4596 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4597 If REGEXP, only list groups matching REGEXP."
4598   (set-buffer gnus-group-buffer)
4599   (let ((buffer-read-only nil)
4600         (newsrc (cdr gnus-newsrc-alist))
4601         (lowest (or lowest 1))
4602         info clevel unread group params)
4603     (erase-buffer)
4604     (if (< lowest gnus-level-zombie)
4605         ;; List living groups.
4606         (while newsrc
4607           (setq info (car newsrc)
4608                 group (gnus-info-group info)
4609                 params (gnus-info-params info)
4610                 newsrc (cdr newsrc)
4611                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4612           (and unread                   ; This group might be bogus
4613                (or (not regexp)
4614                    (string-match regexp group))
4615                (<= (setq clevel (gnus-info-level info)) level)
4616                (>= clevel lowest)
4617                (or all                  ; We list all groups?
4618                    (if (eq unread t)    ; Unactivated?
4619                        gnus-group-list-inactive-groups ; We list unactivated 
4620                      (> unread 0))      ; We list groups with unread articles
4621                    (and gnus-list-groups-with-ticked-articles
4622                         (cdr (assq 'tick (gnus-info-marks info))))
4623                                         ; And groups with tickeds
4624                    ;; Check for permanent visibility.
4625                    (and gnus-permanently-visible-groups
4626                         (string-match gnus-permanently-visible-groups
4627                                       group))
4628                    (memq 'visible params)
4629                    (cdr (assq 'visible params)))
4630                (gnus-group-insert-group-line
4631                 group (gnus-info-level info)
4632                 (gnus-info-marks info) unread (gnus-info-method info)))))
4633
4634     ;; List dead groups.
4635     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4636          (gnus-group-prepare-flat-list-dead
4637           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4638           gnus-level-zombie ?Z
4639           regexp))
4640     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4641          (gnus-group-prepare-flat-list-dead
4642           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4643           gnus-level-killed ?K regexp))
4644
4645     (gnus-group-set-mode-line)
4646     (setq gnus-group-list-mode (cons level all))
4647     (run-hooks 'gnus-group-prepare-hook)))
4648
4649 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4650   ;; List zombies and killed lists somewhat faster, which was
4651   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4652   ;; this by ignoring the group format specification altogether.
4653   (let (group)
4654     (if regexp
4655         ;; This loop is used when listing groups that match some
4656         ;; regexp.
4657         (while groups
4658           (setq group (pop groups))
4659           (when (string-match regexp group)
4660             (gnus-add-text-properties
4661              (point) (prog1 (1+ (point))
4662                        (insert " " mark "     *: " group "\n"))
4663              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4664                    'gnus-unread t
4665                    'gnus-level level))))
4666       ;; This loop is used when listing all groups.
4667       (while groups
4668         (gnus-add-text-properties
4669          (point) (prog1 (1+ (point))
4670                    (insert " " mark "     *: "
4671                            (setq group (pop groups)) "\n"))
4672          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4673                'gnus-unread t
4674                'gnus-level level))))))
4675
4676 (defmacro gnus-group-real-name (group)
4677   "Find the real name of a foreign newsgroup."
4678   `(let ((gname ,group))
4679      (if (string-match ":[^:]+$" gname)
4680          (substring gname (1+ (match-beginning 0)))
4681        gname)))
4682
4683 (defsubst gnus-server-add-address (method)
4684   (let ((method-name (symbol-name (car method))))
4685     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4686              (not (assq (intern (concat method-name "-address")) method)))
4687         (append method (list (list (intern (concat method-name "-address"))
4688                                    (nth 1 method))))
4689       method)))
4690
4691 (defsubst gnus-server-get-method (group method)
4692   ;; Input either a server name, and extended server name, or a
4693   ;; select method, and return a select method.
4694   (cond ((stringp method)
4695          (gnus-server-to-method method))
4696         ((equal method gnus-select-method)
4697          gnus-select-method)
4698         ((and (stringp (car method)) group)
4699          (gnus-server-extend-method group method))
4700         ((and method (not group)
4701               (equal (cadr method) ""))
4702          method)
4703         (t
4704          (gnus-server-add-address method))))
4705
4706 (defun gnus-server-to-method (server)
4707   "Map virtual server names to select methods."
4708   (or 
4709    ;; Is this a method, perhaps?
4710    (and server (listp server) server)
4711    ;; Perhaps this is the native server?
4712    (and (equal server "native") gnus-select-method)
4713    ;; It should be in the server alist.
4714    (cdr (assoc server gnus-server-alist))
4715    ;; If not, we look through all the opened server
4716    ;; to see whether we can find it there.
4717    (let ((opened gnus-opened-servers))
4718      (while (and opened
4719                  (not (equal server (format "%s:%s" (caaar opened)
4720                                             (cadaar opened)))))
4721        (pop opened))
4722      (caar opened))))
4723
4724 (defmacro gnus-method-equal (ss1 ss2)
4725   "Say whether two servers are equal."
4726   `(let ((s1 ,ss1)
4727          (s2 ,ss2))
4728      (or (equal s1 s2)
4729          (and (= (length s1) (length s2))
4730               (progn
4731                 (while (and s1 (member (car s1) s2))
4732                   (setq s1 (cdr s1)))
4733                 (null s1))))))
4734
4735 (defun gnus-server-equal (m1 m2)
4736   "Say whether two methods are equal."
4737   (let ((m1 (cond ((null m1) gnus-select-method)
4738                   ((stringp m1) (gnus-server-to-method m1))
4739                   (t m1)))
4740         (m2 (cond ((null m2) gnus-select-method)
4741                   ((stringp m2) (gnus-server-to-method m2))
4742                   (t m2))))
4743     (gnus-method-equal m1 m2)))
4744
4745 (defun gnus-servers-using-backend (backend)
4746   "Return a list of known servers using BACKEND."
4747   (let ((opened gnus-opened-servers)
4748         out)
4749     (while opened
4750       (when (eq backend (caaar opened))
4751         (push (caar opened) out))
4752       (pop opened))
4753     out))
4754
4755 (defun gnus-group-prefixed-name (group method)
4756   "Return the whole name from GROUP and METHOD."
4757   (and (stringp method) (setq method (gnus-server-to-method method)))
4758   (concat (format "%s" (car method))
4759           (if (and
4760                (or (assoc (format "%s" (car method)) 
4761                           (gnus-methods-using 'address))
4762                    (gnus-server-equal method gnus-message-archive-method))
4763                (nth 1 method)
4764                (not (string= (nth 1 method) "")))
4765               (concat "+" (nth 1 method)))
4766           ":" group))
4767
4768 (defun gnus-group-real-prefix (group)
4769   "Return the prefix of the current group name."
4770   (if (string-match "^[^:]+:" group)
4771       (substring group 0 (match-end 0))
4772     ""))
4773
4774 (defun gnus-group-method (group)
4775   "Return the server or method used for selecting GROUP."
4776   (let ((prefix (gnus-group-real-prefix group)))
4777     (if (equal prefix "")
4778         gnus-select-method
4779       (let ((servers gnus-opened-servers)
4780             (server "")
4781             backend possible found)
4782         (if (string-match "^[^\\+]+\\+" prefix)
4783             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4784                   server (substring prefix (match-end 0) (1- (length prefix))))
4785           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4786         (while servers
4787           (when (eq (caaar servers) backend)
4788             (setq possible (caar servers))
4789             (when (equal (cadaar servers) server)
4790               (setq found (caar servers))))
4791           (pop servers))
4792         (or (car (rassoc found gnus-server-alist))
4793             found
4794             (car (rassoc possible gnus-server-alist))
4795             possible
4796             (list backend server))))))
4797
4798 (defsubst gnus-secondary-method-p (method)
4799   "Return whether METHOD is a secondary select method."
4800   (let ((methods gnus-secondary-select-methods)
4801         (gmethod (gnus-server-get-method nil method)))
4802     (while (and methods
4803                 (not (equal (gnus-server-get-method nil (car methods))
4804                             gmethod)))
4805       (setq methods (cdr methods)))
4806     methods))
4807
4808 (defun gnus-group-foreign-p (group)
4809   "Say whether a group is foreign or not."
4810   (and (not (gnus-group-native-p group))
4811        (not (gnus-group-secondary-p group))))
4812
4813 (defun gnus-group-native-p (group)
4814   "Say whether the group is native or not."
4815   (not (string-match ":" group)))
4816
4817 (defun gnus-group-secondary-p (group)
4818   "Say whether the group is secondary or not."
4819   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4820
4821 (defun gnus-group-get-parameter (group &optional symbol)
4822   "Returns the group parameters for GROUP.
4823 If SYMBOL, return the value of that symbol in the group parameters."
4824   (let ((params (gnus-info-params (gnus-get-info group))))
4825     (if symbol
4826         (gnus-group-parameter-value params symbol)
4827       params)))
4828
4829 (defun gnus-group-parameter-value (params symbol)
4830   "Return the value of SYMBOL in group PARAMS."
4831   (or (car (memq symbol params))        ; It's either a simple symbol
4832       (cdr (assq symbol params))))      ; or a cons.
4833
4834 (defun gnus-group-add-parameter (group param)
4835   "Add parameter PARAM to GROUP."
4836   (let ((info (gnus-get-info group)))
4837     (if (not info)
4838         () ; This is a dead group.  We just ignore it.
4839       ;; Cons the new param to the old one and update.
4840       (gnus-group-set-info (cons param (gnus-info-params info))
4841                            group 'params))))
4842
4843 (defun gnus-group-set-parameter (group name value)
4844   "Set parameter NAME to VALUE in GROUP."
4845   (let ((info (gnus-get-info group)))
4846     (if (not info)
4847         () ; This is a dead group.  We just ignore it.
4848       (let ((old-params (gnus-info-params info))
4849             (new-params (list (cons name value))))
4850         (while old-params
4851           (if (or (not (listp (car old-params)))
4852                   (not (eq (caar old-params) name)))
4853               (setq new-params (append new-params (list (car old-params)))))
4854           (setq old-params (cdr old-params)))
4855         (gnus-group-set-info new-params group 'params)))))
4856
4857 (defun gnus-group-add-score (group &optional score)
4858   "Add SCORE to the GROUP score.
4859 If SCORE is nil, add 1 to the score of GROUP."
4860   (let ((info (gnus-get-info group)))
4861     (when info
4862       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4863
4864 (defun gnus-summary-bubble-group ()
4865   "Increase the score of the current group.
4866 This is a handy function to add to `gnus-summary-exit-hook' to
4867 increase the score of each group you read."
4868   (gnus-group-add-score gnus-newsgroup-name))
4869
4870 (defun gnus-group-set-info (info &optional method-only-group part)
4871   (let* ((entry (gnus-gethash
4872                  (or method-only-group (gnus-info-group info))
4873                  gnus-newsrc-hashtb))
4874          (part-info info)
4875          (info (if method-only-group (nth 2 entry) info))
4876          method)
4877     (when method-only-group
4878       (unless entry
4879         (error "Trying to change non-existent group %s" method-only-group))
4880       ;; We have received parts of the actual group info - either the
4881       ;; select method or the group parameters.  We first check
4882       ;; whether we have to extend the info, and if so, do that.
4883       (let ((len (length info))
4884             (total (if (eq part 'method) 5 6)))
4885         (when (< len total)
4886           (setcdr (nthcdr (1- len) info)
4887                   (make-list (- total len) nil)))
4888         ;; Then we enter the new info.
4889         (setcar (nthcdr (1- total) info) part-info)))
4890     (unless entry
4891       ;; This is a new group, so we just create it.
4892       (save-excursion
4893         (set-buffer gnus-group-buffer)
4894         (setq method (gnus-info-method info))
4895         (when (gnus-server-equal method "native")
4896           (setq method nil))
4897         (save-excursion
4898           (set-buffer gnus-group-buffer)
4899           (if method
4900               ;; It's a foreign group...
4901               (gnus-group-make-group
4902                (gnus-group-real-name (gnus-info-group info))
4903                (if (stringp method) method
4904                  (prin1-to-string (car method)))
4905                (and (consp method)
4906                     (nth 1 (gnus-info-method info))))
4907             ;; It's a native group.
4908             (gnus-group-make-group (gnus-info-group info))))
4909         (gnus-message 6 "Note: New group created")
4910         (setq entry
4911               (gnus-gethash (gnus-group-prefixed-name
4912                              (gnus-group-real-name (gnus-info-group info))
4913                              (or (gnus-info-method info) gnus-select-method))
4914                             gnus-newsrc-hashtb))))
4915     ;; Whether it was a new group or not, we now have the entry, so we
4916     ;; can do the update.
4917     (if entry
4918         (progn
4919           (setcar (nthcdr 2 entry) info)
4920           (when (and (not (eq (car entry) t))
4921                      (gnus-active (gnus-info-group info)))
4922             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4923       (error "No such group: %s" (gnus-info-group info)))))
4924
4925 (defun gnus-group-set-method-info (group select-method)
4926   (gnus-group-set-info select-method group 'method))
4927
4928 (defun gnus-group-set-params-info (group params)
4929   (gnus-group-set-info params group 'params))
4930
4931 (defun gnus-group-update-group-line ()
4932   "Update the current line in the group buffer."
4933   (let* ((buffer-read-only nil)
4934          (group (gnus-group-group-name))
4935          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
4936          gnus-group-indentation)
4937     (when group
4938       (and entry
4939            (not (gnus-ephemeral-group-p group))
4940            (gnus-dribble-enter
4941             (concat "(gnus-group-set-info '"
4942                     (prin1-to-string (nth 2 entry)) ")")))
4943       (setq gnus-group-indentation (gnus-group-group-indentation))
4944       (gnus-delete-line)
4945       (gnus-group-insert-group-line-info group)
4946       (forward-line -1)
4947       (gnus-group-position-point))))
4948
4949 (defun gnus-group-insert-group-line-info (group)
4950   "Insert GROUP on the current line."
4951   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4952         active info)
4953     (if entry
4954         (progn
4955           ;; (Un)subscribed group.
4956           (setq info (nth 2 entry))
4957           (gnus-group-insert-group-line
4958            group (gnus-info-level info) (gnus-info-marks info)
4959            (or (car entry) t) (gnus-info-method info)))
4960       ;; This group is dead.
4961       (gnus-group-insert-group-line
4962        group
4963        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4964        nil
4965        (if (setq active (gnus-active group))
4966            (- (1+ (cdr active)) (car active)) 0)
4967        nil))))
4968
4969 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4970                                                     gnus-tmp-marked number
4971                                                     gnus-tmp-method)
4972   "Insert a group line in the group buffer."
4973   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4974          (gnus-tmp-number-total
4975           (if gnus-tmp-active
4976               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4977             0))
4978          (gnus-tmp-number-of-unread
4979           (if (numberp number) (int-to-string (max 0 number))
4980             "*"))
4981          (gnus-tmp-number-of-read
4982           (if (numberp number)
4983               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4984             "*"))
4985          (gnus-tmp-subscribed
4986           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4987                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4988                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4989                 (t ?K)))
4990          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4991          (gnus-tmp-newsgroup-description
4992           (if gnus-description-hashtb
4993               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4994             ""))
4995          (gnus-tmp-moderated
4996           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4997          (gnus-tmp-moderated-string
4998           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4999          (gnus-tmp-method
5000           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
5001          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
5002          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
5003          (gnus-tmp-news-method-string
5004           (if gnus-tmp-method
5005               (format "(%s:%s)" (car gnus-tmp-method)
5006                       (cadr gnus-tmp-method)) ""))
5007          (gnus-tmp-marked-mark
5008           (if (and (numberp number)
5009                    (zerop number)
5010                    (cdr (assq 'tick gnus-tmp-marked)))
5011               ?* ? ))
5012          (gnus-tmp-process-marked
5013           (if (member gnus-tmp-group gnus-group-marked)
5014               gnus-process-mark ? ))
5015          (gnus-tmp-grouplens
5016           (or (and gnus-use-grouplens
5017                    (bbb-grouplens-group-p gnus-tmp-group))
5018               ""))
5019          (buffer-read-only nil)
5020          header gnus-tmp-header)        ; passed as parameter to user-funcs.
5021     (beginning-of-line)
5022     (gnus-add-text-properties
5023      (point)
5024      (prog1 (1+ (point))
5025        ;; Insert the text.
5026        (eval gnus-group-line-format-spec))
5027      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
5028        gnus-unread ,(if (numberp number)
5029                         (string-to-int gnus-tmp-number-of-unread)
5030                       t)
5031        gnus-marked ,gnus-tmp-marked-mark
5032        gnus-indentation ,gnus-group-indentation
5033        gnus-level ,gnus-tmp-level))
5034     (when (inline (gnus-visual-p 'group-highlight 'highlight))
5035       (forward-line -1)
5036       (run-hooks 'gnus-group-update-hook)
5037       (forward-line))
5038     ;; Allow XEmacs to remove front-sticky text properties.
5039     (gnus-group-remove-excess-properties)))
5040
5041 (defun gnus-group-update-group (group &optional visible-only)
5042   "Update all lines where GROUP appear.
5043 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
5044 already."
5045   (save-excursion
5046     (set-buffer gnus-group-buffer)
5047     ;; The buffer may be narrowed.
5048     (save-restriction
5049       (widen)
5050       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
5051             (loc (point-min))
5052             found buffer-read-only)
5053         ;; Enter the current status into the dribble buffer.
5054         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
5055           (if (and entry (not (gnus-ephemeral-group-p group)))
5056               (gnus-dribble-enter
5057                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
5058                        ")"))))
5059         ;; Find all group instances.  If topics are in use, each group
5060         ;; may be listed in more than once.
5061         (while (setq loc (text-property-any
5062                           loc (point-max) 'gnus-group ident))
5063           (setq found t)
5064           (goto-char loc)
5065           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5066             (gnus-delete-line)
5067             (gnus-group-insert-group-line-info group)
5068             (save-excursion
5069               (forward-line -1)
5070               (run-hooks 'gnus-group-update-group-hook)))
5071           (setq loc (1+ loc)))
5072         (unless (or found visible-only)
5073           ;; No such line in the buffer, find out where it's supposed to
5074           ;; go, and insert it there (or at the end of the buffer).
5075           (if gnus-goto-missing-group-function
5076               (funcall gnus-goto-missing-group-function group)
5077             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
5078               (while (and entry (car entry)
5079                           (not
5080                            (gnus-goto-char
5081                             (text-property-any
5082                              (point-min) (point-max)
5083                              'gnus-group (gnus-intern-safe
5084                                           (caar entry) gnus-active-hashtb)))))
5085                 (setq entry (cdr entry)))
5086               (or entry (goto-char (point-max)))))
5087           ;; Finally insert the line.
5088           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5089             (gnus-group-insert-group-line-info group)
5090             (save-excursion
5091               (forward-line -1)
5092               (run-hooks 'gnus-group-update-group-hook))))
5093         (gnus-group-set-mode-line)))))
5094
5095 (defun gnus-group-set-mode-line ()
5096   "Update the mode line in the group buffer."
5097   (when (memq 'group gnus-updated-mode-lines)
5098     ;; Yes, we want to keep this mode line updated.
5099     (save-excursion
5100       (set-buffer gnus-group-buffer)
5101       (let* ((gformat (or gnus-group-mode-line-format-spec
5102                           (setq gnus-group-mode-line-format-spec
5103                                 (gnus-parse-format
5104                                  gnus-group-mode-line-format
5105                                  gnus-group-mode-line-format-alist))))
5106              (gnus-tmp-news-server (cadr gnus-select-method))
5107              (gnus-tmp-news-method (car gnus-select-method))
5108              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
5109              (max-len 60)
5110              gnus-tmp-header            ;Dummy binding for user-defined formats
5111              ;; Get the resulting string.
5112              (modified 
5113               (and gnus-dribble-buffer
5114                    (buffer-name gnus-dribble-buffer)
5115                    (buffer-modified-p gnus-dribble-buffer)
5116                    (save-excursion
5117                      (set-buffer gnus-dribble-buffer)
5118                      (not (zerop (buffer-size))))))
5119              (mode-string (eval gformat)))
5120         ;; Say whether the dribble buffer has been modified.
5121         (setq mode-line-modified
5122               (if modified "---*- " "----- "))
5123         ;; If the line is too long, we chop it off.
5124         (when (> (length mode-string) max-len)
5125           (setq mode-string (substring mode-string 0 (- max-len 4))))
5126         (prog1
5127             (setq mode-line-buffer-identification 
5128                   (gnus-mode-line-buffer-identification
5129                    (list mode-string)))
5130           (set-buffer-modified-p modified))))))
5131
5132 (defun gnus-group-group-name ()
5133   "Get the name of the newsgroup on the current line."
5134   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5135     (and group (symbol-name group))))
5136
5137 (defun gnus-group-group-level ()
5138   "Get the level of the newsgroup on the current line."
5139   (get-text-property (gnus-point-at-bol) 'gnus-level))
5140
5141 (defun gnus-group-group-indentation ()
5142   "Get the indentation of the newsgroup on the current line."
5143   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5144       (and gnus-group-indentation-function
5145            (funcall gnus-group-indentation-function))
5146       ""))
5147
5148 (defun gnus-group-group-unread ()
5149   "Get the number of unread articles of the newsgroup on the current line."
5150   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5151
5152 (defun gnus-group-search-forward (&optional backward all level first-too)
5153   "Find the next newsgroup with unread articles.
5154 If BACKWARD is non-nil, find the previous newsgroup instead.
5155 If ALL is non-nil, just find any newsgroup.
5156 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5157 group exists.
5158 If FIRST-TOO, the current line is also eligible as a target."
5159   (let ((way (if backward -1 1))
5160         (low gnus-level-killed)
5161         (beg (point))
5162         pos found lev)
5163     (if (and backward (progn (beginning-of-line)) (bobp))
5164         nil
5165       (or first-too (forward-line way))
5166       (while (and
5167               (not (eobp))
5168               (not (setq
5169                     found
5170                     (and (or all
5171                              (and
5172                               (let ((unread
5173                                      (get-text-property (point) 'gnus-unread)))
5174                                 (and (numberp unread) (> unread 0)))
5175                               (setq lev (get-text-property (point)
5176                                                            'gnus-level))
5177                               (<= lev gnus-level-subscribed)))
5178                          (or (not level)
5179                              (and (setq lev (get-text-property (point)
5180                                                                'gnus-level))
5181                                   (or (= lev level)
5182                                       (and (< lev low)
5183                                            (< level lev)
5184                                            (progn
5185                                              (setq low lev)
5186                                              (setq pos (point))
5187                                              nil))))))))
5188               (zerop (forward-line way)))))
5189     (if found
5190         (progn (gnus-group-position-point) t)
5191       (goto-char (or pos beg))
5192       (and pos t))))
5193
5194 ;;; Gnus group mode commands
5195
5196 ;; Group marking.
5197
5198 (defun gnus-group-mark-group (n &optional unmark no-advance)
5199   "Mark the current group."
5200   (interactive "p")
5201   (let ((buffer-read-only nil)
5202         group)
5203     (while (and (> n 0)
5204                 (not (eobp)))
5205       (when (setq group (gnus-group-group-name))
5206         ;; Update the mark.
5207         (beginning-of-line)
5208         (forward-char
5209          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5210         (delete-char 1)
5211         (if unmark
5212             (progn
5213               (insert " ")
5214               (setq gnus-group-marked (delete group gnus-group-marked)))
5215           (insert "#")
5216           (setq gnus-group-marked
5217                 (cons group (delete group gnus-group-marked)))))
5218       (or no-advance (gnus-group-next-group 1))
5219       (decf n))
5220     (gnus-summary-position-point)
5221     n))
5222
5223 (defun gnus-group-unmark-group (n)
5224   "Remove the mark from the current group."
5225   (interactive "p")
5226   (gnus-group-mark-group n 'unmark)
5227   (gnus-group-position-point))
5228
5229 (defun gnus-group-unmark-all-groups ()
5230   "Unmark all groups."
5231   (interactive)
5232   (let ((groups gnus-group-marked))
5233     (save-excursion
5234       (while groups
5235         (gnus-group-remove-mark (pop groups)))))
5236   (gnus-group-position-point))
5237
5238 (defun gnus-group-mark-region (unmark beg end)
5239   "Mark all groups between point and mark.
5240 If UNMARK, remove the mark instead."
5241   (interactive "P\nr")
5242   (let ((num (count-lines beg end)))
5243     (save-excursion
5244       (goto-char beg)
5245       (- num (gnus-group-mark-group num unmark)))))
5246
5247 (defun gnus-group-mark-buffer (&optional unmark)
5248   "Mark all groups in the buffer.
5249 If UNMARK, remove the mark instead."
5250   (interactive "P")
5251   (gnus-group-mark-region unmark (point-min) (point-max)))
5252
5253 (defun gnus-group-mark-regexp (regexp)
5254   "Mark all groups that match some regexp."
5255   (interactive "sMark (regexp): ")
5256   (let ((alist (cdr gnus-newsrc-alist))
5257         group)
5258     (while alist
5259       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5260         (gnus-group-set-mark group))))
5261   (gnus-group-position-point))
5262
5263 (defun gnus-group-remove-mark (group)
5264   "Remove the process mark from GROUP and move point there.
5265 Return nil if the group isn't displayed."
5266   (if (gnus-group-goto-group group)
5267       (save-excursion
5268         (gnus-group-mark-group 1 'unmark t)
5269         t)
5270     (setq gnus-group-marked
5271           (delete group gnus-group-marked))
5272     nil))
5273
5274 (defun gnus-group-set-mark (group)
5275   "Set the process mark on GROUP."
5276   (if (gnus-group-goto-group group) 
5277       (save-excursion
5278         (gnus-group-mark-group 1 nil t))
5279     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5280
5281 (defun gnus-group-universal-argument (arg &optional groups func)
5282   "Perform any command on all groups accoring to the process/prefix convention."
5283   (interactive "P")
5284   (let ((groups (or groups (gnus-group-process-prefix arg)))
5285         group func)
5286     (if (eq (setq func (or func
5287                            (key-binding
5288                             (read-key-sequence
5289                              (substitute-command-keys
5290                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5291             'undefined)
5292         (gnus-error 1 "Undefined key")
5293       (while groups
5294         (gnus-group-remove-mark (setq group (pop groups)))
5295         (command-execute func))))
5296   (gnus-group-position-point))
5297
5298 (defun gnus-group-process-prefix (n)
5299   "Return a list of groups to work on.
5300 Take into consideration N (the prefix) and the list of marked groups."
5301   (cond
5302    (n
5303     (setq n (prefix-numeric-value n))
5304     ;; There is a prefix, so we return a list of the N next
5305     ;; groups.
5306     (let ((way (if (< n 0) -1 1))
5307           (n (abs n))
5308           group groups)
5309       (save-excursion
5310         (while (and (> n 0)
5311                     (setq group (gnus-group-group-name)))
5312           (setq groups (cons group groups))
5313           (setq n (1- n))
5314           (gnus-group-next-group way)))
5315       (nreverse groups)))
5316    ((and (boundp 'transient-mark-mode)
5317          transient-mark-mode
5318          (boundp 'mark-active)
5319          mark-active)
5320     ;; Work on the region between point and mark.
5321     (let ((max (max (point) (mark)))
5322           groups)
5323       (save-excursion
5324         (goto-char (min (point) (mark)))
5325         (while
5326             (and
5327              (push (gnus-group-group-name) groups)
5328              (zerop (gnus-group-next-group 1))
5329              (< (point) max)))
5330         (nreverse groups))))
5331    (gnus-group-marked
5332     ;; No prefix, but a list of marked articles.
5333     (reverse gnus-group-marked))
5334    (t
5335     ;; Neither marked articles or a prefix, so we return the
5336     ;; current group.
5337     (let ((group (gnus-group-group-name)))
5338       (and group (list group))))))
5339
5340 ;; Selecting groups.
5341
5342 (defun gnus-group-read-group (&optional all no-article group)
5343   "Read news in this newsgroup.
5344 If the prefix argument ALL is non-nil, already read articles become
5345 readable.  IF ALL is a number, fetch this number of articles.  If the
5346 optional argument NO-ARTICLE is non-nil, no article will be
5347 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5348 group."
5349   (interactive "P")
5350   (let ((group (or group (gnus-group-group-name)))
5351         number active marked entry)
5352     (or group (error "No group on current line"))
5353     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5354                                             group gnus-newsrc-hashtb)))))
5355     ;; This group might be a dead group.  In that case we have to get
5356     ;; the number of unread articles from `gnus-active-hashtb'.
5357     (setq number
5358           (cond ((numberp all) all)
5359                 (entry (car entry))
5360                 ((setq active (gnus-active group))
5361                  (- (1+ (cdr active)) (car active)))))
5362     (gnus-summary-read-group
5363      group (or all (and (numberp number)
5364                         (zerop (+ number (length (cdr (assq 'tick marked)))
5365                                   (length (cdr (assq 'dormant marked)))))))
5366      no-article)))
5367
5368 (defun gnus-group-select-group (&optional all)
5369   "Select this newsgroup.
5370 No article is selected automatically.
5371 If ALL is non-nil, already read articles become readable.
5372 If ALL is a number, fetch this number of articles."
5373   (interactive "P")
5374   (gnus-group-read-group all t))
5375
5376 (defun gnus-group-quick-select-group (&optional all)
5377   "Select the current group \"quickly\".
5378 This means that no highlighting or scoring will be performed."
5379   (interactive "P")
5380   (let (gnus-visual
5381         gnus-score-find-score-files-function
5382         gnus-apply-kill-hook
5383         gnus-summary-expunge-below)
5384     (gnus-group-read-group all t)))
5385
5386 (defun gnus-group-visible-select-group (&optional all)
5387   "Select the current group without hiding any articles."
5388   (interactive "P")
5389   (let ((gnus-inhibit-limiting t))
5390     (gnus-group-read-group all t)))
5391
5392 ;;;###autoload
5393 (defun gnus-fetch-group (group)
5394   "Start Gnus if necessary and enter GROUP.
5395 Returns whether the fetching was successful or not."
5396   (interactive "sGroup name: ")
5397   (or (get-buffer gnus-group-buffer)
5398       (gnus))
5399   (gnus-group-read-group nil nil group))
5400
5401 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5402 ;; if selection was successful.
5403 (defun gnus-group-read-ephemeral-group
5404   (group method &optional activate quit-config)
5405   (let ((group (if (gnus-group-foreign-p group) group
5406                  (gnus-group-prefixed-name group method))))
5407     (gnus-sethash
5408      group
5409      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5410                      ((quit-config . ,(if quit-config quit-config
5411                                         (cons (current-buffer) 'summary))))))
5412      gnus-newsrc-hashtb)
5413     (set-buffer gnus-group-buffer)
5414     (or (gnus-check-server method)
5415         (error "Unable to contact server: %s" (gnus-status-message method)))
5416     (if activate (or (gnus-request-group group)
5417                      (error "Couldn't request group")))
5418     (condition-case ()
5419         (gnus-group-read-group t t group)
5420       (error nil)
5421       (quit nil))))
5422
5423 (defun gnus-group-jump-to-group (group)
5424   "Jump to newsgroup GROUP."
5425   (interactive
5426    (list (completing-read
5427           "Group: " gnus-active-hashtb nil
5428           (gnus-read-active-file-p)
5429           nil
5430           'gnus-group-history)))
5431
5432   (when (equal group "")
5433     (error "Empty group name"))
5434
5435   (when (string-match "[\000-\032]" group)
5436     (error "Control characters in group: %s" group))
5437
5438   (let ((b (text-property-any
5439             (point-min) (point-max)
5440             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5441     (unless (gnus-ephemeral-group-p group)
5442       (if b
5443           ;; Either go to the line in the group buffer...
5444           (goto-char b)
5445         ;; ... or insert the line.
5446         (or
5447          (gnus-active group)
5448          (gnus-activate-group group)
5449          (error "%s error: %s" group (gnus-status-message group)))
5450
5451         (gnus-group-update-group group)
5452         (goto-char (text-property-any
5453                     (point-min) (point-max)
5454                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5455     ;; Adjust cursor point.
5456     (gnus-group-position-point)))
5457
5458 (defun gnus-group-goto-group (group)
5459   "Goto to newsgroup GROUP."
5460   (when group
5461     (let ((b (text-property-any (point-min) (point-max)
5462                                 'gnus-group (gnus-intern-safe
5463                                              group gnus-active-hashtb))))
5464       (and b (goto-char b)))))
5465
5466 (defun gnus-group-next-group (n)
5467   "Go to next N'th newsgroup.
5468 If N is negative, search backward instead.
5469 Returns the difference between N and the number of skips actually
5470 done."
5471   (interactive "p")
5472   (gnus-group-next-unread-group n t))
5473
5474 (defun gnus-group-next-unread-group (n &optional all level)
5475   "Go to next N'th unread newsgroup.
5476 If N is negative, search backward instead.
5477 If ALL is non-nil, choose any newsgroup, unread or not.
5478 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5479 such group can be found, the next group with a level higher than
5480 LEVEL.
5481 Returns the difference between N and the number of skips actually
5482 made."
5483   (interactive "p")
5484   (let ((backward (< n 0))
5485         (n (abs n)))
5486     (while (and (> n 0)
5487                 (gnus-group-search-forward
5488                  backward (or (not gnus-group-goto-unread) all) level))
5489       (setq n (1- n)))
5490     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5491                                (if level " on this level or higher" "")))
5492     n))
5493
5494 (defun gnus-group-prev-group (n)
5495   "Go to previous N'th newsgroup.
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))
5500
5501 (defun gnus-group-prev-unread-group (n)
5502   "Go to previous N'th unread newsgroup.
5503 Returns the difference between N and the number of skips actually
5504 done."
5505   (interactive "p")
5506   (gnus-group-next-unread-group (- n)))
5507
5508 (defun gnus-group-next-unread-group-same-level (n)
5509   "Go to next N'th unread newsgroup on the same level.
5510 If N is negative, search backward instead.
5511 Returns the difference between N and the number of skips actually
5512 done."
5513   (interactive "p")
5514   (gnus-group-next-unread-group n t (gnus-group-group-level))
5515   (gnus-group-position-point))
5516
5517 (defun gnus-group-prev-unread-group-same-level (n)
5518   "Go to next N'th unread newsgroup on the same level.
5519 Returns the difference between N and the number of skips actually
5520 done."
5521   (interactive "p")
5522   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5523   (gnus-group-position-point))
5524
5525 (defun gnus-group-best-unread-group (&optional exclude-group)
5526   "Go to the group with the highest level.
5527 If EXCLUDE-GROUP, do not go to that group."
5528   (interactive)
5529   (goto-char (point-min))
5530   (let ((best 100000)
5531         unread best-point)
5532     (while (not (eobp))
5533       (setq unread (get-text-property (point) 'gnus-unread))
5534       (if (and (numberp unread) (> unread 0))
5535           (progn
5536             (if (and (get-text-property (point) 'gnus-level)
5537                      (< (get-text-property (point) 'gnus-level) best)
5538                      (or (not exclude-group)
5539                          (not (equal exclude-group (gnus-group-group-name)))))
5540                 (progn
5541                   (setq best (get-text-property (point) 'gnus-level))
5542                   (setq best-point (point))))))
5543       (forward-line 1))
5544     (if best-point (goto-char best-point))
5545     (gnus-summary-position-point)
5546     (and best-point (gnus-group-group-name))))
5547
5548 (defun gnus-group-first-unread-group ()
5549   "Go to the first group with unread articles."
5550   (interactive)
5551   (prog1
5552       (let ((opoint (point))
5553             unread)
5554         (goto-char (point-min))
5555         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5556                 (and (numberp unread)   ; Not a topic.
5557                      (not (zerop unread))) ; Has unread articles.
5558                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5559             (point)                     ; Success.
5560           (goto-char opoint)
5561           nil))                         ; Not success.
5562     (gnus-group-position-point)))
5563
5564 (defun gnus-group-enter-server-mode ()
5565   "Jump to the server buffer."
5566   (interactive)
5567   (gnus-enter-server-buffer))
5568
5569 (defun gnus-group-make-group (name &optional method address)
5570   "Add a new newsgroup.
5571 The user will be prompted for a NAME, for a select METHOD, and an
5572 ADDRESS."
5573   (interactive
5574    (cons
5575     (read-string "Group name: ")
5576     (let ((method
5577            (completing-read
5578             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5579             nil t nil 'gnus-method-history)))
5580       (cond ((assoc method gnus-valid-select-methods)
5581              (list method
5582                    (if (memq 'prompt-address
5583                              (assoc method gnus-valid-select-methods))
5584                        (read-string "Address: ")
5585                      "")))
5586             ((assoc method gnus-server-alist)
5587              (list method))
5588             (t
5589              (list method ""))))))
5590
5591   (let* ((meth (and method (if address (list (intern method) address)
5592                              method)))
5593          (nname (if method (gnus-group-prefixed-name name meth) name))
5594          backend info)
5595     (when (gnus-gethash nname gnus-newsrc-hashtb)
5596       (error "Group %s already exists" nname))
5597     ;; Subscribe to the new group.
5598     (gnus-group-change-level
5599      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5600      gnus-level-default-subscribed gnus-level-killed
5601      (and (gnus-group-group-name)
5602           (gnus-gethash (gnus-group-group-name)
5603                         gnus-newsrc-hashtb))
5604      t)
5605     ;; Make it active.
5606     (gnus-set-active nname (cons 1 0))
5607     (or (gnus-ephemeral-group-p name)
5608         (gnus-dribble-enter
5609          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5610     ;; Insert the line.
5611     (gnus-group-insert-group-line-info nname)
5612     (forward-line -1)
5613     (gnus-group-position-point)
5614
5615     ;; Load the backend and try to make the backend create
5616     ;; the group as well.
5617     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5618                                                   nil meth))))
5619                  gnus-valid-select-methods)
5620       (require backend))
5621     (gnus-check-server meth)
5622     (and (gnus-check-backend-function 'request-create-group nname)
5623          (gnus-request-create-group nname))
5624     t))
5625
5626 (defun gnus-group-delete-group (group &optional force)
5627   "Delete the current group.  Only meaningful with mail groups.
5628 If FORCE (the prefix) is non-nil, all the articles in the group will
5629 be deleted.  This is \"deleted\" as in \"removed forever from the face
5630 of the Earth\".  There is no undo.  The user will be prompted before
5631 doing the deletion."
5632   (interactive
5633    (list (gnus-group-group-name)
5634          current-prefix-arg))
5635   (or group (error "No group to rename"))
5636   (or (gnus-check-backend-function 'request-delete-group group)
5637       (error "This backend does not support group deletion"))
5638   (prog1
5639       (if (not (gnus-yes-or-no-p
5640                 (format
5641                  "Do you really want to delete %s%s? "
5642                  group (if force " and all its contents" ""))))
5643           () ; Whew!
5644         (gnus-message 6 "Deleting group %s..." group)
5645         (if (not (gnus-request-delete-group group force))
5646             (gnus-error 3 "Couldn't delete group %s" group)
5647           (gnus-message 6 "Deleting group %s...done" group)
5648           (gnus-group-goto-group group)
5649           (gnus-group-kill-group 1 t)
5650           (gnus-sethash group nil gnus-active-hashtb)
5651           t))
5652     (gnus-group-position-point)))
5653
5654 (defun gnus-group-rename-group (group new-name)
5655   (interactive
5656    (list
5657     (gnus-group-group-name)
5658     (progn
5659       (or (gnus-check-backend-function
5660            'request-rename-group (gnus-group-group-name))
5661           (error "This backend does not support renaming groups"))
5662       (read-string "New group name: "))))
5663
5664   (or (gnus-check-backend-function 'request-rename-group group)
5665       (error "This backend does not support renaming groups"))
5666
5667   (or group (error "No group to rename"))
5668   (and (string-match "^[ \t]*$" new-name)
5669        (error "Not a valid group name"))
5670
5671   ;; We find the proper prefixed name.
5672   (setq new-name
5673         (gnus-group-prefixed-name
5674          (gnus-group-real-name new-name)
5675          (gnus-info-method (gnus-get-info group))))
5676
5677   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5678   (prog1
5679       (if (not (gnus-request-rename-group group new-name))
5680           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5681         ;; We rename the group internally by killing it...
5682         (gnus-group-goto-group group)
5683         (gnus-group-kill-group)
5684         ;; ... changing its name ...
5685         (setcar (cdar gnus-list-of-killed-groups) new-name)
5686         ;; ... and then yanking it.  Magic!
5687         (gnus-group-yank-group)
5688         (gnus-set-active new-name (gnus-active group))
5689         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5690         new-name)
5691     (gnus-group-position-point)))
5692
5693 (defun gnus-group-edit-group (group &optional part)
5694   "Edit the group on the current line."
5695   (interactive (list (gnus-group-group-name)))
5696   (let* ((part (or part 'info))
5697          (done-func `(lambda ()
5698                        "Exit editing mode and update the information."
5699                        (interactive)
5700                        (gnus-group-edit-group-done ',part ,group)))
5701          (winconf (current-window-configuration))
5702          info)
5703     (or group (error "No group on current line"))
5704     (or (setq info (gnus-get-info group))
5705         (error "Killed group; can't be edited"))
5706     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5707     (gnus-configure-windows 'edit-group)
5708     (gnus-add-current-to-buffer-list)
5709     (emacs-lisp-mode)
5710     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5711     (use-local-map (copy-keymap emacs-lisp-mode-map))
5712     (local-set-key "\C-c\C-c" done-func)
5713     (make-local-variable 'gnus-prev-winconf)
5714     (setq gnus-prev-winconf winconf)
5715     (erase-buffer)
5716     (insert
5717      (cond
5718       ((eq part 'method)
5719        ";; Type `C-c C-c' after editing the select method.\n\n")
5720       ((eq part 'params)
5721        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5722       ((eq part 'info)
5723        ";; Type `C-c C-c' after editing the group info.\n\n")))
5724     (insert
5725      (pp-to-string
5726       (cond ((eq part 'method)
5727              (or (gnus-info-method info) "native"))
5728             ((eq part 'params)
5729              (gnus-info-params info))
5730             (t info)))
5731      "\n")))
5732
5733 (defun gnus-group-edit-group-method (group)
5734   "Edit the select method of GROUP."
5735   (interactive (list (gnus-group-group-name)))
5736   (gnus-group-edit-group group 'method))
5737
5738 (defun gnus-group-edit-group-parameters (group)
5739   "Edit the group parameters of GROUP."
5740   (interactive (list (gnus-group-group-name)))
5741   (gnus-group-edit-group group 'params))
5742
5743 (defun gnus-group-edit-group-done (part group)
5744   "Get info from buffer, update variables and jump to the group buffer."
5745   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5746   (goto-char (point-min))
5747   (let* ((form (read (current-buffer)))
5748          (winconf gnus-prev-winconf)
5749          (method (cond ((eq part 'info) (nth 4 form))
5750                        ((eq part 'method) form)
5751                        (t nil)))
5752          (info (cond ((eq part 'info) form)
5753                      ((eq part 'method) (gnus-get-info group))
5754                      (t nil)))
5755          (new-group (if info
5756                       (if (or (not method)
5757                               (gnus-server-equal
5758                                gnus-select-method method))
5759                           (gnus-group-real-name (car info))
5760                         (gnus-group-prefixed-name
5761                          (gnus-group-real-name (car info)) method))
5762                       nil)))
5763     (when (and new-group
5764                (not (equal new-group group)))
5765       (when (gnus-group-goto-group group)
5766         (gnus-group-kill-group 1))
5767       (gnus-activate-group new-group))
5768     ;; Set the info.
5769     (if (and info new-group)
5770         (progn
5771           (setq info (gnus-copy-sequence info))
5772           (setcar info new-group)
5773           (unless (gnus-server-equal method "native")
5774             (unless (nthcdr 3 info)
5775               (nconc info (list nil nil)))
5776             (unless (nthcdr 4 info)
5777               (nconc info (list nil)))
5778             (gnus-info-set-method info method))
5779           (gnus-group-set-info info))
5780       (gnus-group-set-info form (or new-group group) part))
5781     (kill-buffer (current-buffer))
5782     (and winconf (set-window-configuration winconf))
5783     (set-buffer gnus-group-buffer)
5784     (gnus-group-update-group (or new-group group))
5785     (gnus-group-position-point)))
5786
5787 (defun gnus-group-make-help-group ()
5788   "Create the Gnus documentation group."
5789   (interactive)
5790   (let ((path load-path)
5791         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5792         file dir)
5793     (and (gnus-gethash name gnus-newsrc-hashtb)
5794          (error "Documentation group already exists"))
5795     (while path
5796       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5797             file nil)
5798       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5799                 (file-exists-p
5800                  (setq file (concat (file-name-directory
5801                                      (directory-file-name dir))
5802                                     "etc/gnus-tut.txt"))))
5803         (setq path nil)))
5804     (if (not file)
5805         (gnus-message 1 "Couldn't find doc group")
5806       (gnus-group-make-group
5807        (gnus-group-real-name name)
5808        (list 'nndoc "gnus-help"
5809              (list 'nndoc-address file)
5810              (list 'nndoc-article-type 'mbox)))))
5811   (gnus-group-position-point))
5812
5813 (defun gnus-group-make-doc-group (file type)
5814   "Create a group that uses a single file as the source."
5815   (interactive
5816    (list (read-file-name "File name: ")
5817          (and current-prefix-arg 'ask)))
5818   (when (eq type 'ask)
5819     (let ((err "")
5820           char found)
5821       (while (not found)
5822         (message
5823          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5824          err)
5825         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5826                           ((= char ?b) 'babyl)
5827                           ((= char ?d) 'digest)
5828                           ((= char ?f) 'forward)
5829                           ((= char ?a) 'mmfd)
5830                           (t (setq err (format "%c unknown. " char))
5831                              nil))))
5832       (setq type found)))
5833   (let* ((file (expand-file-name file))
5834          (name (gnus-generate-new-group-name
5835                 (gnus-group-prefixed-name
5836                  (file-name-nondirectory file) '(nndoc "")))))
5837     (gnus-group-make-group
5838      (gnus-group-real-name name)
5839      (list 'nndoc (file-name-nondirectory file)
5840            (list 'nndoc-address file)
5841            (list 'nndoc-article-type (or type 'guess))))))
5842
5843 (defun gnus-group-make-archive-group (&optional all)
5844   "Create the (ding) Gnus archive group of the most recent articles.
5845 Given a prefix, create a full group."
5846   (interactive "P")
5847   (let ((group (gnus-group-prefixed-name
5848                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5849     (and (gnus-gethash group gnus-newsrc-hashtb)
5850          (error "Archive group already exists"))
5851     (gnus-group-make-group
5852      (gnus-group-real-name group)
5853      (list 'nndir (if all "hpc" "edu")
5854            (list 'nndir-directory
5855                  (if all gnus-group-archive-directory
5856                    gnus-group-recent-archive-directory))))))
5857
5858 (defun gnus-group-make-directory-group (dir)
5859   "Create an nndir group.
5860 The user will be prompted for a directory.  The contents of this
5861 directory will be used as a newsgroup.  The directory should contain
5862 mail messages or news articles in files that have numeric names."
5863   (interactive
5864    (list (read-file-name "Create group from directory: ")))
5865   (or (file-exists-p dir) (error "No such directory"))
5866   (or (file-directory-p dir) (error "Not a directory"))
5867   (let ((ext "")
5868         (i 0)
5869         group)
5870     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5871       (setq group
5872             (gnus-group-prefixed-name
5873              (concat (file-name-as-directory (directory-file-name dir))
5874                      ext)
5875              '(nndir "")))
5876       (setq ext (format "<%d>" (setq i (1+ i)))))
5877     (gnus-group-make-group
5878      (gnus-group-real-name group)
5879      (list 'nndir group (list 'nndir-directory dir)))))
5880
5881 (defun gnus-group-make-kiboze-group (group address scores)
5882   "Create an nnkiboze group.
5883 The user will be prompted for a name, a regexp to match groups, and
5884 score file entries for articles to include in the group."
5885   (interactive
5886    (list
5887     (read-string "nnkiboze group name: ")
5888     (read-string "Source groups (regexp): ")
5889     (let ((headers (mapcar (lambda (group) (list group))
5890                            '("subject" "from" "number" "date" "message-id"
5891                              "references" "chars" "lines" "xref"
5892                              "followup" "all" "body" "head")))
5893           scores header regexp regexps)
5894       (while (not (equal "" (setq header (completing-read
5895                                           "Match on header: " headers nil t))))
5896         (setq regexps nil)
5897         (while (not (equal "" (setq regexp (read-string
5898                                             (format "Match on %s (string): "
5899                                                     header)))))
5900           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5901         (setq scores (cons (cons header regexps) scores)))
5902       scores)))
5903   (gnus-group-make-group group "nnkiboze" address)
5904   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5905     (let (emacs-lisp-mode-hook)
5906       (pp scores (current-buffer)))))
5907
5908 (defun gnus-group-add-to-virtual (n vgroup)
5909   "Add the current group to a virtual group."
5910   (interactive
5911    (list current-prefix-arg
5912          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5913                           "nnvirtual:")))
5914   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5915       (error "%s is not an nnvirtual group" vgroup))
5916   (let* ((groups (gnus-group-process-prefix n))
5917          (method (gnus-info-method (gnus-get-info vgroup))))
5918     (setcar (cdr method)
5919             (concat
5920              (nth 1 method) "\\|"
5921              (mapconcat
5922               (lambda (s)
5923                 (gnus-group-remove-mark s)
5924                 (concat "\\(^" (regexp-quote s) "$\\)"))
5925               groups "\\|"))))
5926   (gnus-group-position-point))
5927
5928 (defun gnus-group-make-empty-virtual (group)
5929   "Create a new, fresh, empty virtual group."
5930   (interactive "sCreate new, empty virtual group: ")
5931   (let* ((method (list 'nnvirtual "^$"))
5932          (pgroup (gnus-group-prefixed-name group method)))
5933     ;; Check whether it exists already.
5934     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5935          (error "Group %s already exists." pgroup))
5936     ;; Subscribe the new group after the group on the current line.
5937     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5938     (gnus-group-update-group pgroup)
5939     (forward-line -1)
5940     (gnus-group-position-point)))
5941
5942 (defun gnus-group-enter-directory (dir)
5943   "Enter an ephemeral nneething group."
5944   (interactive "DDirectory to read: ")
5945   (let* ((method (list 'nneething dir))
5946          (leaf (gnus-group-prefixed-name
5947                 (file-name-nondirectory (directory-file-name dir))
5948                 method))
5949          (name (gnus-generate-new-group-name leaf)))
5950     (let ((nneething-read-only t))
5951       (or (gnus-group-read-ephemeral-group
5952            name method t
5953            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5954                                       'summary 'group)))
5955           (error "Couldn't enter %s" dir)))))
5956
5957 ;; Group sorting commands
5958 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5959
5960 (defun gnus-group-sort-groups (func &optional reverse)
5961   "Sort the group buffer according to FUNC.
5962 If REVERSE, reverse the sorting order."
5963   (interactive (list gnus-group-sort-function
5964                      current-prefix-arg))
5965   (let ((func (cond 
5966                ((not (listp func)) func)
5967                ((null func) func)
5968                ((= 1 (length func)) (car func))
5969                (t `(lambda (t1 t2)
5970                      ,(gnus-make-sort-function 
5971                        (reverse func)))))))
5972     ;; We peel off the dummy group from the alist.
5973     (when func
5974       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5975         (pop gnus-newsrc-alist))
5976       ;; Do the sorting.
5977       (setq gnus-newsrc-alist
5978             (sort gnus-newsrc-alist func))
5979       (when reverse
5980         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5981       ;; Regenerate the hash table.
5982       (gnus-make-hashtable-from-newsrc-alist)
5983       (gnus-group-list-groups))))
5984
5985 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5986   "Sort the group buffer alphabetically by group name.
5987 If REVERSE, sort in reverse order."
5988   (interactive "P")
5989   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5990
5991 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5992   "Sort the group buffer by number of unread articles.
5993 If REVERSE, sort in reverse order."
5994   (interactive "P")
5995   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5996
5997 (defun gnus-group-sort-groups-by-level (&optional reverse)
5998   "Sort the group buffer by group level.
5999 If REVERSE, sort in reverse order."
6000   (interactive "P")
6001   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
6002
6003 (defun gnus-group-sort-groups-by-score (&optional reverse)
6004   "Sort the group buffer by group score.
6005 If REVERSE, sort in reverse order."
6006   (interactive "P")
6007   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
6008
6009 (defun gnus-group-sort-groups-by-rank (&optional reverse)
6010   "Sort the group buffer by group rank.
6011 If REVERSE, sort in reverse order."
6012   (interactive "P")
6013   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
6014
6015 (defun gnus-group-sort-groups-by-method (&optional reverse)
6016   "Sort the group buffer alphabetically by backend name.
6017 If REVERSE, sort in reverse order."
6018   (interactive "P")
6019   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
6020
6021 (defun gnus-group-sort-by-alphabet (info1 info2)
6022   "Sort alphabetically."
6023   (string< (gnus-info-group info1) (gnus-info-group info2)))
6024
6025 (defun gnus-group-sort-by-unread (info1 info2)
6026   "Sort by number of unread articles."
6027   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
6028         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
6029     (< (or (and (numberp n1) n1) 0)
6030        (or (and (numberp n2) n2) 0))))
6031
6032 (defun gnus-group-sort-by-level (info1 info2)
6033   "Sort by level."
6034   (< (gnus-info-level info1) (gnus-info-level info2)))
6035
6036 (defun gnus-group-sort-by-method (info1 info2)
6037   "Sort alphabetically by backend name."
6038   (string< (symbol-name (car (gnus-find-method-for-group
6039                               (gnus-info-group info1) info1)))
6040            (symbol-name (car (gnus-find-method-for-group
6041                               (gnus-info-group info2) info2)))))
6042
6043 (defun gnus-group-sort-by-score (info1 info2)
6044   "Sort by group score."
6045   (< (gnus-info-score info1) (gnus-info-score info2)))
6046
6047 (defun gnus-group-sort-by-rank (info1 info2)
6048   "Sort by level and score."
6049   (let ((level1 (gnus-info-level info1))
6050         (level2 (gnus-info-level info2)))
6051     (or (< level1 level2)
6052         (and (= level1 level2)
6053              (> (gnus-info-score info1) (gnus-info-score info2))))))
6054
6055 ;; Group catching up.
6056
6057 (defun gnus-group-clear-data (n)
6058   "Clear all marks and read ranges from the current group."
6059   (interactive "P")
6060   (let ((groups (gnus-group-process-prefix n))
6061         group info)
6062     (while (setq group (pop groups))
6063       (setq info (gnus-get-info group))
6064       (gnus-info-set-read info nil)
6065       (when (gnus-info-marks info)
6066         (gnus-info-set-marks info nil))
6067       (gnus-get-unread-articles-in-group info (gnus-active group) t)
6068       (when (gnus-group-goto-group group)
6069         (gnus-group-remove-mark group)
6070         (gnus-group-update-group-line)))))
6071
6072 (defun gnus-group-catchup-current (&optional n all)
6073   "Mark all articles not marked as unread in current newsgroup as read.
6074 If prefix argument N is numeric, the ARG next newsgroups will be
6075 caught up.  If ALL is non-nil, marked articles will also be marked as
6076 read.  Cross references (Xref: header) of articles are ignored.
6077 The difference between N and actual number of newsgroups that were
6078 caught up is returned."
6079   (interactive "P")
6080   (unless (gnus-group-group-name)
6081     (error "No group on the current line"))
6082   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
6083                gnus-expert-user
6084                (gnus-y-or-n-p
6085                 (if all
6086                     "Do you really want to mark all articles as read? "
6087                   "Mark all unread articles as read? "))))
6088       n
6089     (let ((groups (gnus-group-process-prefix n))
6090           (ret 0))
6091       (while groups
6092         ;; Virtual groups have to be given special treatment.
6093         (let ((method (gnus-find-method-for-group (car groups))))
6094           (if (eq 'nnvirtual (car method))
6095               (nnvirtual-catchup-group
6096                (gnus-group-real-name (car groups)) (nth 1 method) all)))
6097         (gnus-group-remove-mark (car groups))
6098         (if (>= (gnus-group-group-level) gnus-level-zombie)
6099             (gnus-message 2 "Dead groups can't be caught up")
6100           (if (prog1
6101                   (gnus-group-goto-group (car groups))
6102                 (gnus-group-catchup (car groups) all))
6103               (gnus-group-update-group-line)
6104             (setq ret (1+ ret))))
6105         (setq groups (cdr groups)))
6106       (gnus-group-next-unread-group 1)
6107       ret)))
6108
6109 (defun gnus-group-catchup-current-all (&optional n)
6110   "Mark all articles in current newsgroup as read.
6111 Cross references (Xref: header) of articles are ignored."
6112   (interactive "P")
6113   (gnus-group-catchup-current n 'all))
6114
6115 (defun gnus-group-catchup (group &optional all)
6116   "Mark all articles in GROUP as read.
6117 If ALL is non-nil, all articles are marked as read.
6118 The return value is the number of articles that were marked as read,
6119 or nil if no action could be taken."
6120   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6121          (num (car entry)))
6122     ;; Do the updating only if the newsgroup isn't killed.
6123     (if (not (numberp (car entry)))
6124         (gnus-message 1 "Can't catch up; non-active group")
6125       ;; Do auto-expirable marks if that's required.
6126       (when (gnus-group-auto-expirable-p group)
6127         (gnus-add-marked-articles
6128          group 'expire (gnus-list-of-unread-articles group))
6129         (when all
6130           (let ((marks (nth 3 (nth 2 entry))))
6131             (gnus-add-marked-articles
6132              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6133             (gnus-add-marked-articles
6134              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6135       (when entry
6136         (gnus-update-read-articles group nil)
6137         ;; Also nix out the lists of marks and dormants.
6138         (when all
6139           (gnus-add-marked-articles group 'tick nil nil 'force)
6140           (gnus-add-marked-articles group 'dormant nil nil 'force))
6141         (run-hooks 'gnus-group-catchup-group-hook)
6142         num))))
6143
6144 (defun gnus-group-expire-articles (&optional n)
6145   "Expire all expirable articles in the current newsgroup."
6146   (interactive "P")
6147   (let ((groups (gnus-group-process-prefix n))
6148         group)
6149     (unless groups
6150       (error "No groups to expire"))
6151     (while (setq group (pop groups))
6152       (gnus-group-remove-mark group)
6153       (when (gnus-check-backend-function 'request-expire-articles group)
6154         (gnus-message 6 "Expiring articles in %s..." group)
6155         (let* ((info (gnus-get-info group))
6156                (expirable (if (gnus-group-total-expirable-p group)
6157                               (cons nil (gnus-list-of-read-articles group))
6158                             (assq 'expire (gnus-info-marks info))))
6159                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6160           (when expirable
6161             (setcdr
6162              expirable
6163              (gnus-compress-sequence
6164               (if expiry-wait
6165                   ;; We set the expiry variables to the groupp
6166                   ;; parameter. 
6167                   (let ((nnmail-expiry-wait-function nil)
6168                         (nnmail-expiry-wait expiry-wait))
6169                     (gnus-request-expire-articles
6170                      (gnus-uncompress-sequence (cdr expirable)) group))
6171                 ;; Just expire using the normal expiry values.
6172                 (gnus-request-expire-articles
6173                  (gnus-uncompress-sequence (cdr expirable)) group))))
6174             (gnus-close-group group))
6175           (gnus-message 6 "Expiring articles in %s...done" group)))
6176       (gnus-group-position-point))))
6177
6178 (defun gnus-group-expire-all-groups ()
6179   "Expire all expirable articles in all newsgroups."
6180   (interactive)
6181   (save-excursion
6182     (gnus-message 5 "Expiring...")
6183     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6184                                      (cdr gnus-newsrc-alist))))
6185       (gnus-group-expire-articles nil)))
6186   (gnus-group-position-point)
6187   (gnus-message 5 "Expiring...done"))
6188
6189 (defun gnus-group-set-current-level (n level)
6190   "Set the level of the next N groups to LEVEL."
6191   (interactive
6192    (list
6193     current-prefix-arg
6194     (string-to-int
6195      (let ((s (read-string
6196                (format "Level (default %s): "
6197                        (or (gnus-group-group-level) 
6198                            gnus-level-default-subscribed)))))
6199        (if (string-match "^\\s-*$" s)
6200            (int-to-string (or (gnus-group-group-level) 
6201                               gnus-level-default-subscribed))
6202          s)))))
6203   (or (and (>= level 1) (<= level gnus-level-killed))
6204       (error "Illegal level: %d" level))
6205   (let ((groups (gnus-group-process-prefix n))
6206         group)
6207     (while (setq group (pop groups))
6208       (gnus-group-remove-mark group)
6209       (gnus-message 6 "Changed level of %s from %d to %d"
6210                     group (or (gnus-group-group-level) gnus-level-killed)
6211                     level)
6212       (gnus-group-change-level
6213        group level (or (gnus-group-group-level) gnus-level-killed))
6214       (gnus-group-update-group-line)))
6215   (gnus-group-position-point))
6216
6217 (defun gnus-group-unsubscribe-current-group (&optional n)
6218   "Toggle subscription of the current group.
6219 If given numerical prefix, toggle the N next groups."
6220   (interactive "P")
6221   (let ((groups (gnus-group-process-prefix n))
6222         group)
6223     (while groups
6224       (setq group (car groups)
6225             groups (cdr groups))
6226       (gnus-group-remove-mark group)
6227       (gnus-group-unsubscribe-group
6228        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6229                  gnus-level-default-unsubscribed
6230                gnus-level-default-subscribed) t)
6231       (gnus-group-update-group-line))
6232     (gnus-group-next-group 1)))
6233
6234 (defun gnus-group-unsubscribe-group (group &optional level silent)
6235   "Toggle subscription to GROUP.
6236 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6237 group line."
6238   (interactive
6239    (list (completing-read
6240           "Group: " gnus-active-hashtb nil
6241           (gnus-read-active-file-p)
6242           nil 
6243           'gnus-group-history)))
6244   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6245     (cond
6246      ((string-match "^[ \t]$" group)
6247       (error "Empty group name"))
6248      (newsrc
6249       ;; Toggle subscription flag.
6250       (gnus-group-change-level
6251        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6252                                       gnus-level-subscribed)
6253                                   (1+ gnus-level-subscribed)
6254                                 gnus-level-default-subscribed)))
6255       (unless silent
6256         (gnus-group-update-group group)))
6257      ((and (stringp group)
6258            (or (not (gnus-read-active-file-p))
6259                (gnus-active group)))
6260       ;; Add new newsgroup.
6261       (gnus-group-change-level
6262        group
6263        (if level level gnus-level-default-subscribed)
6264        (or (and (member group gnus-zombie-list)
6265                 gnus-level-zombie)
6266            gnus-level-killed)
6267        (and (gnus-group-group-name)
6268             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6269       (unless silent
6270         (gnus-group-update-group group)))
6271      (t (error "No such newsgroup: %s" group)))
6272     (gnus-group-position-point)))
6273
6274 (defun gnus-group-transpose-groups (n)
6275   "Move the current newsgroup up N places.
6276 If given a negative prefix, move down instead.  The difference between
6277 N and the number of steps taken is returned."
6278   (interactive "p")
6279   (or (gnus-group-group-name)
6280       (error "No group on current line"))
6281   (gnus-group-kill-group 1)
6282   (prog1
6283       (forward-line (- n))
6284     (gnus-group-yank-group)
6285     (gnus-group-position-point)))
6286
6287 (defun gnus-group-kill-all-zombies ()
6288   "Kill all zombie newsgroups."
6289   (interactive)
6290   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6291   (setq gnus-zombie-list nil)
6292   (gnus-group-list-groups))
6293
6294 (defun gnus-group-kill-region (begin end)
6295   "Kill newsgroups in current region (excluding current point).
6296 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6297   (interactive "r")
6298   (let ((lines
6299          ;; Count lines.
6300          (save-excursion
6301            (count-lines
6302             (progn
6303               (goto-char begin)
6304               (beginning-of-line)
6305               (point))
6306             (progn
6307               (goto-char end)
6308               (beginning-of-line)
6309               (point))))))
6310     (goto-char begin)
6311     (beginning-of-line)                 ;Important when LINES < 1
6312     (gnus-group-kill-group lines)))
6313
6314 (defun gnus-group-kill-group (&optional n discard)
6315   "Kill the next N groups.
6316 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6317 However, only groups that were alive can be yanked; already killed
6318 groups or zombie groups can't be yanked.
6319 The return value is the name of the group that was killed, or a list
6320 of groups killed."
6321   (interactive "P")
6322   (let ((buffer-read-only nil)
6323         (groups (gnus-group-process-prefix n))
6324         group entry level out)
6325     (if (< (length groups) 10)
6326         ;; This is faster when there are few groups.
6327         (while groups
6328           (push (setq group (pop groups)) out)
6329           (gnus-group-remove-mark group)
6330           (setq level (gnus-group-group-level))
6331           (gnus-delete-line)
6332           (when (and (not discard)
6333                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6334             (push (cons (car entry) (nth 2 entry))
6335                   gnus-list-of-killed-groups))
6336           (gnus-group-change-level
6337            (if entry entry group) gnus-level-killed (if entry nil level)))
6338       ;; If there are lots and lots of groups to be killed, we use
6339       ;; this thing instead.
6340       (let (entry)
6341         (setq groups (nreverse groups))
6342         (while groups
6343           (gnus-group-remove-mark (setq group (pop groups)))
6344           (gnus-delete-line)
6345           (push group gnus-killed-list)
6346           (setq gnus-newsrc-alist
6347                 (delq (assoc group gnus-newsrc-alist)
6348                       gnus-newsrc-alist))
6349           (when gnus-group-change-level-function
6350             (funcall gnus-group-change-level-function group 9 3))
6351           (cond
6352            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6353             (push (cons (car entry) (nth 2 entry))
6354                   gnus-list-of-killed-groups)
6355             (setcdr (cdr entry) (cdddr entry)))
6356            ((member group gnus-zombie-list)
6357             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6358         (gnus-make-hashtable-from-newsrc-alist)))
6359
6360     (gnus-group-position-point)
6361     (if (< (length out) 2) (car out) (nreverse out))))
6362
6363 (defun gnus-group-yank-group (&optional arg)
6364   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6365 inserting it before the current newsgroup.  The numeric ARG specifies
6366 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6367 is returned, or (if several groups are yanked) a list of yanked groups
6368 is returned."
6369   (interactive "p")
6370   (setq arg (or arg 1))
6371   (let (info group prev out)
6372     (while (>= (decf arg) 0)
6373       (if (not (setq info (pop gnus-list-of-killed-groups)))
6374           (error "No more newsgroups to yank"))
6375       (push (setq group (nth 1 info)) out)
6376       ;; Find which newsgroup to insert this one before - search
6377       ;; backward until something suitable is found.  If there are no
6378       ;; other newsgroups in this buffer, just make this newsgroup the
6379       ;; first newsgroup.
6380       (setq prev (gnus-group-group-name))
6381       (gnus-group-change-level
6382        info (gnus-info-level (cdr info)) gnus-level-killed
6383        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6384        t)
6385       (gnus-group-insert-group-line-info group))
6386     (forward-line -1)
6387     (gnus-group-position-point)
6388     (if (< (length out) 2) (car out) (nreverse out))))
6389
6390 (defun gnus-group-kill-level (level)
6391   "Kill all groups that is on a certain LEVEL."
6392   (interactive "nKill all groups on level: ")
6393   (cond
6394    ((= level gnus-level-zombie)
6395     (setq gnus-killed-list
6396           (nconc gnus-zombie-list gnus-killed-list))
6397     (setq gnus-zombie-list nil))
6398    ((and (< level gnus-level-zombie)
6399          (> level 0)
6400          (or gnus-expert-user
6401              (gnus-yes-or-no-p
6402               (format
6403                "Do you really want to kill all groups on level %d? "
6404                level))))
6405     (let* ((prev gnus-newsrc-alist)
6406            (alist (cdr prev)))
6407       (while alist
6408         (if (= (gnus-info-level level) level)
6409             (setcdr prev (cdr alist))
6410           (setq prev alist))
6411         (setq alist (cdr alist)))
6412       (gnus-make-hashtable-from-newsrc-alist)
6413       (gnus-group-list-groups)))
6414    (t
6415     (error "Can't kill; illegal level: %d" level))))
6416
6417 (defun gnus-group-list-all-groups (&optional arg)
6418   "List all newsgroups with level ARG or lower.
6419 Default is gnus-level-unsubscribed, which lists all subscribed and most
6420 unsubscribed groups."
6421   (interactive "P")
6422   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6423
6424 ;; Redefine this to list ALL killed groups if prefix arg used.
6425 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6426 (defun gnus-group-list-killed (&optional arg)
6427   "List all killed newsgroups in the group buffer.
6428 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6429 entail asking the server for the groups."
6430   (interactive "P")
6431   ;; Find all possible killed newsgroups if arg.
6432   (when arg
6433     (gnus-get-killed-groups))
6434   (if (not gnus-killed-list)
6435       (gnus-message 6 "No killed groups")
6436     (let (gnus-group-list-mode)
6437       (funcall gnus-group-prepare-function
6438                gnus-level-killed t gnus-level-killed))
6439     (goto-char (point-min)))
6440   (gnus-group-position-point))
6441
6442 (defun gnus-group-list-zombies ()
6443   "List all zombie newsgroups in the group buffer."
6444   (interactive)
6445   (if (not gnus-zombie-list)
6446       (gnus-message 6 "No zombie groups")
6447     (let (gnus-group-list-mode)
6448       (funcall gnus-group-prepare-function
6449                gnus-level-zombie t gnus-level-zombie))
6450     (goto-char (point-min)))
6451   (gnus-group-position-point))
6452
6453 (defun gnus-group-list-active ()
6454   "List all groups that are available from the server(s)."
6455   (interactive)
6456   ;; First we make sure that we have really read the active file.
6457   (unless (gnus-read-active-file-p)
6458     (let ((gnus-read-active-file t))
6459       (gnus-read-active-file)))
6460   ;; Find all groups and sort them.
6461   (let ((groups
6462          (sort
6463           (let (list)
6464             (mapatoms
6465              (lambda (sym)
6466                (and (boundp sym)
6467                     (symbol-value sym)
6468                     (setq list (cons (symbol-name sym) list))))
6469              gnus-active-hashtb)
6470             list)
6471           'string<))
6472         (buffer-read-only nil))
6473     (erase-buffer)
6474     (while groups
6475       (gnus-group-insert-group-line-info (pop groups)))
6476     (goto-char (point-min))))
6477
6478 (defun gnus-activate-all-groups (level)
6479   "Activate absolutely all groups."
6480   (interactive (list 7))
6481   (let ((gnus-activate-level level)
6482         (gnus-activate-foreign-newsgroups level))
6483     (gnus-group-get-new-news)))
6484
6485 (defun gnus-group-get-new-news (&optional arg)
6486   "Get newly arrived articles.
6487 If ARG is a number, it specifies which levels you are interested in
6488 re-scanning.  If ARG is non-nil and not a number, this will force
6489 \"hard\" re-reading of the active files from all servers."
6490   (interactive "P")
6491   (run-hooks 'gnus-get-new-news-hook)
6492   ;; We might read in new NoCeM messages here.
6493   (when (and gnus-use-nocem 
6494              (null arg))
6495     (gnus-nocem-scan-groups))
6496   ;; If ARG is not a number, then we read the active file.
6497   (when (and arg (not (numberp arg)))
6498     (let ((gnus-read-active-file t))
6499       (gnus-read-active-file))
6500     (setq arg nil))
6501
6502   (setq arg (gnus-group-default-level arg t))
6503   (if (and gnus-read-active-file (not arg))
6504       (progn
6505         (gnus-read-active-file)
6506         (gnus-get-unread-articles arg))
6507     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6508       (gnus-get-unread-articles arg)))
6509   (run-hooks 'gnus-after-getting-new-news-hook)
6510   (gnus-group-list-groups))
6511
6512 (defun gnus-group-get-new-news-this-group (&optional n)
6513   "Check for newly arrived news in the current group (and the N-1 next groups).
6514 The difference between N and the number of newsgroup checked is returned.
6515 If N is negative, this group and the N-1 previous groups will be checked."
6516   (interactive "P")
6517   (let* ((groups (gnus-group-process-prefix n))
6518          (ret (if (numberp n) (- n (length groups)) 0))
6519          (beg (unless n (point)))
6520          group)
6521     (while (setq group (pop groups))
6522       (gnus-group-remove-mark group)
6523       (if (gnus-activate-group group 'scan)
6524           (progn
6525             (gnus-get-unread-articles-in-group
6526              (gnus-get-info group) (gnus-active group) t)
6527             (unless (gnus-virtual-group-p group)
6528               (gnus-close-group group))
6529             (gnus-group-update-group group))
6530         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6531     (when beg (goto-char beg))
6532     (when gnus-goto-next-group-when-activating
6533       (gnus-group-next-unread-group 1 t))
6534     (gnus-summary-position-point)
6535     ret))
6536
6537 (defun gnus-group-fetch-faq (group &optional faq-dir)
6538   "Fetch the FAQ for the current group."
6539   (interactive
6540    (list
6541     (and (gnus-group-group-name)
6542          (gnus-group-real-name (gnus-group-group-name)))
6543     (cond (current-prefix-arg
6544            (completing-read
6545             "Faq dir: " (and (listp gnus-group-faq-directory)
6546                              (mapcar (lambda (file) (list file))
6547                                      gnus-group-faq-directory)))))))
6548   (or faq-dir
6549       (setq faq-dir (if (listp gnus-group-faq-directory)
6550                         (car gnus-group-faq-directory)
6551                       gnus-group-faq-directory)))
6552   (or group (error "No group name given"))
6553   (let ((file (concat (file-name-as-directory faq-dir)
6554                       (gnus-group-real-name group))))
6555     (if (not (file-exists-p file))
6556         (error "No such file: %s" file)
6557       (find-file file))))
6558
6559 (defun gnus-group-describe-group (force &optional group)
6560   "Display a description of the current newsgroup."
6561   (interactive (list current-prefix-arg (gnus-group-group-name)))
6562   (when (and force
6563              gnus-description-hashtb)
6564     (gnus-sethash group nil gnus-description-hashtb))
6565   (let ((method (gnus-find-method-for-group group))
6566         desc)
6567     (or group (error "No group name given"))
6568     (and (or (and gnus-description-hashtb
6569                   ;; We check whether this group's method has been
6570                   ;; queried for a description file.
6571                   (gnus-gethash
6572                    (gnus-group-prefixed-name "" method)
6573                    gnus-description-hashtb))
6574              (setq desc (gnus-group-get-description group))
6575              (gnus-read-descriptions-file method))
6576          (gnus-message 1
6577           (or desc (gnus-gethash group gnus-description-hashtb)
6578               "No description available")))))
6579
6580 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6581 (defun gnus-group-describe-all-groups (&optional force)
6582   "Pop up a buffer with descriptions of all newsgroups."
6583   (interactive "P")
6584   (and force (setq gnus-description-hashtb nil))
6585   (if (not (or gnus-description-hashtb
6586                (gnus-read-all-descriptions-files)))
6587       (error "Couldn't request descriptions file"))
6588   (let ((buffer-read-only nil)
6589         b)
6590     (erase-buffer)
6591     (mapatoms
6592      (lambda (group)
6593        (setq b (point))
6594        (insert (format "      *: %-20s %s\n" (symbol-name group)
6595                        (symbol-value group)))
6596        (gnus-add-text-properties
6597         b (1+ b) (list 'gnus-group group
6598                        'gnus-unread t 'gnus-marked nil
6599                        'gnus-level (1+ gnus-level-subscribed))))
6600      gnus-description-hashtb)
6601     (goto-char (point-min))
6602     (gnus-group-position-point)))
6603
6604 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6605 (defun gnus-group-apropos (regexp &optional search-description)
6606   "List all newsgroups that have names that match a regexp."
6607   (interactive "sGnus apropos (regexp): ")
6608   (let ((prev "")
6609         (obuf (current-buffer))
6610         groups des)
6611     ;; Go through all newsgroups that are known to Gnus.
6612     (mapatoms
6613      (lambda (group)
6614        (and (symbol-name group)
6615             (string-match regexp (symbol-name group))
6616             (setq groups (cons (symbol-name group) groups))))
6617      gnus-active-hashtb)
6618     ;; Also go through all descriptions that are known to Gnus.
6619     (when search-description
6620       (mapatoms
6621        (lambda (group)
6622          (and (string-match regexp (symbol-value group))
6623               (gnus-active (symbol-name group))
6624               (setq groups (cons (symbol-name group) groups))))
6625        gnus-description-hashtb))
6626     (if (not groups)
6627         (gnus-message 3 "No groups matched \"%s\"." regexp)
6628       ;; Print out all the groups.
6629       (save-excursion
6630         (pop-to-buffer "*Gnus Help*")
6631         (buffer-disable-undo (current-buffer))
6632         (erase-buffer)
6633         (setq groups (sort groups 'string<))
6634         (while groups
6635           ;; Groups may be entered twice into the list of groups.
6636           (if (not (string= (car groups) prev))
6637               (progn
6638                 (insert (setq prev (car groups)) "\n")
6639                 (if (and gnus-description-hashtb
6640                          (setq des (gnus-gethash (car groups)
6641                                                  gnus-description-hashtb)))
6642                     (insert "  " des "\n"))))
6643           (setq groups (cdr groups)))
6644         (goto-char (point-min))))
6645     (pop-to-buffer obuf)))
6646
6647 (defun gnus-group-description-apropos (regexp)
6648   "List all newsgroups that have names or descriptions that match a regexp."
6649   (interactive "sGnus description apropos (regexp): ")
6650   (if (not (or gnus-description-hashtb
6651                (gnus-read-all-descriptions-files)))
6652       (error "Couldn't request descriptions file"))
6653   (gnus-group-apropos regexp t))
6654
6655 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6656 (defun gnus-group-list-matching (level regexp &optional all lowest)
6657   "List all groups with unread articles that match REGEXP.
6658 If the prefix LEVEL is non-nil, it should be a number that says which
6659 level to cut off listing groups.
6660 If ALL, also list groups with no unread articles.
6661 If LOWEST, don't list groups with level lower than LOWEST.
6662
6663 This command may read the active file."
6664   (interactive "P\nsList newsgroups matching: ")
6665   ;; First make sure active file has been read.
6666   (when (and level
6667              (> (prefix-numeric-value level) gnus-level-killed))
6668     (gnus-get-killed-groups))
6669   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6670                            all (or lowest 1) regexp)
6671   (goto-char (point-min))
6672   (gnus-group-position-point))
6673
6674 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6675   "List all groups that match REGEXP.
6676 If the prefix LEVEL is non-nil, it should be a number that says which
6677 level to cut off listing groups.
6678 If LOWEST, don't list groups with level lower than LOWEST."
6679   (interactive "P\nsList newsgroups matching: ")
6680   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6681
6682 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6683 (defun gnus-group-save-newsrc (&optional force)
6684   "Save the Gnus startup files.
6685 If FORCE, force saving whether it is necessary or not."
6686   (interactive "P")
6687   (gnus-save-newsrc-file force))
6688
6689 (defun gnus-group-restart (&optional arg)
6690   "Force Gnus to read the .newsrc file."
6691   (interactive "P")
6692   (when (gnus-yes-or-no-p
6693          (format "Are you sure you want to read %s? "
6694                  gnus-current-startup-file))
6695     (gnus-save-newsrc-file)
6696     (gnus-setup-news 'force)
6697     (gnus-group-list-groups arg)))
6698
6699 (defun gnus-group-read-init-file ()
6700   "Read the Gnus elisp init file."
6701   (interactive)
6702   (gnus-read-init-file))
6703
6704 (defun gnus-group-check-bogus-groups (&optional silent)
6705   "Check bogus newsgroups.
6706 If given a prefix, don't ask for confirmation before removing a bogus
6707 group."
6708   (interactive "P")
6709   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6710   (gnus-group-list-groups))
6711
6712 (defun gnus-group-edit-global-kill (&optional article group)
6713   "Edit the global kill file.
6714 If GROUP, edit that local kill file instead."
6715   (interactive "P")
6716   (setq gnus-current-kill-article article)
6717   (gnus-kill-file-edit-file group)
6718   (gnus-message
6719    6
6720    (substitute-command-keys
6721     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6722             (if group "local" "global")))))
6723
6724 (defun gnus-group-edit-local-kill (article group)
6725   "Edit a local kill file."
6726   (interactive (list nil (gnus-group-group-name)))
6727   (gnus-group-edit-global-kill article group))
6728
6729 (defun gnus-group-force-update ()
6730   "Update `.newsrc' file."
6731   (interactive)
6732   (gnus-save-newsrc-file))
6733
6734 (defun gnus-group-suspend ()
6735   "Suspend the current Gnus session.
6736 In fact, cleanup buffers except for group mode buffer.
6737 The hook gnus-suspend-gnus-hook is called before actually suspending."
6738   (interactive)
6739   (run-hooks 'gnus-suspend-gnus-hook)
6740   ;; Kill Gnus buffers except for group mode buffer.
6741   (let* ((group-buf (get-buffer gnus-group-buffer))
6742          ;; Do this on a separate list in case the user does a ^G before we finish
6743          (gnus-buffer-list
6744           (delete group-buf (delete gnus-dribble-buffer
6745                                     (append gnus-buffer-list nil)))))
6746     (while gnus-buffer-list
6747       (gnus-kill-buffer (pop gnus-buffer-list)))
6748     (gnus-kill-gnus-frames)
6749     (when group-buf
6750       (setq gnus-buffer-list (list group-buf))
6751       (bury-buffer group-buf)
6752       (delete-windows-on group-buf t))))
6753
6754 (defun gnus-group-clear-dribble ()
6755   "Clear all information from the dribble buffer."
6756   (interactive)
6757   (gnus-dribble-clear)
6758   (gnus-message 7 "Cleared dribble buffer"))
6759
6760 (defun gnus-group-exit ()
6761   "Quit reading news after updating .newsrc.eld and .newsrc.
6762 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6763   (interactive)
6764   (when 
6765       (or noninteractive                ;For gnus-batch-kill
6766           (not gnus-interactive-exit)   ;Without confirmation
6767           gnus-expert-user
6768           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6769     (run-hooks 'gnus-exit-gnus-hook)
6770     ;; Offer to save data from non-quitted summary buffers.
6771     (gnus-offer-save-summaries)
6772     ;; Save the newsrc file(s).
6773     (gnus-save-newsrc-file)
6774     ;; Kill-em-all.
6775     (gnus-close-backends)
6776     ;; Reset everything.
6777     (gnus-clear-system)
6778     ;; Allow the user to do things after cleaning up.
6779     (run-hooks 'gnus-after-exiting-gnus-hook)))
6780
6781 (defun gnus-close-backends ()
6782   ;; Send a close request to all backends that support such a request.
6783   (let ((methods gnus-valid-select-methods)
6784         func)
6785     (while methods
6786       (if (fboundp (setq func (intern (concat (caar methods)
6787                                               "-request-close"))))
6788           (funcall func))
6789       (setq methods (cdr methods)))))
6790
6791 (defun gnus-group-quit ()
6792   "Quit reading news without updating .newsrc.eld or .newsrc.
6793 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6794   (interactive)
6795   (when (or noninteractive              ;For gnus-batch-kill
6796             (zerop (buffer-size))
6797             (not (gnus-server-opened gnus-select-method))
6798             gnus-expert-user
6799             (not gnus-current-startup-file)
6800             (gnus-yes-or-no-p
6801              (format "Quit reading news without saving %s? "
6802                      (file-name-nondirectory gnus-current-startup-file))))
6803     (run-hooks 'gnus-exit-gnus-hook)
6804     (if gnus-use-full-window
6805         (delete-other-windows)
6806       (gnus-remove-some-windows))
6807     (gnus-dribble-save)
6808     (gnus-close-backends)
6809     (gnus-clear-system)
6810     ;; Allow the user to do things after cleaning up.
6811     (run-hooks 'gnus-after-exiting-gnus-hook)))
6812
6813 (defun gnus-offer-save-summaries ()
6814   "Offer to save all active summary buffers."
6815   (save-excursion
6816     (let ((buflist (buffer-list))
6817           buffers bufname)
6818       ;; Go through all buffers and find all summaries.
6819       (while buflist
6820         (and (setq bufname (buffer-name (car buflist)))
6821              (string-match "Summary" bufname)
6822              (save-excursion
6823                (set-buffer bufname)
6824                ;; We check that this is, indeed, a summary buffer.
6825                (and (eq major-mode 'gnus-summary-mode)
6826                     ;; Also make sure this isn't bogus.
6827                     gnus-newsgroup-prepared))
6828              (push bufname buffers))
6829         (setq buflist (cdr buflist)))
6830       ;; Go through all these summary buffers and offer to save them.
6831       (when buffers
6832         (map-y-or-n-p
6833          "Update summary buffer %s? "
6834          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6835          buffers)))))
6836
6837 (defun gnus-group-describe-briefly ()
6838   "Give a one line description of the group mode commands."
6839   (interactive)
6840   (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")))
6841
6842 (defun gnus-group-browse-foreign-server (method)
6843   "Browse a foreign news server.
6844 If called interactively, this function will ask for a select method
6845  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6846 If not, METHOD should be a list where the first element is the method
6847 and the second element is the address."
6848   (interactive
6849    (list (let ((how (completing-read
6850                      "Which backend: "
6851                      (append gnus-valid-select-methods gnus-server-alist)
6852                      nil t (cons "nntp" 0) 'gnus-method-history)))
6853            ;; We either got a backend name or a virtual server name.
6854            ;; If the first, we also need an address.
6855            (if (assoc how gnus-valid-select-methods)
6856                (list (intern how)
6857                      ;; Suggested by mapjph@bath.ac.uk.
6858                      (completing-read
6859                       "Address: "
6860                       (mapcar (lambda (server) (list server))
6861                               gnus-secondary-servers)))
6862              ;; We got a server name, so we find the method.
6863              (gnus-server-to-method how)))))
6864   (gnus-browse-foreign-server method))
6865
6866 \f
6867 ;;;
6868 ;;; Gnus summary mode
6869 ;;;
6870
6871 (defvar gnus-summary-mode-map nil)
6872
6873 (put 'gnus-summary-mode 'mode-class 'special)
6874
6875 (unless gnus-summary-mode-map
6876   (setq gnus-summary-mode-map (make-keymap))
6877   (suppress-keymap gnus-summary-mode-map)
6878
6879   ;; Non-orthogonal keys
6880
6881   (gnus-define-keys gnus-summary-mode-map
6882     " " gnus-summary-next-page
6883     "\177" gnus-summary-prev-page
6884     [delete] gnus-summary-prev-page
6885     "\r" gnus-summary-scroll-up
6886     "n" gnus-summary-next-unread-article
6887     "p" gnus-summary-prev-unread-article
6888     "N" gnus-summary-next-article
6889     "P" gnus-summary-prev-article
6890     "\M-\C-n" gnus-summary-next-same-subject
6891     "\M-\C-p" gnus-summary-prev-same-subject
6892     "\M-n" gnus-summary-next-unread-subject
6893     "\M-p" gnus-summary-prev-unread-subject
6894     "." gnus-summary-first-unread-article
6895     "," gnus-summary-best-unread-article
6896     "\M-s" gnus-summary-search-article-forward
6897     "\M-r" gnus-summary-search-article-backward
6898     "<" gnus-summary-beginning-of-article
6899     ">" gnus-summary-end-of-article
6900     "j" gnus-summary-goto-article
6901     "^" gnus-summary-refer-parent-article
6902     "\M-^" gnus-summary-refer-article
6903     "u" gnus-summary-tick-article-forward
6904     "!" gnus-summary-tick-article-forward
6905     "U" gnus-summary-tick-article-backward
6906     "d" gnus-summary-mark-as-read-forward
6907     "D" gnus-summary-mark-as-read-backward
6908     "E" gnus-summary-mark-as-expirable
6909     "\M-u" gnus-summary-clear-mark-forward
6910     "\M-U" gnus-summary-clear-mark-backward
6911     "k" gnus-summary-kill-same-subject-and-select
6912     "\C-k" gnus-summary-kill-same-subject
6913     "\M-\C-k" gnus-summary-kill-thread
6914     "\M-\C-l" gnus-summary-lower-thread
6915     "e" gnus-summary-edit-article
6916     "#" gnus-summary-mark-as-processable
6917     "\M-#" gnus-summary-unmark-as-processable
6918     "\M-\C-t" gnus-summary-toggle-threads
6919     "\M-\C-s" gnus-summary-show-thread
6920     "\M-\C-h" gnus-summary-hide-thread
6921     "\M-\C-f" gnus-summary-next-thread
6922     "\M-\C-b" gnus-summary-prev-thread
6923     "\M-\C-u" gnus-summary-up-thread
6924     "\M-\C-d" gnus-summary-down-thread
6925     "&" gnus-summary-execute-command
6926     "c" gnus-summary-catchup-and-exit
6927     "\C-w" gnus-summary-mark-region-as-read
6928     "\C-t" gnus-summary-toggle-truncation
6929     "?" gnus-summary-mark-as-dormant
6930     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6931     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6932     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6933     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6934     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6935     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6936     "=" gnus-summary-expand-window
6937     "\C-x\C-s" gnus-summary-reselect-current-group
6938     "\M-g" gnus-summary-rescan-group
6939     "w" gnus-summary-stop-page-breaking
6940     "\C-c\C-r" gnus-summary-caesar-message
6941     "\M-t" gnus-summary-toggle-mime
6942     "f" gnus-summary-followup
6943     "F" gnus-summary-followup-with-original
6944     "C" gnus-summary-cancel-article
6945     "r" gnus-summary-reply
6946     "R" gnus-summary-reply-with-original
6947     "\C-c\C-f" gnus-summary-mail-forward
6948     "o" gnus-summary-save-article
6949     "\C-o" gnus-summary-save-article-mail
6950     "|" gnus-summary-pipe-output
6951     "\M-k" gnus-summary-edit-local-kill
6952     "\M-K" gnus-summary-edit-global-kill
6953     "V" gnus-version
6954     "\C-c\C-d" gnus-summary-describe-group
6955     "q" gnus-summary-exit
6956     "Q" gnus-summary-exit-no-update
6957     "\C-c\C-i" gnus-info-find-node
6958     gnus-mouse-2 gnus-mouse-pick-article
6959     "m" gnus-summary-mail-other-window
6960     "a" gnus-summary-post-news
6961     "x" gnus-summary-limit-to-unread
6962     "s" gnus-summary-isearch-article
6963     "t" gnus-article-hide-headers
6964     "g" gnus-summary-show-article
6965     "l" gnus-summary-goto-last-article
6966     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6967     "\C-d" gnus-summary-enter-digest-group
6968     "\C-c\C-b" gnus-bug
6969     "*" gnus-cache-enter-article
6970     "\M-*" gnus-cache-remove-article
6971     "\M-&" gnus-summary-universal-argument
6972     "\C-l" gnus-recenter
6973     "I" gnus-summary-increase-score
6974     "L" gnus-summary-lower-score
6975
6976     "V" gnus-summary-score-map
6977     "X" gnus-uu-extract-map
6978     "S" gnus-summary-send-map)
6979
6980   ;; Sort of orthogonal keymap
6981   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6982     "t" gnus-summary-tick-article-forward
6983     "!" gnus-summary-tick-article-forward
6984     "d" gnus-summary-mark-as-read-forward
6985     "r" gnus-summary-mark-as-read-forward
6986     "c" gnus-summary-clear-mark-forward
6987     " " gnus-summary-clear-mark-forward
6988     "e" gnus-summary-mark-as-expirable
6989     "x" gnus-summary-mark-as-expirable
6990     "?" gnus-summary-mark-as-dormant
6991     "b" gnus-summary-set-bookmark
6992     "B" gnus-summary-remove-bookmark
6993     "#" gnus-summary-mark-as-processable
6994     "\M-#" gnus-summary-unmark-as-processable
6995     "S" gnus-summary-limit-include-expunged
6996     "C" gnus-summary-catchup
6997     "H" gnus-summary-catchup-to-here
6998     "\C-c" gnus-summary-catchup-all
6999     "k" gnus-summary-kill-same-subject-and-select
7000     "K" gnus-summary-kill-same-subject
7001     "P" gnus-uu-mark-map)
7002
7003   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
7004     "c" gnus-summary-clear-above
7005     "u" gnus-summary-tick-above
7006     "m" gnus-summary-mark-above
7007     "k" gnus-summary-kill-below)
7008
7009   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
7010     "/" gnus-summary-limit-to-subject
7011     "n" gnus-summary-limit-to-articles
7012     "w" gnus-summary-pop-limit
7013     "s" gnus-summary-limit-to-subject
7014     "a" gnus-summary-limit-to-author
7015     "u" gnus-summary-limit-to-unread
7016     "m" gnus-summary-limit-to-marks
7017     "v" gnus-summary-limit-to-score
7018     "D" gnus-summary-limit-include-dormant
7019     "d" gnus-summary-limit-exclude-dormant
7020     ;;  "t" gnus-summary-limit-exclude-thread
7021     "E" gnus-summary-limit-include-expunged
7022     "c" gnus-summary-limit-exclude-childless-dormant
7023     "C" gnus-summary-limit-mark-excluded-as-read)
7024
7025   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
7026     "n" gnus-summary-next-unread-article
7027     "p" gnus-summary-prev-unread-article
7028     "N" gnus-summary-next-article
7029     "P" gnus-summary-prev-article
7030     "\C-n" gnus-summary-next-same-subject
7031     "\C-p" gnus-summary-prev-same-subject
7032     "\M-n" gnus-summary-next-unread-subject
7033     "\M-p" gnus-summary-prev-unread-subject
7034     "f" gnus-summary-first-unread-article
7035     "b" gnus-summary-best-unread-article
7036     "j" gnus-summary-goto-article
7037     "g" gnus-summary-goto-subject
7038     "l" gnus-summary-goto-last-article
7039     "p" gnus-summary-pop-article)
7040
7041   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
7042     "k" gnus-summary-kill-thread
7043     "l" gnus-summary-lower-thread
7044     "i" gnus-summary-raise-thread
7045     "T" gnus-summary-toggle-threads
7046     "t" gnus-summary-rethread-current
7047     "^" gnus-summary-reparent-thread
7048     "s" gnus-summary-show-thread
7049     "S" gnus-summary-show-all-threads
7050     "h" gnus-summary-hide-thread
7051     "H" gnus-summary-hide-all-threads
7052     "n" gnus-summary-next-thread
7053     "p" gnus-summary-prev-thread
7054     "u" gnus-summary-up-thread
7055     "o" gnus-summary-top-thread
7056     "d" gnus-summary-down-thread
7057     "#" gnus-uu-mark-thread
7058     "\M-#" gnus-uu-unmark-thread)
7059
7060   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
7061     "c" gnus-summary-catchup-and-exit
7062     "C" gnus-summary-catchup-all-and-exit
7063     "E" gnus-summary-exit-no-update
7064     "Q" gnus-summary-exit
7065     "Z" gnus-summary-exit
7066     "n" gnus-summary-catchup-and-goto-next-group
7067     "R" gnus-summary-reselect-current-group
7068     "G" gnus-summary-rescan-group
7069     "N" gnus-summary-next-group
7070     "P" gnus-summary-prev-group)
7071
7072   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
7073     " " gnus-summary-next-page
7074     "n" gnus-summary-next-page
7075     "\177" gnus-summary-prev-page
7076     [delete] gnus-summary-prev-page
7077     "p" gnus-summary-prev-page
7078     "\r" gnus-summary-scroll-up
7079     "<" gnus-summary-beginning-of-article
7080     ">" gnus-summary-end-of-article
7081     "b" gnus-summary-beginning-of-article
7082     "e" gnus-summary-end-of-article
7083     "^" gnus-summary-refer-parent-article
7084     "r" gnus-summary-refer-parent-article
7085     "R" gnus-summary-refer-references
7086     "g" gnus-summary-show-article
7087     "s" gnus-summary-isearch-article)
7088
7089   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
7090     "b" gnus-article-add-buttons
7091     "B" gnus-article-add-buttons-to-head
7092     "o" gnus-article-treat-overstrike
7093     ;;  "w" gnus-article-word-wrap
7094     "w" gnus-article-fill-cited-article
7095     "c" gnus-article-remove-cr
7096     "L" gnus-article-remove-trailing-blank-lines
7097     "q" gnus-article-de-quoted-unreadable
7098     "f" gnus-article-display-x-face
7099     "l" gnus-summary-stop-page-breaking
7100     "r" gnus-summary-caesar-message
7101     "t" gnus-article-hide-headers
7102     "v" gnus-summary-verbose-headers
7103     "m" gnus-summary-toggle-mime)
7104
7105   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
7106     "a" gnus-article-hide
7107     "h" gnus-article-hide-headers
7108     "b" gnus-article-hide-boring-headers
7109     "s" gnus-article-hide-signature
7110     "c" gnus-article-hide-citation
7111     "p" gnus-article-hide-pgp
7112     "\C-c" gnus-article-hide-citation-maybe)
7113
7114   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
7115     "a" gnus-article-highlight
7116     "h" gnus-article-highlight-headers
7117     "c" gnus-article-highlight-citation
7118     "s" gnus-article-highlight-signature)
7119
7120   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
7121     "z" gnus-article-date-ut
7122     "u" gnus-article-date-ut
7123     "l" gnus-article-date-local
7124     "e" gnus-article-date-lapsed
7125     "o" gnus-article-date-original)
7126
7127   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7128     "v" gnus-version
7129     "f" gnus-summary-fetch-faq
7130     "d" gnus-summary-describe-group
7131     "h" gnus-summary-describe-briefly
7132     "i" gnus-info-find-node)
7133
7134   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7135     "e" gnus-summary-expire-articles
7136     "\M-\C-e" gnus-summary-expire-articles-now
7137     "\177" gnus-summary-delete-article
7138     [delete] gnus-summary-delete-article
7139     "m" gnus-summary-move-article
7140     "r" gnus-summary-respool-article
7141     "w" gnus-summary-edit-article
7142     "c" gnus-summary-copy-article
7143     "B" gnus-summary-crosspost-article
7144     "q" gnus-summary-respool-query
7145     "i" gnus-summary-import-article)
7146
7147   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7148     "o" gnus-summary-save-article
7149     "m" gnus-summary-save-article-mail
7150     "r" gnus-summary-save-article-rmail
7151     "f" gnus-summary-save-article-file
7152     "b" gnus-summary-save-article-body-file
7153     "h" gnus-summary-save-article-folder
7154     "v" gnus-summary-save-article-vm
7155     "p" gnus-summary-pipe-output
7156     "s" gnus-soup-add-article)
7157   )
7158
7159 \f
7160
7161 (defun gnus-summary-mode (&optional group)
7162   "Major mode for reading articles.
7163
7164 All normal editing commands are switched off.
7165 \\<gnus-summary-mode-map>
7166 Each line in this buffer represents one article.  To read an
7167 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7168 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7169 respectively.
7170
7171 You can also post articles and send mail from this buffer.  To
7172 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7173 of an article, type `\\[gnus-summary-reply]'.
7174
7175 There are approx. one gazillion commands you can execute in this
7176 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7177
7178 The following commands are available:
7179
7180 \\{gnus-summary-mode-map}"
7181   (interactive)
7182   (when (and menu-bar-mode
7183              (gnus-visual-p 'summary-menu 'menu))
7184     (gnus-summary-make-menu-bar))
7185   (kill-all-local-variables)
7186   (gnus-summary-make-local-variables)
7187   (gnus-make-thread-indent-array)
7188   (gnus-simplify-mode-line)
7189   (setq major-mode 'gnus-summary-mode)
7190   (setq mode-name "Summary")
7191   (make-local-variable 'minor-mode-alist)
7192   (use-local-map gnus-summary-mode-map)
7193   (buffer-disable-undo (current-buffer))
7194   (setq buffer-read-only t)             ;Disable modification
7195   (setq truncate-lines t)
7196   (setq selective-display t)
7197   (setq selective-display-ellipses t)   ;Display `...'
7198   (setq buffer-display-table gnus-summary-display-table)
7199   (setq gnus-newsgroup-name group)
7200   (make-local-variable 'gnus-summary-line-format)
7201   (make-local-variable 'gnus-summary-line-format-spec)
7202   (make-local-variable 'gnus-summary-mark-positions)
7203   (run-hooks 'gnus-summary-mode-hook))
7204
7205 (defun gnus-summary-make-local-variables ()
7206   "Make all the local summary buffer variables."
7207   (let ((locals gnus-summary-local-variables)
7208         global local)
7209     (while (setq local (pop locals))
7210       (if (consp local)
7211           (progn
7212             (if (eq (cdr local) 'global)
7213                 ;; Copy the global value of the variable.
7214                 (setq global (symbol-value (car local)))
7215               ;; Use the value from the list.
7216               (setq global (eval (cdr local))))
7217             (make-local-variable (car local))
7218             (set (car local) global))
7219         ;; Simple nil-valued local variable.
7220         (make-local-variable local)
7221         (set local nil)))))
7222
7223 (defun gnus-summary-make-display-table ()
7224   ;; Change the display table.  Odd characters have a tendency to mess
7225   ;; up nicely formatted displays - we make all possible glyphs
7226   ;; display only a single character.
7227
7228   ;; We start from the standard display table, if any.
7229   (setq gnus-summary-display-table
7230         (or (copy-sequence standard-display-table)
7231             (make-display-table)))
7232   ;; Nix out all the control chars...
7233   (let ((i 32))
7234     (while (>= (setq i (1- i)) 0)
7235       (aset gnus-summary-display-table i [??])))
7236   ;; ... but not newline and cr, of course. (cr is necessary for the
7237   ;; selective display).
7238   (aset gnus-summary-display-table ?\n nil)
7239   (aset gnus-summary-display-table ?\r nil)
7240   ;; We nix out any glyphs over 126 that are not set already.
7241   (let ((i 256))
7242     (while (>= (setq i (1- i)) 127)
7243       ;; Only modify if the entry is nil.
7244       (or (aref gnus-summary-display-table i)
7245           (aset gnus-summary-display-table i [??])))))
7246
7247 (defun gnus-summary-clear-local-variables ()
7248   (let ((locals gnus-summary-local-variables))
7249     (while locals
7250       (if (consp (car locals))
7251           (and (vectorp (caar locals))
7252                (set (caar locals) nil))
7253         (and (vectorp (car locals))
7254              (set (car locals) nil)))
7255       (setq locals (cdr locals)))))
7256
7257 ;; Summary data functions.
7258
7259 (defmacro gnus-data-number (data)
7260   `(car ,data))
7261
7262 (defmacro gnus-data-set-number (data number)
7263   `(setcar ,data ,number))
7264
7265 (defmacro gnus-data-mark (data)
7266   `(nth 1 ,data))
7267
7268 (defmacro gnus-data-set-mark (data mark)
7269   `(setcar (nthcdr 1 ,data) ,mark))
7270
7271 (defmacro gnus-data-pos (data)
7272   `(nth 2 ,data))
7273
7274 (defmacro gnus-data-set-pos (data pos)
7275   `(setcar (nthcdr 2 ,data) ,pos))
7276
7277 (defmacro gnus-data-header (data)
7278   `(nth 3 ,data))
7279
7280 (defmacro gnus-data-level (data)
7281   `(nth 4 ,data))
7282
7283 (defmacro gnus-data-unread-p (data)
7284   `(= (nth 1 ,data) gnus-unread-mark))
7285
7286 (defmacro gnus-data-pseudo-p (data)
7287   `(consp (nth 3 ,data)))
7288
7289 (defmacro gnus-data-find (number)
7290   `(assq ,number gnus-newsgroup-data))
7291
7292 (defmacro gnus-data-find-list (number &optional data)
7293   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7294      (memq (assq ,number bdata)
7295            bdata)))
7296
7297 (defmacro gnus-data-make (number mark pos header level)
7298   `(list ,number ,mark ,pos ,header ,level))
7299
7300 (defun gnus-data-enter (after-article number mark pos header level offset)
7301   (let ((data (gnus-data-find-list after-article)))
7302     (or data (error "No such article: %d" after-article))
7303     (setcdr data (cons (gnus-data-make number mark pos header level)
7304                        (cdr data)))
7305     (setq gnus-newsgroup-data-reverse nil)
7306     (gnus-data-update-list (cddr data) offset)))
7307
7308 (defun gnus-data-enter-list (after-article list &optional offset)
7309   (when list
7310     (let ((data (and after-article (gnus-data-find-list after-article)))
7311           (ilist list))
7312       (or data (not after-article) (error "No such article: %d" after-article))
7313       ;; Find the last element in the list to be spliced into the main
7314       ;; list.
7315       (while (cdr list)
7316         (setq list (cdr list)))
7317       (if (not data)
7318           (progn
7319             (setcdr list gnus-newsgroup-data)
7320             (setq gnus-newsgroup-data ilist)
7321             (and offset (gnus-data-update-list (cdr list) offset)))
7322         (setcdr list (cdr data))
7323         (setcdr data ilist)
7324         (and offset (gnus-data-update-list (cdr data) offset)))
7325       (setq gnus-newsgroup-data-reverse nil))))
7326
7327 (defun gnus-data-remove (article &optional offset)
7328   (let ((data gnus-newsgroup-data))
7329     (if (= (gnus-data-number (car data)) article)
7330         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7331               gnus-newsgroup-data-reverse nil)
7332       (while (cdr data)
7333         (and (= (gnus-data-number (cadr data)) article)
7334              (progn
7335                (setcdr data (cddr data))
7336                (and offset (gnus-data-update-list (cdr data) offset))
7337                (setq data nil
7338                      gnus-newsgroup-data-reverse nil)))
7339         (setq data (cdr data))))))
7340
7341 (defmacro gnus-data-list (backward)
7342   `(if ,backward
7343        (or gnus-newsgroup-data-reverse
7344            (setq gnus-newsgroup-data-reverse
7345                  (reverse gnus-newsgroup-data)))
7346      gnus-newsgroup-data))
7347
7348 (defun gnus-data-update-list (data offset)
7349   "Add OFFSET to the POS of all data entries in DATA."
7350   (while data
7351     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7352     (setq data (cdr data))))
7353
7354 (defun gnus-data-compute-positions ()
7355   "Compute the positions of all articles."
7356   (let ((data gnus-newsgroup-data)
7357         pos)
7358     (while data
7359       (when (setq pos (text-property-any
7360                        (point-min) (point-max)
7361                        'gnus-number (gnus-data-number (car data))))
7362         (gnus-data-set-pos (car data) (+ pos 3)))
7363       (setq data (cdr data)))))
7364
7365 (defun gnus-summary-article-pseudo-p (article)
7366   "Say whether this article is a pseudo article or not."
7367   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7368
7369 (defun gnus-article-parent-p (number)
7370   "Say whether this article is a parent or not."
7371   (let ((data (gnus-data-find-list number)))
7372     (and (cdr data)                     ; There has to be an article after...
7373          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7374             (gnus-data-level (nth 1 data))))))
7375
7376 (defun gnus-article-children (number)
7377   "Return a list of all children to NUMBER."
7378   (let* ((data (gnus-data-find-list number))
7379          (level (gnus-data-level (car data)))
7380          children)
7381     (setq data (cdr data))
7382     (while (and data            
7383                 (= (gnus-data-level (car data)) (1+ level)))
7384       (push (gnus-data-number (car data)) children)
7385       (setq data (cdr data)))
7386     children))
7387
7388 (defmacro gnus-summary-skip-intangible ()
7389   "If the current article is intangible, then jump to a different article."
7390   '(let ((to (get-text-property (point) 'gnus-intangible)))
7391     (and to (gnus-summary-goto-subject to))))
7392
7393 (defmacro gnus-summary-article-intangible-p ()
7394   "Say whether this article is intangible or not."
7395   '(get-text-property (point) 'gnus-intangible))
7396
7397 ;; Some summary mode macros.
7398
7399 (defmacro gnus-summary-article-number ()
7400   "The article number of the article on the current line.
7401 If there isn's an article number here, then we return the current
7402 article number."
7403   '(progn
7404      (gnus-summary-skip-intangible)
7405      (or (get-text-property (point) 'gnus-number)
7406          (gnus-summary-last-subject))))
7407
7408 (defmacro gnus-summary-article-header (&optional number)
7409   `(gnus-data-header (gnus-data-find
7410                       ,(or number '(gnus-summary-article-number)))))
7411
7412 (defmacro gnus-summary-thread-level (&optional number)
7413   `(if (and (eq gnus-summary-make-false-root 'dummy)
7414             (get-text-property (point) 'gnus-intangible))
7415        0
7416      (gnus-data-level (gnus-data-find
7417                        ,(or number '(gnus-summary-article-number))))))
7418
7419 (defmacro gnus-summary-article-mark (&optional number)
7420   `(gnus-data-mark (gnus-data-find
7421                     ,(or number '(gnus-summary-article-number)))))
7422
7423 (defmacro gnus-summary-article-pos (&optional number)
7424   `(gnus-data-pos (gnus-data-find
7425                    ,(or number '(gnus-summary-article-number)))))
7426
7427 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7428 (defmacro gnus-summary-article-subject (&optional number)
7429   "Return current subject string or nil if nothing."
7430   `(let ((headers
7431           ,(if number
7432                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7433              '(gnus-data-header (assq (gnus-summary-article-number)
7434                                       gnus-newsgroup-data)))))
7435      (and headers
7436           (vectorp headers)
7437           (mail-header-subject headers))))
7438
7439 (defmacro gnus-summary-article-score (&optional number)
7440   "Return current article score."
7441   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7442                   gnus-newsgroup-scored))
7443        gnus-summary-default-score 0))
7444
7445 (defun gnus-summary-article-children (&optional number)
7446   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7447          (level (gnus-data-level (car data)))
7448          l children)
7449     (while (and (setq data (cdr data))
7450                 (> (setq l (gnus-data-level (car data))) level))
7451       (and (= (1+ level) l)
7452            (setq children (cons (gnus-data-number (car data))
7453                                 children))))
7454     (nreverse children)))
7455
7456 (defun gnus-summary-article-parent (&optional number)
7457   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7458                                     (gnus-data-list t)))
7459          (level (gnus-data-level (car data))))
7460     (if (zerop level)
7461         () ; This is a root.
7462       ;; We search until we find an article with a level less than
7463       ;; this one.  That function has to be the parent.
7464       (while (and (setq data (cdr data))
7465                   (not (< (gnus-data-level (car data)) level))))
7466       (and data (gnus-data-number (car data))))))
7467
7468 (defun gnus-unread-mark-p (mark)
7469   "Say whether MARK is the unread mark."
7470   (= mark gnus-unread-mark))
7471
7472 (defun gnus-read-mark-p (mark)
7473   "Say whether MARK is one of the marks that mark as read.
7474 This is all marks except unread, ticked, dormant, and expirable."
7475   (not (or (= mark gnus-unread-mark)
7476            (= mark gnus-ticked-mark)
7477            (= mark gnus-dormant-mark)
7478            (= mark gnus-expirable-mark))))
7479
7480 ;; Saving hidden threads.
7481
7482 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
7483 (put 'gnus-save-hidden-threads 'lisp-indent-hook 0)
7484 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
7485
7486 (defmacro gnus-save-hidden-threads (&rest forms)
7487   "Save hidden threads, eval FORMS, and restore the hidden threads."
7488   (let ((config (make-symbol "config")))
7489     `(let ((,config (gnus-hidden-threads-configuration)))
7490        (unwind-protect
7491            (progn
7492              ,@forms)
7493          (gnus-restore-hidden-threads-configuration ,config)))))
7494
7495 (defun gnus-hidden-threads-configuration ()
7496   "Return the current hidden threads configuration."
7497   (save-excursion
7498     (let (config)
7499       (goto-char (point-min))
7500       (while (search-forward "\r" nil t)
7501         (push (1- (point)) config))
7502       config)))
7503
7504 (defun gnus-restore-hidden-threads-configuration (config)
7505   "Restore hidden threads configuration from CONFIG."
7506   (let (point buffer-read-only)
7507     (while (setq point (pop config))
7508       (when (and (< point (point-max))
7509                  (goto-char point)
7510                  (= (following-char) ?\n))
7511         (subst-char-in-region point (1+ point) ?\n ?\r)))))
7512
7513 ;; Various summary mode internalish functions.
7514
7515 (defun gnus-mouse-pick-article (e)
7516   (interactive "e")
7517   (mouse-set-point e)
7518   (gnus-summary-next-page nil t))
7519
7520 (defun gnus-summary-setup-buffer (group)
7521   "Initialize summary buffer."
7522   (let ((buffer (concat "*Summary " group "*")))
7523     (if (get-buffer buffer)
7524         (progn
7525           (set-buffer buffer)
7526           (setq gnus-summary-buffer (current-buffer))
7527           (not gnus-newsgroup-prepared))
7528       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7529       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7530       (gnus-add-current-to-buffer-list)
7531       (gnus-summary-mode group)
7532       (when gnus-carpal
7533         (gnus-carpal-setup-buffer 'summary))
7534       (unless gnus-single-article-buffer
7535         (make-local-variable 'gnus-article-buffer)
7536         (make-local-variable 'gnus-article-current)
7537         (make-local-variable 'gnus-original-article-buffer))
7538       (setq gnus-newsgroup-name group)
7539       t)))
7540
7541 (defun gnus-set-global-variables ()
7542   ;; Set the global equivalents of the summary buffer-local variables
7543   ;; to the latest values they had.  These reflect the summary buffer
7544   ;; that was in action when the last article was fetched.
7545   (when (eq major-mode 'gnus-summary-mode)
7546     (setq gnus-summary-buffer (current-buffer))
7547     (let ((name gnus-newsgroup-name)
7548           (marked gnus-newsgroup-marked)
7549           (unread gnus-newsgroup-unreads)
7550           (headers gnus-current-headers)
7551           (data gnus-newsgroup-data)
7552           (summary gnus-summary-buffer)
7553           (article-buffer gnus-article-buffer)
7554           (original gnus-original-article-buffer)
7555           (gac gnus-article-current)
7556           (score-file gnus-current-score-file))
7557       (save-excursion
7558         (set-buffer gnus-group-buffer)
7559         (setq gnus-newsgroup-name name)
7560         (setq gnus-newsgroup-marked marked)
7561         (setq gnus-newsgroup-unreads unread)
7562         (setq gnus-current-headers headers)
7563         (setq gnus-newsgroup-data data)
7564         (setq gnus-article-current gac)
7565         (setq gnus-summary-buffer summary)
7566         (setq gnus-article-buffer article-buffer)
7567         (setq gnus-original-article-buffer original)
7568         (setq gnus-current-score-file score-file)))))
7569
7570 (defun gnus-summary-last-article-p (&optional article)
7571   "Return whether ARTICLE is the last article in the buffer."
7572   (if (not (setq article (or article (gnus-summary-article-number))))
7573       t ; All non-existant numbers are the last article. :-)
7574     (not (cdr (gnus-data-find-list article)))))
7575
7576 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7577   "Insert a dummy root in the summary buffer."
7578   (beginning-of-line)
7579   (gnus-add-text-properties
7580    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7581    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7582
7583 (defun gnus-make-thread-indent-array ()
7584   (let ((n 200))
7585     (unless (and gnus-thread-indent-array
7586                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7587       (setq gnus-thread-indent-array (make-vector 201 "")
7588             gnus-thread-indent-array-level gnus-thread-indent-level)
7589       (while (>= n 0)
7590         (aset gnus-thread-indent-array n
7591               (make-string (* n gnus-thread-indent-level) ? ))
7592         (setq n (1- n))))))
7593
7594 (defun gnus-summary-insert-line
7595   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7596                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7597                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7598   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7599          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7600          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7601          (gnus-tmp-score-char
7602           (if (or (null gnus-summary-default-score)
7603                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7604                       gnus-summary-zcore-fuzz)) ? 
7605             (if (< gnus-tmp-score gnus-summary-default-score)
7606                 gnus-score-below-mark gnus-score-over-mark)))
7607          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7608                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7609                                   gnus-cached-mark)
7610                                  (gnus-tmp-replied gnus-replied-mark)
7611                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7612                                   gnus-saved-mark)
7613                                  (t gnus-unread-mark)))
7614          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7615          (gnus-tmp-name
7616           (cond
7617            ((string-match "(.+)" gnus-tmp-from)
7618             (substring gnus-tmp-from
7619                        (1+ (match-beginning 0)) (1- (match-end 0))))
7620            ((string-match "<[^>]+> *$" gnus-tmp-from)
7621             (let ((beg (match-beginning 0)))
7622               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7623                        (substring gnus-tmp-from (1+ (match-beginning 0))
7624                                   (1- (match-end 0))))
7625                   (substring gnus-tmp-from 0 beg))))
7626            (t gnus-tmp-from)))
7627          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7628          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7629          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7630          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7631          (buffer-read-only nil))
7632     (when (string= gnus-tmp-name "")
7633       (setq gnus-tmp-name gnus-tmp-from))
7634     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7635     (gnus-put-text-property
7636      (point)
7637      (progn (eval gnus-summary-line-format-spec) (point))
7638      'gnus-number gnus-tmp-number)
7639     (when (gnus-visual-p 'summary-highlight 'highlight)
7640       (forward-line -1)
7641       (run-hooks 'gnus-summary-update-hook)
7642       (forward-line 1))))
7643
7644 (defun gnus-summary-update-line (&optional dont-update)
7645   ;; Update summary line after change.
7646   (when (and gnus-summary-default-score
7647              (not gnus-summary-inhibit-highlight))
7648     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7649            (article (gnus-summary-article-number))
7650            (score (gnus-summary-article-score article)))
7651       (unless dont-update
7652         (if (and gnus-summary-mark-below
7653                  (< (gnus-summary-article-score)
7654                     gnus-summary-mark-below))
7655             ;; This article has a low score, so we mark it as read.
7656             (when (memq article gnus-newsgroup-unreads)
7657               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7658           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7659             ;; This article was previously marked as read on account
7660             ;; of a low score, but now it has risen, so we mark it as
7661             ;; unread.
7662             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7663         (gnus-summary-update-mark
7664          (if (or (null gnus-summary-default-score)
7665                  (<= (abs (- score gnus-summary-default-score))
7666                      gnus-summary-zcore-fuzz)) ? 
7667            (if (< score gnus-summary-default-score)
7668                gnus-score-below-mark gnus-score-over-mark)) 'score))
7669       ;; Do visual highlighting.
7670       (when (gnus-visual-p 'summary-highlight 'highlight)
7671         (run-hooks 'gnus-summary-update-hook)))))
7672
7673 (defvar gnus-tmp-new-adopts nil)
7674
7675 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7676   ;; Sum up all elements (and sub-elements) in a list.
7677   (let* ((number
7678           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7679           (cond
7680            ((and (consp thread) (cdr thread))
7681             (apply
7682              '+ 1 (mapcar
7683                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7684            ((null thread)
7685             1)
7686            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7687             1)
7688            (t 0))))
7689     (when (and level (zerop level) gnus-tmp-new-adopts)
7690       (incf number
7691             (apply '+ (mapcar
7692                        'gnus-summary-number-of-articles-in-thread
7693                        gnus-tmp-new-adopts))))
7694     (if char
7695         (if (> number 1) gnus-not-empty-thread-mark
7696           gnus-empty-thread-mark)
7697       number)))
7698
7699 (defun gnus-summary-set-local-parameters (group)
7700  "Go through the local params of GROUP and set all variable specs in that list."
7701   (let ((params (gnus-info-params (gnus-get-info group)))
7702         elem)
7703     (while params
7704       (setq elem (car params)
7705             params (cdr params))
7706       (and (consp elem)                 ; Has to be a cons.
7707            (consp (cdr elem))           ; The cdr has to be a list.
7708            (symbolp (car elem))         ; Has to be a symbol in there.
7709            (not (memq (car elem) 
7710                       '(quit-config to-address to-list to-group)))
7711            (progn                       ; So we set it.
7712              (make-local-variable (car elem))
7713              (set (car elem) (eval (nth 1 elem))))))))
7714
7715 (defun gnus-summary-read-group (group &optional show-all no-article
7716                                       kill-buffer no-display)
7717   "Start reading news in newsgroup GROUP.
7718 If SHOW-ALL is non-nil, already read articles are also listed.
7719 If NO-ARTICLE is non-nil, no article is selected initially.
7720 If NO-DISPLAY, don't generate a summary buffer."
7721   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7722   (let* ((new-group (gnus-summary-setup-buffer group))
7723          (quit-config (gnus-group-quit-config group))
7724          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7725     (cond
7726      ;; This summary buffer exists already, so we just select it.
7727      ((not new-group)
7728       (gnus-set-global-variables)
7729       (when kill-buffer
7730         (gnus-kill-or-deaden-summary kill-buffer))
7731       (gnus-configure-windows 'summary 'force)
7732       (gnus-set-mode-line 'summary)
7733       (gnus-summary-position-point)
7734       (message "")
7735       t)
7736      ;; We couldn't select this group.
7737      ((null did-select)
7738       (when (and (eq major-mode 'gnus-summary-mode)
7739                  (not (equal (current-buffer) kill-buffer)))
7740         (kill-buffer (current-buffer))
7741         (if (not quit-config)
7742             (progn
7743               (set-buffer gnus-group-buffer)
7744               (gnus-group-jump-to-group group)
7745               (gnus-group-next-unread-group 1))
7746           (if (not (buffer-name (car quit-config)))
7747               (gnus-configure-windows 'group 'force)
7748             (set-buffer (car quit-config))
7749             (and (eq major-mode 'gnus-summary-mode)
7750                  (gnus-set-global-variables))
7751             (gnus-configure-windows (cdr quit-config)))))
7752       (gnus-message 3 "Can't select group")
7753       nil)
7754      ;; The user did a `C-g' while prompting for number of articles,
7755      ;; so we exit this group.
7756      ((eq did-select 'quit)
7757       (and (eq major-mode 'gnus-summary-mode)
7758            (not (equal (current-buffer) kill-buffer))
7759            (kill-buffer (current-buffer)))
7760       (when kill-buffer
7761         (gnus-kill-or-deaden-summary kill-buffer))
7762       (if (not quit-config)
7763           (progn
7764             (set-buffer gnus-group-buffer)
7765             (gnus-group-jump-to-group group)
7766             (gnus-group-next-unread-group 1)
7767             (gnus-configure-windows 'group 'force))
7768         (if (not (buffer-name (car quit-config)))
7769             (gnus-configure-windows 'group 'force)
7770           (set-buffer (car quit-config))
7771           (and (eq major-mode 'gnus-summary-mode)
7772                (gnus-set-global-variables))
7773           (gnus-configure-windows (cdr quit-config))))
7774       ;; Finally signal the quit.
7775       (signal 'quit nil))
7776      ;; The group was successfully selected.
7777      (t
7778       (gnus-set-global-variables)
7779       ;; Save the active value in effect when the group was entered.
7780       (setq gnus-newsgroup-active
7781             (gnus-copy-sequence
7782              (gnus-active gnus-newsgroup-name)))
7783       ;; You can change the summary buffer in some way with this hook.
7784       (run-hooks 'gnus-select-group-hook)
7785       ;; Set any local variables in the group parameters.
7786       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7787       (gnus-update-format-specifications)
7788       ;; Do score processing.
7789       (when gnus-use-scoring
7790         (gnus-possibly-score-headers))
7791       ;; Check whether to fill in the gaps in the threads.
7792       (when gnus-build-sparse-threads
7793         (gnus-build-sparse-threads))
7794       ;; Find the initial limit.
7795       (if gnus-show-threads
7796           (if show-all
7797               (let ((gnus-newsgroup-dormant nil))
7798                 (gnus-summary-initial-limit show-all))
7799             (gnus-summary-initial-limit show-all))
7800         (setq gnus-newsgroup-limit 
7801               (mapcar 
7802                (lambda (header) (mail-header-number header))
7803                gnus-newsgroup-headers)))
7804       ;; Generate the summary buffer.
7805       (unless no-display
7806         (gnus-summary-prepare))
7807       (when gnus-use-trees
7808         (gnus-tree-open group)
7809         (setq gnus-summary-highlight-line-function
7810               'gnus-tree-highlight-article))
7811       ;; If the summary buffer is empty, but there are some low-scored
7812       ;; articles or some excluded dormants, we include these in the
7813       ;; buffer.
7814       (when (and (zerop (buffer-size))
7815                  (not no-display))
7816         (cond (gnus-newsgroup-dormant
7817                (gnus-summary-limit-include-dormant))
7818               ((and gnus-newsgroup-scored show-all)
7819                (gnus-summary-limit-include-expunged))))
7820       ;; Function `gnus-apply-kill-file' must be called in this hook.
7821       (run-hooks 'gnus-apply-kill-hook)
7822       (if (and (zerop (buffer-size))
7823                (not no-display))
7824           (progn
7825             ;; This newsgroup is empty.
7826             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7827             (gnus-message 6 "No unread news")
7828             (when kill-buffer
7829               (gnus-kill-or-deaden-summary kill-buffer))
7830             ;; Return nil from this function.
7831             nil)
7832         ;; Hide conversation thread subtrees.  We cannot do this in
7833         ;; gnus-summary-prepare-hook since kill processing may not
7834         ;; work with hidden articles.
7835         (and gnus-show-threads
7836              gnus-thread-hide-subtree
7837              (gnus-summary-hide-all-threads))
7838         ;; Show first unread article if requested.
7839         (if (and (not no-article)
7840                  (not no-display)
7841                  gnus-newsgroup-unreads
7842                  gnus-auto-select-first)
7843             (unless (if (eq gnus-auto-select-first 'best)
7844                         (gnus-summary-best-unread-article)
7845                       (gnus-summary-first-unread-article))
7846               (gnus-configure-windows 'summary))
7847           ;; Don't select any articles, just move point to the first
7848           ;; article in the group.
7849           (goto-char (point-min))
7850           (gnus-summary-position-point)
7851           (gnus-set-mode-line 'summary)
7852           (gnus-configure-windows 'summary 'force))
7853         ;; If we are in async mode, we send some info to the backend.
7854         (when gnus-newsgroup-async
7855           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7856         (when kill-buffer
7857           (gnus-kill-or-deaden-summary kill-buffer))
7858         (when (get-buffer-window gnus-group-buffer t)
7859           ;; Gotta use windows, because recenter does wierd stuff if
7860           ;; the current buffer ain't the displayed window.
7861           (let ((owin (selected-window)))
7862             (select-window (get-buffer-window gnus-group-buffer t))
7863             (when (gnus-group-goto-group group)
7864               (recenter))
7865             (select-window owin))))
7866       ;; Mark this buffer as "prepared".
7867       (setq gnus-newsgroup-prepared t)
7868       t))))
7869
7870 (defun gnus-summary-prepare ()
7871   "Generate the summary buffer."
7872   (let ((buffer-read-only nil))
7873     (erase-buffer)
7874     (setq gnus-newsgroup-data nil
7875           gnus-newsgroup-data-reverse nil)
7876     (run-hooks 'gnus-summary-generate-hook)
7877     ;; Generate the buffer, either with threads or without.
7878     (when gnus-newsgroup-headers
7879       (gnus-summary-prepare-threads
7880        (if gnus-show-threads
7881            (gnus-sort-gathered-threads
7882             (funcall gnus-summary-thread-gathering-function
7883                      (gnus-sort-threads
7884                       (gnus-cut-threads (gnus-make-threads)))))
7885          ;; Unthreaded display.
7886          (gnus-sort-articles gnus-newsgroup-headers))))
7887     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7888     ;; Call hooks for modifying summary buffer.
7889     (goto-char (point-min))
7890     (run-hooks 'gnus-summary-prepare-hook)))
7891
7892 (defun gnus-gather-threads-by-subject (threads)
7893   "Gather threads by looking at Subject headers."
7894   (if (not gnus-summary-make-false-root)
7895       threads
7896     (let ((hashtb (gnus-make-hashtable 1023))
7897           (prev threads)
7898           (result threads)
7899           subject hthread whole-subject)
7900       (while threads
7901         (setq whole-subject (mail-header-subject (caar threads)))
7902         (setq subject
7903               (cond
7904                ;; Truncate the subject.
7905                ((numberp gnus-summary-gather-subject-limit)
7906                 (setq subject (gnus-simplify-subject-re whole-subject))
7907                 (if (> (length subject) gnus-summary-gather-subject-limit)
7908                     (substring subject 0 gnus-summary-gather-subject-limit)
7909                   subject))
7910                ;; Fuzzily simplify it.
7911                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7912                 (gnus-simplify-subject-fuzzy whole-subject))
7913                ;; Just remove the leading "Re:".
7914                (t
7915                 (gnus-simplify-subject-re whole-subject))))
7916
7917         (if (and gnus-summary-gather-exclude-subject
7918                  (string-match gnus-summary-gather-exclude-subject
7919                                subject))
7920             ()          ; We don't want to do anything with this article.
7921           ;; We simplify the subject before looking it up in the
7922           ;; hash table.
7923
7924           (if (setq hthread (gnus-gethash subject hashtb))
7925               (progn
7926                 ;; We enter a dummy root into the thread, if we
7927                 ;; haven't done that already.
7928                 (unless (stringp (caar hthread))
7929                   (setcar hthread (list whole-subject (car hthread))))
7930                 ;; We add this new gathered thread to this gathered
7931                 ;; thread.
7932                 (setcdr (car hthread)
7933                         (nconc (cdar hthread) (list (car threads))))
7934                 ;; Remove it from the list of threads.
7935                 (setcdr prev (cdr threads))
7936                 (setq threads prev))
7937             ;; Enter this thread into the hash table.
7938             (gnus-sethash subject threads hashtb)))
7939         (setq prev threads)
7940         (setq threads (cdr threads)))
7941       result)))
7942
7943 (defun gnus-gather-threads-by-references (threads)
7944   "Gather threads by looking at References headers."
7945   (let ((idhashtb (gnus-make-hashtable 1023))
7946         (thhashtb (gnus-make-hashtable 1023))
7947         (prev threads)
7948         (result threads)
7949         ids references id gthread gid entered)
7950     (while threads
7951       (when (setq references (mail-header-references (caar threads)))
7952         (setq id (mail-header-id (caar threads)))
7953         (setq ids (gnus-split-references references))
7954         (setq entered nil)
7955         (while ids
7956           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7957               (progn
7958                 (gnus-sethash (car ids) id idhashtb)
7959                 (gnus-sethash id threads thhashtb))
7960             (setq gthread (gnus-gethash gid thhashtb))
7961             (unless entered
7962               ;; We enter a dummy root into the thread, if we
7963               ;; haven't done that already.
7964               (unless (stringp (caar gthread))
7965                 (setcar gthread (list (mail-header-subject (caar gthread))
7966                                       (car gthread))))
7967               ;; We add this new gathered thread to this gathered
7968               ;; thread.
7969               (setcdr (car gthread)
7970                       (nconc (cdar gthread) (list (car threads)))))
7971             ;; Add it into the thread hash table.
7972             (gnus-sethash id gthread thhashtb)
7973             (setq entered t)
7974             ;; Remove it from the list of threads.
7975             (setcdr prev (cdr threads))
7976             (setq threads prev))
7977           (setq ids (cdr ids))))
7978       (setq prev threads)
7979       (setq threads (cdr threads)))
7980     result))
7981
7982 (defun gnus-sort-gathered-threads (threads)
7983   "Sort subtreads inside each gathered thread by article number."
7984   (let ((result threads))
7985     (while threads
7986       (when (stringp (caar threads))
7987         (setcdr (car threads)
7988                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7989       (setq threads (cdr threads)))
7990     result))
7991
7992 (defun gnus-make-threads ()
7993   "Go through the dependency hashtb and find the roots.  Return all threads."
7994   (let (threads)
7995     (mapatoms
7996      (lambda (refs)
7997        (unless (car (symbol-value refs))
7998          ;; These threads do not refer back to any other articles,
7999          ;; so they're roots.
8000          (setq threads (append (cdr (symbol-value refs)) threads))))
8001      gnus-newsgroup-dependencies)
8002     threads))
8003
8004 (defun gnus-build-sparse-threads ()
8005   (let ((headers gnus-newsgroup-headers)
8006         (deps gnus-newsgroup-dependencies)
8007         header references generation relations 
8008         cthread subject child end pthread relation)
8009     ;; First we create an alist of generations/relations, where 
8010     ;; generations is how much we trust the ralation, and the relation
8011     ;; is parent/child.
8012     (gnus-message 7 "Making sparse threads...")
8013     (save-excursion
8014       (nnheader-set-temp-buffer " *gnus sparse threads*")
8015       (while (setq header (pop headers))
8016         (when (and (setq references (mail-header-references header))
8017                    (not (string= references "")))
8018           (insert references)
8019           (setq child (mail-header-id header)
8020                 subject (mail-header-subject header))
8021           (setq generation 0)
8022           (while (search-backward ">" nil t)
8023             (setq end (1+ (point)))
8024             (when (search-backward "<" nil t)
8025               (push (list (incf generation) 
8026                           child (setq child (buffer-substring (point) end))
8027                           subject)
8028                     relations)))
8029           (push (list (1+ generation) child nil subject) relations)
8030           (erase-buffer)))
8031       (kill-buffer (current-buffer)))
8032     ;; Sort over trustworthiness.
8033     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
8034     (while (setq relation (pop relations))
8035       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
8036                 (unless (car (symbol-value cthread))
8037                   ;; Make this article the parent of these threads.
8038                   (setcar (symbol-value cthread)
8039                           (vector gnus-reffed-article-number 
8040                                   (cadddr relation) 
8041                                   "" ""
8042                                   (cadr relation) 
8043                                   (or (caddr relation) "") 0 0 "")))
8044               (set cthread (list (vector gnus-reffed-article-number
8045                                          (cadddr relation) 
8046                                          "" "" (cadr relation) 
8047                                          (or (caddr relation) "") 0 0 ""))))
8048         (push gnus-reffed-article-number gnus-newsgroup-limit)
8049         (push gnus-reffed-article-number gnus-newsgroup-sparse)
8050         (push (cons gnus-reffed-article-number gnus-sparse-mark)
8051               gnus-newsgroup-reads)
8052         (decf gnus-reffed-article-number)
8053         ;; Make this new thread the child of its parent.
8054         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
8055             (setcdr (symbol-value pthread)
8056                     (nconc (cdr (symbol-value pthread))
8057                            (list (symbol-value cthread))))
8058           (set pthread (list nil (symbol-value cthread))))))
8059     (gnus-message 7 "Making sparse threads...done")))
8060
8061 (defun gnus-build-old-threads ()
8062   ;; Look at all the articles that refer back to old articles, and
8063   ;; fetch the headers for the articles that aren't there.  This will
8064   ;; build complete threads - if the roots haven't been expired by the
8065   ;; server, that is.
8066   (let (id heads)
8067     (mapatoms
8068      (lambda (refs)
8069        (when (not (car (symbol-value refs)))
8070          (setq heads (cdr (symbol-value refs)))
8071          (while heads
8072            (if (memq (mail-header-number (caar heads))
8073                      gnus-newsgroup-dormant)
8074                (setq heads (cdr heads))
8075              (setq id (symbol-name refs))
8076              (while (and (setq id (gnus-build-get-header id))
8077                          (not (car (gnus-gethash
8078                                     id gnus-newsgroup-dependencies)))))
8079              (setq heads nil)))))
8080      gnus-newsgroup-dependencies)))
8081
8082 (defun gnus-build-get-header (id)
8083   ;; Look through the buffer of NOV lines and find the header to
8084   ;; ID.  Enter this line into the dependencies hash table, and return
8085   ;; the id of the parent article (if any).
8086   (let ((deps gnus-newsgroup-dependencies)
8087         found header)
8088     (prog1
8089         (save-excursion
8090           (set-buffer nntp-server-buffer)
8091           (goto-char (point-min))
8092           (while (and (not found) (search-forward id nil t))
8093             (beginning-of-line)
8094             (setq found (looking-at
8095                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
8096                                  (regexp-quote id))))
8097             (or found (beginning-of-line 2)))
8098           (when found
8099             (beginning-of-line)
8100             (and
8101              (setq header (gnus-nov-parse-line
8102                            (read (current-buffer)) deps))
8103              (gnus-parent-id (mail-header-references header)))))
8104       (when header
8105         (let ((number (mail-header-number header)))
8106           (push number gnus-newsgroup-limit)
8107           (push header gnus-newsgroup-headers)
8108           (if (memq number gnus-newsgroup-unselected)
8109               (progn
8110                 (push number gnus-newsgroup-unreads)
8111                 (setq gnus-newsgroup-unselected
8112                       (delq number gnus-newsgroup-unselected)))
8113             (push number gnus-newsgroup-ancient)))))))
8114
8115 (defun gnus-summary-update-article (article &optional iheader)
8116   "Update ARTICLE in the summary buffer."
8117   (set-buffer gnus-summary-buffer)
8118   (let* ((header (or iheader (gnus-summary-article-header article)))
8119          (id (mail-header-id header))
8120          (data (gnus-data-find article))
8121          (thread (gnus-id-to-thread id))
8122          (references (mail-header-references header))
8123          (parent
8124           (gnus-id-to-thread
8125            (or (gnus-parent-id 
8126                 (if (and references
8127                          (not (equal "" references)))
8128                     references))
8129                "none")))
8130          (buffer-read-only nil)
8131          (old (car thread))
8132          (number (mail-header-number header))
8133          pos)
8134     (when thread
8135       ;; !!! Should this be in or not?
8136       (unless iheader
8137         (setcar thread nil))
8138       (when parent
8139         (delq thread parent))
8140       (if (gnus-summary-insert-subject id header iheader)
8141           ;; Set the (possibly) new article number in the data structure.
8142           (gnus-data-set-number data (gnus-id-to-article id))
8143         (setcar thread old)
8144         nil))))
8145
8146 (defun gnus-rebuild-thread (id)
8147   "Rebuild the thread containing ID."
8148   (let ((buffer-read-only nil)
8149         current thread data)
8150     (if (not gnus-show-threads)
8151         (setq thread (list (car (gnus-id-to-thread id))))
8152       ;; Get the thread this article is part of.
8153       (setq thread (gnus-remove-thread id)))
8154     (setq current (save-excursion
8155                     (and (zerop (forward-line -1))
8156                          (gnus-summary-article-number))))
8157     ;; If this is a gathered thread, we have to go some re-gathering.
8158     (when (stringp (car thread))
8159       (let ((subject (car thread))
8160             roots thr)
8161         (setq thread (cdr thread))
8162         (while thread
8163           (unless (memq (setq thr (gnus-id-to-thread
8164                                       (gnus-root-id
8165                                        (mail-header-id (caar thread)))))
8166                         roots)
8167             (push thr roots))
8168           (setq thread (cdr thread)))
8169         ;; We now have all (unique) roots.
8170         (if (= (length roots) 1)
8171             ;; All the loose roots are now one solid root.
8172             (setq thread (car roots))
8173           (setq thread (cons subject (gnus-sort-threads roots))))))
8174     (let (threads)
8175       ;; We then insert this thread into the summary buffer.
8176       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8177         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8178         (setq data (nreverse gnus-newsgroup-data))
8179         (setq threads gnus-newsgroup-threads))
8180       ;; We splice the new data into the data structure.
8181       (gnus-data-enter-list current data)
8182       (gnus-data-compute-positions)
8183       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8184
8185 (defun gnus-number-to-header (number)
8186   "Return the header for article NUMBER."
8187   (let ((headers gnus-newsgroup-headers))
8188     (while (and headers
8189                 (not (= number (mail-header-number (car headers)))))
8190       (pop headers))
8191     (when headers
8192       (car headers))))
8193
8194 (defun gnus-id-to-thread (id)
8195   "Return the (sub-)thread where ID appears."
8196   (gnus-gethash id gnus-newsgroup-dependencies))
8197
8198 (defun gnus-id-to-article (id)
8199   "Return the article number of ID."
8200   (let ((thread (gnus-id-to-thread id)))
8201     (when (and thread
8202                (car thread))
8203       (mail-header-number (car thread)))))
8204
8205 (defun gnus-id-to-header (id)
8206   "Return the article headers of ID."
8207   (car (gnus-id-to-thread id)))
8208
8209 (defun gnus-article-displayed-root-p (article)
8210   "Say whether ARTICLE is a root(ish) article."
8211   (let ((level (gnus-summary-thread-level article))
8212         (refs (mail-header-references  (gnus-summary-article-header article)))
8213         particle)
8214     (cond 
8215      ((null level) nil)
8216      ((zerop level) t)
8217      ((null refs) t)
8218      ((null (gnus-parent-id refs)) t)
8219      ((and (= 1 level)
8220            (null (setq particle (gnus-id-to-article
8221                                  (gnus-parent-id refs))))
8222            (null (gnus-summary-thread-level particle)))))))
8223
8224 (defun gnus-root-id (id)
8225   "Return the id of the root of the thread where ID appears."
8226   (let (last-id prev)
8227     (while (and id (setq prev (car (gnus-gethash 
8228                                     id gnus-newsgroup-dependencies))))
8229       (setq last-id id
8230             id (gnus-parent-id (mail-header-references prev))))
8231     last-id))
8232
8233 (defun gnus-remove-thread (id &optional dont-remove)
8234   "Remove the thread that has ID in it."
8235   (let ((dep gnus-newsgroup-dependencies)
8236         headers thread last-id)
8237     ;; First go up in this thread until we find the root.
8238     (setq last-id (gnus-root-id id))
8239     (setq headers (list (car (gnus-id-to-thread last-id))
8240                         (caadr (gnus-id-to-thread last-id))))
8241     ;; We have now found the real root of this thread.  It might have
8242     ;; been gathered into some loose thread, so we have to search
8243     ;; through the threads to find the thread we wanted.
8244     (let ((threads gnus-newsgroup-threads)
8245           sub)
8246       (while threads
8247         (setq sub (car threads))
8248         (if (stringp (car sub))
8249             ;; This is a gathered threads, so we look at the roots
8250             ;; below it to find whether this article in in this
8251             ;; gathered root.
8252             (progn
8253               (setq sub (cdr sub))
8254               (while sub
8255                 (when (member (caar sub) headers)
8256                   (setq thread (car threads)
8257                         threads nil
8258                         sub nil))
8259                 (setq sub (cdr sub))))
8260           ;; It's an ordinary thread, so we check it.
8261           (when (eq (car sub) (car headers))
8262             (setq thread sub
8263                   threads nil)))
8264         (setq threads (cdr threads)))
8265       ;; If this article is in no thread, then it's a root.
8266       (if thread
8267           (unless dont-remove
8268             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8269         (setq thread (gnus-gethash last-id dep)))
8270       (when thread
8271         (prog1
8272             thread ; We return this thread.
8273           (unless dont-remove
8274             (if (stringp (car thread))
8275                 (progn
8276                   ;; If we use dummy roots, then we have to remove the
8277                   ;; dummy root as well.
8278                   (when (eq gnus-summary-make-false-root 'dummy)
8279                     ;; Uhm.
8280                     )
8281                   (setq thread (cdr thread))
8282                   (while thread
8283                     (gnus-remove-thread-1 (car thread))
8284                     (setq thread (cdr thread))))
8285               (gnus-remove-thread-1 thread))))))))
8286
8287 (defun gnus-remove-thread-1 (thread)
8288   "Remove the thread THREAD recursively."
8289   (let ((number (mail-header-number (car thread)))
8290         pos)
8291     (when (setq pos (text-property-any
8292                      (point-min) (point-max) 'gnus-number number))
8293       (goto-char pos)
8294       (gnus-delete-line)
8295       (gnus-data-remove number))
8296     (setq thread (cdr thread))
8297     (while thread
8298       (gnus-remove-thread-1 (pop thread)))))
8299
8300 (defun gnus-sort-threads (threads)
8301   "Sort THREADS."
8302   (if (not gnus-thread-sort-functions)
8303       threads
8304     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8305                     (car gnus-thread-sort-functions)
8306                   `(lambda (t1 t2)
8307                      ,(gnus-make-sort-function 
8308                        (reverse gnus-thread-sort-functions))))))
8309       (gnus-message 7 "Sorting threads...")
8310       (prog1
8311           (sort threads func)
8312         (gnus-message 7 "Sorting threads...done")))))
8313
8314 (defun gnus-sort-articles (articles)
8315   "Sort ARTICLES."
8316   (when gnus-article-sort-functions
8317     (let ((func (if (= 1 (length gnus-article-sort-functions))
8318                     (car gnus-article-sort-functions)
8319                   `(lambda (t1 t2)
8320                      ,(gnus-make-sort-function 
8321                        (reverse gnus-article-sort-functions))))))
8322       (gnus-message 7 "Sorting articles...")
8323       (prog1
8324           (setq gnus-newsgroup-headers (sort articles func))
8325         (gnus-message 7 "Sorting articles...done")))))
8326
8327 (defun gnus-make-sort-function (funs)
8328   "Return a composite sort condition based on the functions in FUNC."
8329   (if (cdr funs)
8330       `(or (,(car funs) t1 t2)
8331            (and (not (,(car funs) t2 t1))
8332                 ,(gnus-make-sort-function (cdr funs))))
8333     `(,(car funs) t1 t2)))
8334                  
8335 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8336 (defmacro gnus-thread-header (thread)
8337   ;; Return header of first article in THREAD.
8338   ;; Note that THREAD must never, ever be anything else than a variable -
8339   ;; using some other form will lead to serious barfage.
8340   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8341   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8342   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8343         (vector thread) 2))
8344
8345 (defsubst gnus-article-sort-by-number (h1 h2)
8346   "Sort articles by article number."
8347   (< (mail-header-number h1)
8348      (mail-header-number h2)))
8349
8350 (defun gnus-thread-sort-by-number (h1 h2)
8351   "Sort threads by root article number."
8352   (gnus-article-sort-by-number
8353    (gnus-thread-header h1) (gnus-thread-header h2)))
8354
8355 (defsubst gnus-article-sort-by-author (h1 h2)
8356   "Sort articles by root author."
8357   (string-lessp
8358    (let ((extract (funcall
8359                    gnus-extract-address-components
8360                    (mail-header-from h1))))
8361      (or (car extract) (cdr extract)))
8362    (let ((extract (funcall
8363                    gnus-extract-address-components
8364                    (mail-header-from h2))))
8365      (or (car extract) (cdr extract)))))
8366
8367 (defun gnus-thread-sort-by-author (h1 h2)
8368   "Sort threads by root author."
8369   (gnus-article-sort-by-author
8370    (gnus-thread-header h1)  (gnus-thread-header h2)))
8371
8372 (defsubst gnus-article-sort-by-subject (h1 h2)
8373   "Sort articles by root subject."
8374   (string-lessp
8375    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8376    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8377
8378 (defun gnus-thread-sort-by-subject (h1 h2)
8379   "Sort threads by root subject."
8380   (gnus-article-sort-by-subject
8381    (gnus-thread-header h1) (gnus-thread-header h2)))
8382
8383 (defsubst gnus-article-sort-by-date (h1 h2)
8384   "Sort articles by root article date."
8385   (string-lessp
8386    (inline (gnus-sortable-date (mail-header-date h1)))
8387    (inline (gnus-sortable-date (mail-header-date h2)))))
8388
8389 (defun gnus-thread-sort-by-date (h1 h2)
8390   "Sort threads by root article date."
8391   (gnus-article-sort-by-date
8392    (gnus-thread-header h1) (gnus-thread-header h2)))
8393
8394 (defsubst gnus-article-sort-by-score (h1 h2)
8395   "Sort articles by root article score.
8396 Unscored articles will be counted as having a score of zero."
8397   (> (or (cdr (assq (mail-header-number h1)
8398                     gnus-newsgroup-scored))
8399          gnus-summary-default-score 0)
8400      (or (cdr (assq (mail-header-number h2)
8401                     gnus-newsgroup-scored))
8402          gnus-summary-default-score 0)))
8403
8404 (defun gnus-thread-sort-by-score (h1 h2)
8405   "Sort threads by root article score."
8406   (gnus-article-sort-by-score
8407    (gnus-thread-header h1) (gnus-thread-header h2)))
8408
8409 (defun gnus-thread-sort-by-total-score (h1 h2)
8410   "Sort threads by the sum of all scores in the thread.
8411 Unscored articles will be counted as having a score of zero."
8412   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8413
8414 (defun gnus-thread-total-score (thread)
8415   ;;  This function find the total score of THREAD.
8416   (cond ((null thread)
8417          0)
8418         ((consp thread)
8419          (if (stringp (car thread))
8420              (apply gnus-thread-score-function 0
8421                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8422            (gnus-thread-total-score-1 thread)))
8423         (t
8424          (gnus-thread-total-score-1 (list thread)))))
8425
8426 (defun gnus-thread-total-score-1 (root)
8427   ;; This function find the total score of the thread below ROOT.
8428   (setq root (car root))
8429   (apply gnus-thread-score-function
8430          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8431              gnus-summary-default-score 0)
8432          (mapcar 'gnus-thread-total-score
8433                  (cdr (gnus-gethash (mail-header-id root)
8434                                     gnus-newsgroup-dependencies)))))
8435
8436 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8437 (defvar gnus-tmp-prev-subject nil)
8438 (defvar gnus-tmp-false-parent nil)
8439 (defvar gnus-tmp-root-expunged nil)
8440 (defvar gnus-tmp-dummy-line nil)
8441
8442 (defun gnus-summary-prepare-threads (threads)
8443   "Prepare summary buffer from THREADS and indentation LEVEL.
8444 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8445 or a straight list of headers."
8446   (gnus-message 7 "Generating summary...")
8447
8448   (setq gnus-newsgroup-threads threads)
8449   (beginning-of-line)
8450
8451   (let ((gnus-tmp-level 0)
8452         (default-score (or gnus-summary-default-score 0))
8453         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8454         thread number subject stack state gnus-tmp-gathered beg-match
8455         new-roots gnus-tmp-new-adopts thread-end
8456         gnus-tmp-header gnus-tmp-unread
8457         gnus-tmp-replied gnus-tmp-subject-or-nil
8458         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8459         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8460         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8461
8462     (setq gnus-tmp-prev-subject nil)
8463
8464     (if (vectorp (car threads))
8465         ;; If this is a straight (sic) list of headers, then a
8466         ;; threaded summary display isn't required, so we just create
8467         ;; an unthreaded one.
8468         (gnus-summary-prepare-unthreaded threads)
8469
8470       ;; Do the threaded display.
8471
8472       (while (or threads stack gnus-tmp-new-adopts new-roots)
8473
8474         (if (and (= gnus-tmp-level 0)
8475                  (not (setq gnus-tmp-dummy-line nil))
8476                  (or (not stack)
8477                      (= (caar stack) 0))
8478                  (not gnus-tmp-false-parent)
8479                  (or gnus-tmp-new-adopts new-roots))
8480             (if gnus-tmp-new-adopts
8481                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8482                       thread (list (car gnus-tmp-new-adopts))
8483                       gnus-tmp-header (caar thread)
8484                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8485               (if new-roots
8486                   (setq thread (list (car new-roots))
8487                         gnus-tmp-header (caar thread)
8488                         new-roots (cdr new-roots))))
8489
8490           (if threads
8491               ;; If there are some threads, we do them before the
8492               ;; threads on the stack.
8493               (setq thread threads
8494                     gnus-tmp-header (caar thread))
8495             ;; There were no current threads, so we pop something off
8496             ;; the stack.
8497             (setq state (car stack)
8498                   gnus-tmp-level (car state)
8499                   thread (cdr state)
8500                   stack (cdr stack)
8501                   gnus-tmp-header (caar thread))))
8502
8503         (setq gnus-tmp-false-parent nil)
8504         (setq gnus-tmp-root-expunged nil)
8505         (setq thread-end nil)
8506
8507         (if (stringp gnus-tmp-header)
8508             ;; The header is a dummy root.
8509             (cond
8510              ((eq gnus-summary-make-false-root 'adopt)
8511               ;; We let the first article adopt the rest.
8512               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8513                                                (cddar thread)))
8514               (setq gnus-tmp-gathered
8515                     (nconc (mapcar
8516                             (lambda (h) (mail-header-number (car h)))
8517                             (cddar thread))
8518                            gnus-tmp-gathered))
8519               (setq thread (cons (list (caar thread)
8520                                        (cadar thread))
8521                                  (cdr thread)))
8522               (setq gnus-tmp-level -1
8523                     gnus-tmp-false-parent t))
8524              ((eq gnus-summary-make-false-root 'empty)
8525               ;; We print adopted articles with empty subject fields.
8526               (setq gnus-tmp-gathered
8527                     (nconc (mapcar
8528                             (lambda (h) (mail-header-number (car h)))
8529                             (cddar thread))
8530                            gnus-tmp-gathered))
8531               (setq gnus-tmp-level -1))
8532              ((eq gnus-summary-make-false-root 'dummy)
8533               ;; We remember that we probably want to output a dummy
8534               ;; root.
8535               (setq gnus-tmp-dummy-line gnus-tmp-header)
8536               (setq gnus-tmp-prev-subject gnus-tmp-header))
8537              (t
8538               ;; We do not make a root for the gathered
8539               ;; sub-threads at all.
8540               (setq gnus-tmp-level -1)))
8541
8542           (setq number (mail-header-number gnus-tmp-header)
8543                 subject (mail-header-subject gnus-tmp-header))
8544
8545           (cond
8546            ;; If the thread has changed subject, we might want to make
8547            ;; this subthread into a root.
8548            ((and (null gnus-thread-ignore-subject)
8549                  (not (zerop gnus-tmp-level))
8550                  gnus-tmp-prev-subject
8551                  (not (inline
8552                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8553             (setq new-roots (nconc new-roots (list (car thread)))
8554                   thread-end t
8555                   gnus-tmp-header nil))
8556            ;; If the article lies outside the current limit,
8557            ;; then we do not display it.
8558            ((and (not (memq number gnus-newsgroup-limit))
8559                  (not gnus-tmp-dummy-line))
8560             (setq gnus-tmp-gathered
8561                   (nconc (mapcar
8562                           (lambda (h) (mail-header-number (car h)))
8563                           (cdar thread))
8564                          gnus-tmp-gathered))
8565             (setq gnus-tmp-new-adopts (if (cdar thread)
8566                                           (append gnus-tmp-new-adopts
8567                                                   (cdar thread))
8568                                         gnus-tmp-new-adopts)
8569                   thread-end t
8570                   gnus-tmp-header nil)
8571             (when (zerop gnus-tmp-level)
8572               (setq gnus-tmp-root-expunged t)))
8573            ;; Perhaps this article is to be marked as read?
8574            ((and gnus-summary-mark-below
8575                  (< (or (cdr (assq number gnus-newsgroup-scored))
8576                         default-score)
8577                     gnus-summary-mark-below)
8578                  ;; Don't touch sparse articles.
8579                  (not (memq number gnus-newsgroup-sparse))
8580                  (not (memq number gnus-newsgroup-ancient)))
8581             (setq gnus-newsgroup-unreads
8582                   (delq number gnus-newsgroup-unreads))
8583             (if gnus-newsgroup-auto-expire
8584                 (push number gnus-newsgroup-expirable)
8585               (push (cons number gnus-low-score-mark)
8586                     gnus-newsgroup-reads))))
8587
8588           (when gnus-tmp-header
8589             ;; We may have an old dummy line to output before this
8590             ;; article.
8591             (when gnus-tmp-dummy-line
8592               (gnus-summary-insert-dummy-line
8593                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8594               (setq gnus-tmp-dummy-line nil))
8595
8596             ;; Compute the mark.
8597             (setq
8598              gnus-tmp-unread
8599              (cond
8600               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8601               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8602               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8603               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8604               (t (or (cdr (assq number gnus-newsgroup-reads))
8605                      gnus-ancient-mark))))
8606
8607             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8608                                   gnus-tmp-header gnus-tmp-level)
8609                   gnus-newsgroup-data)
8610
8611             ;; Actually insert the line.
8612             (setq
8613              gnus-tmp-subject-or-nil
8614              (cond
8615               ((and gnus-thread-ignore-subject
8616                     gnus-tmp-prev-subject
8617                     (not (inline (gnus-subject-equal
8618                                   gnus-tmp-prev-subject subject))))
8619                subject)
8620               ((zerop gnus-tmp-level)
8621                (if (and (eq gnus-summary-make-false-root 'empty)
8622                         (memq number gnus-tmp-gathered)
8623                         gnus-tmp-prev-subject
8624                         (inline (gnus-subject-equal
8625                                  gnus-tmp-prev-subject subject)))
8626                    gnus-summary-same-subject
8627                  subject))
8628               (t gnus-summary-same-subject)))
8629             (if (and (eq gnus-summary-make-false-root 'adopt)
8630                      (= gnus-tmp-level 1)
8631                      (memq number gnus-tmp-gathered))
8632                 (setq gnus-tmp-opening-bracket ?\<
8633                       gnus-tmp-closing-bracket ?\>)
8634               (setq gnus-tmp-opening-bracket ?\[
8635                     gnus-tmp-closing-bracket ?\]))
8636             (setq
8637              gnus-tmp-indentation
8638              (aref gnus-thread-indent-array gnus-tmp-level)
8639              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8640              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8641                                 gnus-summary-default-score 0)
8642              gnus-tmp-score-char
8643              (if (or (null gnus-summary-default-score)
8644                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8645                          gnus-summary-zcore-fuzz)) ? 
8646                (if (< gnus-tmp-score gnus-summary-default-score)
8647                    gnus-score-below-mark gnus-score-over-mark))
8648              gnus-tmp-replied
8649              (cond ((memq number gnus-newsgroup-processable)
8650                     gnus-process-mark)
8651                    ((memq number gnus-newsgroup-cached)
8652                     gnus-cached-mark)
8653                    ((memq number gnus-newsgroup-replied)
8654                     gnus-replied-mark)
8655                    ((memq number gnus-newsgroup-saved)
8656                     gnus-saved-mark)
8657                    (t gnus-unread-mark))
8658              gnus-tmp-from (mail-header-from gnus-tmp-header)
8659              gnus-tmp-name
8660              (cond
8661               ((string-match "(.+)" gnus-tmp-from)
8662                (substring gnus-tmp-from
8663                           (1+ (match-beginning 0)) (1- (match-end 0))))
8664               ((string-match "<[^>]+> *$" gnus-tmp-from)
8665                (setq beg-match (match-beginning 0))
8666                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8667                         (substring gnus-tmp-from (1+ (match-beginning 0))
8668                                    (1- (match-end 0))))
8669                    (substring gnus-tmp-from 0 beg-match)))
8670               (t gnus-tmp-from)))
8671             (when (string= gnus-tmp-name "")
8672               (setq gnus-tmp-name gnus-tmp-from))
8673             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8674             (gnus-put-text-property
8675              (point)
8676              (progn (eval gnus-summary-line-format-spec) (point))
8677              'gnus-number number)
8678             (when gnus-visual-p
8679               (forward-line -1)
8680               (run-hooks 'gnus-summary-update-hook)
8681               (forward-line 1))
8682
8683             (setq gnus-tmp-prev-subject subject)))
8684
8685         (when (nth 1 thread)
8686           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8687         (incf gnus-tmp-level)
8688         (setq threads (if thread-end nil (cdar thread)))
8689         (unless threads
8690           (setq gnus-tmp-level 0)))))
8691   (gnus-message 7 "Generating summary...done"))
8692
8693 (defun gnus-summary-prepare-unthreaded (headers)
8694   "Generate an unthreaded summary buffer based on HEADERS."
8695   (let (header number mark)
8696
8697     (while headers
8698       ;; We may have to root out some bad articles...
8699       (when (memq (setq number (mail-header-number
8700                                 (setq header (pop headers))))
8701                   gnus-newsgroup-limit)
8702         ;; Mark article as read when it has a low score.
8703         (when (and gnus-summary-mark-below
8704                    (< (or (cdr (assq number gnus-newsgroup-scored))
8705                           gnus-summary-default-score 0)
8706                       gnus-summary-mark-below)
8707                    (not (memq number gnus-newsgroup-ancient)))
8708           (setq gnus-newsgroup-unreads
8709                 (delq number gnus-newsgroup-unreads))
8710           (if gnus-newsgroup-auto-expire
8711               (push number gnus-newsgroup-expirable)
8712             (push (cons number gnus-low-score-mark)
8713                   gnus-newsgroup-reads)))
8714
8715         (setq mark
8716               (cond
8717                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8718                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8719                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8720                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8721                (t (or (cdr (assq number gnus-newsgroup-reads))
8722                       gnus-ancient-mark))))
8723         (setq gnus-newsgroup-data
8724               (cons (gnus-data-make number mark (1+ (point)) header 0)
8725                     gnus-newsgroup-data))
8726         (gnus-summary-insert-line
8727          header 0 nil mark (memq number gnus-newsgroup-replied)
8728          (memq number gnus-newsgroup-expirable)
8729          (mail-header-subject header) nil
8730          (cdr (assq number gnus-newsgroup-scored))
8731          (memq number gnus-newsgroup-processable))))))
8732
8733 (defun gnus-select-newsgroup (group &optional read-all)
8734   "Select newsgroup GROUP.
8735 If READ-ALL is non-nil, all articles in the group are selected."
8736   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8737          (info (nth 2 entry))
8738          articles fetched-articles cached)
8739
8740     (or (gnus-check-server
8741          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8742         (error "Couldn't open server"))
8743
8744     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8745         (gnus-activate-group group)     ; Or we can activate it...
8746         (progn                          ; Or we bug out.
8747           (when (equal major-mode 'gnus-summary-mode)
8748             (kill-buffer (current-buffer)))
8749           (error "Couldn't request group %s: %s"
8750                  group (gnus-status-message group))))
8751
8752     (unless (gnus-request-group group t)
8753       (when (equal major-mode 'gnus-summary-mode)
8754         (kill-buffer (current-buffer)))
8755       (error "Couldn't request group %s: %s"
8756              group (gnus-status-message group)))      
8757
8758     (setq gnus-newsgroup-name group)
8759     (setq gnus-newsgroup-unselected nil)
8760     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8761
8762     (and gnus-asynchronous
8763          (gnus-check-backend-function
8764           'request-asynchronous gnus-newsgroup-name)
8765          (setq gnus-newsgroup-async
8766                (gnus-request-asynchronous gnus-newsgroup-name)))
8767
8768     ;; Adjust and set lists of article marks.
8769     (when info
8770       (gnus-adjust-marked-articles info))
8771
8772     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8773     (when (gnus-virtual-group-p group)
8774       (setq cached gnus-newsgroup-cached))
8775
8776     (setq gnus-newsgroup-unreads
8777           (gnus-set-difference
8778            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8779            gnus-newsgroup-dormant))
8780
8781     (setq gnus-newsgroup-processable nil)
8782
8783     (setq articles (gnus-articles-to-read group read-all))
8784
8785     (cond
8786      ((null articles)
8787       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8788       'quit)
8789      ((eq articles 0) nil)
8790      (t
8791       ;; Init the dependencies hash table.
8792       (setq gnus-newsgroup-dependencies
8793             (gnus-make-hashtable (length articles)))
8794       ;; Retrieve the headers and read them in.
8795       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8796       (setq gnus-newsgroup-headers
8797             (if (eq 'nov
8798                     (setq gnus-headers-retrieved-by
8799                           (gnus-retrieve-headers
8800                            articles gnus-newsgroup-name
8801                            ;; We might want to fetch old headers, but
8802                            ;; not if there is only 1 article.
8803                            (and gnus-fetch-old-headers
8804                                 (or (and
8805                                      (not (eq gnus-fetch-old-headers 'some))
8806                                      (not (numberp gnus-fetch-old-headers)))
8807                                     (> (length articles) 1))))))
8808                 (gnus-get-newsgroup-headers-xover articles)
8809               (gnus-get-newsgroup-headers)))
8810       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8811
8812       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8813       (when cached
8814         (setq gnus-newsgroup-cached cached))
8815
8816       ;; Set the initial limit.
8817       (setq gnus-newsgroup-limit (copy-sequence articles))
8818       ;; Remove canceled articles from the list of unread articles.
8819       (setq gnus-newsgroup-unreads
8820             (gnus-set-sorted-intersection
8821              gnus-newsgroup-unreads
8822              (setq fetched-articles
8823                    (mapcar (lambda (headers) (mail-header-number headers))
8824                            gnus-newsgroup-headers))))
8825       ;; Removed marked articles that do not exist.
8826       (gnus-update-missing-marks
8827        (gnus-sorted-complement fetched-articles articles))
8828       ;; We might want to build some more threads first.
8829       (and gnus-fetch-old-headers
8830            (eq gnus-headers-retrieved-by 'nov)
8831            (gnus-build-old-threads))
8832       ;; Check whether auto-expire is to be done in this group.
8833       (setq gnus-newsgroup-auto-expire
8834             (gnus-group-auto-expirable-p group))
8835       ;; Set up the article buffer now, if necessary.
8836       (unless gnus-single-article-buffer
8837         (gnus-article-setup-buffer))
8838       ;; First and last article in this newsgroup.
8839       (when gnus-newsgroup-headers
8840         (setq gnus-newsgroup-begin
8841               (mail-header-number (car gnus-newsgroup-headers))
8842               gnus-newsgroup-end
8843               (mail-header-number
8844                (gnus-last-element gnus-newsgroup-headers))))
8845       (setq gnus-reffed-article-number -1)
8846       ;; GROUP is successfully selected.
8847       (or gnus-newsgroup-headers t)))))
8848
8849 (defun gnus-articles-to-read (group read-all)
8850   ;; Find out what articles the user wants to read.
8851   (let* ((articles
8852           ;; Select all articles if `read-all' is non-nil, or if there
8853           ;; are no unread articles.
8854           (if (or read-all
8855                   (and (zerop (length gnus-newsgroup-marked))
8856                        (zerop (length gnus-newsgroup-unreads))))
8857               (gnus-uncompress-range (gnus-active group))
8858             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8859                           (copy-sequence gnus-newsgroup-unreads))
8860                   '<)))
8861          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8862          (scored (length scored-list))
8863          (number (length articles))
8864          (marked (+ (length gnus-newsgroup-marked)
8865                     (length gnus-newsgroup-dormant)))
8866          (select
8867           (cond
8868            ((numberp read-all)
8869             read-all)
8870            (t
8871             (condition-case ()
8872                 (cond
8873                  ((and (or (<= scored marked) (= scored number))
8874                        (numberp gnus-large-newsgroup)
8875                        (> number gnus-large-newsgroup))
8876                   (let ((input
8877                          (read-string
8878                           (format
8879                            "How many articles from %s (default %d): "
8880                            gnus-newsgroup-name number))))
8881                     (if (string-match "^[ \t]*$" input) number input)))
8882                  ((and (> scored marked) (< scored number)
8883                        (> (- scored number) 20))
8884                   (let ((input
8885                          (read-string
8886                           (format "%s %s (%d scored, %d total): "
8887                                   "How many articles from"
8888                                   group scored number))))
8889                     (if (string-match "^[ \t]*$" input)
8890                         number input)))
8891                  (t number))
8892               (quit nil))))))
8893     (setq select (if (stringp select) (string-to-number select) select))
8894     (if (or (null select) (zerop select))
8895         select
8896       (if (and (not (zerop scored)) (<= (abs select) scored))
8897           (progn
8898             (setq articles (sort scored-list '<))
8899             (setq number (length articles)))
8900         (setq articles (copy-sequence articles)))
8901
8902       (if (< (abs select) number)
8903           (if (< select 0)
8904               ;; Select the N oldest articles.
8905               (setcdr (nthcdr (1- (abs select)) articles) nil)
8906             ;; Select the N most recent articles.
8907             (setq articles (nthcdr (- number select) articles))))
8908       (setq gnus-newsgroup-unselected
8909             (gnus-sorted-intersection
8910              gnus-newsgroup-unreads
8911              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8912       articles)))
8913
8914 (defun gnus-killed-articles (killed articles)
8915   (let (out)
8916     (while articles
8917       (if (inline (gnus-member-of-range (car articles) killed))
8918           (setq out (cons (car articles) out)))
8919       (setq articles (cdr articles)))
8920     out))
8921
8922 (defun gnus-uncompress-marks (marks)
8923   "Uncompress the mark ranges in MARKS."
8924   (let ((uncompressed '(score bookmark))
8925         out)
8926     (while marks
8927       (if (memq (caar marks) uncompressed)
8928           (push (car marks) out)
8929         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8930       (setq marks (cdr marks)))
8931     out))
8932
8933 (defun gnus-adjust-marked-articles (info)
8934   "Set all article lists and remove all marks that are no longer legal."
8935   (let* ((marked-lists (gnus-info-marks info))
8936          (active (gnus-active (gnus-info-group info)))
8937          (min (car active))
8938          (max (cdr active))
8939          (types gnus-article-mark-lists)
8940          (uncompressed '(score bookmark))
8941          marks var articles article mark)
8942
8943     (while marked-lists
8944       (setq marks (pop marked-lists))
8945       (set (setq var (intern (format "gnus-newsgroup-%s"
8946                                      (car (rassq (setq mark (car marks))
8947                                                  types)))))
8948            (if (memq (car marks) uncompressed) (cdr marks)
8949              (gnus-uncompress-range (cdr marks))))
8950
8951       (setq articles (symbol-value var))
8952
8953       ;; All articles have to be subsets of the active articles.
8954       (cond
8955        ;; Adjust "simple" lists.
8956        ((memq mark '(tick dormant expirable reply killed save))
8957         (while articles
8958           (when (or (< (setq article (pop articles)) min) (> article max))
8959             (set var (delq article (symbol-value var))))))
8960        ;; Adjust assocs.
8961        ((memq mark '(score bookmark))
8962         (while articles
8963           (when (or (< (car (setq article (pop articles))) min)
8964                     (> (car article) max))
8965             (set var (delq article (symbol-value var))))))))))
8966
8967 (defun gnus-update-missing-marks (missing)
8968   "Go through the list of MISSING articles and remove them mark lists."
8969   (when missing
8970     (let ((types gnus-article-mark-lists)
8971           var m)
8972       ;; Go through all types.
8973       (while types
8974         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8975         (when (symbol-value var)
8976           ;; This list has articles.  So we delete all missing articles
8977           ;; from it.
8978           (setq m missing)
8979           (while m
8980             (set var (delq (pop m) (symbol-value var)))))))))
8981
8982 (defun gnus-update-marks ()
8983   "Enter the various lists of marked articles into the newsgroup info list."
8984   (let ((types gnus-article-mark-lists)
8985         (info (gnus-get-info gnus-newsgroup-name))
8986         (uncompressed '(score bookmark killed))
8987         type list newmarked symbol)
8988     (when info
8989       ;; Add all marks lists that are non-nil to the list of marks lists.
8990       (while types
8991         (setq type (pop types))
8992         (when (setq list (symbol-value
8993                           (setq symbol
8994                                 (intern (format "gnus-newsgroup-%s"
8995                                                 (car type))))))
8996           (push (cons (cdr type)
8997                       (if (memq (cdr type) uncompressed) list
8998                         (gnus-compress-sequence 
8999                          (set symbol (sort list '<)) t)))
9000                 newmarked)))
9001
9002       ;; Enter these new marks into the info of the group.
9003       (if (nthcdr 3 info)
9004           (setcar (nthcdr 3 info) newmarked)
9005         ;; Add the marks lists to the end of the info.
9006         (when newmarked
9007           (setcdr (nthcdr 2 info) (list newmarked))))
9008
9009       ;; Cut off the end of the info if there's nothing else there.
9010       (let ((i 5))
9011         (while (and (> i 2)
9012                     (not (nth i info)))
9013           (when (nthcdr (decf i) info)
9014             (setcdr (nthcdr i info) nil)))))))
9015
9016 (defun gnus-add-marked-articles (group type articles &optional info force)
9017   ;; Add ARTICLES of TYPE to the info of GROUP.
9018   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
9019   ;; add, but replace marked articles of TYPE with ARTICLES.
9020   (let ((info (or info (gnus-get-info group)))
9021         (uncompressed '(score bookmark killed))
9022         marked m)
9023     (or (not info)
9024         (and (not (setq marked (nthcdr 3 info)))
9025              (or (null articles)
9026                  (setcdr (nthcdr 2 info)
9027                          (list (list (cons type (gnus-compress-sequence
9028                                                  articles t)))))))
9029         (and (not (setq m (assq type (car marked))))
9030              (or (null articles)
9031                  (setcar marked
9032                          (cons (cons type (gnus-compress-sequence articles t) )
9033                                (car marked)))))
9034         (if force
9035             (if (null articles)
9036                 (setcar (nthcdr 3 info)
9037                         (delq (assq type (car marked)) (car marked)))
9038               (setcdr m (gnus-compress-sequence articles t)))
9039           (setcdr m (gnus-compress-sequence
9040                      (sort (nconc (gnus-uncompress-range (cdr m))
9041                                   (copy-sequence articles)) '<) t))))))
9042
9043 (defun gnus-set-mode-line (where)
9044   "This function sets the mode line of the article or summary buffers.
9045 If WHERE is `summary', the summary mode line format will be used."
9046   ;; Is this mode line one we keep updated?
9047   (when (memq where gnus-updated-mode-lines)
9048     (let (mode-string)
9049       (save-excursion
9050         ;; We evaluate this in the summary buffer since these
9051         ;; variables are buffer-local to that buffer.
9052         (set-buffer gnus-summary-buffer)
9053         ;; We bind all these variables that are used in the `eval' form
9054         ;; below.
9055         (let* ((mformat (symbol-value
9056                          (intern
9057                           (format "gnus-%s-mode-line-format-spec" where))))
9058                (gnus-tmp-group-name gnus-newsgroup-name)
9059                (gnus-tmp-article-number (or gnus-current-article 0))
9060                (gnus-tmp-unread gnus-newsgroup-unreads)
9061                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
9062                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
9063                (gnus-tmp-unread-and-unselected
9064                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
9065                             (zerop gnus-tmp-unselected)) "")
9066                       ((zerop gnus-tmp-unselected)
9067                        (format "{%d more}" gnus-tmp-unread-and-unticked))
9068                       (t (format "{%d(+%d) more}"
9069                                  gnus-tmp-unread-and-unticked
9070                                  gnus-tmp-unselected))))
9071                (gnus-tmp-subject
9072                 (if (and gnus-current-headers
9073                          (vectorp gnus-current-headers))
9074                     (gnus-mode-string-quote
9075                      (mail-header-subject gnus-current-headers)) ""))
9076                max-len
9077                gnus-tmp-header);; passed as argument to any user-format-funcs
9078           (setq mode-string (eval mformat))
9079           (setq max-len (max 4 (if gnus-mode-non-string-length
9080                                    (- (window-width)
9081                                       gnus-mode-non-string-length)
9082                                  (length mode-string))))
9083           ;; We might have to chop a bit of the string off...
9084           (when (> (length mode-string) max-len)
9085             (setq mode-string
9086                   (concat (gnus-truncate-string mode-string (- max-len 3))
9087                           "...")))
9088           ;; Pad the mode string a bit.
9089           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
9090       ;; Update the mode line.
9091       (setq mode-line-buffer-identification 
9092             (gnus-mode-line-buffer-identification
9093              (list mode-string)))
9094       (set-buffer-modified-p t))))
9095
9096 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
9097   "Go through the HEADERS list and add all Xrefs to a hash table.
9098 The resulting hash table is returned, or nil if no Xrefs were found."
9099   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
9100          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
9101          (xref-hashtb (make-vector 63 0))
9102          start group entry number xrefs header)
9103     (while headers
9104       (setq header (pop headers))
9105       (when (and (setq xrefs (mail-header-xref header))
9106                  (not (memq (setq number (mail-header-number header))
9107                             unreads)))
9108         (setq start 0)
9109         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
9110           (setq start (match-end 0))
9111           (setq group (if prefix
9112                           (concat prefix (substring xrefs (match-beginning 1)
9113                                                     (match-end 1)))
9114                         (substring xrefs (match-beginning 1) (match-end 1))))
9115           (setq number
9116                 (string-to-int (substring xrefs (match-beginning 2)
9117                                           (match-end 2))))
9118           (if (setq entry (gnus-gethash group xref-hashtb))
9119               (setcdr entry (cons number (cdr entry)))
9120             (gnus-sethash group (cons number nil) xref-hashtb)))))
9121     (and start xref-hashtb)))
9122
9123 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
9124   "Look through all the headers and mark the Xrefs as read."
9125   (let ((virtual (gnus-virtual-group-p from-newsgroup))
9126         name entry info xref-hashtb idlist method nth4)
9127     (save-excursion
9128       (set-buffer gnus-group-buffer)
9129       (when (setq xref-hashtb
9130                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
9131         (mapatoms
9132          (lambda (group)
9133            (unless (string= from-newsgroup (setq name (symbol-name group)))
9134              (setq idlist (symbol-value group))
9135              ;; Dead groups are not updated.
9136              (and (prog1
9137                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
9138                             info (nth 2 entry))
9139                     (if (stringp (setq nth4 (gnus-info-method info)))
9140                         (setq nth4 (gnus-server-to-method nth4))))
9141                   ;; Only do the xrefs if the group has the same
9142                   ;; select method as the group we have just read.
9143                   (or (gnus-methods-equal-p
9144                        nth4 (gnus-find-method-for-group from-newsgroup))
9145                       virtual
9146                       (equal nth4 (setq method (gnus-find-method-for-group
9147                                                 from-newsgroup)))
9148                       (and (equal (car nth4) (car method))
9149                            (equal (nth 1 nth4) (nth 1 method))))
9150                   gnus-use-cross-reference
9151                   (or (not (eq gnus-use-cross-reference t))
9152                       virtual
9153                       ;; Only do cross-references on subscribed
9154                       ;; groups, if that is what is wanted.
9155                       (<= (gnus-info-level info) gnus-level-subscribed))
9156                   (gnus-group-make-articles-read name idlist))))
9157          xref-hashtb)))))
9158
9159 (defun gnus-group-make-articles-read (group articles)
9160   (let* ((num 0)
9161          (entry (gnus-gethash group gnus-newsrc-hashtb))
9162          (info (nth 2 entry))
9163          (active (gnus-active group))
9164          range)
9165     ;; First peel off all illegal article numbers.
9166     (if active
9167         (let ((ids articles)
9168               id first)
9169           (while ids
9170             (setq id (car ids))
9171             (if (and first (> id (cdr active)))
9172                 (progn
9173                   ;; We'll end up in this situation in one particular
9174                   ;; obscure situation.  If you re-scan a group and get
9175                   ;; a new article that is cross-posted to a different
9176                   ;; group that has not been re-scanned, you might get
9177                   ;; crossposted article that has a higher number than
9178                   ;; Gnus believes possible.  So we re-activate this
9179                   ;; group as well.  This might mean doing the
9180                   ;; crossposting thingy will *increase* the number
9181                   ;; of articles in some groups.  Tsk, tsk.
9182                   (setq active (or (gnus-activate-group group) active))))
9183             (if (or (> id (cdr active))
9184                     (< id (car active)))
9185                 (setq articles (delq id articles)))
9186             (setq ids (cdr ids)))))
9187     ;; If the read list is nil, we init it.
9188     (and active
9189          (null (gnus-info-read info))
9190          (> (car active) 1)
9191          (gnus-info-set-read info (cons 1 (1- (car active)))))
9192     ;; Then we add the read articles to the range.
9193     (gnus-info-set-read
9194      info
9195      (setq range
9196            (gnus-add-to-range
9197             (gnus-info-read info) (setq articles (sort articles '<)))))
9198     ;; Then we have to re-compute how many unread
9199     ;; articles there are in this group.
9200     (if active
9201         (progn
9202           (cond
9203            ((not range)
9204             (setq num (- (1+ (cdr active)) (car active))))
9205            ((not (listp (cdr range)))
9206             (setq num (- (cdr active) (- (1+ (cdr range))
9207                                          (car range)))))
9208            (t
9209             (while range
9210               (if (numberp (car range))
9211                   (setq num (1+ num))
9212                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9213               (setq range (cdr range)))
9214             (setq num (- (cdr active) num))))
9215           ;; Update the number of unread articles.
9216           (setcar entry num)
9217           ;; Update the group buffer.
9218           (gnus-group-update-group group t)))))
9219
9220 (defun gnus-methods-equal-p (m1 m2)
9221   (let ((m1 (or m1 gnus-select-method))
9222         (m2 (or m2 gnus-select-method)))
9223     (or (equal m1 m2)
9224         (and (eq (car m1) (car m2))
9225              (or (not (memq 'address (assoc (symbol-name (car m1))
9226                                             gnus-valid-select-methods)))
9227                  (equal (nth 1 m1) (nth 1 m2)))))))
9228
9229 (defsubst gnus-header-value ()
9230   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9231
9232 (defvar gnus-newsgroup-none-id 0)
9233
9234 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9235   (let ((cur nntp-server-buffer)
9236         (dependencies
9237          (or dependencies
9238              (save-excursion (set-buffer gnus-summary-buffer)
9239                              gnus-newsgroup-dependencies)))
9240         headers id id-dep ref-dep end ref)
9241     (save-excursion
9242       (set-buffer nntp-server-buffer)
9243       (run-hooks 'gnus-parse-headers-hook)
9244       (let ((case-fold-search t)
9245             in-reply-to header p lines)
9246         (goto-char (point-min))
9247         ;; Search to the beginning of the next header.  Error messages
9248         ;; do not begin with 2 or 3.
9249         (while (re-search-forward "^[23][0-9]+ " nil t)
9250           (setq id nil
9251                 ref nil)
9252           ;; This implementation of this function, with nine
9253           ;; search-forwards instead of the one re-search-forward and
9254           ;; a case (which basically was the old function) is actually
9255           ;; about twice as fast, even though it looks messier.  You
9256           ;; can't have everything, I guess.  Speed and elegance
9257           ;; doesn't always go hand in hand.
9258           (setq
9259            header
9260            (vector
9261             ;; Number.
9262             (prog1
9263                 (read cur)
9264               (end-of-line)
9265               (setq p (point))
9266               (narrow-to-region (point)
9267                                 (or (and (search-forward "\n.\n" nil t)
9268                                          (- (point) 2))
9269                                     (point))))
9270             ;; Subject.
9271             (progn
9272               (goto-char p)
9273               (if (search-forward "\nsubject: " nil t)
9274                   (gnus-header-value) "(none)"))
9275             ;; From.
9276             (progn
9277               (goto-char p)
9278               (if (search-forward "\nfrom: " nil t)
9279                   (gnus-header-value) "(nobody)"))
9280             ;; Date.
9281             (progn
9282               (goto-char p)
9283               (if (search-forward "\ndate: " nil t)
9284                   (gnus-header-value) ""))
9285             ;; Message-ID.
9286             (progn
9287               (goto-char p)
9288               (if (search-forward "\nmessage-id: " nil t)
9289                   (setq id (gnus-header-value))
9290                 ;; If there was no message-id, we just fake one to make
9291                 ;; subsequent routines simpler.
9292                 (setq id (concat "none+"
9293                                  (int-to-string
9294                                   (setq gnus-newsgroup-none-id
9295                                         (1+ gnus-newsgroup-none-id)))))))
9296             ;; References.
9297             (progn
9298               (goto-char p)
9299               (if (search-forward "\nreferences: " nil t)
9300                   (progn
9301                     (setq end (point))
9302                     (prog1
9303                         (gnus-header-value)
9304                       (setq ref
9305                             (buffer-substring
9306                              (progn
9307                                (end-of-line)
9308                                (search-backward ">" end t)
9309                                (1+ (point)))
9310                              (progn
9311                                (search-backward "<" end t)
9312                                (point))))))
9313                 ;; Get the references from the in-reply-to header if there
9314                 ;; were no references and the in-reply-to header looks
9315                 ;; promising.
9316                 (if (and (search-forward "\nin-reply-to: " nil t)
9317                          (setq in-reply-to (gnus-header-value))
9318                          (string-match "<[^>]+>" in-reply-to))
9319                     (setq ref (substring in-reply-to (match-beginning 0)
9320                                          (match-end 0)))
9321                   (setq ref ""))))
9322             ;; Chars.
9323             0
9324             ;; Lines.
9325             (progn
9326               (goto-char p)
9327               (if (search-forward "\nlines: " nil t)
9328                   (if (numberp (setq lines (read cur)))
9329                       lines 0)
9330                 0))
9331             ;; Xref.
9332             (progn
9333               (goto-char p)
9334               (and (search-forward "\nxref: " nil t)
9335                    (gnus-header-value)))))
9336           ;; We do the threading while we read the headers.  The
9337           ;; message-id and the last reference are both entered into
9338           ;; the same hash table.  Some tippy-toeing around has to be
9339           ;; done in case an article has arrived before the article
9340           ;; which it refers to.
9341           (if (boundp (setq id-dep (intern id dependencies)))
9342               (if (and (car (symbol-value id-dep))
9343                        (not force-new))
9344                   ;; An article with this Message-ID has already
9345                   ;; been seen, so we ignore this one, except we add
9346                   ;; any additional Xrefs (in case the two articles
9347                   ;; came from different servers).
9348                   (progn
9349                     (mail-header-set-xref
9350                      (car (symbol-value id-dep))
9351                      (concat (or (mail-header-xref
9352                                   (car (symbol-value id-dep))) "")
9353                              (or (mail-header-xref header) "")))
9354                     (setq header nil))
9355                 (setcar (symbol-value id-dep) header))
9356             (set id-dep (list header)))
9357           (when header
9358             (if (boundp (setq ref-dep (intern ref dependencies)))
9359                 (setcdr (symbol-value ref-dep)
9360                         (nconc (cdr (symbol-value ref-dep))
9361                                (list (symbol-value id-dep))))
9362               (set ref-dep (list nil (symbol-value id-dep))))
9363             (setq headers (cons header headers)))
9364           (goto-char (point-max))
9365           (widen))
9366         (nreverse headers)))))
9367
9368 ;; The following macros and functions were written by Felix Lee
9369 ;; <flee@cse.psu.edu>.
9370
9371 (defmacro gnus-nov-read-integer ()
9372   '(prog1
9373        (if (= (following-char) ?\t)
9374            0
9375          (let ((num (condition-case nil (read buffer) (error nil))))
9376            (if (numberp num) num 0)))
9377      (or (eobp) (forward-char 1))))
9378
9379 (defmacro gnus-nov-skip-field ()
9380   '(search-forward "\t" eol 'move))
9381
9382 (defmacro gnus-nov-field ()
9383   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9384
9385 ;; Goes through the xover lines and returns a list of vectors
9386 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9387                                                   force-new dependencies)
9388   "Parse the news overview data in the server buffer, and return a
9389 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9390   ;; Get the Xref when the users reads the articles since most/some
9391   ;; NNTP servers do not include Xrefs when using XOVER.
9392   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9393   (let ((cur nntp-server-buffer)
9394         (dependencies (or dependencies gnus-newsgroup-dependencies))
9395         number headers header)
9396     (save-excursion
9397       (set-buffer nntp-server-buffer)
9398       ;; Allow the user to mangle the headers before parsing them.
9399       (run-hooks 'gnus-parse-headers-hook)
9400       (goto-char (point-min))
9401       (while (and sequence (not (eobp)))
9402         (setq number (read cur))
9403         (while (and sequence (< (car sequence) number))
9404           (setq sequence (cdr sequence)))
9405         (and sequence
9406              (eq number (car sequence))
9407              (progn
9408                (setq sequence (cdr sequence))
9409                (if (setq header
9410                          (inline (gnus-nov-parse-line
9411                                   number dependencies force-new)))
9412                    (setq headers (cons header headers)))))
9413         (forward-line 1))
9414       (setq headers (nreverse headers)))
9415     headers))
9416
9417 ;; This function has to be called with point after the article number
9418 ;; on the beginning of the line.
9419 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9420   (let ((none 0)
9421         (eol (gnus-point-at-eol))
9422         (buffer (current-buffer))
9423         header ref id id-dep ref-dep)
9424
9425     ;; overview: [num subject from date id refs chars lines misc]
9426     (narrow-to-region (point) eol)
9427     (or (eobp) (forward-char))
9428
9429     (condition-case nil
9430         (setq header
9431               (vector
9432                number                   ; number
9433                (gnus-nov-field)         ; subject
9434                (gnus-nov-field)         ; from
9435                (gnus-nov-field)         ; date
9436                (setq id (or (gnus-nov-field)
9437                             (concat "none+"
9438                                     (int-to-string
9439                                      (setq none (1+ none)))))) ; id
9440                (progn
9441                  (save-excursion
9442                    (let ((beg (point)))
9443                      (search-forward "\t" eol)
9444                      (if (search-backward ">" beg t)
9445                          (setq ref
9446                                (buffer-substring
9447                                 (1+ (point))
9448                                 (search-backward "<" beg t)))
9449                        (setq ref nil))))
9450                  (gnus-nov-field))      ; refs
9451                (gnus-nov-read-integer)  ; chars
9452                (gnus-nov-read-integer)  ; lines
9453                (if (= (following-char) ?\n)
9454                    nil
9455                  (gnus-nov-field))      ; misc
9456                ))
9457       (error (progn
9458                (gnus-error 4 "Strange nov line")
9459                (setq header nil)
9460                (goto-char eol))))
9461
9462     (widen)
9463
9464     ;; We build the thread tree.
9465     (when header
9466       (if (boundp (setq id-dep (intern id dependencies)))
9467           (if (and (car (symbol-value id-dep))
9468                    (not force-new))
9469               ;; An article with this Message-ID has already been seen,
9470               ;; so we ignore this one, except we add any additional
9471               ;; Xrefs (in case the two articles came from different
9472               ;; servers.
9473               (progn
9474                 (mail-header-set-xref
9475                  (car (symbol-value id-dep))
9476                  (concat (or (mail-header-xref
9477                               (car (symbol-value id-dep))) "")
9478                          (or (mail-header-xref header) "")))
9479                 (setq header nil))
9480             (setcar (symbol-value id-dep) header))
9481         (set id-dep (list header))))
9482     (when header
9483       (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9484           (setcdr (symbol-value ref-dep)
9485                   (nconc (cdr (symbol-value ref-dep))
9486                          (list (symbol-value id-dep))))
9487         (set ref-dep (list nil (symbol-value id-dep)))))
9488     header))
9489
9490 (defun gnus-article-get-xrefs ()
9491   "Fill in the Xref value in `gnus-current-headers', if necessary.
9492 This is meant to be called in `gnus-article-internal-prepare-hook'."
9493   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9494                                  gnus-current-headers)))
9495     (or (not gnus-use-cross-reference)
9496         (not headers)
9497         (and (mail-header-xref headers)
9498              (not (string= (mail-header-xref headers) "")))
9499         (let ((case-fold-search t)
9500               xref)
9501           (save-restriction
9502             (nnheader-narrow-to-headers)
9503             (goto-char (point-min))
9504             (if (or (and (eq (downcase (following-char)) ?x)
9505                          (looking-at "Xref:"))
9506                     (search-forward "\nXref:" nil t))
9507                 (progn
9508                   (goto-char (1+ (match-end 0)))
9509                   (setq xref (buffer-substring (point)
9510                                                (progn (end-of-line) (point))))
9511                   (mail-header-set-xref headers xref))))))))
9512
9513 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
9514   "Find article ID and insert the summary line for that article."
9515   (let ((header (if (and old-header use-old-header)
9516                     old-header (gnus-read-header id)))
9517         (number (and (numberp id) id))
9518         pos)
9519     (when header
9520       ;; Rebuild the thread that this article is part of and go to the
9521       ;; article we have fetched.
9522       (when (and (not gnus-show-threads)
9523                  old-header)
9524         (when (setq pos (text-property-any
9525                          (point-min) (point-max) 'gnus-number 
9526                          (mail-header-number old-header)))
9527           (goto-char pos)
9528           (gnus-delete-line)
9529           (gnus-data-remove (mail-header-number old-header))))
9530       (when old-header
9531         (mail-header-set-number header (mail-header-number old-header)))
9532       (setq gnus-newsgroup-sparse
9533             (delq (setq number (mail-header-number header)) 
9534                   gnus-newsgroup-sparse))
9535       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
9536       (gnus-rebuild-thread (mail-header-id header))
9537       (gnus-summary-goto-subject number nil t))
9538     (when (and (numberp number)
9539                (> number 0))
9540       ;; We have to update the boundaries even if we can't fetch the
9541       ;; article if ID is a number -- so that the next `P' or `N'
9542       ;; command will fetch the previous (or next) article even
9543       ;; if the one we tried to fetch this time has been canceled.
9544       (and (> number gnus-newsgroup-end)
9545            (setq gnus-newsgroup-end number))
9546       (and (< number gnus-newsgroup-begin)
9547            (setq gnus-newsgroup-begin number))
9548       (setq gnus-newsgroup-unselected
9549             (delq number gnus-newsgroup-unselected)))
9550     ;; Report back a success?
9551     (and header (mail-header-number header))))
9552
9553 (defun gnus-summary-work-articles (n)
9554   "Return a list of articles to be worked upon.  The prefix argument,
9555 the list of process marked articles, and the current article will be
9556 taken into consideration."
9557   (cond
9558    (n
9559     ;; A numerical prefix has been given.
9560     (let ((backward (< n 0))
9561           (n (abs (prefix-numeric-value n)))
9562           articles article)
9563       (save-excursion
9564         (while
9565             (and (> n 0)
9566                  (push (setq article (gnus-summary-article-number))
9567                        articles)
9568                  (if backward
9569                      (gnus-summary-find-prev nil article)
9570                    (gnus-summary-find-next nil article)))
9571           (decf n)))
9572       (nreverse articles)))
9573    ((and (boundp 'transient-mark-mode)
9574          transient-mark-mode
9575          mark-active)
9576     ;; Work on the region between point and mark.
9577     (let ((max (max (point) (mark)))
9578           articles article)
9579       (save-excursion
9580         (goto-char (min (point) (mark)))
9581         (while
9582             (and
9583              (push (setq article (gnus-summary-article-number)) articles)
9584              (gnus-summary-find-next nil article)
9585              (< (point) max)))
9586         (nreverse articles))))
9587    (gnus-newsgroup-processable
9588     ;; There are process-marked articles present.
9589     (reverse gnus-newsgroup-processable))
9590    (t
9591     ;; Just return the current article.
9592     (list (gnus-summary-article-number)))))
9593
9594 (defun gnus-summary-search-group (&optional backward use-level)
9595   "Search for next unread newsgroup.
9596 If optional argument BACKWARD is non-nil, search backward instead."
9597   (save-excursion
9598     (set-buffer gnus-group-buffer)
9599     (if (gnus-group-search-forward
9600          backward nil (if use-level (gnus-group-group-level) nil))
9601         (gnus-group-group-name))))
9602
9603 (defun gnus-summary-best-group (&optional exclude-group)
9604   "Find the name of the best unread group.
9605 If EXCLUDE-GROUP, do not go to this group."
9606   (save-excursion
9607     (set-buffer gnus-group-buffer)
9608     (save-excursion
9609       (gnus-group-best-unread-group exclude-group))))
9610
9611 (defun gnus-summary-find-next (&optional unread article backward)
9612   (if backward (gnus-summary-find-prev)
9613     (let* ((dummy (gnus-summary-article-intangible-p))
9614            (article (or article (gnus-summary-article-number)))
9615            (arts (gnus-data-find-list article))
9616            result)
9617       (when (and (not dummy)
9618                  (or (not gnus-summary-check-current)
9619                      (not unread)
9620                      (not (gnus-data-unread-p (car arts)))))
9621         (setq arts (cdr arts)))
9622       (when (setq result
9623                   (if unread
9624                       (progn
9625                         (while arts
9626                           (when (gnus-data-unread-p (car arts))
9627                             (setq result (car arts)
9628                                   arts nil))
9629                           (setq arts (cdr arts)))
9630                         result)
9631                     (car arts)))
9632         (goto-char (gnus-data-pos result))
9633         (gnus-data-number result)))))
9634
9635 (defun gnus-summary-find-prev (&optional unread article)
9636   (let* ((eobp (eobp))
9637          (article (or article (gnus-summary-article-number)))
9638          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9639          result)
9640     (when (and (not eobp)
9641                (or (not gnus-summary-check-current)
9642                    (not unread)
9643                    (not (gnus-data-unread-p (car arts)))))
9644       (setq arts (cdr arts)))
9645     (if (setq result
9646               (if unread
9647                   (progn
9648                     (while arts
9649                       (and (gnus-data-unread-p (car arts))
9650                            (setq result (car arts)
9651                                  arts nil))
9652                       (setq arts (cdr arts)))
9653                     result)
9654                 (car arts)))
9655         (progn
9656           (goto-char (gnus-data-pos result))
9657           (gnus-data-number result)))))
9658
9659 (defun gnus-summary-find-subject (subject &optional unread backward article)
9660   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9661          (article (or article (gnus-summary-article-number)))
9662          (articles (gnus-data-list backward))
9663          (arts (gnus-data-find-list article articles))
9664          result)
9665     (when (or (not gnus-summary-check-current)
9666               (not unread)
9667               (not (gnus-data-unread-p (car arts))))
9668       (setq arts (cdr arts)))
9669     (while arts
9670       (and (or (not unread)
9671                (gnus-data-unread-p (car arts)))
9672            (vectorp (gnus-data-header (car arts)))
9673            (gnus-subject-equal
9674             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9675            (setq result (car arts)
9676                  arts nil))
9677       (setq arts (cdr arts)))
9678     (and result
9679          (goto-char (gnus-data-pos result))
9680          (gnus-data-number result))))
9681
9682 (defun gnus-summary-search-forward (&optional unread subject backward)
9683   "Search forward for an article.
9684 If UNREAD, look for unread articles.  If SUBJECT, look for
9685 articles with that subject.  If BACKWARD, search backward instead."
9686   (cond (subject (gnus-summary-find-subject subject unread backward))
9687         (backward (gnus-summary-find-prev unread))
9688         (t (gnus-summary-find-next unread))))
9689
9690 (defun gnus-recenter (&optional n)
9691   "Center point in window and redisplay frame.
9692 Also do horizontal recentering."
9693   (interactive "P")
9694   (when (and gnus-auto-center-summary
9695              (not (eq gnus-auto-center-summary 'vertical)))
9696     (gnus-horizontal-recenter))
9697   (recenter n))
9698
9699 (defun gnus-summary-recenter ()
9700   "Center point in the summary window.
9701 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9702 displayed, no centering will be performed."
9703   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9704   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9705   (let* ((top (cond ((< (window-height) 4) 0)
9706                     ((< (window-height) 7) 1)
9707                     (t 2)))
9708          (height (1- (window-height)))
9709          (bottom (save-excursion (goto-char (point-max))
9710                                  (forward-line (- height))
9711                                  (point)))
9712          (window (get-buffer-window (current-buffer))))
9713     ;; The user has to want it.
9714     (when gnus-auto-center-summary
9715       (when (get-buffer-window gnus-article-buffer)
9716        ;; Only do recentering when the article buffer is displayed,
9717        ;; Set the window start to either `bottom', which is the biggest
9718        ;; possible valid number, or the second line from the top,
9719        ;; whichever is the least.
9720        (set-window-start
9721         window (min bottom (save-excursion 
9722                              (forward-line (- top)) (point)))))
9723       ;; Do horizontal recentering while we're at it.
9724       (when (and (get-buffer-window (current-buffer) t)
9725                  (not (eq gnus-auto-center-summary 'vertical)))
9726         (let ((selected (selected-window)))
9727           (select-window (get-buffer-window (current-buffer) t))
9728           (gnus-summary-position-point)
9729           (gnus-horizontal-recenter)
9730           (select-window selected))))))
9731
9732 (defun gnus-horizontal-recenter ()
9733   "Recenter the current buffer horizontally."
9734   (if (< (current-column) (/ (window-width) 2))
9735       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9736     (let* ((orig (point))
9737            (end (window-end (get-buffer-window (current-buffer) t)))
9738            (max 0))
9739       ;; Find the longest line currently displayed in the window.
9740       (goto-char (window-start))
9741       (while (and (not (eobp)) 
9742                   (< (point) end))
9743         (end-of-line)
9744         (setq max (max max (current-column)))
9745         (forward-line 1))
9746       (goto-char orig)
9747       ;; Scroll horizontally to center (sort of) the point.
9748       (if (> max (window-width))
9749           (set-window-hscroll 
9750            (get-buffer-window (current-buffer) t)
9751            (min (- (current-column) (/ (window-width) 3))
9752                 (+ 2 (- max (window-width)))))
9753         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9754       max)))
9755
9756 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9757 (defun gnus-short-group-name (group &optional levels)
9758   "Collapse GROUP name LEVELS."
9759   (let* ((name "") 
9760          (foreign "")
9761          (depth 0) 
9762          (skip 1)
9763          (levels (or levels
9764                      (progn
9765                        (while (string-match "\\." group skip)
9766                          (setq skip (match-end 0)
9767                                depth (+ depth 1)))
9768                        depth))))
9769     (if (string-match ":" group)
9770         (setq foreign (substring group 0 (match-end 0))
9771               group (substring group (match-end 0))))
9772     (while group
9773       (if (and (string-match "\\." group)
9774                (> levels (- gnus-group-uncollapsed-levels 1)))
9775           (setq name (concat name (substring group 0 1))
9776                 group (substring group (match-end 0))
9777                 levels (- levels 1)
9778                 name (concat name "."))
9779         (setq name (concat foreign name group)
9780               group nil)))
9781     name))
9782
9783 (defun gnus-summary-jump-to-group (newsgroup)
9784   "Move point to NEWSGROUP in group mode buffer."
9785   ;; Keep update point of group mode buffer if visible.
9786   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9787       (save-window-excursion
9788         ;; Take care of tree window mode.
9789         (if (get-buffer-window gnus-group-buffer)
9790             (pop-to-buffer gnus-group-buffer))
9791         (gnus-group-jump-to-group newsgroup))
9792     (save-excursion
9793       ;; Take care of tree window mode.
9794       (if (get-buffer-window gnus-group-buffer)
9795           (pop-to-buffer gnus-group-buffer)
9796         (set-buffer gnus-group-buffer))
9797       (gnus-group-jump-to-group newsgroup))))
9798
9799 ;; This function returns a list of article numbers based on the
9800 ;; difference between the ranges of read articles in this group and
9801 ;; the range of active articles.
9802 (defun gnus-list-of-unread-articles (group)
9803   (let* ((read (gnus-info-read (gnus-get-info group)))
9804          (active (gnus-active group))
9805          (last (cdr active))
9806          first nlast unread)
9807     ;; If none are read, then all are unread.
9808     (if (not read)
9809         (setq first (car active))
9810       ;; If the range of read articles is a single range, then the
9811       ;; first unread article is the article after the last read
9812       ;; article.  Sounds logical, doesn't it?
9813       (if (not (listp (cdr read)))
9814           (setq first (1+ (cdr read)))
9815         ;; `read' is a list of ranges.
9816         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9817                                 (caar read))) 1)
9818             (setq first 1))
9819         (while read
9820           (if first
9821               (while (< first nlast)
9822                 (setq unread (cons first unread))
9823                 (setq first (1+ first))))
9824           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9825           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9826           (setq read (cdr read)))))
9827     ;; And add the last unread articles.
9828     (while (<= first last)
9829       (setq unread (cons first unread))
9830       (setq first (1+ first)))
9831     ;; Return the list of unread articles.
9832     (nreverse unread)))
9833
9834 (defun gnus-list-of-read-articles (group)
9835   "Return a list of unread, unticked and non-dormant articles."
9836   (let* ((info (gnus-get-info group))
9837          (marked (gnus-info-marks info))
9838          (active (gnus-active group)))
9839     (and info active
9840          (gnus-set-difference
9841           (gnus-sorted-complement
9842            (gnus-uncompress-range active)
9843            (gnus-list-of-unread-articles group))
9844           (append
9845            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9846            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9847
9848 ;; Various summary commands
9849
9850 (defun gnus-summary-universal-argument (arg)
9851   "Perform any operation on all articles that are process/prefixed."
9852   (interactive "P")
9853   (gnus-set-global-variables)
9854   (let ((articles (gnus-summary-work-articles arg))
9855         func article)
9856     (if (eq
9857          (setq
9858           func
9859           (key-binding
9860            (read-key-sequence
9861             (substitute-command-keys
9862              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9863              ))))
9864          'undefined)
9865         (gnus-error 1 "Undefined key")
9866       (save-excursion
9867         (while articles
9868           (gnus-summary-goto-subject (setq article (pop articles)))
9869           (command-execute func)
9870           (gnus-summary-remove-process-mark article)))))
9871   (gnus-summary-position-point))
9872
9873 (defun gnus-summary-toggle-truncation (&optional arg)
9874   "Toggle truncation of summary lines.
9875 With arg, turn line truncation on iff arg is positive."
9876   (interactive "P")
9877   (setq truncate-lines
9878         (if (null arg) (not truncate-lines)
9879           (> (prefix-numeric-value arg) 0)))
9880   (redraw-display))
9881
9882 (defun gnus-summary-reselect-current-group (&optional all rescan)
9883   "Exit and then reselect the current newsgroup.
9884 The prefix argument ALL means to select all articles."
9885   (interactive "P")
9886   (gnus-set-global-variables)
9887   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
9888     (error "Ephemeral groups can't be reselected"))
9889   (let ((current-subject (gnus-summary-article-number))
9890         (group gnus-newsgroup-name))
9891     (setq gnus-newsgroup-begin nil)
9892     (gnus-summary-exit)
9893     ;; We have to adjust the point of group mode buffer because the
9894     ;; current point was moved to the next unread newsgroup by
9895     ;; exiting.
9896     (gnus-summary-jump-to-group group)
9897     (when rescan
9898       (save-excursion
9899         (gnus-group-get-new-news-this-group 1)))
9900     (gnus-group-read-group all t)
9901     (gnus-summary-goto-subject current-subject nil t)))
9902
9903 (defun gnus-summary-rescan-group (&optional all)
9904   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9905   (interactive "P")
9906   (gnus-summary-reselect-current-group all t))
9907
9908 (defun gnus-summary-update-info ()
9909   (let* ((group gnus-newsgroup-name))
9910     (when gnus-newsgroup-kill-headers
9911       (setq gnus-newsgroup-killed
9912             (gnus-compress-sequence
9913              (nconc
9914               (gnus-set-sorted-intersection
9915                (gnus-uncompress-range gnus-newsgroup-killed)
9916                (setq gnus-newsgroup-unselected
9917                      (sort gnus-newsgroup-unselected '<)))
9918               (setq gnus-newsgroup-unreads
9919                     (sort gnus-newsgroup-unreads '<))) t)))
9920     (unless (listp (cdr gnus-newsgroup-killed))
9921       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9922     (let ((headers gnus-newsgroup-headers))
9923       (run-hooks 'gnus-exit-group-hook)
9924       (unless gnus-save-score
9925         (setq gnus-newsgroup-scored nil))
9926       ;; Set the new ranges of read articles.
9927       (gnus-update-read-articles
9928        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9929       ;; Set the current article marks.
9930       (gnus-update-marks)
9931       ;; Do the cross-ref thing.
9932       (when gnus-use-cross-reference
9933         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9934       ;; Do adaptive scoring, and possibly save score files.
9935       (when gnus-newsgroup-adaptive
9936         (gnus-score-adaptive))
9937       (when gnus-use-scoring
9938         (gnus-score-save))
9939       ;; Do not switch windows but change the buffer to work.
9940       (set-buffer gnus-group-buffer)
9941       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9942           (gnus-group-update-group group)))))
9943
9944 (defun gnus-summary-exit (&optional temporary)
9945   "Exit reading current newsgroup, and then return to group selection mode.
9946 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9947   (interactive)
9948   (gnus-set-global-variables)
9949   (gnus-kill-save-kill-buffer)
9950   (let* ((group gnus-newsgroup-name)
9951          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9952          (mode major-mode)
9953          (buf (current-buffer)))
9954     (run-hooks 'gnus-summary-prepare-exit-hook)
9955     ;; If we have several article buffers, we kill them at exit.
9956     (unless gnus-single-article-buffer
9957       (gnus-kill-buffer gnus-original-article-buffer)
9958       (setq gnus-article-current nil))
9959     (when gnus-use-cache
9960       (gnus-cache-possibly-remove-articles)
9961       (gnus-cache-save-buffers))
9962     (when gnus-use-trees
9963       (gnus-tree-close group))
9964     ;; Make all changes in this group permanent.
9965     (unless quit-config
9966       (gnus-summary-update-info))
9967     (gnus-close-group group)
9968     ;; Make sure where I was, and go to next newsgroup.
9969     (set-buffer gnus-group-buffer)
9970     (unless quit-config
9971       (gnus-group-jump-to-group group))
9972     (run-hooks 'gnus-summary-exit-hook)
9973     (unless quit-config
9974       (gnus-group-next-unread-group 1))
9975     (if temporary
9976         nil                             ;Nothing to do.
9977       ;; If we have several article buffers, we kill them at exit.
9978       (unless gnus-single-article-buffer
9979         (gnus-kill-buffer gnus-article-buffer)
9980         (gnus-kill-buffer gnus-original-article-buffer)
9981         (setq gnus-article-current nil))
9982       (set-buffer buf)
9983       (if (not gnus-kill-summary-on-exit)
9984           (gnus-deaden-summary)
9985         ;; We set all buffer-local variables to nil.  It is unclear why
9986         ;; this is needed, but if we don't, buffer-local variables are
9987         ;; not garbage-collected, it seems.  This would the lead to en
9988         ;; ever-growing Emacs.
9989         (gnus-summary-clear-local-variables)
9990         (when (get-buffer gnus-article-buffer)
9991           (bury-buffer gnus-article-buffer))
9992         ;; We clear the global counterparts of the buffer-local
9993         ;; variables as well, just to be on the safe side.
9994         (gnus-configure-windows 'group 'force)
9995         (gnus-summary-clear-local-variables)
9996         ;; Return to group mode buffer.
9997         (if (eq mode 'gnus-summary-mode)
9998             (gnus-kill-buffer buf)))
9999       (setq gnus-current-select-method gnus-select-method)
10000       (pop-to-buffer gnus-group-buffer)
10001       ;; Clear the current group name.
10002       (if (not quit-config)
10003           (progn
10004             (gnus-group-jump-to-group group)
10005             (gnus-group-next-unread-group 1)
10006             (gnus-configure-windows 'group 'force))
10007         (if (not (buffer-name (car quit-config)))
10008             (gnus-configure-windows 'group 'force)
10009           (set-buffer (car quit-config))
10010           (and (eq major-mode 'gnus-summary-mode)
10011                (gnus-set-global-variables))
10012           (gnus-configure-windows (cdr quit-config))))
10013       (unless quit-config
10014         (setq gnus-newsgroup-name nil)))))
10015
10016 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
10017 (defun gnus-summary-exit-no-update (&optional no-questions)
10018   "Quit reading current newsgroup without updating read article info."
10019   (interactive)
10020   (gnus-set-global-variables)
10021   (let* ((group gnus-newsgroup-name)
10022          (quit-config (gnus-group-quit-config group)))
10023     (when (or no-questions
10024               gnus-expert-user
10025               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
10026       ;; If we have several article buffers, we kill them at exit.
10027       (unless gnus-single-article-buffer
10028         (gnus-kill-buffer gnus-article-buffer)
10029         (gnus-kill-buffer gnus-original-article-buffer)
10030         (setq gnus-article-current nil))
10031       (if (not gnus-kill-summary-on-exit)
10032           (gnus-deaden-summary)
10033         (gnus-close-group group)
10034         (gnus-summary-clear-local-variables)
10035         (set-buffer gnus-group-buffer)
10036         (gnus-summary-clear-local-variables)
10037         (when (get-buffer gnus-summary-buffer)
10038           (kill-buffer gnus-summary-buffer)))
10039       (unless gnus-single-article-buffer
10040         (setq gnus-article-current nil))
10041       (when gnus-use-trees
10042         (gnus-tree-close group))
10043       (when (get-buffer gnus-article-buffer)
10044         (bury-buffer gnus-article-buffer))
10045       ;; Return to the group buffer.
10046       (gnus-configure-windows 'group 'force)
10047       ;; Clear the current group name.
10048       (setq gnus-newsgroup-name nil)
10049       (when (equal (gnus-group-group-name) group)
10050         (gnus-group-next-unread-group 1))
10051       (when quit-config
10052         (if (not (buffer-name (car quit-config)))
10053             (gnus-configure-windows 'group 'force)
10054           (set-buffer (car quit-config))
10055           (when (eq major-mode 'gnus-summary-mode)
10056             (gnus-set-global-variables))
10057           (gnus-configure-windows (cdr quit-config)))))))
10058
10059 ;;; Dead summaries.
10060
10061 (defvar gnus-dead-summary-mode-map nil)
10062
10063 (if gnus-dead-summary-mode-map
10064     nil
10065   (setq gnus-dead-summary-mode-map (make-keymap))
10066   (suppress-keymap gnus-dead-summary-mode-map)
10067   (substitute-key-definition
10068    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
10069   (let ((keys '("\C-d" "\r" "\177")))
10070     (while keys
10071       (define-key gnus-dead-summary-mode-map
10072         (pop keys) 'gnus-summary-wake-up-the-dead))))
10073
10074 (defvar gnus-dead-summary-mode nil
10075   "Minor mode for Gnus summary buffers.")
10076
10077 (defun gnus-dead-summary-mode (&optional arg)
10078   "Minor mode for Gnus summary buffers."
10079   (interactive "P")
10080   (when (eq major-mode 'gnus-summary-mode)
10081     (make-local-variable 'gnus-dead-summary-mode)
10082     (setq gnus-dead-summary-mode
10083           (if (null arg) (not gnus-dead-summary-mode)
10084             (> (prefix-numeric-value arg) 0)))
10085     (when gnus-dead-summary-mode
10086       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
10087         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
10088       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
10089         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
10090               minor-mode-map-alist)))))
10091
10092 (defun gnus-deaden-summary ()
10093   "Make the current summary buffer into a dead summary buffer."
10094   ;; Kill any previous dead summary buffer.
10095   (when (and gnus-dead-summary
10096              (buffer-name gnus-dead-summary))
10097     (save-excursion
10098       (set-buffer gnus-dead-summary)
10099       (when gnus-dead-summary-mode
10100         (kill-buffer (current-buffer)))))
10101   ;; Make this the current dead summary.
10102   (setq gnus-dead-summary (current-buffer))
10103   (gnus-dead-summary-mode 1)
10104   (let ((name (buffer-name)))
10105     (when (string-match "Summary" name)
10106       (rename-buffer
10107        (concat (substring name 0 (match-beginning 0)) "Dead "
10108                (substring name (match-beginning 0))) t))))
10109
10110 (defun gnus-kill-or-deaden-summary (buffer)
10111   "Kill or deaden the summary BUFFER."
10112   (when (and (buffer-name buffer)
10113              (not gnus-single-article-buffer))
10114     (save-excursion
10115       (set-buffer buffer)
10116       (gnus-kill-buffer gnus-article-buffer)
10117       (gnus-kill-buffer gnus-original-article-buffer)))
10118   (cond (gnus-kill-summary-on-exit
10119          (when (and gnus-use-trees
10120                     (and (get-buffer buffer)
10121                          (buffer-name (get-buffer buffer))))
10122            (save-excursion
10123              (set-buffer (get-buffer buffer))
10124              (gnus-tree-close gnus-newsgroup-name)))
10125          (gnus-kill-buffer buffer))
10126         ((and (get-buffer buffer)
10127               (buffer-name (get-buffer buffer)))
10128          (save-excursion
10129            (set-buffer buffer)
10130            (gnus-deaden-summary)))))
10131
10132 (defun gnus-summary-wake-up-the-dead (&rest args)
10133   "Wake up the dead summary buffer."
10134   (interactive)
10135   (gnus-dead-summary-mode -1)
10136   (let ((name (buffer-name)))
10137     (when (string-match "Dead " name)
10138       (rename-buffer
10139        (concat (substring name 0 (match-beginning 0))
10140                (substring name (match-end 0))) t)))
10141   (gnus-message 3 "This dead summary is now alive again"))
10142
10143 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
10144 (defun gnus-summary-fetch-faq (&optional faq-dir)
10145   "Fetch the FAQ for the current group.
10146 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
10147 in."
10148   (interactive
10149    (list
10150     (if current-prefix-arg
10151         (completing-read
10152          "Faq dir: " (and (listp gnus-group-faq-directory)
10153                           gnus-group-faq-directory)))))
10154   (let (gnus-faq-buffer)
10155     (and (setq gnus-faq-buffer
10156                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
10157          (gnus-configure-windows 'summary-faq))))
10158
10159 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10160 (defun gnus-summary-describe-group (&optional force)
10161   "Describe the current newsgroup."
10162   (interactive "P")
10163   (gnus-group-describe-group force gnus-newsgroup-name))
10164
10165 (defun gnus-summary-describe-briefly ()
10166   "Describe summary mode commands briefly."
10167   (interactive)
10168   (gnus-message 6
10169                 (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")))
10170
10171 ;; Walking around group mode buffer from summary mode.
10172
10173 (defun gnus-summary-next-group (&optional no-article target-group backward)
10174   "Exit current newsgroup and then select next unread newsgroup.
10175 If prefix argument NO-ARTICLE is non-nil, no article is selected
10176 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
10177 previous group instead."
10178   (interactive "P")
10179   (gnus-set-global-variables)
10180   (let ((current-group gnus-newsgroup-name)
10181         (current-buffer (current-buffer))
10182         entered)
10183     ;; First we semi-exit this group to update Xrefs and all variables.
10184     ;; We can't do a real exit, because the window conf must remain
10185     ;; the same in case the user is prompted for info, and we don't
10186     ;; want the window conf to change before that...
10187     (gnus-summary-exit t)
10188     (while (not entered)
10189       ;; Then we find what group we are supposed to enter.
10190       (set-buffer gnus-group-buffer)
10191       (gnus-group-jump-to-group current-group)
10192       (setq target-group
10193             (or target-group
10194                 (if (eq gnus-keep-same-level 'best)
10195                     (gnus-summary-best-group gnus-newsgroup-name)
10196                   (gnus-summary-search-group backward gnus-keep-same-level))))
10197       (if (not target-group)
10198           ;; There are no further groups, so we return to the group
10199           ;; buffer.
10200           (progn
10201             (gnus-message 5 "Returning to the group buffer")
10202             (setq entered t)
10203             (set-buffer current-buffer)
10204             (gnus-summary-exit))
10205         ;; We try to enter the target group.
10206         (gnus-group-jump-to-group target-group)
10207         (let ((unreads (gnus-group-group-unread)))
10208           (if (and (or (eq t unreads)
10209                        (and unreads (not (zerop unreads))))
10210                    (gnus-summary-read-group
10211                     target-group nil no-article current-buffer))
10212               (setq entered t)
10213             (setq current-group target-group
10214                   target-group nil)))))))
10215
10216 (defun gnus-summary-prev-group (&optional no-article)
10217   "Exit current newsgroup and then select previous unread newsgroup.
10218 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10219   (interactive "P")
10220   (gnus-summary-next-group no-article nil t))
10221
10222 ;; Walking around summary lines.
10223
10224 (defun gnus-summary-first-subject (&optional unread)
10225   "Go to the first unread subject.
10226 If UNREAD is non-nil, go to the first unread article.
10227 Returns the article selected or nil if there are no unread articles."
10228   (interactive "P")
10229   (prog1
10230       (cond
10231        ;; Empty summary.
10232        ((null gnus-newsgroup-data)
10233         (gnus-message 3 "No articles in the group")
10234         nil)
10235        ;; Pick the first article.
10236        ((not unread)
10237         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10238         (gnus-data-number (car gnus-newsgroup-data)))
10239        ;; No unread articles.
10240        ((null gnus-newsgroup-unreads)
10241         (gnus-message 3 "No more unread articles")
10242         nil)
10243        ;; Find the first unread article.
10244        (t
10245         (let ((data gnus-newsgroup-data))
10246           (while (and data
10247                       (not (gnus-data-unread-p (car data))))
10248             (setq data (cdr data)))
10249           (if data
10250               (progn
10251                 (goto-char (gnus-data-pos (car data)))
10252                 (gnus-data-number (car data)))))))
10253     (gnus-summary-position-point)))
10254
10255 (defun gnus-summary-next-subject (n &optional unread dont-display)
10256   "Go to next N'th summary line.
10257 If N is negative, go to the previous N'th subject line.
10258 If UNREAD is non-nil, only unread articles are selected.
10259 The difference between N and the actual number of steps taken is
10260 returned."
10261   (interactive "p")
10262   (let ((backward (< n 0))
10263         (n (abs n)))
10264     (while (and (> n 0)
10265                 (if backward
10266                     (gnus-summary-find-prev unread)
10267                   (gnus-summary-find-next unread)))
10268       (setq n (1- n)))
10269     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10270                                (if unread " unread" "")))
10271     (unless dont-display
10272       (gnus-summary-recenter)
10273       (gnus-summary-position-point))
10274     n))
10275
10276 (defun gnus-summary-next-unread-subject (n)
10277   "Go to next N'th unread summary line."
10278   (interactive "p")
10279   (gnus-summary-next-subject n t))
10280
10281 (defun gnus-summary-prev-subject (n &optional unread)
10282   "Go to previous N'th summary line.
10283 If optional argument UNREAD is non-nil, only unread article is selected."
10284   (interactive "p")
10285   (gnus-summary-next-subject (- n) unread))
10286
10287 (defun gnus-summary-prev-unread-subject (n)
10288   "Go to previous N'th unread summary line."
10289   (interactive "p")
10290   (gnus-summary-next-subject (- n) t))
10291
10292 (defun gnus-summary-goto-subject (article &optional force silent)
10293   "Go the subject line of ARTICLE.
10294 If FORCE, also allow jumping to articles not currently shown."
10295   (let ((b (point))
10296         (data (gnus-data-find article)))
10297     ;; We read in the article if we have to.
10298     (and (not data)
10299          force
10300          (gnus-summary-insert-subject article (and (vectorp force) force) t)
10301          (setq data (gnus-data-find article)))
10302     (goto-char b)
10303     (if (not data)
10304         (progn
10305           (unless silent
10306             (gnus-message 3 "Can't find article %d" article))
10307           nil)
10308       (goto-char (gnus-data-pos data))
10309       article)))
10310
10311 ;; Walking around summary lines with displaying articles.
10312
10313 (defun gnus-summary-expand-window (&optional arg)
10314   "Make the summary buffer take up the entire Emacs frame.
10315 Given a prefix, will force an `article' buffer configuration."
10316   (interactive "P")
10317   (gnus-set-global-variables)
10318   (if arg
10319       (gnus-configure-windows 'article 'force)
10320     (gnus-configure-windows 'summary 'force)))
10321
10322 (defun gnus-summary-display-article (article &optional all-header)
10323   "Display ARTICLE in article buffer."
10324   (gnus-set-global-variables)
10325   (if (null article)
10326       nil
10327     (prog1
10328         (if gnus-summary-display-article-function
10329             (funcall gnus-summary-display-article-function article all-header)
10330           (gnus-article-prepare article all-header))
10331       (run-hooks 'gnus-select-article-hook)
10332       (unless (zerop gnus-current-article)
10333         (gnus-summary-goto-subject gnus-current-article))
10334       (gnus-summary-recenter)
10335       (when gnus-use-trees
10336         (gnus-possibly-generate-tree article)
10337         (gnus-highlight-selected-tree article))
10338       ;; Successfully display article.
10339       (gnus-article-set-window-start
10340        (cdr (assq article gnus-newsgroup-bookmarks))))))
10341
10342 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10343   "Select the current article.
10344 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10345 non-nil, the article will be re-fetched even if it already present in
10346 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10347 be displayed."
10348   ;; Make sure we are in the summary buffer to work around bbdb bug.
10349   (unless (eq major-mode 'gnus-summary-mode)
10350     (set-buffer gnus-summary-buffer))
10351   (let ((article (or article (gnus-summary-article-number)))
10352         (all-headers (not (not all-headers))) ;Must be T or NIL.
10353         gnus-summary-display-article-function
10354         did)
10355     (and (not pseudo)
10356          (gnus-summary-article-pseudo-p article)
10357          (error "This is a pseudo-article."))
10358     (prog1
10359         (save-excursion
10360           (set-buffer gnus-summary-buffer)
10361           (if (or (and gnus-single-article-buffer
10362                        (or (null gnus-current-article)
10363                            (null gnus-article-current)
10364                            (null (get-buffer gnus-article-buffer))
10365                            (not (eq article (cdr gnus-article-current)))
10366                            (not (equal (car gnus-article-current)
10367                                        gnus-newsgroup-name))))
10368                   (and (not gnus-single-article-buffer)
10369                        (or (null gnus-current-article)
10370                            (not (eq gnus-current-article article))))
10371                   force)
10372               ;; The requested article is different from the current article.
10373               (prog1
10374                   (gnus-summary-display-article article all-headers)
10375                 (setq did article))
10376             (if (or all-headers gnus-show-all-headers)
10377                 (gnus-article-show-all-headers))
10378             'old))
10379       (if did
10380           (gnus-article-set-window-start
10381            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10382
10383 (defun gnus-summary-set-current-mark (&optional current-mark)
10384   "Obsolete function."
10385   nil)
10386
10387 (defun gnus-summary-next-article (&optional unread subject backward push)
10388   "Select the next article.
10389 If UNREAD, only unread articles are selected.
10390 If SUBJECT, only articles with SUBJECT are selected.
10391 If BACKWARD, the previous article is selected instead of the next."
10392   (interactive "P")
10393   (gnus-set-global-variables)
10394   (cond
10395    ;; Is there such an article?
10396    ((and (gnus-summary-search-forward unread subject backward)
10397          (or (gnus-summary-display-article (gnus-summary-article-number))
10398              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10399     (gnus-summary-position-point))
10400    ;; If not, we try the first unread, if that is wanted.
10401    ((and subject
10402          gnus-auto-select-same
10403          (or (gnus-summary-first-unread-article)
10404              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10405     (gnus-summary-position-point)
10406     (gnus-message 6 "Wrapped"))
10407    ;; Try to get next/previous article not displayed in this group.
10408    ((and gnus-auto-extend-newsgroup
10409          (not unread) (not subject))
10410     (gnus-summary-goto-article
10411      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10412      nil t))
10413    ;; Go to next/previous group.
10414    (t
10415     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10416         (gnus-summary-jump-to-group gnus-newsgroup-name))
10417     (let ((cmd last-command-char)
10418           (group
10419            (if (eq gnus-keep-same-level 'best)
10420                (gnus-summary-best-group gnus-newsgroup-name)
10421              (gnus-summary-search-group backward gnus-keep-same-level))))
10422       ;; For some reason, the group window gets selected.  We change
10423       ;; it back.
10424       (select-window (get-buffer-window (current-buffer)))
10425       ;; Select next unread newsgroup automagically.
10426       (cond
10427        ((or (not gnus-auto-select-next)
10428             (not cmd))
10429         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10430        ((or (eq gnus-auto-select-next 'quietly)
10431             (and (eq gnus-auto-select-next 'slightly-quietly)
10432                  push)
10433             (and (eq gnus-auto-select-next 'almost-quietly)
10434                  (gnus-summary-last-article-p)))
10435         ;; Select quietly.
10436         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10437             (gnus-summary-exit)
10438           (gnus-message 7 "No more%s articles (%s)..."
10439                         (if unread " unread" "")
10440                         (if group (concat "selecting " group)
10441                           "exiting"))
10442           (gnus-summary-next-group nil group backward)))
10443        (t
10444         (gnus-summary-walk-group-buffer
10445          gnus-newsgroup-name cmd unread backward)))))))
10446
10447 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10448   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10449                       (?\C-p (gnus-group-prev-unread-group 1))))
10450         keve key group ended)
10451     (save-excursion
10452       (set-buffer gnus-group-buffer)
10453       (gnus-summary-jump-to-group from-group)
10454       (setq group
10455             (if (eq gnus-keep-same-level 'best)
10456                 (gnus-summary-best-group gnus-newsgroup-name)
10457               (gnus-summary-search-group backward gnus-keep-same-level))))
10458     (while (not ended)
10459       (gnus-message
10460        5 "No more%s articles%s" (if unread " unread" "")
10461        (if (and group
10462                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10463            (format " (Type %s for %s [%s])"
10464                    (single-key-description cmd) group
10465                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10466          (format " (Type %s to exit %s)"
10467                  (single-key-description cmd)
10468                  gnus-newsgroup-name)))
10469       ;; Confirm auto selection.
10470       (setq key (car (setq keve (gnus-read-event-char))))
10471       (setq ended t)
10472       (cond
10473        ((assq key keystrokes)
10474         (let ((obuf (current-buffer)))
10475           (switch-to-buffer gnus-group-buffer)
10476           (and group
10477                (gnus-group-jump-to-group group))
10478           (eval (cadr (assq key keystrokes)))
10479           (setq group (gnus-group-group-name))
10480           (switch-to-buffer obuf))
10481         (setq ended nil))
10482        ((equal key cmd)
10483         (if (or (not group)
10484                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10485             (gnus-summary-exit)
10486           (gnus-summary-next-group nil group backward)))
10487        (t
10488         (push (cdr keve) unread-command-events))))))
10489
10490 (defun gnus-read-event-char ()
10491   "Get the next event."
10492   (let ((event (read-event)))
10493     (cons (and (numberp event) event) event)))
10494
10495 (defun gnus-summary-next-unread-article ()
10496   "Select unread article after current one."
10497   (interactive)
10498   (gnus-summary-next-article t (and gnus-auto-select-same
10499                                     (gnus-summary-article-subject))))
10500
10501 (defun gnus-summary-prev-article (&optional unread subject)
10502   "Select the article after the current one.
10503 If UNREAD is non-nil, only unread articles are selected."
10504   (interactive "P")
10505   (gnus-summary-next-article unread subject t))
10506
10507 (defun gnus-summary-prev-unread-article ()
10508   "Select unred article before current one."
10509   (interactive)
10510   (gnus-summary-prev-article t (and gnus-auto-select-same
10511                                     (gnus-summary-article-subject))))
10512
10513 (defun gnus-summary-next-page (&optional lines circular)
10514   "Show next page of the selected article.
10515 If at the end of the current article, select the next article.
10516 LINES says how many lines should be scrolled up.
10517
10518 If CIRCULAR is non-nil, go to the start of the article instead of
10519 selecting the next article when reaching the end of the current
10520 article."
10521   (interactive "P")
10522   (setq gnus-summary-buffer (current-buffer))
10523   (gnus-set-global-variables)
10524   (let ((article (gnus-summary-article-number))
10525         (endp nil))
10526     (gnus-configure-windows 'article)
10527     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10528         (if (and (eq gnus-summary-goto-unread 'never)
10529                  (not (gnus-summary-last-article-p article)))
10530             (gnus-summary-next-article)
10531           (gnus-summary-next-unread-article))
10532       (if (or (null gnus-current-article)
10533               (null gnus-article-current)
10534               (/= article (cdr gnus-article-current))
10535               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10536           ;; Selected subject is different from current article's.
10537           (gnus-summary-display-article article)
10538         (gnus-eval-in-buffer-window gnus-article-buffer
10539           (setq endp (gnus-article-next-page lines)))
10540         (if endp
10541             (cond (circular
10542                    (gnus-summary-beginning-of-article))
10543                   (lines
10544                    (gnus-message 3 "End of message"))
10545                   ((null lines)
10546                    (if (and (eq gnus-summary-goto-unread 'never)
10547                             (not (gnus-summary-last-article-p article)))
10548                        (gnus-summary-next-article)
10549                      (gnus-summary-next-unread-article)))))))
10550     (gnus-summary-recenter)
10551     (gnus-summary-position-point)))
10552
10553 (defun gnus-summary-prev-page (&optional lines)
10554   "Show previous page of selected article.
10555 Argument LINES specifies lines to be scrolled down."
10556   (interactive "P")
10557   (gnus-set-global-variables)
10558   (let ((article (gnus-summary-article-number)))
10559     (gnus-configure-windows 'article)
10560     (if (or (null gnus-current-article)
10561             (null gnus-article-current)
10562             (/= article (cdr gnus-article-current))
10563             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10564         ;; Selected subject is different from current article's.
10565         (gnus-summary-display-article article)
10566       (gnus-summary-recenter)
10567       (gnus-eval-in-buffer-window gnus-article-buffer
10568         (gnus-article-prev-page lines))))
10569   (gnus-summary-position-point))
10570
10571 (defun gnus-summary-scroll-up (lines)
10572   "Scroll up (or down) one line current article.
10573 Argument LINES specifies lines to be scrolled up (or down if negative)."
10574   (interactive "p")
10575   (gnus-set-global-variables)
10576   (gnus-configure-windows 'article)
10577   (gnus-summary-show-thread)
10578   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10579     (gnus-eval-in-buffer-window gnus-article-buffer
10580       (cond ((> lines 0)
10581              (if (gnus-article-next-page lines)
10582                  (gnus-message 3 "End of message")))
10583             ((< lines 0)
10584              (gnus-article-prev-page (- lines))))))
10585   (gnus-summary-recenter)
10586   (gnus-summary-position-point))
10587
10588 (defun gnus-summary-next-same-subject ()
10589   "Select next article which has the same subject as current one."
10590   (interactive)
10591   (gnus-set-global-variables)
10592   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10593
10594 (defun gnus-summary-prev-same-subject ()
10595   "Select previous article which has the same subject as current one."
10596   (interactive)
10597   (gnus-set-global-variables)
10598   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10599
10600 (defun gnus-summary-next-unread-same-subject ()
10601   "Select next unread article which has the same subject as current one."
10602   (interactive)
10603   (gnus-set-global-variables)
10604   (gnus-summary-next-article t (gnus-summary-article-subject)))
10605
10606 (defun gnus-summary-prev-unread-same-subject ()
10607   "Select previous unread article which has the same subject as current one."
10608   (interactive)
10609   (gnus-set-global-variables)
10610   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10611
10612 (defun gnus-summary-first-unread-article ()
10613   "Select the first unread article.
10614 Return nil if there are no unread articles."
10615   (interactive)
10616   (gnus-set-global-variables)
10617   (prog1
10618       (if (gnus-summary-first-subject t)
10619           (progn
10620             (gnus-summary-show-thread)
10621             (gnus-summary-first-subject t)
10622             (gnus-summary-display-article (gnus-summary-article-number))))
10623     (gnus-summary-position-point)))
10624
10625 (defun gnus-summary-best-unread-article ()
10626   "Select the unread article with the highest score."
10627   (interactive)
10628   (gnus-set-global-variables)
10629   (let ((best -1000000)
10630         (data gnus-newsgroup-data)
10631         article score)
10632     (while data
10633       (and (gnus-data-unread-p (car data))
10634            (> (setq score
10635                     (gnus-summary-article-score (gnus-data-number (car data))))
10636               best)
10637            (setq best score
10638                  article (gnus-data-number (car data))))
10639       (setq data (cdr data)))
10640     (prog1
10641         (if article
10642             (gnus-summary-goto-article article)
10643           (error "No unread articles"))
10644       (gnus-summary-position-point))))
10645
10646 (defun gnus-summary-last-subject ()
10647   "Go to the last displayed subject line in the group."
10648   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10649     (when article
10650       (gnus-summary-goto-subject article))))
10651
10652 (defun gnus-summary-goto-article (article &optional all-headers force)
10653   "Fetch ARTICLE and display it if it exists.
10654 If ALL-HEADERS is non-nil, no header lines are hidden."
10655   (interactive
10656    (list
10657     (string-to-int
10658      (completing-read
10659       "Article number: "
10660       (mapcar (lambda (number) (list (int-to-string number)))
10661               gnus-newsgroup-limit)))
10662     current-prefix-arg
10663     t))
10664   (prog1
10665       (if (gnus-summary-goto-subject article force)
10666           (gnus-summary-display-article article all-headers)
10667         (gnus-message 4 "Couldn't go to article %s" article) nil)
10668     (gnus-summary-position-point)))
10669
10670 (defun gnus-summary-goto-last-article ()
10671   "Go to the previously read article."
10672   (interactive)
10673   (prog1
10674       (and gnus-last-article
10675            (gnus-summary-goto-article gnus-last-article))
10676     (gnus-summary-position-point)))
10677
10678 (defun gnus-summary-pop-article (number)
10679   "Pop one article off the history and go to the previous.
10680 NUMBER articles will be popped off."
10681   (interactive "p")
10682   (let (to)
10683     (setq gnus-newsgroup-history
10684           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10685     (if to
10686         (gnus-summary-goto-article (car to))
10687       (error "Article history empty")))
10688   (gnus-summary-position-point))
10689
10690 ;; Summary commands and functions for limiting the summary buffer.
10691
10692 (defun gnus-summary-limit-to-articles (n)
10693   "Limit the summary buffer to the next N articles.
10694 If not given a prefix, use the process marked articles instead."
10695   (interactive "P")
10696   (gnus-set-global-variables)
10697   (prog1
10698       (let ((articles (gnus-summary-work-articles n)))
10699         (setq gnus-newsgroup-processable nil)
10700         (gnus-summary-limit articles))
10701     (gnus-summary-position-point)))
10702
10703 (defun gnus-summary-pop-limit (&optional total)
10704   "Restore the previous limit.
10705 If given a prefix, remove all limits."
10706   (interactive "P")
10707   (gnus-set-global-variables)
10708   (when total 
10709     (setq gnus-newsgroup-limits
10710           (list (mapcar (lambda (h) (mail-header-number h))
10711                         gnus-newsgroup-headers))))
10712   (unless gnus-newsgroup-limits
10713     (error "No limit to pop"))
10714   (prog1
10715       (gnus-summary-limit nil 'pop)
10716     (gnus-summary-position-point)))
10717
10718 (defun gnus-summary-limit-to-subject (subject &optional header)
10719   "Limit the summary buffer to articles that have subjects that match a regexp."
10720   (interactive "sRegexp: ")
10721   (unless header
10722     (setq header "subject"))
10723   (when (not (equal "" subject))
10724     (prog1
10725         (let ((articles (gnus-summary-find-matching
10726                          (or header "subject") subject 'all)))
10727           (or articles (error "Found no matches for \"%s\"" subject))
10728           (gnus-summary-limit articles))
10729       (gnus-summary-position-point))))
10730
10731 (defun gnus-summary-limit-to-author (from)
10732   "Limit the summary buffer to articles that have authors that match a regexp."
10733   (interactive "sRegexp: ")
10734   (gnus-summary-limit-to-subject from "from"))
10735
10736 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10737 (make-obsolete
10738  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10739
10740 (defun gnus-summary-limit-to-unread (&optional all)
10741   "Limit the summary buffer to articles that are not marked as read.
10742 If ALL is non-nil, limit strictly to unread articles."
10743   (interactive "P")
10744   (if all
10745       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10746     (gnus-summary-limit-to-marks
10747      ;; Concat all the marks that say that an article is read and have
10748      ;; those removed.
10749      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10750            gnus-killed-mark gnus-kill-file-mark
10751            gnus-low-score-mark gnus-expirable-mark
10752            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10753      'reverse)))
10754
10755 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10756 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10757
10758 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10759   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10760 If REVERSE, limit the summary buffer to articles that are not marked
10761 with MARKS.  MARKS can either be a string of marks or a list of marks.
10762 Returns how many articles were removed."
10763   (interactive "sMarks: ")
10764   (gnus-set-global-variables)
10765   (prog1
10766       (let ((data gnus-newsgroup-data)
10767             (marks (if (listp marks) marks
10768                      (append marks nil))) ; Transform to list.
10769             articles)
10770         (while data
10771           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10772                  (memq (gnus-data-mark (car data)) marks))
10773                (setq articles (cons (gnus-data-number (car data)) articles)))
10774           (setq data (cdr data)))
10775         (gnus-summary-limit articles))
10776     (gnus-summary-position-point)))
10777
10778 (defun gnus-summary-limit-to-score (&optional score)
10779   "Limit to articles with score at or above SCORE."
10780   (interactive "P")
10781   (gnus-set-global-variables)
10782   (setq score (if score
10783                   (prefix-numeric-value score)
10784                 (or gnus-summary-default-score 0)))
10785   (let ((data gnus-newsgroup-data)
10786         articles)
10787     (while data
10788       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10789                 score)
10790         (push (gnus-data-number (car data)) articles))
10791       (setq data (cdr data)))
10792     (prog1
10793         (gnus-summary-limit articles)
10794       (gnus-summary-position-point))))
10795
10796 (defun gnus-summary-limit-include-dormant ()
10797   "Display all the hidden articles that are marked as dormant."
10798   (interactive)
10799   (gnus-set-global-variables)
10800   (or gnus-newsgroup-dormant
10801       (error "There are no dormant articles in this group"))
10802   (prog1
10803       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10804     (gnus-summary-position-point)))
10805
10806 (defun gnus-summary-limit-exclude-dormant ()
10807   "Hide all dormant articles."
10808   (interactive)
10809   (gnus-set-global-variables)
10810   (prog1
10811       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10812     (gnus-summary-position-point)))
10813
10814 (defun gnus-summary-limit-exclude-childless-dormant ()
10815   "Hide all dormant articles that have no children."
10816   (interactive)
10817   (gnus-set-global-variables)
10818   (let ((data (gnus-data-list t))
10819         articles d children)
10820     ;; Find all articles that are either not dormant or have
10821     ;; children.
10822     (while (setq d (pop data))
10823       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10824                 (and (setq children 
10825                            (gnus-article-children (gnus-data-number d)))
10826                      (let (found)
10827                        (while children
10828                          (when (memq (car children) articles)
10829                            (setq children nil
10830                                  found t))
10831                          (pop children))
10832                        found)))
10833         (push (gnus-data-number d) articles)))
10834     ;; Do the limiting.
10835     (prog1
10836         (gnus-summary-limit articles)
10837       (gnus-summary-position-point))))
10838
10839 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10840   "Mark all unread excluded articles as read.
10841 If ALL, mark even excluded ticked and dormants as read."
10842   (interactive "P")
10843   (let ((articles (gnus-sorted-complement
10844                    (sort
10845                     (mapcar (lambda (h) (mail-header-number h))
10846                             gnus-newsgroup-headers)
10847                     '<)
10848                    (sort gnus-newsgroup-limit '<)))
10849         article)
10850     (setq gnus-newsgroup-unreads nil)
10851     (if all
10852         (setq gnus-newsgroup-dormant nil
10853               gnus-newsgroup-marked nil
10854               gnus-newsgroup-reads
10855               (nconc
10856                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10857                gnus-newsgroup-reads))
10858       (while (setq article (pop articles))
10859         (unless (or (memq article gnus-newsgroup-dormant)
10860                     (memq article gnus-newsgroup-marked))
10861           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10862
10863 (defun gnus-summary-limit (articles &optional pop)
10864   (if pop
10865       ;; We pop the previous limit off the stack and use that.
10866       (setq articles (car gnus-newsgroup-limits)
10867             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10868     ;; We use the new limit, so we push the old limit on the stack.
10869     (setq gnus-newsgroup-limits
10870           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10871   ;; Set the limit.
10872   (setq gnus-newsgroup-limit articles)
10873   (let ((total (length gnus-newsgroup-data))
10874         (data (gnus-data-find-list (gnus-summary-article-number)))
10875         found)
10876     ;; This will do all the work of generating the new summary buffer
10877     ;; according to the new limit.
10878     (gnus-summary-prepare)
10879     ;; Hide any threads, possibly.
10880     (and gnus-show-threads
10881          gnus-thread-hide-subtree
10882          (gnus-summary-hide-all-threads))
10883     ;; Try to return to the article you were at, or one in the
10884     ;; neighborhood.
10885     (if data
10886         ;; We try to find some article after the current one.
10887         (while data
10888           (and (gnus-summary-goto-subject
10889                 (gnus-data-number (car data)) nil t)
10890                (setq data nil
10891                      found t))
10892           (setq data (cdr data))))
10893     (or found
10894         ;; If there is no data, that means that we were after the last
10895         ;; article.  The same goes when we can't find any articles
10896         ;; after the current one.
10897         (progn
10898           (goto-char (point-max))
10899           (gnus-summary-find-prev)))
10900     ;; We return how many articles were removed from the summary
10901     ;; buffer as a result of the new limit.
10902     (- total (length gnus-newsgroup-data))))
10903
10904 (defsubst gnus-invisible-cut-children (threads)
10905   (let ((num 0))
10906     (while threads
10907       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
10908         (incf num))
10909       (pop threads))
10910     (< num 2)))
10911
10912 (defsubst gnus-cut-thread (thread)
10913   "Go forwards in the thread until we find an article that we want to display."
10914   (when (or (eq gnus-fetch-old-headers 'some)
10915             (eq gnus-build-sparse-threads 'some)
10916             (eq gnus-build-sparse-threads 'more))
10917     ;; Deal with old-fetched headers and sparse threads.
10918     (while (and
10919             thread
10920             (or
10921              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10922              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
10923             (or (<= (length (cdr thread)) 1)
10924                 (gnus-invisible-cut-children (cdr thread))))
10925       (setq thread (cadr thread))))
10926   thread)
10927
10928 (defun gnus-cut-threads (threads)
10929   "Cut off all uninteresting articles from the beginning of threads."
10930   (when (or (eq gnus-fetch-old-headers 'some)
10931             (eq gnus-build-sparse-threads 'some)
10932             (eq gnus-build-sparse-threads 'more))
10933     (let ((th threads))
10934       (while th
10935         (setcar th (gnus-cut-thread (car th)))
10936         (setq th (cdr th)))))
10937   ;; Remove nixed out threads.
10938   (delq nil threads))
10939
10940 (defun gnus-summary-initial-limit (&optional show-if-empty)
10941   "Figure out what the initial limit is supposed to be on group entry.
10942 This entails weeding out unwanted dormants, low-scored articles,
10943 fetch-old-headers verbiage, and so on."
10944   ;; Most groups have nothing to remove.
10945   (if (or gnus-inhibit-limiting
10946           (and (null gnus-newsgroup-dormant)
10947                (not (eq gnus-fetch-old-headers 'some))
10948                (null gnus-summary-expunge-below)
10949                (not (eq gnus-build-sparse-threads 'some))
10950                (not (eq gnus-build-sparse-threads 'more))
10951                (null gnus-thread-expunge-below)
10952                (not gnus-use-nocem)))
10953       () ; Do nothing.
10954     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10955     (setq gnus-newsgroup-limit nil)
10956     (mapatoms
10957      (lambda (node)
10958        (unless (car (symbol-value node))
10959          ;; These threads have no parents -- they are roots.
10960          (let ((nodes (cdr (symbol-value node)))
10961                thread)
10962            (while nodes
10963              (if (and gnus-thread-expunge-below
10964                       (< (gnus-thread-total-score (car nodes))
10965                          gnus-thread-expunge-below))
10966                  (gnus-expunge-thread (pop nodes))
10967                (setq thread (pop nodes))
10968                (gnus-summary-limit-children thread))))))
10969      gnus-newsgroup-dependencies)
10970     ;; If this limitation resulted in an empty group, we might
10971     ;; pop the previous limit and use it instead.
10972     (when (and (not gnus-newsgroup-limit)
10973                show-if-empty)
10974       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10975     gnus-newsgroup-limit))
10976
10977 (defun gnus-summary-limit-children (thread)
10978   "Return 1 if this subthread is visible and 0 if it is not."
10979   ;; First we get the number of visible children to this thread.  This
10980   ;; is done by recursing down the thread using this function, so this
10981   ;; will really go down to a leaf article first, before slowly
10982   ;; working its way up towards the root.
10983   (when thread
10984     (let ((children
10985            (if (cdr thread)
10986                (apply '+ (mapcar 'gnus-summary-limit-children
10987                                  (cdr thread)))
10988              0))
10989           (number (mail-header-number (car thread)))
10990           score)
10991       (if (or
10992            ;; If this article is dormant and has absolutely no visible
10993            ;; children, then this article isn't visible.
10994            (and (memq number gnus-newsgroup-dormant)
10995                 (= children 0))
10996            ;; If this is "fetch-old-headered" and there is only one
10997            ;; visible child (or less), then we don't want this article.
10998            (and (eq gnus-fetch-old-headers 'some)
10999                 (memq number gnus-newsgroup-ancient)
11000                 (zerop children))
11001            ;; If this is a sparsely inserted article with no children,
11002            ;; we don't want it.
11003            (and (eq gnus-build-sparse-threads 'some)
11004                 (memq number gnus-newsgroup-sparse)
11005                 (zerop children))
11006            ;; If we use expunging, and this article is really
11007            ;; low-scored, then we don't want this article.
11008            (when (and gnus-summary-expunge-below
11009                       (< (setq score
11010                                (or (cdr (assq number gnus-newsgroup-scored))
11011                                    gnus-summary-default-score))
11012                          gnus-summary-expunge-below))
11013              ;; We increase the expunge-tally here, but that has
11014              ;; nothing to do with the limits, really.
11015              (incf gnus-newsgroup-expunged-tally)
11016              ;; We also mark as read here, if that's wanted.
11017              (when (and gnus-summary-mark-below
11018                         (< score gnus-summary-mark-below))
11019                (setq gnus-newsgroup-unreads
11020                      (delq number gnus-newsgroup-unreads))
11021                (if gnus-newsgroup-auto-expire
11022                    (push number gnus-newsgroup-expirable)
11023                  (push (cons number gnus-low-score-mark)
11024                        gnus-newsgroup-reads)))
11025              t)
11026            (and gnus-use-nocem
11027                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
11028           ;; Nope, invisible article.
11029           0
11030         ;; Ok, this article is to be visible, so we add it to the limit
11031         ;; and return 1.
11032         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
11033         1))))
11034
11035 (defun gnus-expunge-thread (thread)
11036   "Mark all articles in THREAD as read."
11037   (let* ((number (mail-header-number (car thread))))
11038     (incf gnus-newsgroup-expunged-tally)
11039     ;; We also mark as read here, if that's wanted.
11040     (setq gnus-newsgroup-unreads
11041           (delq number gnus-newsgroup-unreads))
11042     (if gnus-newsgroup-auto-expire
11043         (push number gnus-newsgroup-expirable)
11044       (push (cons number gnus-low-score-mark)
11045             gnus-newsgroup-reads)))
11046   ;; Go recursively through all subthreads.
11047   (mapcar 'gnus-expunge-thread (cdr thread)))
11048
11049 ;; Summary article oriented commands
11050
11051 (defun gnus-summary-refer-parent-article (n)
11052   "Refer parent article N times.
11053 The difference between N and the number of articles fetched is returned."
11054   (interactive "p")
11055   (gnus-set-global-variables)
11056   (while
11057       (and
11058        (> n 0)
11059        (let* ((header (gnus-summary-article-header))
11060               (ref
11061                ;; If we try to find the parent of the currently
11062                ;; displayed article, then we take a look at the actual
11063                ;; References header, since this is slightly more
11064                ;; reliable than the References field we got from the
11065                ;; server.
11066                (if (and (eq (mail-header-number header)
11067                             (cdr gnus-article-current))
11068                         (equal gnus-newsgroup-name
11069                                (car gnus-article-current)))
11070                    (save-excursion
11071                      (set-buffer gnus-original-article-buffer)
11072                      (nnheader-narrow-to-headers)
11073                      (prog1
11074                          (message-fetch-field "references")
11075                        (widen)))
11076                  ;; It's not the current article, so we take a bet on
11077                  ;; the value we got from the server.
11078                  (mail-header-references header))))
11079          (if (setq ref (or ref (mail-header-references header)))
11080              (or (gnus-summary-refer-article (gnus-parent-id ref))
11081                  (gnus-message 1 "Couldn't find parent"))
11082            (gnus-message 1 "No references in article %d"
11083                          (gnus-summary-article-number))
11084            nil)))
11085     (setq n (1- n)))
11086   (gnus-summary-position-point)
11087   n)
11088
11089 (defun gnus-summary-refer-references ()
11090   "Fetch all articles mentioned in the References header.
11091 Return how many articles were fetched."
11092   (interactive)
11093   (gnus-set-global-variables)
11094   (let ((ref (mail-header-references (gnus-summary-article-header)))
11095         (current (gnus-summary-article-number))
11096         (n 0))
11097     ;; For each Message-ID in the References header...
11098     (while (string-match "<[^>]*>" ref)
11099       (incf n)
11100       ;; ... fetch that article.
11101       (gnus-summary-refer-article
11102        (prog1 (match-string 0 ref)
11103          (setq ref (substring ref (match-end 0))))))
11104     (gnus-summary-goto-subject current)
11105     (gnus-summary-position-point)
11106     n))
11107
11108 (defun gnus-summary-refer-article (message-id)
11109   "Fetch an article specified by MESSAGE-ID."
11110   (interactive "sMessage-ID: ")
11111   (when (and (stringp message-id)
11112              (not (zerop (length message-id))))
11113     ;; Construct the correct Message-ID if necessary.
11114     ;; Suggested by tale@pawl.rpi.edu.
11115     (unless (string-match "^<" message-id)
11116       (setq message-id (concat "<" message-id)))
11117     (unless (string-match ">$" message-id)
11118       (setq message-id (concat message-id ">")))
11119     (let* ((header (gnus-id-to-header message-id))
11120            (sparse (and header
11121                         (memq (mail-header-number header)
11122                               gnus-newsgroup-sparse))))
11123       (if header
11124           (prog1
11125               ;; The article is present in the buffer, to we just go to it.
11126               (gnus-summary-goto-article 
11127                (mail-header-number header) nil header)
11128             (when sparse
11129               (gnus-summary-update-article (mail-header-number header))))
11130         ;; We fetch the article
11131         (let ((gnus-override-method 
11132                (and (gnus-news-group-p gnus-newsgroup-name)
11133                     gnus-refer-article-method))
11134               number)
11135           ;; Start the special refer-article method, if necessary.
11136           (when (and gnus-refer-article-method
11137                      (gnus-news-group-p gnus-newsgroup-name))
11138             (gnus-check-server gnus-refer-article-method))
11139           ;; Fetch the header, and display the article.
11140           (if (setq number (gnus-summary-insert-subject message-id))
11141               (gnus-summary-select-article nil nil nil number)
11142             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
11143
11144 (defun gnus-summary-enter-digest-group (&optional force)
11145   "Enter a digest group based on the current article."
11146   (interactive "P")
11147   (gnus-set-global-variables)
11148   (gnus-summary-select-article)
11149   (let ((name (format "%s-%d"
11150                       (gnus-group-prefixed-name
11151                        gnus-newsgroup-name (list 'nndoc ""))
11152                       gnus-current-article))
11153         (ogroup gnus-newsgroup-name)
11154         (case-fold-search t)
11155         (buf (current-buffer))
11156         dig)
11157     (save-excursion
11158       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
11159       (insert-buffer-substring gnus-original-article-buffer)
11160       (narrow-to-region
11161        (goto-char (point-min))
11162        (or (search-forward "\n\n" nil t) (point)))
11163       (goto-char (point-min))
11164       (delete-matching-lines "^\\(Path\\):\\|^From ")
11165       (widen))
11166     (unwind-protect
11167         (if (gnus-group-read-ephemeral-group
11168              name `(nndoc ,name (nndoc-address
11169                                  ,(get-buffer dig))
11170                           (nndoc-article-type ,(if force 'digest 'guess))) t)
11171             ;; Make all postings to this group go to the parent group.
11172             (nconc (gnus-info-params (gnus-get-info name))
11173                    (list (cons 'to-group ogroup)))
11174           ;; Couldn't select this doc group.
11175           (switch-to-buffer buf)
11176           (gnus-set-global-variables)
11177           (gnus-configure-windows 'summary)
11178           (gnus-message 3 "Article couldn't be entered?"))
11179       (kill-buffer dig))))
11180
11181 (defun gnus-summary-isearch-article (&optional regexp-p)
11182   "Do incremental search forward on the current article.
11183 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
11184   (interactive "P")
11185   (gnus-set-global-variables)
11186   (gnus-summary-select-article)
11187   (gnus-configure-windows 'article)
11188   (gnus-eval-in-buffer-window gnus-article-buffer
11189     (goto-char (point-min))
11190     (isearch-forward regexp-p)))
11191
11192 (defun gnus-summary-search-article-forward (regexp &optional backward)
11193   "Search for an article containing REGEXP forward.
11194 If BACKWARD, search backward instead."
11195   (interactive
11196    (list (read-string
11197           (format "Search article %s (regexp%s): "
11198                   (if current-prefix-arg "backward" "forward")
11199                   (if gnus-last-search-regexp
11200                       (concat ", default " gnus-last-search-regexp)
11201                     "")))
11202          current-prefix-arg))
11203   (gnus-set-global-variables)
11204   (if (string-equal regexp "")
11205       (setq regexp (or gnus-last-search-regexp ""))
11206     (setq gnus-last-search-regexp regexp))
11207   (unless (gnus-summary-search-article regexp backward)
11208     (error "Search failed: \"%s\"" regexp)))
11209
11210 (defun gnus-summary-search-article-backward (regexp)
11211   "Search for an article containing REGEXP backward."
11212   (interactive
11213    (list (read-string
11214           (format "Search article backward (regexp%s): "
11215                   (if gnus-last-search-regexp
11216                       (concat ", default " gnus-last-search-regexp)
11217                     "")))))
11218   (gnus-summary-search-article-forward regexp 'backward))
11219
11220 (defun gnus-summary-search-article (regexp &optional backward)
11221   "Search for an article containing REGEXP.
11222 Optional argument BACKWARD means do search for backward.
11223 `gnus-select-article-hook' is not called during the search."
11224   (let ((gnus-select-article-hook nil)  ;Disable hook.
11225         (gnus-article-display-hook nil)
11226         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11227         (re-search
11228          (if backward
11229              're-search-backward 're-search-forward))
11230         (sum (current-buffer))
11231         (found nil))
11232     (gnus-save-hidden-threads
11233       (gnus-summary-select-article)
11234       (set-buffer gnus-article-buffer)
11235       (when backward
11236         (forward-line -1))
11237       (while (not found)
11238         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
11239         (if (if backward
11240                 (re-search-backward regexp nil t)
11241               (re-search-forward regexp nil t))
11242             ;; We found the regexp.
11243             (progn
11244               (setq found 'found)
11245               (beginning-of-line)
11246               (set-window-start
11247                (get-buffer-window (current-buffer))
11248                (point))
11249               (forward-line 1)
11250               (set-buffer sum))
11251           ;; We didn't find it, so we go to the next article.
11252           (set-buffer sum)
11253           (if (not (if backward (gnus-summary-find-prev)
11254                      (gnus-summary-find-next)))
11255               ;; No more articles.
11256               (setq found t)
11257             ;; Select the next article and adjust point.
11258             (gnus-summary-select-article)
11259             (set-buffer gnus-article-buffer)
11260             (widen)
11261             (goto-char (if backward (point-max) (point-min))))))
11262       (gnus-message 7 ""))
11263     ;; Return whether we found the regexp.
11264     (when (eq found 'found)
11265       (gnus-summary-show-thread)
11266       (gnus-summary-goto-subject gnus-current-article)
11267       (gnus-summary-position-point)
11268       t)))
11269
11270 (defun gnus-summary-find-matching (header regexp &optional backward unread
11271                                           not-case-fold)
11272   "Return a list of all articles that match REGEXP on HEADER.
11273 The search stars on the current article and goes forwards unless
11274 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11275 If UNREAD is non-nil, only unread articles will
11276 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11277 in the comparisons."
11278   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11279                 (gnus-data-find-list
11280                  (gnus-summary-article-number) (gnus-data-list backward))))
11281         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11282         (case-fold-search (not not-case-fold))
11283         articles d)
11284     (or (fboundp (intern (concat "mail-header-" header)))
11285         (error "%s is not a valid header" header))
11286     (while data
11287       (setq d (car data))
11288       (and (or (not unread)             ; We want all articles...
11289                (gnus-data-unread-p d))  ; Or just unreads.
11290            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11291            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11292            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11293       (setq data (cdr data)))
11294     (nreverse articles)))
11295
11296 (defun gnus-summary-execute-command (header regexp command &optional backward)
11297   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11298 If HEADER is an empty string (or nil), the match is done on the entire
11299 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11300   (interactive
11301    (list (let ((completion-ignore-case t))
11302            (completing-read
11303             "Header name: "
11304             (mapcar (lambda (string) (list string))
11305                     '("Number" "Subject" "From" "Lines" "Date"
11306                       "Message-ID" "Xref" "References" "Body"))
11307             nil 'require-match))
11308          (read-string "Regexp: ")
11309          (read-key-sequence "Command: ")
11310          current-prefix-arg))
11311   (when (equal header "Body")
11312     (setq header ""))
11313   (gnus-set-global-variables)
11314   ;; Hidden thread subtrees must be searched as well.
11315   (gnus-summary-show-all-threads)
11316   ;; We don't want to change current point nor window configuration.
11317   (save-excursion
11318     (save-window-excursion
11319       (gnus-message 6 "Executing %s..." (key-description command))
11320       ;; We'd like to execute COMMAND interactively so as to give arguments.
11321       (gnus-execute header regexp
11322                     `(lambda () (call-interactively ',(key-binding command)))
11323                     backward)
11324       (gnus-message 6 "Executing %s...done" (key-description command)))))
11325
11326 (defun gnus-summary-beginning-of-article ()
11327   "Scroll the article back to the beginning."
11328   (interactive)
11329   (gnus-set-global-variables)
11330   (gnus-summary-select-article)
11331   (gnus-configure-windows 'article)
11332   (gnus-eval-in-buffer-window gnus-article-buffer
11333     (widen)
11334     (goto-char (point-min))
11335     (and gnus-break-pages (gnus-narrow-to-page))))
11336
11337 (defun gnus-summary-end-of-article ()
11338   "Scroll to the end of the article."
11339   (interactive)
11340   (gnus-set-global-variables)
11341   (gnus-summary-select-article)
11342   (gnus-configure-windows 'article)
11343   (gnus-eval-in-buffer-window gnus-article-buffer
11344     (widen)
11345     (goto-char (point-max))
11346     (recenter -3)
11347     (and gnus-break-pages (gnus-narrow-to-page))))
11348
11349 (defun gnus-summary-show-article (&optional arg)
11350   "Force re-fetching of the current article.
11351 If ARG (the prefix) is non-nil, show the raw article without any
11352 article massaging functions being run."
11353   (interactive "P")
11354   (gnus-set-global-variables)
11355   (if (not arg)
11356       ;; Select the article the normal way.
11357       (gnus-summary-select-article nil 'force)
11358     ;; Bind the article treatment functions to nil.
11359     (let ((gnus-have-all-headers t)
11360           gnus-article-display-hook
11361           gnus-article-prepare-hook
11362           gnus-break-pages
11363           gnus-visual)
11364       (gnus-summary-select-article nil 'force)))
11365   (gnus-summary-goto-subject gnus-current-article)
11366 ;  (gnus-configure-windows 'article)
11367   (gnus-summary-position-point))
11368
11369 (defun gnus-summary-verbose-headers (&optional arg)
11370   "Toggle permanent full header display.
11371 If ARG is a positive number, turn header display on.
11372 If ARG is a negative number, turn header display off."
11373   (interactive "P")
11374   (gnus-set-global-variables)
11375   (gnus-summary-toggle-header arg)
11376   (setq gnus-show-all-headers
11377         (cond ((or (not (numberp arg))
11378                    (zerop arg))
11379                (not gnus-show-all-headers))
11380               ((natnump arg)
11381                t))))
11382
11383 (defun gnus-summary-toggle-header (&optional arg)
11384   "Show the headers if they are hidden, or hide them if they are shown.
11385 If ARG is a positive number, show the entire header.
11386 If ARG is a negative number, hide the unwanted header lines."
11387   (interactive "P")
11388   (gnus-set-global-variables)
11389   (save-excursion
11390     (set-buffer gnus-article-buffer)
11391     (let* ((buffer-read-only nil)
11392            (inhibit-point-motion-hooks t)
11393            (hidden (text-property-any
11394                     (goto-char (point-min)) (search-forward "\n\n")
11395                     'invisible t))
11396            e)
11397       (goto-char (point-min))
11398       (when (search-forward "\n\n" nil t)
11399         (delete-region (point-min) (1- (point))))
11400       (goto-char (point-min))
11401       (save-excursion
11402         (set-buffer gnus-original-article-buffer)
11403         (goto-char (point-min))
11404         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11405       (insert-buffer-substring gnus-original-article-buffer 1 e)
11406       (let ((gnus-inhibit-hiding t))
11407         (run-hooks 'gnus-article-display-hook))
11408       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11409           (gnus-article-hide-headers)))))
11410
11411 (defun gnus-summary-show-all-headers ()
11412   "Make all header lines visible."
11413   (interactive)
11414   (gnus-set-global-variables)
11415   (gnus-article-show-all-headers))
11416
11417 (defun gnus-summary-toggle-mime (&optional arg)
11418   "Toggle MIME processing.
11419 If ARG is a positive number, turn MIME processing on."
11420   (interactive "P")
11421   (gnus-set-global-variables)
11422   (setq gnus-show-mime
11423         (if (null arg) (not gnus-show-mime)
11424           (> (prefix-numeric-value arg) 0)))
11425   (gnus-summary-select-article t 'force))
11426
11427 (defun gnus-summary-caesar-message (&optional arg)
11428   "Caesar rotate the current article by 13.
11429 The numerical prefix specifies how manu places to rotate each letter
11430 forward."
11431   (interactive "P")
11432   (gnus-set-global-variables)
11433   (gnus-summary-select-article)
11434   (let ((mail-header-separator ""))
11435     (gnus-eval-in-buffer-window gnus-article-buffer
11436       (save-restriction
11437         (widen)
11438         (let ((start (window-start))
11439               buffer-read-only)
11440           (message-caesar-buffer-body arg)
11441           (set-window-start (get-buffer-window (current-buffer)) start))))))
11442
11443 (defun gnus-summary-stop-page-breaking ()
11444   "Stop page breaking in the current article."
11445   (interactive)
11446   (gnus-set-global-variables)
11447   (gnus-summary-select-article)
11448   (gnus-eval-in-buffer-window gnus-article-buffer
11449     (widen)))
11450
11451 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11452   "Move the current article to a different newsgroup.
11453 If N is a positive number, move the N next articles.
11454 If N is a negative number, move the N previous articles.
11455 If N is nil and any articles have been marked with the process mark,
11456 move those articles instead.
11457 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11458 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11459 re-spool using this method.
11460
11461 For this function to work, both the current newsgroup and the
11462 newsgroup that you want to move to have to support the `request-move'
11463 and `request-accept' functions."
11464   (interactive "P")
11465   (unless action (setq action 'move))
11466   (gnus-set-global-variables)
11467   ;; Check whether the source group supports the required functions.
11468   (cond ((and (eq action 'move)
11469               (not (gnus-check-backend-function
11470                     'request-move-article gnus-newsgroup-name)))
11471          (error "The current group does not support article moving"))
11472         ((and (eq action 'crosspost)
11473               (not (gnus-check-backend-function
11474                     'request-replace-article gnus-newsgroup-name)))
11475          (error "The current group does not support article editing")))
11476   (let ((articles (gnus-summary-work-articles n))
11477         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11478         (names '((move "Move" "Moving")
11479                  (copy "Copy" "Copying")
11480                  (crosspost "Crosspost" "Crossposting")))
11481         (copy-buf (save-excursion
11482                     (nnheader-set-temp-buffer " *copy article*")))
11483         art-group to-method new-xref article to-groups)
11484     (unless (assq action names)
11485       (error "Unknown action %s" action))
11486     ;; Read the newsgroup name.
11487     (when (and (not to-newsgroup)
11488                (not select-method))
11489       (setq to-newsgroup
11490             (gnus-read-move-group-name
11491              (cadr (assq action names))
11492              (symbol-value (intern (format "gnus-current-%s-group" action)))
11493              articles prefix))
11494       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11495     (setq to-method (or select-method 
11496                         (gnus-group-name-to-method to-newsgroup)))
11497     ;; Check the method we are to move this article to...
11498     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11499         (error "%s does not support article copying" (car to-method)))
11500     (or (gnus-check-server to-method)
11501         (error "Can't open server %s" (car to-method)))
11502     (gnus-message 6 "%s to %s: %s..."
11503                   (caddr (assq action names))
11504                   (or (car select-method) to-newsgroup) articles)
11505     (while articles
11506       (setq article (pop articles))
11507       (setq
11508        art-group
11509        (cond
11510         ;; Move the article.
11511         ((eq action 'move)
11512          (gnus-request-move-article
11513           article                       ; Article to move
11514           gnus-newsgroup-name           ; From newsgrouo
11515           (nth 1 (gnus-find-method-for-group
11516                   gnus-newsgroup-name)) ; Server
11517           (list 'gnus-request-accept-article
11518                 to-newsgroup (list 'quote select-method)
11519                 (not articles))         ; Accept form
11520           (not articles)))              ; Only save nov last time
11521         ;; Copy the article.
11522         ((eq action 'copy)
11523          (save-excursion
11524            (set-buffer copy-buf)
11525            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11526            (gnus-request-accept-article
11527             to-newsgroup select-method (not articles))))
11528         ;; Crosspost the article.
11529         ((eq action 'crosspost)
11530          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11531            (setq new-xref (concat gnus-newsgroup-name ":" article))
11532            (if (and xref (not (string= xref "")))
11533                (progn
11534                  (when (string-match "^Xref: " xref)
11535                    (setq xref (substring xref (match-end 0))))
11536                  (setq new-xref (concat xref " " new-xref)))
11537              (setq new-xref (concat (system-name) " " new-xref)))
11538            (save-excursion
11539              (set-buffer copy-buf)
11540              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11541              (nnheader-replace-header "xref" new-xref)
11542              (gnus-request-accept-article
11543               to-newsgroup select-method (not articles)))))))
11544       (if (not art-group)
11545           (gnus-message 1 "Couldn't %s article %s"
11546                         (cadr (assq action names)) article)
11547         (let* ((entry
11548                 (or
11549                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11550                  (gnus-gethash
11551                   (gnus-group-prefixed-name
11552                    (car art-group)
11553                    (or select-method 
11554                        (gnus-find-method-for-group to-newsgroup)))
11555                   gnus-newsrc-hashtb)))
11556                (info (nth 2 entry))
11557                (to-group (gnus-info-group info)))
11558           ;; Update the group that has been moved to.
11559           (when (and info
11560                      (memq action '(move copy)))
11561             (unless (member to-group to-groups)
11562               (push to-group to-groups))
11563
11564             (unless (memq article gnus-newsgroup-unreads)
11565               (gnus-info-set-read
11566                info (gnus-add-to-range (gnus-info-read info)
11567                                        (list (cdr art-group)))))
11568
11569             ;; Copy any marks over to the new group.
11570             (let ((marks gnus-article-mark-lists)
11571                   (to-article (cdr art-group)))
11572
11573               ;; See whether the article is to be put in the cache.
11574               (when gnus-use-cache
11575                 (gnus-cache-possibly-enter-article
11576                  to-group to-article
11577                  (let ((header (copy-sequence
11578                                 (gnus-summary-article-header article))))
11579                    (mail-header-set-number header to-article)
11580                    header)
11581                  (memq article gnus-newsgroup-marked)
11582                  (memq article gnus-newsgroup-dormant)
11583                  (memq article gnus-newsgroup-unreads)))
11584
11585               (while marks
11586                 (when (memq article (symbol-value
11587                                      (intern (format "gnus-newsgroup-%s"
11588                                                      (caar marks)))))
11589                   ;; If the other group is the same as this group,
11590                   ;; then we have to add the mark to the list.
11591                   (when (equal to-group gnus-newsgroup-name)
11592                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11593                          (cons to-article
11594                                (symbol-value
11595                                 (intern (format "gnus-newsgroup-%s"
11596                                                 (caar marks)))))))
11597                   ;; Copy mark to other group.
11598                   (gnus-add-marked-articles
11599                    to-group (cdar marks) (list to-article) info))
11600                 (setq marks (cdr marks)))))
11601
11602           ;; Update the Xref header in this article to point to
11603           ;; the new crossposted article we have just created.
11604           (when (eq action 'crosspost)
11605             (save-excursion
11606               (set-buffer copy-buf)
11607               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11608               (nnheader-replace-header
11609                "xref" (concat new-xref " " (gnus-group-prefixed-name
11610                                             (car art-group) to-method)
11611                               ":" (cdr art-group)))
11612               (gnus-request-replace-article
11613                article gnus-newsgroup-name (current-buffer)))))
11614
11615         (gnus-summary-goto-subject article)
11616         (when (eq action 'move)
11617           (gnus-summary-mark-article article gnus-canceled-mark)))
11618       (gnus-summary-remove-process-mark article))
11619     ;; Re-activate all groups that have been moved to.
11620     (while to-groups
11621       (gnus-activate-group (pop to-groups)))
11622     
11623     (gnus-kill-buffer copy-buf)
11624     (gnus-summary-position-point)
11625     (gnus-set-mode-line 'summary)))
11626
11627 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11628   "Move the current article to a different newsgroup.
11629 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11630 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11631 re-spool using this method."
11632   (interactive "P")
11633   (gnus-summary-move-article n nil select-method 'copy))
11634
11635 (defun gnus-summary-crosspost-article (&optional n)
11636   "Crosspost the current article to some other group."
11637   (interactive "P")
11638   (gnus-summary-move-article n nil nil 'crosspost))
11639
11640 (defvar gnus-summary-respool-default-method nil
11641   "Default method for respooling an article.  
11642 If nil, use to the current newsgroup method.")
11643
11644 (defun gnus-summary-respool-article (&optional n method)
11645   "Respool the current article.
11646 The article will be squeezed through the mail spooling process again,
11647 which means that it will be put in some mail newsgroup or other
11648 depending on `nnmail-split-methods'.
11649 If N is a positive number, respool the N next articles.
11650 If N is a negative number, respool the N previous articles.
11651 If N is nil and any articles have been marked with the process mark,
11652 respool those articles instead.
11653
11654 Respooling can be done both from mail groups and \"real\" newsgroups.
11655 In the former case, the articles in question will be moved from the
11656 current group into whatever groups they are destined to.  In the
11657 latter case, they will be copied into the relevant groups."
11658   (interactive 
11659    (list current-prefix-arg
11660          (let* ((methods (gnus-methods-using 'respool))
11661                 (methname
11662                  (symbol-name (or gnus-summary-respool-default-method
11663                                   (car (gnus-find-method-for-group
11664                                         gnus-newsgroup-name)))))
11665                 (method
11666                  (gnus-completing-read 
11667                   methname "What backend do you want to use when respooling?"
11668                   methods nil t nil 'gnus-method-history))
11669                 ms)
11670            (cond
11671             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11672              (list (intern method) ""))
11673             ((= 1 (length ms))
11674              (car ms))
11675             (t
11676              (cdr (completing-read 
11677                    "Server name: "
11678                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11679   (gnus-set-global-variables)
11680   (unless method
11681     (error "No method given for respooling"))
11682   (if (assoc (symbol-name
11683               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11684              (gnus-methods-using 'respool))
11685       (gnus-summary-move-article n nil method)
11686     (gnus-summary-copy-article n nil method)))
11687
11688 (defun gnus-summary-import-article (file)
11689   "Import a random file into a mail newsgroup."
11690   (interactive "fImport file: ")
11691   (gnus-set-global-variables)
11692   (let ((group gnus-newsgroup-name)
11693         (now (current-time))
11694         atts lines)
11695     (or (gnus-check-backend-function 'request-accept-article group)
11696         (error "%s does not support article importing" group))
11697     (or (file-readable-p file)
11698         (not (file-regular-p file))
11699         (error "Can't read %s" file))
11700     (save-excursion
11701       (set-buffer (get-buffer-create " *import file*"))
11702       (buffer-disable-undo (current-buffer))
11703       (erase-buffer)
11704       (insert-file-contents file)
11705       (goto-char (point-min))
11706       (unless (nnheader-article-p)
11707         ;; This doesn't look like an article, so we fudge some headers.
11708         (setq atts (file-attributes file)
11709               lines (count-lines (point-min) (point-max)))
11710         (insert "From: " (read-string "From: ") "\n"
11711                 "Subject: " (read-string "Subject: ") "\n"
11712                 "Date: " (timezone-make-date-arpa-standard
11713                           (current-time-string (nth 5 atts))
11714                           (current-time-zone now)
11715                           (current-time-zone now)) "\n"
11716                 "Message-ID: " (message-make-message-id) "\n"
11717                 "Lines: " (int-to-string lines) "\n"
11718                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11719       (gnus-request-accept-article group nil t)
11720       (kill-buffer (current-buffer)))))
11721
11722 (defun gnus-summary-expire-articles (&optional now)
11723   "Expire all articles that are marked as expirable in the current group."
11724   (interactive)
11725   (gnus-set-global-variables)
11726   (when (gnus-check-backend-function
11727          'request-expire-articles gnus-newsgroup-name)
11728     ;; This backend supports expiry.
11729     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11730            (expirable (if total
11731                           (gnus-list-of-read-articles gnus-newsgroup-name)
11732                         (setq gnus-newsgroup-expirable
11733                               (sort gnus-newsgroup-expirable '<))))
11734            (expiry-wait (if now 'immediate
11735                           (gnus-group-get-parameter
11736                            gnus-newsgroup-name 'expiry-wait)))
11737            es)
11738       (when expirable
11739         ;; There are expirable articles in this group, so we run them
11740         ;; through the expiry process.
11741         (gnus-message 6 "Expiring articles...")
11742         ;; The list of articles that weren't expired is returned.
11743         (if expiry-wait
11744             (let ((nnmail-expiry-wait-function nil)
11745                   (nnmail-expiry-wait expiry-wait))
11746               (setq es (gnus-request-expire-articles
11747                         expirable gnus-newsgroup-name)))
11748           (setq es (gnus-request-expire-articles
11749                     expirable gnus-newsgroup-name)))
11750         (or total (setq gnus-newsgroup-expirable es))
11751         ;; We go through the old list of expirable, and mark all
11752         ;; really expired articles as nonexistent.
11753         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11754           (let ((gnus-use-cache nil))
11755             (while expirable
11756               (unless (memq (car expirable) es)
11757                 (when (gnus-data-find (car expirable))
11758                   (gnus-summary-mark-article
11759                    (car expirable) gnus-canceled-mark)))
11760               (setq expirable (cdr expirable)))))
11761         (gnus-message 6 "Expiring articles...done")))))
11762
11763 (defun gnus-summary-expire-articles-now ()
11764   "Expunge all expirable articles in the current group.
11765 This means that *all* articles that are marked as expirable will be
11766 deleted forever, right now."
11767   (interactive)
11768   (gnus-set-global-variables)
11769   (or gnus-expert-user
11770       (gnus-y-or-n-p
11771        "Are you really, really, really sure you want to delete all these messages? ")
11772       (error "Phew!"))
11773   (gnus-summary-expire-articles t))
11774
11775 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11776 (defun gnus-summary-delete-article (&optional n)
11777   "Delete the N next (mail) articles.
11778 This command actually deletes articles.  This is not a marking
11779 command.  The article will disappear forever from your life, never to
11780 return.
11781 If N is negative, delete backwards.
11782 If N is nil and articles have been marked with the process mark,
11783 delete these instead."
11784   (interactive "P")
11785   (gnus-set-global-variables)
11786   (or (gnus-check-backend-function 'request-expire-articles
11787                                    gnus-newsgroup-name)
11788       (error "The current newsgroup does not support article deletion."))
11789   ;; Compute the list of articles to delete.
11790   (let ((articles (gnus-summary-work-articles n))
11791         not-deleted)
11792     (if (and gnus-novice-user
11793              (not (gnus-y-or-n-p
11794                    (format "Do you really want to delete %s forever? "
11795                            (if (> (length articles) 1) 
11796                                (format "these %s articles" (length articles))
11797                              "this article")))))
11798         ()
11799       ;; Delete the articles.
11800       (setq not-deleted (gnus-request-expire-articles
11801                          articles gnus-newsgroup-name 'force))
11802       (while articles
11803         (gnus-summary-remove-process-mark (car articles))
11804         ;; The backend might not have been able to delete the article
11805         ;; after all.
11806         (or (memq (car articles) not-deleted)
11807             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11808         (setq articles (cdr articles))))
11809     (gnus-summary-position-point)
11810     (gnus-set-mode-line 'summary)
11811     not-deleted))
11812
11813 (defun gnus-summary-edit-article (&optional force)
11814   "Enter into a buffer and edit the current article.
11815 This will have permanent effect only in mail groups.
11816 If FORCE is non-nil, allow editing of articles even in read-only
11817 groups."
11818   (interactive "P")
11819   (save-excursion
11820     (set-buffer gnus-summary-buffer)
11821     (gnus-set-global-variables)
11822     (when (and (not force)
11823                (gnus-group-read-only-p))
11824       (error "The current newsgroup does not support article editing."))
11825     (gnus-summary-select-article t nil t)
11826     (gnus-configure-windows 'article)
11827     (select-window (get-buffer-window gnus-article-buffer))
11828     (gnus-message 6 "C-c C-c to end edits")
11829     (setq buffer-read-only nil)
11830     (text-mode)
11831     (use-local-map (copy-keymap (current-local-map)))
11832     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11833     (buffer-enable-undo)
11834     (widen)
11835     (goto-char (point-min))
11836     (search-forward "\n\n" nil t)))
11837
11838 (defun gnus-summary-edit-article-done ()
11839   "Make edits to the current article permanent."
11840   (interactive)
11841   (if (gnus-group-read-only-p)
11842       (progn
11843         (gnus-summary-edit-article-postpone)
11844         (gnus-error
11845          1 "The current newsgroup does not support article editing."))
11846     (let ((buf (format "%s" (buffer-string))))
11847       (erase-buffer)
11848       (insert buf)
11849       (if (not (gnus-request-replace-article
11850                 (cdr gnus-article-current) (car gnus-article-current)
11851                 (current-buffer)))
11852           (error "Couldn't replace article.")
11853         (gnus-article-mode)
11854         (use-local-map gnus-article-mode-map)
11855         (setq buffer-read-only t)
11856         (buffer-disable-undo (current-buffer))
11857         (gnus-configure-windows 'summary)
11858         (gnus-summary-update-article (cdr gnus-article-current))
11859         (when gnus-use-cache
11860           (gnus-cache-update-article    
11861            (car gnus-article-current) (cdr gnus-article-current)))
11862         (when gnus-keep-backlog
11863           (gnus-backlog-remove-article 
11864            (car gnus-article-current) (cdr gnus-article-current))))
11865       (save-excursion
11866         (when (get-buffer gnus-original-article-buffer)
11867           (set-buffer gnus-original-article-buffer)
11868           (setq gnus-original-article nil)))
11869       (setq gnus-article-current nil
11870             gnus-current-article nil)
11871       (run-hooks 'gnus-article-display-hook)
11872       (and (gnus-visual-p 'summary-highlight 'highlight)
11873            (run-hooks 'gnus-visual-mark-article-hook)))))
11874
11875 (defun gnus-summary-edit-article-postpone ()
11876   "Postpone changes to the current article."
11877   (interactive)
11878   (gnus-article-mode)
11879   (use-local-map gnus-article-mode-map)
11880   (setq buffer-read-only t)
11881   (buffer-disable-undo (current-buffer))
11882   (gnus-configure-windows 'summary)
11883   (and (gnus-visual-p 'summary-highlight 'highlight)
11884        (run-hooks 'gnus-visual-mark-article-hook)))
11885
11886 (defun gnus-summary-respool-query ()
11887   "Query where the respool algorithm would put this article."
11888   (interactive)
11889   (gnus-set-global-variables)
11890   (gnus-summary-select-article)
11891   (save-excursion
11892     (set-buffer gnus-article-buffer)
11893     (save-restriction
11894       (goto-char (point-min))
11895       (search-forward "\n\n")
11896       (narrow-to-region (point-min) (point))
11897       (pp-eval-expression
11898        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11899
11900 ;; Summary marking commands.
11901
11902 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11903   "Mark articles which has the same subject as read, and then select the next.
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     ;; Select next unread article.  If auto-select-same mode, should
11914     ;; select the first unread article.
11915     (gnus-summary-next-article t (and gnus-auto-select-same
11916                                       (gnus-summary-article-subject)))
11917     (gnus-message 7 "%d article%s marked as %s"
11918                   count (if (= count 1) " is" "s are")
11919                   (if unmark "unread" "read"))))
11920
11921 (defun gnus-summary-kill-same-subject (&optional unmark)
11922   "Mark articles which has the same subject as read.
11923 If UNMARK is positive, remove any kind of mark.
11924 If UNMARK is negative, tick articles."
11925   (interactive "P")
11926   (gnus-set-global-variables)
11927   (if unmark
11928       (setq unmark (prefix-numeric-value unmark)))
11929   (let ((count
11930          (gnus-summary-mark-same-subject
11931           (gnus-summary-article-subject) unmark)))
11932     ;; If marked as read, go to next unread subject.
11933     (if (null unmark)
11934         ;; Go to next unread subject.
11935         (gnus-summary-next-subject 1 t))
11936     (gnus-message 7 "%d articles are marked as %s"
11937                   count (if unmark "unread" "read"))))
11938
11939 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11940   "Mark articles with same SUBJECT as read, and return marked number.
11941 If optional argument UNMARK is positive, remove any kinds of marks.
11942 If optional argument UNMARK is negative, mark articles as unread instead."
11943   (let ((count 1))
11944     (save-excursion
11945       (cond
11946        ((null unmark)                   ; Mark as read.
11947         (while (and
11948                 (progn
11949                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11950                   (gnus-summary-show-thread) t)
11951                 (gnus-summary-find-subject subject))
11952           (setq count (1+ count))))
11953        ((> unmark 0)                    ; Tick.
11954         (while (and
11955                 (progn
11956                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11957                   (gnus-summary-show-thread) t)
11958                 (gnus-summary-find-subject subject))
11959           (setq count (1+ count))))
11960        (t                               ; Mark as unread.
11961         (while (and
11962                 (progn
11963                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11964                   (gnus-summary-show-thread) t)
11965                 (gnus-summary-find-subject subject))
11966           (setq count (1+ count)))))
11967       (gnus-set-mode-line 'summary)
11968       ;; Return the number of marked articles.
11969       count)))
11970
11971 (defun gnus-summary-mark-as-processable (n &optional unmark)
11972   "Set the process mark on the next N articles.
11973 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11974 the process mark instead.  The difference between N and the actual
11975 number of articles marked is returned."
11976   (interactive "p")
11977   (gnus-set-global-variables)
11978   (let ((backward (< n 0))
11979         (n (abs n)))
11980     (while (and
11981             (> n 0)
11982             (if unmark
11983                 (gnus-summary-remove-process-mark
11984                  (gnus-summary-article-number))
11985               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11986             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11987       (setq n (1- n)))
11988     (if (/= 0 n) (gnus-message 7 "No more articles"))
11989     (gnus-summary-recenter)
11990     (gnus-summary-position-point)
11991     n))
11992
11993 (defun gnus-summary-unmark-as-processable (n)
11994   "Remove the process mark from the next N articles.
11995 If N is negative, mark backward instead.  The difference between N and
11996 the actual number of articles marked is returned."
11997   (interactive "p")
11998   (gnus-set-global-variables)
11999   (gnus-summary-mark-as-processable n t))
12000
12001 (defun gnus-summary-unmark-all-processable ()
12002   "Remove the process mark from all articles."
12003   (interactive)
12004   (gnus-set-global-variables)
12005   (save-excursion
12006     (while gnus-newsgroup-processable
12007       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
12008   (gnus-summary-position-point))
12009
12010 (defun gnus-summary-mark-as-expirable (n)
12011   "Mark N articles forward as expirable.
12012 If N is negative, mark backward instead.  The difference between N and
12013 the actual number of articles marked is returned."
12014   (interactive "p")
12015   (gnus-set-global-variables)
12016   (gnus-summary-mark-forward n gnus-expirable-mark))
12017
12018 (defun gnus-summary-mark-article-as-replied (article)
12019   "Mark ARTICLE replied and update the summary line."
12020   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
12021   (let ((buffer-read-only nil))
12022     (when (gnus-summary-goto-subject article)
12023       (gnus-summary-update-secondary-mark article))))
12024
12025 (defun gnus-summary-set-bookmark (article)
12026   "Set a bookmark in current article."
12027   (interactive (list (gnus-summary-article-number)))
12028   (gnus-set-global-variables)
12029   (if (or (not (get-buffer gnus-article-buffer))
12030           (not gnus-current-article)
12031           (not gnus-article-current)
12032           (not (equal gnus-newsgroup-name (car gnus-article-current))))
12033       (error "No current article selected"))
12034   ;; Remove old bookmark, if one exists.
12035   (let ((old (assq article gnus-newsgroup-bookmarks)))
12036     (if old (setq gnus-newsgroup-bookmarks
12037                   (delq old gnus-newsgroup-bookmarks))))
12038   ;; Set the new bookmark, which is on the form
12039   ;; (article-number . line-number-in-body).
12040   (setq gnus-newsgroup-bookmarks
12041         (cons
12042          (cons article
12043                (save-excursion
12044                  (set-buffer gnus-article-buffer)
12045                  (count-lines
12046                   (min (point)
12047                        (save-excursion
12048                          (goto-char (point-min))
12049                          (search-forward "\n\n" nil t)
12050                          (point)))
12051                   (point))))
12052          gnus-newsgroup-bookmarks))
12053   (gnus-message 6 "A bookmark has been added to the current article."))
12054
12055 (defun gnus-summary-remove-bookmark (article)
12056   "Remove the bookmark from the current article."
12057   (interactive (list (gnus-summary-article-number)))
12058   (gnus-set-global-variables)
12059   ;; Remove old bookmark, if one exists.
12060   (let ((old (assq article gnus-newsgroup-bookmarks)))
12061     (if old
12062         (progn
12063           (setq gnus-newsgroup-bookmarks
12064                 (delq old gnus-newsgroup-bookmarks))
12065           (gnus-message 6 "Removed bookmark."))
12066       (gnus-message 6 "No bookmark in current article."))))
12067
12068 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12069 (defun gnus-summary-mark-as-dormant (n)
12070   "Mark N articles forward as dormant.
12071 If N is negative, mark backward instead.  The difference between N and
12072 the actual number of articles marked is returned."
12073   (interactive "p")
12074   (gnus-set-global-variables)
12075   (gnus-summary-mark-forward n gnus-dormant-mark))
12076
12077 (defun gnus-summary-set-process-mark (article)
12078   "Set the process mark on ARTICLE and update the summary line."
12079   (setq gnus-newsgroup-processable
12080         (cons article
12081               (delq article gnus-newsgroup-processable)))
12082   (when (gnus-summary-goto-subject article)
12083     (gnus-summary-show-thread)
12084     (gnus-summary-update-secondary-mark article)))
12085
12086 (defun gnus-summary-remove-process-mark (article)
12087   "Remove the process mark from ARTICLE and update the summary line."
12088   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
12089   (when (gnus-summary-goto-subject article)
12090     (gnus-summary-show-thread)
12091     (gnus-summary-update-secondary-mark article)))
12092
12093 (defun gnus-summary-set-saved-mark (article)
12094   "Set the process mark on ARTICLE and update the summary line."
12095   (push article gnus-newsgroup-saved)
12096   (when (gnus-summary-goto-subject article)
12097     (gnus-summary-update-secondary-mark article)))
12098
12099 (defun gnus-summary-mark-forward (n &optional mark no-expire)
12100   "Mark N articles as read forwards.
12101 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
12102 The difference between N and the actual number of articles marked is
12103 returned."
12104   (interactive "p")
12105   (gnus-set-global-variables)
12106   (let ((backward (< n 0))
12107         (gnus-summary-goto-unread
12108          (and gnus-summary-goto-unread
12109               (not (eq gnus-summary-goto-unread 'never))
12110               (not (memq mark (list gnus-unread-mark
12111                                     gnus-ticked-mark gnus-dormant-mark)))))
12112         (n (abs n))
12113         (mark (or mark gnus-del-mark)))
12114     (while (and (> n 0)
12115                 (gnus-summary-mark-article nil mark no-expire)
12116                 (zerop (gnus-summary-next-subject
12117                         (if backward -1 1)
12118                         (and gnus-summary-goto-unread
12119                              (not (eq gnus-summary-goto-unread 'never)))
12120                         t)))
12121       (setq n (1- n)))
12122     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12123     (gnus-summary-recenter)
12124     (gnus-summary-position-point)
12125     (gnus-set-mode-line 'summary)
12126     n))
12127
12128 (defun gnus-summary-mark-article-as-read (mark)
12129   "Mark the current article quickly as read with MARK."
12130   (let ((article (gnus-summary-article-number)))
12131     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12132     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12133     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12134     (setq gnus-newsgroup-reads
12135           (cons (cons article mark) gnus-newsgroup-reads))
12136     ;; Possibly remove from cache, if that is used.
12137     (and gnus-use-cache (gnus-cache-enter-remove-article article))
12138     ;; Allow the backend to change the mark.
12139     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
12140     ;; Check for auto-expiry.
12141     (when (and gnus-newsgroup-auto-expire
12142                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12143                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12144                    (= mark gnus-ancient-mark)
12145                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
12146       (setq mark gnus-expirable-mark)
12147       (push article gnus-newsgroup-expirable))
12148     ;; Set the mark in the buffer.
12149     (gnus-summary-update-mark mark 'unread)
12150     t))
12151
12152 (defun gnus-summary-mark-article-as-unread (mark)
12153   "Mark the current article quickly as unread with MARK."
12154   (let ((article (gnus-summary-article-number)))
12155     (if (< article 0)
12156         (gnus-error 1 "Unmarkable article")
12157       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12158       (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12159       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12160       (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
12161       (cond ((= mark gnus-ticked-mark)
12162              (push article gnus-newsgroup-marked))
12163             ((= mark gnus-dormant-mark)
12164              (push article gnus-newsgroup-dormant))
12165             (t
12166              (push article gnus-newsgroup-unreads)))
12167       (setq gnus-newsgroup-reads
12168             (delq (assq article gnus-newsgroup-reads)
12169                   gnus-newsgroup-reads))
12170
12171       ;; See whether the article is to be put in the cache.
12172       (and gnus-use-cache
12173            (vectorp (gnus-summary-article-header article))
12174            (save-excursion
12175              (gnus-cache-possibly-enter-article
12176               gnus-newsgroup-name article
12177               (gnus-summary-article-header article)
12178               (= mark gnus-ticked-mark)
12179               (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12180
12181       ;; Fix the mark.
12182       (gnus-summary-update-mark mark 'unread))
12183     t))
12184
12185 (defun gnus-summary-mark-article (&optional article mark no-expire)
12186   "Mark ARTICLE with MARK.  MARK can be any character.
12187 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12188 `??' (dormant) and `?E' (expirable).
12189 If MARK is nil, then the default character `?D' is used.
12190 If ARTICLE is nil, then the article on the current line will be
12191 marked."
12192   ;; The mark might be a string.
12193   (and (stringp mark)
12194        (setq mark (aref mark 0)))
12195   ;; If no mark is given, then we check auto-expiring.
12196   (and (not no-expire)
12197        gnus-newsgroup-auto-expire
12198        (or (not mark)
12199            (and (numberp mark)
12200                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12201                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12202                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12203        (setq mark gnus-expirable-mark))
12204   (let* ((mark (or mark gnus-del-mark))
12205          (article (or article (gnus-summary-article-number))))
12206     (or article (error "No article on current line"))
12207     (if (or (= mark gnus-unread-mark)
12208             (= mark gnus-ticked-mark)
12209             (= mark gnus-dormant-mark))
12210         (gnus-mark-article-as-unread article mark)
12211       (gnus-mark-article-as-read article mark))
12212
12213     ;; See whether the article is to be put in the cache.
12214     (and gnus-use-cache
12215          (not (= mark gnus-canceled-mark))
12216          (vectorp (gnus-summary-article-header article))
12217          (save-excursion
12218            (gnus-cache-possibly-enter-article
12219             gnus-newsgroup-name article
12220             (gnus-summary-article-header article)
12221             (= mark gnus-ticked-mark)
12222             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12223
12224     (if (gnus-summary-goto-subject article nil t)
12225         (let ((buffer-read-only nil))
12226           (gnus-summary-show-thread)
12227           ;; Fix the mark.
12228           (gnus-summary-update-mark mark 'unread)
12229           t))))
12230
12231 (defun gnus-summary-update-secondary-mark (article)
12232   "Update the secondary (read, process, cache) mark."
12233   (gnus-summary-update-mark
12234    (cond ((memq article gnus-newsgroup-processable)
12235           gnus-process-mark)
12236          ((memq article gnus-newsgroup-cached)
12237           gnus-cached-mark)
12238          ((memq article gnus-newsgroup-replied)
12239           gnus-replied-mark)
12240          ((memq article gnus-newsgroup-saved)
12241           gnus-saved-mark)
12242          (t gnus-unread-mark))
12243    'replied)
12244   (when (gnus-visual-p 'summary-highlight 'highlight)
12245     (run-hooks 'gnus-summary-update-hook))
12246   t)
12247
12248 (defun gnus-summary-update-mark (mark type)
12249   (beginning-of-line)
12250   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12251         (buffer-read-only nil))
12252     (when (and forward
12253                (<= (+ forward (point)) (point-max)))
12254       ;; Go to the right position on the line.
12255       (goto-char (+ forward (point)))
12256       ;; Replace the old mark with the new mark.
12257       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12258       ;; Optionally update the marks by some user rule.
12259       (when (eq type 'unread)
12260         (gnus-data-set-mark
12261          (gnus-data-find (gnus-summary-article-number)) mark)
12262         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12263
12264 (defun gnus-mark-article-as-read (article &optional mark)
12265   "Enter ARTICLE in the pertinent lists and remove it from others."
12266   ;; Make the article expirable.
12267   (let ((mark (or mark gnus-del-mark)))
12268     (if (= mark gnus-expirable-mark)
12269         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12270       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12271     ;; Remove from unread and marked lists.
12272     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12273     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12274     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12275     (push (cons article mark) gnus-newsgroup-reads)
12276     ;; Possibly remove from cache, if that is used.
12277     (when gnus-use-cache
12278       (gnus-cache-enter-remove-article article))))
12279
12280 (defun gnus-mark-article-as-unread (article &optional mark)
12281   "Enter ARTICLE in the pertinent lists and remove it from others."
12282   (let ((mark (or mark gnus-ticked-mark)))
12283     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12284     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12285     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12286     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12287     (cond ((= mark gnus-ticked-mark)
12288            (push article gnus-newsgroup-marked))
12289           ((= mark gnus-dormant-mark)
12290            (push article gnus-newsgroup-dormant))
12291           (t
12292            (push article gnus-newsgroup-unreads)))
12293     (setq gnus-newsgroup-reads
12294           (delq (assq article gnus-newsgroup-reads)
12295                 gnus-newsgroup-reads))))
12296
12297 (defalias 'gnus-summary-mark-as-unread-forward
12298   'gnus-summary-tick-article-forward)
12299 (make-obsolete 'gnus-summary-mark-as-unread-forward
12300                'gnus-summary-tick-article-forward)
12301 (defun gnus-summary-tick-article-forward (n)
12302   "Tick N articles forwards.
12303 If N is negative, tick backwards instead.
12304 The difference between N and the number of articles ticked is returned."
12305   (interactive "p")
12306   (gnus-summary-mark-forward n gnus-ticked-mark))
12307
12308 (defalias 'gnus-summary-mark-as-unread-backward
12309   'gnus-summary-tick-article-backward)
12310 (make-obsolete 'gnus-summary-mark-as-unread-backward
12311                'gnus-summary-tick-article-backward)
12312 (defun gnus-summary-tick-article-backward (n)
12313   "Tick N articles backwards.
12314 The difference between N and the number of articles ticked is returned."
12315   (interactive "p")
12316   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12317
12318 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12319 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12320 (defun gnus-summary-tick-article (&optional article clear-mark)
12321   "Mark current article as unread.
12322 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12323 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12324   (interactive)
12325   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12326                                        gnus-ticked-mark)))
12327
12328 (defun gnus-summary-mark-as-read-forward (n)
12329   "Mark N articles as read forwards.
12330 If N is negative, mark backwards instead.
12331 The difference between N and the actual number of articles marked is
12332 returned."
12333   (interactive "p")
12334   (gnus-summary-mark-forward n gnus-del-mark t))
12335
12336 (defun gnus-summary-mark-as-read-backward (n)
12337   "Mark the N articles as read backwards.
12338 The difference between N and the actual number of articles marked is
12339 returned."
12340   (interactive "p")
12341   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12342
12343 (defun gnus-summary-mark-as-read (&optional article mark)
12344   "Mark current article as read.
12345 ARTICLE specifies the article to be marked as read.
12346 MARK specifies a string to be inserted at the beginning of the line."
12347   (gnus-summary-mark-article article mark))
12348
12349 (defun gnus-summary-clear-mark-forward (n)
12350   "Clear marks from N articles forward.
12351 If N is negative, clear backward instead.
12352 The difference between N and the number of marks cleared is returned."
12353   (interactive "p")
12354   (gnus-summary-mark-forward n gnus-unread-mark))
12355
12356 (defun gnus-summary-clear-mark-backward (n)
12357   "Clear marks from N articles backward.
12358 The difference between N and the number of marks cleared is returned."
12359   (interactive "p")
12360   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12361
12362 (defun gnus-summary-mark-unread-as-read ()
12363   "Intended to be used by `gnus-summary-mark-article-hook'."
12364   (when (memq gnus-current-article gnus-newsgroup-unreads)
12365     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12366
12367 (defun gnus-summary-mark-read-and-unread-as-read ()
12368   "Intended to be used by `gnus-summary-mark-article-hook'."
12369   (let ((mark (gnus-summary-article-mark)))
12370     (when (or (gnus-unread-mark-p mark)
12371               (gnus-read-mark-p mark))
12372       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12373
12374 (defun gnus-summary-mark-region-as-read (point mark all)
12375   "Mark all unread articles between point and mark as read.
12376 If given a prefix, mark all articles between point and mark as read,
12377 even ticked and dormant ones."
12378   (interactive "r\nP")
12379   (save-excursion
12380     (let (article)
12381       (goto-char point)
12382       (beginning-of-line)
12383       (while (and
12384               (< (point) mark)
12385               (progn
12386                 (when (or all
12387                           (memq (setq article (gnus-summary-article-number))
12388                                 gnus-newsgroup-unreads))
12389                   (gnus-summary-mark-article article gnus-del-mark))
12390                 t)
12391               (gnus-summary-find-next))))))
12392
12393 (defun gnus-summary-mark-below (score mark)
12394   "Mark articles with score less than SCORE with MARK."
12395   (interactive "P\ncMark: ")
12396   (gnus-set-global-variables)
12397   (setq score (if score
12398                   (prefix-numeric-value score)
12399                 (or gnus-summary-default-score 0)))
12400   (save-excursion
12401     (set-buffer gnus-summary-buffer)
12402     (goto-char (point-min))
12403     (while 
12404         (progn
12405           (and (< (gnus-summary-article-score) score)
12406                (gnus-summary-mark-article nil mark))
12407           (gnus-summary-find-next)))))
12408
12409 (defun gnus-summary-kill-below (&optional score)
12410   "Mark articles with score below SCORE as read."
12411   (interactive "P")
12412   (gnus-set-global-variables)
12413   (gnus-summary-mark-below score gnus-killed-mark))
12414
12415 (defun gnus-summary-clear-above (&optional score)
12416   "Clear all marks from articles with score above SCORE."
12417   (interactive "P")
12418   (gnus-set-global-variables)
12419   (gnus-summary-mark-above score gnus-unread-mark))
12420
12421 (defun gnus-summary-tick-above (&optional score)
12422   "Tick all articles with score above SCORE."
12423   (interactive "P")
12424   (gnus-set-global-variables)
12425   (gnus-summary-mark-above score gnus-ticked-mark))
12426
12427 (defun gnus-summary-mark-above (score mark)
12428   "Mark articles with score over SCORE with MARK."
12429   (interactive "P\ncMark: ")
12430   (gnus-set-global-variables)
12431   (setq score (if score
12432                   (prefix-numeric-value score)
12433                 (or gnus-summary-default-score 0)))
12434   (save-excursion
12435     (set-buffer gnus-summary-buffer)
12436     (goto-char (point-min))
12437     (while (and (progn
12438                   (if (> (gnus-summary-article-score) score)
12439                       (gnus-summary-mark-article nil mark))
12440                   t)
12441                 (gnus-summary-find-next)))))
12442
12443 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12444 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12445 (defun gnus-summary-limit-include-expunged ()
12446   "Display all the hidden articles that were expunged for low scores."
12447   (interactive)
12448   (gnus-set-global-variables)
12449   (let ((buffer-read-only nil))
12450     (let ((scored gnus-newsgroup-scored)
12451           headers h)
12452       (while scored
12453         (or (gnus-summary-goto-subject (caar scored))
12454             (and (setq h (gnus-summary-article-header (caar scored)))
12455                  (< (cdar scored) gnus-summary-expunge-below)
12456                  (setq headers (cons h headers))))
12457         (setq scored (cdr scored)))
12458       (or headers (error "No expunged articles hidden."))
12459       (goto-char (point-min))
12460       (gnus-summary-prepare-unthreaded (nreverse headers)))
12461     (goto-char (point-min))
12462     (gnus-summary-position-point)))
12463
12464 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12465   "Mark all articles not marked as unread in this newsgroup as read.
12466 If prefix argument ALL is non-nil, all articles are marked as read.
12467 If QUIETLY is non-nil, no questions will be asked.
12468 If TO-HERE is non-nil, it should be a point in the buffer.  All
12469 articles before this point will be marked as read.
12470 The number of articles marked as read is returned."
12471   (interactive "P")
12472   (gnus-set-global-variables)
12473   (prog1
12474       (if (or quietly
12475               (not gnus-interactive-catchup) ;Without confirmation?
12476               gnus-expert-user
12477               (gnus-y-or-n-p
12478                (if all
12479                    "Mark absolutely all articles as read? "
12480                  "Mark all unread articles as read? ")))
12481           (if (and not-mark
12482                    (not gnus-newsgroup-adaptive)
12483                    (not gnus-newsgroup-auto-expire))
12484               (progn
12485                 (when all
12486                   (setq gnus-newsgroup-marked nil
12487                         gnus-newsgroup-dormant nil))
12488                 (setq gnus-newsgroup-unreads nil))
12489             ;; We actually mark all articles as canceled, which we
12490             ;; have to do when using auto-expiry or adaptive scoring.
12491             (gnus-summary-show-all-threads)
12492             (if (gnus-summary-first-subject (not all))
12493                 (while (and
12494                         (if to-here (< (point) to-here) t)
12495                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12496                         (gnus-summary-find-next (not all)))))
12497             (unless to-here
12498               (setq gnus-newsgroup-unreads nil))
12499             (gnus-set-mode-line 'summary)))
12500     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12501       (if (and (not to-here) (eq 'nnvirtual (car method)))
12502           (nnvirtual-catchup-group
12503            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12504     (gnus-summary-position-point)))
12505
12506 (defun gnus-summary-catchup-to-here (&optional all)
12507   "Mark all unticked articles before the current one as read.
12508 If ALL is non-nil, also mark ticked and dormant articles as read."
12509   (interactive "P")
12510   (gnus-set-global-variables)
12511   (save-excursion
12512     (gnus-save-hidden-threads
12513       (let ((beg (point)))
12514         ;; We check that there are unread articles.
12515         (when (or all (gnus-summary-find-prev))
12516           (gnus-summary-catchup all t beg)))))
12517   (gnus-summary-position-point))
12518
12519 (defun gnus-summary-catchup-all (&optional quietly)
12520   "Mark all articles in this newsgroup as read."
12521   (interactive "P")
12522   (gnus-set-global-variables)
12523   (gnus-summary-catchup t quietly))
12524
12525 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12526   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12527 If prefix argument ALL is non-nil, all articles are marked as read."
12528   (interactive "P")
12529   (gnus-set-global-variables)
12530   (gnus-summary-catchup all quietly nil 'fast)
12531   ;; Select next newsgroup or exit.
12532   (if (eq gnus-auto-select-next 'quietly)
12533       (gnus-summary-next-group nil)
12534     (gnus-summary-exit)))
12535
12536 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12537   "Mark all articles in this newsgroup as read, and then exit."
12538   (interactive "P")
12539   (gnus-set-global-variables)
12540   (gnus-summary-catchup-and-exit t quietly))
12541
12542 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12543 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12544   "Mark all articles in this group as read and select the next group.
12545 If given a prefix, mark all articles, unread as well as ticked, as
12546 read."
12547   (interactive "P")
12548   (gnus-set-global-variables)
12549   (save-excursion
12550     (gnus-summary-catchup all))
12551   (gnus-summary-next-article t nil nil t))
12552
12553 ;; Thread-based commands.
12554
12555 (defun gnus-summary-articles-in-thread (&optional article)
12556   "Return a list of all articles in the current thread.
12557 If ARTICLE is non-nil, return all articles in the thread that starts
12558 with that article."
12559   (let* ((article (or article (gnus-summary-article-number)))
12560          (data (gnus-data-find-list article))
12561          (top-level (gnus-data-level (car data)))
12562          (top-subject
12563           (cond ((null gnus-thread-operation-ignore-subject)
12564                  (gnus-simplify-subject-re
12565                   (mail-header-subject (gnus-data-header (car data)))))
12566                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12567                  (gnus-simplify-subject-fuzzy
12568                   (mail-header-subject (gnus-data-header (car data)))))
12569                 (t nil)))
12570          (end-point (save-excursion
12571                       (if (gnus-summary-go-to-next-thread) 
12572                           (point) (point-max))))
12573          articles)
12574     (while (and data
12575                 (< (gnus-data-pos (car data)) end-point))
12576       (when (or (not top-subject)
12577                 (string= top-subject
12578                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12579                              (gnus-simplify-subject-fuzzy
12580                               (mail-header-subject
12581                                (gnus-data-header (car data))))
12582                            (gnus-simplify-subject-re
12583                             (mail-header-subject
12584                              (gnus-data-header (car data)))))))
12585         (push (gnus-data-number (car data)) articles))
12586       (unless (and (setq data (cdr data))
12587                    (> (gnus-data-level (car data)) top-level))
12588         (setq data nil)))
12589     ;; Return the list of articles.
12590     (nreverse articles)))
12591
12592 (defun gnus-summary-rethread-current ()
12593   "Rethread the thread the current article is part of."
12594   (interactive)
12595   (gnus-set-global-variables)
12596   (let* ((gnus-show-threads t)
12597          (article (gnus-summary-article-number))
12598          (id (mail-header-id (gnus-summary-article-header)))
12599          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12600     (unless id
12601       (error "No article on the current line"))
12602     (gnus-rebuild-thread id)
12603     (gnus-summary-goto-subject article)))
12604
12605 (defun gnus-summary-reparent-thread ()
12606   "Make current article child of the marked (or previous) article.
12607
12608 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12609 is non-nil or the Subject: of both articles are the same."
12610   (interactive)
12611   (or (not (gnus-group-read-only-p))
12612       (error "The current newsgroup does not support article editing."))
12613   (or (<= (length gnus-newsgroup-processable) 1)
12614       (error "No more than one article may be marked."))
12615   (save-window-excursion
12616     (let ((gnus-article-buffer " *reparent*")
12617           (current-article (gnus-summary-article-number))
12618           ; first grab the marked article, otherwise one line up.
12619           (parent-article (if (not (null gnus-newsgroup-processable))
12620                               (car gnus-newsgroup-processable)
12621                             (save-excursion
12622                               (if (eq (forward-line -1) 0)
12623                                   (gnus-summary-article-number)
12624                                 (error "Beginning of summary buffer."))))))
12625       (or (not (eq current-article parent-article))
12626           (error "An article may not be self-referential."))
12627       (let ((message-id (mail-header-id 
12628                          (gnus-summary-article-header parent-article))))
12629         (or (and message-id (not (equal message-id "")))
12630             (error "No message-id in desired parent."))
12631         (gnus-summary-select-article t t nil current-article)
12632         (set-buffer gnus-article-buffer)
12633         (setq buffer-read-only nil)
12634         (let ((buf (format "%s" (buffer-string))))
12635           (erase-buffer)
12636           (insert buf))
12637         (goto-char (point-min))
12638         (if (search-forward-regexp "^References: " nil t)
12639             (insert message-id " " )
12640           (insert "References: " message-id "\n"))
12641         (or (gnus-request-replace-article current-article
12642                                           (car gnus-article-current)
12643                                           gnus-article-buffer)
12644             (error "Couldn't replace article."))
12645         (set-buffer gnus-summary-buffer)
12646         (gnus-summary-unmark-all-processable)
12647         (gnus-summary-rethread-current)
12648         (gnus-message 3 "Article %d is now the child of article %d."
12649                       current-article parent-article)))))
12650
12651 (defun gnus-summary-toggle-threads (&optional arg)
12652   "Toggle showing conversation threads.
12653 If ARG is positive number, turn showing conversation threads on."
12654   (interactive "P")
12655   (gnus-set-global-variables)
12656   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12657     (setq gnus-show-threads
12658           (if (null arg) (not gnus-show-threads)
12659             (> (prefix-numeric-value arg) 0)))
12660     (gnus-summary-prepare)
12661     (gnus-summary-goto-subject current)
12662     (gnus-summary-position-point)))
12663
12664 (defun gnus-summary-show-all-threads ()
12665   "Show all threads."
12666   (interactive)
12667   (gnus-set-global-variables)
12668   (save-excursion
12669     (let ((buffer-read-only nil))
12670       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12671   (gnus-summary-position-point))
12672
12673 (defun gnus-summary-show-thread ()
12674   "Show thread subtrees.
12675 Returns nil if no thread was there to be shown."
12676   (interactive)
12677   (gnus-set-global-variables)
12678   (let ((buffer-read-only nil)
12679         (orig (point))
12680         ;; first goto end then to beg, to have point at beg after let
12681         (end (progn (end-of-line) (point)))
12682         (beg (progn (beginning-of-line) (point))))
12683     (prog1
12684         ;; Any hidden lines here?
12685         (search-forward "\r" end t)
12686       (subst-char-in-region beg end ?\^M ?\n t)
12687       (goto-char orig)
12688       (gnus-summary-position-point))))
12689
12690 (defun gnus-summary-hide-all-threads ()
12691   "Hide all thread subtrees."
12692   (interactive)
12693   (gnus-set-global-variables)
12694   (save-excursion
12695     (goto-char (point-min))
12696     (gnus-summary-hide-thread)
12697     (while (zerop (gnus-summary-next-thread 1 t))
12698       (gnus-summary-hide-thread)))
12699   (gnus-summary-position-point))
12700
12701 (defun gnus-summary-hide-thread ()
12702   "Hide thread subtrees.
12703 Returns nil if no threads were there to be hidden."
12704   (interactive)
12705   (gnus-set-global-variables)
12706   (let ((buffer-read-only nil)
12707         (start (point))
12708         (article (gnus-summary-article-number)))
12709     (goto-char start)
12710     ;; Go forward until either the buffer ends or the subthread
12711     ;; ends.
12712     (when (and (not (eobp))
12713                (or (zerop (gnus-summary-next-thread 1 t))
12714                    (goto-char (point-max))))
12715       (prog1
12716           (if (and (> (point) start)
12717                    (search-backward "\n" start t))
12718               (progn
12719                 (subst-char-in-region start (point) ?\n ?\^M)
12720                 (gnus-summary-goto-subject article))
12721             (goto-char start)
12722             nil)
12723         ;;(gnus-summary-position-point)
12724         ))))
12725
12726 (defun gnus-summary-go-to-next-thread (&optional previous)
12727   "Go to the same level (or less) next thread.
12728 If PREVIOUS is non-nil, go to previous thread instead.
12729 Return the article number moved to, or nil if moving was impossible."
12730   (let ((level (gnus-summary-thread-level))
12731         (way (if previous -1 1))
12732         (beg (point)))
12733     (forward-line way)
12734     (while (and (not (eobp))
12735                 (< level (gnus-summary-thread-level)))
12736       (forward-line way))
12737     (if (eobp)
12738         (progn
12739           (goto-char beg)
12740           nil)
12741       (setq beg (point))
12742       (prog1
12743           (gnus-summary-article-number)
12744         (goto-char beg)))))
12745
12746 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12747   "Go to the same level (or less) next thread.
12748 If PREVIOUS is non-nil, go to previous thread instead.
12749 Return the article number moved to, or nil if moving was impossible."
12750   (if (and (eq gnus-summary-make-false-root 'dummy)
12751            (gnus-summary-article-intangible-p))
12752       (let ((beg (point)))
12753         (while (and (zerop (forward-line 1))
12754                     (not (gnus-summary-article-intangible-p))
12755                     (not (zerop (save-excursion 
12756                                   (gnus-summary-thread-level))))))
12757         (if (eobp)
12758             (progn
12759               (goto-char beg)
12760               nil)
12761           (point)))
12762     (let* ((level (gnus-summary-thread-level))
12763            (article (gnus-summary-article-number))
12764            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12765            oart)
12766       (while data
12767         (if (<= (gnus-data-level (car data)) level)
12768             (setq oart (gnus-data-number (car data))
12769                   data nil)
12770           (setq data (cdr data))))
12771       (and oart
12772            (gnus-summary-goto-subject oart)))))
12773
12774 (defun gnus-summary-next-thread (n &optional silent)
12775   "Go to the same level next N'th thread.
12776 If N is negative, search backward instead.
12777 Returns the difference between N and the number of skips actually
12778 done.
12779
12780 If SILENT, don't output messages."
12781   (interactive "p")
12782   (gnus-set-global-variables)
12783   (let ((backward (< n 0))
12784         (n (abs n))
12785         old dum int)
12786     (while (and (> n 0)
12787                 (gnus-summary-go-to-next-thread backward))
12788       (decf n))
12789     (unless silent 
12790       (gnus-summary-position-point))
12791     (when (and (not silent) (/= 0 n))
12792       (gnus-message 7 "No more threads"))
12793     n))
12794
12795 (defun gnus-summary-prev-thread (n)
12796   "Go to the same level previous N'th thread.
12797 Returns the difference between N and the number of skips actually
12798 done."
12799   (interactive "p")
12800   (gnus-set-global-variables)
12801   (gnus-summary-next-thread (- n)))
12802
12803 (defun gnus-summary-go-down-thread ()
12804   "Go down one level in the current thread."
12805   (let ((children (gnus-summary-article-children)))
12806     (and children
12807          (gnus-summary-goto-subject (car children)))))
12808
12809 (defun gnus-summary-go-up-thread ()
12810   "Go up one level in the current thread."
12811   (let ((parent (gnus-summary-article-parent)))
12812     (and parent
12813          (gnus-summary-goto-subject parent))))
12814
12815 (defun gnus-summary-down-thread (n)
12816   "Go down thread N steps.
12817 If N is negative, go up instead.
12818 Returns the difference between N and how many steps down that were
12819 taken."
12820   (interactive "p")
12821   (gnus-set-global-variables)
12822   (let ((up (< n 0))
12823         (n (abs n)))
12824     (while (and (> n 0)
12825                 (if up (gnus-summary-go-up-thread)
12826                   (gnus-summary-go-down-thread)))
12827       (setq n (1- n)))
12828     (gnus-summary-position-point)
12829     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12830     n))
12831
12832 (defun gnus-summary-up-thread (n)
12833   "Go up thread N steps.
12834 If N is negative, go up instead.
12835 Returns the difference between N and how many steps down that were
12836 taken."
12837   (interactive "p")
12838   (gnus-set-global-variables)
12839   (gnus-summary-down-thread (- n)))
12840
12841 (defun gnus-summary-top-thread ()
12842   "Go to the top of the thread."
12843   (interactive)
12844   (gnus-set-global-variables)
12845   (while (gnus-summary-go-up-thread))
12846   (gnus-summary-article-number))
12847
12848 (defun gnus-summary-kill-thread (&optional unmark)
12849   "Mark articles under current thread as read.
12850 If the prefix argument is positive, remove any kinds of marks.
12851 If the prefix argument is negative, tick articles instead."
12852   (interactive "P")
12853   (gnus-set-global-variables)
12854   (when unmark
12855     (setq unmark (prefix-numeric-value unmark)))
12856   (let ((articles (gnus-summary-articles-in-thread)))
12857     (save-excursion
12858       ;; Expand the thread.
12859       (gnus-summary-show-thread)
12860       ;; Mark all the articles.
12861       (while articles
12862         (gnus-summary-goto-subject (car articles))
12863         (cond ((null unmark)
12864                (gnus-summary-mark-article-as-read gnus-killed-mark))
12865               ((> unmark 0)
12866                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12867               (t
12868                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12869         (setq articles (cdr articles))))
12870     ;; Hide killed subtrees.
12871     (and (null unmark)
12872          gnus-thread-hide-killed
12873          (gnus-summary-hide-thread))
12874     ;; If marked as read, go to next unread subject.
12875     (if (null unmark)
12876         ;; Go to next unread subject.
12877         (gnus-summary-next-subject 1 t)))
12878   (gnus-set-mode-line 'summary))
12879
12880 ;; Summary sorting commands
12881
12882 (defun gnus-summary-sort-by-number (&optional reverse)
12883   "Sort summary buffer by article number.
12884 Argument REVERSE means reverse order."
12885   (interactive "P")
12886   (gnus-summary-sort 'number reverse))
12887
12888 (defun gnus-summary-sort-by-author (&optional reverse)
12889   "Sort summary buffer by author name alphabetically.
12890 If case-fold-search is non-nil, case of letters is ignored.
12891 Argument REVERSE means reverse order."
12892   (interactive "P")
12893   (gnus-summary-sort 'author reverse))
12894
12895 (defun gnus-summary-sort-by-subject (&optional reverse)
12896   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12897 If case-fold-search is non-nil, case of letters is ignored.
12898 Argument REVERSE means reverse order."
12899   (interactive "P")
12900   (gnus-summary-sort 'subject reverse))
12901
12902 (defun gnus-summary-sort-by-date (&optional reverse)
12903   "Sort summary buffer by date.
12904 Argument REVERSE means reverse order."
12905   (interactive "P")
12906   (gnus-summary-sort 'date reverse))
12907
12908 (defun gnus-summary-sort-by-score (&optional reverse)
12909   "Sort summary buffer by score.
12910 Argument REVERSE means reverse order."
12911   (interactive "P")
12912   (gnus-summary-sort 'score reverse))
12913
12914 (defun gnus-summary-sort (predicate reverse)
12915   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12916   (gnus-set-global-variables)
12917   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12918          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12919          (gnus-thread-sort-functions
12920           (list
12921            (if (not reverse)
12922                thread
12923              `(lambda (t1 t2)
12924                 (,thread t2 t1)))))
12925          (gnus-article-sort-functions
12926           (list
12927            (if (not reverse)
12928                article
12929              `(lambda (t1 t2)
12930                 (,article t2 t1)))))
12931          (buffer-read-only)
12932          (gnus-summary-prepare-hook nil))
12933     ;; We do the sorting by regenerating the threads.
12934     (gnus-summary-prepare)
12935     ;; Hide subthreads if needed.
12936     (when (and gnus-show-threads gnus-thread-hide-subtree)
12937       (gnus-summary-hide-all-threads)))
12938   ;; If in async mode, we send some info to the backend.
12939   (when gnus-newsgroup-async
12940     (gnus-request-asynchronous
12941      gnus-newsgroup-name gnus-newsgroup-data)))
12942
12943 (defun gnus-sortable-date (date)
12944   "Make sortable string by string-lessp from DATE.
12945 Timezone package is used."
12946   (condition-case ()
12947       (progn
12948         (setq date (inline (timezone-fix-time 
12949                             date nil 
12950                             (aref (inline (timezone-parse-date date)) 4))))
12951         (inline
12952           (timezone-make-sortable-date
12953            (aref date 0) (aref date 1) (aref date 2)
12954            (inline
12955              (timezone-make-time-string
12956               (aref date 3) (aref date 4) (aref date 5))))))
12957     (error "")))
12958   
12959 ;; Summary saving commands.
12960
12961 (defun gnus-summary-save-article (&optional n not-saved)
12962   "Save the current article using the default saver function.
12963 If N is a positive number, save the N next articles.
12964 If N is a negative number, save the N previous articles.
12965 If N is nil and any articles have been marked with the process mark,
12966 save those articles instead.
12967 The variable `gnus-default-article-saver' specifies the saver function."
12968   (interactive "P")
12969   (gnus-set-global-variables)
12970   (let ((articles (gnus-summary-work-articles n))
12971         (save-buffer (save-excursion 
12972                        (nnheader-set-temp-buffer " *Gnus Save*")))
12973         file header article)
12974     (while articles
12975       (setq header (gnus-summary-article-header
12976                     (setq article (pop articles))))
12977       (if (not (vectorp header))
12978           ;; This is a pseudo-article.
12979           (if (assq 'name header)
12980               (gnus-copy-file (cdr (assq 'name header)))
12981             (gnus-message 1 "Article %d is unsaveable" article))
12982         ;; This is a real article.
12983         (save-window-excursion
12984           (gnus-summary-select-article t nil nil article))
12985         (save-excursion
12986           (set-buffer save-buffer)
12987           (erase-buffer)
12988           (insert-buffer-substring gnus-original-article-buffer))
12989         (unless gnus-save-all-headers
12990           ;; Remove headers accoring to `gnus-saved-headers'.
12991           (let ((gnus-visible-headers
12992                  (or gnus-saved-headers gnus-visible-headers))
12993                 (gnus-article-buffer save-buffer))
12994             (gnus-article-hide-headers 1 t)))
12995         (save-window-excursion
12996           (if (not gnus-default-article-saver)
12997               (error "No default saver is defined.")
12998             ;; !!! Magic!  The saving functions all save
12999             ;; `gnus-original-article-buffer' (or so they think),
13000             ;; but we bind that variable to our save-buffer.
13001             (set-buffer gnus-article-buffer)
13002             (let ((gnus-original-article-buffer save-buffer))
13003               (set-buffer gnus-summary-buffer)
13004               (setq file (funcall
13005                           gnus-default-article-saver
13006                           (cond
13007                            ((not gnus-prompt-before-saving)
13008                             'default)
13009                            ((eq gnus-prompt-before-saving 'always)
13010                             nil)
13011                            (t file)))))))
13012         (gnus-summary-remove-process-mark article)
13013         (unless not-saved
13014           (gnus-summary-set-saved-mark article))))
13015     (gnus-kill-buffer save-buffer)
13016     (gnus-summary-position-point)
13017     n))
13018
13019 (defun gnus-summary-pipe-output (&optional arg)
13020   "Pipe the current article to a subprocess.
13021 If N is a positive number, pipe the N next articles.
13022 If N is a negative number, pipe the N previous articles.
13023 If N is nil and any articles have been marked with the process mark,
13024 pipe those articles instead."
13025   (interactive "P")
13026   (gnus-set-global-variables)
13027   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
13028     (gnus-summary-save-article arg t))
13029   (gnus-configure-windows 'pipe))
13030
13031 (defun gnus-summary-save-article-mail (&optional arg)
13032   "Append the current article to an mail file.
13033 If N is a positive number, save the N next articles.
13034 If N is a negative number, save the N previous articles.
13035 If N is nil and any articles have been marked with the process mark,
13036 save those articles instead."
13037   (interactive "P")
13038   (gnus-set-global-variables)
13039   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
13040     (gnus-summary-save-article arg)))
13041
13042 (defun gnus-summary-save-article-rmail (&optional arg)
13043   "Append the current article to an rmail file.
13044 If N is a positive number, save the N next articles.
13045 If N is a negative number, save the N previous articles.
13046 If N is nil and any articles have been marked with the process mark,
13047 save those articles instead."
13048   (interactive "P")
13049   (gnus-set-global-variables)
13050   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
13051     (gnus-summary-save-article arg)))
13052
13053 (defun gnus-summary-save-article-file (&optional arg)
13054   "Append the current article to a file.
13055 If N is a positive number, save the N next articles.
13056 If N is a negative number, save the N previous articles.
13057 If N is nil and any articles have been marked with the process mark,
13058 save those articles instead."
13059   (interactive "P")
13060   (gnus-set-global-variables)
13061   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
13062     (gnus-summary-save-article arg)))
13063
13064 (defun gnus-summary-save-article-body-file (&optional arg)
13065   "Append the current article body to a file.
13066 If N is a positive number, save the N next articles.
13067 If N is a negative number, save the N previous articles.
13068 If N is nil and any articles have been marked with the process mark,
13069 save those articles instead."
13070   (interactive "P")
13071   (gnus-set-global-variables)
13072   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
13073     (gnus-summary-save-article arg)))
13074
13075 (defun gnus-get-split-value (methods)
13076   "Return a value based on the split METHODS."
13077   (let (split-name method result match)
13078     (when methods
13079       (save-excursion
13080         (set-buffer gnus-original-article-buffer)
13081         (save-restriction
13082           (nnheader-narrow-to-headers)
13083           (while methods
13084             (goto-char (point-min))
13085             (setq method (pop methods))
13086             (setq match (car method))
13087             (when (cond
13088                    ((stringp match)
13089                     ;; Regular expression.
13090                     (condition-case ()
13091                         (re-search-forward match nil t)
13092                       (error nil)))
13093                    ((gnus-functionp match)
13094                     ;; Function.
13095                     (save-restriction
13096                       (widen)
13097                       (setq result (funcall match gnus-newsgroup-name))))
13098                    ((consp match)
13099                     ;; Form.
13100                     (save-restriction
13101                       (widen)
13102                       (setq result (eval match)))))
13103               (setq split-name (append (cdr method) split-name))
13104               (cond ((stringp result)
13105                      (push result split-name))
13106                     ((consp result)
13107                      (setq split-name (append result split-name)))))))))
13108     split-name))
13109
13110 (defun gnus-read-move-group-name (prompt default articles prefix)
13111   "Read a group name."
13112   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
13113          (minibuffer-confirm-incomplete nil) ; XEmacs
13114          group-map
13115          (dum (mapatoms
13116                (lambda (g) 
13117                  (and (boundp g)
13118                       (symbol-name g)
13119                       (memq 'respool
13120                             (assoc (symbol-name
13121                                     (car (gnus-find-method-for-group
13122                                           (symbol-name g))))
13123                                    gnus-valid-select-methods))
13124                       (push (list (symbol-name g)) group-map)))
13125                gnus-active-hashtb))
13126          (prom
13127           (format "%s %s to:"
13128                   prompt
13129                   (if (> (length articles) 1)
13130                       (format "these %d articles" (length articles))
13131                     "this article")))
13132          (to-newsgroup
13133           (cond
13134            ((null split-name)
13135             (gnus-completing-read default prom
13136                                   group-map nil nil prefix
13137                                   'gnus-group-history))
13138            ((= 1 (length split-name))
13139             (gnus-completing-read (car split-name) prom group-map
13140                                   nil nil nil
13141                                   'gnus-group-history))
13142            (t
13143             (gnus-completing-read nil prom 
13144                                   (mapcar (lambda (el) (list el))
13145                                           (nreverse split-name))
13146                                   nil nil nil
13147                                   'gnus-group-history)))))
13148     (when to-newsgroup
13149       (if (or (string= to-newsgroup "")
13150               (string= to-newsgroup prefix))
13151           (setq to-newsgroup (or default "")))
13152       (or (gnus-active to-newsgroup)
13153           (gnus-activate-group to-newsgroup)
13154           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
13155                                      to-newsgroup))
13156               (or (and (gnus-request-create-group 
13157                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
13158                        (gnus-activate-group to-newsgroup nil nil
13159                                             (gnus-group-name-to-method
13160                                              to-newsgroup)))
13161                   (error "Couldn't create group %s" to-newsgroup)))
13162           (error "No such group: %s" to-newsgroup)))
13163     to-newsgroup))
13164
13165 (defun gnus-read-save-file-name (prompt default-name)
13166   (let* ((split-name (gnus-get-split-value gnus-split-methods))
13167          (file
13168           ;; Let the split methods have their say.
13169           (cond
13170            ;; No split name was found.
13171            ((null split-name)
13172             (read-file-name
13173              (concat prompt " (default "
13174                      (file-name-nondirectory default-name) ") ")
13175              (file-name-directory default-name)
13176              default-name))
13177            ;; A single split name was found
13178            ((= 1 (length split-name))
13179             (let* ((name (car split-name))
13180                    (dir (cond ((file-directory-p name)
13181                                (file-name-as-directory name))
13182                               ((file-exists-p name) name)
13183                               (t gnus-article-save-directory))))
13184               (read-file-name
13185                (concat prompt " (default " name ") ")
13186                dir name)))
13187            ;; A list of splits was found.
13188            (t
13189             (setq split-name (nreverse split-name))
13190             (let (result)
13191               (let ((file-name-history (nconc split-name file-name-history)))
13192                 (setq result
13193                       (read-file-name
13194                        (concat prompt " (`M-p' for defaults) ")
13195                        gnus-article-save-directory
13196                        (car split-name))))
13197               (car (push result file-name-history)))))))
13198     ;; If we have read a directory, we append the default file name.
13199     (when (file-directory-p file)
13200       (setq file (concat (file-name-as-directory file)
13201                          (file-name-nondirectory default-name))))
13202     ;; Possibly translate some charaters.
13203     (nnheader-translate-file-chars file)))
13204
13205 (defun gnus-article-archive-name (group)
13206   "Return the first instance of an \"Archive-name\" in the current buffer."
13207   (let ((case-fold-search t))
13208     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13209       (match-string 1))))
13210
13211 (defun gnus-summary-save-in-rmail (&optional filename)
13212   "Append this article to Rmail file.
13213 Optional argument FILENAME specifies file name.
13214 Directory to save to is default to `gnus-article-save-directory'."
13215   (interactive)
13216   (gnus-set-global-variables)
13217   (let ((default-name
13218           (funcall gnus-rmail-save-name gnus-newsgroup-name
13219                    gnus-current-headers gnus-newsgroup-last-rmail)))
13220     (setq filename
13221           (cond ((eq filename 'default)
13222                  default-name)
13223                 (filename filename)
13224                 (t (gnus-read-save-file-name
13225                     "Save in rmail file:" default-name))))
13226     (gnus-make-directory (file-name-directory filename))
13227     (gnus-eval-in-buffer-window gnus-original-article-buffer
13228       (save-excursion
13229         (save-restriction
13230           (widen)
13231           (gnus-output-to-rmail filename))))
13232     ;; Remember the directory name to save articles
13233     (setq gnus-newsgroup-last-rmail filename)))
13234
13235 (defun gnus-summary-save-in-mail (&optional filename)
13236   "Append this article to Unix mail file.
13237 Optional argument FILENAME specifies file name.
13238 Directory to save to is default to `gnus-article-save-directory'."
13239   (interactive)
13240   (gnus-set-global-variables)
13241   (let ((default-name
13242           (funcall gnus-mail-save-name gnus-newsgroup-name
13243                    gnus-current-headers gnus-newsgroup-last-mail)))
13244     (setq filename
13245           (cond ((eq filename 'default)
13246                  default-name)
13247                 (filename filename)
13248                 (t (gnus-read-save-file-name
13249                     "Save in Unix mail file:" default-name))))
13250     (setq filename
13251           (expand-file-name filename
13252                             (and default-name
13253                                  (file-name-directory 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           (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13260               (gnus-output-to-rmail filename)
13261             (let ((mail-use-rfc822 t))
13262               (rmail-output filename 1 t t))))))
13263     ;; Remember the directory name to save articles.
13264     (setq gnus-newsgroup-last-mail filename)))
13265
13266 (defun gnus-summary-save-in-file (&optional filename)
13267   "Append this article to file.
13268 Optional argument FILENAME specifies file name.
13269 Directory to save to is default to `gnus-article-save-directory'."
13270   (interactive)
13271   (gnus-set-global-variables)
13272   (let ((default-name
13273           (funcall gnus-file-save-name gnus-newsgroup-name
13274                    gnus-current-headers gnus-newsgroup-last-file)))
13275     (setq filename
13276           (cond ((eq filename 'default)
13277                  default-name)
13278                 (filename filename)
13279                 (t (gnus-read-save-file-name
13280                     "Save in file:" default-name))))
13281     (gnus-make-directory (file-name-directory filename))
13282     (gnus-eval-in-buffer-window gnus-original-article-buffer
13283       (save-excursion
13284         (save-restriction
13285           (widen)
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-body-in-file (&optional filename)
13291   "Append this article body to a file.
13292 Optional argument FILENAME specifies file name.
13293 The directory to save in defaults to `gnus-article-save-directory'."
13294   (interactive)
13295   (gnus-set-global-variables)
13296   (let ((default-name
13297           (funcall gnus-file-save-name gnus-newsgroup-name
13298                    gnus-current-headers gnus-newsgroup-last-file)))
13299     (setq filename
13300           (cond ((eq filename 'default)
13301                  default-name)
13302                 (filename filename)
13303                 (t (gnus-read-save-file-name
13304                     "Save body in file:" default-name))))
13305     (gnus-make-directory (file-name-directory filename))
13306     (gnus-eval-in-buffer-window gnus-article-buffer
13307       (save-excursion
13308         (save-restriction
13309           (widen)
13310           (goto-char (point-min))
13311           (and (search-forward "\n\n" nil t)
13312                (narrow-to-region (point) (point-max)))
13313           (gnus-output-to-file filename))))
13314     ;; Remember the directory name to save articles.
13315     (setq gnus-newsgroup-last-file filename)))
13316
13317 (defun gnus-summary-save-in-pipe (&optional command)
13318   "Pipe this article to subprocess."
13319   (interactive)
13320   (gnus-set-global-variables)
13321   (setq command
13322         (cond ((eq command 'default)
13323                gnus-last-shell-command)
13324               (command command)
13325               (t (read-string "Shell command on article: "
13326                               gnus-last-shell-command))))
13327   (if (string-equal command "")
13328       (setq command gnus-last-shell-command))
13329   (gnus-eval-in-buffer-window gnus-article-buffer
13330     (save-restriction
13331       (widen)
13332       (shell-command-on-region (point-min) (point-max) command nil)))
13333   (setq gnus-last-shell-command command))
13334
13335 ;; Summary extract commands
13336
13337 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13338   (let ((buffer-read-only nil)
13339         (article (gnus-summary-article-number))
13340         after-article b e)
13341     (or (gnus-summary-goto-subject article)
13342         (error (format "No such article: %d" article)))
13343     (gnus-summary-position-point)
13344     ;; If all commands are to be bunched up on one line, we collect
13345     ;; them here.
13346     (if gnus-view-pseudos-separately
13347         ()
13348       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13349             files action)
13350         (while ps
13351           (setq action (cdr (assq 'action (car ps))))
13352           (setq files (list (cdr (assq 'name (car ps)))))
13353           (while (and ps (cdr ps)
13354                       (string= (or action "1")
13355                                (or (cdr (assq 'action (cadr ps))) "2")))
13356             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13357             (setcdr ps (cddr ps)))
13358           (if (not files)
13359               ()
13360             (if (not (string-match "%s" action))
13361                 (setq files (cons " " files)))
13362             (setq files (cons " " files))
13363             (and (assq 'execute (car ps))
13364                  (setcdr (assq 'execute (car ps))
13365                          (funcall (if (string-match "%s" action)
13366                                       'format 'concat)
13367                                   action
13368                                   (mapconcat (lambda (f) f) files " ")))))
13369           (setq ps (cdr ps)))))
13370     (if (and gnus-view-pseudos (not not-view))
13371         (while pslist
13372           (and (assq 'execute (car pslist))
13373                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13374                                      (eq gnus-view-pseudos 'not-confirm)))
13375           (setq pslist (cdr pslist)))
13376       (save-excursion
13377         (while pslist
13378           (setq after-article (or (cdr (assq 'article (car pslist)))
13379                                   (gnus-summary-article-number)))
13380           (gnus-summary-goto-subject after-article)
13381           (forward-line 1)
13382           (setq b (point))
13383           (insert "    " (file-name-nondirectory
13384                                 (cdr (assq 'name (car pslist))))
13385                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13386           (setq e (point))
13387           (forward-line -1)             ; back to `b'
13388           (gnus-add-text-properties
13389            b (1- e) (list 'gnus-number gnus-reffed-article-number
13390                           gnus-mouse-face-prop gnus-mouse-face))
13391           (gnus-data-enter
13392            after-article gnus-reffed-article-number
13393            gnus-unread-mark b (car pslist) 0 (- e b))
13394           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13395           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13396           (setq pslist (cdr pslist)))))))
13397
13398 (defun gnus-pseudos< (p1 p2)
13399   (let ((c1 (cdr (assq 'action p1)))
13400         (c2 (cdr (assq 'action p2))))
13401     (and c1 c2 (string< c1 c2))))
13402
13403 (defun gnus-request-pseudo-article (props)
13404   (cond ((assq 'execute props)
13405          (gnus-execute-command (cdr (assq 'execute props)))))
13406   (let ((gnus-current-article (gnus-summary-article-number)))
13407     (run-hooks 'gnus-mark-article-hook)))
13408
13409 (defun gnus-execute-command (command &optional automatic)
13410   (save-excursion
13411     (gnus-article-setup-buffer)
13412     (set-buffer gnus-article-buffer)
13413     (setq buffer-read-only nil)
13414     (let ((command (if automatic command (read-string "Command: " command)))
13415           ;; Just binding this here doesn't help, because there might
13416           ;; be output from the process after exiting the scope of 
13417           ;; this `let'.
13418           ;; (buffer-read-only nil)
13419           )
13420       (erase-buffer)
13421       (insert "$ " command "\n\n")
13422       (if gnus-view-pseudo-asynchronously
13423           (start-process "gnus-execute" nil shell-file-name
13424                          shell-command-switch command)
13425         (call-process shell-file-name nil t nil
13426                       shell-command-switch command)))))
13427
13428 (defun gnus-copy-file (file &optional to)
13429   "Copy FILE to TO."
13430   (interactive
13431    (list (read-file-name "Copy file: " default-directory)
13432          (read-file-name "Copy file to: " default-directory)))
13433   (gnus-set-global-variables)
13434   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13435   (and (file-directory-p to)
13436        (setq to (concat (file-name-as-directory to)
13437                         (file-name-nondirectory file))))
13438   (copy-file file to))
13439
13440 ;; Summary kill commands.
13441
13442 (defun gnus-summary-edit-global-kill (article)
13443   "Edit the \"global\" kill file."
13444   (interactive (list (gnus-summary-article-number)))
13445   (gnus-set-global-variables)
13446   (gnus-group-edit-global-kill article))
13447
13448 (defun gnus-summary-edit-local-kill ()
13449   "Edit a local kill file applied to the current newsgroup."
13450   (interactive)
13451   (gnus-set-global-variables)
13452   (setq gnus-current-headers (gnus-summary-article-header))
13453   (gnus-set-global-variables)
13454   (gnus-group-edit-local-kill
13455    (gnus-summary-article-number) gnus-newsgroup-name))
13456
13457 \f
13458 ;;;
13459 ;;; Gnus article mode
13460 ;;;
13461
13462 (put 'gnus-article-mode 'mode-class 'special)
13463
13464 (if gnus-article-mode-map
13465     nil
13466   (setq gnus-article-mode-map (make-keymap))
13467   (suppress-keymap gnus-article-mode-map)
13468
13469   (gnus-define-keys gnus-article-mode-map
13470     " " gnus-article-goto-next-page
13471     "\177" gnus-article-goto-prev-page
13472     [delete] gnus-article-goto-prev-page
13473     "\C-c^" gnus-article-refer-article
13474     "h" gnus-article-show-summary
13475     "s" gnus-article-show-summary
13476     "\C-c\C-m" gnus-article-mail
13477     "?" gnus-article-describe-briefly
13478     gnus-mouse-2 gnus-article-push-button
13479     "\r" gnus-article-press-button
13480     "\t" gnus-article-next-button
13481     "\M-\t" gnus-article-prev-button
13482     "<" beginning-of-bnuffer
13483     ">" end-of-bnuffer
13484     "\C-c\C-b" gnus-bug)
13485
13486   (substitute-key-definition
13487    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13488
13489 (defun gnus-article-mode ()
13490   "Major mode for displaying an article.
13491
13492 All normal editing commands are switched off.
13493
13494 The following commands are available:
13495
13496 \\<gnus-article-mode-map>
13497 \\[gnus-article-next-page]\t Scroll the article one page forwards
13498 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13499 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13500 \\[gnus-article-show-summary]\t Display the summary buffer
13501 \\[gnus-article-mail]\t Send a reply to the address near point
13502 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13503 \\[gnus-info-find-node]\t Go to the Gnus info node"
13504   (interactive)
13505   (when (and menu-bar-mode
13506              (gnus-visual-p 'article-menu 'menu))
13507     (gnus-article-make-menu-bar))
13508   (kill-all-local-variables)
13509   (gnus-simplify-mode-line)
13510   (setq mode-name "Article")
13511   (setq major-mode 'gnus-article-mode)
13512   (make-local-variable 'minor-mode-alist)
13513   (or (assq 'gnus-show-mime minor-mode-alist)
13514       (setq minor-mode-alist
13515             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13516   (use-local-map gnus-article-mode-map)
13517   (make-local-variable 'page-delimiter)
13518   (setq page-delimiter gnus-page-delimiter)
13519   (buffer-disable-undo (current-buffer))
13520   (setq buffer-read-only t)             ;Disable modification
13521   (run-hooks 'gnus-article-mode-hook))
13522
13523 (defun gnus-article-setup-buffer ()
13524   "Initialize the article buffer."
13525   (let* ((name (if gnus-single-article-buffer "*Article*"
13526                  (concat "*Article " gnus-newsgroup-name "*")))
13527          (original
13528           (progn (string-match "\\*Article" name)
13529                  (concat " *Original Article"
13530                          (substring name (match-end 0))))))
13531     (setq gnus-article-buffer name)
13532     (setq gnus-original-article-buffer original)
13533     ;; This might be a variable local to the summary buffer.
13534     (unless gnus-single-article-buffer
13535       (save-excursion
13536         (set-buffer gnus-summary-buffer)
13537         (setq gnus-article-buffer name)
13538         (setq gnus-original-article-buffer original)
13539         (gnus-set-global-variables))
13540       (make-local-variable 'gnus-summary-buffer))
13541     ;; Init original article buffer.
13542     (save-excursion
13543       (set-buffer (get-buffer-create gnus-original-article-buffer))
13544       (buffer-disable-undo (current-buffer))
13545       (setq major-mode 'gnus-original-article-mode)
13546       (make-local-variable 'gnus-original-article))
13547     (if (get-buffer name)
13548         (save-excursion
13549           (set-buffer name)
13550           (buffer-disable-undo (current-buffer))
13551           (setq buffer-read-only t)
13552           (gnus-add-current-to-buffer-list)
13553           (or (eq major-mode 'gnus-article-mode)
13554               (gnus-article-mode))
13555           (current-buffer))
13556       (save-excursion
13557         (set-buffer (get-buffer-create name))
13558         (gnus-add-current-to-buffer-list)
13559         (gnus-article-mode)
13560         (current-buffer)))))
13561
13562 ;; Set article window start at LINE, where LINE is the number of lines
13563 ;; from the head of the article.
13564 (defun gnus-article-set-window-start (&optional line)
13565   (set-window-start
13566    (get-buffer-window gnus-article-buffer t)
13567    (save-excursion
13568      (set-buffer gnus-article-buffer)
13569      (goto-char (point-min))
13570      (if (not line)
13571          (point-min)
13572        (gnus-message 6 "Moved to bookmark")
13573        (search-forward "\n\n" nil t)
13574        (forward-line line)
13575        (point)))))
13576
13577 (defun gnus-kill-all-overlays ()
13578   "Delete all overlays in the current buffer."
13579   (when (fboundp 'overlay-lists)
13580     (let* ((overlayss (overlay-lists))
13581            (buffer-read-only nil)
13582            (overlays (nconc (car overlayss) (cdr overlayss))))
13583       (while overlays
13584         (delete-overlay (pop overlays))))))
13585
13586 (defun gnus-request-article-this-buffer (article group)
13587   "Get an article and insert it into this buffer."
13588   (let (do-update-line)
13589     (prog1
13590         (save-excursion
13591           (erase-buffer)
13592           (gnus-kill-all-overlays)
13593           (setq group (or group gnus-newsgroup-name))
13594
13595           ;; Open server if it has closed.
13596           (gnus-check-server (gnus-find-method-for-group group))
13597
13598           ;; Using `gnus-request-article' directly will insert the article into
13599           ;; `nntp-server-buffer' - so we'll save some time by not having to
13600           ;; copy it from the server buffer into the article buffer.
13601
13602           ;; We only request an article by message-id when we do not have the
13603           ;; headers for it, so we'll have to get those.
13604           (when (stringp article)
13605             (let ((gnus-override-method gnus-refer-article-method))
13606               (gnus-read-header article)))
13607
13608           ;; If the article number is negative, that means that this article
13609           ;; doesn't belong in this newsgroup (possibly), so we find its
13610           ;; message-id and request it by id instead of number.
13611           (when (and (numberp article)
13612                      gnus-summary-buffer
13613                      (get-buffer gnus-summary-buffer)
13614                      (buffer-name (get-buffer gnus-summary-buffer)))
13615             (save-excursion
13616               (set-buffer gnus-summary-buffer)
13617               (let ((header (gnus-summary-article-header article)))
13618                 (if (< article 0)
13619                     (cond 
13620                      ((memq article gnus-newsgroup-sparse)
13621                       ;; This is a sparse gap article.
13622                       (setq do-update-line article)
13623                       (setq article (mail-header-id header))
13624                       (let ((gnus-override-method gnus-refer-article-method))
13625                         (gnus-read-header article))
13626                       (setq gnus-newsgroup-sparse
13627                             (delq article gnus-newsgroup-sparse)))
13628                      ((vectorp header)
13629                       ;; It's a real article.
13630                       (setq article (mail-header-id header)))
13631                      (t
13632                       ;; It is an extracted pseudo-article.
13633                       (setq article 'pseudo)
13634                       (gnus-request-pseudo-article header))))
13635                 
13636                 (let ((method (gnus-find-method-for-group 
13637                                gnus-newsgroup-name)))
13638                   (if (not (eq (car method) 'nneething))
13639                       ()
13640                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13641                                        (mail-header-subject header))))
13642                       (if (file-directory-p dir)
13643                           (progn
13644                             (setq article 'nneething)
13645                             (gnus-group-enter-directory dir)))))))))
13646
13647           (cond
13648            ;; Refuse to select canceled articles.
13649            ((and (numberp article)
13650                  gnus-summary-buffer
13651                  (get-buffer gnus-summary-buffer)
13652                  (buffer-name (get-buffer gnus-summary-buffer))
13653                  (eq (cdr (save-excursion
13654                             (set-buffer gnus-summary-buffer)
13655                             (assq article gnus-newsgroup-reads)))
13656                      gnus-canceled-mark))
13657             nil)
13658            ;; We first check `gnus-original-article-buffer'.
13659            ((and (get-buffer gnus-original-article-buffer)
13660                  (numberp article)
13661                  (save-excursion
13662                    (set-buffer gnus-original-article-buffer)
13663                    (and (equal (car gnus-original-article) group)
13664                         (eq (cdr gnus-original-article) article))))
13665             (insert-buffer-substring gnus-original-article-buffer)
13666             'article)
13667            ;; Check the backlog.
13668            ((and gnus-keep-backlog
13669                  (gnus-backlog-request-article group article (current-buffer)))
13670             'article)
13671            ;; Check the cache.
13672            ((and gnus-use-cache
13673                  (numberp article)
13674                  (gnus-cache-request-article article group))
13675             'article)
13676            ;; Get the article and put into the article buffer.
13677            ((or (stringp article) (numberp article))
13678             (let ((gnus-override-method
13679                    (and (stringp article) gnus-refer-article-method))
13680                   (buffer-read-only nil))
13681               (erase-buffer)
13682               (gnus-kill-all-overlays)
13683               (if (gnus-request-article article group (current-buffer))
13684                   (progn
13685                     (and gnus-keep-backlog
13686                          (numberp article)
13687                          (gnus-backlog-enter-article
13688                           group article (current-buffer)))
13689                     'article))))
13690            ;; It was a pseudo.
13691            (t article)))
13692
13693       ;; Take the article from the original article buffer
13694       ;; and place it in the buffer it's supposed to be in.
13695       (when (and (get-buffer gnus-article-buffer)
13696                  ;;(numberp article)
13697                  (equal (buffer-name (current-buffer))
13698                         (buffer-name (get-buffer gnus-article-buffer))))
13699         (save-excursion
13700           (if (get-buffer gnus-original-article-buffer)
13701               (set-buffer (get-buffer gnus-original-article-buffer))
13702             (set-buffer (get-buffer-create gnus-original-article-buffer))
13703             (buffer-disable-undo (current-buffer))
13704             (setq major-mode 'gnus-original-article-mode)
13705             (setq buffer-read-only t)
13706             (gnus-add-current-to-buffer-list))
13707           (let (buffer-read-only)
13708             (erase-buffer)
13709             (insert-buffer-substring gnus-article-buffer))
13710           (setq gnus-original-article (cons group article))))
13711     
13712       ;; Update sparse articles.
13713       (when (and do-update-line
13714                  (or (numberp article)
13715                      (stringp article)))
13716         (let ((buf (current-buffer)))
13717           (set-buffer gnus-summary-buffer)
13718           (gnus-summary-update-article do-update-line)
13719           (gnus-summary-goto-subject do-update-line nil t)
13720           (set-window-point (get-buffer-window (current-buffer) t)
13721                             (point))
13722           (set-buffer buf))))))
13723
13724 (defun gnus-read-header (id &optional header)
13725   "Read the headers of article ID and enter them into the Gnus system."
13726   (let ((group gnus-newsgroup-name)
13727         (gnus-override-method 
13728          (and (gnus-news-group-p gnus-newsgroup-name)
13729               gnus-refer-article-method))       
13730         where)
13731     ;; First we check to see whether the header in question is already
13732     ;; fetched.
13733     (if (stringp id)
13734         ;; This is a Message-ID.
13735         (setq header (or header (gnus-id-to-header id)))
13736       ;; This is an article number.
13737       (setq header (or header (gnus-summary-article-header id))))
13738     (if (and header
13739              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13740         ;; We have found the header.
13741         header
13742       ;; We have to really fetch the header to this article.
13743       (when (setq where (gnus-request-head id group))
13744         (save-excursion
13745           (set-buffer nntp-server-buffer)
13746           (goto-char (point-max))
13747           (insert ".\n")
13748           (goto-char (point-min))
13749           (insert "211 ")
13750           (princ (cond
13751                   ((numberp id) id)
13752                   ((cdr where) (cdr where))
13753                   (header (mail-header-number header))
13754                   (t gnus-reffed-article-number))
13755                  (current-buffer))
13756           (insert " Article retrieved.\n"))
13757         ;(when (and header
13758         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13759         ;  (setcar (gnus-id-to-thread id) nil))
13760         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13761             ()                          ; Malformed head.
13762           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13763             (if (and (stringp id)
13764                      (not (string= (gnus-group-real-name group)
13765                                    (car where))))
13766                 ;; If we fetched by Message-ID and the article came
13767                 ;; from a different group, we fudge some bogus article
13768                 ;; numbers for this article.
13769                 (mail-header-set-number header gnus-reffed-article-number))
13770             (decf gnus-reffed-article-number)
13771             (gnus-remove-header (mail-header-number header))
13772             (push header gnus-newsgroup-headers)
13773             (setq gnus-current-headers header)
13774             (push (mail-header-number header) gnus-newsgroup-limit))
13775           header)))))
13776
13777 (defun gnus-remove-header (number)
13778   "Remove header NUMBER from `gnus-newsgroup-headers'."
13779   (if (and gnus-newsgroup-headers
13780            (= number (mail-header-number (car gnus-newsgroup-headers))))
13781       (pop gnus-newsgroup-headers)
13782     (let ((headers gnus-newsgroup-headers))
13783       (while (and (cdr headers)
13784                   (not (= number (mail-header-number (cadr headers)))))
13785         (pop headers))
13786       (when (cdr headers)
13787         (setcdr headers (cddr headers))))))
13788
13789 (defun gnus-article-prepare (article &optional all-headers header)
13790   "Prepare ARTICLE in article mode buffer.
13791 ARTICLE should either be an article number or a Message-ID.
13792 If ARTICLE is an id, HEADER should be the article headers.
13793 If ALL-HEADERS is non-nil, no headers are hidden."
13794   (save-excursion
13795     ;; Make sure we start in a summary buffer.
13796     (unless (eq major-mode 'gnus-summary-mode)
13797       (set-buffer gnus-summary-buffer))
13798     (setq gnus-summary-buffer (current-buffer))
13799     ;; Make sure the connection to the server is alive.
13800     (unless (gnus-server-opened
13801              (gnus-find-method-for-group gnus-newsgroup-name))
13802       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13803       (gnus-request-group gnus-newsgroup-name t))
13804     (let* ((article (if header (mail-header-number header) article))
13805            (summary-buffer (current-buffer))
13806            (internal-hook gnus-article-internal-prepare-hook)
13807            (group gnus-newsgroup-name)
13808            result)
13809       (save-excursion
13810         (gnus-article-setup-buffer)
13811         (set-buffer gnus-article-buffer)
13812         ;; Deactivate active regions.
13813         (when (and (boundp 'transient-mark-mode)
13814                    transient-mark-mode)
13815           (setq mark-active nil))
13816         (if (not (setq result (let ((buffer-read-only nil))
13817                                 (gnus-request-article-this-buffer
13818                                  article group))))
13819             ;; There is no such article.
13820             (save-excursion
13821               (when (and (numberp article)
13822                          (not (memq article gnus-newsgroup-sparse)))
13823                 (setq gnus-article-current
13824                       (cons gnus-newsgroup-name article))
13825                 (set-buffer gnus-summary-buffer)
13826                 (setq gnus-current-article article)
13827                 (gnus-summary-mark-article article gnus-canceled-mark))
13828               (unless (memq article gnus-newsgroup-sparse)
13829                 (gnus-error
13830                  1 "No such article (may have expired or been canceled)")))
13831           (if (or (eq result 'pseudo) (eq result 'nneething))
13832               (progn
13833                 (save-excursion
13834                   (set-buffer summary-buffer)
13835                   (setq gnus-last-article gnus-current-article
13836                         gnus-newsgroup-history (cons gnus-current-article
13837                                                      gnus-newsgroup-history)
13838                         gnus-current-article 0
13839                         gnus-current-headers nil
13840                         gnus-article-current nil)
13841                   (if (eq result 'nneething)
13842                       (gnus-configure-windows 'summary)
13843                     (gnus-configure-windows 'article))
13844                   (gnus-set-global-variables))
13845                 (gnus-set-mode-line 'article))
13846             ;; The result from the `request' was an actual article -
13847             ;; or at least some text that is now displayed in the
13848             ;; article buffer.
13849             (if (and (numberp article)
13850                      (not (eq article gnus-current-article)))
13851                 ;; Seems like a new article has been selected.
13852                 ;; `gnus-current-article' must be an article number.
13853                 (save-excursion
13854                   (set-buffer summary-buffer)
13855                   (setq gnus-last-article gnus-current-article
13856                         gnus-newsgroup-history (cons gnus-current-article
13857                                                      gnus-newsgroup-history)
13858                         gnus-current-article article
13859                         gnus-current-headers
13860                         (gnus-summary-article-header gnus-current-article)
13861                         gnus-article-current
13862                         (cons gnus-newsgroup-name gnus-current-article))
13863                   (unless (vectorp gnus-current-headers)
13864                     (setq gnus-current-headers nil))
13865                   (gnus-summary-show-thread)
13866                   (run-hooks 'gnus-mark-article-hook)
13867                   (gnus-set-mode-line 'summary)
13868                   (and (gnus-visual-p 'article-highlight 'highlight)
13869                        (run-hooks 'gnus-visual-mark-article-hook))
13870                   ;; Set the global newsgroup variables here.
13871                   ;; Suggested by Jim Sisolak
13872                   ;; <sisolak@trans4.neep.wisc.edu>.
13873                   (gnus-set-global-variables)
13874                   (setq gnus-have-all-headers
13875                         (or all-headers gnus-show-all-headers))
13876                   (and gnus-use-cache
13877                        (vectorp (gnus-summary-article-header article))
13878                        (gnus-cache-possibly-enter-article
13879                         group article
13880                         (gnus-summary-article-header article)
13881                         (memq article gnus-newsgroup-marked)
13882                         (memq article gnus-newsgroup-dormant)
13883                         (memq article gnus-newsgroup-unreads)))))
13884             (when (or (numberp article)
13885                       (stringp article))
13886               ;; Hooks for getting information from the article.
13887               ;; This hook must be called before being narrowed.
13888               (let (buffer-read-only)
13889                 (run-hooks 'internal-hook)
13890                 (run-hooks 'gnus-article-prepare-hook)
13891                 ;; Decode MIME message.
13892                 (if gnus-show-mime
13893                     (if (or (not gnus-strict-mime)
13894                             (gnus-fetch-field "Mime-Version"))
13895                         (funcall gnus-show-mime-method)
13896                       (funcall gnus-decode-encoded-word-method)))
13897                 ;; Perform the article display hooks.
13898                 (run-hooks 'gnus-article-display-hook))
13899               ;; Do page break.
13900               (goto-char (point-min))
13901               (and gnus-break-pages (gnus-narrow-to-page)))
13902             (gnus-set-mode-line 'article)
13903             (gnus-configure-windows 'article)
13904             (goto-char (point-min))
13905             t))))))
13906
13907 (defun gnus-article-show-all-headers ()
13908   "Show all article headers in article mode buffer."
13909   (save-excursion
13910     (gnus-article-setup-buffer)
13911     (set-buffer gnus-article-buffer)
13912     (let ((buffer-read-only nil))
13913       (gnus-unhide-text (point-min) (point-max)))))
13914
13915 (defun gnus-article-hide-headers-if-wanted ()
13916   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13917 Provided for backwards compatibility."
13918   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13919       gnus-inhibit-hiding
13920       (gnus-article-hide-headers)))
13921
13922 (defsubst gnus-article-header-rank ()
13923   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13924   (let ((list gnus-sorted-header-list)
13925         (i 0))
13926     (while list
13927       (when (looking-at (car list))
13928         (setq list nil))
13929       (setq list (cdr list))
13930       (incf i))
13931     i))
13932
13933 (defun gnus-article-hide-headers (&optional arg delete)
13934   "Toggle whether to hide unwanted headers and possibly sort them as well.
13935 If given a negative prefix, always show; if given a positive prefix,
13936 always hide."
13937   (interactive (gnus-hidden-arg))
13938   (if (gnus-article-check-hidden-text 'headers arg)
13939       ;; Show boring headers as well.
13940       (gnus-article-show-hidden-text 'boring-headers)
13941     ;; This function might be inhibited.
13942     (unless gnus-inhibit-hiding
13943       (save-excursion
13944         (set-buffer gnus-article-buffer)
13945         (save-restriction
13946           (let ((buffer-read-only nil)
13947                 (props (nconc (list 'gnus-type 'headers)
13948                               gnus-hidden-properties))
13949                 (max (1+ (length gnus-sorted-header-list)))
13950                 (ignored (when (not (stringp gnus-visible-headers))
13951                            (cond ((stringp gnus-ignored-headers)
13952                                   gnus-ignored-headers)
13953                                  ((listp gnus-ignored-headers)
13954                                   (mapconcat 'identity gnus-ignored-headers
13955                                              "\\|")))))
13956                 (visible
13957                  (cond ((stringp gnus-visible-headers)
13958                         gnus-visible-headers)
13959                        ((and gnus-visible-headers
13960                              (listp gnus-visible-headers))
13961                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13962                 (inhibit-point-motion-hooks t)
13963                 want-list beg)
13964             ;; First we narrow to just the headers.
13965             (widen)
13966             (goto-char (point-min))
13967             ;; Hide any "From " lines at the beginning of (mail) articles.
13968             (while (looking-at "From ")
13969               (forward-line 1))
13970             (unless (bobp)
13971               (if delete
13972                   (delete-region (point-min) (point))
13973                 (gnus-hide-text (point-min) (point) props)))
13974             ;; Then treat the rest of the header lines.
13975             (narrow-to-region
13976              (point)
13977              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13978             ;; Then we use the two regular expressions
13979             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13980             ;; select which header lines is to remain visible in the
13981             ;; article buffer.
13982             (goto-char (point-min))
13983             (while (re-search-forward "^[^ \t]*:" nil t)
13984               (beginning-of-line)
13985               ;; We add the headers we want to keep to a list and delete
13986               ;; them from the buffer.
13987               (gnus-put-text-property 
13988                (point) (1+ (point)) 'message-rank
13989                (if (or (and visible (looking-at visible))
13990                        (and ignored
13991                             (not (looking-at ignored))))
13992                    (gnus-article-header-rank) 
13993                  (+ 2 max)))
13994               (forward-line 1))
13995             (message-sort-headers-1)
13996             (when (setq beg (text-property-any 
13997                              (point-min) (point-max) 'message-rank (+ 2 max)))
13998               ;; We make the unwanted headers invisible.
13999               (if delete
14000                   (delete-region beg (point-max))
14001                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
14002                 (gnus-hide-text-type beg (point-max) 'headers))
14003               ;; Work around XEmacs lossage.
14004               (gnus-put-text-property (point-min) beg 'invisible nil))))))))
14005
14006 (defun gnus-article-hide-boring-headers (&optional arg)
14007   "Toggle hiding of headers that aren't very interesting.
14008 If given a negative prefix, always show; if given a positive prefix,
14009 always hide."
14010   (interactive (gnus-hidden-arg))
14011   (unless (gnus-article-check-hidden-text 'boring-headers arg)
14012     (save-excursion
14013       (set-buffer gnus-article-buffer)
14014       (save-restriction
14015         (let ((buffer-read-only nil)
14016               (list gnus-boring-article-headers)
14017               (inhibit-point-motion-hooks t)
14018               elem)
14019           (nnheader-narrow-to-headers)
14020           (while list
14021             (setq elem (pop list))
14022             (goto-char (point-min))
14023             (cond
14024              ;; Hide empty headers.
14025              ((eq elem 'empty)
14026               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
14027                 (forward-line -1)
14028                 (gnus-hide-text-type
14029                  (progn (beginning-of-line) (point))
14030                  (progn 
14031                    (end-of-line)
14032                    (if (re-search-forward "^[^ \t]" nil t)
14033                        (match-beginning 0)
14034                      (point-max)))
14035                  'boring-headers)))
14036              ;; Hide boring Newsgroups header.
14037              ((eq elem 'newsgroups)
14038               (when (equal (message-fetch-field "newsgroups")
14039                            (gnus-group-real-name gnus-newsgroup-name))
14040                 (gnus-article-hide-header "newsgroups")))
14041              ((eq elem 'followup-to)
14042               (when (equal (message-fetch-field "followup-to")
14043                            (message-fetch-field "newsgroups"))
14044                 (gnus-article-hide-header "followup-to")))
14045              ((eq elem 'reply-to)
14046               (let ((from (message-fetch-field "from"))
14047                     (reply-to (message-fetch-field "reply-to")))
14048                 (when (and
14049                        from reply-to
14050                        (equal 
14051                         (nth 1 (funcall gnus-extract-address-components from))
14052                         (nth 1 (funcall gnus-extract-address-components
14053                                         reply-to))))
14054                   (gnus-article-hide-header "reply-to"))))
14055              ((eq elem 'date)
14056               (let ((date (message-fetch-field "date")))
14057                 (when (and date
14058                            (< (gnus-days-between date (current-time-string))
14059                               4))
14060                   (gnus-article-hide-header "date")))))))))))
14061
14062 (defun gnus-article-hide-header (header)
14063   (save-excursion
14064     (goto-char (point-min))
14065     (when (re-search-forward (concat "^" header ":") nil t)
14066       (gnus-hide-text-type
14067        (progn (beginning-of-line) (point))
14068        (progn 
14069          (end-of-line)
14070          (if (re-search-forward "^[^ \t]" nil t)
14071              (match-beginning 0)
14072            (point-max)))
14073        'boring-headers))))
14074
14075 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
14076 (defun gnus-article-treat-overstrike ()
14077   "Translate overstrikes into bold text."
14078   (interactive)
14079   (save-excursion
14080     (set-buffer gnus-article-buffer)
14081     (let ((buffer-read-only nil))
14082       (while (search-forward "\b" nil t)
14083         (let ((next (following-char))
14084               (previous (char-after (- (point) 2))))
14085           (cond 
14086            ((eq next previous)
14087             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14088             (gnus-put-text-property (point) (1+ (point)) 'face 'bold))
14089            ((eq next ?_)
14090             (gnus-put-text-property (1- (point)) (1+ (point)) 'invisible t)
14091             (gnus-put-text-property
14092              (- (point) 2) (1- (point)) 'face 'underline))
14093            ((eq previous ?_)
14094             (gnus-put-text-property (- (point) 2) (point) 'invisible t)
14095             (gnus-put-text-property
14096              (point) (1+ (point))       'face 'underline))))))))
14097
14098 (defun gnus-article-word-wrap ()
14099   "Format too long lines."
14100   (interactive)
14101   (save-excursion
14102     (set-buffer gnus-article-buffer)
14103     (let ((buffer-read-only nil))
14104       (widen)
14105       (goto-char (point-min))
14106       (search-forward "\n\n" nil t)
14107       (end-of-line 1)
14108       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
14109             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
14110             (adaptive-fill-mode t))
14111         (while (not (eobp))
14112           (and (>= (current-column) (min fill-column (window-width)))
14113                (/= (preceding-char) ?:)
14114                (fill-paragraph nil))
14115           (end-of-line 2))))))
14116
14117 (defun gnus-article-remove-cr ()
14118   "Remove carriage returns from an article."
14119   (interactive)
14120   (save-excursion
14121     (set-buffer gnus-article-buffer)
14122     (let ((buffer-read-only nil))
14123       (goto-char (point-min))
14124       (while (search-forward "\r" nil t)
14125         (replace-match "" t t)))))
14126
14127 (defun gnus-article-remove-trailing-blank-lines ()
14128   "Remove all trailing blank lines from the article."
14129   (interactive)
14130   (save-excursion
14131     (set-buffer gnus-article-buffer)
14132     (let ((buffer-read-only nil))
14133       (goto-char (point-max))
14134       (delete-region
14135        (point)
14136        (progn
14137          (while (looking-at "^[ \t]*$")
14138            (forward-line -1))
14139          (forward-line 1)
14140          (point))))))
14141
14142 (defun gnus-article-display-x-face (&optional force)
14143   "Look for an X-Face header and display it if present."
14144   (interactive (list 'force))
14145   (save-excursion
14146     (set-buffer gnus-article-buffer)
14147     ;; Delete the old process, if any.
14148     (when (process-status "gnus-x-face")
14149       (delete-process "gnus-x-face"))
14150     (let ((inhibit-point-motion-hooks t)
14151           (case-fold-search nil)
14152           from)
14153       (save-restriction
14154         (nnheader-narrow-to-headers)
14155         (setq from (message-fetch-field "from"))
14156         (goto-char (point-min))
14157         (when (and gnus-article-x-face-command
14158                    (or force
14159                        ;; Check whether this face is censored.
14160                        (not gnus-article-x-face-too-ugly)
14161                        (and gnus-article-x-face-too-ugly from
14162                             (not (string-match gnus-article-x-face-too-ugly
14163                                                from))))
14164                    ;; Has to be present.
14165                    (re-search-forward "^X-Face: " nil t))
14166           ;; We now have the area of the buffer where the X-Face is stored.
14167           (let ((beg (point))
14168                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
14169             ;; We display the face.
14170             (if (symbolp gnus-article-x-face-command)
14171                 ;; The command is a lisp function, so we call it.
14172                 (if (gnus-functionp gnus-article-x-face-command)
14173                     (funcall gnus-article-x-face-command beg end)
14174                   (error "%s is not a function" gnus-article-x-face-command))
14175               ;; The command is a string, so we interpret the command
14176               ;; as a, well, command, and fork it off.
14177               (let ((process-connection-type nil))
14178                 (process-kill-without-query
14179                  (start-process
14180                   "gnus-x-face" nil shell-file-name shell-command-switch
14181                   gnus-article-x-face-command))
14182                 (process-send-region "gnus-x-face" beg end)
14183                 (process-send-eof "gnus-x-face")))))))))
14184
14185 (defalias 'gnus-headers-decode-quoted-printable 'gnus-decode-rfc1522)
14186 (defun gnus-decode-rfc1522 ()
14187   "Hack to remove QP encoding from headers."
14188   (let ((case-fold-search t)
14189         (inhibit-point-motion-hooks t)
14190         (buffer-read-only nil)
14191         string)
14192     (save-restriction
14193       (narrow-to-region
14194        (goto-char (point-min))
14195        (or (search-forward "\n\n" nil t) (point-max)))
14196
14197       (while (re-search-forward 
14198               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
14199         (setq string (match-string 1))
14200         (narrow-to-region (match-beginning 0) (match-end 0))
14201         (delete-region (point-min) (point-max))
14202         (insert string)
14203         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
14204         (subst-char-in-region (point-min) (point-max) ?_ ? )
14205         (widen)
14206         (goto-char (point-min))))))
14207
14208 (defun gnus-article-de-quoted-unreadable (&optional force)
14209   "Do a naive translation of a quoted-printable-encoded article.
14210 This is in no way, shape or form meant as a replacement for real MIME
14211 processing, but is simply a stop-gap measure until MIME support is
14212 written.
14213 If FORCE, decode the article whether it is marked as quoted-printable
14214 or not."
14215   (interactive (list 'force))
14216   (save-excursion
14217     (set-buffer gnus-article-buffer)
14218     (let ((case-fold-search t)
14219           (buffer-read-only nil)
14220           (type (gnus-fetch-field "content-transfer-encoding")))
14221       (gnus-decode-rfc1522)
14222       (when (or force
14223                 (and type (string-match "quoted-printable" (downcase type))))
14224         (goto-char (point-min))
14225         (search-forward "\n\n" nil 'move)
14226         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14227
14228 (defun gnus-mime-decode-quoted-printable (from to)
14229   "Decode Quoted-Printable in the region between FROM and TO."
14230   (interactive "r")
14231   (goto-char from)
14232   (while (search-forward "=" to t)
14233     (cond ((eq (following-char) ?\n)
14234            (delete-char -1)
14235            (delete-char 1))
14236           ((looking-at "[0-9A-F][0-9A-F]")
14237            (subst-char-in-region
14238             (1- (point)) (point) ?=
14239             (hexl-hex-string-to-integer
14240              (buffer-substring (point) (+ 2 (point)))))
14241            (delete-char 2))
14242           ((looking-at "=")
14243            (delete-char 1))
14244           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14245
14246 (defun gnus-article-hide-pgp (&optional arg)
14247   "Toggle hiding of any PGP headers and signatures in the current article.
14248 If given a negative prefix, always show; if given a positive prefix,
14249 always hide."
14250   (interactive (gnus-hidden-arg))
14251   (unless (gnus-article-check-hidden-text 'pgp arg)
14252     (save-excursion
14253       (set-buffer gnus-article-buffer)
14254       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14255             buffer-read-only beg end)
14256         (widen)
14257         (goto-char (point-min))
14258         ;; Hide the "header".
14259         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14260              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14261         (setq beg (point))
14262         ;; Hide the actual signature.
14263         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14264              (setq end (1+ (match-beginning 0)))
14265              (gnus-hide-text
14266               end
14267               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14268                   (match-end 0)
14269                 ;; Perhaps we shouldn't hide to the end of the buffer
14270                 ;; if there is no end to the signature?
14271                 (point-max))
14272               props))
14273         ;; Hide "- " PGP quotation markers.
14274         (when (and beg end)
14275           (narrow-to-region beg end)
14276           (goto-char (point-min))
14277           (while (re-search-forward "^- " nil t)
14278             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14279           (widen))))))
14280
14281 (defun gnus-article-hide-signature (&optional arg)
14282   "Hide the signature in the current article.
14283 If given a negative prefix, always show; if given a positive prefix,
14284 always hide."
14285   (interactive (gnus-hidden-arg))
14286   (unless (gnus-article-check-hidden-text 'signature arg)
14287     (save-excursion
14288       (set-buffer gnus-article-buffer)
14289       (save-restriction
14290         (let ((buffer-read-only nil))
14291           (when (gnus-narrow-to-signature)
14292             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14293
14294 (defun gnus-article-strip-leading-blank-lines ()
14295   "Remove all blank lines from the beginning of the article."
14296   (interactive)
14297   (save-excursion
14298     (set-buffer gnus-article-buffer)
14299     (let (buffer-read-only)
14300       (goto-char (point-min))
14301       (when (search-forward "\n\n" nil t)
14302         (while (looking-at "[ \t]$")
14303           (gnus-delete-line))))))
14304
14305 (defvar mime::preview/content-list)
14306 (defvar mime::preview-content-info/point-min)
14307 (defun gnus-narrow-to-signature ()
14308   "Narrow to the signature."
14309   (widen)
14310   (if (and (boundp 'mime::preview/content-list)
14311            mime::preview/content-list)
14312       (let ((pcinfo (car (last mime::preview/content-list))))
14313         (condition-case ()
14314             (narrow-to-region
14315              (funcall (intern "mime::preview-content-info/point-min") pcinfo)
14316              (point-max))
14317           (error nil))))
14318   (goto-char (point-max))
14319   (when (re-search-backward gnus-signature-separator nil t)
14320     (forward-line 1)
14321     (when (or (null gnus-signature-limit)
14322               (and (numberp gnus-signature-limit)
14323                    (< (- (point-max) (point)) gnus-signature-limit))
14324               (and (gnus-functionp gnus-signature-limit)
14325                    (funcall gnus-signature-limit))
14326               (and (stringp gnus-signature-limit)
14327                    (not (re-search-forward gnus-signature-limit nil t))))
14328       (narrow-to-region (point) (point-max))
14329       t)))
14330
14331 (defun gnus-hidden-arg ()
14332   "Return the current prefix arg as a number, or 0 if no prefix."
14333   (list (if current-prefix-arg
14334             (prefix-numeric-value current-prefix-arg)
14335           0)))
14336
14337 (defun gnus-article-check-hidden-text (type arg)
14338   "Return nil if hiding is necessary.
14339 Arg can be nil or a number.  Nil and positive means hide, negative
14340 means show, 0 means toggle."
14341   (save-excursion
14342     (set-buffer gnus-article-buffer)
14343     (let ((hide (gnus-article-hidden-text-p type)))
14344       (cond
14345        ((or (null arg)
14346             (> arg 0))
14347         nil)
14348        ((< arg 0)
14349         (gnus-article-show-hidden-text type))
14350        (t
14351         (if (eq hide 'hidden)
14352             (gnus-article-show-hidden-text type)
14353           nil))))))
14354
14355 (defun gnus-article-hidden-text-p (type)
14356   "Say whether the current buffer contains hidden text of type TYPE."
14357   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14358     (when pos
14359       (if (get-text-property pos 'invisible)
14360           'hidden
14361         'shown))))
14362
14363 (defun gnus-article-hide (&optional arg force)
14364   "Hide all the gruft in the current article.
14365 This means that PGP stuff, signatures, cited text and (some)
14366 headers will be hidden.
14367 If given a prefix, show the hidden text instead."
14368   (interactive (list current-prefix-arg 'force))
14369   (gnus-article-hide-headers arg)
14370   (gnus-article-hide-pgp arg)
14371   (gnus-article-hide-citation-maybe arg force)
14372   (gnus-article-hide-signature arg))
14373
14374 (defun gnus-article-show-hidden-text (type &optional hide)
14375   "Show all hidden text of type TYPE.
14376 If HIDE, hide the text instead."
14377   (save-excursion
14378     (set-buffer gnus-article-buffer)
14379     (let ((buffer-read-only nil)
14380           (inhibit-point-motion-hooks t)
14381           (beg (point-min)))
14382       (while (gnus-goto-char (text-property-any
14383                               beg (point-max) 'gnus-type type))
14384         (setq beg (point))
14385         (forward-char)
14386         (if hide
14387             (gnus-hide-text beg (point) gnus-hidden-properties)
14388           (gnus-unhide-text beg (point)))
14389         (setq beg (point)))
14390       t)))
14391
14392 (defvar gnus-article-time-units
14393   `((year . ,(* 365.25 24 60 60))
14394     (week . ,(* 7 24 60 60))
14395     (day . ,(* 24 60 60))
14396     (hour . ,(* 60 60))
14397     (minute . 60)
14398     (second . 1))
14399   "Mapping from time units to seconds.")
14400
14401 (defun gnus-article-date-ut (&optional type highlight)
14402   "Convert DATE date to universal time in the current article.
14403 If TYPE is `local', convert to local time; if it is `lapsed', output
14404 how much time has lapsed since DATE."
14405   (interactive (list 'ut t))
14406   (let* ((header (or gnus-current-headers
14407                      (gnus-summary-article-header) ""))
14408          (date (and (vectorp header) (mail-header-date header)))
14409          (date-regexp "^Date: \\|^X-Sent: ")
14410          (now (current-time))
14411          (inhibit-point-motion-hooks t)
14412          bface eface)
14413     (when (and date (not (string= date "")))
14414       (save-excursion
14415         (set-buffer gnus-article-buffer)
14416         (save-restriction
14417           (nnheader-narrow-to-headers)
14418           (let ((buffer-read-only nil))
14419             ;; Delete any old Date headers.
14420             (if (re-search-forward date-regexp nil t)
14421                 (progn
14422                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14423                         eface (get-text-property (1- (gnus-point-at-eol))
14424                                                  'face))
14425                   (message-remove-header date-regexp t)
14426                   (beginning-of-line))
14427               (goto-char (point-max)))
14428             (insert (gnus-make-date-line date type))
14429             ;; Do highlighting.
14430             (forward-line -1)
14431             (when (and (gnus-visual-p 'article-highlight 'highlight)
14432                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14433               (gnus-put-text-property (match-beginning 1) (match-end 1)
14434                                  'face bface)
14435               (gnus-put-text-property (match-beginning 2) (match-end 2)
14436                                  'face eface))))))))
14437
14438 (defun gnus-make-date-line (date type)
14439   "Return a DATE line of TYPE."
14440   (cond
14441    ;; Convert to the local timezone.  We have to slap a
14442    ;; `condition-case' round the calls to the timezone
14443    ;; functions since they aren't particularly resistant to
14444    ;; buggy dates.
14445    ((eq type 'local)
14446     (concat "Date: " (condition-case ()
14447                          (timezone-make-date-arpa-standard date)
14448                        (error date))
14449             "\n"))
14450    ;; Convert to Universal Time.
14451    ((eq type 'ut)
14452     (concat "Date: "
14453             (condition-case ()
14454                 (timezone-make-date-arpa-standard date nil "UT")
14455               (error date))
14456             "\n"))
14457    ;; Get the original date from the article.
14458    ((eq type 'original)
14459     (concat "Date: " date "\n"))
14460    ;; Do an X-Sent lapsed format.
14461    ((eq type 'lapsed)
14462     ;; If the date is seriously mangled, the timezone
14463     ;; functions are liable to bug out, so we condition-case
14464     ;; the entire thing.
14465     (let* ((now (current-time))
14466            (real-time
14467             (condition-case ()
14468                 (gnus-time-minus
14469                  (gnus-encode-date
14470                   (timezone-make-date-arpa-standard
14471                    (current-time-string now)
14472                    (current-time-zone now) "UT"))
14473                  (gnus-encode-date
14474                   (timezone-make-date-arpa-standard
14475                    date nil "UT")))
14476               (error '(0 0))))
14477            (real-sec (+ (* (float (car real-time)) 65536)
14478                         (cadr real-time)))
14479            (sec (abs real-sec))
14480            num prev)
14481       (cond
14482        ((equal real-time '(0 0))
14483         "X-Sent: Unknown\n")
14484        ((zerop sec)
14485         "X-Sent: Now\n")
14486        (t
14487         (concat
14488          "X-Sent: "
14489          ;; This is a bit convoluted, but basically we go
14490          ;; through the time units for years, weeks, etc,
14491          ;; and divide things to see whether that results
14492          ;; in positive answers.
14493          (mapconcat
14494           (lambda (unit)
14495             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14496                 ;; The (remaining) seconds are too few to
14497                 ;; be divided into this time unit.
14498                 ""
14499               ;; It's big enough, so we output it.
14500               (setq sec (- sec (* num (cdr unit))))
14501               (prog1
14502                   (concat (if prev ", " "") (int-to-string
14503                                              (floor num))
14504                           " " (symbol-name (car unit)) 
14505                           (if (> num 1) "s" ""))
14506                 (setq prev t))))
14507           gnus-article-time-units "")
14508          ;; If dates are odd, then it might appear like the
14509          ;; article was sent in the future.
14510          (if (> real-sec 0)
14511              " ago\n"
14512            " in the future\n"))))))
14513    (t
14514     (error "Unknown conversion type: %s" type))))
14515
14516 (defun gnus-article-date-local (&optional highlight)
14517   "Convert the current article date to the local timezone."
14518   (interactive (list t))
14519   (gnus-article-date-ut 'local highlight))
14520
14521 (defun gnus-article-date-original (&optional highlight)
14522   "Convert the current article date to what it was originally.
14523 This is only useful if you have used some other date conversion
14524 function and want to see what the date was before converting."
14525   (interactive (list t))
14526   (gnus-article-date-ut 'original highlight))
14527
14528 (defun gnus-article-date-lapsed (&optional highlight)
14529   "Convert the current article date to time lapsed since it was sent."
14530   (interactive (list t))
14531   (gnus-article-date-ut 'lapsed highlight))
14532
14533 (defun gnus-article-maybe-highlight ()
14534   "Do some article highlighting if `gnus-visual' is non-nil."
14535   (if (gnus-visual-p 'article-highlight 'highlight)
14536       (gnus-article-highlight-some)))
14537
14538 ;;; Article savers.
14539
14540 (defun gnus-output-to-rmail (file-name)
14541   "Append the current article to an Rmail file named FILE-NAME."
14542   (require 'rmail)
14543   ;; Most of these codes are borrowed from rmailout.el.
14544   (setq file-name (expand-file-name file-name))
14545   (setq rmail-default-rmail-file file-name)
14546   (let ((artbuf (current-buffer))
14547         (tmpbuf (get-buffer-create " *Gnus-output*")))
14548     (save-excursion
14549       (or (get-file-buffer file-name)
14550           (file-exists-p file-name)
14551           (if (gnus-yes-or-no-p
14552                (concat "\"" file-name "\" does not exist, create it? "))
14553               (let ((file-buffer (create-file-buffer file-name)))
14554                 (save-excursion
14555                   (set-buffer file-buffer)
14556                   (rmail-insert-rmail-file-header)
14557                   (let ((require-final-newline nil))
14558                     (write-region (point-min) (point-max) file-name t 1)))
14559                 (kill-buffer file-buffer))
14560             (error "Output file does not exist")))
14561       (set-buffer tmpbuf)
14562       (buffer-disable-undo (current-buffer))
14563       (erase-buffer)
14564       (insert-buffer-substring artbuf)
14565       (gnus-convert-article-to-rmail)
14566       ;; Decide whether to append to a file or to an Emacs buffer.
14567       (let ((outbuf (get-file-buffer file-name)))
14568         (if (not outbuf)
14569             (append-to-file (point-min) (point-max) file-name)
14570           ;; File has been visited, in buffer OUTBUF.
14571           (set-buffer outbuf)
14572           (let ((buffer-read-only nil)
14573                 (msg (and (boundp 'rmail-current-message)
14574                           (symbol-value 'rmail-current-message))))
14575             ;; If MSG is non-nil, buffer is in RMAIL mode.
14576             (if msg
14577                 (progn (widen)
14578                        (narrow-to-region (point-max) (point-max))))
14579             (insert-buffer-substring tmpbuf)
14580             (if msg
14581                 (progn
14582                   (goto-char (point-min))
14583                   (widen)
14584                   (search-backward "\^_")
14585                   (narrow-to-region (point) (point-max))
14586                   (goto-char (1+ (point-min)))
14587                   (rmail-count-new-messages t)
14588                   (rmail-show-message msg)))))))
14589     (kill-buffer tmpbuf)))
14590
14591 (defun gnus-output-to-file (file-name)
14592   "Append the current article to a file named FILE-NAME."
14593   (let ((artbuf (current-buffer)))
14594     (nnheader-temp-write nil
14595       (insert-buffer-substring artbuf)
14596       ;; Append newline at end of the buffer as separator, and then
14597       ;; save it to file.
14598       (goto-char (point-max))
14599       (insert "\n")
14600       (append-to-file (point-min) (point-max) file-name))))
14601
14602 (defun gnus-convert-article-to-rmail ()
14603   "Convert article in current buffer to Rmail message format."
14604   (let ((buffer-read-only nil))
14605     ;; Convert article directly into Babyl format.
14606     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14607     (goto-char (point-min))
14608     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14609     (while (search-forward "\n\^_" nil t) ;single char
14610       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14611     (goto-char (point-max))
14612     (insert "\^_")))
14613
14614 (defun gnus-narrow-to-page (&optional arg)
14615   "Narrow the article buffer to a page.
14616 If given a numerical ARG, move forward ARG pages."
14617   (interactive "P")
14618   (setq arg (if arg (prefix-numeric-value arg) 0))
14619   (save-excursion
14620     (set-buffer gnus-article-buffer)
14621     (goto-char (point-min))
14622     (widen)
14623     (when (gnus-visual-p 'page-marker)
14624       (let ((buffer-read-only nil))
14625         (gnus-remove-text-with-property 'gnus-prev)
14626         (gnus-remove-text-with-property 'gnus-next)))
14627     (when
14628         (cond ((< arg 0)
14629                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14630               ((> arg 0)
14631                (re-search-forward page-delimiter nil 'move arg)))
14632       (goto-char (match-end 0)))
14633     (narrow-to-region
14634      (point)
14635      (if (re-search-forward page-delimiter nil 'move)
14636          (match-beginning 0)
14637        (point)))
14638     (when (and (gnus-visual-p 'page-marker)
14639                (not (= (point-min) 1)))
14640       (save-excursion
14641         (goto-char (point-min))
14642         (gnus-insert-prev-page-button)))
14643     (when (and (gnus-visual-p 'page-marker)
14644                (not (= (1- (point-max)) (buffer-size))))
14645       (save-excursion
14646         (goto-char (point-max))
14647         (gnus-insert-next-page-button)))))
14648
14649 ;; Article mode commands
14650
14651 (defun gnus-article-goto-next-page ()
14652   "Show the next page of the article."
14653   (interactive)
14654   (when (gnus-article-next-page)
14655     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14656
14657 (defun gnus-article-goto-prev-page ()
14658   "Show the next page of the article."
14659   (interactive)
14660   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14661     (gnus-article-prev-page nil)))
14662
14663 (defun gnus-article-next-page (&optional lines)
14664   "Show the next page of the current article.
14665 If end of article, return non-nil.  Otherwise return nil.
14666 Argument LINES specifies lines to be scrolled up."
14667   (interactive "p")
14668   (move-to-window-line -1)
14669   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14670   (if (save-excursion
14671         (end-of-line)
14672         (and (pos-visible-in-window-p)  ;Not continuation line.
14673              (eobp)))
14674       ;; Nothing in this page.
14675       (if (or (not gnus-break-pages)
14676               (save-excursion
14677                 (save-restriction
14678                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14679           t                             ;Nothing more.
14680         (gnus-narrow-to-page 1)         ;Go to next page.
14681         nil)
14682     ;; More in this page.
14683     (condition-case ()
14684         (scroll-up lines)
14685       (end-of-buffer
14686        ;; Long lines may cause an end-of-buffer error.
14687        (goto-char (point-max))))
14688     (move-to-window-line 0)
14689     nil))
14690
14691 (defun gnus-article-prev-page (&optional lines)
14692   "Show previous page of current article.
14693 Argument LINES specifies lines to be scrolled down."
14694   (interactive "p")
14695   (move-to-window-line 0)
14696   (if (and gnus-break-pages
14697            (bobp)
14698            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14699       (progn
14700         (gnus-narrow-to-page -1)        ;Go to previous page.
14701         (goto-char (point-max))
14702         (recenter -1))
14703     (prog1
14704         (condition-case ()
14705             (scroll-down lines)
14706           (error nil))
14707       (move-to-window-line 0))))
14708
14709 (defun gnus-article-refer-article ()
14710   "Read article specified by message-id around point."
14711   (interactive)
14712   (let ((point (point)))
14713     (search-forward ">" nil t)          ;Move point to end of "<....>".
14714     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14715         (let ((message-id (match-string 1)))
14716           (goto-char point)
14717           (set-buffer gnus-summary-buffer)
14718           (gnus-summary-refer-article message-id))
14719       (goto-char (point))
14720       (error "No references around point"))))
14721
14722 (defun gnus-article-show-summary ()
14723   "Reconfigure windows to show summary buffer."
14724   (interactive)
14725   (gnus-configure-windows 'article)
14726   (gnus-summary-goto-subject gnus-current-article))
14727
14728 (defun gnus-article-describe-briefly ()
14729   "Describe article mode commands briefly."
14730   (interactive)
14731   (gnus-message 6
14732                 (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")))
14733
14734 (defun gnus-article-summary-command ()
14735   "Execute the last keystroke in the summary buffer."
14736   (interactive)
14737   (let ((obuf (current-buffer))
14738         (owin (current-window-configuration))
14739         func)
14740     (switch-to-buffer gnus-summary-buffer 'norecord)
14741     (setq func (lookup-key (current-local-map) (this-command-keys)))
14742     (call-interactively func)
14743     (set-buffer obuf)
14744     (set-window-configuration owin)
14745     (set-window-point (get-buffer-window (current-buffer)) (point))))
14746
14747 (defun gnus-article-summary-command-nosave ()
14748   "Execute the last keystroke in the summary buffer."
14749   (interactive)
14750   (let (func)
14751     (pop-to-buffer gnus-summary-buffer 'norecord)
14752     (setq func (lookup-key (current-local-map) (this-command-keys)))
14753     (call-interactively func)))
14754
14755 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14756   "Read a summary buffer key sequence and execute it from the article buffer."
14757   (interactive "P")
14758   (let ((nosaves
14759          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14760            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14761            "=" "^" "\M-^" "|"))
14762         keys)
14763     (save-excursion
14764       (set-buffer gnus-summary-buffer)
14765       (push (or key last-command-event) unread-command-events)
14766       (setq keys (read-key-sequence nil)))
14767     (message "")
14768
14769     (if (member keys nosaves)
14770         (let (func)
14771           (pop-to-buffer gnus-summary-buffer 'norecord)
14772           (if (setq func (lookup-key (current-local-map) keys))
14773               (call-interactively func)
14774             (ding)))
14775       (let ((obuf (current-buffer))
14776             (owin (current-window-configuration))
14777             (opoint (point))
14778             func in-buffer)
14779         (if not-restore-window
14780             (pop-to-buffer gnus-summary-buffer 'norecord)
14781           (switch-to-buffer gnus-summary-buffer 'norecord))
14782         (setq in-buffer (current-buffer))
14783         (if (setq func (lookup-key (current-local-map) keys))
14784             (call-interactively func)
14785           (ding))
14786         (when (eq in-buffer (current-buffer))
14787           (set-buffer obuf)
14788           (unless not-restore-window
14789             (set-window-configuration owin))
14790           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14791
14792 \f
14793 ;;;
14794 ;;; Kill file handling.
14795 ;;;
14796
14797 ;;;###autoload
14798 (defalias 'gnus-batch-kill 'gnus-batch-score)
14799 ;;;###autoload
14800 (defun gnus-batch-score ()
14801   "Run batched scoring.
14802 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14803 Newsgroups is a list of strings in Bnews format.  If you want to score
14804 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14805 score the alt hierarchy, you'd say \"!alt.all\"."
14806   (interactive)
14807   (let* ((yes-and-no
14808           (gnus-newsrc-parse-options
14809            (apply (function concat)
14810                   (mapcar (lambda (g) (concat g " "))
14811                           command-line-args-left))))
14812          (gnus-expert-user t)
14813          (nnmail-spool-file nil)
14814          (gnus-use-dribble-file nil)
14815          (yes (car yes-and-no))
14816          (no (cdr yes-and-no))
14817          group newsrc entry
14818          ;; Disable verbose message.
14819          gnus-novice-user gnus-large-newsgroup)
14820     ;; Eat all arguments.
14821     (setq command-line-args-left nil)
14822     ;; Start Gnus.
14823     (gnus)
14824     ;; Apply kills to specified newsgroups in command line arguments.
14825     (setq newsrc (cdr gnus-newsrc-alist))
14826     (while newsrc
14827       (setq group (caar newsrc))
14828       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14829       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14830                (and (car entry)
14831                     (or (eq (car entry) t)
14832                         (not (zerop (car entry)))))
14833                (if yes (string-match yes group) t)
14834                (or (null no) (not (string-match no group))))
14835           (progn
14836             (gnus-summary-read-group group nil t nil t)
14837             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14838                  (gnus-summary-exit))))
14839       (setq newsrc (cdr newsrc)))
14840     ;; Exit Emacs.
14841     (switch-to-buffer gnus-group-buffer)
14842     (gnus-group-save-newsrc)))
14843
14844 (defun gnus-apply-kill-file ()
14845   "Apply a kill file to the current newsgroup.
14846 Returns the number of articles marked as read."
14847   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14848           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14849       (gnus-apply-kill-file-internal)
14850     0))
14851
14852 (defun gnus-kill-save-kill-buffer ()
14853   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14854     (when (get-file-buffer file)
14855       (save-excursion
14856         (set-buffer (get-file-buffer file))
14857         (and (buffer-modified-p) (save-buffer))
14858         (kill-buffer (current-buffer))))))
14859
14860 (defvar gnus-kill-file-name "KILL"
14861   "Suffix of the kill files.")
14862
14863 (defun gnus-newsgroup-kill-file (newsgroup)
14864   "Return the name of a kill file name for NEWSGROUP.
14865 If NEWSGROUP is nil, return the global kill file name instead."
14866   (cond 
14867    ;; The global KILL file is placed at top of the directory.
14868    ((or (null newsgroup)
14869         (string-equal newsgroup ""))
14870     (expand-file-name gnus-kill-file-name
14871                       gnus-kill-files-directory))
14872    ;; Append ".KILL" to newsgroup name.
14873    ((gnus-use-long-file-name 'not-kill)
14874     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14875                               "." gnus-kill-file-name)
14876                       gnus-kill-files-directory))
14877    ;; Place "KILL" under the hierarchical directory.
14878    (t
14879     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14880                               "/" gnus-kill-file-name)
14881                       gnus-kill-files-directory))))
14882
14883 \f
14884 ;;;
14885 ;;; Dribble file
14886 ;;;
14887
14888 (defvar gnus-dribble-ignore nil)
14889 (defvar gnus-dribble-eval-file nil)
14890
14891 (defun gnus-dribble-file-name ()
14892   "Return the dribble file for the current .newsrc."
14893   (concat
14894    (if gnus-dribble-directory
14895        (concat (file-name-as-directory gnus-dribble-directory)
14896                (file-name-nondirectory gnus-current-startup-file))
14897      gnus-current-startup-file)
14898    "-dribble"))
14899
14900 (defun gnus-dribble-enter (string)
14901   "Enter STRING into the dribble buffer."
14902   (if (and (not gnus-dribble-ignore)
14903            gnus-dribble-buffer
14904            (buffer-name gnus-dribble-buffer))
14905       (let ((obuf (current-buffer)))
14906         (set-buffer gnus-dribble-buffer)
14907         (insert string "\n")
14908         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14909         (set-buffer obuf))))
14910
14911 (defun gnus-dribble-read-file ()
14912   "Read the dribble file from disk."
14913   (let ((dribble-file (gnus-dribble-file-name)))
14914     (save-excursion
14915       (set-buffer (setq gnus-dribble-buffer
14916                         (get-buffer-create
14917                          (file-name-nondirectory dribble-file))))
14918       (gnus-add-current-to-buffer-list)
14919       (erase-buffer)
14920       (setq buffer-file-name dribble-file)
14921       (auto-save-mode t)
14922       (buffer-disable-undo (current-buffer))
14923       (bury-buffer (current-buffer))
14924       (set-buffer-modified-p nil)
14925       (let ((auto (make-auto-save-file-name))
14926             (gnus-dribble-ignore t)
14927             modes)
14928         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14929           ;; Load whichever file is newest -- the auto save file
14930           ;; or the "real" file.
14931           (if (file-newer-than-file-p auto dribble-file)
14932               (insert-file-contents auto)
14933             (insert-file-contents dribble-file))
14934           (unless (zerop (buffer-size))
14935             (set-buffer-modified-p t))
14936           ;; Set the file modes to reflect the .newsrc file modes.
14937           (save-buffer)
14938           (when (and (file-exists-p gnus-current-startup-file)
14939                      (setq modes (file-modes gnus-current-startup-file)))
14940             (set-file-modes dribble-file modes))
14941           ;; Possibly eval the file later.
14942           (when (gnus-y-or-n-p
14943                  "Auto-save file exists.  Do you want to read it? ")
14944             (setq gnus-dribble-eval-file t)))))))
14945
14946 (defun gnus-dribble-eval-file ()
14947   (when gnus-dribble-eval-file
14948     (setq gnus-dribble-eval-file nil)
14949     (save-excursion
14950       (let ((gnus-dribble-ignore t))
14951         (set-buffer gnus-dribble-buffer)
14952         (eval-buffer (current-buffer))))))
14953
14954 (defun gnus-dribble-delete-file ()
14955   (when (file-exists-p (gnus-dribble-file-name))
14956     (delete-file (gnus-dribble-file-name)))
14957   (when gnus-dribble-buffer
14958     (save-excursion
14959       (set-buffer gnus-dribble-buffer)
14960       (let ((auto (make-auto-save-file-name)))
14961         (if (file-exists-p auto)
14962             (delete-file auto))
14963         (erase-buffer)
14964         (set-buffer-modified-p nil)))))
14965
14966 (defun gnus-dribble-save ()
14967   (when (and gnus-dribble-buffer
14968              (buffer-name gnus-dribble-buffer))
14969     (save-excursion
14970       (set-buffer gnus-dribble-buffer)
14971       (save-buffer))))
14972
14973 (defun gnus-dribble-clear ()
14974   (when (gnus-buffer-exists-p gnus-dribble-buffer)
14975     (save-excursion
14976       (set-buffer gnus-dribble-buffer)
14977       (erase-buffer)
14978       (set-buffer-modified-p nil)
14979       (setq buffer-saved-size (buffer-size)))))
14980
14981 \f
14982 ;;;
14983 ;;; Server Communication
14984 ;;;
14985
14986 (defun gnus-start-news-server (&optional confirm)
14987   "Open a method for getting news.
14988 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14989   (let (how)
14990     (if gnus-current-select-method
14991         ;; Stream is already opened.
14992         nil
14993       ;; Open NNTP server.
14994       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14995       (if confirm
14996           (progn
14997             ;; Read server name with completion.
14998             (setq gnus-nntp-server
14999                   (completing-read "NNTP server: "
15000                                    (mapcar (lambda (server) (list server))
15001                                            (cons (list gnus-nntp-server)
15002                                                  gnus-secondary-servers))
15003                                    nil nil gnus-nntp-server))))
15004
15005       (if (and gnus-nntp-server
15006                (stringp gnus-nntp-server)
15007                (not (string= gnus-nntp-server "")))
15008           (setq gnus-select-method
15009                 (cond ((or (string= gnus-nntp-server "")
15010                            (string= gnus-nntp-server "::"))
15011                        (list 'nnspool (system-name)))
15012                       ((string-match "^:" gnus-nntp-server)
15013                        (list 'nnmh gnus-nntp-server
15014                              (list 'nnmh-directory
15015                                    (file-name-as-directory
15016                                     (expand-file-name
15017                                      (concat "~/" (substring
15018                                                    gnus-nntp-server 1)))))
15019                              (list 'nnmh-get-new-mail nil)))
15020                       (t
15021                        (list 'nntp gnus-nntp-server)))))
15022
15023       (setq how (car gnus-select-method))
15024       (cond ((eq how 'nnspool)
15025              (require 'nnspool)
15026              (gnus-message 5 "Looking up local news spool..."))
15027             ((eq how 'nnmh)
15028              (require 'nnmh)
15029              (gnus-message 5 "Looking up mh spool..."))
15030             (t
15031              (require 'nntp)))
15032       (setq gnus-current-select-method gnus-select-method)
15033       (run-hooks 'gnus-open-server-hook)
15034       (or
15035        ;; gnus-open-server-hook might have opened it
15036        (gnus-server-opened gnus-select-method)
15037        (gnus-open-server gnus-select-method)
15038        (gnus-y-or-n-p
15039         (format
15040          "%s (%s) open error: '%s'.     Continue? "
15041          (car gnus-select-method) (cadr gnus-select-method)
15042          (gnus-status-message gnus-select-method)))
15043        (gnus-error 1 "Couldn't open server on %s"
15044                    (nth 1 gnus-select-method))))))
15045
15046 (defun gnus-check-group (group)
15047   "Try to make sure that the server where GROUP exists is alive."
15048   (let ((method (gnus-find-method-for-group group)))
15049     (or (gnus-server-opened method)
15050         (gnus-open-server method))))
15051
15052 (defun gnus-check-server (&optional method silent)
15053   "Check whether the connection to METHOD is down.
15054 If METHOD is nil, use `gnus-select-method'.
15055 If it is down, start it up (again)."
15056   (let ((method (or method gnus-select-method)))
15057     ;; Transform virtual server names into select methods.
15058     (when (stringp method)
15059       (setq method (gnus-server-to-method method)))
15060     (if (gnus-server-opened method)
15061         ;; The stream is already opened.
15062         t
15063       ;; Open the server.
15064       (unless silent
15065         (gnus-message 5 "Opening %s server%s..." (car method)
15066                       (if (equal (nth 1 method) "") ""
15067                         (format " on %s" (nth 1 method)))))
15068       (run-hooks 'gnus-open-server-hook)
15069       (prog1
15070           (gnus-open-server method)
15071         (unless silent
15072           (message ""))))))
15073
15074 (defun gnus-get-function (method function &optional noerror)
15075   "Return a function symbol based on METHOD and FUNCTION."
15076   ;; Translate server names into methods.
15077   (unless method
15078     (error "Attempted use of a nil select method"))
15079   (when (stringp method)
15080     (setq method (gnus-server-to-method method)))
15081   (let ((func (intern (format "%s-%s" (car method) function))))
15082     ;; If the functions isn't bound, we require the backend in
15083     ;; question.
15084     (unless (fboundp func)
15085       (require (car method))
15086       (when (and (not (fboundp func))
15087                  (not noerror))
15088         ;; This backend doesn't implement this function.
15089         (error "No such function: %s" func)))
15090     func))
15091
15092 \f
15093 ;;;
15094 ;;; Interface functions to the backends.
15095 ;;;
15096
15097 (defun gnus-open-server (method)
15098   "Open a connection to METHOD."
15099   (when (stringp method)
15100     (setq method (gnus-server-to-method method)))
15101   (let ((elem (assoc method gnus-opened-servers)))
15102     ;; If this method was previously denied, we just return nil.
15103     (if (eq (nth 1 elem) 'denied)
15104         (progn
15105           (gnus-message 1 "Denied server")
15106           nil)
15107       ;; Open the server.
15108       (let ((result
15109              (funcall (gnus-get-function method 'open-server)
15110                       (nth 1 method) (nthcdr 2 method))))
15111         ;; If this hasn't been opened before, we add it to the list.
15112         (unless elem
15113           (setq elem (list method nil)
15114                 gnus-opened-servers (cons elem gnus-opened-servers)))
15115         ;; Set the status of this server.
15116         (setcar (cdr elem) (if result 'ok 'denied))
15117         ;; Return the result from the "open" call.
15118         result))))
15119
15120 (defun gnus-close-server (method)
15121   "Close the connection to METHOD."
15122   (when (stringp method)
15123     (setq method (gnus-server-to-method method)))
15124   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
15125
15126 (defun gnus-request-list (method)
15127   "Request the active file from METHOD."
15128   (when (stringp method)
15129     (setq method (gnus-server-to-method method)))
15130   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
15131
15132 (defun gnus-request-list-newsgroups (method)
15133   "Request the newsgroups file from METHOD."
15134   (when (stringp method)
15135     (setq method (gnus-server-to-method method)))
15136   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
15137
15138 (defun gnus-request-newgroups (date method)
15139   "Request all new groups since DATE from METHOD."
15140   (when (stringp method)
15141     (setq method (gnus-server-to-method method)))
15142   (funcall (gnus-get-function method 'request-newgroups)
15143            date (nth 1 method)))
15144
15145 (defun gnus-server-opened (method)
15146   "Check whether a connection to METHOD has been opened."
15147   (when (stringp method)
15148     (setq method (gnus-server-to-method method)))
15149   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
15150
15151 (defun gnus-status-message (method)
15152   "Return the status message from METHOD.
15153 If METHOD is a string, it is interpreted as a group name.   The method
15154 this group uses will be queried."
15155   (let ((method (if (stringp method) (gnus-find-method-for-group method)
15156                   method)))
15157     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
15158
15159 (defun gnus-request-group (group &optional dont-check method)
15160   "Request GROUP.  If DONT-CHECK, no information is required."
15161   (let ((method (or method (gnus-find-method-for-group group))))
15162     (when (stringp method)
15163       (setq method (gnus-server-to-method method)))
15164     (funcall (gnus-get-function method 'request-group)
15165              (gnus-group-real-name group) (nth 1 method) dont-check)))
15166
15167 (defun gnus-request-asynchronous (group &optional articles)
15168   "Request that GROUP behave asynchronously.
15169 ARTICLES is the `data' of the group."
15170   (let ((method (gnus-find-method-for-group group)))
15171     (funcall (gnus-get-function method 'request-asynchronous)
15172              (gnus-group-real-name group) (nth 1 method) articles)))
15173
15174 (defun gnus-list-active-group (group)
15175   "Request active information on GROUP."
15176   (let ((method (gnus-find-method-for-group group))
15177         (func 'list-active-group))
15178     (when (gnus-check-backend-function func group)
15179       (funcall (gnus-get-function method func)
15180                (gnus-group-real-name group) (nth 1 method)))))
15181
15182 (defun gnus-request-group-description (group)
15183   "Request a description of GROUP."
15184   (let ((method (gnus-find-method-for-group group))
15185         (func 'request-group-description))
15186     (when (gnus-check-backend-function func group)
15187       (funcall (gnus-get-function method func)
15188                (gnus-group-real-name group) (nth 1 method)))))
15189
15190 (defun gnus-close-group (group)
15191   "Request the GROUP be closed."
15192   (let ((method (gnus-find-method-for-group group)))
15193     (funcall (gnus-get-function method 'close-group)
15194              (gnus-group-real-name group) (nth 1 method))))
15195
15196 (defun gnus-retrieve-headers (articles group &optional fetch-old)
15197   "Request headers for ARTICLES in GROUP.
15198 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
15199   (let ((method (gnus-find-method-for-group group)))
15200     (if (and gnus-use-cache (numberp (car articles)))
15201         (gnus-cache-retrieve-headers articles group fetch-old)
15202       (funcall (gnus-get-function method 'retrieve-headers)
15203                articles (gnus-group-real-name group) (nth 1 method)
15204                fetch-old))))
15205
15206 (defun gnus-retrieve-groups (groups method)
15207   "Request active information on GROUPS from METHOD."
15208   (when (stringp method)
15209     (setq method (gnus-server-to-method method)))
15210   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
15211
15212 (defun gnus-request-type (group &optional article)
15213   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15214   (let ((method (gnus-find-method-for-group group)))
15215     (if (not (gnus-check-backend-function 'request-type (car method)))
15216         'unknown
15217       (funcall (gnus-get-function method 'request-type)
15218                (gnus-group-real-name group) article))))
15219
15220 (defun gnus-request-update-mark (group article mark)
15221   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15222   (let ((method (gnus-find-method-for-group group)))
15223     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
15224         mark
15225       (funcall (gnus-get-function method 'request-update-mark)
15226                (gnus-group-real-name group) article mark))))
15227
15228 (defun gnus-request-article (article group &optional buffer)
15229   "Request the ARTICLE in GROUP.
15230 ARTICLE can either be an article number or an article Message-ID.
15231 If BUFFER, insert the article in that group."
15232   (let ((method (gnus-find-method-for-group group)))
15233     (funcall (gnus-get-function method 'request-article)
15234              article (gnus-group-real-name group) (nth 1 method) buffer)))
15235
15236 (defun gnus-request-head (article group)
15237   "Request the head of ARTICLE in GROUP."
15238   (let* ((method (gnus-find-method-for-group group))
15239          (head (gnus-get-function method 'request-head t)))
15240     (if (fboundp head)
15241         (funcall head article (gnus-group-real-name group) (nth 1 method))
15242       (let ((res (gnus-request-article article group)))
15243         (when res
15244           (save-excursion
15245             (set-buffer nntp-server-buffer)
15246             (goto-char (point-min))
15247             (when (search-forward "\n\n" nil t)
15248               (delete-region (1- (point)) (point-max)))
15249             (nnheader-fold-continuation-lines)))
15250         res))))
15251
15252 (defun gnus-request-body (article group)
15253   "Request the body of ARTICLE in GROUP."
15254   (let ((method (gnus-find-method-for-group group)))
15255     (funcall (gnus-get-function method 'request-body)
15256              article (gnus-group-real-name group) (nth 1 method))))
15257
15258 (defun gnus-request-post (method)
15259   "Post the current buffer using METHOD."
15260   (when (stringp method)
15261     (setq method (gnus-server-to-method method)))
15262   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
15263
15264 (defun gnus-request-scan (group method)
15265   "Request a SCAN being performed in GROUP from METHOD.
15266 If GROUP is nil, all groups on METHOD are scanned."
15267   (let ((method (if group (gnus-find-method-for-group group) method)))
15268     (funcall (gnus-get-function method 'request-scan)
15269              (and group (gnus-group-real-name group)) (nth 1 method))))
15270
15271 (defsubst gnus-request-update-info (info method)
15272   "Request that METHOD update INFO."
15273   (when (stringp method)
15274     (setq method (gnus-server-to-method method)))
15275   (when (gnus-check-backend-function 'request-update-info (car method))
15276     (funcall (gnus-get-function method 'request-update-info)
15277              (gnus-group-real-name (gnus-info-group info))
15278              info (nth 1 method))))
15279
15280 (defun gnus-request-expire-articles (articles group &optional force)
15281   (let ((method (gnus-find-method-for-group group)))
15282     (funcall (gnus-get-function method 'request-expire-articles)
15283              articles (gnus-group-real-name group) (nth 1 method)
15284              force)))
15285
15286 (defun gnus-request-move-article
15287   (article group server accept-function &optional last)
15288   (let ((method (gnus-find-method-for-group group)))
15289     (funcall (gnus-get-function method 'request-move-article)
15290              article (gnus-group-real-name group)
15291              (nth 1 method) accept-function last)))
15292
15293 (defun gnus-request-accept-article (group method &optional last)
15294   ;; Make sure there's a newline at the end of the article.
15295   (when (stringp method)
15296     (setq method (gnus-server-to-method method)))
15297   (when (and (not method)
15298              (stringp group))
15299     (setq method (gnus-group-name-to-method group)))
15300   (goto-char (point-max))
15301   (unless (bolp)
15302     (insert "\n"))
15303   (let ((func (car (or method (gnus-find-method-for-group group)))))
15304     (funcall (intern (format "%s-request-accept-article" func))
15305              (if (stringp group) (gnus-group-real-name group) group)
15306              (cadr method)
15307              last)))
15308
15309 (defun gnus-request-replace-article (article group buffer)
15310   (let ((func (car (gnus-find-method-for-group group))))
15311     (funcall (intern (format "%s-request-replace-article" func))
15312              article (gnus-group-real-name group) buffer)))
15313
15314 (defun gnus-request-associate-buffer (group)
15315   (let ((method (gnus-find-method-for-group group)))
15316     (funcall (gnus-get-function method 'request-associate-buffer)
15317              (gnus-group-real-name group))))
15318
15319 (defun gnus-request-restore-buffer (article group)
15320   "Request a new buffer restored to the state of ARTICLE."
15321   (let ((method (gnus-find-method-for-group group)))
15322     (funcall (gnus-get-function method 'request-restore-buffer)
15323              article (gnus-group-real-name group) (nth 1 method))))
15324
15325 (defun gnus-request-create-group (group &optional method)
15326   (when (stringp method)
15327     (setq method (gnus-server-to-method method)))
15328   (let ((method (or method (gnus-find-method-for-group group))))
15329     (funcall (gnus-get-function method 'request-create-group)
15330              (gnus-group-real-name group) (nth 1 method))))
15331
15332 (defun gnus-request-delete-group (group &optional force)
15333   (let ((method (gnus-find-method-for-group group)))
15334     (funcall (gnus-get-function method 'request-delete-group)
15335              (gnus-group-real-name group) force (nth 1 method))))
15336
15337 (defun gnus-request-rename-group (group new-name)
15338   (let ((method (gnus-find-method-for-group group)))
15339     (funcall (gnus-get-function method 'request-rename-group)
15340              (gnus-group-real-name group)
15341              (gnus-group-real-name new-name) (nth 1 method))))
15342
15343 (defun gnus-member-of-valid (symbol group)
15344   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15345   (memq symbol (assoc
15346                 (symbol-name (car (gnus-find-method-for-group group)))
15347                 gnus-valid-select-methods)))
15348
15349 (defun gnus-method-option-p (method option)
15350   "Return non-nil if select METHOD has OPTION as a parameter."
15351   (when (stringp method)
15352     (setq method (gnus-server-to-method method)))
15353   (memq option (assoc (format "%s" (car method))
15354                       gnus-valid-select-methods)))
15355
15356 (defun gnus-server-extend-method (group method)
15357   ;; This function "extends" a virtual server.  If the server is
15358   ;; "hello", and the select method is ("hello" (my-var "something"))
15359   ;; in the group "alt.alt", this will result in a new virtual server
15360   ;; called "hello+alt.alt".
15361   (let ((entry
15362          (gnus-copy-sequence
15363           (if (equal (car method) "native") gnus-select-method
15364             (cdr (assoc (car method) gnus-server-alist))))))
15365     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15366     (nconc entry (cdr method))))
15367
15368 (defun gnus-group-name-to-method (group)
15369   "Return a select method suitable for GROUP."
15370   (if (string-match ":" group)
15371       (let ((server (substring group 0 (match-beginning 0))))
15372         (if (string-match "\\+" server)
15373             (list (intern (substring server 0 (match-beginning 0)))
15374                   (substring server (match-end 0)))
15375           (list (intern server) "")))
15376     gnus-select-method))
15377
15378 (defun gnus-find-method-for-group (group &optional info)
15379   "Find the select method that GROUP uses."
15380   (or gnus-override-method
15381       (and (not group)
15382            gnus-select-method)
15383       (let ((info (or info (gnus-get-info group)))
15384             method)
15385         (if (or (not info)
15386                 (not (setq method (gnus-info-method info)))
15387                 (equal method "native"))
15388             gnus-select-method
15389           (setq method
15390                 (cond ((stringp method)
15391                        (gnus-server-to-method method))
15392                       ((stringp (car method))
15393                        (gnus-server-extend-method group method))
15394                       (t
15395                        method)))
15396           (cond ((equal (cadr method) "")
15397                  method)
15398                 ((null (cadr method))
15399                  (list (car method) ""))
15400                 (t
15401                  (gnus-server-add-address method)))))))
15402
15403 (defun gnus-check-backend-function (func group)
15404   "Check whether GROUP supports function FUNC."
15405   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15406                   group)))
15407     (fboundp (intern (format "%s-%s" method func)))))
15408
15409 (defun gnus-methods-using (feature)
15410   "Find all methods that have FEATURE."
15411   (let ((valids gnus-valid-select-methods)
15412         outs)
15413     (while valids
15414       (if (memq feature (car valids))
15415           (setq outs (cons (car valids) outs)))
15416       (setq valids (cdr valids)))
15417     outs))
15418
15419 \f
15420 ;;;
15421 ;;; Active & Newsrc File Handling
15422 ;;;
15423
15424 (defun gnus-setup-news (&optional rawfile level dont-connect)
15425   "Setup news information.
15426 If RAWFILE is non-nil, the .newsrc file will also be read.
15427 If LEVEL is non-nil, the news will be set up at level LEVEL."
15428   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15429
15430     (when init 
15431       ;; Clear some variables to re-initialize news information.
15432       (setq gnus-newsrc-alist nil
15433             gnus-active-hashtb nil)
15434       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15435       (gnus-read-newsrc-file rawfile))
15436
15437     (when (and (not (assoc "archive" gnus-server-alist))
15438                gnus-message-archive-method)
15439       (push (cons "archive" gnus-message-archive-method)
15440             gnus-server-alist))
15441
15442     ;; If we don't read the complete active file, we fill in the
15443     ;; hashtb here.
15444     (if (or (null gnus-read-active-file)
15445             (eq gnus-read-active-file 'some))
15446         (gnus-update-active-hashtb-from-killed))
15447
15448     ;; Read the active file and create `gnus-active-hashtb'.
15449     ;; If `gnus-read-active-file' is nil, then we just create an empty
15450     ;; hash table.  The partial filling out of the hash table will be
15451     ;; done in `gnus-get-unread-articles'.
15452     (and gnus-read-active-file
15453          (not level)
15454          (gnus-read-active-file))
15455
15456     (or gnus-active-hashtb
15457         (setq gnus-active-hashtb (make-vector 4095 0)))
15458
15459     ;; Initialize the cache.
15460     (when gnus-use-cache
15461       (gnus-cache-open))
15462
15463     ;; Possibly eval the dribble file.
15464     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15465
15466     ;; Slave Gnusii should then clear the dribble buffer.
15467     (when (and init gnus-slave)
15468       (gnus-dribble-clear))
15469
15470     (gnus-update-format-specifications)
15471
15472     ;; See whether we need to read the description file.
15473     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15474              (not gnus-description-hashtb)
15475              (not dont-connect)
15476              gnus-read-active-file)
15477         (gnus-read-all-descriptions-files))
15478
15479     ;; Find new newsgroups and treat them.
15480     (if (and init gnus-check-new-newsgroups (not level)
15481              (gnus-check-server gnus-select-method))
15482         (gnus-find-new-newsgroups))
15483
15484     ;; We might read in new NoCeM messages here.
15485     (when (and gnus-use-nocem 
15486                (not level)
15487                (not dont-connect))
15488       (gnus-nocem-scan-groups))
15489
15490     ;; Find the number of unread articles in each non-dead group.
15491     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15492       (gnus-get-unread-articles level))
15493
15494     (if (and init gnus-check-bogus-newsgroups
15495              gnus-read-active-file (not level)
15496              (gnus-server-opened gnus-select-method))
15497         (gnus-check-bogus-newsgroups))))
15498
15499 (defun gnus-find-new-newsgroups (&optional arg)
15500   "Search for new newsgroups and add them.
15501 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15502 The `-n' option line from .newsrc is respected.
15503 If ARG (the prefix), use the `ask-server' method to query
15504 the server for new groups."
15505   (interactive "P")
15506   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15507                        (null gnus-read-active-file)
15508                        (eq gnus-read-active-file 'some))
15509                    'ask-server gnus-check-new-newsgroups)))
15510     (unless (gnus-check-first-time-used)
15511       (if (or (consp check)
15512               (eq check 'ask-server))
15513           ;; Ask the server for new groups.
15514           (gnus-ask-server-for-new-groups)
15515         ;; Go through the active hashtb and look for new groups.
15516         (let ((groups 0)
15517               group new-newsgroups)
15518           (gnus-message 5 "Looking for new newsgroups...")
15519           (unless gnus-have-read-active-file
15520             (gnus-read-active-file))
15521           (setq gnus-newsrc-last-checked-date (current-time-string))
15522           (unless gnus-killed-hashtb
15523             (gnus-make-hashtable-from-killed))
15524           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15525           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15526           (mapatoms
15527            (lambda (sym)
15528              (if (or (null (setq group (symbol-name sym)))
15529                      (not (boundp sym))
15530                      (null (symbol-value sym))
15531                      (gnus-gethash group gnus-killed-hashtb)
15532                      (gnus-gethash group gnus-newsrc-hashtb))
15533                  ()
15534                (let ((do-sub (gnus-matches-options-n group)))
15535                  (cond
15536                   ((eq do-sub 'subscribe)
15537                    (setq groups (1+ groups))
15538                    (gnus-sethash group group gnus-killed-hashtb)
15539                    (funcall gnus-subscribe-options-newsgroup-method group))
15540                   ((eq do-sub 'ignore)
15541                    nil)
15542                   (t
15543                    (setq groups (1+ groups))
15544                    (gnus-sethash group group gnus-killed-hashtb)
15545                    (if gnus-subscribe-hierarchical-interactive
15546                        (setq new-newsgroups (cons group new-newsgroups))
15547                      (funcall gnus-subscribe-newsgroup-method group)))))))
15548            gnus-active-hashtb)
15549           (when new-newsgroups
15550             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15551           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15552           (if (> groups 0)
15553               (gnus-message 6 "%d new newsgroup%s arrived."
15554                             groups (if (> groups 1) "s have" " has"))
15555             (gnus-message 6 "No new newsgroups.")))))))
15556
15557 (defun gnus-matches-options-n (group)
15558   ;; Returns `subscribe' if the group is to be unconditionally
15559   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15560   ;; no match for the group.
15561
15562   ;; First we check the two user variables.
15563   (cond
15564    ((and gnus-options-subscribe
15565          (string-match gnus-options-subscribe group))
15566     'subscribe)
15567    ((and gnus-auto-subscribed-groups
15568          (string-match gnus-auto-subscribed-groups group))
15569     'subscribe)
15570    ((and gnus-options-not-subscribe
15571          (string-match gnus-options-not-subscribe group))
15572     'ignore)
15573    ;; Then we go through the list that was retrieved from the .newsrc
15574    ;; file.  This list has elements on the form
15575    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15576    ;; is in the reverse order of the options line) is returned.
15577    (t
15578     (let ((regs gnus-newsrc-options-n))
15579       (while (and regs
15580                   (not (string-match (caar regs) group)))
15581         (setq regs (cdr regs)))
15582       (and regs (cdar regs))))))
15583
15584 (defun gnus-ask-server-for-new-groups ()
15585   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15586          (methods (cons gnus-select-method
15587                         (nconc
15588                          (when gnus-message-archive-method
15589                            (list "archive"))
15590                          (append
15591                           (and (consp gnus-check-new-newsgroups)
15592                                gnus-check-new-newsgroups)
15593                           gnus-secondary-select-methods))))
15594          (groups 0)
15595          (new-date (current-time-string))
15596          group new-newsgroups got-new method hashtb
15597          gnus-override-subscribe-method)
15598     ;; Go through both primary and secondary select methods and
15599     ;; request new newsgroups.
15600     (while (setq method (gnus-server-get-method nil (pop methods)))
15601       (setq new-newsgroups nil)
15602       (setq gnus-override-subscribe-method method)
15603       (when (and (gnus-check-server method)
15604                  (gnus-request-newgroups date method))
15605         (save-excursion
15606           (setq got-new t)
15607           (setq hashtb (gnus-make-hashtable 100))
15608           (set-buffer nntp-server-buffer)
15609           ;; Enter all the new groups into a hashtable.
15610           (gnus-active-to-gnus-format method hashtb 'ignore))
15611         ;; Now all new groups from `method' are in `hashtb'.
15612         (mapatoms
15613          (lambda (group-sym)
15614            (if (or (null (setq group (symbol-name group-sym)))
15615                    (not (boundp group-sym))
15616                    (null (symbol-value group-sym))
15617                    (gnus-gethash group gnus-newsrc-hashtb)
15618                    (member group gnus-zombie-list)
15619                    (member group gnus-killed-list))
15620                ;; The group is already known.
15621                ()
15622              ;; Make this group active.
15623              (when (symbol-value group-sym)
15624                (gnus-set-active group (symbol-value group-sym)))
15625              ;; Check whether we want it or not.
15626              (let ((do-sub (gnus-matches-options-n group)))
15627                (cond
15628                 ((eq do-sub 'subscribe)
15629                  (incf groups)
15630                  (gnus-sethash group group gnus-killed-hashtb)
15631                  (funcall gnus-subscribe-options-newsgroup-method group))
15632                 ((eq do-sub 'ignore)
15633                  nil)
15634                 (t
15635                  (incf groups)
15636                  (gnus-sethash group group gnus-killed-hashtb)
15637                  (if gnus-subscribe-hierarchical-interactive
15638                      (push group new-newsgroups)
15639                    (funcall gnus-subscribe-newsgroup-method group)))))))
15640          hashtb))
15641       (when new-newsgroups
15642         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15643     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15644     (when (> groups 0)
15645       (gnus-message 6 "%d new newsgroup%s arrived."
15646                     groups (if (> groups 1) "s have" " has")))
15647     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15648     got-new))
15649
15650 (defun gnus-check-first-time-used ()
15651   (if (or (> (length gnus-newsrc-alist) 1)
15652           (file-exists-p gnus-startup-file)
15653           (file-exists-p (concat gnus-startup-file ".el"))
15654           (file-exists-p (concat gnus-startup-file ".eld")))
15655       nil
15656     (gnus-message 6 "First time user; subscribing you to default groups")
15657     (unless (gnus-read-active-file-p)
15658       (gnus-read-active-file))
15659     (setq gnus-newsrc-last-checked-date (current-time-string))
15660     (let ((groups gnus-default-subscribed-newsgroups)
15661           group)
15662       (if (eq groups t)
15663           nil
15664         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15665         (mapatoms
15666          (lambda (sym)
15667            (if (null (setq group (symbol-name sym)))
15668                ()
15669              (let ((do-sub (gnus-matches-options-n group)))
15670                (cond
15671                 ((eq do-sub 'subscribe)
15672                  (gnus-sethash group group gnus-killed-hashtb)
15673                  (funcall gnus-subscribe-options-newsgroup-method group))
15674                 ((eq do-sub 'ignore)
15675                  nil)
15676                 (t
15677                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15678          gnus-active-hashtb)
15679         (while groups
15680           (if (gnus-active (car groups))
15681               (gnus-group-change-level
15682                (car groups) gnus-level-default-subscribed gnus-level-killed))
15683           (setq groups (cdr groups)))
15684         (gnus-group-make-help-group)
15685         (and gnus-novice-user
15686              (gnus-message 7 "`A k' to list killed groups"))))))
15687
15688 (defun gnus-subscribe-group (group previous &optional method)
15689   (gnus-group-change-level
15690    (if method
15691        (list t group gnus-level-default-subscribed nil nil method)
15692      group)
15693    gnus-level-default-subscribed gnus-level-killed previous t))
15694
15695 ;; `gnus-group-change-level' is the fundamental function for changing
15696 ;; subscription levels of newsgroups.  This might mean just changing
15697 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15698 ;; again, which subscribes/unsubscribes a group, which is equally
15699 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15700 ;; from 8-9 to 1-7 means that you remove the group from the list of
15701 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15702 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15703 ;; which is trivial.
15704 ;; ENTRY can either be a string (newsgroup name) or a list (if
15705 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15706 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15707 ;; entries.
15708 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15709 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15710 ;; after.
15711 (defun gnus-group-change-level (entry level &optional oldlevel
15712                                       previous fromkilled)
15713   (let (group info active num)
15714     ;; Glean what info we can from the arguments
15715     (if (consp entry)
15716         (if fromkilled (setq group (nth 1 entry))
15717           (setq group (car (nth 2 entry))))
15718       (setq group entry))
15719     (if (and (stringp entry)
15720              oldlevel
15721              (< oldlevel gnus-level-zombie))
15722         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15723     (if (and (not oldlevel)
15724              (consp entry))
15725         (setq oldlevel (gnus-info-level (nth 2 entry)))
15726       (setq oldlevel (or oldlevel 9)))
15727     (if (stringp previous)
15728         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15729
15730     (if (and (>= oldlevel gnus-level-zombie)
15731              (gnus-gethash group gnus-newsrc-hashtb))
15732         ;; We are trying to subscribe a group that is already
15733         ;; subscribed.
15734         ()                              ; Do nothing.
15735
15736       (or (gnus-ephemeral-group-p group)
15737           (gnus-dribble-enter
15738            (format "(gnus-group-change-level %S %S %S %S %S)"
15739                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15740
15741       ;; Then we remove the newgroup from any old structures, if needed.
15742       ;; If the group was killed, we remove it from the killed or zombie
15743       ;; list.  If not, and it is in fact going to be killed, we remove
15744       ;; it from the newsrc hash table and assoc.
15745       (cond
15746        ((>= oldlevel gnus-level-zombie)
15747         (if (= oldlevel gnus-level-zombie)
15748             (setq gnus-zombie-list (delete group gnus-zombie-list))
15749           (setq gnus-killed-list (delete group gnus-killed-list))))
15750        (t
15751         (if (and (>= level gnus-level-zombie)
15752                  entry)
15753             (progn
15754               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15755               (if (nth 3 entry)
15756                   (setcdr (gnus-gethash (car (nth 3 entry))
15757                                         gnus-newsrc-hashtb)
15758                           (cdr entry)))
15759               (setcdr (cdr entry) (cdddr entry))))))
15760
15761       ;; Finally we enter (if needed) the list where it is supposed to
15762       ;; go, and change the subscription level.  If it is to be killed,
15763       ;; we enter it into the killed or zombie list.
15764       (cond 
15765        ((>= level gnus-level-zombie)
15766         ;; Remove from the hash table.
15767         (gnus-sethash group nil gnus-newsrc-hashtb)
15768         ;; We do not enter foreign groups into the list of dead
15769         ;; groups.
15770         (unless (gnus-group-foreign-p group)
15771           (if (= level gnus-level-zombie)
15772               (setq gnus-zombie-list (cons group gnus-zombie-list))
15773             (setq gnus-killed-list (cons group gnus-killed-list)))))
15774        (t
15775         ;; If the list is to be entered into the newsrc assoc, and
15776         ;; it was killed, we have to create an entry in the newsrc
15777         ;; hashtb format and fix the pointers in the newsrc assoc.
15778         (if (< oldlevel gnus-level-zombie)
15779             ;; It was alive, and it is going to stay alive, so we
15780             ;; just change the level and don't change any pointers or
15781             ;; hash table entries.
15782             (setcar (cdaddr entry) level)
15783           (if (listp entry)
15784               (setq info (cdr entry)
15785                     num (car entry))
15786             (setq active (gnus-active group))
15787             (setq num
15788                   (if active (- (1+ (cdr active)) (car active)) t))
15789             ;; Check whether the group is foreign.  If so, the
15790             ;; foreign select method has to be entered into the
15791             ;; info.
15792             (let ((method (or gnus-override-subscribe-method
15793                               (gnus-group-method group))))
15794               (if (eq method gnus-select-method)
15795                   (setq info (list group level nil))
15796                 (setq info (list group level nil nil method)))))
15797           (unless previous
15798             (setq previous
15799                   (let ((p gnus-newsrc-alist))
15800                     (while (cddr p)
15801                       (setq p (cdr p)))
15802                     p)))
15803           (setq entry (cons info (cddr previous)))
15804           (if (cdr previous)
15805               (progn
15806                 (setcdr (cdr previous) entry)
15807                 (gnus-sethash group (cons num (cdr previous))
15808                               gnus-newsrc-hashtb))
15809             (setcdr previous entry)
15810             (gnus-sethash group (cons num previous)
15811                           gnus-newsrc-hashtb))
15812           (when (cdr entry)
15813             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15814       (when gnus-group-change-level-function
15815         (funcall gnus-group-change-level-function group level oldlevel)))))
15816
15817 (defun gnus-kill-newsgroup (newsgroup)
15818   "Obsolete function.  Kills a newsgroup."
15819   (gnus-group-change-level
15820    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15821
15822 (defun gnus-check-bogus-newsgroups (&optional confirm)
15823   "Remove bogus newsgroups.
15824 If CONFIRM is non-nil, the user has to confirm the deletion of every
15825 newsgroup."
15826   (let ((newsrc (cdr gnus-newsrc-alist))
15827         bogus group entry info)
15828     (gnus-message 5 "Checking bogus newsgroups...")
15829     (unless (gnus-read-active-file-p)
15830       (gnus-read-active-file))
15831     (when (gnus-read-active-file-p)
15832       ;; Find all bogus newsgroup that are subscribed.
15833       (while newsrc
15834         (setq info (pop newsrc)
15835               group (gnus-info-group info))
15836         (unless (or (gnus-active group) ; Active
15837                     (gnus-info-method info) ; Foreign
15838                     (and confirm
15839                          (not (gnus-y-or-n-p
15840                                (format "Remove bogus newsgroup: %s " group)))))
15841           ;; Found a bogus newsgroup.
15842           (push group bogus)))
15843       ;; Remove all bogus subscribed groups by first killing them, and
15844       ;; then removing them from the list of killed groups.
15845       (while bogus
15846         (when (setq entry (gnus-gethash (setq group (pop bogus))
15847                                         gnus-newsrc-hashtb))
15848           (gnus-group-change-level entry gnus-level-killed)
15849           (setq gnus-killed-list (delete group gnus-killed-list))))
15850       ;; Then we remove all bogus groups from the list of killed and
15851       ;; zombie groups.  They are are removed without confirmation.
15852       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15853             killed)
15854         (while dead-lists
15855           (setq killed (symbol-value (car dead-lists)))
15856           (while killed
15857             (unless (gnus-active (setq group (pop killed)))
15858               ;; The group is bogus.
15859               ;; !!!Slow as hell.
15860               (set (car dead-lists)
15861                    (delete group (symbol-value (car dead-lists))))))
15862           (setq dead-lists (cdr dead-lists))))
15863       (gnus-message 5 "Checking bogus newsgroups...done"))))
15864
15865 (defun gnus-check-duplicate-killed-groups ()
15866   "Remove duplicates from the list of killed groups."
15867   (interactive)
15868   (let ((killed gnus-killed-list))
15869     (while killed
15870       (gnus-message 9 "%d" (length killed))
15871       (setcdr killed (delete (car killed) (cdr killed)))
15872       (setq killed (cdr killed)))))
15873
15874 ;; We want to inline a function from gnus-cache, so we cheat here:
15875 (eval-when-compile
15876   (provide 'gnus)
15877   (require 'gnus-cache))
15878
15879 (defun gnus-get-unread-articles-in-group (info active &optional update)
15880   (when active
15881     ;; Allow the backend to update the info in the group.
15882     (when (and update 
15883                (gnus-request-update-info
15884                 info (gnus-find-method-for-group (gnus-info-group info))))
15885       (gnus-activate-group (gnus-info-group info) nil t))
15886     (let* ((range (gnus-info-read info))
15887            (num 0))
15888       ;; If a cache is present, we may have to alter the active info.
15889       (when (and gnus-use-cache info)
15890         (inline (gnus-cache-possibly-alter-active 
15891                  (gnus-info-group info) active)))
15892       ;; Modify the list of read articles according to what articles
15893       ;; are available; then tally the unread articles and add the
15894       ;; number to the group hash table entry.
15895       (cond
15896        ((zerop (cdr active))
15897         (setq num 0))
15898        ((not range)
15899         (setq num (- (1+ (cdr active)) (car active))))
15900        ((not (listp (cdr range)))
15901         ;; Fix a single (num . num) range according to the
15902         ;; active hash table.
15903         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15904         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15905         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15906         ;; Compute number of unread articles.
15907         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15908        (t
15909         ;; The read list is a list of ranges.  Fix them according to
15910         ;; the active hash table.
15911         ;; First peel off any elements that are below the lower
15912         ;; active limit.
15913         (while (and (cdr range)
15914                     (>= (car active)
15915                         (or (and (atom (cadr range)) (cadr range))
15916                             (caadr range))))
15917           (if (numberp (car range))
15918               (setcar range
15919                       (cons (car range)
15920                             (or (and (numberp (cadr range))
15921                                      (cadr range))
15922                                 (cdadr range))))
15923             (setcdr (car range)
15924                     (or (and (numberp (nth 1 range)) (nth 1 range))
15925                         (cdadr range))))
15926           (setcdr range (cddr range)))
15927         ;; Adjust the first element to be the same as the lower limit.
15928         (if (and (not (atom (car range)))
15929                  (< (cdar range) (car active)))
15930             (setcdr (car range) (1- (car active))))
15931         ;; Then we want to peel off any elements that are higher
15932         ;; than the upper active limit.
15933         (let ((srange range))
15934           ;; Go past all legal elements.
15935           (while (and (cdr srange)
15936                       (<= (or (and (atom (cadr srange))
15937                                    (cadr srange))
15938                               (caadr srange)) (cdr active)))
15939             (setq srange (cdr srange)))
15940           (if (cdr srange)
15941               ;; Nuke all remaining illegal elements.
15942               (setcdr srange nil))
15943
15944           ;; Adjust the final element.
15945           (if (and (not (atom (car srange)))
15946                    (> (cdar srange) (cdr active)))
15947               (setcdr (car srange) (cdr active))))
15948         ;; Compute the number of unread articles.
15949         (while range
15950           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15951                                       (cdar range)))
15952                               (or (and (atom (car range)) (car range))
15953                                   (caar range)))))
15954           (setq range (cdr range)))
15955         (setq num (max 0 (- (cdr active) num)))))
15956       ;; Set the number of unread articles.
15957       (when info
15958         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15959       num)))
15960
15961 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15962 ;; and compute how many unread articles there are in each group.
15963 (defun gnus-get-unread-articles (&optional level)
15964   (let* ((newsrc (cdr gnus-newsrc-alist))
15965          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15966          (foreign-level
15967           (min
15968            (cond ((and gnus-activate-foreign-newsgroups
15969                        (not (numberp gnus-activate-foreign-newsgroups)))
15970                   (1+ gnus-level-subscribed))
15971                  ((numberp gnus-activate-foreign-newsgroups)
15972                   gnus-activate-foreign-newsgroups)
15973                  (t 0))
15974            level))
15975          info group active method)
15976     (gnus-message 5 "Checking new news...")
15977
15978     (while newsrc
15979       (setq active (gnus-active (setq group (gnus-info-group
15980                                              (setq info (pop newsrc))))))
15981
15982       ;; Check newsgroups.  If the user doesn't want to check them, or
15983       ;; they can't be checked (for instance, if the news server can't
15984       ;; be reached) we just set the number of unread articles in this
15985       ;; newsgroup to t.  This means that Gnus thinks that there are
15986       ;; unread articles, but it has no idea how many.
15987       (if (and (setq method (gnus-info-method info))
15988                (not (gnus-server-equal
15989                      gnus-select-method
15990                      (setq method (gnus-server-get-method nil method))))
15991                (not (gnus-secondary-method-p method)))
15992           ;; These groups are foreign.  Check the level.
15993           (when (<= (gnus-info-level info) foreign-level)
15994             (setq active (gnus-activate-group group 'scan))
15995             (unless (inline (gnus-virtual-group-p group))
15996               (inline (gnus-close-group group)))
15997             (when (fboundp (intern (concat (symbol-name (car method))
15998                                            "-request-update-info")))
15999               (inline (gnus-request-update-info info method))))
16000         ;; These groups are native or secondary.
16001         (when (and (<= (gnus-info-level info) level)
16002                    (not gnus-read-active-file))
16003           (setq active (gnus-activate-group group 'scan))
16004           (inline (gnus-close-group group))))
16005
16006       ;; Get the number of unread articles in the group.
16007       (if active
16008           (inline (gnus-get-unread-articles-in-group info active))
16009         ;; The group couldn't be reached, so we nix out the number of
16010         ;; unread articles and stuff.
16011         (gnus-set-active group nil)
16012         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
16013
16014     (gnus-message 5 "Checking new news...done")))
16015
16016 ;; Create a hash table out of the newsrc alist.  The `car's of the
16017 ;; alist elements are used as keys.
16018 (defun gnus-make-hashtable-from-newsrc-alist ()
16019   (let ((alist gnus-newsrc-alist)
16020         (ohashtb gnus-newsrc-hashtb)
16021         prev)
16022     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
16023     (setq alist
16024           (setq prev (setq gnus-newsrc-alist
16025                            (if (equal (caar gnus-newsrc-alist)
16026                                       "dummy.group")
16027                                gnus-newsrc-alist
16028                              (cons (list "dummy.group" 0 nil) alist)))))
16029     (while alist
16030       (gnus-sethash
16031        (caar alist)
16032        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
16033              prev)
16034        gnus-newsrc-hashtb)
16035       (setq prev alist
16036             alist (cdr alist)))))
16037
16038 (defun gnus-make-hashtable-from-killed ()
16039   "Create a hash table from the killed and zombie lists."
16040   (let ((lists '(gnus-killed-list gnus-zombie-list))
16041         list)
16042     (setq gnus-killed-hashtb
16043           (gnus-make-hashtable
16044            (+ (length gnus-killed-list) (length gnus-zombie-list))))
16045     (while (setq list (pop lists))
16046       (setq list (symbol-value list))
16047       (while list
16048         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
16049
16050 (defun gnus-activate-group (group &optional scan dont-check &optional method)
16051   ;; Check whether a group has been activated or not.
16052   ;; If SCAN, request a scan of that group as well.
16053   (let ((method (or method (gnus-find-method-for-group group)))
16054         active)
16055     (and (gnus-check-server method)
16056          ;; We escape all bugs and quit here to make it possible to
16057          ;; continue if a group is so out-there that it reports bugs
16058          ;; and stuff.
16059          (progn
16060            (and scan
16061                 (gnus-check-backend-function 'request-scan (car method))
16062                 (gnus-request-scan group method))
16063            t)
16064          (condition-case ()
16065              (gnus-request-group group dont-check)
16066         ;   (error nil)
16067            (quit nil))
16068          (save-excursion
16069            (set-buffer nntp-server-buffer)
16070            (goto-char (point-min))
16071            ;; Parse the result we got from `gnus-request-group'.
16072            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
16073                 (progn
16074                   (goto-char (match-beginning 1))
16075                   (gnus-set-active
16076                    group (setq active (cons (read (current-buffer))
16077                                             (read (current-buffer)))))
16078                   ;; Return the new active info.
16079                   active))))))
16080
16081 (defun gnus-update-read-articles (group unread)
16082   "Update the list of read and ticked articles in GROUP using the
16083 UNREAD and TICKED lists.
16084 Note: UNSELECTED has to be sorted over `<'.
16085 Returns whether the updating was successful."
16086   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
16087          (entry (gnus-gethash group gnus-newsrc-hashtb))
16088          (info (nth 2 entry))
16089          (prev 1)
16090          (unread (sort (copy-sequence unread) '<))
16091          read)
16092     (if (or (not info) (not active))
16093         ;; There is no info on this group if it was, in fact,
16094         ;; killed.  Gnus stores no information on killed groups, so
16095         ;; there's nothing to be done.
16096         ;; One could store the information somewhere temporarily,
16097         ;; perhaps...  Hmmm...
16098         ()
16099       ;; Remove any negative articles numbers.
16100       (while (and unread (< (car unread) 0))
16101         (setq unread (cdr unread)))
16102       ;; Remove any expired article numbers
16103       (while (and unread (< (car unread) (car active)))
16104         (setq unread (cdr unread)))
16105       ;; Compute the ranges of read articles by looking at the list of
16106       ;; unread articles.
16107       (while unread
16108         (if (/= (car unread) prev)
16109             (setq read (cons (if (= prev (1- (car unread))) prev
16110                                (cons prev (1- (car unread)))) read)))
16111         (setq prev (1+ (car unread)))
16112         (setq unread (cdr unread)))
16113       (when (<= prev (cdr active))
16114         (setq read (cons (cons prev (cdr active)) read)))
16115       ;; Enter this list into the group info.
16116       (gnus-info-set-read
16117        info (if (> (length read) 1) (nreverse read) read))
16118       ;; Set the number of unread articles in gnus-newsrc-hashtb.
16119       (gnus-get-unread-articles-in-group info (gnus-active group))
16120       t)))
16121
16122 (defun gnus-make-articles-unread (group articles)
16123   "Mark ARTICLES in GROUP as unread."
16124   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
16125                           (gnus-gethash (gnus-group-real-name group)
16126                                         gnus-newsrc-hashtb))))
16127          (ranges (gnus-info-read info))
16128          news article)
16129     (while articles
16130       (when (gnus-member-of-range
16131              (setq article (pop articles)) ranges)
16132         (setq news (cons article news))))
16133     (when news
16134       (gnus-info-set-read
16135        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
16136       (gnus-group-update-group group t))))
16137
16138 ;; Enter all dead groups into the hashtb.
16139 (defun gnus-update-active-hashtb-from-killed ()
16140   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
16141         (lists (list gnus-killed-list gnus-zombie-list))
16142         killed)
16143     (while lists
16144       (setq killed (car lists))
16145       (while killed
16146         (gnus-sethash (car killed) nil hashtb)
16147         (setq killed (cdr killed)))
16148       (setq lists (cdr lists)))))
16149
16150 (defun gnus-get-killed-groups ()
16151   "Go through the active hashtb and all all unknown groups as killed."
16152   ;; First make sure active file has been read.
16153   (unless (gnus-read-active-file-p)
16154     (let ((gnus-read-active-file t))
16155       (gnus-read-active-file)))
16156   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
16157   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
16158   (mapatoms
16159    (lambda (sym)
16160      (let ((groups 0)
16161            (group (symbol-name sym)))
16162        (if (or (null group)
16163                (gnus-gethash group gnus-killed-hashtb)
16164                (gnus-gethash group gnus-newsrc-hashtb))
16165            ()
16166          (let ((do-sub (gnus-matches-options-n group)))
16167            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
16168                ()
16169              (setq groups (1+ groups))
16170              (setq gnus-killed-list
16171                    (cons group gnus-killed-list))
16172              (gnus-sethash group group gnus-killed-hashtb))))))
16173    gnus-active-hashtb))
16174
16175 ;; Get the active file(s) from the backend(s).
16176 (defun gnus-read-active-file ()
16177   (gnus-group-set-mode-line)
16178   (let ((methods 
16179          (append
16180           (if (gnus-check-server gnus-select-method)
16181               ;; The native server is available.
16182               (cons gnus-select-method gnus-secondary-select-methods)
16183             ;; The native server is down, so we just do the
16184             ;; secondary ones.
16185             gnus-secondary-select-methods)
16186           ;; Also read from the archive server.
16187           (when gnus-message-archive-method
16188             (list "archive"))))
16189         list-type)
16190     (setq gnus-have-read-active-file nil)
16191     (save-excursion
16192       (set-buffer nntp-server-buffer)
16193       (while methods
16194         (let* ((method (if (stringp (car methods))
16195                            (gnus-server-get-method nil (car methods))
16196                          (car methods)))
16197                (where (nth 1 method))
16198                (mesg (format "Reading active file%s via %s..."
16199                              (if (and where (not (zerop (length where))))
16200                                  (concat " from " where) "")
16201                              (car method))))
16202           (gnus-message 5 mesg)
16203           (when (gnus-check-server method)
16204             ;; Request that the backend scan its incoming messages.
16205             (and (gnus-check-backend-function 'request-scan (car method))
16206                  (gnus-request-scan nil method))
16207             (cond
16208              ((and (eq gnus-read-active-file 'some)
16209                    (gnus-check-backend-function 'retrieve-groups (car method)))
16210               (let ((newsrc (cdr gnus-newsrc-alist))
16211                     (gmethod (gnus-server-get-method nil method))
16212                     groups info)
16213                 (while (setq info (pop newsrc))
16214                   (when (gnus-server-equal
16215                          (gnus-find-method-for-group 
16216                           (gnus-info-group info) info)
16217                          gmethod)
16218                     (push (gnus-group-real-name (gnus-info-group info)) 
16219                           groups)))
16220                 (when groups
16221                   (gnus-check-server method)
16222                   (setq list-type (gnus-retrieve-groups groups method))
16223                   (cond
16224                    ((not list-type)
16225                     (gnus-error
16226                      1.2 "Cannot read partial active file from %s server."
16227                      (car method)))
16228                    ((eq list-type 'active)
16229                     (gnus-active-to-gnus-format method gnus-active-hashtb))
16230                    (t
16231                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
16232              (t
16233               (if (not (gnus-request-list method))
16234                   (unless (equal method gnus-message-archive-method)
16235                     (gnus-error 1 "Cannot read active file from %s server."
16236                                 (car method)))
16237                 (gnus-message 5 mesg)
16238                 (gnus-active-to-gnus-format method gnus-active-hashtb)
16239                 ;; We mark this active file as read.
16240                 (push method gnus-have-read-active-file)
16241                 (gnus-message 5 "%sdone" mesg))))))
16242         (setq methods (cdr methods))))))
16243
16244 ;; Read an active file and place the results in `gnus-active-hashtb'.
16245 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
16246   (unless method
16247     (setq method gnus-select-method))
16248   (let ((cur (current-buffer))
16249         (hashtb (or hashtb
16250                     (if (and gnus-active-hashtb
16251                              (not (equal method gnus-select-method)))
16252                         gnus-active-hashtb
16253                       (setq gnus-active-hashtb
16254                             (if (equal method gnus-select-method)
16255                                 (gnus-make-hashtable
16256                                  (count-lines (point-min) (point-max)))
16257                               (gnus-make-hashtable 4096)))))))
16258     ;; Delete unnecessary lines.
16259     (goto-char (point-min))
16260     (while (search-forward "\nto." nil t)
16261       (delete-region (1+ (match-beginning 0))
16262                      (progn (forward-line 1) (point))))
16263     (or (string= gnus-ignored-newsgroups "")
16264         (progn
16265           (goto-char (point-min))
16266           (delete-matching-lines gnus-ignored-newsgroups)))
16267     ;; Make the group names readable as a lisp expression even if they
16268     ;; contain special characters.
16269     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
16270     (goto-char (point-max))
16271     (while (re-search-backward "[][';?()#]" nil t)
16272       (insert ?\\))
16273     ;; If these are groups from a foreign select method, we insert the
16274     ;; group prefix in front of the group names.
16275     (and method (not (gnus-server-equal
16276                       (gnus-server-get-method nil method)
16277                       (gnus-server-get-method nil gnus-select-method)))
16278          (let ((prefix (gnus-group-prefixed-name "" method)))
16279            (goto-char (point-min))
16280            (while (and (not (eobp))
16281                        (progn (insert prefix)
16282                               (zerop (forward-line 1)))))))
16283     ;; Store the active file in a hash table.
16284     (goto-char (point-min))
16285     (if (string-match "%[oO]" gnus-group-line-format)
16286         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16287         ;; If we want information on moderated groups, we use this
16288         ;; loop...
16289         (let* ((mod-hashtb (make-vector 7 0))
16290                (m (intern "m" mod-hashtb))
16291                group max min)
16292           (while (not (eobp))
16293             (condition-case nil
16294                 (progn
16295                   (narrow-to-region (point) (gnus-point-at-eol))
16296                   (setq group (let ((obarray hashtb)) (read cur)))
16297                   (if (and (numberp (setq max (read cur)))
16298                            (numberp (setq min (read cur)))
16299                            (progn
16300                              (skip-chars-forward " \t")
16301                              (not
16302                               (or (= (following-char) ?=)
16303                                   (= (following-char) ?x)
16304                                   (= (following-char) ?j)))))
16305                       (set group (cons min max))
16306                     (set group nil))
16307                   ;; Enter moderated groups into a list.
16308                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16309                       (setq gnus-moderated-list
16310                             (cons (symbol-name group) gnus-moderated-list))))
16311               (error
16312                (and group
16313                     (symbolp group)
16314                     (set group nil))))
16315             (widen)
16316             (forward-line 1)))
16317       ;; And if we do not care about moderation, we use this loop,
16318       ;; which is faster.
16319       (let (group max min)
16320         (while (not (eobp))
16321           (condition-case ()
16322               (progn
16323                 (narrow-to-region (point) (gnus-point-at-eol))
16324                 ;; group gets set to a symbol interned in the hash table
16325                 ;; (what a hack!!) - jwz
16326                 (setq group (let ((obarray hashtb)) (read cur)))
16327                 (if (and (numberp (setq max (read cur)))
16328                          (numberp (setq min (read cur)))
16329                          (progn
16330                            (skip-chars-forward " \t")
16331                            (not
16332                             (or (= (following-char) ?=)
16333                                 (= (following-char) ?x)
16334                                 (= (following-char) ?j)))))
16335                     (set group (cons min max))
16336                   (set group nil)))
16337             (error
16338              (progn
16339                (and group
16340                     (symbolp group)
16341                     (set group nil))
16342                (or ignore-errors
16343                    (gnus-message 3 "Warning - illegal active: %s"
16344                                  (buffer-substring
16345                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16346           (widen)
16347           (forward-line 1))))))
16348
16349 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16350   ;; Parse a "groups" active file.
16351   (let ((cur (current-buffer))
16352         (hashtb (or hashtb
16353                     (if (and method gnus-active-hashtb)
16354                         gnus-active-hashtb
16355                       (setq gnus-active-hashtb
16356                             (gnus-make-hashtable
16357                              (count-lines (point-min) (point-max)))))))
16358         (prefix (and method
16359                      (not (gnus-server-equal
16360                            (gnus-server-get-method nil method)
16361                            (gnus-server-get-method nil gnus-select-method)))
16362                      (gnus-group-prefixed-name "" method))))
16363
16364     (goto-char (point-min))
16365     ;; We split this into to separate loops, one with the prefix
16366     ;; and one without to speed the reading up somewhat.
16367     (if prefix
16368         (let (min max opoint group)
16369           (while (not (eobp))
16370             (condition-case ()
16371                 (progn
16372                   (read cur) (read cur)
16373                   (setq min (read cur)
16374                         max (read cur)
16375                         opoint (point))
16376                   (skip-chars-forward " \t")
16377                   (insert prefix)
16378                   (goto-char opoint)
16379                   (set (let ((obarray hashtb)) (read cur))
16380                        (cons min max)))
16381               (error (and group (symbolp group) (set group nil))))
16382             (forward-line 1)))
16383       (let (min max group)
16384         (while (not (eobp))
16385           (condition-case ()
16386               (if (= (following-char) ?2)
16387                   (progn
16388                     (read cur) (read cur)
16389                     (setq min (read cur)
16390                           max (read cur))
16391                     (set (setq group (let ((obarray hashtb)) (read cur)))
16392                          (cons min max))))
16393             (error (and group (symbolp group) (set group nil))))
16394           (forward-line 1))))))
16395
16396 (defun gnus-read-newsrc-file (&optional force)
16397   "Read startup file.
16398 If FORCE is non-nil, the .newsrc file is read."
16399   ;; Reset variables that might be defined in the .newsrc.eld file.
16400   (let ((variables gnus-variable-list))
16401     (while variables
16402       (set (car variables) nil)
16403       (setq variables (cdr variables))))
16404   (let* ((newsrc-file gnus-current-startup-file)
16405          (quick-file (concat newsrc-file ".el")))
16406     (save-excursion
16407       ;; We always load the .newsrc.eld file.  If always contains
16408       ;; much information that can not be gotten from the .newsrc
16409       ;; file (ticked articles, killed groups, foreign methods, etc.)
16410       (gnus-read-newsrc-el-file quick-file)
16411
16412       (if (and (file-exists-p gnus-current-startup-file)
16413                (or force
16414                    (and (file-newer-than-file-p newsrc-file quick-file)
16415                         (file-newer-than-file-p newsrc-file
16416                                                 (concat quick-file "d")))
16417                    (not gnus-newsrc-alist)))
16418           ;; We read the .newsrc file.  Note that if there if a
16419           ;; .newsrc.eld file exists, it has already been read, and
16420           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16421           ;; the .newsrc file, Gnus will only use the information it
16422           ;; can find there for changing the data already read -
16423           ;; ie. reading the .newsrc file will not trash the data
16424           ;; already read (except for read articles).
16425           (save-excursion
16426             (gnus-message 5 "Reading %s..." newsrc-file)
16427             (set-buffer (find-file-noselect newsrc-file))
16428             (buffer-disable-undo (current-buffer))
16429             (gnus-newsrc-to-gnus-format)
16430             (kill-buffer (current-buffer))
16431             (gnus-message 5 "Reading %s...done" newsrc-file)))
16432
16433       ;; Read any slave files.
16434       (unless gnus-slave
16435         (gnus-master-read-slave-newsrc))
16436       
16437       ;; Convert old to new.
16438       (gnus-convert-old-newsrc))))
16439
16440 (defun gnus-continuum-version (version)
16441   "Return VERSION as a floating point number."
16442   (when (or (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16443             (string-match "^\\(.?\\)gnus-\\([0-9.]+\\)$" version))
16444     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16445            (number (match-string 2 version))
16446            major minor least)
16447       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16448       (setq major (string-to-number (match-string 1 number)))
16449       (setq minor (string-to-number (match-string 2 number)))
16450       (setq least (if (match-beginning 3)
16451                       (string-to-number (match-string 3 number))
16452                     0))
16453       (string-to-number
16454        (if (zerop major)
16455            (format "%s00%02d%02d"
16456                    (cond 
16457                     ((member alpha '("(ding)" "d")) "4.99")
16458                     ((member alpha '("September" "s")) "5.01")
16459                     ((member alpha '("Red" "r")) "5.03"))
16460                    minor least)
16461          (format "%d.%02d%02d" major minor least))))))
16462
16463 (defun gnus-convert-old-newsrc ()
16464   "Convert old newsrc into the new format, if needed."
16465   (let ((fcv (and gnus-newsrc-file-version
16466                   (gnus-continuum-version gnus-newsrc-file-version))))
16467     (cond
16468      ;; No .newsrc.eld file was loaded.
16469      ((null fcv) nil)
16470      ;; Gnus 5 .newsrc.eld was loaded.
16471      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16472       (gnus-convert-old-ticks)))))
16473
16474 (defun gnus-convert-old-ticks ()
16475   (let ((newsrc (cdr gnus-newsrc-alist))
16476         marks info dormant ticked)
16477     (while (setq info (pop newsrc))
16478       (when (setq marks (gnus-info-marks info))
16479         (setq dormant (cdr (assq 'dormant marks))
16480               ticked (cdr (assq 'tick marks)))
16481         (when (or dormant ticked)
16482           (gnus-info-set-read
16483            info
16484            (gnus-add-to-range
16485             (gnus-info-read info)
16486             (nconc (gnus-uncompress-range dormant)
16487                    (gnus-uncompress-range ticked)))))))))
16488
16489 (defun gnus-read-newsrc-el-file (file)
16490   (let ((ding-file (concat file "d")))
16491     ;; We always, always read the .eld file.
16492     (gnus-message 5 "Reading %s..." ding-file)
16493     (let (gnus-newsrc-assoc)
16494       (condition-case nil
16495           (load ding-file t t t)
16496         (error
16497          (gnus-error 1 "Error in %s" ding-file)))
16498       (when gnus-newsrc-assoc
16499         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16500     (gnus-make-hashtable-from-newsrc-alist)
16501     (when (file-newer-than-file-p file ding-file)
16502       ;; Old format quick file
16503       (gnus-message 5 "Reading %s..." file)
16504       ;; The .el file is newer than the .eld file, so we read that one
16505       ;; as well.
16506       (gnus-read-old-newsrc-el-file file))))
16507
16508 ;; Parse the old-style quick startup file
16509 (defun gnus-read-old-newsrc-el-file (file)
16510   (let (newsrc killed marked group m info)
16511     (prog1
16512         (let ((gnus-killed-assoc nil)
16513               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16514           (prog1
16515               (condition-case nil
16516                   (load file t t t)
16517                 (error nil))
16518             (setq newsrc gnus-newsrc-assoc
16519                   killed gnus-killed-assoc
16520                   marked gnus-marked-assoc)))
16521       (setq gnus-newsrc-alist nil)
16522       (while (setq group (pop newsrc))
16523         (if (setq info (gnus-get-info (car group)))
16524             (progn
16525               (gnus-info-set-read info (cddr group))
16526               (gnus-info-set-level
16527                info (if (nth 1 group) gnus-level-default-subscribed
16528                       gnus-level-default-unsubscribed))
16529               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16530           (push (setq info
16531                       (list (car group)
16532                             (if (nth 1 group) gnus-level-default-subscribed
16533                               gnus-level-default-unsubscribed)
16534                             (cddr group)))
16535                 gnus-newsrc-alist))
16536         ;; Copy marks into info.
16537         (when (setq m (assoc (car group) marked))
16538           (unless (nthcdr 3 info)
16539             (nconc info (list nil)))
16540           (gnus-info-set-marks
16541            info (list (cons 'tick (gnus-compress-sequence 
16542                                    (sort (cdr m) '<) t))))))
16543       (setq newsrc killed)
16544       (while newsrc
16545         (setcar newsrc (caar newsrc))
16546         (setq newsrc (cdr newsrc)))
16547       (setq gnus-killed-list killed))
16548     ;; The .el file version of this variable does not begin with
16549     ;; "options", while the .eld version does, so we just add it if it
16550     ;; isn't there.
16551     (and
16552      gnus-newsrc-options
16553      (progn
16554        (and (not (string-match "^ *options" gnus-newsrc-options))
16555             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16556        (and (not (string-match "\n$" gnus-newsrc-options))
16557             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16558        ;; Finally, if we read some options lines, we parse them.
16559        (or (string= gnus-newsrc-options "")
16560            (gnus-newsrc-parse-options gnus-newsrc-options))))
16561
16562     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16563     (gnus-make-hashtable-from-newsrc-alist)))
16564
16565 (defun gnus-make-newsrc-file (file)
16566   "Make server dependent file name by catenating FILE and server host name."
16567   (let* ((file (expand-file-name file nil))
16568          (real-file (concat file "-" (nth 1 gnus-select-method))))
16569     (if (or (file-exists-p real-file)
16570             (file-exists-p (concat real-file ".el"))
16571             (file-exists-p (concat real-file ".eld")))
16572         real-file file)))
16573
16574 (defun gnus-newsrc-to-gnus-format ()
16575   (setq gnus-newsrc-options "")
16576   (setq gnus-newsrc-options-n nil)
16577
16578   (or gnus-active-hashtb
16579       (setq gnus-active-hashtb (make-vector 4095 0)))
16580   (let ((buf (current-buffer))
16581         (already-read (> (length gnus-newsrc-alist) 1))
16582         group subscribed options-symbol newsrc Options-symbol
16583         symbol reads num1)
16584     (goto-char (point-min))
16585     ;; We intern the symbol `options' in the active hashtb so that we
16586     ;; can `eq' against it later.
16587     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16588     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16589
16590     (while (not (eobp))
16591       ;; We first read the first word on the line by narrowing and
16592       ;; then reading into `gnus-active-hashtb'.  Most groups will
16593       ;; already exist in that hashtb, so this will save some string
16594       ;; space.
16595       (narrow-to-region
16596        (point)
16597        (progn (skip-chars-forward "^ \t!:\n") (point)))
16598       (goto-char (point-min))
16599       (setq symbol
16600             (and (/= (point-min) (point-max))
16601                  (let ((obarray gnus-active-hashtb)) (read buf))))
16602       (widen)
16603       ;; Now, the symbol we have read is either `options' or a group
16604       ;; name.  If it is an options line, we just add it to a string.
16605       (cond
16606        ((or (eq symbol options-symbol)
16607             (eq symbol Options-symbol))
16608         (setq gnus-newsrc-options
16609               ;; This concating is quite inefficient, but since our
16610               ;; thorough studies show that approx 99.37% of all
16611               ;; .newsrc files only contain a single options line, we
16612               ;; don't give a damn, frankly, my dear.
16613               (concat gnus-newsrc-options
16614                       (buffer-substring
16615                        (gnus-point-at-bol)
16616                        ;; Options may continue on the next line.
16617                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16618                                 (progn (beginning-of-line) (point)))
16619                            (point)))))
16620         (forward-line -1))
16621        (symbol
16622         ;; Group names can be just numbers.  
16623         (when (numberp symbol) 
16624           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16625         (or (boundp symbol) (set symbol nil))
16626         ;; It was a group name.
16627         (setq subscribed (= (following-char) ?:)
16628               group (symbol-name symbol)
16629               reads nil)
16630         (if (eolp)
16631             ;; If the line ends here, this is clearly a buggy line, so
16632             ;; we put point a the beginning of line and let the cond
16633             ;; below do the error handling.
16634             (beginning-of-line)
16635           ;; We skip to the beginning of the ranges.
16636           (skip-chars-forward "!: \t"))
16637         ;; We are now at the beginning of the list of read articles.
16638         ;; We read them range by range.
16639         (while
16640             (cond
16641              ((looking-at "[0-9]+")
16642               ;; We narrow and read a number instead of buffer-substring/
16643               ;; string-to-int because it's faster.  narrow/widen is
16644               ;; faster than save-restriction/narrow, and save-restriction
16645               ;; produces a garbage object.
16646               (setq num1 (progn
16647                            (narrow-to-region (match-beginning 0) (match-end 0))
16648                            (read buf)))
16649               (widen)
16650               ;; If the next character is a dash, then this is a range.
16651               (if (= (following-char) ?-)
16652                   (progn
16653                     ;; We read the upper bound of the range.
16654                     (forward-char 1)
16655                     (if (not (looking-at "[0-9]+"))
16656                         ;; This is a buggy line, by we pretend that
16657                         ;; it's kinda OK.  Perhaps the user should be
16658                         ;; dinged?
16659                         (setq reads (cons num1 reads))
16660                       (setq reads
16661                             (cons
16662                              (cons num1
16663                                    (progn
16664                                      (narrow-to-region (match-beginning 0)
16665                                                        (match-end 0))
16666                                      (read buf)))
16667                              reads))
16668                       (widen)))
16669                 ;; It was just a simple number, so we add it to the
16670                 ;; list of ranges.
16671                 (setq reads (cons num1 reads)))
16672               ;; If the next char in ?\n, then we have reached the end
16673               ;; of the line and return nil.
16674               (/= (following-char) ?\n))
16675              ((= (following-char) ?\n)
16676               ;; End of line, so we end.
16677               nil)
16678              (t
16679               ;; Not numbers and not eol, so this might be a buggy
16680               ;; line...
16681               (or (eobp)
16682                   ;; If it was eob instead of ?\n, we allow it.
16683                   (progn
16684                     ;; The line was buggy.
16685                     (setq group nil)
16686                     (gnus-error 3.1 "Mangled line: %s"
16687                                 (buffer-substring (gnus-point-at-bol)
16688                                                   (gnus-point-at-eol)))))
16689               nil))
16690           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16691           ;; we allow them, because it's a common mistake to put a
16692           ;; space after the comma.
16693           (skip-chars-forward ", "))
16694
16695         ;; We have already read .newsrc.eld, so we gently update the
16696         ;; data in the hash table with the information we have just
16697         ;; read.
16698         (when group
16699           (let ((info (gnus-get-info group))
16700                 level)
16701             (if info
16702                 ;; There is an entry for this file in the alist.
16703                 (progn
16704                   (gnus-info-set-read info (nreverse reads))
16705                   ;; We update the level very gently.  In fact, we
16706                   ;; only change it if there's been a status change
16707                   ;; from subscribed to unsubscribed, or vice versa.
16708                   (setq level (gnus-info-level info))
16709                   (cond ((and (<= level gnus-level-subscribed)
16710                               (not subscribed))
16711                          (setq level (if reads
16712                                          gnus-level-default-unsubscribed
16713                                        (1+ gnus-level-default-unsubscribed))))
16714                         ((and (> level gnus-level-subscribed) subscribed)
16715                          (setq level gnus-level-default-subscribed)))
16716                   (gnus-info-set-level info level))
16717               ;; This is a new group.
16718               (setq info (list group
16719                                (if subscribed
16720                                    gnus-level-default-subscribed
16721                                  (if reads
16722                                      (1+ gnus-level-subscribed)
16723                                    gnus-level-default-unsubscribed))
16724                                (nreverse reads))))
16725             (setq newsrc (cons info newsrc))))))
16726       (forward-line 1))
16727
16728     (setq newsrc (nreverse newsrc))
16729
16730     (if (not already-read)
16731         ()
16732       ;; We now have two newsrc lists - `newsrc', which is what we
16733       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16734       ;; what we've read from .newsrc.eld.  We have to merge these
16735       ;; lists.  We do this by "attaching" any (foreign) groups in the
16736       ;; gnus-newsrc-alist to the (native) group that precedes them.
16737       (let ((rc (cdr gnus-newsrc-alist))
16738             (prev gnus-newsrc-alist)
16739             entry mentry)
16740         (while rc
16741           (or (null (nth 4 (car rc)))   ; It's a native group.
16742               (assoc (caar rc) newsrc) ; It's already in the alist.
16743               (if (setq entry (assoc (caar prev) newsrc))
16744                   (setcdr (setq mentry (memq entry newsrc))
16745                           (cons (car rc) (cdr mentry)))
16746                 (setq newsrc (cons (car rc) newsrc))))
16747           (setq prev rc
16748                 rc (cdr rc)))))
16749
16750     (setq gnus-newsrc-alist newsrc)
16751     ;; We make the newsrc hashtb.
16752     (gnus-make-hashtable-from-newsrc-alist)
16753
16754     ;; Finally, if we read some options lines, we parse them.
16755     (or (string= gnus-newsrc-options "")
16756         (gnus-newsrc-parse-options gnus-newsrc-options))))
16757
16758 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16759 ;; The return value will be a list on the form
16760 ;; ((regexp1 . ignore)
16761 ;;  (regexp2 . subscribe)...)
16762 ;; When handling new newsgroups, groups that match a `ignore' regexp
16763 ;; will be ignored, and groups that match a `subscribe' regexp will be
16764 ;; subscribed.  A line like
16765 ;; options -n !all rec.all
16766 ;; will lead to a list that looks like
16767 ;; (("^rec\\..+" . subscribe)
16768 ;;  ("^.+" . ignore))
16769 ;; So all "rec.*" groups will be subscribed, while all the other
16770 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16771 ;; different from "options -n rec.all !all".
16772 (defun gnus-newsrc-parse-options (options)
16773   (let (out eol)
16774     (save-excursion
16775       (gnus-set-work-buffer)
16776       (insert (regexp-quote options))
16777       ;; First we treat all continuation lines.
16778       (goto-char (point-min))
16779       (while (re-search-forward "\n[ \t]+" nil t)
16780         (replace-match " " t t))
16781       ;; Then we transform all "all"s into ".+"s.
16782       (goto-char (point-min))
16783       (while (re-search-forward "\\ball\\b" nil t)
16784         (replace-match ".+" t t))
16785       (goto-char (point-min))
16786       ;; We remove all other options than the "-n" ones.
16787       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16788         (replace-match " ")
16789         (forward-char -1))
16790       (goto-char (point-min))
16791
16792       ;; We are only interested in "options -n" lines - we
16793       ;; ignore the other option lines.
16794       (while (re-search-forward "[ \t]-n" nil t)
16795         (setq eol
16796               (or (save-excursion
16797                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16798                          (- (point) 2)))
16799                   (gnus-point-at-eol)))
16800         ;; Search for all "words"...
16801         (while (re-search-forward "[^ \t,\n]+" eol t)
16802           (if (= (char-after (match-beginning 0)) ?!)
16803               ;; If the word begins with a bang (!), this is a "not"
16804               ;; spec.  We put this spec (minus the bang) and the
16805               ;; symbol `ignore' into the list.
16806               (setq out (cons (cons (concat
16807                                      "^" (buffer-substring
16808                                           (1+ (match-beginning 0))
16809                                           (match-end 0)))
16810                                     'ignore) out))
16811             ;; There was no bang, so this is a "yes" spec.
16812             (setq out (cons (cons (concat "^" (match-string 0))
16813                                   'subscribe) out)))))
16814
16815       (setq gnus-newsrc-options-n out))))
16816
16817 (defun gnus-save-newsrc-file (&optional force)
16818   "Save .newsrc file."
16819   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16820   ;; from the variable gnus-newsrc-alist.
16821   (when (and (or gnus-newsrc-alist gnus-killed-list)
16822              gnus-current-startup-file)
16823     (save-excursion
16824       (if (and (or gnus-use-dribble-file gnus-slave)
16825                (not force)
16826                (or (not gnus-dribble-buffer)
16827                    (not (buffer-name gnus-dribble-buffer))
16828                    (zerop (save-excursion
16829                             (set-buffer gnus-dribble-buffer)
16830                             (buffer-size)))))
16831           (gnus-message 4 "(No changes need to be saved)")
16832         (run-hooks 'gnus-save-newsrc-hook)
16833         (if gnus-slave
16834             (gnus-slave-save-newsrc)
16835           ;; Save .newsrc.
16836           (when gnus-save-newsrc-file
16837             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16838             (gnus-gnus-to-newsrc-format)
16839             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16840           ;; Save .newsrc.eld.
16841           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16842           (make-local-variable 'version-control)
16843           (setq version-control 'never)
16844           (setq buffer-file-name
16845                 (concat gnus-current-startup-file ".eld"))
16846           (setq default-directory (file-name-directory buffer-file-name))
16847           (gnus-add-current-to-buffer-list)
16848           (buffer-disable-undo (current-buffer))
16849           (erase-buffer)
16850           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16851           (gnus-gnus-to-quick-newsrc-format)
16852           (run-hooks 'gnus-save-quick-newsrc-hook)
16853           (save-buffer)
16854           (kill-buffer (current-buffer))
16855           (gnus-message
16856            5 "Saving %s.eld...done" gnus-current-startup-file))
16857         (gnus-dribble-delete-file)
16858         (gnus-group-set-mode-line)))))
16859
16860 (defun gnus-gnus-to-quick-newsrc-format ()
16861   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16862   (insert ";; Gnus startup file.\n")
16863   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16864   (insert ";; to read .newsrc.\n")
16865   (insert "(setq gnus-newsrc-file-version "
16866           (prin1-to-string gnus-version) ")\n")
16867   (let ((variables
16868          (if gnus-save-killed-list gnus-variable-list
16869            ;; Remove the `gnus-killed-list' from the list of variables
16870            ;; to be saved, if required.
16871            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16872         ;; Peel off the "dummy" group.
16873         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16874         variable)
16875     ;; Insert the variables into the file.
16876     (while variables
16877       (when (and (boundp (setq variable (pop variables)))
16878                  (symbol-value variable))
16879         (insert "(setq " (symbol-name variable) " '")
16880         (prin1 (symbol-value variable) (current-buffer))
16881         (insert ")\n")))))
16882
16883 (defun gnus-gnus-to-newsrc-format ()
16884   ;; Generate and save the .newsrc file.
16885   (save-excursion
16886     (set-buffer (create-file-buffer gnus-current-startup-file))
16887     (let ((newsrc (cdr gnus-newsrc-alist))
16888           (standard-output (current-buffer))
16889           info ranges range method)
16890       (setq buffer-file-name gnus-current-startup-file)
16891       (setq default-directory (file-name-directory buffer-file-name))
16892       (buffer-disable-undo (current-buffer))
16893       (erase-buffer)
16894       ;; Write options.
16895       (if gnus-newsrc-options (insert gnus-newsrc-options))
16896       ;; Write subscribed and unsubscribed.
16897       (while (setq info (pop newsrc))
16898         ;; Don't write foreign groups to .newsrc.
16899         (when (or (null (setq method (gnus-info-method info)))
16900                   (equal method "native")
16901                   (gnus-server-equal method gnus-select-method))
16902           (insert (gnus-info-group info)
16903                   (if (> (gnus-info-level info) gnus-level-subscribed)
16904                       "!" ":"))
16905           (when (setq ranges (gnus-info-read info))
16906             (insert " ")
16907             (if (not (listp (cdr ranges)))
16908                 (if (= (car ranges) (cdr ranges))
16909                     (princ (car ranges))
16910                   (princ (car ranges))
16911                   (insert "-")
16912                   (princ (cdr ranges)))
16913               (while (setq range (pop ranges))
16914                 (if (or (atom range) (= (car range) (cdr range)))
16915                     (princ (or (and (atom range) range) (car range)))
16916                   (princ (car range))
16917                   (insert "-")
16918                   (princ (cdr range)))
16919                 (if ranges (insert ",")))))
16920           (insert "\n")))
16921       (make-local-variable 'version-control)
16922       (setq version-control 'never)
16923       ;; It has been reported that sometime the modtime on the .newsrc
16924       ;; file seems to be off.  We really do want to overwrite it, so
16925       ;; we clear the modtime here before saving.  It's a bit odd,
16926       ;; though...
16927       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16928       ;; delete the silly thing entirely first.  but this fails to provide
16929       ;; such niceties as .newsrc~ creation.
16930       (if gnus-modtime-botch
16931           (delete-file gnus-startup-file)
16932         (clear-visited-file-modtime))
16933       (run-hooks 'gnus-save-standard-newsrc-hook)
16934       (save-buffer)
16935       (kill-buffer (current-buffer)))))
16936
16937 \f
16938 ;;;
16939 ;;; Slave functions.
16940 ;;;
16941
16942 (defun gnus-slave-save-newsrc ()
16943   (save-excursion
16944     (set-buffer gnus-dribble-buffer)
16945     (let ((slave-name
16946            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16947       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16948
16949 (defun gnus-master-read-slave-newsrc ()
16950   (let ((slave-files
16951          (directory-files
16952           (file-name-directory gnus-current-startup-file)
16953           t (concat
16954              "^" (regexp-quote
16955                   (concat
16956                    (file-name-nondirectory gnus-current-startup-file)
16957                    "-slave-")))
16958           t))
16959         file)
16960     (if (not slave-files)
16961         ()                              ; There are no slave files to read.
16962       (gnus-message 7 "Reading slave newsrcs...")
16963       (save-excursion
16964         (set-buffer (get-buffer-create " *gnus slave*"))
16965         (buffer-disable-undo (current-buffer))
16966         (setq slave-files
16967               (sort (mapcar (lambda (file)
16968                               (list (nth 5 (file-attributes file)) file))
16969                             slave-files)
16970                     (lambda (f1 f2)
16971                       (or (< (caar f1) (caar f2))
16972                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16973         (while slave-files
16974           (erase-buffer)
16975           (setq file (nth 1 (car slave-files)))
16976           (insert-file-contents file)
16977           (if (condition-case ()
16978                   (progn
16979                     (eval-buffer (current-buffer))
16980                     t)
16981                 (error
16982                  (gnus-error 3.2 "Possible error in %s" file)
16983                  nil))
16984               (or gnus-slave ; Slaves shouldn't delete these files.
16985                   (condition-case ()
16986                       (delete-file file)
16987                     (error nil))))
16988           (setq slave-files (cdr slave-files))))
16989       (gnus-message 7 "Reading slave newsrcs...done"))))
16990
16991 \f
16992 ;;;
16993 ;;; Group description.
16994 ;;;
16995
16996 (defun gnus-read-all-descriptions-files ()
16997   (let ((methods (cons gnus-select-method 
16998                        (nconc
16999                         (when gnus-message-archive-method
17000                           (list "archive"))
17001                         gnus-secondary-select-methods))))
17002     (while methods
17003       (gnus-read-descriptions-file (car methods))
17004       (setq methods (cdr methods)))
17005     t))
17006
17007 (defun gnus-read-descriptions-file (&optional method)
17008   (let ((method (or method gnus-select-method))
17009         group)
17010     (when (stringp method)
17011       (setq method (gnus-server-to-method method)))
17012     ;; We create the hashtable whether we manage to read the desc file
17013     ;; to avoid trying to re-read after a failed read.
17014     (or gnus-description-hashtb
17015         (setq gnus-description-hashtb
17016               (gnus-make-hashtable (length gnus-active-hashtb))))
17017     ;; Mark this method's desc file as read.
17018     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
17019                   gnus-description-hashtb)
17020
17021     (gnus-message 5 "Reading descriptions file via %s..." (car method))
17022     (cond
17023      ((not (gnus-check-server method))
17024       (gnus-message 1 "Couldn't open server")
17025       nil)
17026      ((not (gnus-request-list-newsgroups method))
17027       (gnus-message 1 "Couldn't read newsgroups descriptions")
17028       nil)
17029      (t
17030       (save-excursion
17031         (save-restriction
17032           (set-buffer nntp-server-buffer)
17033           (goto-char (point-min))
17034           (when (or (search-forward "\n.\n" nil t)
17035                     (goto-char (point-max)))
17036             (beginning-of-line)
17037             (narrow-to-region (point-min) (point)))
17038           ;; If these are groups from a foreign select method, we insert the
17039           ;; group prefix in front of the group names.
17040           (and method (not (gnus-server-equal
17041                             (gnus-server-get-method nil method)
17042                             (gnus-server-get-method nil gnus-select-method)))
17043                (let ((prefix (gnus-group-prefixed-name "" method)))
17044                  (goto-char (point-min))
17045                  (while (and (not (eobp))
17046                              (progn (insert prefix)
17047                                     (zerop (forward-line 1)))))))
17048           (goto-char (point-min))
17049           (while (not (eobp))
17050             ;; If we get an error, we set group to 0, which is not a
17051             ;; symbol...
17052             (setq group
17053                   (condition-case ()
17054                       (let ((obarray gnus-description-hashtb))
17055                         ;; Group is set to a symbol interned in this
17056                         ;; hash table.
17057                         (read nntp-server-buffer))
17058                     (error 0)))
17059             (skip-chars-forward " \t")
17060             ;; ...  which leads to this line being effectively ignored.
17061             (and (symbolp group)
17062                  (set group (buffer-substring
17063                              (point) (progn (end-of-line) (point)))))
17064             (forward-line 1))))
17065       (gnus-message 5 "Reading descriptions file...done")
17066       t))))
17067
17068 (defun gnus-group-get-description (group)
17069   "Get the description of a group by sending XGTITLE to the server."
17070   (when (gnus-request-group-description group)
17071     (save-excursion
17072       (set-buffer nntp-server-buffer)
17073       (goto-char (point-min))
17074       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
17075         (match-string 1)))))
17076
17077 \f
17078 ;;;
17079 ;;; Buffering of read articles.
17080 ;;;
17081
17082 (defvar gnus-backlog-buffer " *Gnus Backlog*")
17083 (defvar gnus-backlog-articles nil)
17084 (defvar gnus-backlog-hashtb nil)
17085
17086 (defun gnus-backlog-buffer ()
17087   "Return the backlog buffer."
17088   (or (get-buffer gnus-backlog-buffer)
17089       (save-excursion
17090         (set-buffer (get-buffer-create gnus-backlog-buffer))
17091         (buffer-disable-undo (current-buffer))
17092         (setq buffer-read-only t)
17093         (gnus-add-current-to-buffer-list)
17094         (get-buffer gnus-backlog-buffer))))
17095
17096 (defun gnus-backlog-setup ()
17097   "Initialize backlog variables."
17098   (unless gnus-backlog-hashtb
17099     (setq gnus-backlog-hashtb (make-vector 1023 0))))
17100
17101 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
17102
17103 (defun gnus-backlog-shutdown ()
17104   "Clear all backlog variables and buffers."
17105   (when (get-buffer gnus-backlog-buffer)
17106     (kill-buffer gnus-backlog-buffer))
17107   (setq gnus-backlog-hashtb nil
17108         gnus-backlog-articles nil))
17109
17110 (defun gnus-backlog-enter-article (group number buffer)
17111   (gnus-backlog-setup)
17112   (let ((ident (intern (concat group ":" (int-to-string number))
17113                        gnus-backlog-hashtb))
17114         b)
17115     (if (memq ident gnus-backlog-articles)
17116         () ; It's already kept.
17117       ;; Remove the oldest article, if necessary.
17118       (and (numberp gnus-keep-backlog)
17119            (>= (length gnus-backlog-articles) gnus-keep-backlog)
17120            (gnus-backlog-remove-oldest-article))
17121       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
17122       ;; Insert the new article.
17123       (save-excursion
17124         (set-buffer (gnus-backlog-buffer))
17125         (let (buffer-read-only)
17126           (goto-char (point-max))
17127           (or (bolp) (insert "\n"))
17128           (setq b (point))
17129           (insert-buffer-substring buffer)
17130           ;; Tag the beginning of the article with the ident.
17131           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
17132
17133 (defun gnus-backlog-remove-oldest-article ()
17134   (save-excursion
17135     (set-buffer (gnus-backlog-buffer))
17136     (goto-char (point-min))
17137     (if (zerop (buffer-size))
17138         () ; The buffer is empty.
17139       (let ((ident (get-text-property (point) 'gnus-backlog))
17140             buffer-read-only)
17141         ;; Remove the ident from the list of articles.
17142         (when ident
17143           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17144         ;; Delete the article itself.
17145         (delete-region
17146          (point) (next-single-property-change
17147                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
17148
17149 (defun gnus-backlog-remove-article (group number)
17150   "Remove article NUMBER in GROUP from the backlog."
17151   (when (numberp number)
17152     (gnus-backlog-setup)
17153     (let ((ident (intern (concat group ":" (int-to-string number))
17154                          gnus-backlog-hashtb))
17155           beg end)
17156       (when (memq ident gnus-backlog-articles)
17157         ;; It was in the backlog.
17158         (save-excursion
17159           (set-buffer (gnus-backlog-buffer))
17160           (let (buffer-read-only)
17161             (when (setq beg (text-property-any
17162                              (point-min) (point-max) 'gnus-backlog
17163                              ident))
17164               ;; Find the end (i. e., the beginning of the next article).
17165               (setq end
17166                     (next-single-property-change
17167                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
17168               (delete-region beg end)
17169               ;; Return success.
17170               t)))))))
17171
17172 (defun gnus-backlog-request-article (group number buffer)
17173   (when (numberp number)
17174     (gnus-backlog-setup)
17175     (let ((ident (intern (concat group ":" (int-to-string number))
17176                          gnus-backlog-hashtb))
17177           beg end)
17178       (when (memq ident gnus-backlog-articles)
17179         ;; It was in the backlog.
17180         (save-excursion
17181           (set-buffer (gnus-backlog-buffer))
17182           (if (not (setq beg (text-property-any
17183                               (point-min) (point-max) 'gnus-backlog
17184                               ident)))
17185               ;; It wasn't in the backlog after all.
17186               (ignore
17187                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
17188             ;; Find the end (i. e., the beginning of the next article).
17189             (setq end
17190                   (next-single-property-change
17191                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
17192         (let ((buffer-read-only nil))
17193           (erase-buffer)
17194           (insert-buffer-substring gnus-backlog-buffer beg end)
17195           t)))))
17196
17197 ;; Allow redefinition of Gnus functions.
17198
17199 (gnus-ems-redefine)
17200
17201 (provide 'gnus)
17202
17203 ;;; gnus.el ends here