*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval '(run-hooks 'gnus-load-hook))
30
31 (require 'mail-utils)
32 (require 'timezone)
33 (require 'nnheader)
34 (require 'message)
35
36 (eval-when-compile (require 'cl))
37
38 (defvar gnus-directory (or (getenv "SAVEDIR") "~/News/")
39   "*Directory variable from which all other Gnus file variables are derived.")
40
41 ;; Site dependent variables.  These variables should be defined in
42 ;; paths.el.
43
44 (defvar gnus-default-nntp-server nil
45   "Specify a default NNTP server.
46 This variable should be defined in paths.el, and should never be set
47 by the user.
48 If you want to change servers, you should use `gnus-select-method'.
49 See the documentation to that variable.")
50
51 (defvar gnus-backup-default-subscribed-newsgroups
52   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
53   "Default default new newsgroups the first time Gnus is run.
54 Should be set in paths.el, and shouldn't be touched by the user.")
55
56 (defvar gnus-local-domain nil
57   "Local domain name without a host name.
58 The DOMAINNAME environment variable is used instead if it is defined.
59 If the `system-name' function returns the full Internet name, there is
60 no need to set this variable.")
61
62 (defvar gnus-local-organization nil
63   "String with a description of what organization (if any) the user belongs to.
64 The ORGANIZATION environment variable is used instead if it is defined.
65 If this variable contains a function, this function will be called
66 with the current newsgroup name as the argument.  The function should
67 return a string.
68
69 In any case, if the string (either in the variable, in the environment
70 variable, or returned by the function) is a file name, the contents of
71 this file will be used as the organization.")
72
73 ;; Customization variables
74
75 ;; Don't touch this variable.
76 (defvar gnus-nntp-service "nntp"
77   "*NNTP service name (\"nntp\" or 119).
78 This is an obsolete variable, which is scarcely used.  If you use an
79 nntp server for your newsgroup and want to change the port number
80 used to 899, you would say something along these lines:
81
82  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
83
84 (defvar gnus-nntpserver-file "/etc/nntpserver"
85   "*A file with only the name of the nntp server in it.")
86
87 ;; This function is used to check both the environment variable
88 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
89 ;; an nntp server name default.
90 (defun gnus-getenv-nntpserver ()
91   (or (getenv "NNTPSERVER")
92       (and (file-readable-p gnus-nntpserver-file)
93            (save-excursion
94              (set-buffer (get-buffer-create " *gnus nntp*"))
95              (buffer-disable-undo (current-buffer))
96              (insert-file-contents gnus-nntpserver-file)
97              (let ((name (buffer-string)))
98                (prog1
99                    (if (string-match "^[ \t\n]*$" name)
100                        nil
101                      name)
102                  (kill-buffer (current-buffer))))))))
103
104 (defvar gnus-select-method
105   (nconc
106    (list 'nntp (or (condition-case ()
107                        (gnus-getenv-nntpserver)
108                      (error nil))
109                    (if (and gnus-default-nntp-server
110                             (not (string= gnus-default-nntp-server "")))
111                        gnus-default-nntp-server)
112                    (system-name)))
113    (if (or (null gnus-nntp-service)
114            (equal gnus-nntp-service "nntp"))
115        nil
116      (list gnus-nntp-service)))
117   "*Default method for selecting a newsgroup.
118 This variable should be a list, where the first element is how the
119 news is to be fetched, the second is the address.
120
121 For instance, if you want to get your news via NNTP from
122 \"flab.flab.edu\", you could say:
123
124 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
125
126 If you want to use your local spool, say:
127
128 (setq gnus-select-method (list 'nnspool (system-name)))
129
130 If you use this variable, you must set `gnus-nntp-server' to nil.
131
132 There is a lot more to know about select methods and virtual servers -
133 see the manual for details.")
134
135 (defvar gnus-message-archive-method 
136   `(nnfolder
137     "archive"
138     (nnfolder-directory ,(nnheader-concat message-directory "archive"))
139     (nnfolder-active-file 
140      ,(nnheader-concat message-directory "archive/active"))
141     (nnfolder-get-new-mail nil)
142     (nnfolder-inhibit-expiry t))
143   "*Method used for archiving messages you've sent.
144 This should be a mail method.")
145
146 (defvar gnus-refer-article-method nil
147   "*Preferred method for fetching an article by Message-ID.
148 If you are reading news from the local spool (with nnspool), fetching
149 articles by Message-ID is painfully slow.  By setting this method to an
150 nntp method, you might get acceptable results.
151
152 The value of this variable must be a valid select method as discussed
153 in the documentation of `gnus-select-method'.")
154
155 (defvar gnus-secondary-select-methods nil
156   "*A list of secondary methods that will be used for reading news.
157 This is a list where each element is a complete select method (see
158 `gnus-select-method').
159
160 If, for instance, you want to read your mail with the nnml backend,
161 you could set this variable:
162
163 (setq gnus-secondary-select-methods '((nnml \"\")))")
164
165 (defvar gnus-secondary-servers nil
166   "*List of NNTP servers that the user can choose between interactively.
167 To make Gnus query you for a server, you have to give `gnus' a
168 non-numeric prefix - `C-u M-x gnus', in short.")
169
170 (defvar gnus-nntp-server nil
171   "*The name of the host running the NNTP server.
172 This variable is semi-obsolete.  Use the `gnus-select-method'
173 variable instead.")
174
175 (defvar gnus-startup-file "~/.newsrc"
176   "*Your `.newsrc' file.
177 `.newsrc-SERVER' will be used instead if that exists.")
178
179 (defvar gnus-init-file "~/.gnus"
180   "*Your Gnus elisp startup file.
181 If a file with the .el or .elc suffixes exist, it will be read
182 instead.")
183
184 (defvar gnus-group-faq-directory
185   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
186     "/ftp@sunsite.auc.dk:/pub/usenet/"
187     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
188     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
189     "/ftp@rtfm.mit.edu:/pub/usenet/"
190     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
191     "/ftp@ftp.sunet.se:/pub/usenet/"
192     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
193     "/ftp@hwarang.postech.ac.kr:/pub/usenet/"
194     "/ftp@ftp.hk.super.net:/mirror/faqs/")
195   "*Directory where the group FAQs are stored.
196 This will most commonly be on a remote machine, and the file will be
197 fetched by ange-ftp.
198
199 This variable can also be a list of directories.  In that case, the
200 first element in the list will be used by default, and the others will
201 be used as backup sites.
202
203 Note that Gnus uses an aol machine as the default directory.  If this
204 feels fundamentally unclean, just think of it as a way to finally get
205 something of value back from them.
206
207 If the default site is too slow, try one of these:
208
209    North America: mirrors.aol.com                /pub/rtfm/usenet
210                   ftp.seas.gwu.edu               /pub/rtfm
211                   rtfm.mit.edu                   /pub/usenet
212    Europe:        ftp.uni-paderborn.de           /pub/FAQ
213                   src.doc.ic.ac.uk               /usenet/news-FAQS
214                   ftp.sunet.se                   /pub/usenet
215                   sunsite.auc.dk                 /pub/usenet
216    Asia:          nctuccca.edu.tw                /USENET/FAQ
217                   hwarang.postech.ac.kr          /pub/usenet
218                   ftp.hk.super.net               /mirror/faqs")
219
220 (defvar gnus-group-archive-directory
221   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
222   "*The address of the (ding) archives.")
223
224 (defvar gnus-group-recent-archive-directory
225   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
226   "*The address of the most recent (ding) articles.")
227
228 (defvar gnus-default-subscribed-newsgroups nil
229   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
230 It should be a list of strings.
231 If it is `t', Gnus will not do anything special the first time it is
232 started; it'll just use the normal newsgroups subscription methods.")
233
234 (defvar gnus-use-cross-reference t
235   "*Non-nil means that cross referenced articles will be marked as read.
236 If nil, ignore cross references.  If t, mark articles as read in
237 subscribed newsgroups.  If neither t nor nil, mark as read in all
238 newsgroups.")
239
240 (defvar gnus-single-article-buffer t
241   "*If non-nil, display all articles in the same buffer.
242 If nil, each group will get its own article buffer.")
243
244 (defvar gnus-use-dribble-file t
245   "*Non-nil means that Gnus will use a dribble file to store user updates.
246 If Emacs should crash without saving the .newsrc files, complete
247 information can be restored from the dribble file.")
248
249 (defvar gnus-dribble-directory nil
250   "*The directory where dribble files will be saved.
251 If this variable is nil, the directory where the .newsrc files are
252 saved will be used.")
253
254 (defvar gnus-asynchronous nil
255   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
256
257 (defvar gnus-kill-summary-on-exit t
258   "*If non-nil, kill the summary buffer when you exit from it.
259 If nil, the summary will become a \"*Dead Summary*\" buffer, and
260 it will be killed sometime later.")
261
262 (defvar gnus-large-newsgroup 200
263   "*The number of articles which indicates a large newsgroup.
264 If the number of articles in a newsgroup is greater than this value,
265 confirmation is required for selecting the newsgroup.")
266
267 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
268 (defvar gnus-no-groups-message "No news is horrible news"
269   "*Message displayed by Gnus when no groups are available.")
270
271 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
272   "*Non-nil means that the default name of a file to save articles in is the group name.
273 If it's nil, the directory form of the group name is used instead.
274
275 If this variable is a list, and the list contains the element
276 `not-score', long file names will not be used for score files; if it
277 contains the element `not-save', long file names will not be used for
278 saving; and if it contains the element `not-kill', long file names
279 will not be used for kill files.")
280
281 (defvar gnus-article-save-directory gnus-directory
282   "*Name of the directory articles will be saved in (default \"~/News\").")
283
284 (defvar gnus-kill-files-directory gnus-directory
285   "*Name of the directory where kill files will be stored (default \"~/News\").")
286
287 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
288   "*A function to save articles in your favorite format.
289 The function must be interactively callable (in other words, it must
290 be an Emacs command).
291
292 Gnus provides the following functions:
293
294 * gnus-summary-save-in-rmail (Rmail format)
295 * gnus-summary-save-in-mail (Unix mail format)
296 * gnus-summary-save-in-folder (MH folder)
297 * gnus-summary-save-in-file (article format).
298 * gnus-summary-save-in-vm (use VM's folder format).")
299
300 (defvar gnus-prompt-before-saving 'always
301   "*This variable says how much prompting is to be done when saving articles.
302 If it is nil, no prompting will be done, and the articles will be
303 saved to the default files.  If this variable is `always', each and
304 every article that is saved will be preceded by a prompt, even when
305 saving large batches of articles.  If this variable is neither nil not
306 `always', there the user will be prompted once for a file name for
307 each invocation of the saving commands.")
308
309 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
310   "*A function generating a file name to save articles in Rmail format.
311 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
312
313 (defvar gnus-mail-save-name (function gnus-plain-save-name)
314   "*A function generating a file name to save articles in Unix mail format.
315 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
316
317 (defvar gnus-folder-save-name (function gnus-folder-save-name)
318   "*A function generating a file name to save articles in MH folder.
319 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
320
321 (defvar gnus-file-save-name (function gnus-numeric-save-name)
322   "*A function generating a file name to save articles in article format.
323 The function is called with NEWSGROUP, HEADERS, and optional
324 LAST-FILE.")
325
326 (defvar gnus-split-methods
327   '((gnus-article-archive-name))
328   "*Variable used to suggest where articles are to be saved.
329 For instance, if you would like to save articles related to Gnus in
330 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
331 you could set this variable to something like:
332
333  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
334    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
335
336 This variable is an alist where the where the key is the match and the
337 value is a list of possible files to save in if the match is non-nil.
338
339 If the match is a string, it is used as a regexp match on the
340 article.  If the match is a symbol, that symbol will be funcalled
341 from the buffer of the article to be saved with the newsgroup as the
342 parameter.  If it is a list, it will be evaled in the same buffer.
343
344 If this form or function returns a string, this string will be used as
345 a possible file name; and if it returns a non-nil list, that list will
346 be used as possible file names.")
347
348 (defvar gnus-move-split-methods nil
349   "*Variable used to suggest where articles are to be moved to.
350 It uses the same syntax as the `gnus-split-methods' variable.")
351
352 (defvar gnus-save-score nil
353   "*If non-nil, save group scoring info.")
354
355 (defvar gnus-use-adaptive-scoring nil
356   "*If non-nil, use some adaptive scoring scheme.")
357
358 (defvar gnus-use-cache nil
359   "*If nil, Gnus will ignore the article cache.
360 If `passive', it will allow entering (and reading) articles
361 explicitly entered into the cache.  If anything else, use the
362 cache to the full extent of the law.")
363
364 (defvar gnus-use-trees nil
365   "*If non-nil, display a thread tree buffer.")
366
367 (defvar gnus-use-grouplens nil
368   "*If non-nil, use GroupLens ratings.")
369
370 (defvar gnus-keep-backlog nil
371   "*If non-nil, Gnus will keep read articles for later re-retrieval.
372 If it is a number N, then Gnus will only keep the last N articles
373 read.  If it is neither nil nor a number, Gnus will keep all read
374 articles.  This is not a good idea.")
375
376 (defvar gnus-use-nocem nil
377   "*If non-nil, Gnus will read NoCeM cancel messages.")
378
379 (defvar gnus-use-demon nil
380   "If non-nil, Gnus might use some demons.")
381
382 (defvar gnus-use-scoring t
383   "*If non-nil, enable scoring.")
384
385 (defvar gnus-use-picons nil
386   "*If non-nil, display picons.")
387
388 (defvar gnus-fetch-old-headers nil
389   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
390 If an unread article in the group refers to an older, already read (or
391 just marked as read) article, the old article will not normally be
392 displayed in the Summary buffer.  If this variable is non-nil, Gnus
393 will attempt to grab the headers to the old articles, and thereby
394 build complete threads.  If it has the value `some', only enough
395 headers to connect otherwise loose threads will be displayed.
396 This variable can also be a number.  In that case, no more than that
397 number of old headers will be fetched.
398
399 The server has to support NOV for any of this to work.")
400
401 ;see gnus-cus.el
402 ;(defvar gnus-visual t
403 ;  "*If non-nil, will do various highlighting.
404 ;If nil, no mouse highlights (or any other highlights) will be
405 ;performed.  This might speed up Gnus some when generating large group
406 ;and summary buffers.")
407
408 (defvar gnus-novice-user t
409   "*Non-nil means that you are a usenet novice.
410 If non-nil, verbose messages may be displayed and confirmations may be
411 required.")
412
413 (defvar gnus-expert-user nil
414   "*Non-nil means that you will never be asked for confirmation about anything.
415 And that means *anything*.")
416
417 (defvar gnus-verbose 7
418   "*Integer that says how verbose Gnus should be.
419 The higher the number, the more messages Gnus will flash to say what
420 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
421 display most important messages; and at ten, Gnus will keep on
422 jabbering all the time.")
423
424 (defvar gnus-keep-same-level nil
425   "*Non-nil means that the next newsgroup after the current will be on the same level.
426 When you type, for instance, `n' after reading the last article in the
427 current newsgroup, you will go to the next newsgroup.  If this variable
428 is nil, the next newsgroup will be the next from the group
429 buffer.
430 If this variable is non-nil, Gnus will either put you in the
431 next newsgroup with the same level, or, if no such newsgroup is
432 available, the next newsgroup with the lowest possible level higher
433 than the current level.
434 If this variable is `best', Gnus will make the next newsgroup the one
435 with the best level.")
436
437 (defvar gnus-summary-make-false-root 'adopt
438   "*nil means that Gnus won't gather loose threads.
439 If the root of a thread has expired or been read in a previous
440 session, the information necessary to build a complete thread has been
441 lost.  Instead of having many small sub-threads from this original thread
442 scattered all over the summary buffer, Gnus can gather them.
443
444 If non-nil, Gnus will try to gather all loose sub-threads from an
445 original thread into one large thread.
446
447 If this variable is non-nil, it should be one of `none', `adopt',
448 `dummy' or `empty'.
449
450 If this variable is `none', Gnus will not make a false root, but just
451 present the sub-threads after another.
452 If this variable is `dummy', Gnus will create a dummy root that will
453 have all the sub-threads as children.
454 If this variable is `adopt', Gnus will make one of the \"children\"
455 the parent and mark all the step-children as such.
456 If this variable is `empty', the \"children\" are printed with empty
457 subject fields.  (Or rather, they will be printed with a string
458 given by the `gnus-summary-same-subject' variable.)")
459
460 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
461   "*A regexp to match subjects to be excluded from loose thread gathering.
462 As loose thread gathering is done on subjects only, that means that
463 there can be many false gatherings performed.  By rooting out certain
464 common subjects, gathering might become saner.")
465
466 (defvar gnus-summary-gather-subject-limit nil
467   "*Maximum length of subject comparisons when gathering loose threads.
468 Use nil to compare full subjects.  Setting this variable to a low
469 number will help gather threads that have been corrupted by
470 newsreaders chopping off subject lines, but it might also mean that
471 unrelated articles that have subject that happen to begin with the
472 same few characters will be incorrectly gathered.
473
474 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
475 comparing subjects.")
476
477 (defvar gnus-simplify-ignored-prefixes nil
478   "*Regexp, matches for which are removed from subject lines when simplifying.")
479
480 (defvar gnus-build-sparse-threads nil
481   "*If non-nil, fill in the gaps in threads.
482 If `some', only fill in the gaps that are needed to tie loose threads
483 together.  If `more', fill in all leaf nodes that Gnus can find.  If
484 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
485
486 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
487   "Function used for gathering loose threads.
488 There are two pre-defined functions: `gnus-gather-threads-by-subject',
489 which only takes Subjects into consideration; and
490 `gnus-gather-threads-by-references', which compared the References
491 headers of the articles to find matches.")
492
493 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
494 (defvar gnus-summary-same-subject ""
495   "*String indicating that the current article has the same subject as the previous.
496 This variable will only be used if the value of
497 `gnus-summary-make-false-root' is `empty'.")
498
499 (defvar gnus-summary-goto-unread t
500   "*If non-nil, marking commands will go to the next unread article.
501 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
502 whether it is read or not.")
503
504 (defvar gnus-group-goto-unread t
505   "*If non-nil, movement commands will go to the next unread and subscribed group.")
506
507 (defvar gnus-goto-next-group-when-activating t
508   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
509
510 (defvar gnus-check-new-newsgroups t
511   "*Non-nil means that Gnus will add new newsgroups at startup.
512 If this variable is `ask-server', Gnus will ask the server for new
513 groups since the last time it checked.  This means that the killed list
514 is no longer necessary, so you could set `gnus-save-killed-list' to
515 nil.
516
517 A variant is to have this variable be a list of select methods.  Gnus
518 will then use the `ask-server' method on all these select methods to
519 query for new groups from all those servers.
520
521 Eg.
522   (setq gnus-check-new-newsgroups
523         '((nntp \"some.server\") (nntp \"other.server\")))
524
525 If this variable is nil, then you have to tell Gnus explicitly to
526 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
527
528 (defvar gnus-check-bogus-newsgroups nil
529   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
530 If this variable is nil, then you have to tell Gnus explicitly to
531 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
532
533 (defvar gnus-read-active-file t
534   "*Non-nil means that Gnus will read the entire active file at startup.
535 If this variable is nil, Gnus will only know about the groups in your
536 `.newsrc' file.
537
538 If this variable is `some', Gnus will try to only read the relevant
539 parts of the active file from the server.  Not all servers support
540 this, and it might be quite slow with other servers, but this should
541 generally be faster than both the t and nil value.
542
543 If you set this variable to nil or `some', you probably still want to
544 be told about new newsgroups that arrive.  To do that, set
545 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
546 properly with all servers.")
547
548 (defvar gnus-level-subscribed 5
549   "*Groups with levels less than or equal to this variable are subscribed.")
550
551 (defvar gnus-level-unsubscribed 7
552   "*Groups with levels less than or equal to this variable are unsubscribed.
553 Groups with levels less than `gnus-level-subscribed', which should be
554 less than this variable, are subscribed.")
555
556 (defvar gnus-level-zombie 8
557   "*Groups with this level are zombie groups.")
558
559 (defvar gnus-level-killed 9
560   "*Groups with this level are killed.")
561
562 (defvar gnus-level-default-subscribed 3
563   "*New subscribed groups will be subscribed at this level.")
564
565 (defvar gnus-level-default-unsubscribed 6
566   "*New unsubscribed groups will be unsubscribed at this level.")
567
568 (defvar gnus-activate-level (1+ gnus-level-subscribed)
569   "*Groups higher than this level won't be activated on startup.
570 Setting this variable to something log might save lots of time when
571 you have many groups that you aren't interested in.")
572
573 (defvar gnus-activate-foreign-newsgroups 4
574   "*If nil, Gnus will not check foreign newsgroups at startup.
575 If it is non-nil, it should be a number between one and nine.  Foreign
576 newsgroups that have a level lower or equal to this number will be
577 activated on startup.  For instance, if you want to active all
578 subscribed newsgroups, but not the rest, you'd set this variable to
579 `gnus-level-subscribed'.
580
581 If you subscribe to lots of newsgroups from different servers, startup
582 might take a while.  By setting this variable to nil, you'll save time,
583 but you won't be told how many unread articles there are in the
584 groups.")
585
586 (defvar gnus-save-newsrc-file t
587   "*Non-nil means that Gnus will save the `.newsrc' file.
588 Gnus always saves its own startup file, which is called
589 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
590 be readily understood by other newsreaders.  If you don't plan on
591 using other newsreaders, set this variable to nil to save some time on
592 exit.")
593
594 (defvar gnus-save-killed-list t
595   "*If non-nil, save the list of killed groups to the startup file.
596 If you set this variable to nil, you'll save both time (when starting
597 and quitting) and space (both memory and disk), but it will also mean
598 that Gnus has no record of which groups are new and which are old, so
599 the automatic new newsgroups subscription methods become meaningless.
600
601 You should always set `gnus-check-new-newsgroups' to `ask-server' or
602 nil if you set this variable to nil.")
603
604 (defvar gnus-interactive-catchup t
605   "*If non-nil, require your confirmation when catching up a group.")
606
607 (defvar gnus-interactive-exit t
608   "*If non-nil, require your confirmation when exiting Gnus.")
609
610 (defvar gnus-kill-killed t
611   "*If non-nil, Gnus will apply kill files to already killed articles.
612 If it is nil, Gnus will never apply kill files to articles that have
613 already been through the scoring process, which might very well save lots
614 of time.")
615
616 (defvar gnus-extract-address-components 'gnus-extract-address-components
617   "*Function for extracting address components from a From header.
618 Two pre-defined function exist: `gnus-extract-address-components',
619 which is the default, quite fast, and too simplistic solution, and
620 `mail-extract-address-components', which works much better, but is
621 slower.")
622
623 (defvar gnus-summary-default-score 0
624   "*Default article score level.
625 If this variable is nil, scoring will be disabled.")
626
627 (defvar gnus-summary-zcore-fuzz 0
628   "*Fuzziness factor for the zcore in the summary buffer.
629 Articles with scores closer than this to `gnus-summary-default-score'
630 will not be marked.")
631
632 (defvar gnus-simplify-subject-fuzzy-regexp nil
633   "*Strings to be removed when doing fuzzy matches.
634 This can either be a regular expression or list of regular expressions
635 that will be removed from subject strings if fuzzy subject
636 simplification is selected.")
637
638 (defvar gnus-permanently-visible-groups nil
639   "*Regexp to match groups that should always be listed in the group buffer.
640 This means that they will still be listed when there are no unread
641 articles in the groups.")
642
643 (defvar gnus-list-groups-with-ticked-articles t
644   "*If non-nil, list groups that have only ticked articles.
645 If nil, only list groups that have unread articles.")
646
647 (defvar gnus-group-default-list-level gnus-level-subscribed
648   "*Default listing level.
649 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
650
651 (defvar gnus-group-use-permanent-levels nil
652   "*If non-nil, once you set a level, Gnus will use this level.")
653
654 (defvar gnus-group-list-inactive-groups t
655   "*If non-nil, inactive groups will be listed.")
656
657 (defvar gnus-show-mime nil
658   "*If non-nil, do mime processing of articles.
659 The articles will simply be fed to the function given by
660 `gnus-show-mime-method'.")
661
662 (defvar gnus-strict-mime t
663   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
664
665 (defvar gnus-show-mime-method 'metamail-buffer
666   "*Function to process a MIME message.
667 The function is called from the article buffer.")
668
669 (defvar gnus-decode-encoded-word-method (lambda ())
670   "*Function to decode a MIME encoded-words.
671 The function is called from the article buffer.")
672
673 (defvar gnus-show-threads t
674   "*If non-nil, display threads in summary mode.")
675
676 (defvar gnus-thread-hide-subtree nil
677   "*If non-nil, hide all threads initially.
678 If threads are hidden, you have to run the command
679 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
680 to expose hidden threads.")
681
682 (defvar gnus-thread-hide-killed t
683   "*If non-nil, hide killed threads automatically.")
684
685 (defvar gnus-thread-ignore-subject nil
686   "*If non-nil, ignore subjects and do all threading based on the Reference header.
687 If nil, which is the default, articles that have different subjects
688 from their parents will start separate threads.")
689
690 (defvar gnus-thread-operation-ignore-subject t
691   "*If non-nil, subjects will be ignored when doing thread commands.
692 This affects commands like `gnus-summary-kill-thread' and
693 `gnus-summary-lower-thread'.
694
695 If this variable is nil, articles in the same thread with different
696 subjects will not be included in the operation in question.  If this
697 variable is `fuzzy', only articles that have subjects that are fuzzily
698 equal will be included.")
699
700 (defvar gnus-thread-indent-level 4
701   "*Number that says how much each sub-thread should be indented.")
702
703 (defvar gnus-ignored-newsgroups
704   (purecopy (mapconcat 'identity
705                        '("^to\\."       ; not "real" groups
706                          "^[0-9. \t]+ " ; all digits in name
707                          "[][\"#'()]"   ; bogus characters
708                          )
709                        "\\|"))
710   "*A regexp to match uninteresting newsgroups in the active file.
711 Any lines in the active file matching this regular expression are
712 removed from the newsgroup list before anything else is done to it,
713 thus making them effectively non-existent.")
714
715 (defvar gnus-ignored-headers
716   "^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:"
717   "*All headers that match this regexp will be hidden.
718 This variable can also be a list of regexps of headers to be ignored.
719 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
720
721 (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-"
722   "*All headers that do not match this regexp will be hidden.
723 This variable can also be a list of regexp of headers to remain visible.
724 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
725
726 (defvar gnus-sorted-header-list
727   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
728     "^Cc:" "^Date:" "^Organization:")
729   "*This variable is a list of regular expressions.
730 If it is non-nil, headers that match the regular expressions will
731 be placed first in the article buffer in the sequence specified by
732 this list.")
733
734 (defvar gnus-boring-article-headers
735   '(empty followup-to reply-to)
736   "*Headers that are only to be displayed if they have interesting data.
737 Possible values in this list are `empty', `newsgroups', `followup-to',
738 `reply-to', and `date'.")
739
740 (defvar gnus-show-all-headers nil
741   "*If non-nil, don't hide any headers.")
742
743 (defvar gnus-save-all-headers t
744   "*If non-nil, don't remove any headers before saving.")
745
746 (defvar gnus-saved-headers gnus-visible-headers
747   "*Headers to keep if `gnus-save-all-headers' is nil.
748 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
749 If that variable is nil, however, all headers that match this regexp
750 will be kept while the rest will be deleted before saving.")
751
752 (defvar gnus-inhibit-startup-message nil
753   "*If non-nil, the startup message will not be displayed.")
754
755 (defvar gnus-signature-separator "^-- *$"
756   "Regexp matching signature separator.")
757
758 (defvar gnus-signature-limit nil
759   "Provide a limit to what is considered a signature.
760 If it is a number, no signature may not be longer (in characters) than
761 that number.  If it is a function, the function will be called without
762 any parameters, and if it returns nil, there is no signature in the
763 buffer.  If it is a string, it will be used as a regexp.  If it
764 matches, the text in question is not a signature.")
765
766 (defvar gnus-auto-extend-newsgroup t
767   "*If non-nil, extend newsgroup forward and backward when requested.")
768
769 (defvar gnus-auto-select-first t
770   "*If nil, don't select the first unread article when entering a group.
771 If this variable is `best', select the highest-scored unread article
772 in the group.  If neither nil nor `best', select the first unread
773 article.
774
775 If you want to prevent automatic selection of the first unread article
776 in some newsgroups, set the variable to nil in
777 `gnus-select-group-hook'.")
778
779 (defvar gnus-auto-select-next t
780   "*If non-nil, offer to go to the next group from the end of the previous.
781 If the value is t and the next newsgroup is empty, Gnus will exit
782 summary mode and go back to group mode.  If the value is neither nil
783 nor t, Gnus will select the following unread newsgroup.  In
784 particular, if the value is the symbol `quietly', the next unread
785 newsgroup will be selected without any confirmation, and if it is
786 `almost-quietly', the next group will be selected without any
787 confirmation if you are located on the last article in the group.
788 Finally, if this variable is `slightly-quietly', the `Z n' command
789 will go to the next group without confirmation.")
790
791 (defvar gnus-auto-select-same nil
792   "*If non-nil, select the next article with the same subject.")
793
794 (defvar gnus-summary-check-current nil
795   "*If non-nil, consider the current article when moving.
796 The \"unread\" movement commands will stay on the same line if the
797 current article is unread.")
798
799 (defvar gnus-auto-center-summary t
800   "*If non-nil, always center the current summary buffer.
801 In particular, if `vertical' do only vertical recentering.  If non-nil
802 and non-`vertical', do both horizontal and vertical recentering.")
803
804 (defvar gnus-break-pages t
805   "*If non-nil, do page breaking on articles.
806 The page delimiter is specified by the `gnus-page-delimiter'
807 variable.")
808
809 (defvar gnus-page-delimiter "^\^L"
810   "*Regexp describing what to use as article page delimiters.
811 The default value is \"^\^L\", which is a form linefeed at the
812 beginning of a line.")
813
814 (defvar gnus-use-full-window t
815   "*If non-nil, use the entire Emacs screen.")
816
817 (defvar gnus-window-configuration nil
818   "Obsolete variable.  See `gnus-buffer-configuration'.")
819
820 (defvar gnus-window-min-width 2
821   "*Minimum width of Gnus buffers.")
822
823 (defvar gnus-window-min-height 1
824   "*Minimum height of Gnus buffers.")
825
826 (defvar gnus-buffer-configuration
827   '((group
828      (vertical 1.0
829                (group 1.0 point)
830                (if gnus-carpal '(group-carpal 4))))
831     (summary
832      (vertical 1.0
833                (summary 1.0 point)
834                (if gnus-carpal '(summary-carpal 4))))
835     (article
836      (cond 
837       (gnus-use-picons
838        '(frame 1.0
839                (vertical 1.0
840                          (summary 0.25 point)
841                          (if gnus-carpal '(summary-carpal 4))
842                          (article 1.0))
843                (vertical ((height . 5) (width . 15)
844                           (user-position . t)
845                           (left . -1) (top . 1))
846                          (picons 1.0))))
847       (gnus-use-trees
848        '(vertical 1.0
849                   (summary 0.25 point)
850                   (tree 0.25)
851                   (article 1.0)))
852       (t
853        '(vertical 1.0
854                  (summary 0.25 point)
855                  (if gnus-carpal '(summary-carpal 4))
856                  (if gnus-use-trees '(tree 0.25))
857                  (article 1.0)))))
858     (server
859      (vertical 1.0
860                (server 1.0 point)
861                (if gnus-carpal '(server-carpal 2))))
862     (browse
863      (vertical 1.0
864                (browse 1.0 point)
865                (if gnus-carpal '(browse-carpal 2))))
866     (message
867      (vertical 1.0
868                (message 1.0 point)))
869     (pick
870      (vertical 1.0
871                (article 1.0 point)))
872     (info
873      (vertical 1.0
874                (info 1.0 point)))
875     (summary-faq
876      (vertical 1.0
877                (summary 0.25)
878                (faq 1.0 point)))
879     (edit-group
880      (vertical 1.0
881                (group 0.5)
882                (edit-group 1.0 point)))
883     (edit-server
884      (vertical 1.0
885                (server 0.5)
886                (edit-server 1.0 point)))
887     (edit-score
888      (vertical 1.0
889                (summary 0.25)
890                (edit-score 1.0 point)))
891     (post
892      (vertical 1.0
893                (post 1.0 point)))
894     (reply
895      (vertical 1.0
896                (article-copy 0.5)
897                (message 1.0 point)))
898     (forward
899      (vertical 1.0
900                (message 1.0 point)))
901     (reply-yank
902      (vertical 1.0
903                (message 1.0 point)))
904     (mail-bounce
905      (vertical 1.0
906                (article 0.5)
907                (message 1.0 point)))
908     (draft
909      (vertical 1.0
910                (draft 1.0 point)))
911     (pipe
912      (vertical 1.0
913                (summary 0.25 point)
914                (if gnus-carpal '(summary-carpal 4))
915                ("*Shell Command Output*" 1.0)))
916     (bug
917      (vertical 1.0
918                ("*Gnus Help Bug*" 0.5)
919                ("*Gnus Bug*" 1.0 point)))
920     (compose-bounce
921      (vertical 1.0
922                (article 0.5)
923                (message 1.0 point))))
924   "Window configuration for all possible Gnus buffers.
925 This variable is a list of lists.  Each of these lists has a NAME and
926 a RULE.  The NAMEs are commonsense names like `group', which names a
927 rule used when displaying the group buffer; `summary', which names a
928 rule for what happens when you enter a group and do not display an
929 article buffer; and so on.  See the value of this variable for a
930 complete list of NAMEs.
931
932 Each RULE is a list of vectors.  The first element in this vector is
933 the name of the buffer to be displayed; the second element is the
934 percentage of the screen this buffer is to occupy (a number in the
935 0.0-0.99 range); the optional third element is `point', which should
936 be present to denote which buffer point is to go to after making this
937 buffer configuration.")
938
939 (defvar gnus-window-to-buffer
940   '((group . gnus-group-buffer)
941     (summary . gnus-summary-buffer)
942     (article . gnus-article-buffer)
943     (server . gnus-server-buffer)
944     (browse . "*Gnus Browse Server*")
945     (edit-group . gnus-group-edit-buffer)
946     (edit-server . gnus-server-edit-buffer)
947     (group-carpal . gnus-carpal-group-buffer)
948     (summary-carpal . gnus-carpal-summary-buffer)
949     (server-carpal . gnus-carpal-server-buffer)
950     (browse-carpal . gnus-carpal-browse-buffer)
951     (edit-score . gnus-score-edit-buffer)
952     (message . gnus-message-buffer)
953     (mail . gnus-message-buffer)
954     (post-news . gnus-message-buffer)
955     (faq . gnus-faq-buffer)
956     (picons . "*Picons*")
957     (tree . gnus-tree-buffer)
958     (info . gnus-info-buffer)
959     (article-copy . gnus-article-copy)
960     (draft . gnus-draft-buffer))
961   "Mapping from short symbols to buffer names or buffer variables.")
962
963 (defvar gnus-carpal nil
964   "*If non-nil, display clickable icons.")
965
966 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
967   "*Function called with a group name when new group is detected.
968 A few pre-made functions are supplied: `gnus-subscribe-randomly'
969 inserts new groups at the beginning of the list of groups;
970 `gnus-subscribe-alphabetically' inserts new groups in strict
971 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
972 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
973 for your decision; `gnus-subscribe-killed' kills all new groups;
974 `gnus-subscribe-zombies' will make all new groups into zombies.")
975
976 ;; Suggested by a bug report by Hallvard B Furuseth.
977 ;; <h.b.furuseth@usit.uio.no>.
978 (defvar gnus-subscribe-options-newsgroup-method
979   (function gnus-subscribe-alphabetically)
980   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
981 If, for instance, you want to subscribe to all newsgroups in the
982 \"no\" and \"alt\" hierarchies, you'd put the following in your
983 .newsrc file:
984
985 options -n no.all alt.all
986
987 Gnus will the subscribe all new newsgroups in these hierarchies with
988 the subscription method in this variable.")
989
990 (defvar gnus-subscribe-hierarchical-interactive nil
991   "*If non-nil, Gnus will offer to subscribe hierarchically.
992 When a new hierarchy appears, Gnus will ask the user:
993
994 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
995
996 If the user pressed `d', Gnus will descend the hierarchy, `y' will
997 subscribe to all newsgroups in the hierarchy and `s' will skip this
998 hierarchy in its entirety.")
999
1000 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1001   "*Function used for sorting the group buffer.
1002 This function will be called with group info entries as the arguments
1003 for the groups to be sorted.  Pre-made functions include
1004 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1005 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1006 `gnus-group-sort-by-rank'.
1007
1008 This variable can also be a list of sorting functions.  In that case,
1009 the most significant sort function should be the last function in the
1010 list.")
1011
1012 ;; Mark variables suggested by Thomas Michanek
1013 ;; <Thomas.Michanek@telelogic.se>.
1014 (defvar gnus-unread-mark ? 
1015   "*Mark used for unread articles.")
1016 (defvar gnus-ticked-mark ?!
1017   "*Mark used for ticked articles.")
1018 (defvar gnus-dormant-mark ??
1019   "*Mark used for dormant articles.")
1020 (defvar gnus-del-mark ?r
1021   "*Mark used for del'd articles.")
1022 (defvar gnus-read-mark ?R
1023   "*Mark used for read articles.")
1024 (defvar gnus-expirable-mark ?E
1025   "*Mark used for expirable articles.")
1026 (defvar gnus-killed-mark ?K
1027   "*Mark used for killed articles.")
1028 (defvar gnus-souped-mark ?F
1029   "*Mark used for killed articles.")
1030 (defvar gnus-kill-file-mark ?X
1031   "*Mark used for articles killed by kill files.")
1032 (defvar gnus-low-score-mark ?Y
1033   "*Mark used for articles with a low score.")
1034 (defvar gnus-catchup-mark ?C
1035   "*Mark used for articles that are caught up.")
1036 (defvar gnus-replied-mark ?A
1037   "*Mark used for articles that have been replied to.")
1038 (defvar gnus-cached-mark ?*
1039   "*Mark used for articles that are in the cache.")
1040 (defvar gnus-saved-mark ?S
1041   "*Mark used for articles that have been saved to.")
1042 (defvar gnus-process-mark ?#
1043   "*Process mark.")
1044 (defvar gnus-ancient-mark ?O
1045   "*Mark used for ancient articles.")
1046 (defvar gnus-sparse-mark ?Q
1047   "*Mark used for sparsely reffed articles.")
1048 (defvar gnus-canceled-mark ?G
1049   "*Mark used for canceled articles.")
1050 (defvar gnus-score-over-mark ?+
1051   "*Score mark used for articles with high scores.")
1052 (defvar gnus-score-below-mark ?-
1053   "*Score mark used for articles with low scores.")
1054 (defvar gnus-empty-thread-mark ? 
1055   "*There is no thread under the article.")
1056 (defvar gnus-not-empty-thread-mark ?=
1057   "*There is a thread under the article.")
1058
1059 (defvar gnus-view-pseudo-asynchronously nil
1060   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1061
1062 (defvar gnus-view-pseudos nil
1063   "*If `automatic', pseudo-articles will be viewed automatically.
1064 If `not-confirm', pseudos will be viewed automatically, and the user
1065 will not be asked to confirm the command.")
1066
1067 (defvar gnus-view-pseudos-separately t
1068   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1069 If nil, all files that use the same viewing command will be given as a
1070 list of parameters to that command.")
1071
1072 (defvar gnus-insert-pseudo-articles t
1073   "*If non-nil, insert pseudo-articles when decoding articles.")
1074
1075 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1076   "*Format of group lines.
1077 It works along the same lines as a normal formatting string,
1078 with some simple extensions.
1079
1080 %M    Only marked articles (character, \"*\" or \" \")
1081 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1082 %L    Level of subscribedness (integer)
1083 %N    Number of unread articles (integer)
1084 %I    Number of dormant articles (integer)
1085 %i    Number of ticked and dormant (integer)
1086 %T    Number of ticked articles (integer)
1087 %R    Number of read articles (integer)
1088 %t    Total number of articles (integer)
1089 %y    Number of unread, unticked articles (integer)
1090 %G    Group name (string)
1091 %g    Qualified group name (string)
1092 %D    Group description (string)
1093 %s    Select method (string)
1094 %o    Moderated group (char, \"m\")
1095 %p    Process mark (char)
1096 %O    Moderated group (string, \"(m)\" or \"\")
1097 %P    Topic indentation (string)
1098 %l    Whether there are GroupLens predictions for this group (string)
1099 %n    Select from where (string)
1100 %z    A string that look like `<%s:%n>' if a foreign select method is used
1101 %u    User defined specifier.  The next character in the format string should
1102       be a letter.  Gnus will call the function gnus-user-format-function-X,
1103       where X is the letter following %u.  The function will be passed the
1104       current header as argument.  The function should return a string, which
1105       will be inserted into the buffer just like information from any other
1106       group specifier.
1107
1108 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1109 the mouse point move inside the area.  There can only be one such area.
1110
1111 Note that this format specification is not always respected.  For
1112 reasons of efficiency, when listing killed groups, this specification
1113 is ignored altogether.  If the spec is changed considerably, your
1114 output may end up looking strange when listing both alive and killed
1115 groups.
1116
1117 If you use %o or %O, reading the active file will be slower and quite
1118 a bit of extra memory will be used. %D will also worsen performance.
1119 Also note that if you change the format specification to include any
1120 of these specs, you must probably re-start Gnus to see them go into
1121 effect.")
1122
1123 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1124   "*The format specification of the lines in the summary buffer.
1125
1126 It works along the same lines as a normal formatting string,
1127 with some simple extensions.
1128
1129 %N   Article number, left padded with spaces (string)
1130 %S   Subject (string)
1131 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1132 %n   Name of the poster (string)
1133 %a   Extracted name of the poster (string)
1134 %A   Extracted address of the poster (string)
1135 %F   Contents of the From: header (string)
1136 %x   Contents of the Xref: header (string)
1137 %D   Date of the article (string)
1138 %d   Date of the article (string) in DD-MMM format
1139 %M   Message-id of the article (string)
1140 %r   References of the article (string)
1141 %c   Number of characters in the article (integer)
1142 %L   Number of lines in the article (integer)
1143 %I   Indentation based on thread level (a string of spaces)
1144 %T   A string with two possible values: 80 spaces if the article
1145      is on thread level two or larger and 0 spaces on level one
1146 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1147 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1148 %[   Opening bracket (character, \"[\" or \"<\")
1149 %]   Closing bracket (character, \"]\" or \">\")
1150 %>   Spaces of length thread-level (string)
1151 %<   Spaces of length (- 20 thread-level) (string)
1152 %i   Article score (number)
1153 %z   Article zcore (character)
1154 %t   Number of articles under the current thread (number).
1155 %e   Whether the thread is empty or not (character).
1156 %l   GroupLens score (string).
1157 %u   User defined specifier.  The next character in the format string should
1158      be a letter.  Gnus will call the function gnus-user-format-function-X,
1159      where X is the letter following %u.  The function will be passed the
1160      current header as argument.  The function should return a string, which
1161      will be inserted into the summary just like information from any other
1162      summary specifier.
1163
1164 Text between %( and %) will be highlighted with `gnus-mouse-face'
1165 when the mouse point is placed inside the area.  There can only be one
1166 such area.
1167
1168 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1169 with care.  For reasons of efficiency, Gnus will compute what column
1170 these characters will end up in, and \"hard-code\" that.  This means that
1171 it is illegal to have these specs after a variable-length spec.  Well,
1172 you might not be arrested, but your summary buffer will look strange,
1173 which is bad enough.
1174
1175 The smart choice is to have these specs as for to the left as
1176 possible.
1177
1178 This restriction may disappear in later versions of Gnus.")
1179
1180 (defvar gnus-summary-dummy-line-format
1181   "*  %(:                          :%) %S\n"
1182   "*The format specification for the dummy roots in the summary buffer.
1183 It works along the same lines as a normal formatting string,
1184 with some simple extensions.
1185
1186 %S  The subject")
1187
1188 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1189   "*The format specification for the summary mode line.
1190 It works along the same lines as a normal formatting string,
1191 with some simple extensions:
1192
1193 %G  Group name
1194 %p  Unprefixed group name
1195 %A  Current article number
1196 %V  Gnus version
1197 %U  Number of unread articles in the group
1198 %e  Number of unselected articles in the group
1199 %Z  A string with unread/unselected article counts
1200 %g  Shortish group name
1201 %S  Subject of the current article
1202 %u  User-defined spec
1203 %s  Current score file name
1204 %d  Number of dormant articles
1205 %r  Number of articles that have been marked as read in this session
1206 %E  Number of articles expunged by the score files")
1207
1208 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1209   "*The format specification for the article mode line.
1210 See `gnus-summary-mode-line-format' for a closer description.")
1211
1212 (defvar gnus-group-mode-line-format "Gnus: %%b {%M%:%S}"
1213   "*The format specification for the group mode line.
1214 It works along the same lines as a normal formatting string,
1215 with some simple extensions:
1216
1217 %S   The native news server.
1218 %M   The native select method.
1219 %:   \":\" if %S isn't \"\".")
1220
1221 (defvar gnus-valid-select-methods
1222   '(("nntp" post address prompt-address)
1223     ("nnspool" post address)
1224     ("nnvirtual" post-mail virtual prompt-address)
1225     ("nnmbox" mail respool address)
1226     ("nnml" mail respool address)
1227     ("nnmh" mail respool address)
1228     ("nndir" post-mail prompt-address address)
1229     ("nneething" none address prompt-address)
1230     ("nndoc" none address prompt-address)
1231     ("nnbabyl" mail address respool)
1232     ("nnkiboze" post address virtual)
1233     ("nnsoup" post-mail address)
1234     ("nndraft" post-mail)
1235     ("nnfolder" mail respool address))
1236   "An alist of valid select methods.
1237 The first element of each list lists should be a string with the name
1238 of the select method.  The other elements may be be the category of
1239 this method (ie. `post', `mail', `none' or whatever) or other
1240 properties that this method has (like being respoolable).
1241 If you implement a new select method, all you should have to change is
1242 this variable.  I think.")
1243
1244 (defvar gnus-updated-mode-lines '(group article summary tree)
1245   "*List of buffers that should update their mode lines.
1246 The list may contain the symbols `group', `article' and `summary'.  If
1247 the corresponding symbol is present, Gnus will keep that mode line
1248 updated with information that may be pertinent.
1249 If this variable is nil, screen refresh may be quicker.")
1250
1251 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1252 (defvar gnus-mode-non-string-length nil
1253   "*Max length of mode-line non-string contents.
1254 If this is nil, Gnus will take space as is needed, leaving the rest
1255 of the modeline intact.")
1256
1257 ;see gnus-cus.el
1258 ;(defvar gnus-mouse-face 'highlight
1259 ;  "*Face used for mouse highlighting in Gnus.
1260 ;No mouse highlights will be done if `gnus-visual' is nil.")
1261
1262 (defvar gnus-summary-mark-below nil
1263   "*Mark all articles with a score below this variable as read.
1264 This variable is local to each summary buffer and usually set by the
1265 score file.")
1266
1267 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1268   "*List of functions used for sorting articles in the summary buffer.
1269 This variable is only used when not using a threaded display.")
1270
1271 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1272   "*List of functions used for sorting threads in the summary buffer.
1273 By default, threads are sorted by article number.
1274
1275 Each function takes two threads and return non-nil if the first thread
1276 should be sorted before the other.  If you use more than one function,
1277 the primary sort function should be the last.  You should probably
1278 always include `gnus-thread-sort-by-number' in the list of sorting
1279 functions -- preferably first.
1280
1281 Ready-mady functions include `gnus-thread-sort-by-number',
1282 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1283 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1284 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1285
1286 (defvar gnus-thread-score-function '+
1287   "*Function used for calculating the total score of a thread.
1288
1289 The function is called with the scores of the article and each
1290 subthread and should then return the score of the thread.
1291
1292 Some functions you can use are `+', `max', or `min'.")
1293
1294 (defvar gnus-summary-expunge-below nil
1295   "All articles that have a score less than this variable will be expunged.")
1296
1297 (defvar gnus-thread-expunge-below nil
1298   "All threads that have a total score less than this variable will be expunged.
1299 See `gnus-thread-score-function' for en explanation of what a
1300 \"thread score\" is.")
1301
1302 (defvar gnus-auto-subscribed-groups
1303   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1304   "*All new groups that match this regexp will be subscribed automatically.
1305 Note that this variable only deals with new groups.  It has no effect
1306 whatsoever on old groups.")
1307
1308 (defvar gnus-options-subscribe nil
1309   "*All new groups matching this regexp will be subscribed unconditionally.
1310 Note that this variable deals only with new newsgroups.  This variable
1311 does not affect old newsgroups.")
1312
1313 (defvar gnus-options-not-subscribe nil
1314   "*All new groups matching this regexp will be ignored.
1315 Note that this variable deals only with new newsgroups.  This variable
1316 does not affect old (already subscribed) newsgroups.")
1317
1318 (defvar gnus-auto-expirable-newsgroups nil
1319   "*Groups in which to automatically mark read articles as expirable.
1320 If non-nil, this should be a regexp that should match all groups in
1321 which to perform auto-expiry.  This only makes sense for mail groups.")
1322
1323 (defvar gnus-total-expirable-newsgroups nil
1324   "*Groups in which to perform expiry of all read articles.
1325 Use with extreme caution.  All groups that match this regexp will be
1326 expiring - which means that all read articles will be deleted after
1327 (say) one week.  (This only goes for mail groups and the like, of
1328 course.)")
1329
1330 (defvar gnus-group-uncollapsed-levels 1
1331   "Number of group name elements to leave alone when making a short group name.")
1332
1333 (defvar gnus-hidden-properties '(invisible t intangible t)
1334   "Property list to use for hiding text.")
1335
1336 (defvar gnus-modtime-botch nil
1337   "*Non-nil means .newsrc should be deleted prior to save.  
1338 Its use is due to the bogus appearance that .newsrc was modified on
1339 disc.")
1340
1341 ;; Hooks.
1342
1343 (defvar gnus-group-mode-hook nil
1344   "*A hook for Gnus group mode.")
1345
1346 (defvar gnus-summary-mode-hook nil
1347   "*A hook for Gnus summary mode.
1348 This hook is run before any variables are set in the summary buffer.")
1349
1350 (defvar gnus-article-mode-hook nil
1351   "*A hook for Gnus article mode.")
1352
1353 (defvar gnus-summary-prepare-exit-hook nil
1354   "*A hook called when preparing to exit from the summary buffer.
1355 It calls `gnus-summary-expire-articles' by default.")
1356 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1357
1358 (defvar gnus-summary-exit-hook nil
1359   "*A hook called on exit from the summary buffer.")
1360
1361 (defvar gnus-group-catchup-group-hook nil
1362   "*A hook run when catching up a group from the group buffer.")
1363
1364 (defvar gnus-open-server-hook nil
1365   "*A hook called just before opening connection to the news server.")
1366
1367 (defvar gnus-load-hook nil
1368   "*A hook run while Gnus is loaded.")
1369
1370 (defvar gnus-startup-hook nil
1371   "*A hook called at startup.
1372 This hook is called after Gnus is connected to the NNTP server.")
1373
1374 (defvar gnus-get-new-news-hook nil
1375   "*A hook run just before Gnus checks for new news.")
1376
1377 (defvar gnus-after-getting-new-news-hook nil
1378   "*A hook run after Gnus checks for new news.")
1379
1380 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1381   "*A function that is called to generate the group buffer.
1382 The function is called with three arguments: The first is a number;
1383 all group with a level less or equal to that number should be listed,
1384 if the second is non-nil, empty groups should also be displayed.  If
1385 the third is non-nil, it is a number.  No groups with a level lower
1386 than this number should be displayed.
1387
1388 The only current function implemented is `gnus-group-prepare-flat'.")
1389
1390 (defvar gnus-group-prepare-hook nil
1391   "*A hook called after the group buffer has been generated.
1392 If you want to modify the group buffer, you can use this hook.")
1393
1394 (defvar gnus-summary-prepare-hook nil
1395   "*A hook called after the summary buffer has been generated.
1396 If you want to modify the summary buffer, you can use this hook.")
1397
1398 (defvar gnus-summary-generate-hook nil
1399   "*A hook run just before generating the summary buffer.
1400 This hook is commonly used to customize threading variables and the
1401 like.")
1402
1403 (defvar gnus-article-prepare-hook nil
1404   "*A hook called after an article has been prepared in the article buffer.
1405 If you want to run a special decoding program like nkf, use this hook.")
1406
1407 ;(defvar gnus-article-display-hook nil
1408 ;  "*A hook called after the article is displayed in the article buffer.
1409 ;The hook is designed to change the contents of the article
1410 ;buffer.  Typical functions that this hook may contain are
1411 ;`gnus-article-hide-headers' (hide selected headers),
1412 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1413 ;`gnus-article-hide-signature' (hide signature) and
1414 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1415 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1416 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1417 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1418
1419 (defvar gnus-article-x-face-command
1420   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1421   "String or function to be executed to display an X-Face header.
1422 If it is a string, the command will be executed in a sub-shell
1423 asynchronously.  The compressed face will be piped to this command.")
1424
1425 (defvar gnus-article-x-face-too-ugly nil
1426   "Regexp matching posters whose face shouldn't be shown automatically.")
1427
1428 (defvar gnus-select-group-hook nil
1429   "*A hook called when a newsgroup is selected.
1430
1431 If you'd like to simplify subjects like the
1432 `gnus-summary-next-same-subject' command does, you can use the
1433 following hook:
1434
1435  (setq gnus-select-group-hook
1436       (list
1437         (lambda ()
1438           (mapcar (lambda (header)
1439                      (mail-header-set-subject
1440                       header
1441                       (gnus-simplify-subject
1442                        (mail-header-subject header) 're-only)))
1443                   gnus-newsgroup-headers))))")
1444
1445 (defvar gnus-select-article-hook nil
1446   "*A hook called when an article is selected.")
1447
1448 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1449   "*A hook called to apply kill files to a group.
1450 This hook is intended to apply a kill file to the selected newsgroup.
1451 The function `gnus-apply-kill-file' is called by default.
1452
1453 Since a general kill file is too heavy to use only for a few
1454 newsgroups, I recommend you to use a lighter hook function.  For
1455 example, if you'd like to apply a kill file to articles which contains
1456 a string `rmgroup' in subject in newsgroup `control', you can use the
1457 following hook:
1458
1459  (setq gnus-apply-kill-hook
1460       (list
1461         (lambda ()
1462           (cond ((string-match \"control\" gnus-newsgroup-name)
1463                  (gnus-kill \"Subject\" \"rmgroup\")
1464                  (gnus-expunge \"X\"))))))")
1465
1466 (defvar gnus-visual-mark-article-hook
1467   (list 'gnus-highlight-selected-summary)
1468   "*Hook run after selecting an article in the summary buffer.
1469 It is meant to be used for highlighting the article in some way.  It
1470 is not run if `gnus-visual' is nil.")
1471
1472 (defvar gnus-parse-headers-hook nil
1473   "*A hook called before parsing the headers.")
1474 (add-hook 'gnus-parse-headers-hook 'gnus-decode-rfc1522)
1475
1476 (defvar gnus-exit-group-hook nil
1477   "*A hook called when exiting (not quitting) summary mode.")
1478
1479 (defvar gnus-suspend-gnus-hook nil
1480   "*A hook called when suspending (not exiting) Gnus.")
1481
1482 (defvar gnus-exit-gnus-hook nil
1483   "*A hook called when exiting Gnus.")
1484
1485 (defvar gnus-after-exiting-gnus-hook nil
1486   "*A hook called after exiting Gnus.")
1487
1488 (defvar gnus-save-newsrc-hook nil
1489   "*A hook called before saving any of the newsrc files.")
1490
1491 (defvar gnus-save-quick-newsrc-hook nil
1492   "*A hook called just before saving the quick newsrc file.
1493 Can be used to turn version control on or off.")
1494
1495 (defvar gnus-save-standard-newsrc-hook nil
1496   "*A hook called just before saving the standard newsrc file.
1497 Can be used to turn version control on or off.")
1498
1499 (defvar gnus-summary-update-hook
1500   (list 'gnus-summary-highlight-line)
1501   "*A hook called when a summary line is changed.
1502 The hook will not be called if `gnus-visual' is nil.
1503
1504 The default function `gnus-summary-highlight-line' will
1505 highlight the line according to the `gnus-summary-highlight'
1506 variable.")
1507
1508 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1509   "*A hook called when a group line is changed.
1510 The hook will not be called if `gnus-visual' is nil.
1511
1512 The default function `gnus-group-highlight-line' will
1513 highlight the line according to the `gnus-group-highlight'
1514 variable.")
1515
1516 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1517   "*A hook called when an article is selected for the first time.
1518 The hook is intended to mark an article as read (or unread)
1519 automatically when it is selected.")
1520
1521 (defvar gnus-group-change-level-function nil
1522   "Function run when a group level is changed.
1523 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1524
1525 ;; Remove any hilit infestation.
1526 (add-hook 'gnus-startup-hook
1527           (lambda ()
1528             (remove-hook 'gnus-summary-prepare-hook
1529                          'hilit-rehighlight-buffer-quietly)
1530             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1531             (setq gnus-mark-article-hook
1532                   '(gnus-summary-mark-read-and-unread-as-read))
1533             (remove-hook 'gnus-article-prepare-hook
1534                          'hilit-rehighlight-buffer-quietly)))
1535
1536 \f
1537 ;; Internal variables
1538
1539 (defvar gnus-tree-buffer "*Tree*"
1540   "Buffer where Gnus thread trees are displayed.")
1541
1542 ;; Dummy variable.
1543 (defvar gnus-use-generic-from nil)
1544
1545 (defvar gnus-thread-indent-array nil)
1546 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1547
1548 (defvar gnus-newsrc-file-version nil)
1549
1550 (defvar gnus-method-history nil)
1551 ;; Variable holding the user answers to all method prompts.
1552
1553 (defvar gnus-group-history nil)
1554 ;; Variable holding the user answers to all group prompts.
1555
1556 (defvar gnus-server-alist nil
1557   "List of available servers.")
1558
1559 (defvar gnus-group-indentation-function nil)
1560
1561 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1562
1563 (defvar gnus-goto-missing-group-function nil)
1564
1565 (defvar gnus-override-subscribe-method nil)
1566
1567 (defvar gnus-group-goto-next-group-function nil
1568   "Function to override finding the next group after listing groups.")
1569
1570 (defconst gnus-article-mark-lists
1571   '((marked . tick) (replied . reply)
1572     (expirable . expire) (killed . killed)
1573     (bookmarks . bookmark) (dormant . dormant)
1574     (scored . score) (saved . save)
1575     (cached . cache)
1576     ))
1577
1578 ;; Avoid highlighting in kill files.
1579 (defvar gnus-summary-inhibit-highlight nil)
1580 (defvar gnus-newsgroup-selected-overlay nil)
1581
1582 (defvar gnus-inhibit-hiding nil)
1583 (defvar gnus-group-indentation "")
1584 (defvar gnus-inhibit-limiting nil)
1585 (defvar gnus-created-frames nil)
1586
1587 (defvar gnus-article-mode-map nil)
1588 (defvar gnus-dribble-buffer nil)
1589 (defvar gnus-headers-retrieved-by nil)
1590 (defvar gnus-article-reply nil)
1591 (defvar gnus-override-method nil)
1592 (defvar gnus-article-check-size nil)
1593
1594 (defvar gnus-current-score-file nil)
1595 (defvar gnus-newsgroup-adaptive-score-file nil)
1596 (defvar gnus-scores-exclude-files nil)
1597
1598 (defvar gnus-opened-servers nil)
1599
1600 (defvar gnus-current-move-group nil)
1601 (defvar gnus-current-copy-group nil)
1602 (defvar gnus-current-crosspost-group nil)
1603
1604 (defvar gnus-newsgroup-dependencies nil)
1605 (defvar gnus-newsgroup-async nil)
1606 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1607
1608 (defvar gnus-newsgroup-adaptive nil)
1609
1610 (defvar gnus-summary-display-table nil)
1611 (defvar gnus-summary-display-article-function nil)
1612
1613 (defvar gnus-summary-highlight-line-function nil
1614   "Function called after highlighting a summary line.")
1615
1616 (defvar gnus-group-line-format-alist
1617   `((?M gnus-tmp-marked-mark ?c)
1618     (?S gnus-tmp-subscribed ?c)
1619     (?L gnus-tmp-level ?d)
1620     (?N (cond ((eq number t) "*" )
1621               ((numberp number) 
1622                (int-to-string
1623                 (+ number
1624                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1625                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1626               (t number)) ?s)
1627     (?R gnus-tmp-number-of-read ?s)
1628     (?t gnus-tmp-number-total ?d)
1629     (?y gnus-tmp-number-of-unread ?s)
1630     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1631     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1632     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1633            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1634     (?g gnus-tmp-group ?s)
1635     (?G gnus-tmp-qualified-group ?s)
1636     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1637     (?D gnus-tmp-newsgroup-description ?s)
1638     (?o gnus-tmp-moderated ?c)
1639     (?O gnus-tmp-moderated-string ?s)
1640     (?p gnus-tmp-process-marked ?c)
1641     (?s gnus-tmp-news-server ?s)
1642     (?n gnus-tmp-news-method ?s)
1643     (?P gnus-group-indentation ?s)
1644     (?l gnus-tmp-grouplens ?s)
1645     (?z gnus-tmp-news-method-string ?s)
1646     (?u gnus-tmp-user-defined ?s)))
1647
1648 (defvar gnus-summary-line-format-alist
1649   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1650     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1651     (?s gnus-tmp-subject-or-nil ?s)
1652     (?n gnus-tmp-name ?s)
1653     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1654         ?s)
1655     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1656             gnus-tmp-from) ?s)
1657     (?F gnus-tmp-from ?s)
1658     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1659     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1660     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1661     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1662     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1663     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1664     (?L gnus-tmp-lines ?d)
1665     (?I gnus-tmp-indentation ?s)
1666     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1667     (?R gnus-tmp-replied ?c)
1668     (?\[ gnus-tmp-opening-bracket ?c)
1669     (?\] gnus-tmp-closing-bracket ?c)
1670     (?\> (make-string gnus-tmp-level ? ) ?s)
1671     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1672     (?i gnus-tmp-score ?d)
1673     (?z gnus-tmp-score-char ?c)
1674     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1675     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1676     (?U gnus-tmp-unread ?c)
1677     (?t (gnus-summary-number-of-articles-in-thread
1678          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1679         ?d)
1680     (?e (gnus-summary-number-of-articles-in-thread
1681          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1682         ?c)
1683     (?u gnus-tmp-user-defined ?s))
1684   "An alist of format specifications that can appear in summary lines,
1685 and what variables they correspond with, along with the type of the
1686 variable (string, integer, character, etc).")
1687
1688 (defvar gnus-summary-dummy-line-format-alist
1689   `((?S gnus-tmp-subject ?s)
1690     (?N gnus-tmp-number ?d)
1691     (?u gnus-tmp-user-defined ?s)))
1692
1693 (defvar gnus-summary-mode-line-format-alist
1694   `((?G gnus-tmp-group-name ?s)
1695     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1696     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1697     (?A gnus-tmp-article-number ?d)
1698     (?Z gnus-tmp-unread-and-unselected ?s)
1699     (?V gnus-version ?s)
1700     (?U gnus-tmp-unread ?d)
1701     (?S gnus-tmp-subject ?s)
1702     (?e gnus-tmp-unselected ?d)
1703     (?u gnus-tmp-user-defined ?s)
1704     (?d (length gnus-newsgroup-dormant) ?d)
1705     (?t (length gnus-newsgroup-marked) ?d)
1706     (?r (length gnus-newsgroup-reads) ?d)
1707     (?E gnus-newsgroup-expunged-tally ?d)
1708     (?s (gnus-current-score-file-nondirectory) ?s)))
1709
1710 (defvar gnus-article-mode-line-format-alist
1711   gnus-summary-mode-line-format-alist)
1712
1713 (defvar gnus-group-mode-line-format-alist
1714   `((?S gnus-tmp-news-server ?s)
1715     (?M gnus-tmp-news-method ?s)
1716     (?u gnus-tmp-user-defined ?s)
1717     (?: gnus-tmp-colon ?s)))
1718
1719 (defvar gnus-have-read-active-file nil)
1720
1721 (defconst gnus-maintainer
1722   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1723   "The mail address of the Gnus maintainers.")
1724
1725 (defconst gnus-version "September Gnus v0.91"
1726   "Version number for this version of Gnus.")
1727
1728 (defvar gnus-info-nodes
1729   '((gnus-group-mode "(gnus)The Group Buffer")
1730     (gnus-summary-mode "(gnus)The Summary Buffer")
1731     (gnus-article-mode "(gnus)The Article Buffer"))
1732   "Assoc list of major modes and related Info nodes.")
1733
1734 (defvar gnus-group-buffer "*Group*")
1735 (defvar gnus-summary-buffer "*Summary*")
1736 (defvar gnus-article-buffer "*Article*")
1737 (defvar gnus-server-buffer "*Server*")
1738
1739 (defvar gnus-work-buffer " *gnus work*")
1740
1741 (defvar gnus-original-article-buffer " *Original Article*")
1742 (defvar gnus-original-article nil)
1743
1744 (defvar gnus-buffer-list nil
1745   "Gnus buffers that should be killed on exit.")
1746
1747 (defvar gnus-slave nil
1748   "Whether this Gnus is a slave or not.")
1749
1750 (defvar gnus-variable-list
1751   '(gnus-newsrc-options gnus-newsrc-options-n
1752     gnus-newsrc-last-checked-date
1753     gnus-newsrc-alist gnus-server-alist
1754     gnus-killed-list gnus-zombie-list
1755     gnus-topic-topology gnus-topic-alist
1756     gnus-format-specs)
1757   "Gnus variables saved in the quick startup file.")
1758
1759 (defvar gnus-newsrc-options nil
1760   "Options line in the .newsrc file.")
1761
1762 (defvar gnus-newsrc-options-n nil
1763   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1764
1765 (defvar gnus-newsrc-last-checked-date nil
1766   "Date Gnus last asked server for new newsgroups.")
1767
1768 (defvar gnus-topic-topology nil
1769   "The complete topic hierarchy.")
1770
1771 (defvar gnus-topic-alist nil
1772   "The complete topic-group alist.")
1773
1774 (defvar gnus-newsrc-alist nil
1775   "Assoc list of read articles.
1776 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1777
1778 (defvar gnus-newsrc-hashtb nil
1779   "Hashtable of gnus-newsrc-alist.")
1780
1781 (defvar gnus-killed-list nil
1782   "List of killed newsgroups.")
1783
1784 (defvar gnus-killed-hashtb nil
1785   "Hash table equivalent of gnus-killed-list.")
1786
1787 (defvar gnus-zombie-list nil
1788   "List of almost dead newsgroups.")
1789
1790 (defvar gnus-description-hashtb nil
1791   "Descriptions of newsgroups.")
1792
1793 (defvar gnus-list-of-killed-groups nil
1794   "List of newsgroups that have recently been killed by the user.")
1795
1796 (defvar gnus-active-hashtb nil
1797   "Hashtable of active articles.")
1798
1799 (defvar gnus-moderated-list nil
1800   "List of moderated newsgroups.")
1801
1802 (defvar gnus-group-marked nil)
1803
1804 (defvar gnus-current-startup-file nil
1805   "Startup file for the current host.")
1806
1807 (defvar gnus-last-search-regexp nil
1808   "Default regexp for article search command.")
1809
1810 (defvar gnus-last-shell-command nil
1811   "Default shell command on article.")
1812
1813 (defvar gnus-current-select-method nil
1814   "The current method for selecting a newsgroup.")
1815
1816 (defvar gnus-group-list-mode nil)
1817
1818 (defvar gnus-article-internal-prepare-hook nil)
1819
1820 (defvar gnus-newsgroup-name nil)
1821 (defvar gnus-newsgroup-begin nil)
1822 (defvar gnus-newsgroup-end nil)
1823 (defvar gnus-newsgroup-last-rmail nil)
1824 (defvar gnus-newsgroup-last-mail nil)
1825 (defvar gnus-newsgroup-last-folder nil)
1826 (defvar gnus-newsgroup-last-file nil)
1827 (defvar gnus-newsgroup-auto-expire nil)
1828 (defvar gnus-newsgroup-active nil)
1829
1830 (defvar gnus-newsgroup-data nil)
1831 (defvar gnus-newsgroup-data-reverse nil)
1832 (defvar gnus-newsgroup-limit nil)
1833 (defvar gnus-newsgroup-limits nil)
1834
1835 (defvar gnus-newsgroup-unreads nil
1836   "List of unread articles in the current newsgroup.")
1837
1838 (defvar gnus-newsgroup-unselected nil
1839   "List of unselected unread articles in the current newsgroup.")
1840
1841 (defvar gnus-newsgroup-reads nil
1842   "Alist of read articles and article marks in the current newsgroup.")
1843
1844 (defvar gnus-newsgroup-expunged-tally nil)
1845
1846 (defvar gnus-newsgroup-marked nil
1847   "List of ticked articles in the current newsgroup (a subset of unread art).")
1848
1849 (defvar gnus-newsgroup-killed nil
1850   "List of ranges of articles that have been through the scoring process.")
1851
1852 (defvar gnus-newsgroup-cached nil
1853   "List of articles that come from the article cache.")
1854
1855 (defvar gnus-newsgroup-saved nil
1856   "List of articles that have been saved.")
1857
1858 (defvar gnus-newsgroup-kill-headers nil)
1859
1860 (defvar gnus-newsgroup-replied nil
1861   "List of articles that have been replied to in the current newsgroup.")
1862
1863 (defvar gnus-newsgroup-expirable nil
1864   "List of articles in the current newsgroup that can be expired.")
1865
1866 (defvar gnus-newsgroup-processable nil
1867   "List of articles in the current newsgroup that can be processed.")
1868
1869 (defvar gnus-newsgroup-bookmarks nil
1870   "List of articles in the current newsgroup that have bookmarks.")
1871
1872 (defvar gnus-newsgroup-dormant nil
1873   "List of dormant articles in the current newsgroup.")
1874
1875 (defvar gnus-newsgroup-scored nil
1876   "List of scored articles in the current newsgroup.")
1877
1878 (defvar gnus-newsgroup-headers nil
1879   "List of article headers in the current newsgroup.")
1880
1881 (defvar gnus-newsgroup-threads nil)
1882
1883 (defvar gnus-newsgroup-prepared nil
1884   "Whether the current group has been prepared properly.")
1885
1886 (defvar gnus-newsgroup-ancient nil
1887   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1888
1889 (defvar gnus-newsgroup-sparse nil)
1890
1891 (defvar gnus-current-article nil)
1892 (defvar gnus-article-current nil)
1893 (defvar gnus-current-headers nil)
1894 (defvar gnus-have-all-headers nil)
1895 (defvar gnus-last-article nil)
1896 (defvar gnus-newsgroup-history nil)
1897 (defvar gnus-current-kill-article nil)
1898
1899 ;; Save window configuration.
1900 (defvar gnus-prev-winconf nil)
1901
1902 (defvar gnus-summary-mark-positions nil)
1903 (defvar gnus-group-mark-positions nil)
1904
1905 (defvar gnus-reffed-article-number nil)
1906
1907 ;;; Let the byte-compiler know that we know about this variable.
1908 (defvar rmail-default-rmail-file)
1909
1910 (defvar gnus-cache-removable-articles nil)
1911
1912 (defvar gnus-dead-summary nil)
1913
1914 (defconst gnus-summary-local-variables
1915   '(gnus-newsgroup-name
1916     gnus-newsgroup-begin gnus-newsgroup-end
1917     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1918     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1919     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1920     gnus-newsgroup-unselected gnus-newsgroup-marked
1921     gnus-newsgroup-reads gnus-newsgroup-saved
1922     gnus-newsgroup-replied gnus-newsgroup-expirable
1923     gnus-newsgroup-processable gnus-newsgroup-killed
1924     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1925     gnus-newsgroup-headers gnus-newsgroup-threads
1926     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1927     gnus-current-article gnus-current-headers gnus-have-all-headers
1928     gnus-last-article gnus-article-internal-prepare-hook
1929     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1930     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1931     gnus-newsgroup-async gnus-thread-expunge-below
1932     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1933     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1934     gnus-newsgroup-history gnus-newsgroup-ancient
1935     gnus-newsgroup-sparse
1936     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1937     gnus-newsgroup-adaptive-score-file
1938     (gnus-newsgroup-expunged-tally . 0)
1939     gnus-cache-removable-articles gnus-newsgroup-cached
1940     gnus-newsgroup-data gnus-newsgroup-data-reverse
1941     gnus-newsgroup-limit gnus-newsgroup-limits)
1942   "Variables that are buffer-local to the summary buffers.")
1943
1944 (defconst gnus-bug-message
1945   "Sending a bug report to the Gnus Towers.
1946 ========================================
1947
1948 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1949 be sent to the Gnus Bug Exterminators.
1950
1951 At the bottom of the buffer you'll see lots of variable settings.
1952 Please do not delete those.  They will tell the Bug People what your
1953 environment is, so that it will be easier to locate the bugs.
1954
1955 If you have found a bug that makes Emacs go \"beep\", set
1956 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1957 and include the backtrace in your bug report.
1958
1959 Please describe the bug in annoying, painstaking detail.
1960
1961 Thank you for your help in stamping out bugs.
1962 ")
1963
1964 ;;; End of variables.
1965
1966 ;; Define some autoload functions Gnus might use.
1967 (eval-and-compile
1968
1969   ;; This little mapcar goes through the list below and marks the
1970   ;; symbols in question as autoloaded functions.
1971   (mapcar
1972    (lambda (package)
1973      (let ((interactive (nth 1 (memq ':interactive package))))
1974        (mapcar
1975         (lambda (function)
1976           (let (keymap)
1977             (when (consp function)
1978               (setq keymap (car (memq 'keymap function)))
1979               (setq function (car function)))
1980             (autoload function (car package) nil interactive keymap)))
1981         (if (eq (nth 1 package) ':interactive)
1982             (cdddr package)
1983           (cdr package)))))
1984    '(("metamail" metamail-buffer)
1985      ("info" Info-goto-node)
1986      ("hexl" hexl-hex-string-to-integer)
1987      ("pp" pp pp-to-string pp-eval-expression)
1988      ("mail-extr" mail-extract-address-components)
1989      ("nnmail" nnmail-split-fancy nnmail-article-group)
1990      ("nnvirtual" nnvirtual-catchup-group)
1991      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1992       timezone-make-sortable-date timezone-make-time-string)
1993      ("sendmail" mail-position-on-field mail-setup)
1994      ("rmailout" rmail-output)
1995      ("rnewspost" news-mail-other-window news-reply-yank-original
1996       news-caesar-buffer-body)
1997      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1998       rmail-show-message)
1999      ("gnus-soup" :interactive t
2000       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
2001       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
2002      ("nnsoup" nnsoup-pack-replies)
2003      ("gnus-scomo" :interactive t gnus-score-mode)
2004      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
2005       gnus-Folder-save-name gnus-folder-save-name)
2006      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
2007      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
2008       gnus-server-make-menu-bar gnus-article-make-menu-bar
2009       gnus-browse-make-menu-bar gnus-highlight-selected-summary
2010       gnus-summary-highlight-line gnus-carpal-setup-buffer
2011       gnus-group-highlight-line
2012       gnus-article-add-button gnus-insert-next-page-button
2013       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2014      ("gnus-vis" :interactive t
2015       gnus-article-push-button gnus-article-press-button
2016       gnus-article-highlight gnus-article-highlight-some
2017       gnus-article-highlight-headers gnus-article-highlight-signature
2018       gnus-article-add-buttons gnus-article-add-buttons-to-head
2019       gnus-article-next-button gnus-article-prev-button)
2020      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2021       gnus-demon-add-disconnection gnus-demon-add-handler
2022       gnus-demon-remove-handler)
2023      ("gnus-demon" :interactive t
2024       gnus-demon-init gnus-demon-cancel)
2025      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2026       gnus-tree-open gnus-tree-close)
2027      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2028       gnus-nocem-unwanted-article-p)
2029      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2030      ("gnus-srvr" gnus-browse-foreign-server)
2031      ("gnus-cite" :interactive t
2032       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2033       gnus-article-hide-citation gnus-article-fill-cited-article
2034       gnus-article-hide-citation-in-followups)
2035      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2036       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2037       gnus-execute gnus-expunge)
2038      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2039       gnus-cache-possibly-remove-articles gnus-cache-request-article
2040       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2041       gnus-cache-enter-remove-article gnus-cached-article-p
2042       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2043      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2044       gnus-cache-remove-article)
2045      ("gnus-score" :interactive t
2046       gnus-summary-increase-score gnus-summary-lower-score
2047       gnus-score-flush-cache gnus-score-close
2048       gnus-score-raise-same-subject-and-select
2049       gnus-score-raise-same-subject gnus-score-default
2050       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2051       gnus-score-lower-same-subject gnus-score-lower-thread
2052       gnus-possibly-score-headers gnus-summary-raise-score 
2053       gnus-summary-set-score gnus-summary-current-score)
2054      ("gnus-score"
2055       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2056       gnus-current-score-file-nondirectory gnus-score-adaptive
2057       gnus-score-find-trace gnus-score-file-name)
2058      ("gnus-edit" :interactive t gnus-score-customize)
2059      ("gnus-topic" :interactive t gnus-topic-mode)
2060      ("gnus-topic" gnus-topic-remove-group)
2061      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2062      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2063      ("gnus-uu" :interactive t
2064       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2065       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2066       gnus-uu-mark-by-regexp gnus-uu-mark-all
2067       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2068       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2069       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2070       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2071       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2072       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2073       gnus-uu-decode-binhex-view)
2074      ("gnus-msg" (gnus-summary-send-map keymap)
2075       gnus-mail-yank-original gnus-mail-send-and-exit
2076       gnus-sendmail-setup-mail gnus-article-mail
2077       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2078      ("gnus-msg" :interactive t
2079       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2080       gnus-summary-followup gnus-summary-followup-with-original
2081       gnus-summary-followup-and-reply
2082       gnus-summary-followup-and-reply-with-original
2083       gnus-summary-cancel-article gnus-summary-supersede-article
2084       gnus-post-news gnus-inews-news gnus-cancel-news
2085       gnus-summary-reply gnus-summary-reply-with-original
2086       gnus-summary-mail-forward gnus-summary-mail-other-window
2087       gnus-bug)
2088      ("gnus-picon" :interactive t gnus-article-display-picons
2089       gnus-group-display-picons gnus-picons-article-display-x-face)
2090      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2091       gnus-grouplens-mode)
2092      ("gnus-vm" gnus-vm-mail-setup)
2093      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2094       gnus-summary-save-article-vm gnus-yank-article))))
2095
2096 \f
2097
2098 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2099 ;; If you want the cursor to go somewhere else, set these two
2100 ;; functions in some startup hook to whatever you want.
2101 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2102 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2103
2104 ;;; Various macros and substs.
2105
2106 (defun gnus-header-from (header)
2107   (mail-header-from header))
2108
2109 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2110   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2111   (let ((tempvar (make-symbol "GnusStartBufferWindow"))
2112         (w (make-symbol "w"))
2113         (buf (make-symbol "buf")))
2114     `(let* ((,tempvar (selected-window))
2115             (,buf ,buffer)
2116             (,w (get-buffer-window ,buf 'visible)))
2117        (unwind-protect
2118            (progn
2119              (if ,w
2120                  (select-window ,w)
2121                (pop-to-buffer ,buf))
2122              ,@forms)
2123          (select-window ,tempvar)))))
2124
2125 (defmacro gnus-gethash (string hashtable)
2126   "Get hash value of STRING in HASHTABLE."
2127   `(symbol-value (intern-soft ,string ,hashtable)))
2128
2129 (defmacro gnus-sethash (string value hashtable)
2130   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2131   `(set (intern ,string ,hashtable) ,value))
2132
2133 (defmacro gnus-intern-safe (string hashtable)
2134   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2135   `(let ((symbol (intern ,string ,hashtable)))
2136      (or (boundp symbol)
2137          (set symbol nil))
2138      symbol))
2139
2140 (defmacro gnus-group-unread (group)
2141   "Get the currently computed number of unread articles in GROUP."
2142   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2143
2144 (defmacro gnus-group-entry (group)
2145   "Get the newsrc entry for GROUP."
2146   `(gnus-gethash ,group gnus-newsrc-hashtb))
2147
2148 (defmacro gnus-active (group)
2149   "Get active info on GROUP."
2150   `(gnus-gethash ,group gnus-active-hashtb))
2151
2152 (defmacro gnus-set-active (group active)
2153   "Set GROUP's active info."
2154   `(gnus-sethash ,group ,active gnus-active-hashtb))
2155
2156 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2157 ;;   function `substring' might cut on a middle of multi-octet
2158 ;;   character.
2159 (defun gnus-truncate-string (str width)
2160   (substring str 0 width))
2161
2162 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2163 ;; to limit the length of a string.  This function is necessary since
2164 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2165 (defsubst gnus-limit-string (str width)
2166   (if (> (length str) width)
2167       (substring str 0 width)
2168     str))
2169
2170 (defsubst gnus-simplify-subject-re (subject)
2171   "Remove \"Re:\" from subject lines."
2172   (if (string-match "^[Rr][Ee]: *" subject)
2173       (substring subject (match-end 0))
2174     subject))
2175
2176 (defsubst gnus-functionp (form)
2177   "Return non-nil if FORM is funcallable."
2178   (or (and (symbolp form) (fboundp form))
2179       (and (listp form) (eq (car form) 'lambda))))
2180
2181 (defsubst gnus-goto-char (point)
2182   (and point (goto-char point)))
2183
2184 (defmacro gnus-buffer-exists-p (buffer)
2185   `(let ((buffer ,buffer))
2186      (and buffer
2187           (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
2188                    buffer))))
2189
2190 (defmacro gnus-kill-buffer (buffer)
2191   `(let ((buf ,buffer))
2192      (if (gnus-buffer-exists-p buf)
2193          (kill-buffer buf))))
2194
2195 (defsubst gnus-point-at-bol ()
2196   "Return point at the beginning of the line."
2197   (let ((p (point)))
2198     (beginning-of-line)
2199     (prog1
2200         (point)
2201       (goto-char p))))
2202
2203 (defsubst gnus-point-at-eol ()
2204   "Return point at the end of the line."
2205   (let ((p (point)))
2206     (end-of-line)
2207     (prog1
2208         (point)
2209       (goto-char p))))
2210
2211 (defun gnus-alive-p ()
2212   "Say whether Gnus is running or not."
2213   (and gnus-group-buffer
2214        (get-buffer gnus-group-buffer)))
2215
2216 ;; Delete the current line (and the next N lines.);
2217 (defmacro gnus-delete-line (&optional n)
2218   `(delete-region (progn (beginning-of-line) (point))
2219                   (progn (forward-line ,(or n 1)) (point))))
2220
2221 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2222 (defvar gnus-init-inhibit nil)
2223 (defun gnus-read-init-file (&optional inhibit-next)
2224   (if gnus-init-inhibit
2225       (setq gnus-init-inhibit nil)
2226     (setq gnus-init-inhibit inhibit-next)
2227     (and gnus-init-file
2228          (or (and (file-exists-p gnus-init-file)
2229                   ;; Don't try to load a directory.
2230                   (not (file-directory-p gnus-init-file)))
2231              (file-exists-p (concat gnus-init-file ".el"))
2232              (file-exists-p (concat gnus-init-file ".elc")))
2233          (condition-case var
2234              (load gnus-init-file nil t)
2235            (error
2236             (error "Error in %s: %s" gnus-init-file var))))))
2237
2238 ;; Info access macros.
2239
2240 (defmacro gnus-info-group (info)
2241   `(nth 0 ,info))
2242 (defmacro gnus-info-rank (info)
2243   `(nth 1 ,info))
2244 (defmacro gnus-info-read (info)
2245   `(nth 2 ,info))
2246 (defmacro gnus-info-marks (info)
2247   `(nth 3 ,info))
2248 (defmacro gnus-info-method (info)
2249   `(nth 4 ,info))
2250 (defmacro gnus-info-params (info)
2251   `(nth 5 ,info))
2252
2253 (defmacro gnus-info-level (info)
2254   `(let ((rank (gnus-info-rank ,info)))
2255      (if (consp rank)
2256          (car rank)
2257        rank)))
2258 (defmacro gnus-info-score (info)
2259   `(let ((rank (gnus-info-rank ,info)))
2260      (or (and (consp rank) (cdr rank)) 0)))
2261
2262 (defmacro gnus-info-set-group (info group)
2263   `(setcar ,info ,group))
2264 (defmacro gnus-info-set-rank (info rank)
2265   `(setcar (nthcdr 1 ,info) ,rank))
2266 (defmacro gnus-info-set-read (info read)
2267   `(setcar (nthcdr 2 ,info) ,read))
2268 (defmacro gnus-info-set-marks (info marks)
2269   `(setcar (nthcdr 3 ,info) ,marks))
2270 (defmacro gnus-info-set-method (info method)
2271   `(setcar (nthcdr 4 ,info) ,method))
2272 (defmacro gnus-info-set-params (info params)
2273   `(setcar (nthcdr 5 ,info) ,params))
2274
2275 (defmacro gnus-info-set-level (info level)
2276   `(let ((rank (cdr ,info)))
2277      (if (consp (car rank))
2278          (setcar (car rank) ,level)
2279        (setcar rank ,level))))
2280 (defmacro gnus-info-set-score (info score)
2281   `(let ((rank (cdr ,info)))
2282      (if (consp (car rank))
2283          (setcdr (car rank) ,score)
2284        (setcar rank (cons (car rank) ,score)))))
2285
2286 (defmacro gnus-get-info (group)
2287   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2288
2289 (defun gnus-byte-code (func)
2290   "Return a form that can be `eval'ed based on FUNC."
2291   (let ((fval (symbol-function func)))
2292     (if (byte-code-function-p fval)
2293         (let ((flist (append fval nil)))
2294           (setcar flist 'byte-code)
2295           flist)
2296       (cons 'progn (cddr fval)))))
2297
2298 ;;; Load the compatability functions.
2299
2300 (require 'gnus-cus)
2301 (require 'gnus-ems)
2302
2303 \f
2304 ;;;
2305 ;;; Shutdown
2306 ;;;
2307
2308 (defvar gnus-shutdown-alist nil)
2309
2310 (defun gnus-add-shutdown (function &rest symbols)
2311   "Run FUNCTION whenever one of SYMBOLS is shut down."
2312   (push (cons function symbols) gnus-shutdown-alist))
2313
2314 (defun gnus-shutdown (symbol)
2315   "Shut down everything that waits for SYMBOL."
2316   (let ((alist gnus-shutdown-alist)
2317         entry)
2318     (while (setq entry (pop alist))
2319       (when (memq symbol (cdr entry))
2320         (funcall (car entry))))))
2321
2322 \f
2323
2324 ;; Format specs.  The chunks below are the machine-generated forms
2325 ;; that are to be evaled as the result of the default format strings.
2326 ;; We write them in here to get them byte-compiled.  That way the
2327 ;; default actions will be quite fast, while still retaining the full
2328 ;; flexibility of the user-defined format specs.
2329
2330 ;; First we have lots of dummy defvars to let the compiler know these
2331 ;; are really dynamic variables.
2332
2333 (defvar gnus-tmp-unread)
2334 (defvar gnus-tmp-replied)
2335 (defvar gnus-tmp-score-char)
2336 (defvar gnus-tmp-indentation)
2337 (defvar gnus-tmp-opening-bracket)
2338 (defvar gnus-tmp-lines)
2339 (defvar gnus-tmp-name)
2340 (defvar gnus-tmp-closing-bracket)
2341 (defvar gnus-tmp-subject-or-nil)
2342 (defvar gnus-tmp-subject)
2343 (defvar gnus-tmp-marked)
2344 (defvar gnus-tmp-marked-mark)
2345 (defvar gnus-tmp-subscribed)
2346 (defvar gnus-tmp-process-marked)
2347 (defvar gnus-tmp-number-of-unread)
2348 (defvar gnus-tmp-group-name)
2349 (defvar gnus-tmp-group)
2350 (defvar gnus-tmp-article-number)
2351 (defvar gnus-tmp-unread-and-unselected)
2352 (defvar gnus-tmp-news-method)
2353 (defvar gnus-tmp-news-server)
2354 (defvar gnus-tmp-article-number)
2355 (defvar gnus-mouse-face)
2356 (defvar gnus-mouse-face-prop)
2357
2358 (defun gnus-summary-line-format-spec ()
2359   (insert gnus-tmp-unread gnus-tmp-replied
2360           gnus-tmp-score-char gnus-tmp-indentation)
2361   (gnus-put-text-property
2362    (point)
2363    (progn
2364      (insert
2365       gnus-tmp-opening-bracket
2366       (format "%4d: %-20s"
2367               gnus-tmp-lines
2368               (if (> (length gnus-tmp-name) 20)
2369                   (substring gnus-tmp-name 0 20)
2370                 gnus-tmp-name))
2371       gnus-tmp-closing-bracket)
2372      (point))
2373    gnus-mouse-face-prop gnus-mouse-face)
2374   (insert " " gnus-tmp-subject-or-nil "\n"))
2375
2376 (defvar gnus-summary-line-format-spec
2377   (gnus-byte-code 'gnus-summary-line-format-spec))
2378
2379 (defun gnus-summary-dummy-line-format-spec ()
2380   (insert "*  ")
2381   (gnus-put-text-property
2382    (point)
2383    (progn
2384      (insert ":                          :")
2385      (point))
2386    gnus-mouse-face-prop gnus-mouse-face)
2387   (insert " " gnus-tmp-subject "\n"))
2388
2389 (defvar gnus-summary-dummy-line-format-spec
2390   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2391
2392 (defun gnus-group-line-format-spec ()
2393   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2394           gnus-tmp-process-marked
2395           gnus-group-indentation
2396           (format "%5s: " gnus-tmp-number-of-unread))
2397   (gnus-put-text-property
2398    (point)
2399    (progn
2400      (insert gnus-tmp-group "\n")
2401      (1- (point)))
2402    gnus-mouse-face-prop gnus-mouse-face))
2403 (defvar gnus-group-line-format-spec
2404   (gnus-byte-code 'gnus-group-line-format-spec))
2405
2406 (defvar gnus-format-specs
2407   `((version . ,emacs-version)
2408     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2409     (summary-dummy ,gnus-summary-dummy-line-format
2410                    ,gnus-summary-dummy-line-format-spec)
2411     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2412
2413 (defvar gnus-article-mode-line-format-spec nil)
2414 (defvar gnus-summary-mode-line-format-spec nil)
2415 (defvar gnus-group-mode-line-format-spec nil)
2416
2417 ;;; Phew.  All that gruft is over, fortunately.
2418
2419 \f
2420 ;;;
2421 ;;; Gnus Utility Functions
2422 ;;;
2423
2424 (defun gnus-extract-address-components (from)
2425   (let (name address)
2426     ;; First find the address - the thing with the @ in it.  This may
2427     ;; not be accurate in mail addresses, but does the trick most of
2428     ;; the time in news messages.
2429     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2430         (setq address (substring from (match-beginning 0) (match-end 0))))
2431     ;; Then we check whether the "name <address>" format is used.
2432     (and address
2433          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2434          ;; Linear white space is not required.
2435          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2436          (and (setq name (substring from 0 (match-beginning 0)))
2437               ;; Strip any quotes from the name.
2438               (string-match "\".*\"" name)
2439               (setq name (substring name 1 (1- (match-end 0))))))
2440     ;; If not, then "address (name)" is used.
2441     (or name
2442         (and (string-match "(.+)" from)
2443              (setq name (substring from (1+ (match-beginning 0))
2444                                    (1- (match-end 0)))))
2445         (and (string-match "()" from)
2446              (setq name address))
2447         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2448         ;; XOVER might not support folded From headers.
2449         (and (string-match "(.*" from)
2450              (setq name (substring from (1+ (match-beginning 0))
2451                                    (match-end 0)))))
2452     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2453     (list (or name from) (or address from))))
2454
2455 (defun gnus-fetch-field (field)
2456   "Return the value of the header FIELD of current article."
2457   (save-excursion
2458     (save-restriction
2459       (let ((case-fold-search t)
2460             (inhibit-point-motion-hooks t))
2461         (nnheader-narrow-to-headers)
2462         (message-fetch-field field)))))
2463
2464 (defun gnus-goto-colon ()
2465   (beginning-of-line)
2466   (search-forward ":" (gnus-point-at-eol) t))
2467
2468 ;;;###autoload
2469 (defun gnus-update-format (var)
2470   "Update the format specification near point."
2471   (interactive
2472    (list
2473     (save-excursion
2474       (eval-defun nil)
2475       ;; Find the end of the current word.
2476       (re-search-forward "[ \t\n]" nil t)
2477       ;; Search backward.
2478       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2479         (match-string 1)))))
2480   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2481                               (match-string 1 var))))
2482          (entry (assq type gnus-format-specs))
2483          value spec)
2484     (when entry
2485       (setq gnus-format-specs (delq entry gnus-format-specs)))
2486     (set
2487      (intern (format "%s-spec" var))
2488      (gnus-parse-format (setq value (symbol-value (intern var)))
2489                         (symbol-value (intern (format "%s-alist" var)))
2490                         (not (string-match "mode" var))))
2491     (setq spec (symbol-value (intern (format "%s-spec" var))))
2492     (push (list type value spec) gnus-format-specs)
2493
2494     (pop-to-buffer "*Gnus Format*")
2495     (erase-buffer)
2496     (lisp-interaction-mode)
2497     (insert (pp-to-string spec))))
2498
2499 (defun gnus-update-format-specifications (&optional force)
2500   "Update all (necessary) format specifications."
2501   ;; Make the indentation array.
2502   (gnus-make-thread-indent-array)
2503
2504   ;; See whether all the stored info needs to be flushed.
2505   (when (or force
2506             (not (equal emacs-version
2507                         (cdr (assq 'version gnus-format-specs)))))
2508     (setq gnus-format-specs nil))
2509
2510   ;; Go through all the formats and see whether they need updating.
2511   (let ((types '(summary summary-dummy group
2512                          summary-mode group-mode article-mode))
2513         new-format entry type val)
2514     (while (setq type (pop types))
2515       ;; Jump to the proper buffer to find out the value of
2516       ;; the variable, if possible.  (It may be buffer-local.)
2517       (save-excursion
2518         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2519               val)
2520           (when (and (boundp buffer)
2521                      (setq val (symbol-value buffer))
2522                      (get-buffer val)
2523                      (buffer-name (get-buffer val)))
2524             (set-buffer (get-buffer val)))
2525           (setq new-format (symbol-value
2526                             (intern (format "gnus-%s-line-format" type))))))
2527       (setq entry (cdr (assq type gnus-format-specs)))
2528       (if (and entry
2529                (equal (car entry) new-format))
2530           ;; Use the old format.
2531           (set (intern (format "gnus-%s-line-format-spec" type))
2532                (cadr entry))
2533         ;; This is a new format.
2534         (setq val
2535               (if (not (stringp new-format))
2536                   ;; This is a function call or something.
2537                   new-format
2538                 ;; This is a "real" format.
2539                 (gnus-parse-format
2540                  new-format
2541                  (symbol-value
2542                   (intern (format "gnus-%s-line-format-alist"
2543                                   (if (eq type 'article-mode)
2544                                       'summary-mode type))))
2545                  (not (string-match "mode$" (symbol-name type))))))
2546         ;; Enter the new format spec into the list.
2547         (if entry
2548             (progn
2549               (setcar (cdr entry) val)
2550               (setcar entry new-format))
2551           (push (list type new-format val) gnus-format-specs))
2552         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2553
2554   (unless (assq 'version gnus-format-specs)
2555     (push (cons 'version emacs-version) gnus-format-specs))
2556
2557   (gnus-update-group-mark-positions)
2558   (gnus-update-summary-mark-positions))
2559
2560 (defun gnus-update-summary-mark-positions ()
2561   "Compute where the summary marks are to go."
2562   (save-excursion
2563     (when (and gnus-summary-buffer
2564                (get-buffer gnus-summary-buffer)
2565                (buffer-name (get-buffer gnus-summary-buffer)))
2566       (set-buffer gnus-summary-buffer))
2567     (let ((gnus-replied-mark 129)
2568           (gnus-score-below-mark 130)
2569           (gnus-score-over-mark 130)
2570           (thread nil)
2571           (gnus-visual nil)
2572           (spec gnus-summary-line-format-spec)
2573           pos)
2574       (save-excursion
2575         (gnus-set-work-buffer)
2576         (let ((gnus-summary-line-format-spec spec))
2577           (gnus-summary-insert-line
2578            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2579           (goto-char (point-min))
2580           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2581                                              (- (point) 2)))))
2582           (goto-char (point-min))
2583           (push (cons 'replied (and (search-forward "\201" nil t) 
2584                                     (- (point) 2)))
2585                 pos)
2586           (goto-char (point-min))
2587           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2588                 pos)))
2589       (setq gnus-summary-mark-positions pos))))
2590
2591 (defun gnus-update-group-mark-positions ()
2592   (save-excursion
2593     (let ((gnus-process-mark 128)
2594           (gnus-group-marked '("dummy.group"))
2595           (gnus-active-hashtb (make-vector 10 0)))
2596       (gnus-set-active "dummy.group" '(0 . 0))
2597       (gnus-set-work-buffer)
2598       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2599       (goto-char (point-min))
2600       (setq gnus-group-mark-positions
2601             (list (cons 'process (and (search-forward "\200" nil t)
2602                                       (- (point) 2))))))))
2603
2604 (defvar gnus-mouse-face-0 'highlight)
2605 (defvar gnus-mouse-face-1 'highlight)
2606 (defvar gnus-mouse-face-2 'highlight)
2607 (defvar gnus-mouse-face-3 'highlight)
2608 (defvar gnus-mouse-face-4 'highlight)
2609
2610 (defun gnus-mouse-face-function (form type)
2611   `(gnus-put-text-property
2612     (point) (progn ,@form (point))
2613     gnus-mouse-face-prop
2614     ,(if (equal type 0)
2615          'gnus-mouse-face
2616        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2617
2618 (defvar gnus-face-0 'bold)
2619 (defvar gnus-face-1 'italic)
2620 (defvar gnus-face-2 'bold-italic)
2621 (defvar gnus-face-3 'bold)
2622 (defvar gnus-face-4 'bold)
2623
2624 (defun gnus-face-face-function (form type)
2625   `(gnus-put-text-property
2626     (point) (progn ,@form (point))
2627     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2628
2629 (defun gnus-max-width-function (el max-width)
2630   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2631   (if (symbolp el)
2632       `(if (> (length ,el) ,max-width)
2633            (substring ,el 0 ,max-width)
2634          ,el)
2635     `(let ((val (eval ,el)))
2636        (if (numberp val)
2637            (setq val (int-to-string val)))
2638        (if (> (length val) ,max-width)
2639            (substring val 0 ,max-width)
2640          val))))
2641
2642 (defun gnus-parse-format (format spec-alist &optional insert)
2643   ;; This function parses the FORMAT string with the help of the
2644   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2645   ;; string.  If the FORMAT string contains the specifiers %( and %)
2646   ;; the text between them will have the mouse-face text property.
2647   (if (string-match
2648        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2649        format)
2650       (gnus-parse-complex-format format spec-alist)
2651     ;; This is a simple format.
2652     (gnus-parse-simple-format format spec-alist insert)))
2653
2654 (defun gnus-parse-complex-format (format spec-alist)
2655   (save-excursion
2656     (gnus-set-work-buffer)
2657     (insert format)
2658     (goto-char (point-min))
2659     (while (re-search-forward "\"" nil t)
2660       (replace-match "\\\"" nil t))
2661     (goto-char (point-min))
2662     (insert "(\"")
2663     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2664       (let ((number (if (match-beginning 1)
2665                         (match-string 1) "0"))
2666             (delim (aref (match-string 2) 0)))
2667         (if (or (= delim ?\() (= delim ?\{))
2668             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2669                                    " " number " \""))
2670           (replace-match "\")\""))))
2671     (goto-char (point-max))
2672     (insert "\")")
2673     (goto-char (point-min))
2674     (let ((form (read (current-buffer))))
2675       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2676
2677 (defun gnus-complex-form-to-spec (form spec-alist)
2678   (delq nil
2679         (mapcar
2680          (lambda (sform)
2681            (if (stringp sform)
2682                (gnus-parse-simple-format sform spec-alist t)
2683              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2684                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2685                       (nth 1 sform))))
2686          form)))
2687
2688 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2689   ;; This function parses the FORMAT string with the help of the
2690   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2691   ;; string.
2692   (let ((max-width 0)
2693         spec flist fstring newspec elem beg result dontinsert)
2694     (save-excursion
2695       (gnus-set-work-buffer)
2696       (insert format)
2697       (goto-char (point-min))
2698       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2699                                 nil t)
2700         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2701               (setq newspec "%"
2702                     beg (1+ (match-beginning 0)))
2703           ;; First check if there are any specs that look anything like
2704           ;; "%12,12A", ie. with a "max width specification".  These have
2705           ;; to be treated specially.
2706           (if (setq beg (match-beginning 1))
2707               (setq max-width
2708                     (string-to-int
2709                      (buffer-substring
2710                       (1+ (match-beginning 1)) (match-end 1))))
2711             (setq max-width 0)
2712             (setq beg (match-beginning 2)))
2713           ;; Find the specification from `spec-alist'.
2714           (unless (setq elem (cdr (assq spec spec-alist)))
2715             (setq elem '("*" ?s)))
2716           ;; Treat user defined format specifiers specially.
2717           (when (eq (car elem) 'gnus-tmp-user-defined)
2718             (setq elem
2719                   (list
2720                    (list (intern (concat "gnus-user-format-function-"
2721                                          (match-string 3)))
2722                          'gnus-tmp-header) ?s))
2723             (delete-region (match-beginning 3) (match-end 3)))
2724           (if (not (zerop max-width))
2725               (let ((el (car elem)))
2726                 (cond ((= (cadr elem) ?c)
2727                        (setq el (list 'char-to-string el)))
2728                       ((= (cadr elem) ?d)
2729                        (setq el (list 'int-to-string el))))
2730                 (setq flist (cons (gnus-max-width-function el max-width)
2731                                   flist))
2732                 (setq newspec ?s))
2733             (progn
2734               (setq flist (cons (car elem) flist))
2735               (setq newspec (cadr elem)))))
2736         ;; Remove the old specification (and possibly a ",12" string).
2737         (delete-region beg (match-end 2))
2738         ;; Insert the new specification.
2739         (goto-char beg)
2740         (insert newspec))
2741       (setq fstring (buffer-substring 1 (point-max))))
2742     ;; Do some postprocessing to increase efficiency.
2743     (setq
2744      result
2745      (cond
2746       ;; Emptyness.
2747       ((string= fstring "")
2748        nil)
2749       ;; Not a format string.
2750       ((not (string-match "%" fstring))
2751        (list fstring))
2752       ;; A format string with just a single string spec.
2753       ((string= fstring "%s")
2754        (list (car flist)))
2755       ;; A single character.
2756       ((string= fstring "%c")
2757        (list (car flist)))
2758       ;; A single number.
2759       ((string= fstring "%d")
2760        (setq dontinsert)
2761        (if insert
2762            (list `(princ ,(car flist)))
2763          (list `(int-to-string ,(car flist)))))
2764       ;; Just lots of chars and strings.
2765       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2766        (nreverse flist))
2767       ;; A single string spec at the beginning of the spec.
2768       ((string-match "\\`%[sc][^%]+\\'" fstring)
2769        (list (car flist) (substring fstring 2)))
2770       ;; A single string spec in the middle of the spec.
2771       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2772        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2773       ;; A single string spec in the end of the spec.
2774       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2775        (list (match-string 1 fstring) (car flist)))
2776       ;; A more complex spec.
2777       (t
2778        (list (cons 'format (cons fstring (nreverse flist)))))))
2779
2780     (if insert
2781         (when result
2782           (if dontinsert
2783               result
2784             (cons 'insert result)))
2785       (cond ((stringp result)
2786              result)
2787             ((consp result)
2788              (cons 'concat result))
2789             (t "")))))
2790
2791 (defun gnus-eval-format (format &optional alist props)
2792   "Eval the format variable FORMAT, using ALIST.
2793 If PROPS, insert the result."
2794   (let ((form (gnus-parse-format format alist props)))
2795     (if props
2796         (gnus-add-text-properties (point) (progn (eval form) (point)) props)
2797       (eval form))))
2798
2799 (defun gnus-remove-text-with-property (prop)
2800   "Delete all text in the current buffer with text property PROP."
2801   (save-excursion
2802     (goto-char (point-min))
2803     (while (not (eobp))
2804       (while (get-text-property (point) prop)
2805         (delete-char 1))
2806       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2807
2808 (defun gnus-set-work-buffer ()
2809   (if (get-buffer gnus-work-buffer)
2810       (progn
2811         (set-buffer gnus-work-buffer)
2812         (erase-buffer))
2813     (set-buffer (get-buffer-create gnus-work-buffer))
2814     (kill-all-local-variables)
2815     (buffer-disable-undo (current-buffer))
2816     (gnus-add-current-to-buffer-list)))
2817
2818 ;; Article file names when saving.
2819
2820 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2821   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2822 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2823 Otherwise, it is like ~/News/news/group/num."
2824   (let ((default
2825           (expand-file-name
2826            (concat (if (gnus-use-long-file-name 'not-save)
2827                        (gnus-capitalize-newsgroup newsgroup)
2828                      (gnus-newsgroup-directory-form newsgroup))
2829                    "/" (int-to-string (mail-header-number headers)))
2830            gnus-article-save-directory)))
2831     (if (and last-file
2832              (string-equal (file-name-directory default)
2833                            (file-name-directory last-file))
2834              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2835         default
2836       (or last-file default))))
2837
2838 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2839   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2840 If variable `gnus-use-long-file-name' is non-nil, it is
2841 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2842   (let ((default
2843           (expand-file-name
2844            (concat (if (gnus-use-long-file-name 'not-save)
2845                        newsgroup
2846                      (gnus-newsgroup-directory-form newsgroup))
2847                    "/" (int-to-string (mail-header-number headers)))
2848            gnus-article-save-directory)))
2849     (if (and last-file
2850              (string-equal (file-name-directory default)
2851                            (file-name-directory last-file))
2852              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2853         default
2854       (or last-file default))))
2855
2856 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2857   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2858 If variable `gnus-use-long-file-name' is non-nil, it is
2859 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2860   (or last-file
2861       (expand-file-name
2862        (if (gnus-use-long-file-name 'not-save)
2863            (gnus-capitalize-newsgroup newsgroup)
2864          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2865        gnus-article-save-directory)))
2866
2867 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2868   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2869 If variable `gnus-use-long-file-name' is non-nil, it is
2870 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2871   (or last-file
2872       (expand-file-name
2873        (if (gnus-use-long-file-name 'not-save)
2874            newsgroup
2875          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2876        gnus-article-save-directory)))
2877
2878 ;; For subscribing new newsgroup
2879
2880 (defun gnus-subscribe-hierarchical-interactive (groups)
2881   (let ((groups (sort groups 'string<))
2882         prefixes prefix start ans group starts)
2883     (while groups
2884       (setq prefixes (list "^"))
2885       (while (and groups prefixes)
2886         (while (not (string-match (car prefixes) (car groups)))
2887           (setq prefixes (cdr prefixes)))
2888         (setq prefix (car prefixes))
2889         (setq start (1- (length prefix)))
2890         (if (and (string-match "[^\\.]\\." (car groups) start)
2891                  (cdr groups)
2892                  (setq prefix
2893                        (concat "^" (substring (car groups) 0 (match-end 0))))
2894                  (string-match prefix (cadr groups)))
2895             (progn
2896               (setq prefixes (cons prefix prefixes))
2897               (message "Descend hierarchy %s? ([y]nsq): "
2898                        (substring prefix 1 (1- (length prefix))))
2899               (setq ans (read-char))
2900               (cond ((= ans ?n)
2901                      (while (and groups
2902                                  (string-match prefix
2903                                                (setq group (car groups))))
2904                        (setq gnus-killed-list
2905                              (cons group gnus-killed-list))
2906                        (gnus-sethash group group gnus-killed-hashtb)
2907                        (setq groups (cdr groups)))
2908                      (setq starts (cdr starts)))
2909                     ((= ans ?s)
2910                      (while (and groups
2911                                  (string-match prefix
2912                                                (setq group (car groups))))
2913                        (gnus-sethash group group gnus-killed-hashtb)
2914                        (gnus-subscribe-alphabetically (car groups))
2915                        (setq groups (cdr groups)))
2916                      (setq starts (cdr starts)))
2917                     ((= ans ?q)
2918                      (while groups
2919                        (setq group (car groups))
2920                        (setq gnus-killed-list (cons group gnus-killed-list))
2921                        (gnus-sethash group group gnus-killed-hashtb)
2922                        (setq groups (cdr groups))))
2923                     (t nil)))
2924           (message "Subscribe %s? ([n]yq)" (car groups))
2925           (setq ans (read-char))
2926           (setq group (car groups))
2927           (cond ((= ans ?y)
2928                  (gnus-subscribe-alphabetically (car groups))
2929                  (gnus-sethash group group gnus-killed-hashtb))
2930                 ((= ans ?q)
2931                  (while groups
2932                    (setq group (car groups))
2933                    (setq gnus-killed-list (cons group gnus-killed-list))
2934                    (gnus-sethash group group gnus-killed-hashtb)
2935                    (setq groups (cdr groups))))
2936                 (t
2937                  (setq gnus-killed-list (cons group gnus-killed-list))
2938                  (gnus-sethash group group gnus-killed-hashtb)))
2939           (setq groups (cdr groups)))))))
2940
2941 (defun gnus-subscribe-randomly (newsgroup)
2942   "Subscribe new NEWSGROUP by making it the first newsgroup."
2943   (gnus-subscribe-newsgroup newsgroup))
2944
2945 (defun gnus-subscribe-alphabetically (newgroup)
2946   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2947   (let ((groups (cdr gnus-newsrc-alist))
2948         before)
2949     (while (and (not before) groups)
2950       (if (string< newgroup (caar groups))
2951           (setq before (caar groups))
2952         (setq groups (cdr groups))))
2953     (gnus-subscribe-newsgroup newgroup before)))
2954
2955 (defun gnus-subscribe-hierarchically (newgroup)
2956   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2957   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2958   (save-excursion
2959     (set-buffer (find-file-noselect gnus-current-startup-file))
2960     (let ((groupkey newgroup)
2961           before)
2962       (while (and (not before) groupkey)
2963         (goto-char (point-min))
2964         (let ((groupkey-re
2965                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2966           (while (and (re-search-forward groupkey-re nil t)
2967                       (progn
2968                         (setq before (match-string 1))
2969                         (string< before newgroup)))))
2970         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2971         (setq groupkey
2972               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2973                   (substring groupkey (match-beginning 1) (match-end 1)))))
2974       (gnus-subscribe-newsgroup newgroup before))))
2975
2976 (defun gnus-subscribe-interactively (group)
2977   "Subscribe the new GROUP interactively.
2978 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2979 it is killed."
2980   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2981       (gnus-subscribe-hierarchically group)
2982     (push group gnus-killed-list)))
2983
2984 (defun gnus-subscribe-zombies (group)
2985   "Make the new GROUP into a zombie group."
2986   (push group gnus-zombie-list))
2987
2988 (defun gnus-subscribe-killed (group)
2989   "Make the new GROUP a killed group."
2990   (push group gnus-killed-list))
2991
2992 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2993   "Subscribe new NEWSGROUP.
2994 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2995 the first newsgroup."
2996   ;; We subscribe the group by changing its level to `subscribed'.
2997   (gnus-group-change-level
2998    newsgroup gnus-level-default-subscribed
2999    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
3000   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
3001
3002 ;; For directories
3003
3004 (defun gnus-newsgroup-directory-form (newsgroup)
3005   "Make hierarchical directory name from NEWSGROUP name."
3006   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
3007         (len (length newsgroup))
3008         idx)
3009     ;; If this is a foreign group, we don't want to translate the
3010     ;; entire name.
3011     (if (setq idx (string-match ":" newsgroup))
3012         (aset newsgroup idx ?/)
3013       (setq idx 0))
3014     ;; Replace all occurrences of `.' with `/'.
3015     (while (< idx len)
3016       (if (= (aref newsgroup idx) ?.)
3017           (aset newsgroup idx ?/))
3018       (setq idx (1+ idx)))
3019     newsgroup))
3020
3021 (defun gnus-newsgroup-savable-name (group)
3022   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
3023   ;; with dots.
3024   (nnheader-replace-chars-in-string group ?/ ?.))
3025
3026 (defun gnus-make-directory (dir)
3027   "Make DIRECTORY recursively."
3028   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
3029   ;; of the many mysteries of the universe.
3030   (let* ((dir (expand-file-name dir default-directory))
3031          dirs err)
3032     (if (string-match "/$" dir)
3033         (setq dir (substring dir 0 (match-beginning 0))))
3034     ;; First go down the path until we find a directory that exists.
3035     (while (not (file-exists-p dir))
3036       (setq dirs (cons dir dirs))
3037       (string-match "/[^/]+$" dir)
3038       (setq dir (substring dir 0 (match-beginning 0))))
3039     ;; Then create all the subdirs.
3040     (while (and dirs (not err))
3041       (condition-case ()
3042           (make-directory (car dirs))
3043         (error (setq err t)))
3044       (setq dirs (cdr dirs)))
3045     ;; We return whether we were successful or not.
3046     (not dirs)))
3047
3048 (defun gnus-capitalize-newsgroup (newsgroup)
3049   "Capitalize NEWSGROUP name."
3050   (and (not (zerop (length newsgroup)))
3051        (concat (char-to-string (upcase (aref newsgroup 0)))
3052                (substring newsgroup 1))))
3053
3054 ;; Various... things.
3055
3056 (defun gnus-simplify-subject (subject &optional re-only)
3057   "Remove `Re:' and words in parentheses.
3058 If RE-ONLY is non-nil, strip leading `Re:'s only."
3059   (let ((case-fold-search t))           ;Ignore case.
3060     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3061     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3062       (setq subject (substring subject (match-end 0))))
3063     ;; Remove uninteresting prefixes.
3064     (if (and (not re-only)
3065              gnus-simplify-ignored-prefixes
3066              (string-match gnus-simplify-ignored-prefixes subject))
3067         (setq subject (substring subject (match-end 0))))
3068     ;; Remove words in parentheses from end.
3069     (unless re-only
3070       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3071         (setq subject (substring subject 0 (match-beginning 0)))))
3072     ;; Return subject string.
3073     subject))
3074
3075 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3076 ;; all whitespace.
3077 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3078 (defun gnus-simplify-buffer-fuzzy ()
3079   (goto-char (point-min))
3080   (while (search-forward "\t" nil t)
3081     (replace-match " " t t))
3082   (goto-char (point-min))
3083   (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
3084   (goto-char (match-beginning 0))
3085   (while (or
3086           (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
3087           (looking-at "^[[].*: .*[]]$"))
3088     (goto-char (point-min))
3089     (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
3090                               nil t)
3091       (replace-match "" t t))
3092     (goto-char (point-min))
3093     (while (re-search-forward "^[[].*: .*[]]$" nil t)
3094       (goto-char (match-end 0))
3095       (delete-char -1)
3096       (delete-region
3097        (progn (goto-char (match-beginning 0)))
3098        (re-search-forward ":"))))
3099   (goto-char (point-min))
3100   (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
3101     (replace-match "" t t))
3102   (goto-char (point-min))
3103   (while (re-search-forward "  +" nil t)
3104     (replace-match " " t t))
3105   (goto-char (point-min))
3106   (while (re-search-forward " $" nil t)
3107     (replace-match "" t t))
3108   (goto-char (point-min))
3109   (while (re-search-forward "^ +" nil t)
3110     (replace-match "" t t))
3111   (goto-char (point-min))
3112   (when gnus-simplify-subject-fuzzy-regexp
3113     (if (listp gnus-simplify-subject-fuzzy-regexp)
3114         (let ((list gnus-simplify-subject-fuzzy-regexp))
3115           (while list
3116             (goto-char (point-min))
3117             (while (re-search-forward (car list) nil t)
3118               (replace-match "" t t))
3119             (setq list (cdr list))))
3120       (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3121         (replace-match "" t t)))))
3122
3123 (defun gnus-simplify-subject-fuzzy (subject)
3124   "Siplify a subject string fuzzily."
3125   (save-excursion
3126     (gnus-set-work-buffer)
3127     (let ((case-fold-search t))
3128       (insert subject)
3129       (inline (gnus-simplify-buffer-fuzzy))
3130       (buffer-string))))
3131
3132 ;; Add the current buffer to the list of buffers to be killed on exit.
3133 (defun gnus-add-current-to-buffer-list ()
3134   (or (memq (current-buffer) gnus-buffer-list)
3135       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3136
3137 (defun gnus-string> (s1 s2)
3138   (not (or (string< s1 s2)
3139            (string= s1 s2))))
3140
3141 (defun gnus-read-active-file-p ()
3142   "Say whether the active file has been read from `gnus-select-method'."
3143   (memq gnus-select-method gnus-have-read-active-file))
3144
3145 ;;; General various misc type functions.
3146
3147 (defun gnus-clear-system ()
3148   "Clear all variables and buffers."
3149   ;; Clear Gnus variables.
3150   (let ((variables gnus-variable-list))
3151     (while variables
3152       (set (car variables) nil)
3153       (setq variables (cdr variables))))
3154   ;; Clear other internal variables.
3155   (setq gnus-list-of-killed-groups nil
3156         gnus-have-read-active-file nil
3157         gnus-newsrc-alist nil
3158         gnus-newsrc-hashtb nil
3159         gnus-killed-list nil
3160         gnus-zombie-list nil
3161         gnus-killed-hashtb nil
3162         gnus-active-hashtb nil
3163         gnus-moderated-list nil
3164         gnus-description-hashtb nil
3165         gnus-current-headers nil
3166         gnus-thread-indent-array nil
3167         gnus-newsgroup-headers nil
3168         gnus-newsgroup-name nil
3169         gnus-server-alist nil
3170         gnus-group-list-mode nil
3171         gnus-opened-servers nil
3172         gnus-current-select-method nil)
3173   (gnus-shutdown 'gnus)
3174   ;; Kill the startup file.
3175   (and gnus-current-startup-file
3176        (get-file-buffer gnus-current-startup-file)
3177        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3178   ;; Clear the dribble buffer.
3179   (gnus-dribble-clear)
3180   ;; Kill global KILL file buffer.
3181   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3182     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3183   (gnus-kill-buffer nntp-server-buffer)
3184   ;; Kill Gnus buffers.
3185   (while gnus-buffer-list
3186     (gnus-kill-buffer (pop gnus-buffer-list)))
3187   ;; Remove Gnus frames.
3188   (while gnus-created-frames
3189     (when (frame-live-p (car gnus-created-frames))
3190       ;; We slap a condition-case around this `delete-frame' to ensure 
3191       ;; agains errors if we try do delete the single frame that's left.
3192       (condition-case ()
3193           (delete-frame (car gnus-created-frames))
3194         (error nil)))
3195     (pop gnus-created-frames)))
3196
3197 (defun gnus-windows-old-to-new (setting)
3198   ;; First we take care of the really, really old Gnus 3 actions.
3199   (when (symbolp setting)
3200     (setq setting
3201           ;; Take care of ooold GNUS 3.x values.
3202           (cond ((eq setting 'SelectArticle) 'article)
3203                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3204                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3205                 (t setting))))
3206   (if (or (listp setting)
3207           (not (and gnus-window-configuration
3208                     (memq setting '(group summary article)))))
3209       setting
3210     (let* ((setting (if (eq setting 'group)
3211                         (if (assq 'newsgroup gnus-window-configuration)
3212                             'newsgroup
3213                           'newsgroups) setting))
3214            (elem (cadr (assq setting gnus-window-configuration)))
3215            (total (apply '+ elem))
3216            (types '(group summary article))
3217            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3218            (i 0)
3219            perc
3220            out)
3221       (while (< i 3)
3222         (or (not (numberp (nth i elem)))
3223             (zerop (nth i elem))
3224             (progn
3225               (setq perc  (/ (float (nth 0 elem)) total))
3226               (setq out (cons (if (eq pbuf (nth i types))
3227                                   (vector (nth i types) perc 'point)
3228                                 (vector (nth i types) perc))
3229                               out))))
3230         (setq i (1+ i)))
3231       (list (nreverse out)))))
3232
3233 ;;;###autoload
3234 (defun gnus-add-configuration (conf)
3235   "Add the window configuration CONF to `gnus-buffer-configuration'."
3236   (setq gnus-buffer-configuration
3237         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3238                          gnus-buffer-configuration))))
3239
3240 (defvar gnus-frame-list nil)
3241
3242 (defun gnus-configure-frame (split &optional window)
3243   "Split WINDOW according to SPLIT."
3244   (unless window
3245     (setq window (get-buffer-window (current-buffer))))
3246   (select-window window)
3247   ;; This might be an old-stylee buffer config.
3248   (when (vectorp split)
3249     (setq split (append split nil)))
3250   (when (or (consp (car split))
3251             (vectorp (car split)))
3252     (push 1.0 split)
3253     (push 'vertical split))
3254   ;; The SPLIT might be something that is to be evaled to
3255   ;; return a new SPLIT.
3256   (while (and (not (assq (car split) gnus-window-to-buffer))
3257               (gnus-functionp (car split)))
3258     (setq split (eval split)))
3259   (let* ((type (car split))
3260          (subs (cddr split))
3261          (len (if (eq type 'horizontal) (window-width) (window-height)))
3262          (total 0)
3263          (window-min-width (or gnus-window-min-width window-min-width))
3264          (window-min-height (or gnus-window-min-height window-min-height))
3265          s result new-win rest comp-subs size sub)
3266     (cond
3267      ;; Nothing to do here.
3268      ((null split))
3269      ;; Don't switch buffers.
3270      ((null type)
3271       (and (memq 'point split) window))
3272      ;; This is a buffer to be selected.
3273      ((not (memq type '(frame horizontal vertical)))
3274       (let ((buffer (cond ((stringp type) type)
3275                           (t (cdr (assq type gnus-window-to-buffer)))))
3276             buf)
3277         (unless buffer
3278           (error "Illegal buffer type: %s" type))
3279         (unless (setq buf (get-buffer (if (symbolp buffer)
3280                                           (symbol-value buffer) buffer)))
3281           (setq buf (get-buffer-create (if (symbolp buffer)
3282                                            (symbol-value buffer) buffer))))
3283         (switch-to-buffer buf)
3284         ;; We return the window if it has the `point' spec.
3285         (and (memq 'point split) window)))
3286      ;; This is a frame split.
3287      ((eq type 'frame)
3288       (unless gnus-frame-list
3289         (setq gnus-frame-list (list (window-frame
3290                                      (get-buffer-window (current-buffer))))))
3291       (let ((i 0)
3292             params frame fresult)
3293         (while (< i (length subs))
3294           ;; Frame parameter is gotten from the sub-split.
3295           (setq params (cadr (elt subs i)))
3296           ;; It should be a list.
3297           (unless (listp params)
3298             (setq params nil))
3299           ;; Create a new frame?
3300           (unless (setq frame (elt gnus-frame-list i))
3301             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3302             (push frame gnus-created-frames))
3303           ;; Is the old frame still alive?
3304           (unless (frame-live-p frame)
3305             (setcar (nthcdr i gnus-frame-list)
3306                     (setq frame (make-frame params))))
3307           ;; Select the frame in question and do more splits there.
3308           (select-frame frame)
3309           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3310           (incf i))
3311         ;; Select the frame that has the selected buffer.
3312         (when fresult
3313           (select-frame (window-frame fresult)))))
3314      ;; This is a normal split.
3315      (t
3316       (when (> (length subs) 0)
3317         ;; First we have to compute the sizes of all new windows.
3318         (while subs
3319           (setq sub (append (pop subs) nil))
3320           (while (and (not (assq (car sub) gnus-window-to-buffer))
3321                       (gnus-functionp (car sub)))
3322             (setq sub (eval sub)))
3323           (when sub
3324             (push sub comp-subs)
3325             (setq size (cadar comp-subs))
3326             (cond ((equal size 1.0)
3327                    (setq rest (car comp-subs))
3328                    (setq s 0))
3329                   ((floatp size)
3330                    (setq s (floor (* size len))))
3331                   ((integerp size)
3332                    (setq s size))
3333                   (t
3334                    (error "Illegal size: %s" size)))
3335             ;; Try to make sure that we are inside the safe limits.
3336             (cond ((zerop s))
3337                   ((eq type 'horizontal)
3338                    (setq s (max s window-min-width)))
3339                   ((eq type 'vertical)
3340                    (setq s (max s window-min-height))))
3341             (setcar (cdar comp-subs) s)
3342             (incf total s)))
3343         ;; Take care of the "1.0" spec.
3344         (if rest
3345             (setcar (cdr rest) (- len total))
3346           (error "No 1.0 specs in %s" split))
3347         ;; The we do the actual splitting in a nice recursive
3348         ;; fashion.
3349         (setq comp-subs (nreverse comp-subs))
3350         (while comp-subs
3351           (if (null (cdr comp-subs))
3352               (setq new-win window)
3353             (setq new-win
3354                   (split-window window (cadar comp-subs)
3355                                 (eq type 'horizontal))))
3356           (setq result (or (gnus-configure-frame
3357                             (car comp-subs) window) result))
3358           (select-window new-win)
3359           (setq window new-win)
3360           (setq comp-subs (cdr comp-subs))))
3361       ;; Return the proper window, if any.
3362       (when result
3363         (select-window result))))))
3364
3365 (defvar gnus-frame-split-p nil)
3366
3367 (defun gnus-configure-windows (setting &optional force)
3368   (setq setting (gnus-windows-old-to-new setting))
3369   (let ((split (if (symbolp setting)
3370                    (cadr (assq setting gnus-buffer-configuration))
3371                  setting))
3372         all-visible)
3373
3374     (setq gnus-frame-split-p nil)
3375
3376     (unless split
3377       (error "No such setting: %s" setting))
3378
3379     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3380              (not force))
3381         ;; All the windows mentioned are already visible, so we just
3382         ;; put point in the assigned buffer, and do not touch the
3383         ;; winconf.
3384         (select-window all-visible)
3385
3386       ;; Either remove all windows or just remove all Gnus windows.
3387       (let ((frame (selected-frame)))
3388         (unwind-protect
3389             (if gnus-use-full-window
3390                 ;; We want to remove all other windows.
3391                 (if (not gnus-frame-split-p)
3392                     ;; This is not a `frame' split, so we ignore the
3393                     ;; other frames.  
3394                     (delete-other-windows)
3395                   ;; This is a `frame' split, so we delete all windows
3396                   ;; on all frames.
3397                   (mapcar 
3398                    (lambda (frame)
3399                      (unless (eq (cdr (assq 'minibuffer
3400                                             (frame-parameters frame)))
3401                                  'only)
3402                        (select-frame frame)
3403                        (delete-other-windows)))
3404                    (frame-list)))
3405               ;; Just remove some windows.
3406               (gnus-remove-some-windows)
3407               (switch-to-buffer nntp-server-buffer))
3408           (select-frame frame)))
3409
3410       (switch-to-buffer nntp-server-buffer)
3411       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3412
3413 (defun gnus-all-windows-visible-p (split)
3414   "Say whether all buffers in SPLIT are currently visible.
3415 In particular, the value returned will be the window that
3416 should have point."
3417   (let ((stack (list split))
3418         (all-visible t)
3419         type buffer win buf)
3420     (while (and (setq split (pop stack))
3421                 all-visible)
3422       ;; Be backwards compatible.
3423       (when (vectorp split)
3424         (setq split (append split nil)))
3425       (when (or (consp (car split))
3426                 (vectorp (car split)))
3427         (push 1.0 split)
3428         (push 'vertical split))
3429       ;; The SPLIT might be something that is to be evaled to
3430       ;; return a new SPLIT.
3431       (while (and (not (assq (car split) gnus-window-to-buffer))
3432                   (gnus-functionp (car split)))
3433         (setq split (eval split)))
3434
3435       (setq type (elt split 0))
3436       (cond
3437        ;; Nothing here.
3438        ((null split) t)
3439        ;; A buffer.
3440        ((not (memq type '(horizontal vertical frame)))
3441         (setq buffer (cond ((stringp type) type)
3442                            (t (cdr (assq type gnus-window-to-buffer)))))
3443         (unless buffer
3444           (error "Illegal buffer type: %s" type))
3445         (when (setq buf (get-buffer (if (symbolp buffer)
3446                                         (symbol-value buffer)
3447                                       buffer)))
3448           (setq win (get-buffer-window buf t)))
3449         (if win
3450             (when (memq 'point split)
3451                 (setq all-visible win))
3452           (setq all-visible nil)))
3453        (t
3454         (when (eq type 'frame)
3455           (setq gnus-frame-split-p t))
3456         (setq stack (append (cddr split) stack)))))
3457     (unless (eq all-visible t)
3458       all-visible)))
3459
3460 (defun gnus-window-top-edge (&optional window)
3461   (nth 1 (window-edges window)))
3462
3463 (defun gnus-remove-some-windows ()
3464   (let ((buffers gnus-window-to-buffer)
3465         buf bufs lowest-buf lowest)
3466     (save-excursion
3467       ;; Remove windows on all known Gnus buffers.
3468       (while buffers
3469         (setq buf (cdar buffers))
3470         (if (symbolp buf)
3471             (setq buf (and (boundp buf) (symbol-value buf))))
3472         (and buf
3473              (get-buffer-window buf)
3474              (progn
3475                (setq bufs (cons buf bufs))
3476                (pop-to-buffer buf)
3477                (if (or (not lowest)
3478                        (< (gnus-window-top-edge) lowest))
3479                    (progn
3480                      (setq lowest (gnus-window-top-edge))
3481                      (setq lowest-buf buf)))))
3482         (setq buffers (cdr buffers)))
3483       ;; Remove windows on *all* summary buffers.
3484       (walk-windows
3485        (lambda (win)
3486          (let ((buf (window-buffer win)))
3487            (if (string-match    "^\\*Summary" (buffer-name buf))
3488                (progn
3489                  (setq bufs (cons buf bufs))
3490                  (pop-to-buffer buf)
3491                  (if (or (not lowest)
3492                          (< (gnus-window-top-edge) lowest))
3493                      (progn
3494                        (setq lowest-buf buf)
3495                        (setq lowest (gnus-window-top-edge)))))))))
3496       (and lowest-buf
3497            (progn
3498              (pop-to-buffer lowest-buf)
3499              (switch-to-buffer nntp-server-buffer)))
3500       (while bufs
3501         (and (not (eq (car bufs) lowest-buf))
3502              (delete-windows-on (car bufs)))
3503         (setq bufs (cdr bufs))))))
3504
3505 (defun gnus-version ()
3506   "Version numbers of this version of Gnus."
3507   (interactive)
3508   (let ((methods gnus-valid-select-methods)
3509         (mess gnus-version)
3510         meth)
3511     ;; Go through all the legal select methods and add their version
3512     ;; numbers to the total version string.  Only the backends that are
3513     ;; currently in use will have their message numbers taken into
3514     ;; consideration.
3515     (while methods
3516       (setq meth (intern (concat (caar methods) "-version")))
3517       (and (boundp meth)
3518            (stringp (symbol-value meth))
3519            (setq mess (concat mess "; " (symbol-value meth))))
3520       (setq methods (cdr methods)))
3521     (gnus-message 2 mess)))
3522
3523 (defun gnus-info-find-node ()
3524   "Find Info documentation of Gnus."
3525   (interactive)
3526   ;; Enlarge info window if needed.
3527   (let ((mode major-mode)
3528         gnus-info-buffer)
3529     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3530     (setq gnus-info-buffer (current-buffer))
3531     (gnus-configure-windows 'info)))
3532
3533 (defun gnus-days-between (date1 date2)
3534   ;; Return the number of days between date1 and date2.
3535   (- (gnus-day-number date1) (gnus-day-number date2)))
3536
3537 (defun gnus-day-number (date)
3538   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3539                      (timezone-parse-date date))))
3540     (timezone-absolute-from-gregorian
3541      (nth 1 dat) (nth 2 dat) (car dat))))
3542
3543 (defun gnus-encode-date (date)
3544   "Convert DATE to internal time."
3545   (let* ((parse (timezone-parse-date date))
3546          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3547          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3548     (encode-time (caddr time) (cadr time) (car time)
3549                  (caddr date) (cadr date) (car date) (nth 4 date))))
3550
3551 (defun gnus-time-minus (t1 t2)
3552   "Subtract two internal times."
3553   (let ((borrow (< (cadr t1) (cadr t2))))
3554     (list (- (car t1) (car t2) (if borrow 1 0))
3555           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3556
3557 (defun gnus-file-newer-than (file date)
3558   (let ((fdate (nth 5 (file-attributes file))))
3559     (or (> (car fdate) (car date))
3560         (and (= (car fdate) (car date))
3561              (> (nth 1 fdate) (nth 1 date))))))
3562
3563 (defmacro gnus-local-set-keys (&rest plist)
3564   "Set the keys in PLIST in the current keymap."
3565   `(gnus-define-keys-1 (current-local-map) ',plist))
3566
3567 (defmacro gnus-define-keys (keymap &rest plist)
3568   "Define all keys in PLIST in KEYMAP."
3569   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3570
3571 (put 'gnus-define-keys 'lisp-indent-function 1)
3572 (put 'gnus-define-keys 'lisp-indent-hook 1)
3573 (put 'gnus-define-keymap 'lisp-indent-function 1)
3574 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3575
3576 (defmacro gnus-define-keymap (keymap &rest plist)
3577   "Define all keys in PLIST in KEYMAP."
3578   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3579
3580 (defun gnus-define-keys-1 (keymap plist)
3581   (when (null keymap)
3582     (error "Can't set keys in a null keymap"))
3583   (cond ((symbolp keymap)
3584          (setq keymap (symbol-value keymap)))
3585         ((keymapp keymap))
3586         ((listp keymap)
3587          (set (car keymap) nil)
3588          (define-prefix-command (car keymap))
3589          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3590          (setq keymap (symbol-value (car keymap)))))
3591   (let (key)
3592     (while plist
3593       (when (symbolp (setq key (pop plist)))
3594         (setq key (symbol-value key)))
3595       (define-key keymap key (pop plist)))))
3596
3597 (defun gnus-group-read-only-p (&optional group)
3598   "Check whether GROUP supports editing or not.
3599 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3600 that that variable is buffer-local to the summary buffers."
3601   (let ((group (or group gnus-newsgroup-name)))
3602     (not (gnus-check-backend-function 'request-replace-article group))))
3603
3604 (defun gnus-group-total-expirable-p (group)
3605   "Check whether GROUP is total-expirable or not."
3606   (let ((params (gnus-info-params (gnus-get-info group))))
3607     (or (memq 'total-expire params)
3608         (cdr (assq 'total-expire params)) ; (total-expire . t)
3609         (and gnus-total-expirable-newsgroups ; Check var.
3610              (string-match gnus-total-expirable-newsgroups group)))))
3611
3612 (defun gnus-group-auto-expirable-p (group)
3613   "Check whether GROUP is total-expirable or not."
3614   (let ((params (gnus-info-params (gnus-get-info group))))
3615     (or (memq 'auto-expire params)
3616         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3617         (and gnus-auto-expirable-newsgroups ; Check var.
3618              (string-match gnus-auto-expirable-newsgroups group)))))
3619
3620 (defun gnus-virtual-group-p (group)
3621   "Say whether GROUP is virtual or not."
3622   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3623                         gnus-valid-select-methods)))
3624
3625 (defun gnus-news-group-p (group &optional article)
3626   "Return non-nil if GROUP (and ARTICLE) come from a news server."
3627   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
3628       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
3629            (eq (gnus-request-type group article) 'news))))
3630
3631 (defsubst gnus-simplify-subject-fully (subject)
3632   "Simplify a subject string according to the user's wishes."
3633   (cond
3634    ((null gnus-summary-gather-subject-limit)
3635     (gnus-simplify-subject-re subject))
3636    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3637     (gnus-simplify-subject-fuzzy subject))
3638    ((numberp gnus-summary-gather-subject-limit)
3639     (gnus-limit-string (gnus-simplify-subject-re subject)
3640                        gnus-summary-gather-subject-limit))
3641    (t
3642     subject)))
3643
3644 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3645   "Check whether two subjects are equal.  If optional argument
3646 simple-first is t, first argument is already simplified."
3647   (cond
3648    ((null simple-first)
3649     (equal (gnus-simplify-subject-fully s1)
3650            (gnus-simplify-subject-fully s2)))
3651    (t
3652     (equal s1
3653            (gnus-simplify-subject-fully s2)))))
3654
3655 ;; Returns a list of writable groups.
3656 (defun gnus-writable-groups ()
3657   (let ((alist gnus-newsrc-alist)
3658         groups group)
3659     (while (setq group (car (pop alist)))
3660       (unless (gnus-group-read-only-p group)
3661         (push group groups)))
3662     (nreverse groups)))
3663
3664 (defun gnus-completing-read (default prompt &rest args)
3665   ;; Like `completing-read', except that DEFAULT is the default argument.
3666   (let* ((prompt (if default 
3667                      (concat prompt " (default " default ") ")
3668                    (concat prompt " ")))
3669          (answer (apply 'completing-read prompt args)))
3670     (if (or (null answer) (zerop (length answer)))
3671         default
3672       answer)))
3673
3674 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3675 ;; the echo area.
3676 (defun gnus-y-or-n-p (prompt)
3677   (prog1
3678       (y-or-n-p prompt)
3679     (message "")))
3680
3681 (defun gnus-yes-or-no-p (prompt)
3682   (prog1
3683       (yes-or-no-p prompt)
3684     (message "")))
3685
3686 ;; Check whether to use long file names.
3687 (defun gnus-use-long-file-name (symbol)
3688   ;; The variable has to be set...
3689   (and gnus-use-long-file-name
3690        ;; If it isn't a list, then we return t.
3691        (or (not (listp gnus-use-long-file-name))
3692            ;; If it is a list, and the list contains `symbol', we
3693            ;; return nil.
3694            (not (memq symbol gnus-use-long-file-name)))))
3695
3696 ;; I suspect there's a better way, but I haven't taken the time to do
3697 ;; it yet. -erik selberg@cs.washington.edu
3698 (defun gnus-dd-mmm (messy-date)
3699   "Return a string like DD-MMM from a big messy string"
3700   (let ((datevec (condition-case () (timezone-parse-date messy-date) 
3701                    (error nil))))
3702     (if (not datevec)
3703         "??-???"
3704       (format "%2s-%s"
3705               (condition-case ()
3706                   ;; Make sure leading zeroes are stripped.
3707                   (number-to-string (string-to-number (aref datevec 2)))
3708                 (error "??"))
3709               (capitalize
3710                (or (car
3711                     (nth (1- (string-to-number (aref datevec 1)))
3712                          timezone-months-assoc))
3713                    "???"))))))
3714
3715 (defun gnus-mode-string-quote (string)
3716   "Quote all \"%\" in STRING."
3717   (save-excursion
3718     (gnus-set-work-buffer)
3719     (insert string)
3720     (goto-char (point-min))
3721     (while (search-forward "%" nil t)
3722       (insert "%"))
3723     (buffer-string)))
3724
3725 ;; Make a hash table (default and minimum size is 255).
3726 ;; Optional argument HASHSIZE specifies the table size.
3727 (defun gnus-make-hashtable (&optional hashsize)
3728   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3729
3730 ;; Make a number that is suitable for hashing; bigger than MIN and one
3731 ;; less than 2^x.
3732 (defun gnus-create-hash-size (min)
3733   (let ((i 1))
3734     (while (< i min)
3735       (setq i (* 2 i)))
3736     (1- i)))
3737
3738 ;; Show message if message has a lower level than `gnus-verbose'.
3739 ;; Guideline for numbers:
3740 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3741 ;; for things that take a long time, 7 - not very important messages
3742 ;; on stuff, 9 - messages inside loops.
3743 (defun gnus-message (level &rest args)
3744   (if (<= level gnus-verbose)
3745       (apply 'message args)
3746     ;; We have to do this format thingy here even if the result isn't
3747     ;; shown - the return value has to be the same as the return value
3748     ;; from `message'.
3749     (apply 'format args)))
3750
3751 (defun gnus-error (level &rest args)
3752   "Beep an error if `gnus-verbose' is on LEVEL or less."
3753   (when (<= (floor level) gnus-verbose)
3754     (apply 'message args)
3755     (ding)
3756     (let (duration)
3757       (when (and (floatp level)
3758                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
3759         (sit-for duration))))
3760   nil)
3761
3762 ;; Generate a unique new group name.
3763 (defun gnus-generate-new-group-name (leaf)
3764   (let ((name leaf)
3765         (num 0))
3766     (while (gnus-gethash name gnus-newsrc-hashtb)
3767       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3768     name))
3769
3770 (defsubst gnus-hide-text (b e props)
3771   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
3772   (gnus-add-text-properties b e props)
3773   (when (memq 'intangible props)
3774     (gnus-put-text-property (max (1- b) (point-min))
3775                        b 'intangible (cddr (memq 'intangible props)))))
3776
3777 (defsubst gnus-unhide-text (b e)
3778   "Remove hidden text properties from region between B and E."
3779   (remove-text-properties b e gnus-hidden-properties)
3780   (when (memq 'intangible gnus-hidden-properties)
3781     (gnus-put-text-property (max (1- b) (point-min))
3782                        b 'intangible nil)))
3783
3784 (defun gnus-hide-text-type (b e type)
3785   "Hide text of TYPE between B and E."
3786   (gnus-hide-text b e (cons 'gnus-type (cons type gnus-hidden-properties))))
3787
3788 ;; Find out whether the gnus-visual TYPE is wanted.
3789 (defun gnus-visual-p (&optional type class)
3790   (and gnus-visual                      ; Has to be non-nil, at least.
3791        (if (not type)                   ; We don't care about type.
3792            gnus-visual
3793          (if (listp gnus-visual)        ; It's a list, so we check it.
3794              (or (memq type gnus-visual)
3795                  (memq class gnus-visual))
3796            t))))
3797
3798 (defun gnus-parent-headers (headers &optional generation)
3799   "Return the headers of the GENERATIONeth parent of HEADERS."
3800   (unless generation 
3801     (setq generation 1))
3802   (let (references parent)
3803     (while (and headers (not (zerop generation)))
3804       (setq references (mail-header-references headers))
3805       (when (and references
3806                  (setq parent (gnus-parent-id references))
3807                  (setq headers (car (gnus-id-to-thread parent))))
3808         (decf generation)))
3809     headers))
3810
3811 (defun gnus-parent-id (references)
3812   "Return the last Message-ID in REFERENCES."
3813   (when (and references
3814              (string-match "\\(<[^\n<>]+>\\)[ \t\n]*\\'" references))
3815     (substring references (match-beginning 1) (match-end 1))))
3816
3817 (defun gnus-split-references (references)
3818   "Return a list of Message-IDs in REFERENCES."
3819   (let ((beg 0)
3820         ids)
3821     (while (string-match "<[^>]+>" references beg)
3822       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3823             ids))
3824     (nreverse ids)))
3825
3826 (defun gnus-buffer-live-p (buffer)
3827   "Say whether BUFFER is alive or not."
3828   (and buffer
3829        (get-buffer buffer)
3830        (buffer-name (get-buffer buffer))))
3831
3832 (defun gnus-ephemeral-group-p (group)
3833   "Say whether GROUP is ephemeral or not."
3834   (gnus-group-get-parameter group 'quit-config))
3835
3836 (defun gnus-group-quit-config (group)
3837   "Return the quit-config of GROUP."
3838   (gnus-group-get-parameter group 'quit-config))
3839
3840 (defun gnus-simplify-mode-line ()
3841   "Make mode lines a bit simpler."
3842   (setq mode-line-modified "-- ")
3843   (when (listp mode-line-format)
3844     (make-local-variable 'mode-line-format)
3845     (setq mode-line-format (copy-sequence mode-line-format))
3846     (when (equal (nth 3 mode-line-format) "   ")
3847       (setcar (nthcdr 3 mode-line-format) " "))))
3848
3849 ;;; List and range functions
3850
3851 (defun gnus-last-element (list)
3852   "Return last element of LIST."
3853   (while (cdr list)
3854     (setq list (cdr list)))
3855   (car list))
3856
3857 (defun gnus-copy-sequence (list)
3858   "Do a complete, total copy of a list."
3859   (if (and (consp list) (not (consp (cdr list))))
3860       (cons (car list) (cdr list))
3861     (mapcar (lambda (elem) (if (consp elem)
3862                                (if (consp (cdr elem))
3863                                    (gnus-copy-sequence elem)
3864                                  (cons (car elem) (cdr elem)))
3865                              elem))
3866             list)))
3867
3868 (defun gnus-set-difference (list1 list2)
3869   "Return a list of elements of LIST1 that do not appear in LIST2."
3870   (let ((list1 (copy-sequence list1)))
3871     (while list2
3872       (setq list1 (delq (car list2) list1))
3873       (setq list2 (cdr list2)))
3874     list1))
3875
3876 (defun gnus-sorted-complement (list1 list2)
3877   "Return a list of elements of LIST1 that do not appear in LIST2.
3878 Both lists have to be sorted over <."
3879   (let (out)
3880     (if (or (null list1) (null list2))
3881         (or list1 list2)
3882       (while (and list1 list2)
3883         (cond ((= (car list1) (car list2))
3884                (setq list1 (cdr list1)
3885                      list2 (cdr list2)))
3886               ((< (car list1) (car list2))
3887                (setq out (cons (car list1) out))
3888                (setq list1 (cdr list1)))
3889               (t
3890                (setq out (cons (car list2) out))
3891                (setq list2 (cdr list2)))))
3892       (nconc (nreverse out) (or list1 list2)))))
3893
3894 (defun gnus-intersection (list1 list2)
3895   (let ((result nil))
3896     (while list2
3897       (if (memq (car list2) list1)
3898           (setq result (cons (car list2) result)))
3899       (setq list2 (cdr list2)))
3900     result))
3901
3902 (defun gnus-sorted-intersection (list1 list2)
3903   ;; LIST1 and LIST2 have to be sorted over <.
3904   (let (out)
3905     (while (and list1 list2)
3906       (cond ((= (car list1) (car list2))
3907              (setq out (cons (car list1) out)
3908                    list1 (cdr list1)
3909                    list2 (cdr list2)))
3910             ((< (car list1) (car list2))
3911              (setq list1 (cdr list1)))
3912             (t
3913              (setq list2 (cdr list2)))))
3914     (nreverse out)))
3915
3916 (defun gnus-set-sorted-intersection (list1 list2)
3917   ;; LIST1 and LIST2 have to be sorted over <.
3918   ;; This function modifies LIST1.
3919   (let* ((top (cons nil list1))
3920          (prev top))
3921     (while (and list1 list2)
3922       (cond ((= (car list1) (car list2))
3923              (setq prev list1
3924                    list1 (cdr list1)
3925                    list2 (cdr list2)))
3926             ((< (car list1) (car list2))
3927              (setcdr prev (cdr list1))
3928              (setq list1 (cdr list1)))
3929             (t
3930              (setq list2 (cdr list2)))))
3931     (setcdr prev nil)
3932     (cdr top)))
3933
3934 (defun gnus-compress-sequence (numbers &optional always-list)
3935   "Convert list of numbers to a list of ranges or a single range.
3936 If ALWAYS-LIST is non-nil, this function will always release a list of
3937 ranges."
3938   (let* ((first (car numbers))
3939          (last (car numbers))
3940          result)
3941     (if (null numbers)
3942         nil
3943       (if (not (listp (cdr numbers)))
3944           numbers
3945         (while numbers
3946           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3947                 ((= (1+ last) (car numbers)) ;Still in sequence
3948                  (setq last (car numbers)))
3949                 (t                      ;End of one sequence
3950                  (setq result
3951                        (cons (if (= first last) first
3952                                (cons first last)) result))
3953                  (setq first (car numbers))
3954                  (setq last  (car numbers))))
3955           (setq numbers (cdr numbers)))
3956         (if (and (not always-list) (null result))
3957             (if (= first last) (list first) (cons first last))
3958           (nreverse (cons (if (= first last) first (cons first last))
3959                           result)))))))
3960
3961 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3962 (defun gnus-uncompress-range (ranges)
3963   "Expand a list of ranges into a list of numbers.
3964 RANGES is either a single range on the form `(num . num)' or a list of
3965 these ranges."
3966   (let (first last result)
3967     (cond
3968      ((null ranges)
3969       nil)
3970      ((not (listp (cdr ranges)))
3971       (setq first (car ranges))
3972       (setq last (cdr ranges))
3973       (while (<= first last)
3974         (setq result (cons first result))
3975         (setq first (1+ first)))
3976       (nreverse result))
3977      (t
3978       (while ranges
3979         (if (atom (car ranges))
3980             (if (numberp (car ranges))
3981                 (setq result (cons (car ranges) result)))
3982           (setq first (caar ranges))
3983           (setq last  (cdar ranges))
3984           (while (<= first last)
3985             (setq result (cons first result))
3986             (setq first (1+ first))))
3987         (setq ranges (cdr ranges)))
3988       (nreverse result)))))
3989
3990 (defun gnus-add-to-range (ranges list)
3991   "Return a list of ranges that has all articles from both RANGES and LIST.
3992 Note: LIST has to be sorted over `<'."
3993   (if (not ranges)
3994       (gnus-compress-sequence list t)
3995     (setq list (copy-sequence list))
3996     (or (listp (cdr ranges))
3997         (setq ranges (list ranges)))
3998     (let ((out ranges)
3999           ilist lowest highest temp)
4000       (while (and ranges list)
4001         (setq ilist list)
4002         (setq lowest (or (and (atom (car ranges)) (car ranges))
4003                          (caar ranges)))
4004         (while (and list (cdr list) (< (cadr list) lowest))
4005           (setq list (cdr list)))
4006         (if (< (car ilist) lowest)
4007             (progn
4008               (setq temp list)
4009               (setq list (cdr list))
4010               (setcdr temp nil)
4011               (setq out (nconc (gnus-compress-sequence ilist t) out))))
4012         (setq highest (or (and (atom (car ranges)) (car ranges))
4013                           (cdar ranges)))
4014         (while (and list (<= (car list) highest))
4015           (setq list (cdr list)))
4016         (setq ranges (cdr ranges)))
4017       (if list
4018           (setq out (nconc (gnus-compress-sequence list t) out)))
4019       (setq out (sort out (lambda (r1 r2)
4020                             (< (or (and (atom r1) r1) (car r1))
4021                                (or (and (atom r2) r2) (car r2))))))
4022       (setq ranges out)
4023       (while ranges
4024         (if (atom (car ranges))
4025             (if (cdr ranges)
4026                 (if (atom (cadr ranges))
4027                     (if (= (1+ (car ranges)) (cadr ranges))
4028                         (progn
4029                           (setcar ranges (cons (car ranges)
4030                                                (cadr ranges)))
4031                           (setcdr ranges (cddr ranges))))
4032                   (if (= (1+ (car ranges)) (caadr ranges))
4033                       (progn
4034                         (setcar (cadr ranges) (car ranges))
4035                         (setcar ranges (cadr ranges))
4036                         (setcdr ranges (cddr ranges))))))
4037           (if (cdr ranges)
4038               (if (atom (cadr ranges))
4039                   (if (= (1+ (cdar ranges)) (cadr ranges))
4040                       (progn
4041                         (setcdr (car ranges) (cadr ranges))
4042                         (setcdr ranges (cddr ranges))))
4043                 (if (= (1+ (cdar ranges)) (caadr ranges))
4044                     (progn
4045                       (setcdr (car ranges) (cdadr ranges))
4046                       (setcdr ranges (cddr ranges)))))))
4047         (setq ranges (cdr ranges)))
4048       out)))
4049
4050 (defun gnus-remove-from-range (ranges list)
4051   "Return a list of ranges that has all articles from LIST removed from RANGES.
4052 Note: LIST has to be sorted over `<'."
4053   ;; !!! This function shouldn't look like this, but I've got a headache.
4054   (gnus-compress-sequence
4055    (gnus-sorted-complement
4056     (gnus-uncompress-range ranges) list)))
4057
4058 (defun gnus-member-of-range (number ranges)
4059   (if (not (listp (cdr ranges)))
4060       (and (>= number (car ranges))
4061            (<= number (cdr ranges)))
4062     (let ((not-stop t))
4063       (while (and ranges
4064                   (if (numberp (car ranges))
4065                       (>= number (car ranges))
4066                     (>= number (caar ranges)))
4067                   not-stop)
4068         (if (if (numberp (car ranges))
4069                 (= number (car ranges))
4070               (and (>= number (caar ranges))
4071                    (<= number (cdar ranges))))
4072             (setq not-stop nil))
4073         (setq ranges (cdr ranges)))
4074       (not not-stop))))
4075
4076 (defun gnus-range-length (range)
4077   "Return the length RANGE would have if uncompressed."
4078   (length (gnus-uncompress-range range)))
4079
4080 (defun gnus-sublist-p (list sublist)
4081   "Test whether all elements in SUBLIST are members of LIST."
4082   (let ((sublistp t))
4083     (while sublist
4084       (unless (memq (pop sublist) list)
4085         (setq sublistp nil
4086               sublist nil)))
4087     sublistp))
4088
4089 \f
4090 ;;;
4091 ;;; Gnus group mode
4092 ;;;
4093
4094 (defvar gnus-group-mode-map nil)
4095 (put 'gnus-group-mode 'mode-class 'special)
4096
4097 (unless gnus-group-mode-map
4098   (setq gnus-group-mode-map (make-keymap))
4099   (suppress-keymap gnus-group-mode-map)
4100
4101   (gnus-define-keys gnus-group-mode-map
4102     " " gnus-group-read-group
4103     "=" gnus-group-select-group
4104     "\r" gnus-group-select-group
4105     "\M-\r" gnus-group-quick-select-group
4106     "j" gnus-group-jump-to-group
4107     "n" gnus-group-next-unread-group
4108     "p" gnus-group-prev-unread-group
4109     "\177" gnus-group-prev-unread-group
4110     [delete] gnus-group-prev-unread-group
4111     "N" gnus-group-next-group
4112     "P" gnus-group-prev-group
4113     "\M-n" gnus-group-next-unread-group-same-level
4114     "\M-p" gnus-group-prev-unread-group-same-level
4115     "," gnus-group-best-unread-group
4116     "." gnus-group-first-unread-group
4117     "u" gnus-group-unsubscribe-current-group
4118     "U" gnus-group-unsubscribe-group
4119     "c" gnus-group-catchup-current
4120     "C" gnus-group-catchup-current-all
4121     "l" gnus-group-list-groups
4122     "L" gnus-group-list-all-groups
4123     "m" gnus-group-mail
4124     "g" gnus-group-get-new-news
4125     "\M-g" gnus-group-get-new-news-this-group
4126     "R" gnus-group-restart
4127     "r" gnus-group-read-init-file
4128     "B" gnus-group-browse-foreign-server
4129     "b" gnus-group-check-bogus-groups
4130     "F" gnus-find-new-newsgroups
4131     "\C-c\C-d" gnus-group-describe-group
4132     "\M-d" gnus-group-describe-all-groups
4133     "\C-c\C-a" gnus-group-apropos
4134     "\C-c\M-\C-a" gnus-group-description-apropos
4135     "a" gnus-group-post-news
4136     "\ek" gnus-group-edit-local-kill
4137     "\eK" gnus-group-edit-global-kill
4138     "\C-k" gnus-group-kill-group
4139     "\C-y" gnus-group-yank-group
4140     "\C-w" gnus-group-kill-region
4141     "\C-x\C-t" gnus-group-transpose-groups
4142     "\C-c\C-l" gnus-group-list-killed
4143     "\C-c\C-x" gnus-group-expire-articles
4144     "\C-c\M-\C-x" gnus-group-expire-all-groups
4145     "V" gnus-version
4146     "s" gnus-group-save-newsrc
4147     "z" gnus-group-suspend
4148     "Z" gnus-group-clear-dribble
4149     "q" gnus-group-exit
4150     "Q" gnus-group-quit
4151     "?" gnus-group-describe-briefly
4152     "\C-c\C-i" gnus-info-find-node
4153     "\M-e" gnus-group-edit-group-method
4154     "^" gnus-group-enter-server-mode
4155     gnus-mouse-2 gnus-mouse-pick-group
4156     "<" beginning-of-buffer
4157     ">" end-of-buffer
4158     "\C-c\C-b" gnus-bug
4159     "\C-c\C-s" gnus-group-sort-groups
4160     "t" gnus-topic-mode
4161     "\C-c\M-g" gnus-activate-all-groups
4162     "\M-&" gnus-group-universal-argument
4163     "#" gnus-group-mark-group
4164     "\M-#" gnus-group-unmark-group)
4165
4166   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4167     "m" gnus-group-mark-group
4168     "u" gnus-group-unmark-group
4169     "w" gnus-group-mark-region
4170     "m" gnus-group-mark-buffer
4171     "r" gnus-group-mark-regexp
4172     "U" gnus-group-unmark-all-groups)
4173
4174   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4175     "d" gnus-group-make-directory-group
4176     "h" gnus-group-make-help-group
4177     "a" gnus-group-make-archive-group
4178     "k" gnus-group-make-kiboze-group
4179     "m" gnus-group-make-group
4180     "E" gnus-group-edit-group
4181     "e" gnus-group-edit-group-method
4182     "p" gnus-group-edit-group-parameters
4183     "v" gnus-group-add-to-virtual
4184     "V" gnus-group-make-empty-virtual
4185     "D" gnus-group-enter-directory
4186     "f" gnus-group-make-doc-group
4187     "r" gnus-group-rename-group
4188     "\177" gnus-group-delete-group
4189     [delete] gnus-group-delete-group)
4190
4191    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4192      "b" gnus-group-brew-soup
4193      "w" gnus-soup-save-areas
4194      "s" gnus-soup-send-replies
4195      "p" gnus-soup-pack-packet
4196      "r" nnsoup-pack-replies)
4197
4198    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4199      "s" gnus-group-sort-groups
4200      "a" gnus-group-sort-groups-by-alphabet
4201      "u" gnus-group-sort-groups-by-unread
4202      "l" gnus-group-sort-groups-by-level
4203      "v" gnus-group-sort-groups-by-score
4204      "r" gnus-group-sort-groups-by-rank
4205      "m" gnus-group-sort-groups-by-method)
4206
4207    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4208      "k" gnus-group-list-killed
4209      "z" gnus-group-list-zombies
4210      "s" gnus-group-list-groups
4211      "u" gnus-group-list-all-groups
4212      "A" gnus-group-list-active
4213      "a" gnus-group-apropos
4214      "d" gnus-group-description-apropos
4215      "m" gnus-group-list-matching
4216      "M" gnus-group-list-all-matching
4217      "l" gnus-group-list-level)
4218
4219    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4220      "f" gnus-score-flush-cache)
4221
4222    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4223      "f" gnus-group-fetch-faq)
4224
4225    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4226      "l" gnus-group-set-current-level
4227      "t" gnus-group-unsubscribe-current-group
4228      "s" gnus-group-unsubscribe-group
4229      "k" gnus-group-kill-group
4230      "y" gnus-group-yank-group
4231      "w" gnus-group-kill-region
4232      "\C-k" gnus-group-kill-level
4233      "z" gnus-group-kill-all-zombies))
4234
4235 (defun gnus-group-mode ()
4236   "Major mode for reading news.
4237
4238 All normal editing commands are switched off.
4239 \\<gnus-group-mode-map>
4240 The group buffer lists (some of) the groups available.  For instance,
4241 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4242 lists all zombie groups.
4243
4244 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4245 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4246
4247 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4248
4249 The following commands are available:
4250
4251 \\{gnus-group-mode-map}"
4252   (interactive)
4253   (when (and menu-bar-mode
4254              (gnus-visual-p 'group-menu 'menu))
4255     (gnus-group-make-menu-bar))
4256   (kill-all-local-variables)
4257   (gnus-simplify-mode-line)
4258   (setq major-mode 'gnus-group-mode)
4259   (setq mode-name "Group")
4260   (gnus-group-set-mode-line)
4261   (setq mode-line-process nil)
4262   (use-local-map gnus-group-mode-map)
4263   (buffer-disable-undo (current-buffer))
4264   (setq truncate-lines t)
4265   (setq buffer-read-only t)
4266   (run-hooks 'gnus-group-mode-hook))
4267
4268 (defun gnus-mouse-pick-group (e)
4269   "Enter the group under the mouse pointer."
4270   (interactive "e")
4271   (mouse-set-point e)
4272   (gnus-group-read-group nil))
4273
4274 ;; Look at LEVEL and find out what the level is really supposed to be.
4275 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4276 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4277 (defun gnus-group-default-level (&optional level number-or-nil)
4278   (cond
4279    (gnus-group-use-permanent-levels
4280     (or (setq gnus-group-use-permanent-levels
4281               (or level (if (numberp gnus-group-use-permanent-levels)
4282                             gnus-group-use-permanent-levels
4283                           (or gnus-group-default-list-level
4284                               gnus-level-subscribed))))
4285         gnus-group-default-list-level gnus-level-subscribed))
4286    (number-or-nil
4287     level)
4288    (t
4289     (or level gnus-group-default-list-level gnus-level-subscribed))))
4290
4291 ;;;###autoload
4292 (defun gnus-slave-no-server (&optional arg)
4293   "Read network news as a slave, without connecting to local server"
4294   (interactive "P")
4295   (gnus-no-server arg t))
4296
4297 ;;;###autoload
4298 (defun gnus-no-server (&optional arg slave)
4299   "Read network news.
4300 If ARG is a positive number, Gnus will use that as the
4301 startup level.  If ARG is nil, Gnus will be started at level 2.
4302 If ARG is non-nil and not a positive number, Gnus will
4303 prompt the user for the name of an NNTP server to use.
4304 As opposed to `gnus', this command will not connect to the local server."
4305   (interactive "P")
4306   (let ((val (or arg (1- gnus-level-default-subscribed))))
4307     (gnus val t slave)
4308     (make-local-variable 'gnus-group-use-permanent-levels)
4309     (setq gnus-group-use-permanent-levels val)))
4310
4311 ;;;###autoload
4312 (defun gnus-slave (&optional arg)
4313   "Read news as a slave."
4314   (interactive "P")
4315   (gnus arg nil 'slave))
4316
4317 ;;;###autoload
4318 (defun gnus-other-frame (&optional arg)
4319   "Pop up a frame to read news."
4320   (interactive "P")
4321   (if (get-buffer gnus-group-buffer)
4322       (let ((pop-up-frames t))
4323         (gnus arg))
4324     (select-frame (make-frame))
4325     (gnus arg)))
4326
4327 ;;;###autoload
4328 (defun gnus (&optional arg dont-connect slave)
4329   "Read network news.
4330 If ARG is non-nil and a positive number, Gnus will use that as the
4331 startup level.  If ARG is non-nil and not a positive number, Gnus will
4332 prompt the user for the name of an NNTP server to use."
4333   (interactive "P")
4334
4335   (if (get-buffer gnus-group-buffer)
4336       (progn
4337         (switch-to-buffer gnus-group-buffer)
4338         (gnus-group-get-new-news))
4339
4340     (gnus-clear-system)
4341     (nnheader-init-server-buffer)
4342     (gnus-read-init-file)
4343     (setq gnus-slave slave)
4344
4345     (gnus-group-setup-buffer)
4346     (let ((buffer-read-only nil))
4347       (erase-buffer)
4348       (if (not gnus-inhibit-startup-message)
4349           (progn
4350             (gnus-group-startup-message)
4351             (sit-for 0))))
4352
4353     (let ((level (and (numberp arg) (> arg 0) arg))
4354           did-connect)
4355       (unwind-protect
4356           (progn
4357             (or dont-connect
4358                 (setq did-connect
4359                       (gnus-start-news-server (and arg (not level))))))
4360         (if (and (not dont-connect)
4361                  (not did-connect))
4362             (gnus-group-quit)
4363           (run-hooks 'gnus-startup-hook)
4364           ;; NNTP server is successfully open.
4365
4366           ;; Find the current startup file name.
4367           (setq gnus-current-startup-file
4368                 (gnus-make-newsrc-file gnus-startup-file))
4369
4370           ;; Read the dribble file.
4371           (when (or gnus-slave gnus-use-dribble-file)
4372             (gnus-dribble-read-file))
4373
4374           ;; Allow using GroupLens predictions.
4375           (when gnus-use-grouplens
4376             (bbb-login)
4377             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4378
4379           (gnus-summary-make-display-table)
4380           ;; Do the actual startup.
4381           (gnus-setup-news nil level dont-connect)
4382           ;; Generate the group buffer.
4383           (gnus-group-list-groups level)
4384           (gnus-group-first-unread-group)
4385           (gnus-configure-windows 'group)
4386           (gnus-group-set-mode-line))))))
4387
4388 (defun gnus-unload ()
4389   "Unload all Gnus features."
4390   (interactive)
4391   (or (boundp 'load-history)
4392       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4393   (let ((history load-history)
4394         feature)
4395     (while history
4396       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4397            (setq feature (cdr (assq 'provide (car history))))
4398            (unload-feature feature 'force))
4399       (setq history (cdr history)))))
4400
4401 (defun gnus-compile ()
4402   "Byte-compile the user-defined format specs."
4403   (interactive)
4404   (let ((entries gnus-format-specs)
4405         entry gnus-tmp-func)
4406     (save-excursion
4407       (gnus-message 7 "Compiling format specs...")
4408
4409       (while entries
4410         (setq entry (pop entries))
4411         (if (eq (car entry) 'version)
4412             (setq gnus-format-specs (delq entry gnus-format-specs))
4413           (when (and (listp (caddr entry))
4414                      (not (eq 'byte-code (caaddr entry))))
4415             (fset 'gnus-tmp-func
4416                   `(lambda () ,(caddr entry)))
4417             (byte-compile 'gnus-tmp-func)
4418             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4419
4420       (push (cons 'version emacs-version) gnus-format-specs)
4421
4422       (gnus-message 7 "Compiling user specs...done"))))
4423
4424 (defun gnus-indent-rigidly (start end arg)
4425   "Indent rigidly using only spaces and no tabs."
4426   (save-excursion
4427     (save-restriction
4428       (narrow-to-region start end)
4429       (indent-rigidly start end arg)
4430       (goto-char (point-min))
4431       (while (search-forward "\t" nil t)
4432         (replace-match "        " t t)))))
4433
4434 (defun gnus-group-startup-message (&optional x y)
4435   "Insert startup message in current buffer."
4436   ;; Insert the message.
4437   (erase-buffer)
4438   (insert
4439    (format "              %s
4440           _    ___ _             _
4441           _ ___ __ ___  __    _ ___
4442           __   _     ___    __  ___
4443               _           ___     _
4444              _  _ __             _
4445              ___   __            _
4446                    __           _
4447                     _      _   _
4448                    _      _    _
4449                       _  _    _
4450                   __  ___
4451                  _   _ _     _
4452                 _   _
4453               _    _
4454              _    _
4455             _
4456           __
4457
4458 "
4459            ""))
4460   ;; And then hack it.
4461   (gnus-indent-rigidly (point-min) (point-max)
4462                        (/ (max (- (window-width) (or x 46)) 0) 2))
4463   (goto-char (point-min))
4464   (forward-line 1)
4465   (let* ((pheight (count-lines (point-min) (point-max)))
4466          (wheight (window-height))
4467          (rest (- wheight pheight)))
4468     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4469   ;; Fontify some.
4470   (goto-char (point-min))
4471   (and (search-forward "Praxis" nil t)
4472        (gnus-put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4473   (goto-char (point-min))
4474   (let* ((mode-string (gnus-group-set-mode-line)))
4475     (setq mode-line-buffer-identification
4476           (list (concat gnus-version (substring (car mode-string) 4))))
4477     (set-buffer-modified-p t)))
4478
4479 (defun gnus-group-setup-buffer ()
4480   (or (get-buffer gnus-group-buffer)
4481       (progn
4482         (switch-to-buffer gnus-group-buffer)
4483         (gnus-add-current-to-buffer-list)
4484         (gnus-group-mode)
4485         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4486
4487 (defun gnus-group-list-groups (&optional level unread lowest)
4488   "List newsgroups with level LEVEL or lower that have unread articles.
4489 Default is all subscribed groups.
4490 If argument UNREAD is non-nil, groups with no unread articles are also
4491 listed."
4492   (interactive (list (if current-prefix-arg
4493                          (prefix-numeric-value current-prefix-arg)
4494                        (or
4495                         (gnus-group-default-level nil t)
4496                         gnus-group-default-list-level
4497                         gnus-level-subscribed))))
4498   (or level
4499       (setq level (car gnus-group-list-mode)
4500             unread (cdr gnus-group-list-mode)))
4501   (setq level (gnus-group-default-level level))
4502   (gnus-group-setup-buffer)             ;May call from out of group buffer
4503   (gnus-update-format-specifications)
4504   (let ((case-fold-search nil)
4505         (props (text-properties-at (gnus-point-at-bol)))
4506         (group (gnus-group-group-name)))
4507     (set-buffer gnus-group-buffer)
4508     (funcall gnus-group-prepare-function level unread lowest)
4509     (if (zerop (buffer-size))
4510         (gnus-message 5 gnus-no-groups-message)
4511       (goto-char (point-max))
4512       (when (or (not gnus-group-goto-next-group-function)
4513                 (not (funcall gnus-group-goto-next-group-function 
4514                               group props)))
4515         (if (not group)
4516             ;; Go to the first group with unread articles.
4517             (gnus-group-search-forward t)
4518           ;; Find the right group to put point on.  If the current group
4519           ;; has disappeared in the new listing, try to find the next
4520           ;; one.        If no next one can be found, just leave point at the
4521           ;; first newsgroup in the buffer.
4522           (if (not (gnus-goto-char
4523                     (text-property-any
4524                      (point-min) (point-max)
4525                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4526               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4527                 (while (and newsrc
4528                             (not (gnus-goto-char
4529                                   (text-property-any
4530                                    (point-min) (point-max) 'gnus-group
4531                                    (gnus-intern-safe
4532                                     (caar newsrc) gnus-active-hashtb)))))
4533                   (setq newsrc (cdr newsrc)))
4534                 (or newsrc (progn (goto-char (point-max))
4535                                   (forward-line -1)))))))
4536       ;; Adjust cursor point.
4537       (gnus-group-position-point))))
4538
4539 (defun gnus-group-list-level (level &optional all)
4540   "List groups on LEVEL.
4541 If ALL (the prefix), also list groups that have no unread articles."
4542   (interactive "nList groups on level: \nP")
4543   (gnus-group-list-groups level all level))
4544
4545 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4546   "List all newsgroups with unread articles of level LEVEL or lower.
4547 If ALL is non-nil, list groups that have no unread articles.
4548 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4549 If REGEXP, only list groups matching REGEXP."
4550   (set-buffer gnus-group-buffer)
4551   (let ((buffer-read-only nil)
4552         (newsrc (cdr gnus-newsrc-alist))
4553         (lowest (or lowest 1))
4554         info clevel unread group params)
4555     (erase-buffer)
4556     (if (< lowest gnus-level-zombie)
4557         ;; List living groups.
4558         (while newsrc
4559           (setq info (car newsrc)
4560                 group (gnus-info-group info)
4561                 params (gnus-info-params info)
4562                 newsrc (cdr newsrc)
4563                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4564           (and unread                   ; This group might be bogus
4565                (or (not regexp)
4566                    (string-match regexp group))
4567                (<= (setq clevel (gnus-info-level info)) level)
4568                (>= clevel lowest)
4569                (or all                  ; We list all groups?
4570                    (if (eq unread t)    ; Unactivated?
4571                        gnus-group-list-inactive-groups ; We list unactivated 
4572                      (> unread 0))      ; We list groups with unread articles
4573                    (and gnus-list-groups-with-ticked-articles
4574                         (cdr (assq 'tick (gnus-info-marks info))))
4575                                         ; And groups with tickeds
4576                    ;; Check for permanent visibility.
4577                    (and gnus-permanently-visible-groups
4578                         (string-match gnus-permanently-visible-groups
4579                                       group))
4580                    (memq 'visible params)
4581                    (cdr (assq 'visible params)))
4582                (gnus-group-insert-group-line
4583                 group (gnus-info-level info)
4584                 (gnus-info-marks info) unread (gnus-info-method info)))))
4585
4586     ;; List dead groups.
4587     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4588          (gnus-group-prepare-flat-list-dead
4589           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4590           gnus-level-zombie ?Z
4591           regexp))
4592     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4593          (gnus-group-prepare-flat-list-dead
4594           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4595           gnus-level-killed ?K regexp))
4596
4597     (gnus-group-set-mode-line)
4598     (setq gnus-group-list-mode (cons level all))
4599     (run-hooks 'gnus-group-prepare-hook)))
4600
4601 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4602   ;; List zombies and killed lists somewhat faster, which was
4603   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4604   ;; this by ignoring the group format specification altogether.
4605   (let (group)
4606     (if regexp
4607         ;; This loop is used when listing groups that match some
4608         ;; regexp.
4609         (while groups
4610           (setq group (pop groups))
4611           (when (string-match regexp group)
4612             (gnus-add-text-properties
4613              (point) (prog1 (1+ (point))
4614                        (insert " " mark "     *: " group "\n"))
4615              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4616                    'gnus-unread t
4617                    'gnus-level level))))
4618       ;; This loop is used when listing all groups.
4619       (while groups
4620         (gnus-add-text-properties
4621          (point) (prog1 (1+ (point))
4622                    (insert " " mark "     *: "
4623                            (setq group (pop groups)) "\n"))
4624          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4625                'gnus-unread t
4626                'gnus-level level))))))
4627
4628 (defmacro gnus-group-real-name (group)
4629   "Find the real name of a foreign newsgroup."
4630   `(let ((gname ,group))
4631      (if (string-match ":[^:]+$" gname)
4632          (substring gname (1+ (match-beginning 0)))
4633        gname)))
4634
4635 (defsubst gnus-server-add-address (method)
4636   (let ((method-name (symbol-name (car method))))
4637     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4638              (not (assq (intern (concat method-name "-address")) method)))
4639         (append method (list (list (intern (concat method-name "-address"))
4640                                    (nth 1 method))))
4641       method)))
4642
4643 (defsubst gnus-server-get-method (group method)
4644   ;; Input either a server name, and extended server name, or a
4645   ;; select method, and return a select method.
4646   (cond ((stringp method)
4647          (gnus-server-to-method method))
4648         ((equal method gnus-select-method)
4649          gnus-select-method)
4650         ((and (stringp (car method)) group)
4651          (gnus-server-extend-method group method))
4652         ((and method (not group)
4653               (equal (cadr method) ""))
4654          method)
4655         (t
4656          (gnus-server-add-address method))))
4657
4658 (defun gnus-server-to-method (server)
4659   "Map virtual server names to select methods."
4660   (or 
4661    ;; Is this a method, perhaps?
4662    (and server (listp server) server)
4663    ;; Perhaps this is the native server?
4664    (and (equal server "native") gnus-select-method)
4665    ;; It should be in the server alist.
4666    (cdr (assoc server gnus-server-alist))
4667    ;; If not, we look through all the opened server
4668    ;; to see whether we can find it there.
4669    (let ((opened gnus-opened-servers))
4670      (while (and opened
4671                  (not (equal server (format "%s:%s" (caaar opened)
4672                                             (cadaar opened)))))
4673        (pop opened))
4674      (caar opened))))
4675
4676 (defmacro gnus-method-equal (ss1 ss2)
4677   "Say whether two servers are equal."
4678   `(let ((s1 ,ss1)
4679          (s2 ,ss2))
4680      (or (equal s1 s2)
4681          (and (= (length s1) (length s2))
4682               (progn
4683                 (while (and s1 (member (car s1) s2))
4684                   (setq s1 (cdr s1)))
4685                 (null s1))))))
4686
4687 (defun gnus-server-equal (m1 m2)
4688   "Say whether two methods are equal."
4689   (let ((m1 (cond ((null m1) gnus-select-method)
4690                   ((stringp m1) (gnus-server-to-method m1))
4691                   (t m1)))
4692         (m2 (cond ((null m2) gnus-select-method)
4693                   ((stringp m2) (gnus-server-to-method m2))
4694                   (t m2))))
4695     (gnus-method-equal m1 m2)))
4696
4697 (defun gnus-servers-using-backend (backend)
4698   "Return a list of known servers using BACKEND."
4699   (let ((opened gnus-opened-servers)
4700         out)
4701     (while opened
4702       (when (eq backend (caaar opened))
4703         (push (caar opened) out))
4704       (pop opened))
4705     out))
4706
4707 (defun gnus-group-prefixed-name (group method)
4708   "Return the whole name from GROUP and METHOD."
4709   (and (stringp method) (setq method (gnus-server-to-method method)))
4710   (concat (format "%s" (car method))
4711           (if (and
4712                (or (assoc (format "%s" (car method)) 
4713                           (gnus-methods-using 'address))
4714                    (gnus-server-equal method gnus-message-archive-method))
4715                (nth 1 method)
4716                (not (string= (nth 1 method) "")))
4717               (concat "+" (nth 1 method)))
4718           ":" group))
4719
4720 (defun gnus-group-real-prefix (group)
4721   "Return the prefix of the current group name."
4722   (if (string-match "^[^:]+:" group)
4723       (substring group 0 (match-end 0))
4724     ""))
4725
4726 (defun gnus-group-method (group)
4727   "Return the server or method used for selecting GROUP."
4728   (let ((prefix (gnus-group-real-prefix group)))
4729     (if (equal prefix "")
4730         gnus-select-method
4731       (let ((servers gnus-opened-servers)
4732             (server "")
4733             backend possible found)
4734         (if (string-match "^[^\\+]+\\+" prefix)
4735             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4736                   server (substring prefix (match-end 0) (1- (length prefix))))
4737           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4738         (while servers
4739           (when (eq (caaar servers) backend)
4740             (setq possible (caar servers))
4741             (when (equal (cadaar servers) server)
4742               (setq found (caar servers))))
4743           (pop servers))
4744         (or (car (rassoc found gnus-server-alist))
4745             found
4746             (car (rassoc possible gnus-server-alist))
4747             possible
4748             (list backend server))))))
4749
4750 (defsubst gnus-secondary-method-p (method)
4751   "Return whether METHOD is a secondary select method."
4752   (let ((methods gnus-secondary-select-methods)
4753         (gmethod (gnus-server-get-method nil method)))
4754     (while (and methods
4755                 (not (equal (gnus-server-get-method nil (car methods))
4756                             gmethod)))
4757       (setq methods (cdr methods)))
4758     methods))
4759
4760 (defun gnus-group-foreign-p (group)
4761   "Say whether a group is foreign or not."
4762   (and (not (gnus-group-native-p group))
4763        (not (gnus-group-secondary-p group))))
4764
4765 (defun gnus-group-native-p (group)
4766   "Say whether the group is native or not."
4767   (not (string-match ":" group)))
4768
4769 (defun gnus-group-secondary-p (group)
4770   "Say whether the group is secondary or not."
4771   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4772
4773 (defun gnus-group-get-parameter (group &optional symbol)
4774   "Returns the group parameters for GROUP.
4775 If SYMBOL, return the value of that symbol in the group parameters."
4776   (let ((params (gnus-info-params (gnus-get-info group))))
4777     (if symbol
4778         (gnus-group-parameter-value params symbol)
4779       params)))
4780
4781 (defun gnus-group-parameter-value (params symbol)
4782   "Return the value of SYMBOL in group PARAMS."
4783   (or (car (memq symbol params))        ; It's either a simple symbol
4784       (cdr (assq symbol params))))      ; or a cons.
4785
4786 (defun gnus-group-add-parameter (group param)
4787   "Add parameter PARAM to GROUP."
4788   (let ((info (gnus-get-info group)))
4789     (if (not info)
4790         () ; This is a dead group.  We just ignore it.
4791       ;; Cons the new param to the old one and update.
4792       (gnus-group-set-info (cons param (gnus-info-params info))
4793                            group 'params))))
4794
4795 (defun gnus-group-set-parameter (group name value)
4796   "Set parameter NAME to VALUE in GROUP."
4797   (let ((info (gnus-get-info group)))
4798     (if (not info)
4799         () ; This is a dead group.  We just ignore it.
4800       (let ((old-params (gnus-info-params info))
4801             (new-params (list (cons name value))))
4802         (while old-params
4803           (if (or (not (listp (car old-params)))
4804                   (not (eq (caar old-params) name)))
4805               (setq new-params (append new-params (list (car old-params)))))
4806           (setq old-params (cdr old-params)))
4807         (gnus-group-set-info new-params group 'params)))))
4808
4809 (defun gnus-group-add-score (group &optional score)
4810   "Add SCORE to the GROUP score.
4811 If SCORE is nil, add 1 to the score of GROUP."
4812   (let ((info (gnus-get-info group)))
4813     (when info
4814       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4815
4816 (defun gnus-summary-bubble-group ()
4817   "Increase the score of the current group.
4818 This is a handy function to add to `gnus-summary-exit-hook' to
4819 increase the score of each group you read."
4820   (gnus-group-add-score gnus-newsgroup-name))
4821
4822 (defun gnus-group-set-info (info &optional method-only-group part)
4823   (let* ((entry (gnus-gethash
4824                  (or method-only-group (gnus-info-group info))
4825                  gnus-newsrc-hashtb))
4826          (part-info info)
4827          (info (if method-only-group (nth 2 entry) info))
4828          method)
4829     (when method-only-group
4830       (unless entry
4831         (error "Trying to change non-existent group %s" method-only-group))
4832       ;; We have received parts of the actual group info - either the
4833       ;; select method or the group parameters.  We first check
4834       ;; whether we have to extend the info, and if so, do that.
4835       (let ((len (length info))
4836             (total (if (eq part 'method) 5 6)))
4837         (when (< len total)
4838           (setcdr (nthcdr (1- len) info)
4839                   (make-list (- total len) nil)))
4840         ;; Then we enter the new info.
4841         (setcar (nthcdr (1- total) info) part-info)))
4842     (unless entry
4843       ;; This is a new group, so we just create it.
4844       (save-excursion
4845         (set-buffer gnus-group-buffer)
4846         (setq method (gnus-info-method info))
4847         (when (gnus-server-equal method "native")
4848           (setq method nil))
4849         (save-excursion
4850           (set-buffer gnus-group-buffer)
4851           (if method
4852               ;; It's a foreign group...
4853               (gnus-group-make-group
4854                (gnus-group-real-name (gnus-info-group info))
4855                (if (stringp method) method
4856                  (prin1-to-string (car method)))
4857                (and (consp method)
4858                     (nth 1 (gnus-info-method info))))
4859             ;; It's a native group.
4860             (gnus-group-make-group (gnus-info-group info))))
4861         (gnus-message 6 "Note: New group created")
4862         (setq entry
4863               (gnus-gethash (gnus-group-prefixed-name
4864                              (gnus-group-real-name (gnus-info-group info))
4865                              (or (gnus-info-method info) gnus-select-method))
4866                             gnus-newsrc-hashtb))))
4867     ;; Whether it was a new group or not, we now have the entry, so we
4868     ;; can do the update.
4869     (if entry
4870         (progn
4871           (setcar (nthcdr 2 entry) info)
4872           (when (and (not (eq (car entry) t))
4873                      (gnus-active (gnus-info-group info)))
4874             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4875       (error "No such group: %s" (gnus-info-group info)))))
4876
4877 (defun gnus-group-set-method-info (group select-method)
4878   (gnus-group-set-info select-method group 'method))
4879
4880 (defun gnus-group-set-params-info (group params)
4881   (gnus-group-set-info params group 'params))
4882
4883 (defun gnus-group-update-group-line ()
4884   "Update the current line in the group buffer."
4885   (let* ((buffer-read-only nil)
4886          (group (gnus-group-group-name))
4887          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
4888          gnus-group-indentation)
4889     (and entry
4890          (not (gnus-ephemeral-group-p group))
4891          (gnus-dribble-enter
4892           (concat "(gnus-group-set-info '"
4893                   (prin1-to-string (nth 2 entry)) ")")))
4894     (setq gnus-group-indentation (gnus-group-group-indentation))
4895     (gnus-delete-line)
4896     (gnus-group-insert-group-line-info group)
4897     (forward-line -1)
4898     (gnus-group-position-point)))
4899
4900 (defun gnus-group-insert-group-line-info (group)
4901   "Insert GROUP on the current line."
4902   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4903         active info)
4904     (if entry
4905         (progn
4906           ;; (Un)subscribed group.
4907           (setq info (nth 2 entry))
4908           (gnus-group-insert-group-line
4909            group (gnus-info-level info) (gnus-info-marks info)
4910            (or (car entry) t) (gnus-info-method info)))
4911       ;; This group is dead.
4912       (gnus-group-insert-group-line
4913        group
4914        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4915        nil
4916        (if (setq active (gnus-active group))
4917            (- (1+ (cdr active)) (car active)) 0)
4918        nil))))
4919
4920 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4921                                                     gnus-tmp-marked number
4922                                                     gnus-tmp-method)
4923   "Insert a group line in the group buffer."
4924   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4925          (gnus-tmp-number-total
4926           (if gnus-tmp-active
4927               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4928             0))
4929          (gnus-tmp-number-of-unread
4930           (if (numberp number) (int-to-string (max 0 number))
4931             "*"))
4932          (gnus-tmp-number-of-read
4933           (if (numberp number)
4934               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4935             "*"))
4936          (gnus-tmp-subscribed
4937           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4938                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4939                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4940                 (t ?K)))
4941          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4942          (gnus-tmp-newsgroup-description
4943           (if gnus-description-hashtb
4944               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4945             ""))
4946          (gnus-tmp-moderated
4947           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4948          (gnus-tmp-moderated-string
4949           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4950          (gnus-tmp-method
4951           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4952          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4953          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4954          (gnus-tmp-news-method-string
4955           (if gnus-tmp-method
4956               (format "(%s:%s)" (car gnus-tmp-method)
4957                       (cadr gnus-tmp-method)) ""))
4958          (gnus-tmp-marked-mark
4959           (if (and (numberp number)
4960                    (zerop number)
4961                    (cdr (assq 'tick gnus-tmp-marked)))
4962               ?* ? ))
4963          (gnus-tmp-process-marked
4964           (if (member gnus-tmp-group gnus-group-marked)
4965               gnus-process-mark ? ))
4966          (gnus-tmp-grouplens
4967           (or (and gnus-use-grouplens
4968                    (bbb-grouplens-group-p gnus-tmp-group))
4969               ""))
4970          (buffer-read-only nil)
4971          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4972     (beginning-of-line)
4973     (gnus-add-text-properties
4974      (point)
4975      (prog1 (1+ (point))
4976        ;; Insert the text.
4977        (eval gnus-group-line-format-spec))
4978      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4979        gnus-unread ,(if (numberp number)
4980                         (string-to-int gnus-tmp-number-of-unread)
4981                       t)
4982        gnus-marked ,gnus-tmp-marked-mark
4983        gnus-indentation ,gnus-group-indentation
4984        gnus-level ,gnus-tmp-level))
4985     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4986       (forward-line -1)
4987       (run-hooks 'gnus-group-update-hook)
4988       (forward-line))
4989     ;; Allow XEmacs to remove front-sticky text properties.
4990     (gnus-group-remove-excess-properties)))
4991
4992 (defun gnus-group-update-group (group &optional visible-only)
4993   "Update all lines where GROUP appear.
4994 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4995 already."
4996   (save-excursion
4997     (set-buffer gnus-group-buffer)
4998     ;; The buffer may be narrowed.
4999     (save-restriction
5000       (widen)
5001       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
5002             (loc (point-min))
5003             found buffer-read-only)
5004         ;; Enter the current status into the dribble buffer.
5005         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
5006           (if (and entry (not (gnus-ephemeral-group-p group)))
5007               (gnus-dribble-enter
5008                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
5009                        ")"))))
5010         ;; Find all group instances.  If topics are in use, each group
5011         ;; may be listed in more than once.
5012         (while (setq loc (text-property-any
5013                           loc (point-max) 'gnus-group ident))
5014           (setq found t)
5015           (goto-char loc)
5016           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5017             (gnus-delete-line)
5018             (gnus-group-insert-group-line-info group))
5019           (setq loc (1+ loc)))
5020         (unless (or found visible-only)
5021           ;; No such line in the buffer, find out where it's supposed to
5022           ;; go, and insert it there (or at the end of the buffer).
5023           (if gnus-goto-missing-group-function
5024               (funcall gnus-goto-missing-group-function group)
5025             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
5026               (while (and entry (car entry)
5027                           (not
5028                            (gnus-goto-char
5029                             (text-property-any
5030                              (point-min) (point-max)
5031                              'gnus-group (gnus-intern-safe
5032                                           (caar entry) gnus-active-hashtb)))))
5033                 (setq entry (cdr entry)))
5034               (or entry (goto-char (point-max)))))
5035           ;; Finally insert the line.
5036           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5037             (gnus-group-insert-group-line-info group)))
5038         (gnus-group-set-mode-line)))))
5039
5040 (defun gnus-group-set-mode-line ()
5041   "Update the mode line in the group buffer."
5042   (when (memq 'group gnus-updated-mode-lines)
5043     ;; Yes, we want to keep this mode line updated.
5044     (save-excursion
5045       (set-buffer gnus-group-buffer)
5046       (let* ((gformat (or gnus-group-mode-line-format-spec
5047                           (setq gnus-group-mode-line-format-spec
5048                                 (gnus-parse-format
5049                                  gnus-group-mode-line-format
5050                                  gnus-group-mode-line-format-alist))))
5051              (gnus-tmp-news-server (cadr gnus-select-method))
5052              (gnus-tmp-news-method (car gnus-select-method))
5053              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
5054              (max-len 60)
5055              gnus-tmp-header            ;Dummy binding for user-defined formats
5056              ;; Get the resulting string.
5057              (mode-string (eval gformat)))
5058         ;; Say whether the dribble buffer has been modified.
5059         (setq mode-line-modified
5060               (if (and gnus-dribble-buffer
5061                        (buffer-name gnus-dribble-buffer)
5062                        (buffer-modified-p gnus-dribble-buffer)
5063                        (save-excursion
5064                          (set-buffer gnus-dribble-buffer)
5065                          (not (zerop (buffer-size)))))
5066                   "---*- " "----- "))
5067         ;; If the line is too long, we chop it off.
5068         (when (> (length mode-string) max-len)
5069           (setq mode-string (substring mode-string 0 (- max-len 4))))
5070         (prog1
5071             (setq mode-line-buffer-identification 
5072                   (list mode-string))
5073           (set-buffer-modified-p t))))))
5074
5075 (defun gnus-group-group-name ()
5076   "Get the name of the newsgroup on the current line."
5077   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5078     (and group (symbol-name group))))
5079
5080 (defun gnus-group-group-level ()
5081   "Get the level of the newsgroup on the current line."
5082   (get-text-property (gnus-point-at-bol) 'gnus-level))
5083
5084 (defun gnus-group-group-indentation ()
5085   "Get the indentation of the newsgroup on the current line."
5086   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5087       (and gnus-group-indentation-function
5088            (funcall gnus-group-indentation-function))
5089       ""))
5090
5091 (defun gnus-group-group-unread ()
5092   "Get the number of unread articles of the newsgroup on the current line."
5093   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5094
5095 (defun gnus-group-search-forward (&optional backward all level first-too)
5096   "Find the next newsgroup with unread articles.
5097 If BACKWARD is non-nil, find the previous newsgroup instead.
5098 If ALL is non-nil, just find any newsgroup.
5099 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5100 group exists.
5101 If FIRST-TOO, the current line is also eligible as a target."
5102   (let ((way (if backward -1 1))
5103         (low gnus-level-killed)
5104         (beg (point))
5105         pos found lev)
5106     (if (and backward (progn (beginning-of-line)) (bobp))
5107         nil
5108       (or first-too (forward-line way))
5109       (while (and
5110               (not (eobp))
5111               (not (setq
5112                     found
5113                     (and (or all
5114                              (and
5115                               (let ((unread
5116                                      (get-text-property (point) 'gnus-unread)))
5117                                 (and (numberp unread) (> unread 0)))
5118                               (setq lev (get-text-property (point)
5119                                                            'gnus-level))
5120                               (<= lev gnus-level-subscribed)))
5121                          (or (not level)
5122                              (and (setq lev (get-text-property (point)
5123                                                                'gnus-level))
5124                                   (or (= lev level)
5125                                       (and (< lev low)
5126                                            (< level lev)
5127                                            (progn
5128                                              (setq low lev)
5129                                              (setq pos (point))
5130                                              nil))))))))
5131               (zerop (forward-line way)))))
5132     (if found
5133         (progn (gnus-group-position-point) t)
5134       (goto-char (or pos beg))
5135       (and pos t))))
5136
5137 ;;; Gnus group mode commands
5138
5139 ;; Group marking.
5140
5141 (defun gnus-group-mark-group (n &optional unmark no-advance)
5142   "Mark the current group."
5143   (interactive "p")
5144   (let ((buffer-read-only nil)
5145         group)
5146     (while (and (> n 0)
5147                 (not (eobp)))
5148       (when (setq group (gnus-group-group-name))
5149         ;; Update the mark.
5150         (beginning-of-line)
5151         (forward-char
5152          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5153         (delete-char 1)
5154         (if unmark
5155             (progn
5156               (insert " ")
5157               (setq gnus-group-marked (delete group gnus-group-marked)))
5158           (insert "#")
5159           (setq gnus-group-marked
5160                 (cons group (delete group gnus-group-marked)))))
5161       (or no-advance (gnus-group-next-group 1))
5162       (decf n))
5163     (gnus-summary-position-point)
5164     n))
5165
5166 (defun gnus-group-unmark-group (n)
5167   "Remove the mark from the current group."
5168   (interactive "p")
5169   (gnus-group-mark-group n 'unmark)
5170   (gnus-group-position-point))
5171
5172 (defun gnus-group-unmark-all-groups ()
5173   "Unmark all groups."
5174   (interactive)
5175   (let ((groups gnus-group-marked))
5176     (save-excursion
5177       (while groups
5178         (gnus-group-remove-mark (pop groups)))))
5179   (gnus-group-position-point))
5180
5181 (defun gnus-group-mark-region (unmark beg end)
5182   "Mark all groups between point and mark.
5183 If UNMARK, remove the mark instead."
5184   (interactive "P\nr")
5185   (let ((num (count-lines beg end)))
5186     (save-excursion
5187       (goto-char beg)
5188       (- num (gnus-group-mark-group num unmark)))))
5189
5190 (defun gnus-group-mark-buffer (&optional unmark)
5191   "Mark all groups in the buffer.
5192 If UNMARK, remove the mark instead."
5193   (interactive "P")
5194   (gnus-group-mark-region unmark (point-min) (point-max)))
5195
5196 (defun gnus-group-mark-regexp (regexp)
5197   "Mark all groups that match some regexp."
5198   (interactive "sMark (regexp): ")
5199   (let ((alist (cdr gnus-newsrc-alist))
5200         group)
5201     (while alist
5202       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5203         (gnus-group-set-mark group))))
5204   (gnus-group-position-point))
5205
5206 (defun gnus-group-remove-mark (group)
5207   "Remove the process mark from GROUP and move point there.
5208 Return nil if the group isn't displayed."
5209   (if (gnus-group-goto-group group)
5210       (save-excursion
5211         (gnus-group-mark-group 1 'unmark t)
5212         t)
5213     (setq gnus-group-marked
5214           (delete group gnus-group-marked))
5215     nil))
5216
5217 (defun gnus-group-set-mark (group)
5218   "Set the process mark on GROUP."
5219   (if (gnus-group-goto-group group) 
5220       (save-excursion
5221         (gnus-group-mark-group 1 nil t))
5222     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5223
5224 (defun gnus-group-universal-argument (arg &optional groups func)
5225   "Perform any command on all groups accoring to the process/prefix convention."
5226   (interactive "P")
5227   (let ((groups (or groups (gnus-group-process-prefix arg)))
5228         group func)
5229     (if (eq (setq func (or func
5230                            (key-binding
5231                             (read-key-sequence
5232                              (substitute-command-keys
5233                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5234             'undefined)
5235         (gnus-error 1 "Undefined key")
5236       (while groups
5237         (gnus-group-remove-mark (setq group (pop groups)))
5238         (command-execute func))))
5239   (gnus-group-position-point))
5240
5241 (defun gnus-group-process-prefix (n)
5242   "Return a list of groups to work on.
5243 Take into consideration N (the prefix) and the list of marked groups."
5244   (cond
5245    (n
5246     (setq n (prefix-numeric-value n))
5247     ;; There is a prefix, so we return a list of the N next
5248     ;; groups.
5249     (let ((way (if (< n 0) -1 1))
5250           (n (abs n))
5251           group groups)
5252       (save-excursion
5253         (while (and (> n 0)
5254                     (setq group (gnus-group-group-name)))
5255           (setq groups (cons group groups))
5256           (setq n (1- n))
5257           (gnus-group-next-group way)))
5258       (nreverse groups)))
5259    ((and (boundp 'transient-mark-mode)
5260          transient-mark-mode
5261          (boundp 'mark-active)
5262          mark-active)
5263     ;; Work on the region between point and mark.
5264     (let ((max (max (point) (mark)))
5265           groups)
5266       (save-excursion
5267         (goto-char (min (point) (mark)))
5268         (while
5269             (and
5270              (push (gnus-group-group-name) groups)
5271              (zerop (gnus-group-next-group 1))
5272              (< (point) max)))
5273         (nreverse groups))))
5274    (gnus-group-marked
5275     ;; No prefix, but a list of marked articles.
5276     (reverse gnus-group-marked))
5277    (t
5278     ;; Neither marked articles or a prefix, so we return the
5279     ;; current group.
5280     (let ((group (gnus-group-group-name)))
5281       (and group (list group))))))
5282
5283 ;; Selecting groups.
5284
5285 (defun gnus-group-read-group (&optional all no-article group)
5286   "Read news in this newsgroup.
5287 If the prefix argument ALL is non-nil, already read articles become
5288 readable.  IF ALL is a number, fetch this number of articles.  If the
5289 optional argument NO-ARTICLE is non-nil, no article will be
5290 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5291 group."
5292   (interactive "P")
5293   (let ((group (or group (gnus-group-group-name)))
5294         number active marked entry)
5295     (or group (error "No group on current line"))
5296     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5297                                             group gnus-newsrc-hashtb)))))
5298     ;; This group might be a dead group.  In that case we have to get
5299     ;; the number of unread articles from `gnus-active-hashtb'.
5300     (setq number
5301           (cond ((numberp all) all)
5302                 (entry (car entry))
5303                 ((setq active (gnus-active group))
5304                  (- (1+ (cdr active)) (car active)))))
5305     (gnus-summary-read-group
5306      group (or all (and (numberp number)
5307                         (zerop (+ number (length (cdr (assq 'tick marked)))
5308                                   (length (cdr (assq 'dormant marked)))))))
5309      no-article)))
5310
5311 (defun gnus-group-select-group (&optional all)
5312   "Select this newsgroup.
5313 No article is selected automatically.
5314 If ALL is non-nil, already read articles become readable.
5315 If ALL is a number, fetch this number of articles."
5316   (interactive "P")
5317   (gnus-group-read-group all t))
5318
5319 (defun gnus-group-quick-select-group (&optional all)
5320   "Select the current group \"quickly\".
5321 This means that no highlighting or scoring will be performed."
5322   (interactive "P")
5323   (let (gnus-visual
5324         gnus-score-find-score-files-function
5325         gnus-apply-kill-hook
5326         gnus-summary-expunge-below)
5327     (gnus-group-read-group all t)))
5328
5329 (defun gnus-group-visible-select-group (&optional all)
5330   "Select the current group without hiding any articles."
5331   (interactive "P")
5332   (let ((gnus-inhibit-limiting t))
5333     (gnus-group-read-group all t)))
5334
5335 ;;;###autoload
5336 (defun gnus-fetch-group (group)
5337   "Start Gnus if necessary and enter GROUP.
5338 Returns whether the fetching was successful or not."
5339   (interactive "sGroup name: ")
5340   (or (get-buffer gnus-group-buffer)
5341       (gnus))
5342   (gnus-group-read-group nil nil group))
5343
5344 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5345 ;; if selection was successful.
5346 (defun gnus-group-read-ephemeral-group
5347   (group method &optional activate quit-config)
5348   (let ((group (if (gnus-group-foreign-p group) group
5349                  (gnus-group-prefixed-name group method))))
5350     (gnus-sethash
5351      group
5352      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5353                      ((quit-config . ,(if quit-config quit-config
5354                                         (cons (current-buffer) 'summary))))))
5355      gnus-newsrc-hashtb)
5356     (set-buffer gnus-group-buffer)
5357     (or (gnus-check-server method)
5358         (error "Unable to contact server: %s" (gnus-status-message method)))
5359     (if activate (or (gnus-request-group group)
5360                      (error "Couldn't request group")))
5361     (condition-case ()
5362         (gnus-group-read-group t t group)
5363       (error nil)
5364       (quit nil))))
5365
5366 (defun gnus-group-jump-to-group (group)
5367   "Jump to newsgroup GROUP."
5368   (interactive
5369    (list (completing-read
5370           "Group: " gnus-active-hashtb nil
5371           (gnus-read-active-file-p)
5372           nil
5373           'gnus-group-history)))
5374
5375   (when (equal group "")
5376     (error "Empty group name"))
5377
5378   (when (string-match "[\000-\032]" group)
5379     (error "Control characters in group: %s" group))
5380
5381   (let ((b (text-property-any
5382             (point-min) (point-max)
5383             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5384     (unless (gnus-ephemeral-group-p group)
5385       (if b
5386           ;; Either go to the line in the group buffer...
5387           (goto-char b)
5388         ;; ... or insert the line.
5389         (or
5390          (gnus-active group)
5391          (gnus-activate-group group)
5392          (error "%s error: %s" group (gnus-status-message group)))
5393
5394         (gnus-group-update-group group)
5395         (goto-char (text-property-any
5396                     (point-min) (point-max)
5397                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5398     ;; Adjust cursor point.
5399     (gnus-group-position-point)))
5400
5401 (defun gnus-group-goto-group (group)
5402   "Goto to newsgroup GROUP."
5403   (when group
5404     (let ((b (text-property-any (point-min) (point-max)
5405                                 'gnus-group (gnus-intern-safe
5406                                              group gnus-active-hashtb))))
5407       (and b (goto-char b)))))
5408
5409 (defun gnus-group-next-group (n)
5410   "Go to next N'th newsgroup.
5411 If N is negative, search backward instead.
5412 Returns the difference between N and the number of skips actually
5413 done."
5414   (interactive "p")
5415   (gnus-group-next-unread-group n t))
5416
5417 (defun gnus-group-next-unread-group (n &optional all level)
5418   "Go to next N'th unread newsgroup.
5419 If N is negative, search backward instead.
5420 If ALL is non-nil, choose any newsgroup, unread or not.
5421 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5422 such group can be found, the next group with a level higher than
5423 LEVEL.
5424 Returns the difference between N and the number of skips actually
5425 made."
5426   (interactive "p")
5427   (let ((backward (< n 0))
5428         (n (abs n)))
5429     (while (and (> n 0)
5430                 (gnus-group-search-forward
5431                  backward (or (not gnus-group-goto-unread) all) level))
5432       (setq n (1- n)))
5433     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5434                                (if level " on this level or higher" "")))
5435     n))
5436
5437 (defun gnus-group-prev-group (n)
5438   "Go to previous N'th newsgroup.
5439 Returns the difference between N and the number of skips actually
5440 done."
5441   (interactive "p")
5442   (gnus-group-next-unread-group (- n) t))
5443
5444 (defun gnus-group-prev-unread-group (n)
5445   "Go to previous N'th unread newsgroup.
5446 Returns the difference between N and the number of skips actually
5447 done."
5448   (interactive "p")
5449   (gnus-group-next-unread-group (- n)))
5450
5451 (defun gnus-group-next-unread-group-same-level (n)
5452   "Go to next N'th unread newsgroup on the same level.
5453 If N is negative, search backward instead.
5454 Returns the difference between N and the number of skips actually
5455 done."
5456   (interactive "p")
5457   (gnus-group-next-unread-group n t (gnus-group-group-level))
5458   (gnus-group-position-point))
5459
5460 (defun gnus-group-prev-unread-group-same-level (n)
5461   "Go to next N'th unread newsgroup on the same level.
5462 Returns the difference between N and the number of skips actually
5463 done."
5464   (interactive "p")
5465   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5466   (gnus-group-position-point))
5467
5468 (defun gnus-group-best-unread-group (&optional exclude-group)
5469   "Go to the group with the highest level.
5470 If EXCLUDE-GROUP, do not go to that group."
5471   (interactive)
5472   (goto-char (point-min))
5473   (let ((best 100000)
5474         unread best-point)
5475     (while (not (eobp))
5476       (setq unread (get-text-property (point) 'gnus-unread))
5477       (if (and (numberp unread) (> unread 0))
5478           (progn
5479             (if (and (get-text-property (point) 'gnus-level)
5480                      (< (get-text-property (point) 'gnus-level) best)
5481                      (or (not exclude-group)
5482                          (not (equal exclude-group (gnus-group-group-name)))))
5483                 (progn
5484                   (setq best (get-text-property (point) 'gnus-level))
5485                   (setq best-point (point))))))
5486       (forward-line 1))
5487     (if best-point (goto-char best-point))
5488     (gnus-summary-position-point)
5489     (and best-point (gnus-group-group-name))))
5490
5491 (defun gnus-group-first-unread-group ()
5492   "Go to the first group with unread articles."
5493   (interactive)
5494   (prog1
5495       (let ((opoint (point))
5496             unread)
5497         (goto-char (point-min))
5498         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5499                 (and (numberp unread)   ; Not a topic.
5500                      (not (zerop unread))) ; Has unread articles.
5501                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5502             (point)                     ; Success.
5503           (goto-char opoint)
5504           nil))                         ; Not success.
5505     (gnus-group-position-point)))
5506
5507 (defun gnus-group-enter-server-mode ()
5508   "Jump to the server buffer."
5509   (interactive)
5510   (gnus-enter-server-buffer))
5511
5512 (defun gnus-group-make-group (name &optional method address)
5513   "Add a new newsgroup.
5514 The user will be prompted for a NAME, for a select METHOD, and an
5515 ADDRESS."
5516   (interactive
5517    (cons
5518     (read-string "Group name: ")
5519     (let ((method
5520            (completing-read
5521             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5522             nil t nil 'gnus-method-history)))
5523       (cond ((assoc method gnus-valid-select-methods)
5524              (list method
5525                    (if (memq 'prompt-address
5526                              (assoc method gnus-valid-select-methods))
5527                        (read-string "Address: ")
5528                      "")))
5529             ((assoc method gnus-server-alist)
5530              (list method))
5531             (t
5532              (list method ""))))))
5533
5534   (let* ((meth (and method (if address (list (intern method) address)
5535                              method)))
5536          (nname (if method (gnus-group-prefixed-name name meth) name))
5537          backend info)
5538     (when (gnus-gethash nname gnus-newsrc-hashtb)
5539       (error "Group %s already exists" nname))
5540     ;; Subscribe to the new group.
5541     (gnus-group-change-level
5542      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5543      gnus-level-default-subscribed gnus-level-killed
5544      (and (gnus-group-group-name)
5545           (gnus-gethash (gnus-group-group-name)
5546                         gnus-newsrc-hashtb))
5547      t)
5548     ;; Make it active.
5549     (gnus-set-active nname (cons 1 0))
5550     (or (gnus-ephemeral-group-p name)
5551         (gnus-dribble-enter
5552          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5553     ;; Insert the line.
5554     (gnus-group-insert-group-line-info nname)
5555     (forward-line -1)
5556     (gnus-group-position-point)
5557
5558     ;; Load the backend and try to make the backend create
5559     ;; the group as well.
5560     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5561                                                   nil meth))))
5562                  gnus-valid-select-methods)
5563       (require backend))
5564     (gnus-check-server meth)
5565     (and (gnus-check-backend-function 'request-create-group nname)
5566          (gnus-request-create-group nname))
5567     t))
5568
5569 (defun gnus-group-delete-group (group &optional force)
5570   "Delete the current group.
5571 If FORCE (the prefix) is non-nil, all the articles in the group will
5572 be deleted.  This is \"deleted\" as in \"removed forever from the face
5573 of the Earth\".  There is no undo."
5574   (interactive
5575    (list (gnus-group-group-name)
5576          current-prefix-arg))
5577   (or group (error "No group to rename"))
5578   (or (gnus-check-backend-function 'request-delete-group group)
5579       (error "This backend does not support group deletion"))
5580   (prog1
5581       (if (not (gnus-yes-or-no-p
5582                 (format
5583                  "Do you really want to delete %s%s? "
5584                  group (if force " and all its contents" ""))))
5585           () ; Whew!
5586         (gnus-message 6 "Deleting group %s..." group)
5587         (if (not (gnus-request-delete-group group force))
5588             (gnus-error 3 "Couldn't delete group %s" group)
5589           (gnus-message 6 "Deleting group %s...done" group)
5590           (gnus-group-goto-group group)
5591           (gnus-group-kill-group 1 t)
5592           (gnus-sethash group nil gnus-active-hashtb)
5593           t))
5594     (gnus-group-position-point)))
5595
5596 (defun gnus-group-rename-group (group new-name)
5597   (interactive
5598    (list
5599     (gnus-group-group-name)
5600     (progn
5601       (or (gnus-check-backend-function
5602            'request-rename-group (gnus-group-group-name))
5603           (error "This backend does not support renaming groups"))
5604       (read-string "New group name: "))))
5605
5606   (or (gnus-check-backend-function 'request-rename-group group)
5607       (error "This backend does not support renaming groups"))
5608
5609   (or group (error "No group to rename"))
5610   (and (string-match "^[ \t]*$" new-name)
5611        (error "Not a valid group name"))
5612
5613   ;; We find the proper prefixed name.
5614   (setq new-name
5615         (gnus-group-prefixed-name
5616          (gnus-group-real-name new-name)
5617          (gnus-info-method (gnus-get-info group))))
5618
5619   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5620   (prog1
5621       (if (not (gnus-request-rename-group group new-name))
5622           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5623         ;; We rename the group internally by killing it...
5624         (gnus-group-goto-group group)
5625         (gnus-group-kill-group)
5626         ;; ... changing its name ...
5627         (setcar (cdar gnus-list-of-killed-groups) new-name)
5628         ;; ... and then yanking it.  Magic!
5629         (gnus-group-yank-group)
5630         (gnus-set-active new-name (gnus-active group))
5631         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5632         new-name)
5633     (gnus-group-position-point)))
5634
5635 (defun gnus-group-edit-group (group &optional part)
5636   "Edit the group on the current line."
5637   (interactive (list (gnus-group-group-name)))
5638   (let* ((part (or part 'info))
5639          (done-func `(lambda ()
5640                        "Exit editing mode and update the information."
5641                        (interactive)
5642                        (gnus-group-edit-group-done ',part ,group)))
5643          (winconf (current-window-configuration))
5644          info)
5645     (or group (error "No group on current line"))
5646     (or (setq info (gnus-get-info group))
5647         (error "Killed group; can't be edited"))
5648     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5649     (gnus-configure-windows 'edit-group)
5650     (gnus-add-current-to-buffer-list)
5651     (emacs-lisp-mode)
5652     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5653     (use-local-map (copy-keymap emacs-lisp-mode-map))
5654     (local-set-key "\C-c\C-c" done-func)
5655     (make-local-variable 'gnus-prev-winconf)
5656     (setq gnus-prev-winconf winconf)
5657     (erase-buffer)
5658     (insert
5659      (cond
5660       ((eq part 'method)
5661        ";; Type `C-c C-c' after editing the select method.\n\n")
5662       ((eq part 'params)
5663        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5664       ((eq part 'info)
5665        ";; Type `C-c C-c' after editing the group info.\n\n")))
5666     (insert
5667      (pp-to-string
5668       (cond ((eq part 'method)
5669              (or (gnus-info-method info) "native"))
5670             ((eq part 'params)
5671              (gnus-info-params info))
5672             (t info)))
5673      "\n")))
5674
5675 (defun gnus-group-edit-group-method (group)
5676   "Edit the select method of GROUP."
5677   (interactive (list (gnus-group-group-name)))
5678   (gnus-group-edit-group group 'method))
5679
5680 (defun gnus-group-edit-group-parameters (group)
5681   "Edit the group parameters of GROUP."
5682   (interactive (list (gnus-group-group-name)))
5683   (gnus-group-edit-group group 'params))
5684
5685 (defun gnus-group-edit-group-done (part group)
5686   "Get info from buffer, update variables and jump to the group buffer."
5687   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5688   (goto-char (point-min))
5689   (let* ((form (read (current-buffer)))
5690          (winconf gnus-prev-winconf)
5691          (method (cond ((eq part 'info) (nth 4 form))
5692                        ((eq part 'method) form)
5693                        (t nil)))
5694          (info (cond ((eq part 'info) form)
5695                      ((eq part 'method) (gnus-get-info group))
5696                      (t nil)))
5697          (new-group (if info
5698                       (if (or (not method)
5699                               (gnus-server-equal
5700                                gnus-select-method method))
5701                           (gnus-group-real-name (car info))
5702                         (gnus-group-prefixed-name
5703                          (gnus-group-real-name (car info)) method))
5704                       nil)))
5705     (when (and new-group
5706                (not (equal new-group group)))
5707       (when (gnus-group-goto-group group)
5708         (gnus-group-kill-group 1))
5709       (gnus-activate-group new-group))
5710     ;; Set the info.
5711     (if (and info new-group)
5712         (progn
5713           (setq info (gnus-copy-sequence info))
5714           (setcar info new-group)
5715           (unless (gnus-server-equal method "native")
5716             (unless (nthcdr 3 info)
5717               (nconc info (list nil nil)))
5718             (unless (nthcdr 4 info)
5719               (nconc info (list nil)))
5720             (gnus-info-set-method info method))
5721           (gnus-group-set-info info))
5722       (gnus-group-set-info form (or new-group group) part))
5723     (kill-buffer (current-buffer))
5724     (and winconf (set-window-configuration winconf))
5725     (set-buffer gnus-group-buffer)
5726     (gnus-group-update-group (or new-group group))
5727     (gnus-group-position-point)))
5728
5729 (defun gnus-group-make-help-group ()
5730   "Create the Gnus documentation group."
5731   (interactive)
5732   (let ((path load-path)
5733         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5734         file dir)
5735     (and (gnus-gethash name gnus-newsrc-hashtb)
5736          (error "Documentation group already exists"))
5737     (while path
5738       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5739             file nil)
5740       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5741                 (file-exists-p
5742                  (setq file (concat (file-name-directory
5743                                      (directory-file-name dir))
5744                                     "etc/gnus-tut.txt"))))
5745         (setq path nil)))
5746     (if (not file)
5747         (gnus-message 1 "Couldn't find doc group")
5748       (gnus-group-make-group
5749        (gnus-group-real-name name)
5750        (list 'nndoc "gnus-help"
5751              (list 'nndoc-address file)
5752              (list 'nndoc-article-type 'mbox)))))
5753   (gnus-group-position-point))
5754
5755 (defun gnus-group-make-doc-group (file type)
5756   "Create a group that uses a single file as the source."
5757   (interactive
5758    (list (read-file-name "File name: ")
5759          (and current-prefix-arg 'ask)))
5760   (when (eq type 'ask)
5761     (let ((err "")
5762           char found)
5763       (while (not found)
5764         (message
5765          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5766          err)
5767         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5768                           ((= char ?b) 'babyl)
5769                           ((= char ?d) 'digest)
5770                           ((= char ?f) 'forward)
5771                           ((= char ?a) 'mmfd)
5772                           (t (setq err (format "%c unknown. " char))
5773                              nil))))
5774       (setq type found)))
5775   (let* ((file (expand-file-name file))
5776          (name (gnus-generate-new-group-name
5777                 (gnus-group-prefixed-name
5778                  (file-name-nondirectory file) '(nndoc "")))))
5779     (gnus-group-make-group
5780      (gnus-group-real-name name)
5781      (list 'nndoc (file-name-nondirectory file)
5782            (list 'nndoc-address file)
5783            (list 'nndoc-article-type (or type 'guess))))))
5784
5785 (defun gnus-group-make-archive-group (&optional all)
5786   "Create the (ding) Gnus archive group of the most recent articles.
5787 Given a prefix, create a full group."
5788   (interactive "P")
5789   (let ((group (gnus-group-prefixed-name
5790                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5791     (and (gnus-gethash group gnus-newsrc-hashtb)
5792          (error "Archive group already exists"))
5793     (gnus-group-make-group
5794      (gnus-group-real-name group)
5795      (list 'nndir (if all "hpc" "edu")
5796            (list 'nndir-directory
5797                  (if all gnus-group-archive-directory
5798                    gnus-group-recent-archive-directory))))))
5799
5800 (defun gnus-group-make-directory-group (dir)
5801   "Create an nndir group.
5802 The user will be prompted for a directory.  The contents of this
5803 directory will be used as a newsgroup.  The directory should contain
5804 mail messages or news articles in files that have numeric names."
5805   (interactive
5806    (list (read-file-name "Create group from directory: ")))
5807   (or (file-exists-p dir) (error "No such directory"))
5808   (or (file-directory-p dir) (error "Not a directory"))
5809   (let ((ext "")
5810         (i 0)
5811         group)
5812     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5813       (setq group
5814             (gnus-group-prefixed-name
5815              (concat (file-name-as-directory (directory-file-name dir))
5816                      ext)
5817              '(nndir "")))
5818       (setq ext (format "<%d>" (setq i (1+ i)))))
5819     (gnus-group-make-group
5820      (gnus-group-real-name group)
5821      (list 'nndir group (list 'nndir-directory dir)))))
5822
5823 (defun gnus-group-make-kiboze-group (group address scores)
5824   "Create an nnkiboze group.
5825 The user will be prompted for a name, a regexp to match groups, and
5826 score file entries for articles to include in the group."
5827   (interactive
5828    (list
5829     (read-string "nnkiboze group name: ")
5830     (read-string "Source groups (regexp): ")
5831     (let ((headers (mapcar (lambda (group) (list group))
5832                            '("subject" "from" "number" "date" "message-id"
5833                              "references" "chars" "lines" "xref"
5834                              "followup" "all" "body" "head")))
5835           scores header regexp regexps)
5836       (while (not (equal "" (setq header (completing-read
5837                                           "Match on header: " headers nil t))))
5838         (setq regexps nil)
5839         (while (not (equal "" (setq regexp (read-string
5840                                             (format "Match on %s (string): "
5841                                                     header)))))
5842           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5843         (setq scores (cons (cons header regexps) scores)))
5844       scores)))
5845   (gnus-group-make-group group "nnkiboze" address)
5846   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5847     (let (emacs-lisp-mode-hook)
5848       (pp scores (current-buffer)))))
5849
5850 (defun gnus-group-add-to-virtual (n vgroup)
5851   "Add the current group to a virtual group."
5852   (interactive
5853    (list current-prefix-arg
5854          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5855                           "nnvirtual:")))
5856   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5857       (error "%s is not an nnvirtual group" vgroup))
5858   (let* ((groups (gnus-group-process-prefix n))
5859          (method (gnus-info-method (gnus-get-info vgroup))))
5860     (setcar (cdr method)
5861             (concat
5862              (nth 1 method) "\\|"
5863              (mapconcat
5864               (lambda (s)
5865                 (gnus-group-remove-mark s)
5866                 (concat "\\(^" (regexp-quote s) "$\\)"))
5867               groups "\\|"))))
5868   (gnus-group-position-point))
5869
5870 (defun gnus-group-make-empty-virtual (group)
5871   "Create a new, fresh, empty virtual group."
5872   (interactive "sCreate new, empty virtual group: ")
5873   (let* ((method (list 'nnvirtual "^$"))
5874          (pgroup (gnus-group-prefixed-name group method)))
5875     ;; Check whether it exists already.
5876     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5877          (error "Group %s already exists." pgroup))
5878     ;; Subscribe the new group after the group on the current line.
5879     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5880     (gnus-group-update-group pgroup)
5881     (forward-line -1)
5882     (gnus-group-position-point)))
5883
5884 (defun gnus-group-enter-directory (dir)
5885   "Enter an ephemeral nneething group."
5886   (interactive "DDirectory to read: ")
5887   (let* ((method (list 'nneething dir))
5888          (leaf (gnus-group-prefixed-name
5889                 (file-name-nondirectory (directory-file-name dir))
5890                 method))
5891          (name (gnus-generate-new-group-name leaf)))
5892     (let ((nneething-read-only t))
5893       (or (gnus-group-read-ephemeral-group
5894            name method t
5895            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5896                                       'summary 'group)))
5897           (error "Couldn't enter %s" dir)))))
5898
5899 ;; Group sorting commands
5900 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5901
5902 (defun gnus-group-sort-groups (func &optional reverse)
5903   "Sort the group buffer according to FUNC.
5904 If REVERSE, reverse the sorting order."
5905   (interactive (list gnus-group-sort-function
5906                      current-prefix-arg))
5907   (let ((func (cond 
5908                ((not (listp func)) func)
5909                ((null func) func)
5910                ((= 1 (length func)) (car func))
5911                (t `(lambda (t1 t2)
5912                      ,(gnus-make-sort-function 
5913                        (reverse func)))))))
5914     ;; We peel off the dummy group from the alist.
5915     (when func
5916       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5917         (pop gnus-newsrc-alist))
5918       ;; Do the sorting.
5919       (setq gnus-newsrc-alist
5920             (sort gnus-newsrc-alist func))
5921       (when reverse
5922         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5923       ;; Regenerate the hash table.
5924       (gnus-make-hashtable-from-newsrc-alist)
5925       (gnus-group-list-groups))))
5926
5927 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5928   "Sort the group buffer alphabetically by group name.
5929 If REVERSE, sort in reverse order."
5930   (interactive "P")
5931   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5932
5933 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5934   "Sort the group buffer by number of unread articles.
5935 If REVERSE, sort in reverse order."
5936   (interactive "P")
5937   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5938
5939 (defun gnus-group-sort-groups-by-level (&optional reverse)
5940   "Sort the group buffer by group level.
5941 If REVERSE, sort in reverse order."
5942   (interactive "P")
5943   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5944
5945 (defun gnus-group-sort-groups-by-score (&optional reverse)
5946   "Sort the group buffer by group score.
5947 If REVERSE, sort in reverse order."
5948   (interactive "P")
5949   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5950
5951 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5952   "Sort the group buffer by group rank.
5953 If REVERSE, sort in reverse order."
5954   (interactive "P")
5955   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5956
5957 (defun gnus-group-sort-groups-by-method (&optional reverse)
5958   "Sort the group buffer alphabetically by backend name.
5959 If REVERSE, sort in reverse order."
5960   (interactive "P")
5961   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5962
5963 (defun gnus-group-sort-by-alphabet (info1 info2)
5964   "Sort alphabetically."
5965   (string< (gnus-info-group info1) (gnus-info-group info2)))
5966
5967 (defun gnus-group-sort-by-unread (info1 info2)
5968   "Sort by number of unread articles."
5969   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5970         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5971     (< (or (and (numberp n1) n1) 0)
5972        (or (and (numberp n2) n2) 0))))
5973
5974 (defun gnus-group-sort-by-level (info1 info2)
5975   "Sort by level."
5976   (< (gnus-info-level info1) (gnus-info-level info2)))
5977
5978 (defun gnus-group-sort-by-method (info1 info2)
5979   "Sort alphabetically by backend name."
5980   (string< (symbol-name (car (gnus-find-method-for-group
5981                               (gnus-info-group info1) info1)))
5982            (symbol-name (car (gnus-find-method-for-group
5983                               (gnus-info-group info2) info2)))))
5984
5985 (defun gnus-group-sort-by-score (info1 info2)
5986   "Sort by group score."
5987   (< (gnus-info-score info1) (gnus-info-score info2)))
5988
5989 (defun gnus-group-sort-by-rank (info1 info2)
5990   "Sort by level and score."
5991   (let ((level1 (gnus-info-level info1))
5992         (level2 (gnus-info-level info2)))
5993     (or (< level1 level2)
5994         (and (= level1 level2)
5995              (> (gnus-info-score info1) (gnus-info-score info2))))))
5996
5997 ;; Group catching up.
5998
5999 (defun gnus-group-clear-data (n)
6000   "Clear all marks and read ranges from the current group."
6001   (interactive "P")
6002   (let ((groups (gnus-group-process-prefix n))
6003         group info)
6004     (while (setq group (pop groups))
6005       (setq info (gnus-get-info group))
6006       (gnus-info-set-read info nil)
6007       (when (gnus-info-marks info)
6008         (gnus-info-set-marks info nil))
6009       (gnus-get-unread-articles-in-group info (gnus-active group) t)
6010       (when (gnus-group-goto-group group)
6011         (gnus-group-remove-mark group)
6012         (gnus-group-update-group-line)))))
6013
6014 (defun gnus-group-catchup-current (&optional n all)
6015   "Mark all articles not marked as unread in current newsgroup as read.
6016 If prefix argument N is numeric, the ARG next newsgroups will be
6017 caught up.  If ALL is non-nil, marked articles will also be marked as
6018 read.  Cross references (Xref: header) of articles are ignored.
6019 The difference between N and actual number of newsgroups that were
6020 caught up is returned."
6021   (interactive "P")
6022   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
6023                gnus-expert-user
6024                (gnus-y-or-n-p
6025                 (if all
6026                     "Do you really want to mark all articles as read? "
6027                   "Mark all unread articles as read? "))))
6028       n
6029     (let ((groups (gnus-group-process-prefix n))
6030           (ret 0))
6031       (while groups
6032         ;; Virtual groups have to be given special treatment.
6033         (let ((method (gnus-find-method-for-group (car groups))))
6034           (if (eq 'nnvirtual (car method))
6035               (nnvirtual-catchup-group
6036                (gnus-group-real-name (car groups)) (nth 1 method) all)))
6037         (gnus-group-remove-mark (car groups))
6038         (if (>= (gnus-group-group-level) gnus-level-zombie)
6039             (gnus-message 2 "Dead groups can't be caught up")
6040           (if (prog1
6041                   (gnus-group-goto-group (car groups))
6042                 (gnus-group-catchup (car groups) all))
6043               (gnus-group-update-group-line)
6044             (setq ret (1+ ret))))
6045         (setq groups (cdr groups)))
6046       (gnus-group-next-unread-group 1)
6047       ret)))
6048
6049 (defun gnus-group-catchup-current-all (&optional n)
6050   "Mark all articles in current newsgroup as read.
6051 Cross references (Xref: header) of articles are ignored."
6052   (interactive "P")
6053   (gnus-group-catchup-current n 'all))
6054
6055 (defun gnus-group-catchup (group &optional all)
6056   "Mark all articles in GROUP as read.
6057 If ALL is non-nil, all articles are marked as read.
6058 The return value is the number of articles that were marked as read,
6059 or nil if no action could be taken."
6060   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6061          (num (car entry)))
6062     ;; Do the updating only if the newsgroup isn't killed.
6063     (if (not (numberp (car entry)))
6064         (gnus-message 1 "Can't catch up; non-active group")
6065       ;; Do auto-expirable marks if that's required.
6066       (when (gnus-group-auto-expirable-p group)
6067         (gnus-add-marked-articles
6068          group 'expire (gnus-list-of-unread-articles group))
6069         (when all
6070           (let ((marks (nth 3 (nth 2 entry))))
6071             (gnus-add-marked-articles
6072              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6073             (gnus-add-marked-articles
6074              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6075       (when entry
6076         (gnus-update-read-articles group nil)
6077         ;; Also nix out the lists of marks and dormants.
6078         (when all
6079           (gnus-add-marked-articles group 'tick nil nil 'force)
6080           (gnus-add-marked-articles group 'dormant nil nil 'force))
6081         (run-hooks 'gnus-group-catchup-group-hook)
6082         num))))
6083
6084 (defun gnus-group-expire-articles (&optional n)
6085   "Expire all expirable articles in the current newsgroup."
6086   (interactive "P")
6087   (let ((groups (gnus-group-process-prefix n))
6088         group)
6089     (unless groups
6090       (error "No groups to expire"))
6091     (while (setq group (pop groups))
6092       (gnus-group-remove-mark group)
6093       (when (gnus-check-backend-function 'request-expire-articles group)
6094         (gnus-message 6 "Expiring articles in %s..." group)
6095         (let* ((info (gnus-get-info group))
6096                (expirable (if (gnus-group-total-expirable-p group)
6097                               (cons nil (gnus-list-of-read-articles group))
6098                             (assq 'expire (gnus-info-marks info))))
6099                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6100           (when expirable
6101             (setcdr
6102              expirable
6103              (gnus-compress-sequence
6104               (if expiry-wait
6105                   ;; We set the expiry variables to the groupp
6106                   ;; parameter. 
6107                   (let ((nnmail-expiry-wait-function nil)
6108                         (nnmail-expiry-wait expiry-wait))
6109                     (gnus-request-expire-articles
6110                      (gnus-uncompress-sequence (cdr expirable)) group))
6111                 ;; Just expire using the normal expiry values.
6112                 (gnus-request-expire-articles
6113                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6114           (gnus-message 6 "Expiring articles in %s...done" group)))
6115       (gnus-group-position-point))))
6116
6117 (defun gnus-group-expire-all-groups ()
6118   "Expire all expirable articles in all newsgroups."
6119   (interactive)
6120   (save-excursion
6121     (gnus-message 5 "Expiring...")
6122     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6123                                      (cdr gnus-newsrc-alist))))
6124       (gnus-group-expire-articles nil)))
6125   (gnus-group-position-point)
6126   (gnus-message 5 "Expiring...done"))
6127
6128 (defun gnus-group-set-current-level (n level)
6129   "Set the level of the next N groups to LEVEL."
6130   (interactive
6131    (list
6132     current-prefix-arg
6133     (string-to-int
6134      (let ((s (read-string
6135                (format "Level (default %s): "
6136                        (or (gnus-group-group-level) 
6137                            gnus-level-default-subscribed)))))
6138        (if (string-match "^\\s-*$" s)
6139            (int-to-string (or (gnus-group-group-level) 
6140                               gnus-level-default-subscribed))
6141          s)))))
6142   (or (and (>= level 1) (<= level gnus-level-killed))
6143       (error "Illegal level: %d" level))
6144   (let ((groups (gnus-group-process-prefix n))
6145         group)
6146     (while (setq group (pop groups))
6147       (gnus-group-remove-mark group)
6148       (gnus-message 6 "Changed level of %s from %d to %d"
6149                     group (or (gnus-group-group-level) gnus-level-killed)
6150                     level)
6151       (gnus-group-change-level
6152        group level (or (gnus-group-group-level) gnus-level-killed))
6153       (gnus-group-update-group-line)))
6154   (gnus-group-position-point))
6155
6156 (defun gnus-group-unsubscribe-current-group (&optional n)
6157   "Toggle subscription of the current group.
6158 If given numerical prefix, toggle the N next groups."
6159   (interactive "P")
6160   (let ((groups (gnus-group-process-prefix n))
6161         group)
6162     (while groups
6163       (setq group (car groups)
6164             groups (cdr groups))
6165       (gnus-group-remove-mark group)
6166       (gnus-group-unsubscribe-group
6167        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6168                  gnus-level-default-unsubscribed
6169                gnus-level-default-subscribed) t)
6170       (gnus-group-update-group-line))
6171     (gnus-group-next-group 1)))
6172
6173 (defun gnus-group-unsubscribe-group (group &optional level silent)
6174   "Toggle subscription to GROUP.
6175 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6176 group line."
6177   (interactive
6178    (list (completing-read
6179           "Group: " gnus-active-hashtb nil
6180           (gnus-read-active-file-p)
6181           nil 
6182           'gnus-group-history)))
6183   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6184     (cond
6185      ((string-match "^[ \t]$" group)
6186       (error "Empty group name"))
6187      (newsrc
6188       ;; Toggle subscription flag.
6189       (gnus-group-change-level
6190        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6191                                       gnus-level-subscribed)
6192                                   (1+ gnus-level-subscribed)
6193                                 gnus-level-default-subscribed)))
6194       (unless silent
6195         (gnus-group-update-group group)))
6196      ((and (stringp group)
6197            (or (not (gnus-read-active-file-p))
6198                (gnus-active group)))
6199       ;; Add new newsgroup.
6200       (gnus-group-change-level
6201        group
6202        (if level level gnus-level-default-subscribed)
6203        (or (and (member group gnus-zombie-list)
6204                 gnus-level-zombie)
6205            gnus-level-killed)
6206        (and (gnus-group-group-name)
6207             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6208       (unless silent
6209         (gnus-group-update-group group)))
6210      (t (error "No such newsgroup: %s" group)))
6211     (gnus-group-position-point)))
6212
6213 (defun gnus-group-transpose-groups (n)
6214   "Move the current newsgroup up N places.
6215 If given a negative prefix, move down instead.  The difference between
6216 N and the number of steps taken is returned."
6217   (interactive "p")
6218   (or (gnus-group-group-name)
6219       (error "No group on current line"))
6220   (gnus-group-kill-group 1)
6221   (prog1
6222       (forward-line (- n))
6223     (gnus-group-yank-group)
6224     (gnus-group-position-point)))
6225
6226 (defun gnus-group-kill-all-zombies ()
6227   "Kill all zombie newsgroups."
6228   (interactive)
6229   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6230   (setq gnus-zombie-list nil)
6231   (gnus-group-list-groups))
6232
6233 (defun gnus-group-kill-region (begin end)
6234   "Kill newsgroups in current region (excluding current point).
6235 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6236   (interactive "r")
6237   (let ((lines
6238          ;; Count lines.
6239          (save-excursion
6240            (count-lines
6241             (progn
6242               (goto-char begin)
6243               (beginning-of-line)
6244               (point))
6245             (progn
6246               (goto-char end)
6247               (beginning-of-line)
6248               (point))))))
6249     (goto-char begin)
6250     (beginning-of-line)                 ;Important when LINES < 1
6251     (gnus-group-kill-group lines)))
6252
6253 (defun gnus-group-kill-group (&optional n discard)
6254   "Kill the next N groups.
6255 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6256 However, only groups that were alive can be yanked; already killed
6257 groups or zombie groups can't be yanked.
6258 The return value is the name of the group that was killed, or a list
6259 of groups killed."
6260   (interactive "P")
6261   (let ((buffer-read-only nil)
6262         (groups (gnus-group-process-prefix n))
6263         group entry level out)
6264     (if (< (length groups) 10)
6265         ;; This is faster when there are few groups.
6266         (while groups
6267           (push (setq group (pop groups)) out)
6268           (gnus-group-remove-mark group)
6269           (setq level (gnus-group-group-level))
6270           (gnus-delete-line)
6271           (when (and (not discard)
6272                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6273             (push (cons (car entry) (nth 2 entry))
6274                   gnus-list-of-killed-groups))
6275           (gnus-group-change-level
6276            (if entry entry group) gnus-level-killed (if entry nil level)))
6277       ;; If there are lots and lots of groups to be killed, we use
6278       ;; this thing instead.
6279       (let (entry)
6280         (setq groups (nreverse groups))
6281         (while groups
6282           (gnus-group-remove-mark (setq group (pop groups)))
6283           (gnus-delete-line)
6284           (cond
6285            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6286             (push (cons (car entry) (nth 2 entry))
6287                   gnus-list-of-killed-groups)
6288             (setcdr (cdr entry) (cdddr entry)))
6289            ((member group gnus-zombie-list)
6290             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6291         (gnus-make-hashtable-from-newsrc-alist)))
6292
6293     (gnus-group-position-point)
6294     (if (< (length out) 2) (car out) (nreverse out))))
6295
6296 (defun gnus-group-yank-group (&optional arg)
6297   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6298 inserting it before the current newsgroup.  The numeric ARG specifies
6299 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6300 is returned, or (if several groups are yanked) a list of yanked groups
6301 is returned."
6302   (interactive "p")
6303   (setq arg (or arg 1))
6304   (let (info group prev out)
6305     (while (>= (decf arg) 0)
6306       (if (not (setq info (pop gnus-list-of-killed-groups)))
6307           (error "No more newsgroups to yank"))
6308       (push (setq group (nth 1 info)) out)
6309       ;; Find which newsgroup to insert this one before - search
6310       ;; backward until something suitable is found.  If there are no
6311       ;; other newsgroups in this buffer, just make this newsgroup the
6312       ;; first newsgroup.
6313       (setq prev (gnus-group-group-name))
6314       (gnus-group-change-level
6315        info (gnus-info-level (cdr info)) gnus-level-killed
6316        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6317        t)
6318       (gnus-group-insert-group-line-info group))
6319     (forward-line -1)
6320     (gnus-group-position-point)
6321     (if (< (length out) 2) (car out) (nreverse out))))
6322
6323 (defun gnus-group-kill-level (level)
6324   "Kill all groups that is on a certain LEVEL."
6325   (interactive "nKill all groups on level: ")
6326   (cond
6327    ((= level gnus-level-zombie)
6328     (setq gnus-killed-list
6329           (nconc gnus-zombie-list gnus-killed-list))
6330     (setq gnus-zombie-list nil))
6331    ((and (< level gnus-level-zombie)
6332          (> level 0)
6333          (or gnus-expert-user
6334              (gnus-yes-or-no-p
6335               (format
6336                "Do you really want to kill all groups on level %d? "
6337                level))))
6338     (let* ((prev gnus-newsrc-alist)
6339            (alist (cdr prev)))
6340       (while alist
6341         (if (= (gnus-info-level level) level)
6342             (setcdr prev (cdr alist))
6343           (setq prev alist))
6344         (setq alist (cdr alist)))
6345       (gnus-make-hashtable-from-newsrc-alist)
6346       (gnus-group-list-groups)))
6347    (t
6348     (error "Can't kill; illegal level: %d" level))))
6349
6350 (defun gnus-group-list-all-groups (&optional arg)
6351   "List all newsgroups with level ARG or lower.
6352 Default is gnus-level-unsubscribed, which lists all subscribed and most
6353 unsubscribed groups."
6354   (interactive "P")
6355   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6356
6357 ;; Redefine this to list ALL killed groups if prefix arg used.
6358 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6359 (defun gnus-group-list-killed (&optional arg)
6360   "List all killed newsgroups in the group buffer.
6361 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6362 entail asking the server for the groups."
6363   (interactive "P")
6364   ;; Find all possible killed newsgroups if arg.
6365   (when arg
6366     (gnus-get-killed-groups))
6367   (if (not gnus-killed-list)
6368       (gnus-message 6 "No killed groups")
6369     (let (gnus-group-list-mode)
6370       (funcall gnus-group-prepare-function
6371                gnus-level-killed t gnus-level-killed))
6372     (goto-char (point-min)))
6373   (gnus-group-position-point))
6374
6375 (defun gnus-group-list-zombies ()
6376   "List all zombie newsgroups in the group buffer."
6377   (interactive)
6378   (if (not gnus-zombie-list)
6379       (gnus-message 6 "No zombie groups")
6380     (let (gnus-group-list-mode)
6381       (funcall gnus-group-prepare-function
6382                gnus-level-zombie t gnus-level-zombie))
6383     (goto-char (point-min)))
6384   (gnus-group-position-point))
6385
6386 (defun gnus-group-list-active ()
6387   "List all groups that are available from the server(s)."
6388   (interactive)
6389   ;; First we make sure that we have really read the active file.
6390   (unless (gnus-read-active-file-p)
6391     (let ((gnus-read-active-file t))
6392       (gnus-read-active-file)))
6393   ;; Find all groups and sort them.
6394   (let ((groups
6395          (sort
6396           (let (list)
6397             (mapatoms
6398              (lambda (sym)
6399                (and (boundp sym)
6400                     (symbol-value sym)
6401                     (setq list (cons (symbol-name sym) list))))
6402              gnus-active-hashtb)
6403             list)
6404           'string<))
6405         (buffer-read-only nil))
6406     (erase-buffer)
6407     (while groups
6408       (gnus-group-insert-group-line-info (pop groups)))
6409     (goto-char (point-min))))
6410
6411 (defun gnus-activate-all-groups (level)
6412   "Activate absolutely all groups."
6413   (interactive (list 7))
6414   (let ((gnus-activate-level level)
6415         (gnus-activate-foreign-newsgroups level))
6416     (gnus-group-get-new-news)))
6417
6418 (defun gnus-group-get-new-news (&optional arg)
6419   "Get newly arrived articles.
6420 If ARG is a number, it specifies which levels you are interested in
6421 re-scanning.  If ARG is non-nil and not a number, this will force
6422 \"hard\" re-reading of the active files from all servers."
6423   (interactive "P")
6424   (run-hooks 'gnus-get-new-news-hook)
6425   ;; We might read in new NoCeM messages here.
6426   (when (and gnus-use-nocem 
6427              (null arg))
6428     (gnus-nocem-scan-groups))
6429   ;; If ARG is not a number, then we read the active file.
6430   (when (and arg (not (numberp arg)))
6431     (let ((gnus-read-active-file t))
6432       (gnus-read-active-file))
6433     (setq arg nil))
6434
6435   (setq arg (gnus-group-default-level arg t))
6436   (if (and gnus-read-active-file (not arg))
6437       (progn
6438         (gnus-read-active-file)
6439         (gnus-get-unread-articles arg))
6440     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6441       (gnus-get-unread-articles arg)))
6442   (run-hooks 'gnus-after-getting-new-news-hook)
6443   (gnus-group-list-groups))
6444
6445 (defun gnus-group-get-new-news-this-group (&optional n)
6446   "Check for newly arrived news in the current group (and the N-1 next groups).
6447 The difference between N and the number of newsgroup checked is returned.
6448 If N is negative, this group and the N-1 previous groups will be checked."
6449   (interactive "P")
6450   (let* ((groups (gnus-group-process-prefix n))
6451          (ret (if (numberp n) (- n (length groups)) 0))
6452          (beg (unless n (point)))
6453          group)
6454     (while (setq group (pop groups))
6455       (gnus-group-remove-mark group)
6456       (if (gnus-activate-group group 'scan)
6457           (progn
6458             (gnus-get-unread-articles-in-group
6459              (gnus-get-info group) (gnus-active group) t)
6460             (unless (gnus-virtual-group-p group)
6461               (gnus-close-group group))
6462             (gnus-group-update-group group))
6463         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6464     (when beg (goto-char beg))
6465     (when gnus-goto-next-group-when-activating
6466       (gnus-group-next-unread-group 1 t))
6467     (gnus-summary-position-point)
6468     ret))
6469
6470 (defun gnus-group-fetch-faq (group &optional faq-dir)
6471   "Fetch the FAQ for the current group."
6472   (interactive
6473    (list
6474     (gnus-group-real-name (gnus-group-group-name))
6475     (cond (current-prefix-arg
6476            (completing-read
6477             "Faq dir: " (and (listp gnus-group-faq-directory)
6478                              gnus-group-faq-directory))))))
6479   (or faq-dir
6480       (setq faq-dir (if (listp gnus-group-faq-directory)
6481                         (car gnus-group-faq-directory)
6482                       gnus-group-faq-directory)))
6483   (or group (error "No group name given"))
6484   (let ((file (concat (file-name-as-directory faq-dir)
6485                       (gnus-group-real-name group))))
6486     (if (not (file-exists-p file))
6487         (error "No such file: %s" file)
6488       (find-file file))))
6489
6490 (defun gnus-group-describe-group (force &optional group)
6491   "Display a description of the current newsgroup."
6492   (interactive (list current-prefix-arg (gnus-group-group-name)))
6493   (and force (setq gnus-description-hashtb nil))
6494   (let ((method (gnus-find-method-for-group group))
6495         desc)
6496     (or group (error "No group name given"))
6497     (and (or (and gnus-description-hashtb
6498                   ;; We check whether this group's method has been
6499                   ;; queried for a description file.
6500                   (gnus-gethash
6501                    (gnus-group-prefixed-name "" method)
6502                    gnus-description-hashtb))
6503              (setq desc (gnus-group-get-description group))
6504              (gnus-read-descriptions-file method))
6505          (gnus-message 1
6506           (or desc (gnus-gethash group gnus-description-hashtb)
6507               "No description available")))))
6508
6509 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6510 (defun gnus-group-describe-all-groups (&optional force)
6511   "Pop up a buffer with descriptions of all newsgroups."
6512   (interactive "P")
6513   (and force (setq gnus-description-hashtb nil))
6514   (if (not (or gnus-description-hashtb
6515                (gnus-read-all-descriptions-files)))
6516       (error "Couldn't request descriptions file"))
6517   (let ((buffer-read-only nil)
6518         b)
6519     (erase-buffer)
6520     (mapatoms
6521      (lambda (group)
6522        (setq b (point))
6523        (insert (format "      *: %-20s %s\n" (symbol-name group)
6524                        (symbol-value group)))
6525        (gnus-add-text-properties
6526         b (1+ b) (list 'gnus-group group
6527                        'gnus-unread t 'gnus-marked nil
6528                        'gnus-level (1+ gnus-level-subscribed))))
6529      gnus-description-hashtb)
6530     (goto-char (point-min))
6531     (gnus-group-position-point)))
6532
6533 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6534 (defun gnus-group-apropos (regexp &optional search-description)
6535   "List all newsgroups that have names that match a regexp."
6536   (interactive "sGnus apropos (regexp): ")
6537   (let ((prev "")
6538         (obuf (current-buffer))
6539         groups des)
6540     ;; Go through all newsgroups that are known to Gnus.
6541     (mapatoms
6542      (lambda (group)
6543        (and (symbol-name group)
6544             (string-match regexp (symbol-name group))
6545             (setq groups (cons (symbol-name group) groups))))
6546      gnus-active-hashtb)
6547     ;; Also go through all descriptions that are known to Gnus.
6548     (when search-description
6549       (mapatoms
6550        (lambda (group)
6551          (and (string-match regexp (symbol-value group))
6552               (gnus-active (symbol-name group))
6553               (setq groups (cons (symbol-name group) groups))))
6554        gnus-description-hashtb))
6555     (if (not groups)
6556         (gnus-message 3 "No groups matched \"%s\"." regexp)
6557       ;; Print out all the groups.
6558       (save-excursion
6559         (pop-to-buffer "*Gnus Help*")
6560         (buffer-disable-undo (current-buffer))
6561         (erase-buffer)
6562         (setq groups (sort groups 'string<))
6563         (while groups
6564           ;; Groups may be entered twice into the list of groups.
6565           (if (not (string= (car groups) prev))
6566               (progn
6567                 (insert (setq prev (car groups)) "\n")
6568                 (if (and gnus-description-hashtb
6569                          (setq des (gnus-gethash (car groups)
6570                                                  gnus-description-hashtb)))
6571                     (insert "  " des "\n"))))
6572           (setq groups (cdr groups)))
6573         (goto-char (point-min))))
6574     (pop-to-buffer obuf)))
6575
6576 (defun gnus-group-description-apropos (regexp)
6577   "List all newsgroups that have names or descriptions that match a regexp."
6578   (interactive "sGnus description apropos (regexp): ")
6579   (if (not (or gnus-description-hashtb
6580                (gnus-read-all-descriptions-files)))
6581       (error "Couldn't request descriptions file"))
6582   (gnus-group-apropos regexp t))
6583
6584 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6585 (defun gnus-group-list-matching (level regexp &optional all lowest)
6586   "List all groups with unread articles that match REGEXP.
6587 If the prefix LEVEL is non-nil, it should be a number that says which
6588 level to cut off listing groups.
6589 If ALL, also list groups with no unread articles.
6590 If LOWEST, don't list groups with level lower than LOWEST.
6591
6592 This command may read the active file."
6593   (interactive "P\nsList newsgroups matching: ")
6594   ;; First make sure active file has been read.
6595   (when (and level
6596              (>= (prefix-numeric-value level) gnus-level-killed))
6597     (gnus-get-killed-groups))
6598   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6599                            all (or lowest 1) regexp)
6600   (goto-char (point-min))
6601   (gnus-group-position-point))
6602
6603 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6604   "List all groups that match REGEXP.
6605 If the prefix LEVEL is non-nil, it should be a number that says which
6606 level to cut off listing groups.
6607 If LOWEST, don't list groups with level lower than LOWEST."
6608   (interactive "P\nsList newsgroups matching: ")
6609   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6610
6611 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6612 (defun gnus-group-save-newsrc (&optional force)
6613   "Save the Gnus startup files.
6614 If FORCE, force saving whether it is necessary or not."
6615   (interactive "P")
6616   (gnus-save-newsrc-file force))
6617
6618 (defun gnus-group-restart (&optional arg)
6619   "Force Gnus to read the .newsrc file."
6620   (interactive "P")
6621   (when (gnus-yes-or-no-p
6622          (format "Are you sure you want to read %s? "
6623                  gnus-current-startup-file))
6624     (gnus-save-newsrc-file)
6625     (gnus-setup-news 'force)
6626     (gnus-group-list-groups arg)))
6627
6628 (defun gnus-group-read-init-file ()
6629   "Read the Gnus elisp init file."
6630   (interactive)
6631   (gnus-read-init-file))
6632
6633 (defun gnus-group-check-bogus-groups (&optional silent)
6634   "Check bogus newsgroups.
6635 If given a prefix, don't ask for confirmation before removing a bogus
6636 group."
6637   (interactive "P")
6638   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6639   (gnus-group-list-groups))
6640
6641 (defun gnus-group-edit-global-kill (&optional article group)
6642   "Edit the global kill file.
6643 If GROUP, edit that local kill file instead."
6644   (interactive "P")
6645   (setq gnus-current-kill-article article)
6646   (gnus-kill-file-edit-file group)
6647   (gnus-message
6648    6
6649    (substitute-command-keys
6650     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6651             (if group "local" "global")))))
6652
6653 (defun gnus-group-edit-local-kill (article group)
6654   "Edit a local kill file."
6655   (interactive (list nil (gnus-group-group-name)))
6656   (gnus-group-edit-global-kill article group))
6657
6658 (defun gnus-group-force-update ()
6659   "Update `.newsrc' file."
6660   (interactive)
6661   (gnus-save-newsrc-file))
6662
6663 (defun gnus-group-suspend ()
6664   "Suspend the current Gnus session.
6665 In fact, cleanup buffers except for group mode buffer.
6666 The hook gnus-suspend-gnus-hook is called before actually suspending."
6667   (interactive)
6668   (run-hooks 'gnus-suspend-gnus-hook)
6669   ;; Kill Gnus buffers except for group mode buffer.
6670   (let ((group-buf (get-buffer gnus-group-buffer)))
6671     ;; Do this on a separate list in case the user does a ^G before we finish
6672     (let ((gnus-buffer-list
6673            (delq group-buf (delq gnus-dribble-buffer
6674                                  (append gnus-buffer-list nil)))))
6675       (while gnus-buffer-list
6676         (gnus-kill-buffer (car gnus-buffer-list))
6677         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6678     (if group-buf
6679         (progn
6680           (setq gnus-buffer-list (list group-buf))
6681           (bury-buffer group-buf)
6682           (delete-windows-on group-buf t)))))
6683
6684 (defun gnus-group-clear-dribble ()
6685   "Clear all information from the dribble buffer."
6686   (interactive)
6687   (gnus-dribble-clear)
6688   (gnus-message 7 "Cleared dribble buffer"))
6689
6690 (defun gnus-group-exit ()
6691   "Quit reading news after updating .newsrc.eld and .newsrc.
6692 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6693   (interactive)
6694   (when 
6695       (or noninteractive                ;For gnus-batch-kill
6696           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6697           (not gnus-interactive-exit)   ;Without confirmation
6698           gnus-expert-user
6699           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6700     (run-hooks 'gnus-exit-gnus-hook)
6701     ;; Offer to save data from non-quitted summary buffers.
6702     (gnus-offer-save-summaries)
6703     ;; Save the newsrc file(s).
6704     (gnus-save-newsrc-file)
6705     ;; Kill-em-all.
6706     (gnus-close-backends)
6707     ;; Reset everything.
6708     (gnus-clear-system)
6709     ;; Allow the user to do things after cleaning up.
6710     (run-hooks 'gnus-after-exiting-gnus-hook)))
6711
6712 (defun gnus-close-backends ()
6713   ;; Send a close request to all backends that support such a request.
6714   (let ((methods gnus-valid-select-methods)
6715         func)
6716     (while methods
6717       (if (fboundp (setq func (intern (concat (caar methods)
6718                                               "-request-close"))))
6719           (funcall func))
6720       (setq methods (cdr methods)))))
6721
6722 (defun gnus-group-quit ()
6723   "Quit reading news without updating .newsrc.eld or .newsrc.
6724 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6725   (interactive)
6726   (when (or noninteractive              ;For gnus-batch-kill
6727             (zerop (buffer-size))
6728             (not (gnus-server-opened gnus-select-method))
6729             gnus-expert-user
6730             (not gnus-current-startup-file)
6731             (gnus-yes-or-no-p
6732              (format "Quit reading news without saving %s? "
6733                      (file-name-nondirectory gnus-current-startup-file))))
6734     (run-hooks 'gnus-exit-gnus-hook)
6735     (if gnus-use-full-window
6736         (delete-other-windows)
6737       (gnus-remove-some-windows))
6738     (gnus-dribble-save)
6739     (gnus-close-backends)
6740     (gnus-clear-system)
6741     ;; Allow the user to do things after cleaning up.
6742     (run-hooks 'gnus-after-exiting-gnus-hook)))
6743
6744 (defun gnus-offer-save-summaries ()
6745   "Offer to save all active summary buffers."
6746   (save-excursion
6747     (let ((buflist (buffer-list))
6748           buffers bufname)
6749       ;; Go through all buffers and find all summaries.
6750       (while buflist
6751         (and (setq bufname (buffer-name (car buflist)))
6752              (string-match "Summary" bufname)
6753              (save-excursion
6754                (set-buffer bufname)
6755                ;; We check that this is, indeed, a summary buffer.
6756                (and (eq major-mode 'gnus-summary-mode)
6757                     ;; Also make sure this isn't bogus.
6758                     gnus-newsgroup-prepared))
6759              (push bufname buffers))
6760         (setq buflist (cdr buflist)))
6761       ;; Go through all these summary buffers and offer to save them.
6762       (when buffers
6763         (map-y-or-n-p
6764          "Update summary buffer %s? "
6765          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6766          buffers)))))
6767
6768 (defun gnus-group-describe-briefly ()
6769   "Give a one line description of the group mode commands."
6770   (interactive)
6771   (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")))
6772
6773 (defun gnus-group-browse-foreign-server (method)
6774   "Browse a foreign news server.
6775 If called interactively, this function will ask for a select method
6776  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6777 If not, METHOD should be a list where the first element is the method
6778 and the second element is the address."
6779   (interactive
6780    (list (let ((how (completing-read
6781                      "Which backend: "
6782                      (append gnus-valid-select-methods gnus-server-alist)
6783                      nil t (cons "nntp" 0) 'gnus-method-history)))
6784            ;; We either got a backend name or a virtual server name.
6785            ;; If the first, we also need an address.
6786            (if (assoc how gnus-valid-select-methods)
6787                (list (intern how)
6788                      ;; Suggested by mapjph@bath.ac.uk.
6789                      (completing-read
6790                       "Address: "
6791                       (mapcar (lambda (server) (list server))
6792                               gnus-secondary-servers)))
6793              ;; We got a server name, so we find the method.
6794              (gnus-server-to-method how)))))
6795   (gnus-browse-foreign-server method))
6796
6797 \f
6798 ;;;
6799 ;;; Gnus summary mode
6800 ;;;
6801
6802 (defvar gnus-summary-mode-map nil)
6803
6804 (put 'gnus-summary-mode 'mode-class 'special)
6805
6806 (unless gnus-summary-mode-map
6807   (setq gnus-summary-mode-map (make-keymap))
6808   (suppress-keymap gnus-summary-mode-map)
6809
6810   ;; Non-orthogonal keys
6811
6812   (gnus-define-keys gnus-summary-mode-map
6813     " " gnus-summary-next-page
6814     "\177" gnus-summary-prev-page
6815     [delete] gnus-summary-prev-page
6816     "\r" gnus-summary-scroll-up
6817     "n" gnus-summary-next-unread-article
6818     "p" gnus-summary-prev-unread-article
6819     "N" gnus-summary-next-article
6820     "P" gnus-summary-prev-article
6821     "\M-\C-n" gnus-summary-next-same-subject
6822     "\M-\C-p" gnus-summary-prev-same-subject
6823     "\M-n" gnus-summary-next-unread-subject
6824     "\M-p" gnus-summary-prev-unread-subject
6825     "." gnus-summary-first-unread-article
6826     "," gnus-summary-best-unread-article
6827     "\M-s" gnus-summary-search-article-forward
6828     "\M-r" gnus-summary-search-article-backward
6829     "<" gnus-summary-beginning-of-article
6830     ">" gnus-summary-end-of-article
6831     "j" gnus-summary-goto-article
6832     "^" gnus-summary-refer-parent-article
6833     "\M-^" gnus-summary-refer-article
6834     "u" gnus-summary-tick-article-forward
6835     "!" gnus-summary-tick-article-forward
6836     "U" gnus-summary-tick-article-backward
6837     "d" gnus-summary-mark-as-read-forward
6838     "D" gnus-summary-mark-as-read-backward
6839     "E" gnus-summary-mark-as-expirable
6840     "\M-u" gnus-summary-clear-mark-forward
6841     "\M-U" gnus-summary-clear-mark-backward
6842     "k" gnus-summary-kill-same-subject-and-select
6843     "\C-k" gnus-summary-kill-same-subject
6844     "\M-\C-k" gnus-summary-kill-thread
6845     "\M-\C-l" gnus-summary-lower-thread
6846     "e" gnus-summary-edit-article
6847     "#" gnus-summary-mark-as-processable
6848     "\M-#" gnus-summary-unmark-as-processable
6849     "\M-\C-t" gnus-summary-toggle-threads
6850     "\M-\C-s" gnus-summary-show-thread
6851     "\M-\C-h" gnus-summary-hide-thread
6852     "\M-\C-f" gnus-summary-next-thread
6853     "\M-\C-b" gnus-summary-prev-thread
6854     "\M-\C-u" gnus-summary-up-thread
6855     "\M-\C-d" gnus-summary-down-thread
6856     "&" gnus-summary-execute-command
6857     "c" gnus-summary-catchup-and-exit
6858     "\C-w" gnus-summary-mark-region-as-read
6859     "\C-t" gnus-summary-toggle-truncation
6860     "?" gnus-summary-mark-as-dormant
6861     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6862     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6863     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6864     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6865     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6866     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6867     "=" gnus-summary-expand-window
6868     "\C-x\C-s" gnus-summary-reselect-current-group
6869     "\M-g" gnus-summary-rescan-group
6870     "w" gnus-summary-stop-page-breaking
6871     "\C-c\C-r" gnus-summary-caesar-message
6872     "\M-t" gnus-summary-toggle-mime
6873     "f" gnus-summary-followup
6874     "F" gnus-summary-followup-with-original
6875     "C" gnus-summary-cancel-article
6876     "r" gnus-summary-reply
6877     "R" gnus-summary-reply-with-original
6878     "\C-c\C-f" gnus-summary-mail-forward
6879     "o" gnus-summary-save-article
6880     "\C-o" gnus-summary-save-article-mail
6881     "|" gnus-summary-pipe-output
6882     "\M-k" gnus-summary-edit-local-kill
6883     "\M-K" gnus-summary-edit-global-kill
6884     "V" gnus-version
6885     "\C-c\C-d" gnus-summary-describe-group
6886     "q" gnus-summary-exit
6887     "Q" gnus-summary-exit-no-update
6888     "\C-c\C-i" gnus-info-find-node
6889     gnus-mouse-2 gnus-mouse-pick-article
6890     "m" gnus-summary-mail-other-window
6891     "a" gnus-summary-post-news
6892     "x" gnus-summary-limit-to-unread
6893     "s" gnus-summary-isearch-article
6894     "t" gnus-article-hide-headers
6895     "g" gnus-summary-show-article
6896     "l" gnus-summary-goto-last-article
6897     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6898     "\C-d" gnus-summary-enter-digest-group
6899     "\C-c\C-b" gnus-bug
6900     "*" gnus-cache-enter-article
6901     "\M-*" gnus-cache-remove-article
6902     "\M-&" gnus-summary-universal-argument
6903     "\C-l" gnus-recenter
6904     "I" gnus-summary-increase-score
6905     "L" gnus-summary-lower-score
6906
6907     "V" gnus-summary-score-map
6908     "X" gnus-uu-extract-map
6909     "S" gnus-summary-send-map)
6910
6911   ;; Sort of orthogonal keymap
6912   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6913     "t" gnus-summary-tick-article-forward
6914     "!" gnus-summary-tick-article-forward
6915     "d" gnus-summary-mark-as-read-forward
6916     "r" gnus-summary-mark-as-read-forward
6917     "c" gnus-summary-clear-mark-forward
6918     " " gnus-summary-clear-mark-forward
6919     "e" gnus-summary-mark-as-expirable
6920     "x" gnus-summary-mark-as-expirable
6921     "?" gnus-summary-mark-as-dormant
6922     "b" gnus-summary-set-bookmark
6923     "B" gnus-summary-remove-bookmark
6924     "#" gnus-summary-mark-as-processable
6925     "\M-#" gnus-summary-unmark-as-processable
6926     "S" gnus-summary-limit-include-expunged
6927     "C" gnus-summary-catchup
6928     "H" gnus-summary-catchup-to-here
6929     "\C-c" gnus-summary-catchup-all
6930     "k" gnus-summary-kill-same-subject-and-select
6931     "K" gnus-summary-kill-same-subject
6932     "P" gnus-uu-mark-map)
6933
6934   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6935     "c" gnus-summary-clear-above
6936     "u" gnus-summary-tick-above
6937     "m" gnus-summary-mark-above
6938     "k" gnus-summary-kill-below)
6939
6940   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6941     "/" gnus-summary-limit-to-subject
6942     "n" gnus-summary-limit-to-articles
6943     "w" gnus-summary-pop-limit
6944     "s" gnus-summary-limit-to-subject
6945     "a" gnus-summary-limit-to-author
6946     "u" gnus-summary-limit-to-unread
6947     "m" gnus-summary-limit-to-marks
6948     "v" gnus-summary-limit-to-score
6949     "D" gnus-summary-limit-include-dormant
6950     "d" gnus-summary-limit-exclude-dormant
6951     ;;  "t" gnus-summary-limit-exclude-thread
6952     "E" gnus-summary-limit-include-expunged
6953     "c" gnus-summary-limit-exclude-childless-dormant
6954     "C" gnus-summary-limit-mark-excluded-as-read)
6955
6956   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6957     "n" gnus-summary-next-unread-article
6958     "p" gnus-summary-prev-unread-article
6959     "N" gnus-summary-next-article
6960     "P" gnus-summary-prev-article
6961     "\C-n" gnus-summary-next-same-subject
6962     "\C-p" gnus-summary-prev-same-subject
6963     "\M-n" gnus-summary-next-unread-subject
6964     "\M-p" gnus-summary-prev-unread-subject
6965     "f" gnus-summary-first-unread-article
6966     "b" gnus-summary-best-unread-article
6967     "j" gnus-summary-goto-article
6968     "g" gnus-summary-goto-subject
6969     "l" gnus-summary-goto-last-article
6970     "p" gnus-summary-pop-article)
6971
6972   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6973     "k" gnus-summary-kill-thread
6974     "l" gnus-summary-lower-thread
6975     "i" gnus-summary-raise-thread
6976     "T" gnus-summary-toggle-threads
6977     "t" gnus-summary-rethread-current
6978     "^" gnus-summary-reparent-thread
6979     "s" gnus-summary-show-thread
6980     "S" gnus-summary-show-all-threads
6981     "h" gnus-summary-hide-thread
6982     "H" gnus-summary-hide-all-threads
6983     "n" gnus-summary-next-thread
6984     "p" gnus-summary-prev-thread
6985     "u" gnus-summary-up-thread
6986     "o" gnus-summary-top-thread
6987     "d" gnus-summary-down-thread
6988     "#" gnus-uu-mark-thread
6989     "\M-#" gnus-uu-unmark-thread)
6990
6991   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6992     "c" gnus-summary-catchup-and-exit
6993     "C" gnus-summary-catchup-all-and-exit
6994     "E" gnus-summary-exit-no-update
6995     "Q" gnus-summary-exit
6996     "Z" gnus-summary-exit
6997     "n" gnus-summary-catchup-and-goto-next-group
6998     "R" gnus-summary-reselect-current-group
6999     "G" gnus-summary-rescan-group
7000     "N" gnus-summary-next-group
7001     "P" gnus-summary-prev-group)
7002
7003   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
7004     " " gnus-summary-next-page
7005     "n" gnus-summary-next-page
7006     "\177" gnus-summary-prev-page
7007     [delete] gnus-summary-prev-page
7008     "p" gnus-summary-prev-page
7009     "\r" gnus-summary-scroll-up
7010     "<" gnus-summary-beginning-of-article
7011     ">" gnus-summary-end-of-article
7012     "b" gnus-summary-beginning-of-article
7013     "e" gnus-summary-end-of-article
7014     "^" gnus-summary-refer-parent-article
7015     "r" gnus-summary-refer-parent-article
7016     "R" gnus-summary-refer-references
7017     "g" gnus-summary-show-article
7018     "s" gnus-summary-isearch-article)
7019
7020   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
7021     "b" gnus-article-add-buttons
7022     "B" gnus-article-add-buttons-to-head
7023     "o" gnus-article-treat-overstrike
7024     ;;  "w" gnus-article-word-wrap
7025     "w" gnus-article-fill-cited-article
7026     "c" gnus-article-remove-cr
7027     "L" gnus-article-remove-trailing-blank-lines
7028     "q" gnus-article-de-quoted-unreadable
7029     "f" gnus-article-display-x-face
7030     "l" gnus-summary-stop-page-breaking
7031     "r" gnus-summary-caesar-message
7032     "t" gnus-article-hide-headers
7033     "v" gnus-summary-verbose-headers
7034     "m" gnus-summary-toggle-mime)
7035
7036   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
7037     "a" gnus-article-hide
7038     "h" gnus-article-hide-headers
7039     "b" gnus-article-hide-boring-headers
7040     "s" gnus-article-hide-signature
7041     "c" gnus-article-hide-citation
7042     "p" gnus-article-hide-pgp
7043     "\C-c" gnus-article-hide-citation-maybe)
7044
7045   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
7046     "a" gnus-article-highlight
7047     "h" gnus-article-highlight-headers
7048     "c" gnus-article-highlight-citation
7049     "s" gnus-article-highlight-signature)
7050
7051   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
7052     "z" gnus-article-date-ut
7053     "u" gnus-article-date-ut
7054     "l" gnus-article-date-local
7055     "e" gnus-article-date-lapsed
7056     "o" gnus-article-date-original)
7057
7058   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7059     "v" gnus-version
7060     "f" gnus-summary-fetch-faq
7061     "d" gnus-summary-describe-group
7062     "h" gnus-summary-describe-briefly
7063     "i" gnus-info-find-node)
7064
7065   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7066     "e" gnus-summary-expire-articles
7067     "\M-\C-e" gnus-summary-expire-articles-now
7068     "\177" gnus-summary-delete-article
7069     [delete] gnus-summary-delete-article
7070     "m" gnus-summary-move-article
7071     "r" gnus-summary-respool-article
7072     "w" gnus-summary-edit-article
7073     "c" gnus-summary-copy-article
7074     "B" gnus-summary-crosspost-article
7075     "q" gnus-summary-respool-query
7076     "i" gnus-summary-import-article)
7077
7078   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7079     "o" gnus-summary-save-article
7080     "m" gnus-summary-save-article-mail
7081     "r" gnus-summary-save-article-rmail
7082     "f" gnus-summary-save-article-file
7083     "b" gnus-summary-save-article-body-file
7084     "h" gnus-summary-save-article-folder
7085     "v" gnus-summary-save-article-vm
7086     "p" gnus-summary-pipe-output
7087     "s" gnus-soup-add-article)
7088   )
7089
7090 \f
7091
7092 (defun gnus-summary-mode (&optional group)
7093   "Major mode for reading articles.
7094
7095 All normal editing commands are switched off.
7096 \\<gnus-summary-mode-map>
7097 Each line in this buffer represents one article.  To read an
7098 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7099 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7100 respectively.
7101
7102 You can also post articles and send mail from this buffer.  To
7103 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7104 of an article, type `\\[gnus-summary-reply]'.
7105
7106 There are approx. one gazillion commands you can execute in this
7107 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7108
7109 The following commands are available:
7110
7111 \\{gnus-summary-mode-map}"
7112   (interactive)
7113   (when (and menu-bar-mode
7114              (gnus-visual-p 'summary-menu 'menu))
7115     (gnus-summary-make-menu-bar))
7116   (kill-all-local-variables)
7117   (let ((locals gnus-summary-local-variables))
7118     (while locals
7119       (if (consp (car locals))
7120           (progn
7121             (make-local-variable (caar locals))
7122             (set (caar locals) (eval (cdar locals))))
7123         (make-local-variable (car locals))
7124         (set (car locals) nil))
7125       (setq locals (cdr locals))))
7126   (gnus-make-thread-indent-array)
7127   (gnus-simplify-mode-line)
7128   (setq major-mode 'gnus-summary-mode)
7129   (setq mode-name "Summary")
7130   (make-local-variable 'minor-mode-alist)
7131   (use-local-map gnus-summary-mode-map)
7132   (buffer-disable-undo (current-buffer))
7133   (setq buffer-read-only t)             ;Disable modification
7134   (setq truncate-lines t)
7135   (setq selective-display t)
7136   (setq selective-display-ellipses t)   ;Display `...'
7137   (setq buffer-display-table gnus-summary-display-table)
7138   (setq gnus-newsgroup-name group)
7139   (make-local-variable 'gnus-summary-line-format)
7140   (make-local-variable 'gnus-summary-line-format-spec)
7141   (make-local-variable 'gnus-summary-mark-positions)
7142   (run-hooks 'gnus-summary-mode-hook))
7143
7144 (defun gnus-summary-make-display-table ()
7145   ;; Change the display table.  Odd characters have a tendency to mess
7146   ;; up nicely formatted displays - we make all possible glyphs
7147   ;; display only a single character.
7148
7149   ;; We start from the standard display table, if any.
7150   (setq gnus-summary-display-table
7151         (or (copy-sequence standard-display-table)
7152             (make-display-table)))
7153   ;; Nix out all the control chars...
7154   (let ((i 32))
7155     (while (>= (setq i (1- i)) 0)
7156       (aset gnus-summary-display-table i [??])))
7157   ;; ... but not newline and cr, of course. (cr is necessary for the
7158   ;; selective display).
7159   (aset gnus-summary-display-table ?\n nil)
7160   (aset gnus-summary-display-table ?\r nil)
7161   ;; We nix out any glyphs over 126 that are not set already.
7162   (let ((i 256))
7163     (while (>= (setq i (1- i)) 127)
7164       ;; Only modify if the entry is nil.
7165       (or (aref gnus-summary-display-table i)
7166           (aset gnus-summary-display-table i [??])))))
7167
7168 (defun gnus-summary-clear-local-variables ()
7169   (let ((locals gnus-summary-local-variables))
7170     (while locals
7171       (if (consp (car locals))
7172           (and (vectorp (caar locals))
7173                (set (caar locals) nil))
7174         (and (vectorp (car locals))
7175              (set (car locals) nil)))
7176       (setq locals (cdr locals)))))
7177
7178 ;; Summary data functions.
7179
7180 (defmacro gnus-data-number (data)
7181   `(car ,data))
7182
7183 (defmacro gnus-data-set-number (data number)
7184   `(setcar ,data ,number))
7185
7186 (defmacro gnus-data-mark (data)
7187   `(nth 1 ,data))
7188
7189 (defmacro gnus-data-set-mark (data mark)
7190   `(setcar (nthcdr 1 ,data) ,mark))
7191
7192 (defmacro gnus-data-pos (data)
7193   `(nth 2 ,data))
7194
7195 (defmacro gnus-data-set-pos (data pos)
7196   `(setcar (nthcdr 2 ,data) ,pos))
7197
7198 (defmacro gnus-data-header (data)
7199   `(nth 3 ,data))
7200
7201 (defmacro gnus-data-level (data)
7202   `(nth 4 ,data))
7203
7204 (defmacro gnus-data-unread-p (data)
7205   `(= (nth 1 ,data) gnus-unread-mark))
7206
7207 (defmacro gnus-data-pseudo-p (data)
7208   `(consp (nth 3 ,data)))
7209
7210 (defmacro gnus-data-find (number)
7211   `(assq ,number gnus-newsgroup-data))
7212
7213 (defmacro gnus-data-find-list (number &optional data)
7214   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7215      (memq (assq ,number bdata)
7216            bdata)))
7217
7218 (defmacro gnus-data-make (number mark pos header level)
7219   `(list ,number ,mark ,pos ,header ,level))
7220
7221 (defun gnus-data-enter (after-article number mark pos header level offset)
7222   (let ((data (gnus-data-find-list after-article)))
7223     (or data (error "No such article: %d" after-article))
7224     (setcdr data (cons (gnus-data-make number mark pos header level)
7225                        (cdr data)))
7226     (setq gnus-newsgroup-data-reverse nil)
7227     (gnus-data-update-list (cddr data) offset)))
7228
7229 (defun gnus-data-enter-list (after-article list &optional offset)
7230   (when list
7231     (let ((data (and after-article (gnus-data-find-list after-article)))
7232           (ilist list))
7233       (or data (not after-article) (error "No such article: %d" after-article))
7234       ;; Find the last element in the list to be spliced into the main
7235       ;; list.
7236       (while (cdr list)
7237         (setq list (cdr list)))
7238       (if (not data)
7239           (progn
7240             (setcdr list gnus-newsgroup-data)
7241             (setq gnus-newsgroup-data ilist)
7242             (and offset (gnus-data-update-list (cdr list) offset)))
7243         (setcdr list (cdr data))
7244         (setcdr data ilist)
7245         (and offset (gnus-data-update-list (cdr data) offset)))
7246       (setq gnus-newsgroup-data-reverse nil))))
7247
7248 (defun gnus-data-remove (article &optional offset)
7249   (let ((data gnus-newsgroup-data))
7250     (if (= (gnus-data-number (car data)) article)
7251         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7252               gnus-newsgroup-data-reverse nil)
7253       (while (cdr data)
7254         (and (= (gnus-data-number (cadr data)) article)
7255              (progn
7256                (setcdr data (cddr data))
7257                (and offset (gnus-data-update-list (cdr data) offset))
7258                (setq data nil
7259                      gnus-newsgroup-data-reverse nil)))
7260         (setq data (cdr data))))))
7261
7262 (defmacro gnus-data-list (backward)
7263   `(if ,backward
7264        (or gnus-newsgroup-data-reverse
7265            (setq gnus-newsgroup-data-reverse
7266                  (reverse gnus-newsgroup-data)))
7267      gnus-newsgroup-data))
7268
7269 (defun gnus-data-update-list (data offset)
7270   "Add OFFSET to the POS of all data entries in DATA."
7271   (while data
7272     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7273     (setq data (cdr data))))
7274
7275 (defun gnus-data-compute-positions ()
7276   "Compute the positions of all articles."
7277   (let ((data gnus-newsgroup-data)
7278         pos)
7279     (while data
7280       (when (setq pos (text-property-any
7281                        (point-min) (point-max)
7282                        'gnus-number (gnus-data-number (car data))))
7283         (gnus-data-set-pos (car data) (+ pos 3)))
7284       (setq data (cdr data)))))
7285
7286 (defun gnus-summary-article-pseudo-p (article)
7287   "Say whether this article is a pseudo article or not."
7288   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7289
7290 (defun gnus-article-parent-p (number)
7291   "Say whether this article is a parent or not."
7292   (let ((data (gnus-data-find-list number)))
7293     (and (cdr data)                     ; There has to be an article after...
7294          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7295             (gnus-data-level (nth 1 data))))))
7296
7297 (defun gnus-article-children (number)
7298   "Return a list of all children to NUMBER."
7299   (let* ((data (gnus-data-find-list number))
7300          (level (gnus-data-level (car data)))
7301          children)
7302     (setq data (cdr data))
7303     (while (and data            
7304                 (= (gnus-data-level (car data)) (1+ level)))
7305       (push (gnus-data-number (car data)) children)
7306       (setq data (cdr data)))
7307     children))
7308
7309 (defmacro gnus-summary-skip-intangible ()
7310   "If the current article is intangible, then jump to a different article."
7311   '(let ((to (get-text-property (point) 'gnus-intangible)))
7312     (and to (gnus-summary-goto-subject to))))
7313
7314 (defmacro gnus-summary-article-intangible-p ()
7315   "Say whether this article is intangible or not."
7316   '(get-text-property (point) 'gnus-intangible))
7317
7318 ;; Some summary mode macros.
7319
7320 (defmacro gnus-summary-article-number ()
7321   "The article number of the article on the current line.
7322 If there isn's an article number here, then we return the current
7323 article number."
7324   '(progn
7325      (gnus-summary-skip-intangible)
7326      (or (get-text-property (point) 'gnus-number)
7327          (gnus-summary-last-subject))))
7328
7329 (defmacro gnus-summary-article-header (&optional number)
7330   `(gnus-data-header (gnus-data-find
7331                       ,(or number '(gnus-summary-article-number)))))
7332
7333 (defmacro gnus-summary-thread-level (&optional number)
7334   `(if (and (eq gnus-summary-make-false-root 'dummy)
7335             (get-text-property (point) 'gnus-intangible))
7336        0
7337      (gnus-data-level (gnus-data-find
7338                        ,(or number '(gnus-summary-article-number))))))
7339
7340 (defmacro gnus-summary-article-mark (&optional number)
7341   `(gnus-data-mark (gnus-data-find
7342                     ,(or number '(gnus-summary-article-number)))))
7343
7344 (defmacro gnus-summary-article-pos (&optional number)
7345   `(gnus-data-pos (gnus-data-find
7346                    ,(or number '(gnus-summary-article-number)))))
7347
7348 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7349 (defmacro gnus-summary-article-subject (&optional number)
7350   "Return current subject string or nil if nothing."
7351   `(let ((headers
7352           ,(if number
7353                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7354              '(gnus-data-header (assq (gnus-summary-article-number)
7355                                       gnus-newsgroup-data)))))
7356      (and headers
7357           (vectorp headers)
7358           (mail-header-subject headers))))
7359
7360 (defmacro gnus-summary-article-score (&optional number)
7361   "Return current article score."
7362   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7363                   gnus-newsgroup-scored))
7364        gnus-summary-default-score 0))
7365
7366 (defun gnus-summary-article-children (&optional number)
7367   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7368          (level (gnus-data-level (car data)))
7369          l children)
7370     (while (and (setq data (cdr data))
7371                 (> (setq l (gnus-data-level (car data))) level))
7372       (and (= (1+ level) l)
7373            (setq children (cons (gnus-data-number (car data))
7374                                 children))))
7375     (nreverse children)))
7376
7377 (defun gnus-summary-article-parent (&optional number)
7378   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7379                                     (gnus-data-list t)))
7380          (level (gnus-data-level (car data))))
7381     (if (zerop level)
7382         () ; This is a root.
7383       ;; We search until we find an article with a level less than
7384       ;; this one.  That function has to be the parent.
7385       (while (and (setq data (cdr data))
7386                   (not (< (gnus-data-level (car data)) level))))
7387       (and data (gnus-data-number (car data))))))
7388
7389 (defun gnus-unread-mark-p (mark)
7390   "Say whether MARK is the unread mark."
7391   (= mark gnus-unread-mark))
7392
7393 (defun gnus-read-mark-p (mark)
7394   "Say whether MARK is one of the marks that mark as read.
7395 This is all marks except unread, ticked, dormant, and expirable."
7396   (not (or (= mark gnus-unread-mark)
7397            (= mark gnus-ticked-mark)
7398            (= mark gnus-dormant-mark)
7399            (= mark gnus-expirable-mark))))
7400
7401 ;; Saving hidden threads.
7402
7403 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
7404 (put 'gnus-save-hidden-threads 'lisp-indent-hook 0)
7405
7406 (defmacro gnus-save-hidden-threads (&rest forms)
7407   "Save hidden threads, eval FORMS, and restore the hidden threads."
7408   (let ((config (make-symbol "config")))
7409     `(let ((,config (gnus-hidden-threads-configuration)))
7410        (unwind-protect
7411            (progn
7412              ,@forms)
7413          (gnus-restore-hidden-threads-configuration ,config)))))
7414
7415 (defun gnus-hidden-threads-configuration ()
7416   "Return the current hidden threads configuration."
7417   (save-excursion
7418     (let (config)
7419       (goto-char (point-min))
7420       (while (search-forward "\r" nil t)
7421         (push (1- (point)) config))
7422       config)))
7423
7424 (defun gnus-restore-hidden-threads-configuration (config)
7425   "Restore hidden threads configuration from CONFIG."
7426   (let (point buffer-read-only)
7427     (while (setq point (pop config))
7428       (when (and (< point (point-max))
7429                  (goto-char point)
7430                  (= (following-char) ?\n))
7431         (subst-char-in-region point (1+ point) ?\n ?\r)))))
7432
7433 ;; Various summary mode internalish functions.
7434
7435 (defun gnus-mouse-pick-article (e)
7436   (interactive "e")
7437   (mouse-set-point e)
7438   (gnus-summary-next-page nil t))
7439
7440 (defun gnus-summary-setup-buffer (group)
7441   "Initialize summary buffer."
7442   (let ((buffer (concat "*Summary " group "*")))
7443     (if (get-buffer buffer)
7444         (progn
7445           (set-buffer buffer)
7446           (setq gnus-summary-buffer (current-buffer))
7447           (not gnus-newsgroup-prepared))
7448       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7449       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7450       (gnus-add-current-to-buffer-list)
7451       (gnus-summary-mode group)
7452       (when gnus-carpal
7453         (gnus-carpal-setup-buffer 'summary))
7454       (unless gnus-single-article-buffer
7455         (make-local-variable 'gnus-article-buffer)
7456         (make-local-variable 'gnus-article-current)
7457         (make-local-variable 'gnus-original-article-buffer))
7458       (setq gnus-newsgroup-name group)
7459       t)))
7460
7461 (defun gnus-set-global-variables ()
7462   ;; Set the global equivalents of the summary buffer-local variables
7463   ;; to the latest values they had.  These reflect the summary buffer
7464   ;; that was in action when the last article was fetched.
7465   (when (eq major-mode 'gnus-summary-mode)
7466     (setq gnus-summary-buffer (current-buffer))
7467     (let ((name gnus-newsgroup-name)
7468           (marked gnus-newsgroup-marked)
7469           (unread gnus-newsgroup-unreads)
7470           (headers gnus-current-headers)
7471           (data gnus-newsgroup-data)
7472           (summary gnus-summary-buffer)
7473           (article-buffer gnus-article-buffer)
7474           (original gnus-original-article-buffer)
7475           (gac gnus-article-current)
7476           (score-file gnus-current-score-file))
7477       (save-excursion
7478         (set-buffer gnus-group-buffer)
7479         (setq gnus-newsgroup-name name)
7480         (setq gnus-newsgroup-marked marked)
7481         (setq gnus-newsgroup-unreads unread)
7482         (setq gnus-current-headers headers)
7483         (setq gnus-newsgroup-data data)
7484         (setq gnus-article-current gac)
7485         (setq gnus-summary-buffer summary)
7486         (setq gnus-article-buffer article-buffer)
7487         (setq gnus-original-article-buffer original)
7488         (setq gnus-current-score-file score-file)))))
7489
7490 (defun gnus-summary-last-article-p (&optional article)
7491   "Return whether ARTICLE is the last article in the buffer."
7492   (if (not (setq article (or article (gnus-summary-article-number))))
7493       t ; All non-existant numbers are the last article. :-)
7494     (not (cdr (gnus-data-find-list article)))))
7495
7496 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7497   "Insert a dummy root in the summary buffer."
7498   (beginning-of-line)
7499   (gnus-add-text-properties
7500    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7501    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7502
7503 (defun gnus-make-thread-indent-array ()
7504   (let ((n 200))
7505     (unless (and gnus-thread-indent-array
7506                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7507       (setq gnus-thread-indent-array (make-vector 201 "")
7508             gnus-thread-indent-array-level gnus-thread-indent-level)
7509       (while (>= n 0)
7510         (aset gnus-thread-indent-array n
7511               (make-string (* n gnus-thread-indent-level) ? ))
7512         (setq n (1- n))))))
7513
7514 (defun gnus-summary-insert-line
7515   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7516                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7517                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7518   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7519          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7520          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7521          (gnus-tmp-score-char
7522           (if (or (null gnus-summary-default-score)
7523                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7524                       gnus-summary-zcore-fuzz)) ? 
7525             (if (< gnus-tmp-score gnus-summary-default-score)
7526                 gnus-score-below-mark gnus-score-over-mark)))
7527          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7528                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7529                                   gnus-cached-mark)
7530                                  (gnus-tmp-replied gnus-replied-mark)
7531                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7532                                   gnus-saved-mark)
7533                                  (t gnus-unread-mark)))
7534          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7535          (gnus-tmp-name
7536           (cond
7537            ((string-match "(.+)" gnus-tmp-from)
7538             (substring gnus-tmp-from
7539                        (1+ (match-beginning 0)) (1- (match-end 0))))
7540            ((string-match "<[^>]+> *$" gnus-tmp-from)
7541             (let ((beg (match-beginning 0)))
7542               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7543                        (substring gnus-tmp-from (1+ (match-beginning 0))
7544                                   (1- (match-end 0))))
7545                   (substring gnus-tmp-from 0 beg))))
7546            (t gnus-tmp-from)))
7547          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7548          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7549          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7550          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7551          (buffer-read-only nil))
7552     (when (string= gnus-tmp-name "")
7553       (setq gnus-tmp-name gnus-tmp-from))
7554     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7555     (gnus-put-text-property
7556      (point)
7557      (progn (eval gnus-summary-line-format-spec) (point))
7558      'gnus-number gnus-tmp-number)
7559     (when (gnus-visual-p 'summary-highlight 'highlight)
7560       (forward-line -1)
7561       (run-hooks 'gnus-summary-update-hook)
7562       (forward-line 1))))
7563
7564 (defun gnus-summary-update-line (&optional dont-update)
7565   ;; Update summary line after change.
7566   (when (and gnus-summary-default-score
7567              (not gnus-summary-inhibit-highlight))
7568     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7569            (article (gnus-summary-article-number))
7570            (score (gnus-summary-article-score article)))
7571       (unless dont-update
7572         (if (and gnus-summary-mark-below
7573                  (< (gnus-summary-article-score)
7574                     gnus-summary-mark-below))
7575             ;; This article has a low score, so we mark it as read.
7576             (when (memq article gnus-newsgroup-unreads)
7577               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7578           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7579             ;; This article was previously marked as read on account
7580             ;; of a low score, but now it has risen, so we mark it as
7581             ;; unread.
7582             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7583         (gnus-summary-update-mark
7584          (if (or (null gnus-summary-default-score)
7585                  (<= (abs (- score gnus-summary-default-score))
7586                      gnus-summary-zcore-fuzz)) ? 
7587            (if (< score gnus-summary-default-score)
7588                gnus-score-below-mark gnus-score-over-mark)) 'score))
7589       ;; Do visual highlighting.
7590       (when (gnus-visual-p 'summary-highlight 'highlight)
7591         (run-hooks 'gnus-summary-update-hook)))))
7592
7593 (defvar gnus-tmp-new-adopts nil)
7594
7595 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7596   ;; Sum up all elements (and sub-elements) in a list.
7597   (let* ((number
7598           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7599           (cond
7600            ((and (consp thread) (cdr thread))
7601             (apply
7602              '+ 1 (mapcar
7603                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7604            ((null thread)
7605             1)
7606            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7607             1)
7608            (t 1))))
7609     (when (and level (zerop level) gnus-tmp-new-adopts)
7610       (incf number
7611             (apply '+ (mapcar
7612                        'gnus-summary-number-of-articles-in-thread
7613                        gnus-tmp-new-adopts))))
7614     (if char
7615         (if (> number 1) gnus-not-empty-thread-mark
7616           gnus-empty-thread-mark)
7617       number)))
7618
7619 (defun gnus-summary-set-local-parameters (group)
7620  "Go through the local params of GROUP and set all variable specs in that list."
7621   (let ((params (gnus-info-params (gnus-get-info group)))
7622         elem)
7623     (while params
7624       (setq elem (car params)
7625             params (cdr params))
7626       (and (consp elem)                 ; Has to be a cons.
7627            (consp (cdr elem))           ; The cdr has to be a list.
7628            (symbolp (car elem))         ; Has to be a symbol in there.
7629            (not (memq (car elem) 
7630                       '(quit-config to-address to-list to-group)))
7631            (progn                       ; So we set it.
7632              (make-local-variable (car elem))
7633              (set (car elem) (eval (nth 1 elem))))))))
7634
7635 (defun gnus-summary-read-group (group &optional show-all no-article
7636                                       kill-buffer no-display)
7637   "Start reading news in newsgroup GROUP.
7638 If SHOW-ALL is non-nil, already read articles are also listed.
7639 If NO-ARTICLE is non-nil, no article is selected initially.
7640 If NO-DISPLAY, don't generate a summary buffer."
7641   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7642   (let* ((new-group (gnus-summary-setup-buffer group))
7643          (quit-config (gnus-group-quit-config group))
7644          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7645     (cond
7646      ;; This summary buffer exists already, so we just select it.
7647      ((not new-group)
7648       (gnus-set-global-variables)
7649       (when kill-buffer
7650         (gnus-kill-or-deaden-summary kill-buffer))
7651       (gnus-configure-windows 'summary 'force)
7652       (gnus-set-mode-line 'summary)
7653       (gnus-summary-position-point)
7654       (message "")
7655       t)
7656      ;; We couldn't select this group.
7657      ((null did-select)
7658       (when (and (eq major-mode 'gnus-summary-mode)
7659                  (not (equal (current-buffer) kill-buffer)))
7660         (kill-buffer (current-buffer))
7661         (if (not quit-config)
7662             (progn
7663               (set-buffer gnus-group-buffer)
7664               (gnus-group-jump-to-group group)
7665               (gnus-group-next-unread-group 1))
7666           (if (not (buffer-name (car quit-config)))
7667               (gnus-configure-windows 'group 'force)
7668             (set-buffer (car quit-config))
7669             (and (eq major-mode 'gnus-summary-mode)
7670                  (gnus-set-global-variables))
7671             (gnus-configure-windows (cdr quit-config)))))
7672       (gnus-message 3 "Can't select group")
7673       nil)
7674      ;; The user did a `C-g' while prompting for number of articles,
7675      ;; so we exit this group.
7676      ((eq did-select 'quit)
7677       (and (eq major-mode 'gnus-summary-mode)
7678            (not (equal (current-buffer) kill-buffer))
7679            (kill-buffer (current-buffer)))
7680       (when kill-buffer
7681         (gnus-kill-or-deaden-summary kill-buffer))
7682       (if (not quit-config)
7683           (progn
7684             (set-buffer gnus-group-buffer)
7685             (gnus-group-jump-to-group group)
7686             (gnus-group-next-unread-group 1)
7687             (gnus-configure-windows 'group 'force))
7688         (if (not (buffer-name (car quit-config)))
7689             (gnus-configure-windows 'group 'force)
7690           (set-buffer (car quit-config))
7691           (and (eq major-mode 'gnus-summary-mode)
7692                (gnus-set-global-variables))
7693           (gnus-configure-windows (cdr quit-config))))
7694       ;; Finally signal the quit.
7695       (signal 'quit nil))
7696      ;; The group was successfully selected.
7697      (t
7698       (gnus-set-global-variables)
7699       ;; Save the active value in effect when the group was entered.
7700       (setq gnus-newsgroup-active
7701             (gnus-copy-sequence
7702              (gnus-active gnus-newsgroup-name)))
7703       ;; You can change the summary buffer in some way with this hook.
7704       (run-hooks 'gnus-select-group-hook)
7705       ;; Set any local variables in the group parameters.
7706       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7707       (gnus-update-format-specifications)
7708       ;; Do score processing.
7709       (when gnus-use-scoring
7710         (gnus-possibly-score-headers))
7711       ;; Check whether to fill in the gaps in the threads.
7712       (when gnus-build-sparse-threads
7713         (gnus-build-sparse-threads))
7714       ;; Find the initial limit.
7715       (if gnus-show-threads
7716           (if show-all
7717               (let ((gnus-newsgroup-dormant nil))
7718                 (gnus-summary-initial-limit show-all))
7719             (gnus-summary-initial-limit show-all))
7720         (setq gnus-newsgroup-limit 
7721               (mapcar 
7722                (lambda (header) (mail-header-number header))
7723                gnus-newsgroup-headers)))
7724       ;; Generate the summary buffer.
7725       (unless no-display
7726         (gnus-summary-prepare))
7727       (when gnus-use-trees
7728         (gnus-tree-open group)
7729         (setq gnus-summary-highlight-line-function
7730               'gnus-tree-highlight-article))
7731       ;; If the summary buffer is empty, but there are some low-scored
7732       ;; articles or some excluded dormants, we include these in the
7733       ;; buffer.
7734       (when (and (zerop (buffer-size))
7735                  (not no-display))
7736         (cond (gnus-newsgroup-dormant
7737                (gnus-summary-limit-include-dormant))
7738               ((and gnus-newsgroup-scored show-all)
7739                (gnus-summary-limit-include-expunged))))
7740       ;; Function `gnus-apply-kill-file' must be called in this hook.
7741       (run-hooks 'gnus-apply-kill-hook)
7742       (if (and (zerop (buffer-size))
7743                (not no-display))
7744           (progn
7745             ;; This newsgroup is empty.
7746             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7747             (gnus-message 6 "No unread news")
7748             (when kill-buffer
7749               (gnus-kill-or-deaden-summary kill-buffer))
7750             ;; Return nil from this function.
7751             nil)
7752         ;; Hide conversation thread subtrees.  We cannot do this in
7753         ;; gnus-summary-prepare-hook since kill processing may not
7754         ;; work with hidden articles.
7755         (and gnus-show-threads
7756              gnus-thread-hide-subtree
7757              (gnus-summary-hide-all-threads))
7758         ;; Show first unread article if requested.
7759         (if (and (not no-article)
7760                  (not no-display)
7761                  gnus-newsgroup-unreads
7762                  gnus-auto-select-first)
7763             (unless (if (eq gnus-auto-select-first 'best)
7764                         (gnus-summary-best-unread-article)
7765                       (gnus-summary-first-unread-article))
7766               (gnus-configure-windows 'summary))
7767           ;; Don't select any articles, just move point to the first
7768           ;; article in the group.
7769           (goto-char (point-min))
7770           (gnus-summary-position-point)
7771           (gnus-set-mode-line 'summary)
7772           (gnus-configure-windows 'summary 'force))
7773         ;; If we are in async mode, we send some info to the backend.
7774         (when gnus-newsgroup-async
7775           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7776         (when kill-buffer
7777           (gnus-kill-or-deaden-summary kill-buffer))
7778         (when (get-buffer-window gnus-group-buffer t)
7779           ;; Gotta use windows, because recenter does wierd stuff if
7780           ;; the current buffer ain't the displayed window.
7781           (let ((owin (selected-window)))
7782             (select-window (get-buffer-window gnus-group-buffer t))
7783             (when (gnus-group-goto-group group)
7784               (recenter))
7785             (select-window owin))))
7786       ;; Mark this buffer as "prepared".
7787       (setq gnus-newsgroup-prepared t)
7788       t))))
7789
7790 (defun gnus-summary-prepare ()
7791   "Generate the summary buffer."
7792   (let ((buffer-read-only nil))
7793     (erase-buffer)
7794     (setq gnus-newsgroup-data nil
7795           gnus-newsgroup-data-reverse nil)
7796     (run-hooks 'gnus-summary-generate-hook)
7797     ;; Generate the buffer, either with threads or without.
7798     (when gnus-newsgroup-headers
7799       (gnus-summary-prepare-threads
7800        (if gnus-show-threads
7801            (gnus-sort-gathered-threads
7802             (funcall gnus-summary-thread-gathering-function
7803                      (gnus-sort-threads
7804                       (gnus-cut-threads (gnus-make-threads)))))
7805          ;; Unthreaded display.
7806          (gnus-sort-articles gnus-newsgroup-headers))))
7807     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7808     ;; Call hooks for modifying summary buffer.
7809     (goto-char (point-min))
7810     (run-hooks 'gnus-summary-prepare-hook)))
7811
7812 (defun gnus-gather-threads-by-subject (threads)
7813   "Gather threads by looking at Subject headers."
7814   (if (not gnus-summary-make-false-root)
7815       threads
7816     (let ((hashtb (gnus-make-hashtable 1023))
7817           (prev threads)
7818           (result threads)
7819           subject hthread whole-subject)
7820       (while threads
7821         (setq whole-subject (mail-header-subject (caar threads)))
7822         (setq subject
7823               (cond
7824                ;; Truncate the subject.
7825                ((numberp gnus-summary-gather-subject-limit)
7826                 (setq subject (gnus-simplify-subject-re whole-subject))
7827                 (if (> (length subject) gnus-summary-gather-subject-limit)
7828                     (substring subject 0 gnus-summary-gather-subject-limit)
7829                   subject))
7830                ;; Fuzzily simplify it.
7831                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7832                 (gnus-simplify-subject-fuzzy whole-subject))
7833                ;; Just remove the leading "Re:".
7834                (t
7835                 (gnus-simplify-subject-re whole-subject))))
7836
7837         (if (and gnus-summary-gather-exclude-subject
7838                  (string-match gnus-summary-gather-exclude-subject
7839                                subject))
7840             ()          ; We don't want to do anything with this article.
7841           ;; We simplify the subject before looking it up in the
7842           ;; hash table.
7843
7844           (if (setq hthread (gnus-gethash subject hashtb))
7845               (progn
7846                 ;; We enter a dummy root into the thread, if we
7847                 ;; haven't done that already.
7848                 (unless (stringp (caar hthread))
7849                   (setcar hthread (list whole-subject (car hthread))))
7850                 ;; We add this new gathered thread to this gathered
7851                 ;; thread.
7852                 (setcdr (car hthread)
7853                         (nconc (cdar hthread) (list (car threads))))
7854                 ;; Remove it from the list of threads.
7855                 (setcdr prev (cdr threads))
7856                 (setq threads prev))
7857             ;; Enter this thread into the hash table.
7858             (gnus-sethash subject threads hashtb)))
7859         (setq prev threads)
7860         (setq threads (cdr threads)))
7861       result)))
7862
7863 (defun gnus-gather-threads-by-references (threads)
7864   "Gather threads by looking at References headers."
7865   (let ((idhashtb (gnus-make-hashtable 1023))
7866         (thhashtb (gnus-make-hashtable 1023))
7867         (prev threads)
7868         (result threads)
7869         ids references id gthread gid entered)
7870     (while threads
7871       (when (setq references (mail-header-references (caar threads)))
7872         (setq id (mail-header-id (caar threads)))
7873         (setq ids (gnus-split-references references))
7874         (setq entered nil)
7875         (while ids
7876           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7877               (progn
7878                 (gnus-sethash (car ids) id idhashtb)
7879                 (gnus-sethash id threads thhashtb))
7880             (setq gthread (gnus-gethash gid thhashtb))
7881             (unless entered
7882               ;; We enter a dummy root into the thread, if we
7883               ;; haven't done that already.
7884               (unless (stringp (caar gthread))
7885                 (setcar gthread (list (mail-header-subject (caar gthread))
7886                                       (car gthread))))
7887               ;; We add this new gathered thread to this gathered
7888               ;; thread.
7889               (setcdr (car gthread)
7890                       (nconc (cdar gthread) (list (car threads)))))
7891             ;; Add it into the thread hash table.
7892             (gnus-sethash id gthread thhashtb)
7893             (setq entered t)
7894             ;; Remove it from the list of threads.
7895             (setcdr prev (cdr threads))
7896             (setq threads prev))
7897           (setq ids (cdr ids))))
7898       (setq prev threads)
7899       (setq threads (cdr threads)))
7900     result))
7901
7902 (defun gnus-sort-gathered-threads (threads)
7903   "Sort subtreads inside each gathered thread by article number."
7904   (let ((result threads))
7905     (while threads
7906       (when (stringp (caar threads))
7907         (setcdr (car threads)
7908                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7909       (setq threads (cdr threads)))
7910     result))
7911
7912 (defun gnus-make-threads ()
7913   "Go through the dependency hashtb and find the roots.  Return all threads."
7914   (let (threads)
7915     (mapatoms
7916      (lambda (refs)
7917        (unless (car (symbol-value refs))
7918          ;; These threads do not refer back to any other articles,
7919          ;; so they're roots.
7920          (setq threads (append (cdr (symbol-value refs)) threads))))
7921      gnus-newsgroup-dependencies)
7922     threads))
7923
7924 (defun gnus-build-sparse-threads ()
7925   (let ((headers gnus-newsgroup-headers)
7926         (deps gnus-newsgroup-dependencies)
7927         header references generation relations 
7928         cthread subject child end pthread relation)
7929     ;; First we create an alist of generations/relations, where 
7930     ;; generations is how much we trust the ralation, and the relation
7931     ;; is parent/child.
7932     (gnus-message 7 "Making sparse threads...")
7933     (save-excursion
7934       (nnheader-set-temp-buffer " *gnus sparse threads*")
7935       (while (setq header (pop headers))
7936         (when (and (setq references (mail-header-references header))
7937                    (not (string= references "")))
7938           (insert references)
7939           (setq child (mail-header-id header)
7940                 subject (mail-header-subject header))
7941           (setq generation 0)
7942           (while (search-backward ">" nil t)
7943             (setq end (1+ (point)))
7944             (when (search-backward "<" nil t)
7945               (push (list (incf generation) 
7946                           child (setq child (buffer-substring (point) end))
7947                           subject)
7948                     relations)))
7949           (push (list (1+ generation) child nil subject) relations)
7950           (erase-buffer)))
7951       (kill-buffer (current-buffer)))
7952     ;; Sort over trustworthiness.
7953     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7954     (while (setq relation (pop relations))
7955       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7956                 (unless (car (symbol-value cthread))
7957                   ;; Make this article the parent of these threads.
7958                   (setcar (symbol-value cthread)
7959                           (vector gnus-reffed-article-number 
7960                                   (cadddr relation) 
7961                                   "" ""
7962                                   (cadr relation) 
7963                                   (or (caddr relation) "") 0 0 "")))
7964               (set cthread (list (vector gnus-reffed-article-number
7965                                          (cadddr relation) 
7966                                          "" "" (cadr relation) 
7967                                          (or (caddr relation) "") 0 0 ""))))
7968         (push gnus-reffed-article-number gnus-newsgroup-limit)
7969         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7970         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7971               gnus-newsgroup-reads)
7972         (decf gnus-reffed-article-number)
7973         ;; Make this new thread the child of its parent.
7974         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7975             (setcdr (symbol-value pthread)
7976                     (nconc (cdr (symbol-value pthread))
7977                            (list (symbol-value cthread))))
7978           (set pthread (list nil (symbol-value cthread))))))
7979     (gnus-message 7 "Making sparse threads...done")))
7980
7981 (defun gnus-build-old-threads ()
7982   ;; Look at all the articles that refer back to old articles, and
7983   ;; fetch the headers for the articles that aren't there.  This will
7984   ;; build complete threads - if the roots haven't been expired by the
7985   ;; server, that is.
7986   (let (id heads)
7987     (mapatoms
7988      (lambda (refs)
7989        (when (not (car (symbol-value refs)))
7990          (setq heads (cdr (symbol-value refs)))
7991          (while heads
7992            (if (memq (mail-header-number (caar heads))
7993                      gnus-newsgroup-dormant)
7994                (setq heads (cdr heads))
7995              (setq id (symbol-name refs))
7996              (while (and (setq id (gnus-build-get-header id))
7997                          (not (car (gnus-gethash
7998                                     id gnus-newsgroup-dependencies)))))
7999              (setq heads nil)))))
8000      gnus-newsgroup-dependencies)))
8001
8002 (defun gnus-build-get-header (id)
8003   ;; Look through the buffer of NOV lines and find the header to
8004   ;; ID.  Enter this line into the dependencies hash table, and return
8005   ;; the id of the parent article (if any).
8006   (let ((deps gnus-newsgroup-dependencies)
8007         found header)
8008     (prog1
8009         (save-excursion
8010           (set-buffer nntp-server-buffer)
8011           (goto-char (point-min))
8012           (while (and (not found) (search-forward id nil t))
8013             (beginning-of-line)
8014             (setq found (looking-at
8015                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
8016                                  (regexp-quote id))))
8017             (or found (beginning-of-line 2)))
8018           (when found
8019             (beginning-of-line)
8020             (and
8021              (setq header (gnus-nov-parse-line
8022                            (read (current-buffer)) deps))
8023              (gnus-parent-id (mail-header-references header)))))
8024       (when header
8025         (let ((number (mail-header-number header)))
8026           (push number gnus-newsgroup-limit)
8027           (push header gnus-newsgroup-headers)
8028           (if (memq number gnus-newsgroup-unselected)
8029               (progn
8030                 (push number gnus-newsgroup-unreads)
8031                 (setq gnus-newsgroup-unselected
8032                       (delq number gnus-newsgroup-unselected)))
8033             (push number gnus-newsgroup-ancient)))))))
8034
8035 (defun gnus-summary-update-article (article &optional header)
8036   "Update ARTICLE in the summary buffer."
8037   (set-buffer gnus-summary-buffer)
8038   (let* ((header (or header (gnus-summary-article-header article)))
8039          (id (mail-header-id header))
8040          (data (gnus-data-find article))
8041          (thread (gnus-id-to-thread id))
8042          (parent
8043           (gnus-id-to-thread (or (gnus-parent-id 
8044                                   (mail-header-references header))
8045                                  "tull")))
8046          (buffer-read-only nil)
8047          (old (car thread))
8048          (number (mail-header-number header))
8049          pos)
8050     (when thread
8051       (setcar thread nil)
8052       (when parent
8053         (delq thread parent))
8054       (if (gnus-summary-insert-subject id header)
8055           ;; Set the (possibly) new article number in the data structure.
8056           (gnus-data-set-number data (gnus-id-to-article id))
8057         (setcar thread old)
8058         nil))))
8059
8060 (defun gnus-rebuild-thread (id)
8061   "Rebuild the thread containing ID."
8062   (let ((buffer-read-only nil)
8063         current thread data)
8064     (if (not gnus-show-threads)
8065         (setq thread (list (car (gnus-id-to-thread id))))
8066       ;; Get the thread this article is part of.
8067       (setq thread (gnus-remove-thread id)))
8068     (setq current (save-excursion
8069                     (and (zerop (forward-line -1))
8070                          (gnus-summary-article-number))))
8071     ;; If this is a gathered thread, we have to go some re-gathering.
8072     (when (stringp (car thread))
8073       (let ((subject (car thread))
8074             roots thr)
8075         (setq thread (cdr thread))
8076         (while thread
8077           (unless (memq (setq thr (gnus-id-to-thread
8078                                       (gnus-root-id
8079                                        (mail-header-id (caar thread)))))
8080                         roots)
8081             (push thr roots))
8082           (setq thread (cdr thread)))
8083         ;; We now have all (unique) roots.
8084         (if (= (length roots) 1)
8085             ;; All the loose roots are now one solid root.
8086             (setq thread (car roots))
8087           (setq thread (cons subject (gnus-sort-threads roots))))))
8088     (let (threads)
8089       ;; We then insert this thread into the summary buffer.
8090       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8091         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8092         (setq data (nreverse gnus-newsgroup-data))
8093         (setq threads gnus-newsgroup-threads))
8094       ;; We splice the new data into the data structure.
8095       (gnus-data-enter-list current data)
8096       (gnus-data-compute-positions)
8097       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8098
8099 (defun gnus-id-to-thread (id)
8100   "Return the (sub-)thread where ID appears."
8101   (gnus-gethash id gnus-newsgroup-dependencies))
8102
8103 (defun gnus-id-to-article (id)
8104   "Return the article number of ID."
8105   (let ((thread (gnus-id-to-thread id)))
8106     (when (and thread
8107                (car thread))
8108       (mail-header-number (car thread)))))
8109
8110 (defun gnus-id-to-header (id)
8111   "Return the article headers of ID."
8112   (car (gnus-id-to-thread id)))
8113
8114 (defun gnus-article-displayed-root-p (article)
8115   "Say whether ARTICLE is a root(ish) article."
8116   (let ((level (gnus-summary-thread-level article))
8117         (refs (mail-header-references  (gnus-summary-article-header article)))
8118         particle)
8119     (cond 
8120      ((null level) nil)
8121      ((zerop level) t)
8122      ((null refs) t)
8123      ((null (gnus-parent-id refs)) t)
8124      ((and (= 1 level)
8125            (null (setq particle (gnus-id-to-article
8126                                  (gnus-parent-id refs))))
8127            (null (gnus-summary-thread-level particle)))))))
8128
8129 (defun gnus-root-id (id)
8130   "Return the id of the root of the thread where ID appears."
8131   (let (last-id prev)
8132     (while (and id (setq prev (car (gnus-gethash 
8133                                     id gnus-newsgroup-dependencies))))
8134       (setq last-id id
8135             id (gnus-parent-id (mail-header-references prev))))
8136     last-id))
8137
8138 (defun gnus-remove-thread (id &optional dont-remove)
8139   "Remove the thread that has ID in it."
8140   (let ((dep gnus-newsgroup-dependencies)
8141         headers thread last-id)
8142     ;; First go up in this thread until we find the root.
8143     (setq last-id (gnus-root-id id))
8144     (setq headers (list (car (gnus-id-to-thread last-id))
8145                         (caadr (gnus-id-to-thread last-id))))
8146     ;; We have now found the real root of this thread.  It might have
8147     ;; been gathered into some loose thread, so we have to search
8148     ;; through the threads to find the thread we wanted.
8149     (let ((threads gnus-newsgroup-threads)
8150           sub)
8151       (while threads
8152         (setq sub (car threads))
8153         (if (stringp (car sub))
8154             ;; This is a gathered threads, so we look at the roots
8155             ;; below it to find whether this article in in this
8156             ;; gathered root.
8157             (progn
8158               (setq sub (cdr sub))
8159               (while sub
8160                 (when (member (caar sub) headers)
8161                   (setq thread (car threads)
8162                         threads nil
8163                         sub nil))
8164                 (setq sub (cdr sub))))
8165           ;; It's an ordinary thread, so we check it.
8166           (when (eq (car sub) (car headers))
8167             (setq thread sub
8168                   threads nil)))
8169         (setq threads (cdr threads)))
8170       ;; If this article is in no thread, then it's a root.
8171       (if thread
8172           (unless dont-remove
8173             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8174         (setq thread (gnus-gethash last-id dep)))
8175       (when thread
8176         (prog1
8177             thread ; We return this thread.
8178           (unless dont-remove
8179             (if (stringp (car thread))
8180                 (progn
8181                   ;; If we use dummy roots, then we have to remove the
8182                   ;; dummy root as well.
8183                   (when (eq gnus-summary-make-false-root 'dummy)
8184                     ;; Uhm.
8185                     )
8186                   (setq thread (cdr thread))
8187                   (while thread
8188                     (gnus-remove-thread-1 (car thread))
8189                     (setq thread (cdr thread))))
8190               (gnus-remove-thread-1 thread))))))))
8191
8192 (defun gnus-remove-thread-1 (thread)
8193   "Remove the thread THREAD recursively."
8194   (let ((number (mail-header-number (car thread)))
8195         pos)
8196     (when (setq pos (text-property-any
8197                      (point-min) (point-max) 'gnus-number number))
8198       (goto-char pos)
8199       (gnus-delete-line)
8200       (gnus-data-remove number))
8201     (setq thread (cdr thread))
8202     (while thread
8203       (gnus-remove-thread-1 (pop thread)))))
8204
8205 (defun gnus-sort-threads (threads)
8206   "Sort THREADS."
8207   (if (not gnus-thread-sort-functions)
8208       threads
8209     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8210                     (car gnus-thread-sort-functions)
8211                   `(lambda (t1 t2)
8212                      ,(gnus-make-sort-function 
8213                        (reverse gnus-thread-sort-functions))))))
8214       (gnus-message 7 "Sorting threads...")
8215       (prog1
8216           (sort threads func)
8217         (gnus-message 7 "Sorting threads...done")))))
8218
8219 (defun gnus-sort-articles (articles)
8220   "Sort ARTICLES."
8221   (when gnus-article-sort-functions
8222     (let ((func (if (= 1 (length gnus-article-sort-functions))
8223                     (car gnus-article-sort-functions)
8224                   `(lambda (t1 t2)
8225                      ,(gnus-make-sort-function 
8226                        (reverse gnus-article-sort-functions))))))
8227       (gnus-message 7 "Sorting articles...")
8228       (prog1
8229           (setq gnus-newsgroup-headers (sort articles func))
8230         (gnus-message 7 "Sorting articles...done")))))
8231
8232 (defun gnus-make-sort-function (funs)
8233   "Return a composite sort condition based on the functions in FUNC."
8234   (if (cdr funs)
8235       `(or (,(car funs) t1 t2)
8236            (and (not (,(car funs) t2 t1))
8237                 ,(gnus-make-sort-function (cdr funs))))
8238     `(,(car funs) t1 t2)))
8239                  
8240 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8241 (defmacro gnus-thread-header (thread)
8242   ;; Return header of first article in THREAD.
8243   ;; Note that THREAD must never, ever be anything else than a variable -
8244   ;; using some other form will lead to serious barfage.
8245   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8246   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8247   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8248         (vector thread) 2))
8249
8250 (defsubst gnus-article-sort-by-number (h1 h2)
8251   "Sort articles by article number."
8252   (< (mail-header-number h1)
8253      (mail-header-number h2)))
8254
8255 (defun gnus-thread-sort-by-number (h1 h2)
8256   "Sort threads by root article number."
8257   (gnus-article-sort-by-number
8258    (gnus-thread-header h1) (gnus-thread-header h2)))
8259
8260 (defsubst gnus-article-sort-by-author (h1 h2)
8261   "Sort articles by root author."
8262   (string-lessp
8263    (let ((extract (funcall
8264                    gnus-extract-address-components
8265                    (mail-header-from h1))))
8266      (or (car extract) (cdr extract)))
8267    (let ((extract (funcall
8268                    gnus-extract-address-components
8269                    (mail-header-from h2))))
8270      (or (car extract) (cdr extract)))))
8271
8272 (defun gnus-thread-sort-by-author (h1 h2)
8273   "Sort threads by root author."
8274   (gnus-article-sort-by-author
8275    (gnus-thread-header h1)  (gnus-thread-header h2)))
8276
8277 (defsubst gnus-article-sort-by-subject (h1 h2)
8278   "Sort articles by root subject."
8279   (string-lessp
8280    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8281    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8282
8283 (defun gnus-thread-sort-by-subject (h1 h2)
8284   "Sort threads by root subject."
8285   (gnus-article-sort-by-subject
8286    (gnus-thread-header h1) (gnus-thread-header h2)))
8287
8288 (defsubst gnus-article-sort-by-date (h1 h2)
8289   "Sort articles by root article date."
8290   (string-lessp
8291    (gnus-sortable-date (mail-header-date h1))
8292    (gnus-sortable-date (mail-header-date h2))))
8293
8294 (defun gnus-thread-sort-by-date (h1 h2)
8295   "Sort threads by root article date."
8296   (gnus-article-sort-by-date
8297    (gnus-thread-header h1) (gnus-thread-header h2)))
8298
8299 (defsubst gnus-article-sort-by-score (h1 h2)
8300   "Sort articles by root article score.
8301 Unscored articles will be counted as having a score of zero."
8302   (> (or (cdr (assq (mail-header-number h1)
8303                     gnus-newsgroup-scored))
8304          gnus-summary-default-score 0)
8305      (or (cdr (assq (mail-header-number h2)
8306                     gnus-newsgroup-scored))
8307          gnus-summary-default-score 0)))
8308
8309 (defun gnus-thread-sort-by-score (h1 h2)
8310   "Sort threads by root article score."
8311   (gnus-article-sort-by-score
8312    (gnus-thread-header h1) (gnus-thread-header h2)))
8313
8314 (defun gnus-thread-sort-by-total-score (h1 h2)
8315   "Sort threads by the sum of all scores in the thread.
8316 Unscored articles will be counted as having a score of zero."
8317   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8318
8319 (defun gnus-thread-total-score (thread)
8320   ;;  This function find the total score of THREAD.
8321   (if (consp thread)
8322       (if (stringp (car thread))
8323           (apply gnus-thread-score-function 0
8324                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8325         (gnus-thread-total-score-1 thread))
8326     (gnus-thread-total-score-1 (list thread))))
8327
8328 (defun gnus-thread-total-score-1 (root)
8329   ;; This function find the total score of the thread below ROOT.
8330   (setq root (car root))
8331   (apply gnus-thread-score-function
8332          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8333              gnus-summary-default-score 0)
8334          (mapcar 'gnus-thread-total-score
8335                  (cdr (gnus-gethash (mail-header-id root)
8336                                     gnus-newsgroup-dependencies)))))
8337
8338 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8339 (defvar gnus-tmp-prev-subject nil)
8340 (defvar gnus-tmp-false-parent nil)
8341 (defvar gnus-tmp-root-expunged nil)
8342 (defvar gnus-tmp-dummy-line nil)
8343
8344 (defun gnus-summary-prepare-threads (threads)
8345   "Prepare summary buffer from THREADS and indentation LEVEL.
8346 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8347 or a straight list of headers."
8348   (gnus-message 7 "Generating summary...")
8349
8350   (setq gnus-newsgroup-threads threads)
8351   (beginning-of-line)
8352
8353   (let ((gnus-tmp-level 0)
8354         (default-score (or gnus-summary-default-score 0))
8355         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8356         thread number subject stack state gnus-tmp-gathered beg-match
8357         new-roots gnus-tmp-new-adopts thread-end
8358         gnus-tmp-header gnus-tmp-unread
8359         gnus-tmp-replied gnus-tmp-subject-or-nil
8360         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8361         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8362         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8363
8364     (setq gnus-tmp-prev-subject nil)
8365
8366     (if (vectorp (car threads))
8367         ;; If this is a straight (sic) list of headers, then a
8368         ;; threaded summary display isn't required, so we just create
8369         ;; an unthreaded one.
8370         (gnus-summary-prepare-unthreaded threads)
8371
8372       ;; Do the threaded display.
8373
8374       (while (or threads stack gnus-tmp-new-adopts new-roots)
8375
8376         (if (and (= gnus-tmp-level 0)
8377                  (not (setq gnus-tmp-dummy-line nil))
8378                  (or (not stack)
8379                      (= (caar stack) 0))
8380                  (not gnus-tmp-false-parent)
8381                  (or gnus-tmp-new-adopts new-roots))
8382             (if gnus-tmp-new-adopts
8383                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8384                       thread (list (car gnus-tmp-new-adopts))
8385                       gnus-tmp-header (caar thread)
8386                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8387               (if new-roots
8388                   (setq thread (list (car new-roots))
8389                         gnus-tmp-header (caar thread)
8390                         new-roots (cdr new-roots))))
8391
8392           (if threads
8393               ;; If there are some threads, we do them before the
8394               ;; threads on the stack.
8395               (setq thread threads
8396                     gnus-tmp-header (caar thread))
8397             ;; There were no current threads, so we pop something off
8398             ;; the stack.
8399             (setq state (car stack)
8400                   gnus-tmp-level (car state)
8401                   thread (cdr state)
8402                   stack (cdr stack)
8403                   gnus-tmp-header (caar thread))))
8404
8405         (setq gnus-tmp-false-parent nil)
8406         (setq gnus-tmp-root-expunged nil)
8407         (setq thread-end nil)
8408
8409         (if (stringp gnus-tmp-header)
8410             ;; The header is a dummy root.
8411             (cond
8412              ((eq gnus-summary-make-false-root 'adopt)
8413               ;; We let the first article adopt the rest.
8414               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8415                                                (cddar thread)))
8416               (setq gnus-tmp-gathered
8417                     (nconc (mapcar
8418                             (lambda (h) (mail-header-number (car h)))
8419                             (cddar thread))
8420                            gnus-tmp-gathered))
8421               (setq thread (cons (list (caar thread)
8422                                        (cadar thread))
8423                                  (cdr thread)))
8424               (setq gnus-tmp-level -1
8425                     gnus-tmp-false-parent t))
8426              ((eq gnus-summary-make-false-root 'empty)
8427               ;; We print adopted articles with empty subject fields.
8428               (setq gnus-tmp-gathered
8429                     (nconc (mapcar
8430                             (lambda (h) (mail-header-number (car h)))
8431                             (cddar thread))
8432                            gnus-tmp-gathered))
8433               (setq gnus-tmp-level -1))
8434              ((eq gnus-summary-make-false-root 'dummy)
8435               ;; We remember that we probably want to output a dummy
8436               ;; root.
8437               (setq gnus-tmp-dummy-line gnus-tmp-header)
8438               (setq gnus-tmp-prev-subject gnus-tmp-header))
8439              (t
8440               ;; We do not make a root for the gathered
8441               ;; sub-threads at all.
8442               (setq gnus-tmp-level -1)))
8443
8444           (setq number (mail-header-number gnus-tmp-header)
8445                 subject (mail-header-subject gnus-tmp-header))
8446
8447           (cond
8448            ;; If the thread has changed subject, we might want to make
8449            ;; this subthread into a root.
8450            ((and (null gnus-thread-ignore-subject)
8451                  (not (zerop gnus-tmp-level))
8452                  gnus-tmp-prev-subject
8453                  (not (inline
8454                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8455             (setq new-roots (nconc new-roots (list (car thread)))
8456                   thread-end t
8457                   gnus-tmp-header nil))
8458            ;; If the article lies outside the current limit,
8459            ;; then we do not display it.
8460            ((and (not (memq number gnus-newsgroup-limit))
8461                  (not gnus-tmp-dummy-line))
8462             (setq gnus-tmp-gathered
8463                   (nconc (mapcar
8464                           (lambda (h) (mail-header-number (car h)))
8465                           (cdar thread))
8466                          gnus-tmp-gathered))
8467             (setq gnus-tmp-new-adopts (if (cdar thread)
8468                                           (append gnus-tmp-new-adopts
8469                                                   (cdar thread))
8470                                         gnus-tmp-new-adopts)
8471                   thread-end t
8472                   gnus-tmp-header nil)
8473             (when (zerop gnus-tmp-level)
8474               (setq gnus-tmp-root-expunged t)))
8475            ;; Perhaps this article is to be marked as read?
8476            ((and gnus-summary-mark-below
8477                  (< (or (cdr (assq number gnus-newsgroup-scored))
8478                         default-score)
8479                     gnus-summary-mark-below)
8480                  ;; Don't touch sparse articles.
8481                  (not (memq number gnus-newsgroup-sparse))
8482                  (not (memq number gnus-newsgroup-ancient)))
8483             (setq gnus-newsgroup-unreads
8484                   (delq number gnus-newsgroup-unreads))
8485             (if gnus-newsgroup-auto-expire
8486                 (push number gnus-newsgroup-expirable)
8487               (push (cons number gnus-low-score-mark)
8488                     gnus-newsgroup-reads))))
8489
8490           (when gnus-tmp-header
8491             ;; We may have an old dummy line to output before this
8492             ;; article.
8493             (when gnus-tmp-dummy-line
8494               (gnus-summary-insert-dummy-line
8495                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8496               (setq gnus-tmp-dummy-line nil))
8497
8498             ;; Compute the mark.
8499             (setq
8500              gnus-tmp-unread
8501              (cond
8502               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8503               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8504               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8505               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8506               (t (or (cdr (assq number gnus-newsgroup-reads))
8507                      gnus-ancient-mark))))
8508
8509             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8510                                   gnus-tmp-header gnus-tmp-level)
8511                   gnus-newsgroup-data)
8512
8513             ;; Actually insert the line.
8514             (setq
8515              gnus-tmp-subject-or-nil
8516              (cond
8517               ((and gnus-thread-ignore-subject
8518                     gnus-tmp-prev-subject
8519                     (not (inline (gnus-subject-equal
8520                                   gnus-tmp-prev-subject subject))))
8521                subject)
8522               ((zerop gnus-tmp-level)
8523                (if (and (eq gnus-summary-make-false-root 'empty)
8524                         (memq number gnus-tmp-gathered)
8525                         gnus-tmp-prev-subject
8526                         (inline (gnus-subject-equal
8527                                  gnus-tmp-prev-subject subject)))
8528                    gnus-summary-same-subject
8529                  subject))
8530               (t gnus-summary-same-subject)))
8531             (if (and (eq gnus-summary-make-false-root 'adopt)
8532                      (= gnus-tmp-level 1)
8533                      (memq number gnus-tmp-gathered))
8534                 (setq gnus-tmp-opening-bracket ?\<
8535                       gnus-tmp-closing-bracket ?\>)
8536               (setq gnus-tmp-opening-bracket ?\[
8537                     gnus-tmp-closing-bracket ?\]))
8538             (setq
8539              gnus-tmp-indentation
8540              (aref gnus-thread-indent-array gnus-tmp-level)
8541              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8542              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8543                                 gnus-summary-default-score 0)
8544              gnus-tmp-score-char
8545              (if (or (null gnus-summary-default-score)
8546                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8547                          gnus-summary-zcore-fuzz)) ? 
8548                (if (< gnus-tmp-score gnus-summary-default-score)
8549                    gnus-score-below-mark gnus-score-over-mark))
8550              gnus-tmp-replied
8551              (cond ((memq number gnus-newsgroup-processable)
8552                     gnus-process-mark)
8553                    ((memq number gnus-newsgroup-cached)
8554                     gnus-cached-mark)
8555                    ((memq number gnus-newsgroup-replied)
8556                     gnus-replied-mark)
8557                    (t gnus-unread-mark))
8558              gnus-tmp-from (mail-header-from gnus-tmp-header)
8559              gnus-tmp-name
8560              (cond
8561               ((string-match "(.+)" gnus-tmp-from)
8562                (substring gnus-tmp-from
8563                           (1+ (match-beginning 0)) (1- (match-end 0))))
8564               ((string-match "<[^>]+> *$" gnus-tmp-from)
8565                (setq beg-match (match-beginning 0))
8566                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8567                         (substring gnus-tmp-from (1+ (match-beginning 0))
8568                                    (1- (match-end 0))))
8569                    (substring gnus-tmp-from 0 beg-match)))
8570               (t gnus-tmp-from)))
8571             (when (string= gnus-tmp-name "")
8572               (setq gnus-tmp-name gnus-tmp-from))
8573             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8574             (gnus-put-text-property
8575              (point)
8576              (progn (eval gnus-summary-line-format-spec) (point))
8577              'gnus-number number)
8578             (when gnus-visual-p
8579               (forward-line -1)
8580               (run-hooks 'gnus-summary-update-hook)
8581               (forward-line 1))
8582
8583             (setq gnus-tmp-prev-subject subject)))
8584
8585         (when (nth 1 thread)
8586           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8587         (incf gnus-tmp-level)
8588         (setq threads (if thread-end nil (cdar thread)))
8589         (unless threads
8590           (setq gnus-tmp-level 0)))))
8591   (gnus-message 7 "Generating summary...done"))
8592
8593 (defun gnus-summary-prepare-unthreaded (headers)
8594   "Generate an unthreaded summary buffer based on HEADERS."
8595   (let (header number mark)
8596
8597     (while headers
8598       ;; We may have to root out some bad articles...
8599       (when (memq (setq number (mail-header-number
8600                                 (setq header (pop headers))))
8601                   gnus-newsgroup-limit)
8602         ;; Mark article as read when it has a low score.
8603         (when (and gnus-summary-mark-below
8604                    (< (or (cdr (assq number gnus-newsgroup-scored))
8605                           gnus-summary-default-score 0)
8606                       gnus-summary-mark-below)
8607                    (not (memq number gnus-newsgroup-ancient)))
8608           (setq gnus-newsgroup-unreads
8609                 (delq number gnus-newsgroup-unreads))
8610           (if gnus-newsgroup-auto-expire
8611               (push number gnus-newsgroup-expirable)
8612             (push (cons number gnus-low-score-mark)
8613                   gnus-newsgroup-reads)))
8614
8615         (setq mark
8616               (cond
8617                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8618                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8619                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8620                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8621                (t (or (cdr (assq number gnus-newsgroup-reads))
8622                       gnus-ancient-mark))))
8623         (setq gnus-newsgroup-data
8624               (cons (gnus-data-make number mark (1+ (point)) header 0)
8625                     gnus-newsgroup-data))
8626         (gnus-summary-insert-line
8627          header 0 nil mark (memq number gnus-newsgroup-replied)
8628          (memq number gnus-newsgroup-expirable)
8629          (mail-header-subject header) nil
8630          (cdr (assq number gnus-newsgroup-scored))
8631          (memq number gnus-newsgroup-processable))))))
8632
8633 (defun gnus-select-newsgroup (group &optional read-all)
8634   "Select newsgroup GROUP.
8635 If READ-ALL is non-nil, all articles in the group are selected."
8636   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8637          (info (nth 2 entry))
8638          articles fetched-articles cached)
8639
8640     (or (gnus-check-server
8641          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8642         (error "Couldn't open server"))
8643
8644     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8645         (gnus-activate-group group)     ; Or we can activate it...
8646         (progn                          ; Or we bug out.
8647           (when (equal major-mode 'gnus-summary-mode)
8648             (kill-buffer (current-buffer)))
8649           (error "Couldn't request group %s: %s"
8650                  group (gnus-status-message group))))
8651
8652     (unless (gnus-request-group group t)
8653       (when (equal major-mode 'gnus-summary-mode)
8654         (kill-buffer (current-buffer)))
8655       (error "Couldn't request group %s: %s"
8656              group (gnus-status-message group)))      
8657
8658     (setq gnus-newsgroup-name group)
8659     (setq gnus-newsgroup-unselected nil)
8660     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8661
8662     (and gnus-asynchronous
8663          (gnus-check-backend-function
8664           'request-asynchronous gnus-newsgroup-name)
8665          (setq gnus-newsgroup-async
8666                (gnus-request-asynchronous gnus-newsgroup-name)))
8667
8668     ;; Adjust and set lists of article marks.
8669     (when info
8670       (gnus-adjust-marked-articles info))
8671
8672     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8673     (when (gnus-virtual-group-p group)
8674       (setq cached gnus-newsgroup-cached))
8675
8676     (setq gnus-newsgroup-unreads
8677           (gnus-set-difference
8678            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8679            gnus-newsgroup-dormant))
8680
8681     (setq gnus-newsgroup-processable nil)
8682
8683     (setq articles (gnus-articles-to-read group read-all))
8684
8685     (cond
8686      ((null articles)
8687       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8688       'quit)
8689      ((eq articles 0) nil)
8690      (t
8691       ;; Init the dependencies hash table.
8692       (setq gnus-newsgroup-dependencies
8693             (gnus-make-hashtable (length articles)))
8694       ;; Retrieve the headers and read them in.
8695       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8696       (setq gnus-newsgroup-headers
8697             (if (eq 'nov
8698                     (setq gnus-headers-retrieved-by
8699                           (gnus-retrieve-headers
8700                            articles gnus-newsgroup-name
8701                            ;; We might want to fetch old headers, but
8702                            ;; not if there is only 1 article.
8703                            (and gnus-fetch-old-headers
8704                                 (or (and
8705                                      (not (eq gnus-fetch-old-headers 'some))
8706                                      (not (numberp gnus-fetch-old-headers)))
8707                                     (> (length articles) 1))))))
8708                 (gnus-get-newsgroup-headers-xover articles)
8709               (gnus-get-newsgroup-headers)))
8710       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8711
8712       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8713       (when cached
8714         (setq gnus-newsgroup-cached cached))
8715
8716       ;; Set the initial limit.
8717       (setq gnus-newsgroup-limit (copy-sequence articles))
8718       ;; Remove canceled articles from the list of unread articles.
8719       (setq gnus-newsgroup-unreads
8720             (gnus-set-sorted-intersection
8721              gnus-newsgroup-unreads
8722              (setq fetched-articles
8723                    (mapcar (lambda (headers) (mail-header-number headers))
8724                            gnus-newsgroup-headers))))
8725       ;; Removed marked articles that do not exist.
8726       (gnus-update-missing-marks
8727        (gnus-sorted-complement fetched-articles articles))
8728       ;; We might want to build some more threads first.
8729       (and gnus-fetch-old-headers
8730            (eq gnus-headers-retrieved-by 'nov)
8731            (gnus-build-old-threads))
8732       ;; Check whether auto-expire is to be done in this group.
8733       (setq gnus-newsgroup-auto-expire
8734             (gnus-group-auto-expirable-p group))
8735       ;; Set up the article buffer now, if necessary.
8736       (unless gnus-single-article-buffer
8737         (gnus-article-setup-buffer))
8738       ;; First and last article in this newsgroup.
8739       (and gnus-newsgroup-headers
8740            (setq gnus-newsgroup-begin
8741                  (mail-header-number (car gnus-newsgroup-headers)))
8742            (setq gnus-newsgroup-end
8743                  (mail-header-number
8744                   (gnus-last-element gnus-newsgroup-headers))))
8745       (setq gnus-reffed-article-number -1)
8746       ;; GROUP is successfully selected.
8747       (or gnus-newsgroup-headers t)))))
8748
8749 (defun gnus-articles-to-read (group read-all)
8750   ;; Find out what articles the user wants to read.
8751   (let* ((articles
8752           ;; Select all articles if `read-all' is non-nil, or if there
8753           ;; are no unread articles.
8754           (if (or read-all
8755                   (and (zerop (length gnus-newsgroup-marked))
8756                        (zerop (length gnus-newsgroup-unreads))))
8757               (gnus-uncompress-range (gnus-active group))
8758             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8759                           (copy-sequence gnus-newsgroup-unreads))
8760                   '<)))
8761          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8762          (scored (length scored-list))
8763          (number (length articles))
8764          (marked (+ (length gnus-newsgroup-marked)
8765                     (length gnus-newsgroup-dormant)))
8766          (select
8767           (cond
8768            ((numberp read-all)
8769             read-all)
8770            (t
8771             (condition-case ()
8772                 (cond
8773                  ((and (or (<= scored marked) (= scored number))
8774                        (numberp gnus-large-newsgroup)
8775                        (> number gnus-large-newsgroup))
8776                   (let ((input
8777                          (read-string
8778                           (format
8779                            "How many articles from %s (default %d): "
8780                            gnus-newsgroup-name number))))
8781                     (if (string-match "^[ \t]*$" input) number input)))
8782                  ((and (> scored marked) (< scored number))
8783                   (let ((input
8784                          (read-string
8785                           (format "%s %s (%d scored, %d total): "
8786                                   "How many articles from"
8787                                   group scored number))))
8788                     (if (string-match "^[ \t]*$" input)
8789                         number input)))
8790                  (t number))
8791               (quit nil))))))
8792     (setq select (if (stringp select) (string-to-number select) select))
8793     (if (or (null select) (zerop select))
8794         select
8795       (if (and (not (zerop scored)) (<= (abs select) scored))
8796           (progn
8797             (setq articles (sort scored-list '<))
8798             (setq number (length articles)))
8799         (setq articles (copy-sequence articles)))
8800
8801       (if (< (abs select) number)
8802           (if (< select 0)
8803               ;; Select the N oldest articles.
8804               (setcdr (nthcdr (1- (abs select)) articles) nil)
8805             ;; Select the N most recent articles.
8806             (setq articles (nthcdr (- number select) articles))))
8807       (setq gnus-newsgroup-unselected
8808             (gnus-sorted-intersection
8809              gnus-newsgroup-unreads
8810              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8811       articles)))
8812
8813 (defun gnus-killed-articles (killed articles)
8814   (let (out)
8815     (while articles
8816       (if (inline (gnus-member-of-range (car articles) killed))
8817           (setq out (cons (car articles) out)))
8818       (setq articles (cdr articles)))
8819     out))
8820
8821 (defun gnus-uncompress-marks (marks)
8822   "Uncompress the mark ranges in MARKS."
8823   (let ((uncompressed '(score bookmark))
8824         out)
8825     (while marks
8826       (if (memq (caar marks) uncompressed)
8827           (push (car marks) out)
8828         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8829       (setq marks (cdr marks)))
8830     out))
8831
8832 (defun gnus-adjust-marked-articles (info)
8833   "Set all article lists and remove all marks that are no longer legal."
8834   (let* ((marked-lists (gnus-info-marks info))
8835          (active (gnus-active (gnus-info-group info)))
8836          (min (car active))
8837          (max (cdr active))
8838          (types gnus-article-mark-lists)
8839          (uncompressed '(score bookmark))
8840          marks var articles article mark)
8841
8842     (while marked-lists
8843       (setq marks (pop marked-lists))
8844       (set (setq var (intern (format "gnus-newsgroup-%s"
8845                                      (car (rassq (setq mark (car marks))
8846                                                  types)))))
8847            (if (memq (car marks) uncompressed) (cdr marks)
8848              (gnus-uncompress-range (cdr marks))))
8849
8850       (setq articles (symbol-value var))
8851
8852       ;; All articles have to be subsets of the active articles.
8853       (cond
8854        ;; Adjust "simple" lists.
8855        ((memq mark '(tick dormant expirable reply killed save))
8856         (while articles
8857           (when (or (< (setq article (pop articles)) min) (> article max))
8858             (set var (delq article (symbol-value var))))))
8859        ;; Adjust assocs.
8860        ((memq mark '(score bookmark))
8861         (while articles
8862           (when (or (< (car (setq article (pop articles))) min)
8863                     (> (car article) max))
8864             (set var (delq article (symbol-value var))))))))))
8865
8866 (defun gnus-update-missing-marks (missing)
8867   "Go through the list of MISSING articles and remove them mark lists."
8868   (when missing
8869     (let ((types gnus-article-mark-lists)
8870           var m)
8871       ;; Go through all types.
8872       (while types
8873         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8874         (when (symbol-value var)
8875           ;; This list has articles.  So we delete all missing articles
8876           ;; from it.
8877           (setq m missing)
8878           (while m
8879             (set var (delq (pop m) (symbol-value var)))))))))
8880
8881 (defun gnus-update-marks ()
8882   "Enter the various lists of marked articles into the newsgroup info list."
8883   (let ((types gnus-article-mark-lists)
8884         (info (gnus-get-info gnus-newsgroup-name))
8885         (uncompressed '(score bookmark killed))
8886         type list newmarked symbol)
8887     (when info
8888       ;; Add all marks lists that are non-nil to the list of marks lists.
8889       (while types
8890         (setq type (pop types))
8891         (when (setq list (symbol-value
8892                           (setq symbol
8893                                 (intern (format "gnus-newsgroup-%s"
8894                                                 (car type))))))
8895           (push (cons (cdr type)
8896                       (if (memq (cdr type) uncompressed) list
8897                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8898                 newmarked)))
8899
8900       ;; Enter these new marks into the info of the group.
8901       (if (nthcdr 3 info)
8902           (setcar (nthcdr 3 info) newmarked)
8903         ;; Add the marks lists to the end of the info.
8904         (when newmarked
8905           (setcdr (nthcdr 2 info) (list newmarked))))
8906
8907       ;; Cut off the end of the info if there's nothing else there.
8908       (let ((i 5))
8909         (while (and (> i 2)
8910                     (not (nth i info)))
8911           (when (nthcdr (decf i) info)
8912             (setcdr (nthcdr i info) nil)))))))
8913
8914 (defun gnus-add-marked-articles (group type articles &optional info force)
8915   ;; Add ARTICLES of TYPE to the info of GROUP.
8916   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8917   ;; add, but replace marked articles of TYPE with ARTICLES.
8918   (let ((info (or info (gnus-get-info group)))
8919         (uncompressed '(score bookmark killed))
8920         marked m)
8921     (or (not info)
8922         (and (not (setq marked (nthcdr 3 info)))
8923              (or (null articles)
8924                  (setcdr (nthcdr 2 info)
8925                          (list (list (cons type (gnus-compress-sequence
8926                                                  articles t)))))))
8927         (and (not (setq m (assq type (car marked))))
8928              (or (null articles)
8929                  (setcar marked
8930                          (cons (cons type (gnus-compress-sequence articles t) )
8931                                (car marked)))))
8932         (if force
8933             (if (null articles)
8934                 (setcar (nthcdr 3 info)
8935                         (delq (assq type (car marked)) (car marked)))
8936               (setcdr m (gnus-compress-sequence articles t)))
8937           (setcdr m (gnus-compress-sequence
8938                      (sort (nconc (gnus-uncompress-range (cdr m))
8939                                   (copy-sequence articles)) '<) t))))))
8940
8941 (defun gnus-set-mode-line (where)
8942   "This function sets the mode line of the article or summary buffers.
8943 If WHERE is `summary', the summary mode line format will be used."
8944   ;; Is this mode line one we keep updated?
8945   (when (memq where gnus-updated-mode-lines)
8946     (let (mode-string)
8947       (save-excursion
8948         ;; We evaluate this in the summary buffer since these
8949         ;; variables are buffer-local to that buffer.
8950         (set-buffer gnus-summary-buffer)
8951         ;; We bind all these variables that are used in the `eval' form
8952         ;; below.
8953         (let* ((mformat (symbol-value
8954                          (intern
8955                           (format "gnus-%s-mode-line-format-spec" where))))
8956                (gnus-tmp-group-name gnus-newsgroup-name)
8957                (gnus-tmp-article-number (or gnus-current-article 0))
8958                (gnus-tmp-unread gnus-newsgroup-unreads)
8959                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8960                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8961                (gnus-tmp-unread-and-unselected
8962                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8963                             (zerop gnus-tmp-unselected)) "")
8964                       ((zerop gnus-tmp-unselected)
8965                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8966                       (t (format "{%d(+%d) more}"
8967                                  gnus-tmp-unread-and-unticked
8968                                  gnus-tmp-unselected))))
8969                (gnus-tmp-subject
8970                 (if (and gnus-current-headers
8971                          (vectorp gnus-current-headers))
8972                     (gnus-mode-string-quote
8973                      (mail-header-subject gnus-current-headers)) ""))
8974                max-len
8975                gnus-tmp-header);; passed as argument to any user-format-funcs
8976           (setq mode-string (eval mformat))
8977           (setq max-len (max 4 (if gnus-mode-non-string-length
8978                                    (- (window-width)
8979                                       gnus-mode-non-string-length)
8980                                  (length mode-string))))
8981           ;; We might have to chop a bit of the string off...
8982           (when (> (length mode-string) max-len)
8983             (setq mode-string
8984                   (concat (gnus-truncate-string mode-string (- max-len 3))
8985                           "...")))
8986           ;; Pad the mode string a bit.
8987           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8988       ;; Update the mode line.
8989       (setq mode-line-buffer-identification (list mode-string))
8990       (set-buffer-modified-p t))))
8991
8992 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8993   "Go through the HEADERS list and add all Xrefs to a hash table.
8994 The resulting hash table is returned, or nil if no Xrefs were found."
8995   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
8996          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8997          (xref-hashtb (make-vector 63 0))
8998          start group entry number xrefs header)
8999     (while headers
9000       (setq header (pop headers))
9001       (when (and (setq xrefs (mail-header-xref header))
9002                  (not (memq (setq number (mail-header-number header))
9003                             unreads)))
9004         (setq start 0)
9005         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
9006           (setq start (match-end 0))
9007           (setq group (if prefix
9008                           (concat prefix (substring xrefs (match-beginning 1)
9009                                                     (match-end 1)))
9010                         (substring xrefs (match-beginning 1) (match-end 1))))
9011           (setq number
9012                 (string-to-int (substring xrefs (match-beginning 2)
9013                                           (match-end 2))))
9014           (if (setq entry (gnus-gethash group xref-hashtb))
9015               (setcdr entry (cons number (cdr entry)))
9016             (gnus-sethash group (cons number nil) xref-hashtb)))))
9017     (and start xref-hashtb)))
9018
9019 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
9020   "Look through all the headers and mark the Xrefs as read."
9021   (let ((virtual (gnus-virtual-group-p from-newsgroup))
9022         name entry info xref-hashtb idlist method nth4)
9023     (save-excursion
9024       (set-buffer gnus-group-buffer)
9025       (when (setq xref-hashtb
9026                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
9027         (mapatoms
9028          (lambda (group)
9029            (unless (string= from-newsgroup (setq name (symbol-name group)))
9030              (setq idlist (symbol-value group))
9031              ;; Dead groups are not updated.
9032              (and (prog1
9033                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
9034                             info (nth 2 entry))
9035                     (if (stringp (setq nth4 (gnus-info-method info)))
9036                         (setq nth4 (gnus-server-to-method nth4))))
9037                   ;; Only do the xrefs if the group has the same
9038                   ;; select method as the group we have just read.
9039                   (or (gnus-methods-equal-p
9040                        nth4 (gnus-find-method-for-group from-newsgroup))
9041                       virtual
9042                       (equal nth4 (setq method (gnus-find-method-for-group
9043                                                 from-newsgroup)))
9044                       (and (equal (car nth4) (car method))
9045                            (equal (nth 1 nth4) (nth 1 method))))
9046                   gnus-use-cross-reference
9047                   (or (not (eq gnus-use-cross-reference t))
9048                       virtual
9049                       ;; Only do cross-references on subscribed
9050                       ;; groups, if that is what is wanted.
9051                       (<= (gnus-info-level info) gnus-level-subscribed))
9052                   (gnus-group-make-articles-read name idlist))))
9053          xref-hashtb)))))
9054
9055 (defun gnus-group-make-articles-read (group articles)
9056   (let* ((num 0)
9057          (entry (gnus-gethash group gnus-newsrc-hashtb))
9058          (info (nth 2 entry))
9059          (active (gnus-active group))
9060          range)
9061     ;; First peel off all illegal article numbers.
9062     (if active
9063         (let ((ids articles)
9064               id first)
9065           (while ids
9066             (setq id (car ids))
9067             (if (and first (> id (cdr active)))
9068                 (progn
9069                   ;; We'll end up in this situation in one particular
9070                   ;; obscure situation.  If you re-scan a group and get
9071                   ;; a new article that is cross-posted to a different
9072                   ;; group that has not been re-scanned, you might get
9073                   ;; crossposted article that has a higher number than
9074                   ;; Gnus believes possible.  So we re-activate this
9075                   ;; group as well.  This might mean doing the
9076                   ;; crossposting thingy will *increase* the number
9077                   ;; of articles in some groups.  Tsk, tsk.
9078                   (setq active (or (gnus-activate-group group) active))))
9079             (if (or (> id (cdr active))
9080                     (< id (car active)))
9081                 (setq articles (delq id articles)))
9082             (setq ids (cdr ids)))))
9083     ;; If the read list is nil, we init it.
9084     (and active
9085          (null (gnus-info-read info))
9086          (> (car active) 1)
9087          (gnus-info-set-read info (cons 1 (1- (car active)))))
9088     ;; Then we add the read articles to the range.
9089     (gnus-info-set-read
9090      info
9091      (setq range
9092            (gnus-add-to-range
9093             (gnus-info-read info) (setq articles (sort articles '<)))))
9094     ;; Then we have to re-compute how many unread
9095     ;; articles there are in this group.
9096     (if active
9097         (progn
9098           (cond
9099            ((not range)
9100             (setq num (- (1+ (cdr active)) (car active))))
9101            ((not (listp (cdr range)))
9102             (setq num (- (cdr active) (- (1+ (cdr range))
9103                                          (car range)))))
9104            (t
9105             (while range
9106               (if (numberp (car range))
9107                   (setq num (1+ num))
9108                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9109               (setq range (cdr range)))
9110             (setq num (- (cdr active) num))))
9111           ;; Update the number of unread articles.
9112           (setcar entry num)
9113           ;; Update the group buffer.
9114           (gnus-group-update-group group t)))))
9115
9116 (defun gnus-methods-equal-p (m1 m2)
9117   (let ((m1 (or m1 gnus-select-method))
9118         (m2 (or m2 gnus-select-method)))
9119     (or (equal m1 m2)
9120         (and (eq (car m1) (car m2))
9121              (or (not (memq 'address (assoc (symbol-name (car m1))
9122                                             gnus-valid-select-methods)))
9123                  (equal (nth 1 m1) (nth 1 m2)))))))
9124
9125 (defsubst gnus-header-value ()
9126   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9127
9128 (defvar gnus-newsgroup-none-id 0)
9129
9130 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9131   (let ((cur nntp-server-buffer)
9132         (dependencies
9133          (or dependencies
9134              (save-excursion (set-buffer gnus-summary-buffer)
9135                              gnus-newsgroup-dependencies)))
9136         headers id id-dep ref-dep end ref)
9137     (save-excursion
9138       (set-buffer nntp-server-buffer)
9139       (run-hooks 'gnus-parse-headers-hook)
9140       (let ((case-fold-search t)
9141             in-reply-to header p lines)
9142         (goto-char (point-min))
9143         ;; Search to the beginning of the next header.  Error messages
9144         ;; do not begin with 2 or 3.
9145         (while (re-search-forward "^[23][0-9]+ " nil t)
9146           (setq id nil
9147                 ref nil)
9148           ;; This implementation of this function, with nine
9149           ;; search-forwards instead of the one re-search-forward and
9150           ;; a case (which basically was the old function) is actually
9151           ;; about twice as fast, even though it looks messier.  You
9152           ;; can't have everything, I guess.  Speed and elegance
9153           ;; doesn't always go hand in hand.
9154           (setq
9155            header
9156            (vector
9157             ;; Number.
9158             (prog1
9159                 (read cur)
9160               (end-of-line)
9161               (setq p (point))
9162               (narrow-to-region (point)
9163                                 (or (and (search-forward "\n.\n" nil t)
9164                                          (- (point) 2))
9165                                     (point))))
9166             ;; Subject.
9167             (progn
9168               (goto-char p)
9169               (if (search-forward "\nsubject: " nil t)
9170                   (gnus-header-value) "(none)"))
9171             ;; From.
9172             (progn
9173               (goto-char p)
9174               (if (search-forward "\nfrom: " nil t)
9175                   (gnus-header-value) "(nobody)"))
9176             ;; Date.
9177             (progn
9178               (goto-char p)
9179               (if (search-forward "\ndate: " nil t)
9180                   (gnus-header-value) ""))
9181             ;; Message-ID.
9182             (progn
9183               (goto-char p)
9184               (if (search-forward "\nmessage-id: " nil t)
9185                   (setq id (gnus-header-value))
9186                 ;; If there was no message-id, we just fake one to make
9187                 ;; subsequent routines simpler.
9188                 (setq id (concat "none+"
9189                                  (int-to-string
9190                                   (setq gnus-newsgroup-none-id
9191                                         (1+ gnus-newsgroup-none-id)))))))
9192             ;; References.
9193             (progn
9194               (goto-char p)
9195               (if (search-forward "\nreferences: " nil t)
9196                   (prog1
9197                       (gnus-header-value)
9198                     (setq end (match-end 0))
9199                     (save-excursion
9200                       (setq ref
9201                             (buffer-substring
9202                              (progn
9203                                (end-of-line)
9204                                (search-backward ">" end t)
9205                                (1+ (point)))
9206                              (progn
9207                                (search-backward "<" end t)
9208                                (point))))))
9209                 ;; Get the references from the in-reply-to header if there
9210                 ;; were no references and the in-reply-to header looks
9211                 ;; promising.
9212                 (if (and (search-forward "\nin-reply-to: " nil t)
9213                          (setq in-reply-to (gnus-header-value))
9214                          (string-match "<[^>]+>" in-reply-to))
9215                     (setq ref (substring in-reply-to (match-beginning 0)
9216                                          (match-end 0)))
9217                   (setq ref ""))))
9218             ;; Chars.
9219             0
9220             ;; Lines.
9221             (progn
9222               (goto-char p)
9223               (if (search-forward "\nlines: " nil t)
9224                   (if (numberp (setq lines (read cur)))
9225                       lines 0)
9226                 0))
9227             ;; Xref.
9228             (progn
9229               (goto-char p)
9230               (and (search-forward "\nxref: " nil t)
9231                    (gnus-header-value)))))
9232           ;; We do the threading while we read the headers.  The
9233           ;; message-id and the last reference are both entered into
9234           ;; the same hash table.  Some tippy-toeing around has to be
9235           ;; done in case an article has arrived before the article
9236           ;; which it refers to.
9237           (if (boundp (setq id-dep (intern id dependencies)))
9238               (if (and (car (symbol-value id-dep))
9239                        (not force-new))
9240                   ;; An article with this Message-ID has already
9241                   ;; been seen, so we ignore this one, except we add
9242                   ;; any additional Xrefs (in case the two articles
9243                   ;; came from different servers).
9244                   (progn
9245                     (mail-header-set-xref
9246                      (car (symbol-value id-dep))
9247                      (concat (or (mail-header-xref
9248                                   (car (symbol-value id-dep))) "")
9249                              (or (mail-header-xref header) "")))
9250                     (setq header nil))
9251                 (setcar (symbol-value id-dep) header))
9252             (set id-dep (list header)))
9253           (when header
9254             (if (boundp (setq ref-dep (intern ref dependencies)))
9255                 (setcdr (symbol-value ref-dep)
9256                         (nconc (cdr (symbol-value ref-dep))
9257                                (list (symbol-value id-dep))))
9258               (set ref-dep (list nil (symbol-value id-dep))))
9259             (setq headers (cons header headers)))
9260           (goto-char (point-max))
9261           (widen))
9262         (nreverse headers)))))
9263
9264 ;; The following macros and functions were written by Felix Lee
9265 ;; <flee@cse.psu.edu>.
9266
9267 (defmacro gnus-nov-read-integer ()
9268   '(prog1
9269        (if (= (following-char) ?\t)
9270            0
9271          (let ((num (condition-case nil (read buffer) (error nil))))
9272            (if (numberp num) num 0)))
9273      (or (eobp) (forward-char 1))))
9274
9275 (defmacro gnus-nov-skip-field ()
9276   '(search-forward "\t" eol 'move))
9277
9278 (defmacro gnus-nov-field ()
9279   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9280
9281 ;; Goes through the xover lines and returns a list of vectors
9282 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9283                                                   force-new dependencies)
9284   "Parse the news overview data in the server buffer, and return a
9285 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9286   ;; Get the Xref when the users reads the articles since most/some
9287   ;; NNTP servers do not include Xrefs when using XOVER.
9288   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9289   (let ((cur nntp-server-buffer)
9290         (dependencies (or dependencies gnus-newsgroup-dependencies))
9291         number headers header)
9292     (save-excursion
9293       (set-buffer nntp-server-buffer)
9294       ;; Allow the user to mangle the headers before parsing them.
9295       (run-hooks 'gnus-parse-headers-hook)
9296       (goto-char (point-min))
9297       (while (and sequence (not (eobp)))
9298         (setq number (read cur))
9299         (while (and sequence (< (car sequence) number))
9300           (setq sequence (cdr sequence)))
9301         (and sequence
9302              (eq number (car sequence))
9303              (progn
9304                (setq sequence (cdr sequence))
9305                (if (setq header
9306                          (inline (gnus-nov-parse-line
9307                                   number dependencies force-new)))
9308                    (setq headers (cons header headers)))))
9309         (forward-line 1))
9310       (setq headers (nreverse headers)))
9311     headers))
9312
9313 ;; This function has to be called with point after the article number
9314 ;; on the beginning of the line.
9315 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9316   (let ((none 0)
9317         (eol (gnus-point-at-eol))
9318         (buffer (current-buffer))
9319         header ref id id-dep ref-dep)
9320
9321     ;; overview: [num subject from date id refs chars lines misc]
9322     (narrow-to-region (point) eol)
9323     (or (eobp) (forward-char))
9324
9325     (condition-case nil
9326         (setq header
9327               (vector
9328                number                   ; number
9329                (gnus-nov-field)         ; subject
9330                (gnus-nov-field)         ; from
9331                (gnus-nov-field)         ; date
9332                (setq id (or (gnus-nov-field)
9333                             (concat "none+"
9334                                     (int-to-string
9335                                      (setq none (1+ none)))))) ; id
9336                (progn
9337                  (save-excursion
9338                    (let ((beg (point)))
9339                      (search-forward "\t" eol)
9340                      (if (search-backward ">" beg t)
9341                          (setq ref
9342                                (buffer-substring
9343                                 (1+ (point))
9344                                 (search-backward "<" beg t)))
9345                        (setq ref nil))))
9346                  (gnus-nov-field))      ; refs
9347                (gnus-nov-read-integer)  ; chars
9348                (gnus-nov-read-integer)  ; lines
9349                (if (= (following-char) ?\n)
9350                    nil
9351                  (gnus-nov-field))      ; misc
9352                ))
9353       (error (progn
9354                (gnus-error 4 "Strange nov line")
9355                (setq header nil)
9356                (goto-char eol))))
9357
9358     (widen)
9359
9360     ;; We build the thread tree.
9361     (when header
9362       (if (boundp (setq id-dep (intern id dependencies)))
9363           (if (and (car (symbol-value id-dep))
9364                    (not force-new))
9365               ;; An article with this Message-ID has already been seen,
9366               ;; so we ignore this one, except we add any additional
9367               ;; Xrefs (in case the two articles came from different
9368               ;; servers.
9369               (progn
9370                 (mail-header-set-xref
9371                  (car (symbol-value id-dep))
9372                  (concat (or (mail-header-xref
9373                               (car (symbol-value id-dep))) "")
9374                          (or (mail-header-xref header) "")))
9375                 (setq header nil))
9376             (setcar (symbol-value id-dep) header))
9377         (set id-dep (list header))))
9378     (if header
9379         (progn
9380           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9381               (setcdr (symbol-value ref-dep)
9382                       (nconc (cdr (symbol-value ref-dep))
9383                              (list (symbol-value id-dep))))
9384             (set ref-dep (list nil (symbol-value id-dep))))))
9385     header))
9386
9387 (defun gnus-article-get-xrefs ()
9388   "Fill in the Xref value in `gnus-current-headers', if necessary.
9389 This is meant to be called in `gnus-article-internal-prepare-hook'."
9390   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9391                                  gnus-current-headers)))
9392     (or (not gnus-use-cross-reference)
9393         (not headers)
9394         (and (mail-header-xref headers)
9395              (not (string= (mail-header-xref headers) "")))
9396         (let ((case-fold-search t)
9397               xref)
9398           (save-restriction
9399             (nnheader-narrow-to-headers)
9400             (goto-char (point-min))
9401             (if (or (and (eq (downcase (following-char)) ?x)
9402                          (looking-at "Xref:"))
9403                     (search-forward "\nXref:" nil t))
9404                 (progn
9405                   (goto-char (1+ (match-end 0)))
9406                   (setq xref (buffer-substring (point)
9407                                                (progn (end-of-line) (point))))
9408                   (mail-header-set-xref headers xref))))))))
9409
9410 (defun gnus-summary-insert-subject (id &optional old-header)
9411   "Find article ID and insert the summary line for that article."
9412   (let ((header (gnus-read-header id))
9413         (number (and (numberp id) id))
9414         pos)
9415     (when header
9416       ;; Rebuild the thread that this article is part of and go to the
9417       ;; article we have fetched.
9418       (when old-header
9419         (when (setq pos (text-property-any
9420                          (point-min) (point-max) 'gnus-number 
9421                          (mail-header-number old-header)))
9422           (goto-char pos)
9423           (gnus-delete-line)
9424           (gnus-data-remove (mail-header-number old-header))))
9425       (when old-header
9426         (mail-header-set-number header (mail-header-number old-header)))
9427       (setq gnus-newsgroup-sparse
9428             (delq (mail-header-number header) gnus-newsgroup-sparse))
9429       (gnus-rebuild-thread (mail-header-id header))
9430       (gnus-summary-goto-subject (setq number (mail-header-number header))
9431                                  nil t))
9432     (when (and (numberp number)
9433                (> number 0))
9434       ;; We have to update the boundaries even if we can't fetch the
9435       ;; article if ID is a number -- so that the next `P' or `N'
9436       ;; command will fetch the previous (or next) article even
9437       ;; if the one we tried to fetch this time has been canceled.
9438       (and (> number gnus-newsgroup-end)
9439            (setq gnus-newsgroup-end number))
9440       (and (< number gnus-newsgroup-begin)
9441            (setq gnus-newsgroup-begin number))
9442       (setq gnus-newsgroup-unselected
9443             (delq number gnus-newsgroup-unselected)))
9444     ;; Report back a success?
9445     (and header (mail-header-number header))))
9446
9447 (defun gnus-summary-work-articles (n)
9448   "Return a list of articles to be worked upon.  The prefix argument,
9449 the list of process marked articles, and the current article will be
9450 taken into consideration."
9451   (cond
9452    ((and n (numberp n))
9453     ;; A numerical prefix has been given.
9454     (let ((backward (< n 0))
9455           (n (abs n))
9456           articles article)
9457       (save-excursion
9458         (while
9459             (and (> n 0)
9460                  (push (setq article (gnus-summary-article-number))
9461                        articles)
9462                  (if backward
9463                      (gnus-summary-find-prev nil article)
9464                    (gnus-summary-find-next nil article)))
9465           (decf n)))
9466       (nreverse articles)))
9467    ((and (boundp 'transient-mark-mode)
9468          transient-mark-mode
9469          mark-active)
9470     ;; Work on the region between point and mark.
9471     (let ((max (max (point) (mark)))
9472           articles article)
9473       (save-excursion
9474         (goto-char (min (point) (mark)))
9475         (while
9476             (and
9477              (push (setq article (gnus-summary-article-number)) articles)
9478              (gnus-summary-find-next nil article)
9479              (< (point) max)))
9480         (nreverse articles))))
9481    (gnus-newsgroup-processable
9482     ;; There are process-marked articles present.
9483     (reverse gnus-newsgroup-processable))
9484    (t
9485     ;; Just return the current article.
9486     (list (gnus-summary-article-number)))))
9487
9488 (defun gnus-summary-search-group (&optional backward use-level)
9489   "Search for next unread newsgroup.
9490 If optional argument BACKWARD is non-nil, search backward instead."
9491   (save-excursion
9492     (set-buffer gnus-group-buffer)
9493     (if (gnus-group-search-forward
9494          backward nil (if use-level (gnus-group-group-level) nil))
9495         (gnus-group-group-name))))
9496
9497 (defun gnus-summary-best-group (&optional exclude-group)
9498   "Find the name of the best unread group.
9499 If EXCLUDE-GROUP, do not go to this group."
9500   (save-excursion
9501     (set-buffer gnus-group-buffer)
9502     (save-excursion
9503       (gnus-group-best-unread-group exclude-group))))
9504
9505 (defun gnus-summary-find-next (&optional unread article backward)
9506   (if backward (gnus-summary-find-prev)
9507     (let* ((dummy (gnus-summary-article-intangible-p))
9508            (article (or article (gnus-summary-article-number)))
9509            (arts (gnus-data-find-list article))
9510            result)
9511       (when (and (not dummy)
9512                  (or (not gnus-summary-check-current)
9513                      (not unread)
9514                      (not (gnus-data-unread-p (car arts)))))
9515         (setq arts (cdr arts)))
9516       (when (setq result
9517                   (if unread
9518                       (progn
9519                         (while arts
9520                           (when (gnus-data-unread-p (car arts))
9521                             (setq result (car arts)
9522                                   arts nil))
9523                           (setq arts (cdr arts)))
9524                         result)
9525                     (car arts)))
9526         (goto-char (gnus-data-pos result))
9527         (gnus-data-number result)))))
9528
9529 (defun gnus-summary-find-prev (&optional unread article)
9530   (let* ((article (or article (gnus-summary-article-number)))
9531          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9532          result)
9533     (when (or (not gnus-summary-check-current)
9534               (not unread)
9535               (not (gnus-data-unread-p (car arts))))
9536       (setq arts (cdr arts)))
9537     (if (setq result
9538               (if unread
9539                   (progn
9540                     (while arts
9541                       (and (gnus-data-unread-p (car arts))
9542                            (setq result (car arts)
9543                                  arts nil))
9544                       (setq arts (cdr arts)))
9545                     result)
9546                 (car arts)))
9547         (progn
9548           (goto-char (gnus-data-pos result))
9549           (gnus-data-number result)))))
9550
9551 (defun gnus-summary-find-subject (subject &optional unread backward article)
9552   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9553          (article (or article (gnus-summary-article-number)))
9554          (articles (gnus-data-list backward))
9555          (arts (gnus-data-find-list article articles))
9556          result)
9557     (when (or (not gnus-summary-check-current)
9558               (not unread)
9559               (not (gnus-data-unread-p (car arts))))
9560       (setq arts (cdr arts)))
9561     (while arts
9562       (and (or (not unread)
9563                (gnus-data-unread-p (car arts)))
9564            (vectorp (gnus-data-header (car arts)))
9565            (gnus-subject-equal
9566             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9567            (setq result (car arts)
9568                  arts nil))
9569       (setq arts (cdr arts)))
9570     (and result
9571          (goto-char (gnus-data-pos result))
9572          (gnus-data-number result))))
9573
9574 (defun gnus-summary-search-forward (&optional unread subject backward)
9575   "Search forward for an article.
9576 If UNREAD, look for unread articles.  If SUBJECT, look for
9577 articles with that subject.  If BACKWARD, search backward instead."
9578   (cond (subject (gnus-summary-find-subject subject unread backward))
9579         (backward (gnus-summary-find-prev unread))
9580         (t (gnus-summary-find-next unread))))
9581
9582 (defun gnus-recenter (&optional n)
9583   "Center point in window and redisplay frame.
9584 Also do horizontal recentering."
9585   (interactive "P")
9586   (when (and gnus-auto-center-summary
9587              (not (eq gnus-auto-center-summary 'vertical)))
9588     (gnus-horizontal-recenter))
9589   (recenter n))
9590
9591 (defun gnus-summary-recenter ()
9592   "Center point in the summary window.
9593 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9594 displayed, no centering will be performed."
9595   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9596   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9597   (let* ((top (cond ((< (window-height) 4) 0)
9598                     ((< (window-height) 7) 1)
9599                     (t 2)))
9600          (height (1- (window-height)))
9601          (bottom (save-excursion (goto-char (point-max))
9602                                  (forward-line (- height))
9603                                  (point)))
9604          (window (get-buffer-window (current-buffer))))
9605     ;; The user has to want it.
9606     (when gnus-auto-center-summary
9607       (when (get-buffer-window gnus-article-buffer)
9608        ;; Only do recentering when the article buffer is displayed,
9609        ;; Set the window start to either `bottom', which is the biggest
9610        ;; possible valid number, or the second line from the top,
9611        ;; whichever is the least.
9612        (set-window-start
9613         window (min bottom (save-excursion 
9614                              (forward-line (- top)) (point)))))
9615       ;; Do horizontal recentering while we're at it.
9616       (when (and (get-buffer-window (current-buffer) t)
9617                  (not (eq gnus-auto-center-summary 'vertical)))
9618         (let ((selected (selected-window)))
9619           (select-window (get-buffer-window (current-buffer) t))
9620           (gnus-summary-position-point)
9621           (gnus-horizontal-recenter)
9622           (select-window selected))))))
9623
9624 (defun gnus-horizontal-recenter ()
9625   "Recenter the current buffer horizontally."
9626   (if (< (current-column) (/ (window-width) 2))
9627       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9628     (let* ((orig (point))
9629            (end (window-end (get-buffer-window (current-buffer) t)))
9630            (max 0))
9631       ;; Find the longest line currently displayed in the window.
9632       (goto-char (window-start))
9633       (while (and (not (eobp)) 
9634                   (< (point) end))
9635         (end-of-line)
9636         (setq max (max max (current-column)))
9637         (forward-line 1))
9638       (goto-char orig)
9639       ;; Scroll horizontally to center (sort of) the point.
9640       (if (> max (window-width))
9641           (set-window-hscroll 
9642            (get-buffer-window (current-buffer) t)
9643            (min (- (current-column) (/ (window-width) 3))
9644                 (+ 2 (- max (window-width)))))
9645         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9646       max)))
9647
9648 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9649 (defun gnus-short-group-name (group &optional levels)
9650   "Collapse GROUP name LEVELS."
9651   (let* ((name "") 
9652          (foreign "")
9653          (depth 0) 
9654          (skip 1)
9655          (levels (or levels
9656                      (progn
9657                        (while (string-match "\\." group skip)
9658                          (setq skip (match-end 0)
9659                                depth (+ depth 1)))
9660                        depth))))
9661     (if (string-match ":" group)
9662         (setq foreign (substring group 0 (match-end 0))
9663               group (substring group (match-end 0))))
9664     (while group
9665       (if (and (string-match "\\." group)
9666                (> levels (- gnus-group-uncollapsed-levels 1)))
9667           (setq name (concat name (substring group 0 1))
9668                 group (substring group (match-end 0))
9669                 levels (- levels 1)
9670                 name (concat name "."))
9671         (setq name (concat foreign name group)
9672               group nil)))
9673     name))
9674
9675 (defun gnus-summary-jump-to-group (newsgroup)
9676   "Move point to NEWSGROUP in group mode buffer."
9677   ;; Keep update point of group mode buffer if visible.
9678   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9679       (save-window-excursion
9680         ;; Take care of tree window mode.
9681         (if (get-buffer-window gnus-group-buffer)
9682             (pop-to-buffer gnus-group-buffer))
9683         (gnus-group-jump-to-group newsgroup))
9684     (save-excursion
9685       ;; Take care of tree window mode.
9686       (if (get-buffer-window gnus-group-buffer)
9687           (pop-to-buffer gnus-group-buffer)
9688         (set-buffer gnus-group-buffer))
9689       (gnus-group-jump-to-group newsgroup))))
9690
9691 ;; This function returns a list of article numbers based on the
9692 ;; difference between the ranges of read articles in this group and
9693 ;; the range of active articles.
9694 (defun gnus-list-of-unread-articles (group)
9695   (let* ((read (gnus-info-read (gnus-get-info group)))
9696          (active (gnus-active group))
9697          (last (cdr active))
9698          first nlast unread)
9699     ;; If none are read, then all are unread.
9700     (if (not read)
9701         (setq first (car active))
9702       ;; If the range of read articles is a single range, then the
9703       ;; first unread article is the article after the last read
9704       ;; article.  Sounds logical, doesn't it?
9705       (if (not (listp (cdr read)))
9706           (setq first (1+ (cdr read)))
9707         ;; `read' is a list of ranges.
9708         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9709                                 (caar read))) 1)
9710             (setq first 1))
9711         (while read
9712           (if first
9713               (while (< first nlast)
9714                 (setq unread (cons first unread))
9715                 (setq first (1+ first))))
9716           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9717           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9718           (setq read (cdr read)))))
9719     ;; And add the last unread articles.
9720     (while (<= first last)
9721       (setq unread (cons first unread))
9722       (setq first (1+ first)))
9723     ;; Return the list of unread articles.
9724     (nreverse unread)))
9725
9726 (defun gnus-list-of-read-articles (group)
9727   "Return a list of unread, unticked and non-dormant articles."
9728   (let* ((info (gnus-get-info group))
9729          (marked (gnus-info-marks info))
9730          (active (gnus-active group)))
9731     (and info active
9732          (gnus-set-difference
9733           (gnus-sorted-complement
9734            (gnus-uncompress-range active)
9735            (gnus-list-of-unread-articles group))
9736           (append
9737            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9738            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9739
9740 ;; Various summary commands
9741
9742 (defun gnus-summary-universal-argument (arg)
9743   "Perform any operation on all articles that are process/prefixed."
9744   (interactive "P")
9745   (gnus-set-global-variables)
9746   (let ((articles (gnus-summary-work-articles arg))
9747         func article)
9748     (if (eq
9749          (setq
9750           func
9751           (key-binding
9752            (read-key-sequence
9753             (substitute-command-keys
9754              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9755              ))))
9756          'undefined)
9757         (gnus-error 1 "Undefined key")
9758       (save-excursion
9759         (while articles
9760           (gnus-summary-goto-subject (setq article (pop articles)))
9761           (command-execute func)
9762           (gnus-summary-remove-process-mark article)))))
9763   (gnus-summary-position-point))
9764
9765 (defun gnus-summary-toggle-truncation (&optional arg)
9766   "Toggle truncation of summary lines.
9767 With arg, turn line truncation on iff arg is positive."
9768   (interactive "P")
9769   (setq truncate-lines
9770         (if (null arg) (not truncate-lines)
9771           (> (prefix-numeric-value arg) 0)))
9772   (redraw-display))
9773
9774 (defun gnus-summary-reselect-current-group (&optional all rescan)
9775   "Exit and then reselect the current newsgroup.
9776 The prefix argument ALL means to select all articles."
9777   (interactive "P")
9778   (gnus-set-global-variables)
9779   (let ((current-subject (gnus-summary-article-number))
9780         (group gnus-newsgroup-name))
9781     (setq gnus-newsgroup-begin nil)
9782     (gnus-summary-exit)
9783     ;; We have to adjust the point of group mode buffer because the
9784     ;; current point was moved to the next unread newsgroup by
9785     ;; exiting.
9786     (gnus-summary-jump-to-group group)
9787     (when rescan
9788       (save-excursion
9789         (gnus-group-get-new-news-this-group 1)))
9790     (gnus-group-read-group all t)
9791     (gnus-summary-goto-subject current-subject)))
9792
9793 (defun gnus-summary-rescan-group (&optional all)
9794   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9795   (interactive "P")
9796   (gnus-summary-reselect-current-group all t))
9797
9798 (defun gnus-summary-update-info ()
9799   (let* ((group gnus-newsgroup-name))
9800     (when gnus-newsgroup-kill-headers
9801       (setq gnus-newsgroup-killed
9802             (gnus-compress-sequence
9803              (nconc
9804               (gnus-set-sorted-intersection
9805                (gnus-uncompress-range gnus-newsgroup-killed)
9806                (setq gnus-newsgroup-unselected
9807                      (sort gnus-newsgroup-unselected '<)))
9808               (setq gnus-newsgroup-unreads
9809                     (sort gnus-newsgroup-unreads '<))) t)))
9810     (unless (listp (cdr gnus-newsgroup-killed))
9811       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9812     (let ((headers gnus-newsgroup-headers))
9813       (run-hooks 'gnus-exit-group-hook)
9814       (unless gnus-save-score
9815         (setq gnus-newsgroup-scored nil))
9816       ;; Set the new ranges of read articles.
9817       (gnus-update-read-articles
9818        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9819       ;; Set the current article marks.
9820       (gnus-update-marks)
9821       ;; Do the cross-ref thing.
9822       (when gnus-use-cross-reference
9823         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9824       ;; Do adaptive scoring, and possibly save score files.
9825       (when gnus-newsgroup-adaptive
9826         (gnus-score-adaptive))
9827       (when gnus-use-scoring
9828         (gnus-score-save))
9829       ;; Do not switch windows but change the buffer to work.
9830       (set-buffer gnus-group-buffer)
9831       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9832           (gnus-group-update-group group)))))
9833
9834 (defun gnus-summary-exit (&optional temporary)
9835   "Exit reading current newsgroup, and then return to group selection mode.
9836 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9837   (interactive)
9838   (gnus-set-global-variables)
9839   (gnus-kill-save-kill-buffer)
9840   (let* ((group gnus-newsgroup-name)
9841          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9842          (mode major-mode)
9843          (buf (current-buffer)))
9844     (run-hooks 'gnus-summary-prepare-exit-hook)
9845     ;; If we have several article buffers, we kill them at exit.
9846     (unless gnus-single-article-buffer
9847       (gnus-kill-buffer gnus-original-article-buffer)
9848       (setq gnus-article-current nil))
9849     (when gnus-use-cache
9850       (gnus-cache-possibly-remove-articles)
9851       (gnus-cache-save-buffers))
9852     (when gnus-use-trees
9853       (gnus-tree-close group))
9854     ;; Make all changes in this group permanent.
9855     (unless quit-config
9856       (gnus-summary-update-info))
9857     (gnus-close-group group)
9858     ;; Make sure where I was, and go to next newsgroup.
9859     (set-buffer gnus-group-buffer)
9860     (unless quit-config
9861       (gnus-group-jump-to-group group)
9862       (gnus-group-next-unread-group 1))
9863     (run-hooks 'gnus-summary-exit-hook)
9864     (if temporary
9865         nil                             ;Nothing to do.
9866       ;; If we have several article buffers, we kill them at exit.
9867       (unless gnus-single-article-buffer
9868         (gnus-kill-buffer gnus-article-buffer)
9869         (gnus-kill-buffer gnus-original-article-buffer)
9870         (setq gnus-article-current nil))
9871       (set-buffer buf)
9872       (if (not gnus-kill-summary-on-exit)
9873           (gnus-deaden-summary)
9874         ;; We set all buffer-local variables to nil.  It is unclear why
9875         ;; this is needed, but if we don't, buffer-local variables are
9876         ;; not garbage-collected, it seems.  This would the lead to en
9877         ;; ever-growing Emacs.
9878         (gnus-summary-clear-local-variables)
9879         (when (get-buffer gnus-article-buffer)
9880           (bury-buffer gnus-article-buffer))
9881         ;; We clear the global counterparts of the buffer-local
9882         ;; variables as well, just to be on the safe side.
9883         (gnus-configure-windows 'group 'force)
9884         (gnus-summary-clear-local-variables)
9885         ;; Return to group mode buffer.
9886         (if (eq mode 'gnus-summary-mode)
9887             (gnus-kill-buffer buf)))
9888       (setq gnus-current-select-method gnus-select-method)
9889       (pop-to-buffer gnus-group-buffer)
9890       ;; Clear the current group name.
9891       (if (not quit-config)
9892           (progn
9893             (gnus-group-jump-to-group group)
9894             (gnus-group-next-unread-group 1)
9895             (gnus-configure-windows 'group 'force))
9896         (if (not (buffer-name (car quit-config)))
9897             (gnus-configure-windows 'group 'force)
9898           (set-buffer (car quit-config))
9899           (and (eq major-mode 'gnus-summary-mode)
9900                (gnus-set-global-variables))
9901           (gnus-configure-windows (cdr quit-config))))
9902       (unless quit-config
9903         (setq gnus-newsgroup-name nil)))))
9904
9905 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9906 (defun gnus-summary-exit-no-update (&optional no-questions)
9907   "Quit reading current newsgroup without updating read article info."
9908   (interactive)
9909   (gnus-set-global-variables)
9910   (let* ((group gnus-newsgroup-name)
9911          (quit-config (gnus-group-quit-config group)))
9912     (when (or no-questions
9913               gnus-expert-user
9914               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9915       ;; If we have several article buffers, we kill them at exit.
9916       (unless gnus-single-article-buffer
9917         (gnus-kill-buffer gnus-article-buffer)
9918         (gnus-kill-buffer gnus-original-article-buffer)
9919         (setq gnus-article-current nil))
9920       (if (not gnus-kill-summary-on-exit)
9921           (gnus-deaden-summary)
9922         (gnus-close-group group)
9923         (gnus-summary-clear-local-variables)
9924         (set-buffer gnus-group-buffer)
9925         (gnus-summary-clear-local-variables)
9926         (when (get-buffer gnus-summary-buffer)
9927           (kill-buffer gnus-summary-buffer)))
9928       (unless gnus-single-article-buffer
9929         (setq gnus-article-current nil))
9930       (when gnus-use-trees
9931         (gnus-tree-close group))
9932       (when (get-buffer gnus-article-buffer)
9933         (bury-buffer gnus-article-buffer))
9934       ;; Return to the group buffer.
9935       (gnus-configure-windows 'group 'force)
9936       ;; Clear the current group name.
9937       (setq gnus-newsgroup-name nil)
9938       (when (equal (gnus-group-group-name) group)
9939         (gnus-group-next-unread-group 1))
9940       (when quit-config
9941         (if (not (buffer-name (car quit-config)))
9942             (gnus-configure-windows 'group 'force)
9943           (set-buffer (car quit-config))
9944           (when (eq major-mode 'gnus-summary-mode)
9945             (gnus-set-global-variables))
9946           (gnus-configure-windows (cdr quit-config)))))))
9947
9948 ;;; Dead summaries.
9949
9950 (defvar gnus-dead-summary-mode-map nil)
9951
9952 (if gnus-dead-summary-mode-map
9953     nil
9954   (setq gnus-dead-summary-mode-map (make-keymap))
9955   (suppress-keymap gnus-dead-summary-mode-map)
9956   (substitute-key-definition
9957    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9958   (let ((keys '("\C-d" "\r" "\177")))
9959     (while keys
9960       (define-key gnus-dead-summary-mode-map
9961         (pop keys) 'gnus-summary-wake-up-the-dead))))
9962
9963 (defvar gnus-dead-summary-mode nil
9964   "Minor mode for Gnus summary buffers.")
9965
9966 (defun gnus-dead-summary-mode (&optional arg)
9967   "Minor mode for Gnus summary buffers."
9968   (interactive "P")
9969   (when (eq major-mode 'gnus-summary-mode)
9970     (make-local-variable 'gnus-dead-summary-mode)
9971     (setq gnus-dead-summary-mode
9972           (if (null arg) (not gnus-dead-summary-mode)
9973             (> (prefix-numeric-value arg) 0)))
9974     (when gnus-dead-summary-mode
9975       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9976         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9977       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9978         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9979               minor-mode-map-alist)))))
9980
9981 (defun gnus-deaden-summary ()
9982   "Make the current summary buffer into a dead summary buffer."
9983   ;; Kill any previous dead summary buffer.
9984   (when (and gnus-dead-summary
9985              (buffer-name gnus-dead-summary))
9986     (save-excursion
9987       (set-buffer gnus-dead-summary)
9988       (when gnus-dead-summary-mode
9989         (kill-buffer (current-buffer)))))
9990   ;; Make this the current dead summary.
9991   (setq gnus-dead-summary (current-buffer))
9992   (gnus-dead-summary-mode 1)
9993   (let ((name (buffer-name)))
9994     (when (string-match "Summary" name)
9995       (rename-buffer
9996        (concat (substring name 0 (match-beginning 0)) "Dead "
9997                (substring name (match-beginning 0))) t))))
9998
9999 (defun gnus-kill-or-deaden-summary (buffer)
10000   "Kill or deaden the summary BUFFER."
10001   (when (and (buffer-name buffer)
10002              (not gnus-single-article-buffer))
10003     (save-excursion
10004       (set-buffer buffer)
10005       (gnus-kill-buffer gnus-article-buffer)
10006       (gnus-kill-buffer gnus-original-article-buffer)))
10007   (cond (gnus-kill-summary-on-exit
10008          (when (and gnus-use-trees
10009                     (and (get-buffer buffer)
10010                          (buffer-name (get-buffer buffer))))
10011            (save-excursion
10012              (set-buffer (get-buffer buffer))
10013              (gnus-tree-close gnus-newsgroup-name)))
10014          (gnus-kill-buffer buffer))
10015         ((and (get-buffer buffer)
10016               (buffer-name (get-buffer buffer)))
10017          (save-excursion
10018            (set-buffer buffer)
10019            (gnus-deaden-summary)))))
10020
10021 (defun gnus-summary-wake-up-the-dead (&rest args)
10022   "Wake up the dead summary buffer."
10023   (interactive)
10024   (gnus-dead-summary-mode -1)
10025   (let ((name (buffer-name)))
10026     (when (string-match "Dead " name)
10027       (rename-buffer
10028        (concat (substring name 0 (match-beginning 0))
10029                (substring name (match-end 0))) t)))
10030   (gnus-message 3 "This dead summary is now alive again"))
10031
10032 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
10033 (defun gnus-summary-fetch-faq (&optional faq-dir)
10034   "Fetch the FAQ for the current group.
10035 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
10036 in."
10037   (interactive
10038    (list
10039     (if current-prefix-arg
10040         (completing-read
10041          "Faq dir: " (and (listp gnus-group-faq-directory)
10042                           gnus-group-faq-directory)))))
10043   (let (gnus-faq-buffer)
10044     (and (setq gnus-faq-buffer
10045                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
10046          (gnus-configure-windows 'summary-faq))))
10047
10048 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10049 (defun gnus-summary-describe-group (&optional force)
10050   "Describe the current newsgroup."
10051   (interactive "P")
10052   (gnus-group-describe-group force gnus-newsgroup-name))
10053
10054 (defun gnus-summary-describe-briefly ()
10055   "Describe summary mode commands briefly."
10056   (interactive)
10057   (gnus-message 6
10058                 (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")))
10059
10060 ;; Walking around group mode buffer from summary mode.
10061
10062 (defun gnus-summary-next-group (&optional no-article target-group backward)
10063   "Exit current newsgroup and then select next unread newsgroup.
10064 If prefix argument NO-ARTICLE is non-nil, no article is selected
10065 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
10066 previous group instead."
10067   (interactive "P")
10068   (gnus-set-global-variables)
10069   (let ((current-group gnus-newsgroup-name)
10070         (current-buffer (current-buffer))
10071         entered)
10072     ;; First we semi-exit this group to update Xrefs and all variables.
10073     ;; We can't do a real exit, because the window conf must remain
10074     ;; the same in case the user is prompted for info, and we don't
10075     ;; want the window conf to change before that...
10076     (gnus-summary-exit t)
10077     (while (not entered)
10078       ;; Then we find what group we are supposed to enter.
10079       (set-buffer gnus-group-buffer)
10080       (gnus-group-jump-to-group current-group)
10081       (setq target-group
10082             (or target-group
10083                 (if (eq gnus-keep-same-level 'best)
10084                     (gnus-summary-best-group gnus-newsgroup-name)
10085                   (gnus-summary-search-group backward gnus-keep-same-level))))
10086       (if (not target-group)
10087           ;; There are no further groups, so we return to the group
10088           ;; buffer.
10089           (progn
10090             (gnus-message 5 "Returning to the group buffer")
10091             (setq entered t)
10092             (set-buffer current-buffer)
10093             (gnus-summary-exit))
10094         ;; We try to enter the target group.
10095         (gnus-group-jump-to-group target-group)
10096         (let ((unreads (gnus-group-group-unread)))
10097           (if (and (or (eq t unreads)
10098                        (and unreads (not (zerop unreads))))
10099                    (gnus-summary-read-group
10100                     target-group nil no-article current-buffer))
10101               (setq entered t)
10102             (setq current-group target-group
10103                   target-group nil)))))))
10104
10105 (defun gnus-summary-prev-group (&optional no-article)
10106   "Exit current newsgroup and then select previous unread newsgroup.
10107 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10108   (interactive "P")
10109   (gnus-summary-next-group no-article nil t))
10110
10111 ;; Walking around summary lines.
10112
10113 (defun gnus-summary-first-subject (&optional unread)
10114   "Go to the first unread subject.
10115 If UNREAD is non-nil, go to the first unread article.
10116 Returns the article selected or nil if there are no unread articles."
10117   (interactive "P")
10118   (prog1
10119       (cond
10120        ;; Empty summary.
10121        ((null gnus-newsgroup-data)
10122         (gnus-message 3 "No articles in the group")
10123         nil)
10124        ;; Pick the first article.
10125        ((not unread)
10126         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10127         (gnus-data-number (car gnus-newsgroup-data)))
10128        ;; No unread articles.
10129        ((null gnus-newsgroup-unreads)
10130         (gnus-message 3 "No more unread articles")
10131         nil)
10132        ;; Find the first unread article.
10133        (t
10134         (let ((data gnus-newsgroup-data))
10135           (while (and data
10136                       (not (gnus-data-unread-p (car data))))
10137             (setq data (cdr data)))
10138           (if data
10139               (progn
10140                 (goto-char (gnus-data-pos (car data)))
10141                 (gnus-data-number (car data)))))))
10142     (gnus-summary-position-point)))
10143
10144 (defun gnus-summary-next-subject (n &optional unread dont-display)
10145   "Go to next N'th summary line.
10146 If N is negative, go to the previous N'th subject line.
10147 If UNREAD is non-nil, only unread articles are selected.
10148 The difference between N and the actual number of steps taken is
10149 returned."
10150   (interactive "p")
10151   (let ((backward (< n 0))
10152         (n (abs n)))
10153     (while (and (> n 0)
10154                 (if backward
10155                     (gnus-summary-find-prev unread)
10156                   (gnus-summary-find-next unread)))
10157       (setq n (1- n)))
10158     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10159                                (if unread " unread" "")))
10160     (unless dont-display
10161       (gnus-summary-recenter)
10162       (gnus-summary-position-point))
10163     n))
10164
10165 (defun gnus-summary-next-unread-subject (n)
10166   "Go to next N'th unread summary line."
10167   (interactive "p")
10168   (gnus-summary-next-subject n t))
10169
10170 (defun gnus-summary-prev-subject (n &optional unread)
10171   "Go to previous N'th summary line.
10172 If optional argument UNREAD is non-nil, only unread article is selected."
10173   (interactive "p")
10174   (gnus-summary-next-subject (- n) unread))
10175
10176 (defun gnus-summary-prev-unread-subject (n)
10177   "Go to previous N'th unread summary line."
10178   (interactive "p")
10179   (gnus-summary-next-subject (- n) t))
10180
10181 (defun gnus-summary-goto-subject (article &optional force silent)
10182   "Go the subject line of ARTICLE.
10183 If FORCE, also allow jumping to articles not currently shown."
10184   (let ((b (point))
10185         (data (gnus-data-find article)))
10186     ;; We read in the article if we have to.
10187     (and (not data)
10188          force
10189          (gnus-summary-insert-subject article)
10190          (setq data (gnus-data-find article)))
10191     (goto-char b)
10192     (if (not data)
10193         (progn
10194           (unless silent
10195             (gnus-message 3 "Can't find article %d" article))
10196           nil)
10197       (goto-char (gnus-data-pos data))
10198       article)))
10199
10200 ;; Walking around summary lines with displaying articles.
10201
10202 (defun gnus-summary-expand-window (&optional arg)
10203   "Make the summary buffer take up the entire Emacs frame.
10204 Given a prefix, will force an `article' buffer configuration."
10205   (interactive "P")
10206   (gnus-set-global-variables)
10207   (if arg
10208       (gnus-configure-windows 'article 'force)
10209     (gnus-configure-windows 'summary 'force)))
10210
10211 (defun gnus-summary-display-article (article &optional all-header)
10212   "Display ARTICLE in article buffer."
10213   (gnus-set-global-variables)
10214   (if (null article)
10215       nil
10216     (prog1
10217         (if gnus-summary-display-article-function
10218             (funcall gnus-summary-display-article-function article all-header)
10219           (gnus-article-prepare article all-header))
10220       (run-hooks 'gnus-select-article-hook)
10221       (unless (zerop gnus-current-article)
10222         (gnus-summary-goto-subject gnus-current-article))
10223       (gnus-summary-recenter)
10224       (when gnus-use-trees
10225         (gnus-possibly-generate-tree article)
10226         (gnus-highlight-selected-tree article))
10227       ;; Successfully display article.
10228       (gnus-article-set-window-start
10229        (cdr (assq article gnus-newsgroup-bookmarks))))))
10230
10231 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10232   "Select the current article.
10233 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10234 non-nil, the article will be re-fetched even if it already present in
10235 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10236 be displayed."
10237   ;; Make sure we are in the summary buffer to work around bbdb bug.
10238   (unless (eq major-mode 'gnus-summary-mode)
10239     (set-buffer gnus-summary-buffer))
10240   (let ((article (or article (gnus-summary-article-number)))
10241         (all-headers (not (not all-headers))) ;Must be T or NIL.
10242         gnus-summary-display-article-function
10243         did)
10244     (and (not pseudo)
10245          (gnus-summary-article-pseudo-p article)
10246          (error "This is a pseudo-article."))
10247     (prog1
10248         (save-excursion
10249           (set-buffer gnus-summary-buffer)
10250           (if (or (and gnus-single-article-buffer
10251                        (or (null gnus-current-article)
10252                            (null gnus-article-current)
10253                            (null (get-buffer gnus-article-buffer))
10254                            (not (eq article (cdr gnus-article-current)))
10255                            (not (equal (car gnus-article-current)
10256                                        gnus-newsgroup-name))))
10257                   (and (not gnus-single-article-buffer)
10258                        (or (null gnus-current-article)
10259                            (not (eq gnus-current-article article))))
10260                   force)
10261               ;; The requested article is different from the current article.
10262               (prog1
10263                   (gnus-summary-display-article article all-headers)
10264                 (setq did article))
10265             (if (or all-headers gnus-show-all-headers)
10266                 (gnus-article-show-all-headers))
10267             'old))
10268       (if did
10269           (gnus-article-set-window-start
10270            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10271
10272 (defun gnus-summary-set-current-mark (&optional current-mark)
10273   "Obsolete function."
10274   nil)
10275
10276 (defun gnus-summary-next-article (&optional unread subject backward push)
10277   "Select the next article.
10278 If UNREAD, only unread articles are selected.
10279 If SUBJECT, only articles with SUBJECT are selected.
10280 If BACKWARD, the previous article is selected instead of the next."
10281   (interactive "P")
10282   (gnus-set-global-variables)
10283   (cond
10284    ;; Is there such an article?
10285    ((and (gnus-summary-search-forward unread subject backward)
10286          (or (gnus-summary-display-article (gnus-summary-article-number))
10287              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10288     (gnus-summary-position-point))
10289    ;; If not, we try the first unread, if that is wanted.
10290    ((and subject
10291          gnus-auto-select-same
10292          (or (gnus-summary-first-unread-article)
10293              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10294     (gnus-summary-position-point)
10295     (gnus-message 6 "Wrapped"))
10296    ;; Try to get next/previous article not displayed in this group.
10297    ((and gnus-auto-extend-newsgroup
10298          (not unread) (not subject))
10299     (gnus-summary-goto-article
10300      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10301      nil t))
10302    ;; Go to next/previous group.
10303    (t
10304     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10305         (gnus-summary-jump-to-group gnus-newsgroup-name))
10306     (let ((cmd last-command-char)
10307           (group
10308            (if (eq gnus-keep-same-level 'best)
10309                (gnus-summary-best-group gnus-newsgroup-name)
10310              (gnus-summary-search-group backward gnus-keep-same-level))))
10311       ;; For some reason, the group window gets selected.  We change
10312       ;; it back.
10313       (select-window (get-buffer-window (current-buffer)))
10314       ;; Select next unread newsgroup automagically.
10315       (cond
10316        ((not gnus-auto-select-next)
10317         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10318        ((or (eq gnus-auto-select-next 'quietly)
10319             (and (eq gnus-auto-select-next 'slightly-quietly)
10320                  push)
10321             (and (eq gnus-auto-select-next 'almost-quietly)
10322                  (gnus-summary-last-article-p)))
10323         ;; Select quietly.
10324         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10325             (gnus-summary-exit)
10326           (gnus-message 7 "No more%s articles (%s)..."
10327                         (if unread " unread" "")
10328                         (if group (concat "selecting " group)
10329                           "exiting"))
10330           (gnus-summary-next-group nil group backward)))
10331        (t
10332         (gnus-summary-walk-group-buffer
10333          gnus-newsgroup-name cmd unread backward)))))))
10334
10335 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10336   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10337                       (?\C-p (gnus-group-prev-unread-group 1))))
10338         keve key group ended)
10339     (save-excursion
10340       (set-buffer gnus-group-buffer)
10341       (gnus-summary-jump-to-group from-group)
10342       (setq group
10343             (if (eq gnus-keep-same-level 'best)
10344                 (gnus-summary-best-group gnus-newsgroup-name)
10345               (gnus-summary-search-group backward gnus-keep-same-level))))
10346     (while (not ended)
10347       (gnus-message
10348        5 "No more%s articles%s" (if unread " unread" "")
10349        (if (and group
10350                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10351            (format " (Type %s for %s [%s])"
10352                    (single-key-description cmd) group
10353                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10354          (format " (Type %s to exit %s)"
10355                  (single-key-description cmd)
10356                  gnus-newsgroup-name)))
10357       ;; Confirm auto selection.
10358       (setq key (car (setq keve (gnus-read-event-char))))
10359       (setq ended t)
10360       (cond
10361        ((assq key keystrokes)
10362         (let ((obuf (current-buffer)))
10363           (switch-to-buffer gnus-group-buffer)
10364           (and group
10365                (gnus-group-jump-to-group group))
10366           (eval (cadr (assq key keystrokes)))
10367           (setq group (gnus-group-group-name))
10368           (switch-to-buffer obuf))
10369         (setq ended nil))
10370        ((equal key cmd)
10371         (if (or (not group)
10372                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10373             (gnus-summary-exit)
10374           (gnus-summary-next-group nil group backward)))
10375        (t
10376         (push (cdr keve) unread-command-events))))))
10377
10378 (defun gnus-read-event-char ()
10379   "Get the next event."
10380   (let ((event (read-event)))
10381     (cons (and (numberp event) event) event)))
10382
10383 (defun gnus-summary-next-unread-article ()
10384   "Select unread article after current one."
10385   (interactive)
10386   (gnus-summary-next-article t (and gnus-auto-select-same
10387                                     (gnus-summary-article-subject))))
10388
10389 (defun gnus-summary-prev-article (&optional unread subject)
10390   "Select the article after the current one.
10391 If UNREAD is non-nil, only unread articles are selected."
10392   (interactive "P")
10393   (gnus-summary-next-article unread subject t))
10394
10395 (defun gnus-summary-prev-unread-article ()
10396   "Select unred article before current one."
10397   (interactive)
10398   (gnus-summary-prev-article t (and gnus-auto-select-same
10399                                     (gnus-summary-article-subject))))
10400
10401 (defun gnus-summary-next-page (&optional lines circular)
10402   "Show next page of the selected article.
10403 If at the end of the current article, select the next article.
10404 LINES says how many lines should be scrolled up.
10405
10406 If CIRCULAR is non-nil, go to the start of the article instead of
10407 selecting the next article when reaching the end of the current
10408 article."
10409   (interactive "P")
10410   (setq gnus-summary-buffer (current-buffer))
10411   (gnus-set-global-variables)
10412   (let ((article (gnus-summary-article-number))
10413         (endp nil))
10414     (gnus-configure-windows 'article)
10415     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10416         (if (and (eq gnus-summary-goto-unread 'never)
10417                  (not (gnus-summary-last-article-p article)))
10418             (gnus-summary-next-article)
10419           (gnus-summary-next-unread-article))
10420       (if (or (null gnus-current-article)
10421               (null gnus-article-current)
10422               (/= article (cdr gnus-article-current))
10423               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10424           ;; Selected subject is different from current article's.
10425           (gnus-summary-display-article article)
10426         (gnus-eval-in-buffer-window
10427          gnus-article-buffer
10428          (setq endp (gnus-article-next-page lines)))
10429         (if endp
10430             (cond (circular
10431                    (gnus-summary-beginning-of-article))
10432                   (lines
10433                    (gnus-message 3 "End of message"))
10434                   ((null lines)
10435                    (if (and (eq gnus-summary-goto-unread 'never)
10436                             (not (gnus-summary-last-article-p article)))
10437                        (gnus-summary-next-article)
10438                      (gnus-summary-next-unread-article)))))))
10439     (gnus-summary-recenter)
10440     (gnus-summary-position-point)))
10441
10442 (defun gnus-summary-prev-page (&optional lines)
10443   "Show previous page of selected article.
10444 Argument LINES specifies lines to be scrolled down."
10445   (interactive "P")
10446   (gnus-set-global-variables)
10447   (let ((article (gnus-summary-article-number)))
10448     (gnus-configure-windows 'article)
10449     (if (or (null gnus-current-article)
10450             (null gnus-article-current)
10451             (/= article (cdr gnus-article-current))
10452             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10453         ;; Selected subject is different from current article's.
10454         (gnus-summary-display-article article)
10455       (gnus-summary-recenter)
10456       (gnus-eval-in-buffer-window gnus-article-buffer
10457                                   (gnus-article-prev-page lines))))
10458   (gnus-summary-position-point))
10459
10460 (defun gnus-summary-scroll-up (lines)
10461   "Scroll up (or down) one line current article.
10462 Argument LINES specifies lines to be scrolled up (or down if negative)."
10463   (interactive "p")
10464   (gnus-set-global-variables)
10465   (gnus-configure-windows 'article)
10466   (gnus-summary-show-thread)
10467   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10468     (gnus-eval-in-buffer-window
10469      gnus-article-buffer
10470      (cond ((> lines 0)
10471             (if (gnus-article-next-page lines)
10472                 (gnus-message 3 "End of message")))
10473            ((< lines 0)
10474             (gnus-article-prev-page (- lines))))))
10475   (gnus-summary-recenter)
10476   (gnus-summary-position-point))
10477
10478 (defun gnus-summary-next-same-subject ()
10479   "Select next article which has the same subject as current one."
10480   (interactive)
10481   (gnus-set-global-variables)
10482   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10483
10484 (defun gnus-summary-prev-same-subject ()
10485   "Select previous article which has the same subject as current one."
10486   (interactive)
10487   (gnus-set-global-variables)
10488   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10489
10490 (defun gnus-summary-next-unread-same-subject ()
10491   "Select next unread article which has the same subject as current one."
10492   (interactive)
10493   (gnus-set-global-variables)
10494   (gnus-summary-next-article t (gnus-summary-article-subject)))
10495
10496 (defun gnus-summary-prev-unread-same-subject ()
10497   "Select previous unread article which has the same subject as current one."
10498   (interactive)
10499   (gnus-set-global-variables)
10500   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10501
10502 (defun gnus-summary-first-unread-article ()
10503   "Select the first unread article.
10504 Return nil if there are no unread articles."
10505   (interactive)
10506   (gnus-set-global-variables)
10507   (prog1
10508       (if (gnus-summary-first-subject t)
10509           (progn
10510             (gnus-summary-show-thread)
10511             (gnus-summary-first-subject t)
10512             (gnus-summary-display-article (gnus-summary-article-number))))
10513     (gnus-summary-position-point)))
10514
10515 (defun gnus-summary-best-unread-article ()
10516   "Select the unread article with the highest score."
10517   (interactive)
10518   (gnus-set-global-variables)
10519   (let ((best -1000000)
10520         (data gnus-newsgroup-data)
10521         article score)
10522     (while data
10523       (and (gnus-data-unread-p (car data))
10524            (> (setq score
10525                     (gnus-summary-article-score (gnus-data-number (car data))))
10526               best)
10527            (setq best score
10528                  article (gnus-data-number (car data))))
10529       (setq data (cdr data)))
10530     (prog1
10531         (if article
10532             (gnus-summary-goto-article article)
10533           (error "No unread articles"))
10534       (gnus-summary-position-point))))
10535
10536 (defun gnus-summary-last-subject ()
10537   "Go to the last displayed subject line in the group."
10538   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10539     (when article
10540       (gnus-summary-goto-subject article))))
10541
10542 (defun gnus-summary-goto-article (article &optional all-headers force)
10543   "Fetch ARTICLE and display it if it exists.
10544 If ALL-HEADERS is non-nil, no header lines are hidden."
10545   (interactive
10546    (list
10547     (string-to-int
10548      (completing-read
10549       "Article number: "
10550       (mapcar (lambda (number) (list (int-to-string number)))
10551               gnus-newsgroup-limit)))
10552     current-prefix-arg
10553     t))
10554   (prog1
10555       (if (gnus-summary-goto-subject article force)
10556           (gnus-summary-display-article article all-headers)
10557         (gnus-message 4 "Couldn't go to article %s" article) nil)
10558     (gnus-summary-position-point)))
10559
10560 (defun gnus-summary-goto-last-article ()
10561   "Go to the previously read article."
10562   (interactive)
10563   (prog1
10564       (and gnus-last-article
10565            (gnus-summary-goto-article gnus-last-article))
10566     (gnus-summary-position-point)))
10567
10568 (defun gnus-summary-pop-article (number)
10569   "Pop one article off the history and go to the previous.
10570 NUMBER articles will be popped off."
10571   (interactive "p")
10572   (let (to)
10573     (setq gnus-newsgroup-history
10574           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10575     (if to
10576         (gnus-summary-goto-article (car to))
10577       (error "Article history empty")))
10578   (gnus-summary-position-point))
10579
10580 ;; Summary commands and functions for limiting the summary buffer.
10581
10582 (defun gnus-summary-limit-to-articles (n)
10583   "Limit the summary buffer to the next N articles.
10584 If not given a prefix, use the process marked articles instead."
10585   (interactive "P")
10586   (gnus-set-global-variables)
10587   (prog1
10588       (let ((articles (gnus-summary-work-articles n)))
10589         (setq gnus-newsgroup-processable nil)
10590         (gnus-summary-limit articles))
10591     (gnus-summary-position-point)))
10592
10593 (defun gnus-summary-pop-limit (&optional total)
10594   "Restore the previous limit.
10595 If given a prefix, remove all limits."
10596   (interactive "P")
10597   (gnus-set-global-variables)
10598   (when total 
10599     (setq gnus-newsgroup-limits
10600           (list (mapcar (lambda (h) (mail-header-number h))
10601                         gnus-newsgroup-headers))))
10602   (unless gnus-newsgroup-limits
10603     (error "No limit to pop"))
10604   (prog1
10605       (gnus-summary-limit nil 'pop)
10606     (gnus-summary-position-point)))
10607
10608 (defun gnus-summary-limit-to-subject (subject &optional header)
10609   "Limit the summary buffer to articles that have subjects that match a regexp."
10610   (interactive "sRegexp: ")
10611   (unless header
10612     (setq header "subject"))
10613   (when (not (equal "" subject))
10614     (prog1
10615         (let ((articles (gnus-summary-find-matching
10616                          (or header "subject") subject 'all)))
10617           (or articles (error "Found no matches for \"%s\"" subject))
10618           (gnus-summary-limit articles))
10619       (gnus-summary-position-point))))
10620
10621 (defun gnus-summary-limit-to-author (from)
10622   "Limit the summary buffer to articles that have authors that match a regexp."
10623   (interactive "sRegexp: ")
10624   (gnus-summary-limit-to-subject from "from"))
10625
10626 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10627 (make-obsolete
10628  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10629
10630 (defun gnus-summary-limit-to-unread (&optional all)
10631   "Limit the summary buffer to articles that are not marked as read.
10632 If ALL is non-nil, limit strictly to unread articles."
10633   (interactive "P")
10634   (if all
10635       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10636     (gnus-summary-limit-to-marks
10637      ;; Concat all the marks that say that an article is read and have
10638      ;; those removed.
10639      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10640            gnus-killed-mark gnus-kill-file-mark
10641            gnus-low-score-mark gnus-expirable-mark
10642            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10643      'reverse)))
10644
10645 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10646 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10647
10648 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10649   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10650 If REVERSE, limit the summary buffer to articles that are not marked
10651 with MARKS.  MARKS can either be a string of marks or a list of marks.
10652 Returns how many articles were removed."
10653   (interactive "sMarks: ")
10654   (gnus-set-global-variables)
10655   (prog1
10656       (let ((data gnus-newsgroup-data)
10657             (marks (if (listp marks) marks
10658                      (append marks nil))) ; Transform to list.
10659             articles)
10660         (while data
10661           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10662                  (memq (gnus-data-mark (car data)) marks))
10663                (setq articles (cons (gnus-data-number (car data)) articles)))
10664           (setq data (cdr data)))
10665         (gnus-summary-limit articles))
10666     (gnus-summary-position-point)))
10667
10668 (defun gnus-summary-limit-to-score (&optional score)
10669   "Limit to articles with score at or above SCORE."
10670   (interactive "P")
10671   (gnus-set-global-variables)
10672   (setq score (if score
10673                   (prefix-numeric-value score)
10674                 (or gnus-summary-default-score 0)))
10675   (let ((data gnus-newsgroup-data)
10676         articles)
10677     (while data
10678       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10679                 score)
10680         (push (gnus-data-number (car data)) articles))
10681       (setq data (cdr data)))
10682     (prog1
10683         (gnus-summary-limit articles)
10684       (gnus-summary-position-point))))
10685
10686 (defun gnus-summary-limit-include-dormant ()
10687   "Display all the hidden articles that are marked as dormant."
10688   (interactive)
10689   (gnus-set-global-variables)
10690   (or gnus-newsgroup-dormant
10691       (error "There are no dormant articles in this group"))
10692   (prog1
10693       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10694     (gnus-summary-position-point)))
10695
10696 (defun gnus-summary-limit-exclude-dormant ()
10697   "Hide all dormant articles."
10698   (interactive)
10699   (gnus-set-global-variables)
10700   (prog1
10701       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10702     (gnus-summary-position-point)))
10703
10704 (defun gnus-summary-limit-exclude-childless-dormant ()
10705   "Hide all dormant articles that have no children."
10706   (interactive)
10707   (gnus-set-global-variables)
10708   (let ((data (gnus-data-list t))
10709         articles d children)
10710     ;; Find all articles that are either not dormant or have
10711     ;; children.
10712     (while (setq d (pop data))
10713       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10714                 (and (setq children 
10715                            (gnus-article-children (gnus-data-number d)))
10716                      (let (found)
10717                        (while children
10718                          (when (memq (car children) articles)
10719                            (setq children nil
10720                                  found t))
10721                          (pop children))
10722                        found)))
10723         (push (gnus-data-number d) articles)))
10724     ;; Do the limiting.
10725     (prog1
10726         (gnus-summary-limit articles)
10727       (gnus-summary-position-point))))
10728
10729 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10730   "Mark all unread excluded articles as read.
10731 If ALL, mark even excluded ticked and dormants as read."
10732   (interactive "P")
10733   (let ((articles (gnus-sorted-complement
10734                    (sort
10735                     (mapcar (lambda (h) (mail-header-number h))
10736                             gnus-newsgroup-headers)
10737                     '<)
10738                    (sort gnus-newsgroup-limit '<)))
10739         article)
10740     (setq gnus-newsgroup-unreads nil)
10741     (if all
10742         (setq gnus-newsgroup-dormant nil
10743               gnus-newsgroup-marked nil
10744               gnus-newsgroup-reads
10745               (nconc
10746                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10747                gnus-newsgroup-reads))
10748       (while (setq article (pop articles))
10749         (unless (or (memq article gnus-newsgroup-dormant)
10750                     (memq article gnus-newsgroup-marked))
10751           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10752
10753 (defun gnus-summary-limit (articles &optional pop)
10754   (if pop
10755       ;; We pop the previous limit off the stack and use that.
10756       (setq articles (car gnus-newsgroup-limits)
10757             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10758     ;; We use the new limit, so we push the old limit on the stack.
10759     (setq gnus-newsgroup-limits
10760           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10761   ;; Set the limit.
10762   (setq gnus-newsgroup-limit articles)
10763   (let ((total (length gnus-newsgroup-data))
10764         (data (gnus-data-find-list (gnus-summary-article-number)))
10765         found)
10766     ;; This will do all the work of generating the new summary buffer
10767     ;; according to the new limit.
10768     (gnus-summary-prepare)
10769     ;; Hide any threads, possibly.
10770     (and gnus-show-threads
10771          gnus-thread-hide-subtree
10772          (gnus-summary-hide-all-threads))
10773     ;; Try to return to the article you were at, or one in the
10774     ;; neighborhood.
10775     (if data
10776         ;; We try to find some article after the current one.
10777         (while data
10778           (and (gnus-summary-goto-subject
10779                 (gnus-data-number (car data)) nil t)
10780                (setq data nil
10781                      found t))
10782           (setq data (cdr data))))
10783     (or found
10784         ;; If there is no data, that means that we were after the last
10785         ;; article.  The same goes when we can't find any articles
10786         ;; after the current one.
10787         (progn
10788           (goto-char (point-max))
10789           (gnus-summary-find-prev)))
10790     ;; We return how many articles were removed from the summary
10791     ;; buffer as a result of the new limit.
10792     (- total (length gnus-newsgroup-data))))
10793
10794 (defsubst gnus-cut-thread (thread)
10795   "Go forwards in the thread until we find an article that we want to display."
10796   (when (eq gnus-fetch-old-headers 'some)
10797     ;; Deal with old-fetched headers.
10798     (while (and thread
10799                 (memq (mail-header-number (car thread)) 
10800                       gnus-newsgroup-ancient)
10801                 (<= (length (cdr thread)) 1))
10802       (setq thread (cadr thread))))
10803   ;; Deal with sparse threads.
10804   (when (or (eq gnus-build-sparse-threads 'some)
10805             (eq gnus-build-sparse-threads 'more))
10806     (while (and thread
10807                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10808                 (= (length (cdr thread)) 1))
10809       (setq thread (cadr thread))))
10810   thread)
10811
10812 (defun gnus-cut-threads (threads)
10813   "Cut off all uninteresting articles from the beginning of threads."
10814   (when (or (eq gnus-fetch-old-headers 'some)
10815             (eq gnus-build-sparse-threads 'some)
10816             (eq gnus-build-sparse-threads 'more))
10817     (let ((th threads))
10818       (while th
10819         (setcar th (gnus-cut-thread (car th)))
10820         (setq th (cdr th)))))
10821   ;; Remove nixed out threads.
10822   (delq nil threads))
10823
10824 (defun gnus-summary-initial-limit (&optional show-if-empty)
10825   "Figure out what the initial limit is supposed to be on group entry.
10826 This entails weeding out unwanted dormants, low-scored articles,
10827 fetch-old-headers verbiage, and so on."
10828   ;; Most groups have nothing to remove.
10829   (if (or gnus-inhibit-limiting
10830           (and (null gnus-newsgroup-dormant)
10831                (not (eq gnus-fetch-old-headers 'some))
10832                (null gnus-summary-expunge-below)
10833                (not (eq gnus-build-sparse-threads 'some))
10834                (not (eq gnus-build-sparse-threads 'more))
10835                (null gnus-thread-expunge-below)
10836                (not gnus-use-nocem)))
10837       () ; Do nothing.
10838     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10839     (setq gnus-newsgroup-limit nil)
10840     (mapatoms
10841      (lambda (node)
10842        (unless (car (symbol-value node))
10843          ;; These threads have no parents -- they are roots.
10844          (let ((nodes (cdr (symbol-value node)))
10845                thread)
10846            (while nodes
10847              (if (and gnus-thread-expunge-below
10848                       (< (gnus-thread-total-score (car nodes))
10849                          gnus-thread-expunge-below))
10850                  (gnus-expunge-thread (pop nodes))
10851                (setq thread (pop nodes))
10852                (gnus-summary-limit-children thread))))))
10853      gnus-newsgroup-dependencies)
10854     ;; If this limitation resulted in an empty group, we might
10855     ;; pop the previous limit and use it instead.
10856     (when (and (not gnus-newsgroup-limit)
10857                show-if-empty)
10858       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10859     gnus-newsgroup-limit))
10860
10861 (defun gnus-summary-limit-children (thread)
10862   "Return 1 if this subthread is visible and 0 if it is not."
10863   ;; First we get the number of visible children to this thread.  This
10864   ;; is done by recursing down the thread using this function, so this
10865   ;; will really go down to a leaf article first, before slowly
10866   ;; working its way up towards the root.
10867   (when thread
10868     (let ((children
10869            (if (cdr thread)
10870                (apply '+ (mapcar 'gnus-summary-limit-children
10871                                  (cdr thread)))
10872              0))
10873           (number (mail-header-number (car thread)))
10874           score)
10875       (if (or
10876            ;; If this article is dormant and has absolutely no visible
10877            ;; children, then this article isn't visible.
10878            (and (memq number gnus-newsgroup-dormant)
10879                 (= children 0))
10880            ;; If this is a "fetch-old-headered" and there is only one
10881            ;; visible child (or less), then we don't want this article.
10882            (and (eq gnus-fetch-old-headers 'some)
10883                 (memq number gnus-newsgroup-ancient)
10884                 (zerop children))
10885            ;; If this is a sparsely inserted article with no children,
10886            ;; we don't want it.
10887            (and (eq gnus-build-sparse-threads 'some)
10888                 (memq number gnus-newsgroup-sparse)
10889                 (zerop children))
10890            ;; If we use expunging, and this article is really
10891            ;; low-scored, then we don't want this article.
10892            (when (and gnus-summary-expunge-below
10893                       (< (setq score
10894                                (or (cdr (assq number gnus-newsgroup-scored))
10895                                    gnus-summary-default-score))
10896                          gnus-summary-expunge-below))
10897              ;; We increase the expunge-tally here, but that has
10898              ;; nothing to do with the limits, really.
10899              (incf gnus-newsgroup-expunged-tally)
10900              ;; We also mark as read here, if that's wanted.
10901              (when (and gnus-summary-mark-below
10902                         (< score gnus-summary-mark-below))
10903                (setq gnus-newsgroup-unreads
10904                      (delq number gnus-newsgroup-unreads))
10905                (if gnus-newsgroup-auto-expire
10906                    (push number gnus-newsgroup-expirable)
10907                  (push (cons number gnus-low-score-mark)
10908                        gnus-newsgroup-reads)))
10909              t)
10910            (and gnus-use-nocem
10911                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10912           ;; Nope, invisible article.
10913           0
10914         ;; Ok, this article is to be visible, so we add it to the limit
10915         ;; and return 1.
10916         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10917         1))))
10918
10919 (defun gnus-expunge-thread (thread)
10920   "Mark all articles in THREAD as read."
10921   (let* ((number (mail-header-number (car thread))))
10922     (incf gnus-newsgroup-expunged-tally)
10923     ;; We also mark as read here, if that's wanted.
10924     (setq gnus-newsgroup-unreads
10925           (delq number gnus-newsgroup-unreads))
10926     (if gnus-newsgroup-auto-expire
10927         (push number gnus-newsgroup-expirable)
10928       (push (cons number gnus-low-score-mark)
10929             gnus-newsgroup-reads)))
10930   ;; Go recursively through all subthreads.
10931   (mapcar 'gnus-expunge-thread (cdr thread)))
10932
10933 ;; Summary article oriented commands
10934
10935 (defun gnus-summary-refer-parent-article (n)
10936   "Refer parent article N times.
10937 The difference between N and the number of articles fetched is returned."
10938   (interactive "p")
10939   (gnus-set-global-variables)
10940   (while
10941       (and
10942        (> n 0)
10943        (let* ((header (gnus-summary-article-header))
10944               (ref
10945                ;; If we try to find the parent of the currently
10946                ;; displayed article, then we take a look at the actual
10947                ;; References header, since this is slightly more
10948                ;; reliable than the References field we got from the
10949                ;; server.
10950                (if (and (eq (mail-header-number header)
10951                             (cdr gnus-article-current))
10952                         (equal gnus-newsgroup-name
10953                                (car gnus-article-current)))
10954                    (save-excursion
10955                      (set-buffer gnus-original-article-buffer)
10956                      (nnheader-narrow-to-headers)
10957                      (prog1
10958                          (message-fetch-field "references")
10959                        (widen)))
10960                  ;; It's not the current article, so we take a bet on
10961                  ;; the value we got from the server.
10962                  (mail-header-references header))))
10963          (if (setq ref (or ref (mail-header-references header)))
10964              (or (gnus-summary-refer-article (gnus-parent-id ref))
10965                  (gnus-message 1 "Couldn't find parent"))
10966            (gnus-message 1 "No references in article %d"
10967                          (gnus-summary-article-number))
10968            nil)))
10969     (setq n (1- n)))
10970   (gnus-summary-position-point)
10971   n)
10972
10973 (defun gnus-summary-refer-references ()
10974   "Fetch all articles mentioned in the References header.
10975 Return how many articles were fetched."
10976   (interactive)
10977   (gnus-set-global-variables)
10978   (let ((ref (mail-header-references (gnus-summary-article-header)))
10979         (current (gnus-summary-article-number))
10980         (n 0))
10981     ;; For each Message-ID in the References header...
10982     (while (string-match "<[^>]*>" ref)
10983       (incf n)
10984       ;; ... fetch that article.
10985       (gnus-summary-refer-article
10986        (prog1 (match-string 0 ref)
10987          (setq ref (substring ref (match-end 0))))))
10988     (gnus-summary-goto-subject current)
10989     (gnus-summary-position-point)
10990     n))
10991
10992 (defun gnus-summary-refer-article (message-id)
10993   "Fetch an article specified by MESSAGE-ID."
10994   (interactive "sMessage-ID: ")
10995   (when (and (stringp message-id)
10996              (not (zerop (length message-id))))
10997     ;; Construct the correct Message-ID if necessary.
10998     ;; Suggested by tale@pawl.rpi.edu.
10999     (unless (string-match "^<" message-id)
11000       (setq message-id (concat "<" message-id)))
11001     (unless (string-match ">$" message-id)
11002       (setq message-id (concat message-id ">")))
11003     (let ((header (car (gnus-gethash message-id
11004                                      gnus-newsgroup-dependencies))))
11005       (if header
11006           ;; The article is present in the buffer, to we just go to it.
11007           (gnus-summary-goto-article (mail-header-number header) nil t)
11008         ;; We fetch the article
11009         (let ((gnus-override-method 
11010                (and (gnus-news-group-p gnus-newsgroup-name)
11011                     gnus-refer-article-method))
11012               number)
11013           ;; Start the special refer-article method, if necessary.
11014           (when (and gnus-refer-article-method
11015                      (gnus-news-group-p gnus-newsgroup-name))
11016             (gnus-check-server gnus-refer-article-method))
11017           ;; Fetch the header, and display the article.
11018           (if (setq number (gnus-summary-insert-subject message-id))
11019               (gnus-summary-select-article nil nil nil number)
11020             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
11021
11022 (defun gnus-summary-enter-digest-group (&optional force)
11023   "Enter a digest group based on the current article."
11024   (interactive "P")
11025   (gnus-set-global-variables)
11026   (gnus-summary-select-article)
11027   (let ((name (format "%s-%d"
11028                       (gnus-group-prefixed-name
11029                        gnus-newsgroup-name (list 'nndoc ""))
11030                       gnus-current-article))
11031         (ogroup gnus-newsgroup-name)
11032         (case-fold-search t)
11033         (buf (current-buffer))
11034         dig)
11035     (save-excursion
11036       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
11037       (insert-buffer-substring gnus-original-article-buffer)
11038       (narrow-to-region
11039        (goto-char (point-min))
11040        (or (search-forward "\n\n" nil t) (point)))
11041       (goto-char (point-min))
11042       (delete-matching-lines "^\\(Path\\):\\|^From ")
11043       (widen))
11044     (unwind-protect
11045         (if (gnus-group-read-ephemeral-group
11046              name `(nndoc ,name (nndoc-address
11047                                  ,(get-buffer dig))
11048                           (nndoc-article-type ,(if force 'digest 'guess))) t)
11049             ;; Make all postings to this group go to the parent group.
11050             (nconc (gnus-info-params (gnus-get-info name))
11051                    (list (cons 'to-group ogroup)))
11052           ;; Couldn't select this doc group.
11053           (switch-to-buffer buf)
11054           (gnus-set-global-variables)
11055           (gnus-configure-windows 'summary)
11056           (gnus-message 3 "Article couldn't be entered?"))
11057       (kill-buffer dig))))
11058
11059 (defun gnus-summary-isearch-article (&optional regexp-p)
11060   "Do incremental search forward on the current article.
11061 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
11062   (interactive "P")
11063   (gnus-set-global-variables)
11064   (gnus-summary-select-article)
11065   (gnus-configure-windows 'article)
11066   (gnus-eval-in-buffer-window
11067    gnus-article-buffer
11068    (goto-char (point-min))
11069    (isearch-forward regexp-p)))
11070
11071 (defun gnus-summary-search-article-forward (regexp &optional backward)
11072   "Search for an article containing REGEXP forward.
11073 If BACKWARD, search backward instead."
11074   (interactive
11075    (list (read-string
11076           (format "Search article %s (regexp%s): "
11077                   (if current-prefix-arg "backward" "forward")
11078                   (if gnus-last-search-regexp
11079                       (concat ", default " gnus-last-search-regexp)
11080                     "")))
11081          current-prefix-arg))
11082   (gnus-set-global-variables)
11083   (if (string-equal regexp "")
11084       (setq regexp (or gnus-last-search-regexp ""))
11085     (setq gnus-last-search-regexp regexp))
11086   (if (gnus-summary-search-article regexp backward)
11087       (gnus-article-set-window-start
11088        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
11089     (error "Search failed: \"%s\"" regexp)))
11090
11091 (defun gnus-summary-search-article-backward (regexp)
11092   "Search for an article containing REGEXP backward."
11093   (interactive
11094    (list (read-string
11095           (format "Search article backward (regexp%s): "
11096                   (if gnus-last-search-regexp
11097                       (concat ", default " gnus-last-search-regexp)
11098                     "")))))
11099   (gnus-summary-search-article-forward regexp 'backward))
11100
11101 (defun gnus-summary-search-article (regexp &optional backward)
11102   "Search for an article containing REGEXP.
11103 Optional argument BACKWARD means do search for backward.
11104 `gnus-select-article-hook' is not called during the search."
11105   (let ((gnus-select-article-hook nil)  ;Disable hook.
11106         (gnus-article-display-hook nil)
11107         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11108         (re-search
11109          (if backward
11110              're-search-backward 're-search-forward))
11111         (sum (current-buffer))
11112         (found nil))
11113     (gnus-save-hidden-threads
11114       (gnus-summary-select-article)
11115       (set-buffer gnus-article-buffer)
11116       (while (not found)
11117         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
11118         (if (if backward
11119                 (re-search-backward regexp nil t)
11120               (re-search-forward regexp nil t))
11121             ;; We found the regexp.
11122             (progn
11123               (setq found 'found)
11124               (beginning-of-line)
11125               (set-window-start
11126                (get-buffer-window (current-buffer))
11127                (point)))
11128           ;; We didn't find it, so we go to the next article.
11129           (set-buffer sum)
11130           (if (not (if backward (gnus-summary-find-prev)
11131                      (gnus-summary-find-next)))
11132               ;; No more articles.
11133               (setq found t)
11134             ;; Select the next article and adjust point.
11135             (gnus-summary-select-article)
11136             (set-buffer gnus-article-buffer)
11137             (widen)
11138             (goto-char (if backward (point-max) (point-min))))))
11139       (set-buffer sum)
11140       (gnus-message 7 ""))
11141     ;; Return whether we found the regexp.
11142     (when (eq found 'found)
11143       (gnus-summary-show-thread)
11144       (gnus-summary-goto-subject gnus-current-article)
11145       (gnus-summary-position-point)
11146       t)))
11147
11148 (defun gnus-summary-find-matching (header regexp &optional backward unread
11149                                           not-case-fold)
11150   "Return a list of all articles that match REGEXP on HEADER.
11151 The search stars on the current article and goes forwards unless
11152 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11153 If UNREAD is non-nil, only unread articles will
11154 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11155 in the comparisons."
11156   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11157                 (gnus-data-find-list
11158                  (gnus-summary-article-number) (gnus-data-list backward))))
11159         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11160         (case-fold-search (not not-case-fold))
11161         articles d)
11162     (or (fboundp (intern (concat "mail-header-" header)))
11163         (error "%s is not a valid header" header))
11164     (while data
11165       (setq d (car data))
11166       (and (or (not unread)             ; We want all articles...
11167                (gnus-data-unread-p d))  ; Or just unreads.
11168            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11169            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11170            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11171       (setq data (cdr data)))
11172     (nreverse articles)))
11173
11174 (defun gnus-summary-execute-command (header regexp command &optional backward)
11175   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11176 If HEADER is an empty string (or nil), the match is done on the entire
11177 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11178   (interactive
11179    (list (let ((completion-ignore-case t))
11180            (completing-read
11181             "Header name: "
11182             (mapcar (lambda (string) (list string))
11183                     '("Number" "Subject" "From" "Lines" "Date"
11184                       "Message-ID" "Xref" "References" "Body"))
11185             nil 'require-match))
11186          (read-string "Regexp: ")
11187          (read-key-sequence "Command: ")
11188          current-prefix-arg))
11189   (when (equal header "Body")
11190     (setq header ""))
11191   (gnus-set-global-variables)
11192   ;; Hidden thread subtrees must be searched as well.
11193   (gnus-summary-show-all-threads)
11194   ;; We don't want to change current point nor window configuration.
11195   (save-excursion
11196     (save-window-excursion
11197       (gnus-message 6 "Executing %s..." (key-description command))
11198       ;; We'd like to execute COMMAND interactively so as to give arguments.
11199       (gnus-execute header regexp
11200                     `(lambda () (call-interactively ',(key-binding command)))
11201                     backward)
11202       (gnus-message 6 "Executing %s...done" (key-description command)))))
11203
11204 (defun gnus-summary-beginning-of-article ()
11205   "Scroll the article back to the beginning."
11206   (interactive)
11207   (gnus-set-global-variables)
11208   (gnus-summary-select-article)
11209   (gnus-configure-windows 'article)
11210   (gnus-eval-in-buffer-window
11211    gnus-article-buffer
11212    (widen)
11213    (goto-char (point-min))
11214    (and gnus-break-pages (gnus-narrow-to-page))))
11215
11216 (defun gnus-summary-end-of-article ()
11217   "Scroll to the end of the article."
11218   (interactive)
11219   (gnus-set-global-variables)
11220   (gnus-summary-select-article)
11221   (gnus-configure-windows 'article)
11222   (gnus-eval-in-buffer-window
11223    gnus-article-buffer
11224    (widen)
11225    (goto-char (point-max))
11226    (recenter -3)
11227    (and gnus-break-pages (gnus-narrow-to-page))))
11228
11229 (defun gnus-summary-show-article (&optional arg)
11230   "Force re-fetching of the current article.
11231 If ARG (the prefix) is non-nil, show the raw article without any
11232 article massaging functions being run."
11233   (interactive "P")
11234   (gnus-set-global-variables)
11235   (if (not arg)
11236       ;; Select the article the normal way.
11237       (gnus-summary-select-article nil 'force)
11238     ;; Bind the article treatment functions to nil.
11239     (let ((gnus-have-all-headers t)
11240           gnus-article-display-hook
11241           gnus-article-prepare-hook
11242           gnus-break-pages
11243           gnus-visual)
11244       (gnus-summary-select-article nil 'force)))
11245   (gnus-summary-goto-subject gnus-current-article)
11246 ;  (gnus-configure-windows 'article)
11247   (gnus-summary-position-point))
11248
11249 (defun gnus-summary-verbose-headers (&optional arg)
11250   "Toggle permanent full header display.
11251 If ARG is a positive number, turn header display on.
11252 If ARG is a negative number, turn header display off."
11253   (interactive "P")
11254   (gnus-set-global-variables)
11255   (gnus-summary-toggle-header arg)
11256   (setq gnus-show-all-headers
11257         (cond ((or (not (numberp arg))
11258                    (zerop arg))
11259                (not gnus-show-all-headers))
11260               ((natnump arg)
11261                t))))
11262
11263 (defun gnus-summary-toggle-header (&optional arg)
11264   "Show the headers if they are hidden, or hide them if they are shown.
11265 If ARG is a positive number, show the entire header.
11266 If ARG is a negative number, hide the unwanted header lines."
11267   (interactive "P")
11268   (gnus-set-global-variables)
11269   (save-excursion
11270     (set-buffer gnus-article-buffer)
11271     (let* ((buffer-read-only nil)
11272            (inhibit-point-motion-hooks t)
11273            (hidden (text-property-any
11274                     (goto-char (point-min)) (search-forward "\n\n")
11275                     'invisible t))
11276            e)
11277       (goto-char (point-min))
11278       (when (search-forward "\n\n" nil t)
11279         (delete-region (point-min) (1- (point))))
11280       (goto-char (point-min))
11281       (save-excursion
11282         (set-buffer gnus-original-article-buffer)
11283         (goto-char (point-min))
11284         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11285       (insert-buffer-substring gnus-original-article-buffer 1 e)
11286       (let ((gnus-inhibit-hiding t))
11287         (run-hooks 'gnus-article-display-hook))
11288       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11289           (gnus-article-hide-headers)))))
11290
11291 (defun gnus-summary-show-all-headers ()
11292   "Make all header lines visible."
11293   (interactive)
11294   (gnus-set-global-variables)
11295   (gnus-article-show-all-headers))
11296
11297 (defun gnus-summary-toggle-mime (&optional arg)
11298   "Toggle MIME processing.
11299 If ARG is a positive number, turn MIME processing on."
11300   (interactive "P")
11301   (gnus-set-global-variables)
11302   (setq gnus-show-mime
11303         (if (null arg) (not gnus-show-mime)
11304           (> (prefix-numeric-value arg) 0)))
11305   (gnus-summary-select-article t 'force))
11306
11307 (defun gnus-summary-caesar-message (&optional arg)
11308   "Caesar rotate the current article by 13.
11309 The numerical prefix specifies how manu places to rotate each letter
11310 forward."
11311   (interactive "P")
11312   (gnus-set-global-variables)
11313   (gnus-summary-select-article)
11314   (let ((mail-header-separator ""))
11315     (gnus-eval-in-buffer-window
11316      gnus-article-buffer
11317      (save-restriction
11318        (widen)
11319        (let ((start (window-start)))
11320          (news-caesar-buffer-body arg)
11321          (set-window-start (get-buffer-window (current-buffer)) start))))))
11322
11323 (defun gnus-summary-stop-page-breaking ()
11324   "Stop page breaking in the current article."
11325   (interactive)
11326   (gnus-set-global-variables)
11327   (gnus-summary-select-article)
11328   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11329
11330 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11331   "Move the current article to a different newsgroup.
11332 If N is a positive number, move the N next articles.
11333 If N is a negative number, move the N previous articles.
11334 If N is nil and any articles have been marked with the process mark,
11335 move those articles instead.
11336 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11337 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11338 re-spool using this method.
11339
11340 For this function to work, both the current newsgroup and the
11341 newsgroup that you want to move to have to support the `request-move'
11342 and `request-accept' functions."
11343   (interactive "P")
11344   (unless action (setq action 'move))
11345   (gnus-set-global-variables)
11346   ;; Check whether the source group supports the required functions.
11347   (cond ((and (eq action 'move)
11348               (not (gnus-check-backend-function
11349                     'request-move-article gnus-newsgroup-name)))
11350          (error "The current group does not support article moving"))
11351         ((and (eq action 'crosspost)
11352               (not (gnus-check-backend-function
11353                     'request-replace-article gnus-newsgroup-name)))
11354          (error "The current group does not support article editing")))
11355   (let ((articles (gnus-summary-work-articles n))
11356         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11357         (names '((move "Move" "Moving")
11358                  (copy "Copy" "Copying")
11359                  (crosspost "Crosspost" "Crossposting")))
11360         (copy-buf (save-excursion
11361                     (nnheader-set-temp-buffer " *copy article*")))
11362         art-group to-method new-xref article to-groups)
11363     (unless (assq action names)
11364       (error "Unknown action %s" action))
11365     ;; Read the newsgroup name.
11366     (when (and (not to-newsgroup)
11367                (not select-method))
11368       (setq to-newsgroup
11369             (gnus-read-move-group-name
11370              (cadr (assq action names))
11371              (symbol-value (intern (format "gnus-current-%s-group" action)))
11372              articles prefix))
11373       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11374     (setq to-method (or select-method 
11375                         (gnus-find-method-for-group to-newsgroup)))
11376     ;; Check the method we are to move this article to...
11377     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11378         (error "%s does not support article copying" (car to-method)))
11379     (or (gnus-check-server to-method)
11380         (error "Can't open server %s" (car to-method)))
11381     (gnus-message 6 "%s to %s: %s..."
11382                   (caddr (assq action names))
11383                   (or (car select-method) to-newsgroup) articles)
11384     (while articles
11385       (setq article (pop articles))
11386       (setq
11387        art-group
11388        (cond
11389         ;; Move the article.
11390         ((eq action 'move)
11391          (gnus-request-move-article
11392           article                       ; Article to move
11393           gnus-newsgroup-name           ; From newsgrouo
11394           (nth 1 (gnus-find-method-for-group
11395                   gnus-newsgroup-name)) ; Server
11396           (list 'gnus-request-accept-article
11397                 to-newsgroup (list 'quote select-method)
11398                 (not articles))         ; Accept form
11399           (not articles)))              ; Only save nov last time
11400         ;; Copy the article.
11401         ((eq action 'copy)
11402          (save-excursion
11403            (set-buffer copy-buf)
11404            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11405            (gnus-request-accept-article
11406             to-newsgroup select-method (not articles))))
11407         ;; Crosspost the article.
11408         ((eq action 'crosspost)
11409          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11410            (setq new-xref (concat gnus-newsgroup-name ":" article))
11411            (if (and xref (not (string= xref "")))
11412                (progn
11413                  (when (string-match "^Xref: " xref)
11414                    (setq xref (substring xref (match-end 0))))
11415                  (setq new-xref (concat xref " " new-xref)))
11416              (setq new-xref (concat (system-name) " " new-xref)))
11417            (save-excursion
11418              (set-buffer copy-buf)
11419              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11420              (nnheader-replace-header "xref" new-xref)
11421              (gnus-request-accept-article
11422               to-newsgroup select-method (not articles)))))))
11423       (if (not art-group)
11424           (gnus-message 1 "Couldn't %s article %s"
11425                         (cadr (assq action names)) article)
11426         (let* ((entry
11427                 (or
11428                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11429                  (gnus-gethash
11430                   (gnus-group-prefixed-name
11431                    (car art-group)
11432                    (or select-method 
11433                        (gnus-find-method-for-group to-newsgroup)))
11434                   gnus-newsrc-hashtb)))
11435                (info (nth 2 entry))
11436                (to-group (gnus-info-group info)))
11437           ;; Update the group that has been moved to.
11438           (when (and info
11439                      (memq action '(move copy)))
11440             (unless (member to-group to-groups)
11441               (push to-group to-groups))
11442
11443             (unless (memq article gnus-newsgroup-unreads)
11444               (gnus-info-set-read
11445                info (gnus-add-to-range (gnus-info-read info)
11446                                        (list (cdr art-group)))))
11447
11448             ;; Copy any marks over to the new group.
11449             (let ((marks gnus-article-mark-lists)
11450                   (to-article (cdr art-group)))
11451
11452               ;; See whether the article is to be put in the cache.
11453               (when gnus-use-cache
11454                 (gnus-cache-possibly-enter-article
11455                  to-group to-article
11456                  (let ((header (copy-sequence
11457                                 (gnus-summary-article-header article))))
11458                    (mail-header-set-number header to-article)
11459                    header)
11460                  (memq article gnus-newsgroup-marked)
11461                  (memq article gnus-newsgroup-dormant)
11462                  (memq article gnus-newsgroup-unreads)))
11463
11464               (while marks
11465                 (when (memq article (symbol-value
11466                                      (intern (format "gnus-newsgroup-%s"
11467                                                      (caar marks)))))
11468                   ;; If the other group is the same as this group,
11469                   ;; then we have to add the mark to the list.
11470                   (when (equal to-group gnus-newsgroup-name)
11471                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11472                          (cons to-article
11473                                (symbol-value
11474                                 (intern (format "gnus-newsgroup-%s"
11475                                                 (caar marks)))))))
11476                   ;; Copy mark to other group.
11477                   (gnus-add-marked-articles
11478                    to-group (cdar marks) (list to-article) info))
11479                 (setq marks (cdr marks)))))
11480
11481           ;; Update the Xref header in this article to point to
11482           ;; the new crossposted article we have just created.
11483           (when (eq action 'crosspost)
11484             (save-excursion
11485               (set-buffer copy-buf)
11486               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11487               (nnheader-replace-header
11488                "xref" (concat new-xref " " (gnus-group-prefixed-name
11489                                             (car art-group) to-method)
11490                               ":" (cdr art-group)))
11491               (gnus-request-replace-article
11492                article gnus-newsgroup-name (current-buffer)))))
11493
11494         (gnus-summary-goto-subject article)
11495         (when (eq action 'move)
11496           (gnus-summary-mark-article article gnus-canceled-mark)))
11497       (gnus-summary-remove-process-mark article))
11498     ;; Re-activate all groups that have been moved to.
11499     (while to-groups
11500       (gnus-activate-group (pop to-groups)))
11501     
11502     (gnus-kill-buffer copy-buf)
11503     (gnus-summary-position-point)
11504     (gnus-set-mode-line 'summary)))
11505
11506 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11507   "Move the current article to a different newsgroup.
11508 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11509 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11510 re-spool using this method."
11511   (interactive "P")
11512   (gnus-summary-move-article n nil select-method 'copy))
11513
11514 (defun gnus-summary-crosspost-article (&optional n)
11515   "Crosspost the current article to some other group."
11516   (interactive "P")
11517   (gnus-summary-move-article n nil nil 'crosspost))
11518
11519 (defvar gnus-summary-respool-default-method nil
11520   "Default method for respooling an article.  
11521 If nil, use to the current newsgroup method.")
11522
11523 (defun gnus-summary-respool-article (&optional n method)
11524   "Respool the current article.
11525 The article will be squeezed through the mail spooling process again,
11526 which means that it will be put in some mail newsgroup or other
11527 depending on `nnmail-split-methods'.
11528 If N is a positive number, respool the N next articles.
11529 If N is a negative number, respool the N previous articles.
11530 If N is nil and any articles have been marked with the process mark,
11531 respool those articles instead.
11532
11533 Respooling can be done both from mail groups and \"real\" newsgroups.
11534 In the former case, the articles in question will be moved from the
11535 current group into whatever groups they are destined to.  In the
11536 latter case, they will be copied into the relevant groups."
11537   (interactive 
11538    (list current-prefix-arg
11539          (let* ((methods (gnus-methods-using 'respool))
11540                 (methname
11541                  (symbol-name (or gnus-summary-respool-default-method
11542                                   (car (gnus-find-method-for-group
11543                                         gnus-newsgroup-name)))))
11544                 (method
11545                  (gnus-completing-read 
11546                   methname "What backend do you want to use when? "
11547                   methods nil t nil 'gnus-method-history))
11548                 ms)
11549            (cond
11550             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11551              (list (intern method) ""))
11552             ((= 1 (length ms))
11553              (car ms))
11554             (t
11555              (cdr (completing-read 
11556                    "Server name: "
11557                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11558   (gnus-set-global-variables)
11559   (unless method
11560     (error "No method given for respooling"))
11561   (if (assoc (symbol-name
11562               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11563              (gnus-methods-using 'respool))
11564       (gnus-summary-move-article n nil method)
11565     (gnus-summary-copy-article n nil method)))
11566
11567 (defun gnus-summary-import-article (file)
11568   "Import a random file into a mail newsgroup."
11569   (interactive "fImport file: ")
11570   (gnus-set-global-variables)
11571   (let ((group gnus-newsgroup-name)
11572         (now (current-time))
11573         atts lines)
11574     (or (gnus-check-backend-function 'request-accept-article group)
11575         (error "%s does not support article importing" group))
11576     (or (file-readable-p file)
11577         (not (file-regular-p file))
11578         (error "Can't read %s" file))
11579     (save-excursion
11580       (set-buffer (get-buffer-create " *import file*"))
11581       (buffer-disable-undo (current-buffer))
11582       (erase-buffer)
11583       (insert-file-contents file)
11584       (goto-char (point-min))
11585       (unless (nnheader-article-p)
11586         ;; This doesn't look like an article, so we fudge some headers.
11587         (setq atts (file-attributes file)
11588               lines (count-lines (point-min) (point-max)))
11589         (insert "From: " (read-string "From: ") "\n"
11590                 "Subject: " (read-string "Subject: ") "\n"
11591                 "Date: " (timezone-make-date-arpa-standard
11592                           (current-time-string (nth 5 atts))
11593                           (current-time-zone now)
11594                           (current-time-zone now)) "\n"
11595                 "Message-ID: " (gnus-inews-message-id) "\n"
11596                 "Lines: " (int-to-string lines) "\n"
11597                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11598       (gnus-request-accept-article group nil t)
11599       (kill-buffer (current-buffer)))))
11600
11601 (defun gnus-summary-expire-articles (&optional now)
11602   "Expire all articles that are marked as expirable in the current group."
11603   (interactive)
11604   (gnus-set-global-variables)
11605   (when (gnus-check-backend-function
11606          'request-expire-articles gnus-newsgroup-name)
11607     ;; This backend supports expiry.
11608     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11609            (expirable (if total
11610                           (gnus-list-of-read-articles gnus-newsgroup-name)
11611                         (setq gnus-newsgroup-expirable
11612                               (sort gnus-newsgroup-expirable '<))))
11613            (expiry-wait (if now 'immediate
11614                           (gnus-group-get-parameter
11615                            gnus-newsgroup-name 'expiry-wait)))
11616            es)
11617       (when expirable
11618         ;; There are expirable articles in this group, so we run them
11619         ;; through the expiry process.
11620         (gnus-message 6 "Expiring articles...")
11621         ;; The list of articles that weren't expired is returned.
11622         (if expiry-wait
11623             (let ((nnmail-expiry-wait-function nil)
11624                   (nnmail-expiry-wait expiry-wait))
11625               (setq es (gnus-request-expire-articles
11626                         expirable gnus-newsgroup-name)))
11627           (setq es (gnus-request-expire-articles
11628                     expirable gnus-newsgroup-name)))
11629         (or total (setq gnus-newsgroup-expirable es))
11630         ;; We go through the old list of expirable, and mark all
11631         ;; really expired articles as nonexistent.
11632         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11633           (let ((gnus-use-cache nil))
11634             (while expirable
11635               (unless (memq (car expirable) es)
11636                 (when (gnus-data-find (car expirable))
11637                   (gnus-summary-mark-article
11638                    (car expirable) gnus-canceled-mark)))
11639               (setq expirable (cdr expirable)))))
11640         (gnus-message 6 "Expiring articles...done")))))
11641
11642 (defun gnus-summary-expire-articles-now ()
11643   "Expunge all expirable articles in the current group.
11644 This means that *all* articles that are marked as expirable will be
11645 deleted forever, right now."
11646   (interactive)
11647   (gnus-set-global-variables)
11648   (or gnus-expert-user
11649       (gnus-y-or-n-p
11650        "Are you really, really, really sure you want to delete all these messages? ")
11651       (error "Phew!"))
11652   (gnus-summary-expire-articles t))
11653
11654 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11655 (defun gnus-summary-delete-article (&optional n)
11656   "Delete the N next (mail) articles.
11657 This command actually deletes articles.  This is not a marking
11658 command.  The article will disappear forever from your life, never to
11659 return.
11660 If N is negative, delete backwards.
11661 If N is nil and articles have been marked with the process mark,
11662 delete these instead."
11663   (interactive "P")
11664   (gnus-set-global-variables)
11665   (or (gnus-check-backend-function 'request-expire-articles
11666                                    gnus-newsgroup-name)
11667       (error "The current newsgroup does not support article deletion."))
11668   ;; Compute the list of articles to delete.
11669   (let ((articles (gnus-summary-work-articles n))
11670         not-deleted)
11671     (if (and gnus-novice-user
11672              (not (gnus-y-or-n-p
11673                    (format "Do you really want to delete %s forever? "
11674                            (if (> (length articles) 1) 
11675                                (format "these %s articles" (length articles))
11676                              "this article")))))
11677         ()
11678       ;; Delete the articles.
11679       (setq not-deleted (gnus-request-expire-articles
11680                          articles gnus-newsgroup-name 'force))
11681       (while articles
11682         (gnus-summary-remove-process-mark (car articles))
11683         ;; The backend might not have been able to delete the article
11684         ;; after all.
11685         (or (memq (car articles) not-deleted)
11686             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11687         (setq articles (cdr articles))))
11688     (gnus-summary-position-point)
11689     (gnus-set-mode-line 'summary)
11690     not-deleted))
11691
11692 (defun gnus-summary-edit-article (&optional force)
11693   "Enter into a buffer and edit the current article.
11694 This will have permanent effect only in mail groups.
11695 If FORCE is non-nil, allow editing of articles even in read-only
11696 groups."
11697   (interactive "P")
11698   (save-excursion
11699     (set-buffer gnus-summary-buffer)
11700     (gnus-set-global-variables)
11701     (when (and (not force)
11702                (gnus-group-read-only-p))
11703       (error "The current newsgroup does not support article editing."))
11704     (gnus-summary-select-article t nil t)
11705     (gnus-configure-windows 'article)
11706     (select-window (get-buffer-window gnus-article-buffer))
11707     (gnus-message 6 "C-c C-c to end edits")
11708     (setq buffer-read-only nil)
11709     (text-mode)
11710     (use-local-map (copy-keymap (current-local-map)))
11711     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11712     (buffer-enable-undo)
11713     (widen)
11714     (goto-char (point-min))
11715     (search-forward "\n\n" nil t)))
11716
11717 (defun gnus-summary-edit-article-done ()
11718   "Make edits to the current article permanent."
11719   (interactive)
11720   (if (gnus-group-read-only-p)
11721       (progn
11722         (gnus-summary-edit-article-postpone)
11723         (gnus-error
11724          1 "The current newsgroup does not support article editing."))
11725     (let ((buf (format "%s" (buffer-string))))
11726       (erase-buffer)
11727       (insert buf)
11728       (if (not (gnus-request-replace-article
11729                 (cdr gnus-article-current) (car gnus-article-current)
11730                 (current-buffer)))
11731           (error "Couldn't replace article.")
11732         (gnus-article-mode)
11733         (use-local-map gnus-article-mode-map)
11734         (setq buffer-read-only t)
11735         (buffer-disable-undo (current-buffer))
11736         (gnus-configure-windows 'summary)
11737         (gnus-summary-update-article (cdr gnus-article-current))
11738         (when gnus-use-cache
11739           (gnus-cache-update-article 
11740            (cdr gnus-article-current) (car gnus-article-current)))
11741         (when gnus-keep-backlog
11742           (gnus-backlog-remove-article 
11743            (car gnus-article-current) (cdr gnus-article-current))))
11744       (save-excursion
11745         (when (get-buffer gnus-original-article-buffer)
11746           (set-buffer gnus-original-article-buffer)
11747           (setq gnus-original-article nil)))
11748       (setq gnus-article-current nil
11749             gnus-current-article nil)
11750       (run-hooks 'gnus-article-display-hook)
11751       (and (gnus-visual-p 'summary-highlight 'highlight)
11752            (run-hooks 'gnus-visual-mark-article-hook)))))
11753
11754 (defun gnus-summary-edit-article-postpone ()
11755   "Postpone changes to the current article."
11756   (interactive)
11757   (gnus-article-mode)
11758   (use-local-map gnus-article-mode-map)
11759   (setq buffer-read-only t)
11760   (buffer-disable-undo (current-buffer))
11761   (gnus-configure-windows 'summary)
11762   (and (gnus-visual-p 'summary-highlight 'highlight)
11763        (run-hooks 'gnus-visual-mark-article-hook)))
11764
11765 (defun gnus-summary-respool-query ()
11766   "Query where the respool algorithm would put this article."
11767   (interactive)
11768   (gnus-set-global-variables)
11769   (gnus-summary-select-article)
11770   (save-excursion
11771     (set-buffer gnus-article-buffer)
11772     (save-restriction
11773       (goto-char (point-min))
11774       (search-forward "\n\n")
11775       (narrow-to-region (point-min) (point))
11776       (pp-eval-expression
11777        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11778
11779 ;; Summary marking commands.
11780
11781 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11782   "Mark articles which has the same subject as read, and then select the next.
11783 If UNMARK is positive, remove any kind of mark.
11784 If UNMARK is negative, tick articles."
11785   (interactive "P")
11786   (gnus-set-global-variables)
11787   (if unmark
11788       (setq unmark (prefix-numeric-value unmark)))
11789   (let ((count
11790          (gnus-summary-mark-same-subject
11791           (gnus-summary-article-subject) unmark)))
11792     ;; Select next unread article.  If auto-select-same mode, should
11793     ;; select the first unread article.
11794     (gnus-summary-next-article t (and gnus-auto-select-same
11795                                       (gnus-summary-article-subject)))
11796     (gnus-message 7 "%d article%s marked as %s"
11797                   count (if (= count 1) " is" "s are")
11798                   (if unmark "unread" "read"))))
11799
11800 (defun gnus-summary-kill-same-subject (&optional unmark)
11801   "Mark articles which has the same subject as read.
11802 If UNMARK is positive, remove any kind of mark.
11803 If UNMARK is negative, tick articles."
11804   (interactive "P")
11805   (gnus-set-global-variables)
11806   (if unmark
11807       (setq unmark (prefix-numeric-value unmark)))
11808   (let ((count
11809          (gnus-summary-mark-same-subject
11810           (gnus-summary-article-subject) unmark)))
11811     ;; If marked as read, go to next unread subject.
11812     (if (null unmark)
11813         ;; Go to next unread subject.
11814         (gnus-summary-next-subject 1 t))
11815     (gnus-message 7 "%d articles are marked as %s"
11816                   count (if unmark "unread" "read"))))
11817
11818 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11819   "Mark articles with same SUBJECT as read, and return marked number.
11820 If optional argument UNMARK is positive, remove any kinds of marks.
11821 If optional argument UNMARK is negative, mark articles as unread instead."
11822   (let ((count 1))
11823     (save-excursion
11824       (cond
11825        ((null unmark)                   ; Mark as read.
11826         (while (and
11827                 (progn
11828                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11829                   (gnus-summary-show-thread) t)
11830                 (gnus-summary-find-subject subject))
11831           (setq count (1+ count))))
11832        ((> unmark 0)                    ; Tick.
11833         (while (and
11834                 (progn
11835                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11836                   (gnus-summary-show-thread) t)
11837                 (gnus-summary-find-subject subject))
11838           (setq count (1+ count))))
11839        (t                               ; Mark as unread.
11840         (while (and
11841                 (progn
11842                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11843                   (gnus-summary-show-thread) t)
11844                 (gnus-summary-find-subject subject))
11845           (setq count (1+ count)))))
11846       (gnus-set-mode-line 'summary)
11847       ;; Return the number of marked articles.
11848       count)))
11849
11850 (defun gnus-summary-mark-as-processable (n &optional unmark)
11851   "Set the process mark on the next N articles.
11852 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11853 the process mark instead.  The difference between N and the actual
11854 number of articles marked is returned."
11855   (interactive "p")
11856   (gnus-set-global-variables)
11857   (let ((backward (< n 0))
11858         (n (abs n)))
11859     (while (and
11860             (> n 0)
11861             (if unmark
11862                 (gnus-summary-remove-process-mark
11863                  (gnus-summary-article-number))
11864               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11865             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11866       (setq n (1- n)))
11867     (if (/= 0 n) (gnus-message 7 "No more articles"))
11868     (gnus-summary-recenter)
11869     (gnus-summary-position-point)
11870     n))
11871
11872 (defun gnus-summary-unmark-as-processable (n)
11873   "Remove the process mark from the next N articles.
11874 If N is negative, mark backward instead.  The difference between N and
11875 the actual number of articles marked is returned."
11876   (interactive "p")
11877   (gnus-set-global-variables)
11878   (gnus-summary-mark-as-processable n t))
11879
11880 (defun gnus-summary-unmark-all-processable ()
11881   "Remove the process mark from all articles."
11882   (interactive)
11883   (gnus-set-global-variables)
11884   (save-excursion
11885     (while gnus-newsgroup-processable
11886       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11887   (gnus-summary-position-point))
11888
11889 (defun gnus-summary-mark-as-expirable (n)
11890   "Mark N articles forward as expirable.
11891 If N is negative, mark backward instead.  The difference between N and
11892 the actual number of articles marked is returned."
11893   (interactive "p")
11894   (gnus-set-global-variables)
11895   (gnus-summary-mark-forward n gnus-expirable-mark))
11896
11897 (defun gnus-summary-mark-article-as-replied (article)
11898   "Mark ARTICLE replied and update the summary line."
11899   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11900   (let ((buffer-read-only nil))
11901     (when (gnus-summary-goto-subject article)
11902       (gnus-summary-update-secondary-mark article))))
11903
11904 (defun gnus-summary-set-bookmark (article)
11905   "Set a bookmark in current article."
11906   (interactive (list (gnus-summary-article-number)))
11907   (gnus-set-global-variables)
11908   (if (or (not (get-buffer gnus-article-buffer))
11909           (not gnus-current-article)
11910           (not gnus-article-current)
11911           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11912       (error "No current article selected"))
11913   ;; Remove old bookmark, if one exists.
11914   (let ((old (assq article gnus-newsgroup-bookmarks)))
11915     (if old (setq gnus-newsgroup-bookmarks
11916                   (delq old gnus-newsgroup-bookmarks))))
11917   ;; Set the new bookmark, which is on the form
11918   ;; (article-number . line-number-in-body).
11919   (setq gnus-newsgroup-bookmarks
11920         (cons
11921          (cons article
11922                (save-excursion
11923                  (set-buffer gnus-article-buffer)
11924                  (count-lines
11925                   (min (point)
11926                        (save-excursion
11927                          (goto-char (point-min))
11928                          (search-forward "\n\n" nil t)
11929                          (point)))
11930                   (point))))
11931          gnus-newsgroup-bookmarks))
11932   (gnus-message 6 "A bookmark has been added to the current article."))
11933
11934 (defun gnus-summary-remove-bookmark (article)
11935   "Remove the bookmark from the current article."
11936   (interactive (list (gnus-summary-article-number)))
11937   (gnus-set-global-variables)
11938   ;; Remove old bookmark, if one exists.
11939   (let ((old (assq article gnus-newsgroup-bookmarks)))
11940     (if old
11941         (progn
11942           (setq gnus-newsgroup-bookmarks
11943                 (delq old gnus-newsgroup-bookmarks))
11944           (gnus-message 6 "Removed bookmark."))
11945       (gnus-message 6 "No bookmark in current article."))))
11946
11947 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11948 (defun gnus-summary-mark-as-dormant (n)
11949   "Mark N articles forward as dormant.
11950 If N is negative, mark backward instead.  The difference between N and
11951 the actual number of articles marked is returned."
11952   (interactive "p")
11953   (gnus-set-global-variables)
11954   (gnus-summary-mark-forward n gnus-dormant-mark))
11955
11956 (defun gnus-summary-set-process-mark (article)
11957   "Set the process mark on ARTICLE and update the summary line."
11958   (setq gnus-newsgroup-processable
11959         (cons article
11960               (delq article gnus-newsgroup-processable)))
11961   (when (gnus-summary-goto-subject article)
11962     (gnus-summary-show-thread)
11963     (gnus-summary-update-secondary-mark article)))
11964
11965 (defun gnus-summary-remove-process-mark (article)
11966   "Remove the process mark from ARTICLE and update the summary line."
11967   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11968   (when (gnus-summary-goto-subject article)
11969     (gnus-summary-show-thread)
11970     (gnus-summary-update-secondary-mark article)))
11971
11972 (defun gnus-summary-set-saved-mark (article)
11973   "Set the process mark on ARTICLE and update the summary line."
11974   (push article gnus-newsgroup-saved)
11975   (when (gnus-summary-goto-subject article)
11976     (gnus-summary-update-secondary-mark article)))
11977
11978 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11979   "Mark N articles as read forwards.
11980 If N is negative, mark backwards instead.
11981 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11982 marked as unread.
11983 The difference between N and the actual number of articles marked is
11984 returned."
11985   (interactive "p")
11986   (gnus-set-global-variables)
11987   (let ((backward (< n 0))
11988         (gnus-summary-goto-unread
11989          (and gnus-summary-goto-unread
11990               (not (eq gnus-summary-goto-unread 'never))
11991               (not (memq mark (list gnus-unread-mark
11992                                     gnus-ticked-mark gnus-dormant-mark)))))
11993         (n (abs n))
11994         (mark (or mark gnus-del-mark)))
11995     (while (and (> n 0)
11996                 (gnus-summary-mark-article nil mark no-expire)
11997                 (zerop (gnus-summary-next-subject
11998                         (if backward -1 1)
11999                         (and gnus-summary-goto-unread
12000                              (not (eq gnus-summary-goto-unread 'never)))
12001                         t)))
12002       (setq n (1- n)))
12003     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12004     (gnus-summary-recenter)
12005     (gnus-summary-position-point)
12006     (gnus-set-mode-line 'summary)
12007     n))
12008
12009 (defun gnus-summary-mark-article-as-read (mark)
12010   "Mark the current article quickly as read with MARK."
12011   (let ((article (gnus-summary-article-number)))
12012     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12013     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12014     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12015     (setq gnus-newsgroup-reads
12016           (cons (cons article mark) gnus-newsgroup-reads))
12017     ;; Possibly remove from cache, if that is used.
12018     (and gnus-use-cache (gnus-cache-enter-remove-article article))
12019     ;; Allow the backend to change the mark.
12020     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
12021     ;; Check for auto-expiry.
12022     (when (and gnus-newsgroup-auto-expire
12023                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12024                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12025                    (= mark gnus-ancient-mark)
12026                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
12027       (setq mark gnus-expirable-mark)
12028       (push article gnus-newsgroup-expirable))
12029     ;; Set the mark in the buffer.
12030     (gnus-summary-update-mark mark 'unread)
12031     t))
12032
12033 (defun gnus-summary-mark-article-as-unread (mark)
12034   "Mark the current article quickly as unread with MARK."
12035   (let ((article (gnus-summary-article-number)))
12036     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12037     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12038     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12039     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
12040     (cond ((= mark gnus-ticked-mark)
12041            (push article gnus-newsgroup-marked))
12042           ((= mark gnus-dormant-mark)
12043            (push article gnus-newsgroup-dormant))
12044           (t
12045            (push article gnus-newsgroup-unreads)))
12046     (setq gnus-newsgroup-reads
12047           (delq (assq article gnus-newsgroup-reads)
12048                 gnus-newsgroup-reads))
12049
12050     ;; See whether the article is to be put in the cache.
12051     (and gnus-use-cache
12052          (vectorp (gnus-summary-article-header article))
12053          (save-excursion
12054            (gnus-cache-possibly-enter-article
12055             gnus-newsgroup-name article
12056             (gnus-summary-article-header article)
12057             (= mark gnus-ticked-mark)
12058             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12059
12060     ;; Fix the mark.
12061     (gnus-summary-update-mark mark 'unread)
12062     t))
12063
12064 (defun gnus-summary-mark-article (&optional article mark no-expire)
12065   "Mark ARTICLE with MARK.  MARK can be any character.
12066 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12067 `??' (dormant) and `?E' (expirable).
12068 If MARK is nil, then the default character `?D' is used.
12069 If ARTICLE is nil, then the article on the current line will be
12070 marked."
12071   ;; The mark might be a string.
12072   (and (stringp mark)
12073        (setq mark (aref mark 0)))
12074   ;; If no mark is given, then we check auto-expiring.
12075   (and (not no-expire)
12076        gnus-newsgroup-auto-expire
12077        (or (not mark)
12078            (and (numberp mark)
12079                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12080                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12081                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12082        (setq mark gnus-expirable-mark))
12083   (let* ((mark (or mark gnus-del-mark))
12084          (article (or article (gnus-summary-article-number))))
12085     (or article (error "No article on current line"))
12086     (if (or (= mark gnus-unread-mark)
12087             (= mark gnus-ticked-mark)
12088             (= mark gnus-dormant-mark))
12089         (gnus-mark-article-as-unread article mark)
12090       (gnus-mark-article-as-read article mark))
12091
12092     ;; See whether the article is to be put in the cache.
12093     (and gnus-use-cache
12094          (not (= mark gnus-canceled-mark))
12095          (vectorp (gnus-summary-article-header article))
12096          (save-excursion
12097            (gnus-cache-possibly-enter-article
12098             gnus-newsgroup-name article
12099             (gnus-summary-article-header article)
12100             (= mark gnus-ticked-mark)
12101             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12102
12103     (if (gnus-summary-goto-subject article nil t)
12104         (let ((buffer-read-only nil))
12105           (gnus-summary-show-thread)
12106           ;; Fix the mark.
12107           (gnus-summary-update-mark mark 'unread)
12108           t))))
12109
12110 (defun gnus-summary-update-secondary-mark (article)
12111   "Update the secondary (read, process, cache) mark."
12112   (gnus-summary-update-mark
12113    (cond ((memq article gnus-newsgroup-processable)
12114           gnus-process-mark)
12115          ((memq article gnus-newsgroup-cached)
12116           gnus-cached-mark)
12117          ((memq article gnus-newsgroup-replied)
12118           gnus-replied-mark)
12119          ((memq article gnus-newsgroup-saved)
12120           gnus-saved-mark)
12121          (t gnus-unread-mark))
12122    'replied)
12123   (when (gnus-visual-p 'summary-highlight 'highlight)
12124     (run-hooks 'gnus-summary-update-hook))
12125   t)
12126
12127 (defun gnus-summary-update-mark (mark type)
12128   (beginning-of-line)
12129   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12130         (buffer-read-only nil))
12131     (when (and forward
12132                (<= (+ forward (point)) (point-max)))
12133       ;; Go to the right position on the line.
12134       (goto-char (+ forward (point)))
12135       ;; Replace the old mark with the new mark.
12136       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12137       ;; Optionally update the marks by some user rule.
12138       (when (eq type 'unread)
12139         (gnus-data-set-mark
12140          (gnus-data-find (gnus-summary-article-number)) mark)
12141         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12142
12143 (defun gnus-mark-article-as-read (article &optional mark)
12144   "Enter ARTICLE in the pertinent lists and remove it from others."
12145   ;; Make the article expirable.
12146   (let ((mark (or mark gnus-del-mark)))
12147     (if (= mark gnus-expirable-mark)
12148         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12149       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12150     ;; Remove from unread and marked lists.
12151     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12152     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12153     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12154     (push (cons article mark) gnus-newsgroup-reads)
12155     ;; Possibly remove from cache, if that is used.
12156     (when gnus-use-cache
12157       (gnus-cache-enter-remove-article article))))
12158
12159 (defun gnus-mark-article-as-unread (article &optional mark)
12160   "Enter ARTICLE in the pertinent lists and remove it from others."
12161   (let ((mark (or mark gnus-ticked-mark)))
12162     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12163     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12164     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12165     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12166     (cond ((= mark gnus-ticked-mark)
12167            (push article gnus-newsgroup-marked))
12168           ((= mark gnus-dormant-mark)
12169            (push article gnus-newsgroup-dormant))
12170           (t
12171            (push article gnus-newsgroup-unreads)))
12172     (setq gnus-newsgroup-reads
12173           (delq (assq article gnus-newsgroup-reads)
12174                 gnus-newsgroup-reads))))
12175
12176 (defalias 'gnus-summary-mark-as-unread-forward
12177   'gnus-summary-tick-article-forward)
12178 (make-obsolete 'gnus-summary-mark-as-unread-forward
12179                'gnus-summary-tick-article-forward)
12180 (defun gnus-summary-tick-article-forward (n)
12181   "Tick N articles forwards.
12182 If N is negative, tick backwards instead.
12183 The difference between N and the number of articles ticked is returned."
12184   (interactive "p")
12185   (gnus-summary-mark-forward n gnus-ticked-mark))
12186
12187 (defalias 'gnus-summary-mark-as-unread-backward
12188   'gnus-summary-tick-article-backward)
12189 (make-obsolete 'gnus-summary-mark-as-unread-backward
12190                'gnus-summary-tick-article-backward)
12191 (defun gnus-summary-tick-article-backward (n)
12192   "Tick N articles backwards.
12193 The difference between N and the number of articles ticked is returned."
12194   (interactive "p")
12195   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12196
12197 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12198 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12199 (defun gnus-summary-tick-article (&optional article clear-mark)
12200   "Mark current article as unread.
12201 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12202 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12203   (interactive)
12204   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12205                                        gnus-ticked-mark)))
12206
12207 (defun gnus-summary-mark-as-read-forward (n)
12208   "Mark N articles as read forwards.
12209 If N is negative, mark backwards instead.
12210 The difference between N and the actual number of articles marked is
12211 returned."
12212   (interactive "p")
12213   (gnus-summary-mark-forward n gnus-del-mark t))
12214
12215 (defun gnus-summary-mark-as-read-backward (n)
12216   "Mark the N articles as read backwards.
12217 The difference between N and the actual number of articles marked is
12218 returned."
12219   (interactive "p")
12220   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12221
12222 (defun gnus-summary-mark-as-read (&optional article mark)
12223   "Mark current article as read.
12224 ARTICLE specifies the article to be marked as read.
12225 MARK specifies a string to be inserted at the beginning of the line."
12226   (gnus-summary-mark-article article mark))
12227
12228 (defun gnus-summary-clear-mark-forward (n)
12229   "Clear marks from N articles forward.
12230 If N is negative, clear backward instead.
12231 The difference between N and the number of marks cleared is returned."
12232   (interactive "p")
12233   (gnus-summary-mark-forward n gnus-unread-mark))
12234
12235 (defun gnus-summary-clear-mark-backward (n)
12236   "Clear marks from N articles backward.
12237 The difference between N and the number of marks cleared is returned."
12238   (interactive "p")
12239   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12240
12241 (defun gnus-summary-mark-unread-as-read ()
12242   "Intended to be used by `gnus-summary-mark-article-hook'."
12243   (when (memq gnus-current-article gnus-newsgroup-unreads)
12244     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12245
12246 (defun gnus-summary-mark-read-and-unread-as-read ()
12247   "Intended to be used by `gnus-summary-mark-article-hook'."
12248   (let ((mark (gnus-summary-article-mark)))
12249     (when (or (gnus-unread-mark-p mark)
12250               (gnus-read-mark-p mark))
12251       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12252
12253 (defun gnus-summary-mark-region-as-read (point mark all)
12254   "Mark all unread articles between point and mark as read.
12255 If given a prefix, mark all articles between point and mark as read,
12256 even ticked and dormant ones."
12257   (interactive "r\nP")
12258   (save-excursion
12259     (let (article)
12260       (goto-char point)
12261       (beginning-of-line)
12262       (while (and
12263               (< (point) mark)
12264               (progn
12265                 (when (or all
12266                           (memq (setq article (gnus-summary-article-number))
12267                                 gnus-newsgroup-unreads))
12268                   (gnus-summary-mark-article article gnus-del-mark))
12269                 t)
12270               (gnus-summary-find-next))))))
12271
12272 (defun gnus-summary-mark-below (score mark)
12273   "Mark articles with score less than SCORE with MARK."
12274   (interactive "P\ncMark: ")
12275   (gnus-set-global-variables)
12276   (setq score (if score
12277                   (prefix-numeric-value score)
12278                 (or gnus-summary-default-score 0)))
12279   (save-excursion
12280     (set-buffer gnus-summary-buffer)
12281     (goto-char (point-min))
12282     (while 
12283         (progn
12284           (and (< (gnus-summary-article-score) score)
12285                (gnus-summary-mark-article nil mark))
12286           (gnus-summary-find-next)))))
12287
12288 (defun gnus-summary-kill-below (&optional score)
12289   "Mark articles with score below SCORE as read."
12290   (interactive "P")
12291   (gnus-set-global-variables)
12292   (gnus-summary-mark-below score gnus-killed-mark))
12293
12294 (defun gnus-summary-clear-above (&optional score)
12295   "Clear all marks from articles with score above SCORE."
12296   (interactive "P")
12297   (gnus-set-global-variables)
12298   (gnus-summary-mark-above score gnus-unread-mark))
12299
12300 (defun gnus-summary-tick-above (&optional score)
12301   "Tick all articles with score above SCORE."
12302   (interactive "P")
12303   (gnus-set-global-variables)
12304   (gnus-summary-mark-above score gnus-ticked-mark))
12305
12306 (defun gnus-summary-mark-above (score mark)
12307   "Mark articles with score over SCORE with MARK."
12308   (interactive "P\ncMark: ")
12309   (gnus-set-global-variables)
12310   (setq score (if score
12311                   (prefix-numeric-value score)
12312                 (or gnus-summary-default-score 0)))
12313   (save-excursion
12314     (set-buffer gnus-summary-buffer)
12315     (goto-char (point-min))
12316     (while (and (progn
12317                   (if (> (gnus-summary-article-score) score)
12318                       (gnus-summary-mark-article nil mark))
12319                   t)
12320                 (gnus-summary-find-next)))))
12321
12322 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12323 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12324 (defun gnus-summary-limit-include-expunged ()
12325   "Display all the hidden articles that were expunged for low scores."
12326   (interactive)
12327   (gnus-set-global-variables)
12328   (let ((buffer-read-only nil))
12329     (let ((scored gnus-newsgroup-scored)
12330           headers h)
12331       (while scored
12332         (or (gnus-summary-goto-subject (caar scored))
12333             (and (setq h (gnus-summary-article-header (caar scored)))
12334                  (< (cdar scored) gnus-summary-expunge-below)
12335                  (setq headers (cons h headers))))
12336         (setq scored (cdr scored)))
12337       (or headers (error "No expunged articles hidden."))
12338       (goto-char (point-min))
12339       (gnus-summary-prepare-unthreaded (nreverse headers)))
12340     (goto-char (point-min))
12341     (gnus-summary-position-point)))
12342
12343 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12344   "Mark all articles not marked as unread in this newsgroup as read.
12345 If prefix argument ALL is non-nil, all articles are marked as read.
12346 If QUIETLY is non-nil, no questions will be asked.
12347 If TO-HERE is non-nil, it should be a point in the buffer.  All
12348 articles before this point will be marked as read.
12349 The number of articles marked as read is returned."
12350   (interactive "P")
12351   (gnus-set-global-variables)
12352   (prog1
12353       (if (or quietly
12354               (not gnus-interactive-catchup) ;Without confirmation?
12355               gnus-expert-user
12356               (gnus-y-or-n-p
12357                (if all
12358                    "Mark absolutely all articles as read? "
12359                  "Mark all unread articles as read? ")))
12360           (if (and not-mark
12361                    (not gnus-newsgroup-adaptive)
12362                    (not gnus-newsgroup-auto-expire))
12363               (progn
12364                 (when all
12365                   (setq gnus-newsgroup-marked nil
12366                         gnus-newsgroup-dormant nil))
12367                 (setq gnus-newsgroup-unreads nil))
12368             ;; We actually mark all articles as canceled, which we
12369             ;; have to do when using auto-expiry or adaptive scoring.
12370             (gnus-summary-show-all-threads)
12371             (if (gnus-summary-first-subject (not all))
12372                 (while (and
12373                         (if to-here (< (point) to-here) t)
12374                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12375                         (gnus-summary-find-next (not all)))))
12376             (unless to-here
12377               (setq gnus-newsgroup-unreads nil))
12378             (gnus-set-mode-line 'summary)))
12379     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12380       (if (and (not to-here) (eq 'nnvirtual (car method)))
12381           (nnvirtual-catchup-group
12382            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12383     (gnus-summary-position-point)))
12384
12385 (defun gnus-summary-catchup-to-here (&optional all)
12386   "Mark all unticked articles before the current one as read.
12387 If ALL is non-nil, also mark ticked and dormant articles as read."
12388   (interactive "P")
12389   (gnus-set-global-variables)
12390   (save-excursion
12391     (let ((beg (point)))
12392       ;; We check that there are unread articles.
12393       (when (or all (gnus-summary-find-prev))
12394         (gnus-summary-catchup all t beg))))
12395   (gnus-summary-position-point))
12396
12397 (defun gnus-summary-catchup-all (&optional quietly)
12398   "Mark all articles in this newsgroup as read."
12399   (interactive "P")
12400   (gnus-set-global-variables)
12401   (gnus-summary-catchup t quietly))
12402
12403 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12404   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12405 If prefix argument ALL is non-nil, all articles are marked as read."
12406   (interactive "P")
12407   (gnus-set-global-variables)
12408   (gnus-summary-catchup all quietly nil 'fast)
12409   ;; Select next newsgroup or exit.
12410   (if (eq gnus-auto-select-next 'quietly)
12411       (gnus-summary-next-group nil)
12412     (gnus-summary-exit)))
12413
12414 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12415   "Mark all articles in this newsgroup as read, and then exit."
12416   (interactive "P")
12417   (gnus-set-global-variables)
12418   (gnus-summary-catchup-and-exit t quietly))
12419
12420 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12421 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12422   "Mark all articles in this group as read and select the next group.
12423 If given a prefix, mark all articles, unread as well as ticked, as
12424 read."
12425   (interactive "P")
12426   (gnus-set-global-variables)
12427   (save-excursion
12428     (gnus-summary-catchup all))
12429   (gnus-summary-next-article t nil nil t))
12430
12431 ;; Thread-based commands.
12432
12433 (defun gnus-summary-articles-in-thread (&optional article)
12434   "Return a list of all articles in the current thread.
12435 If ARTICLE is non-nil, return all articles in the thread that starts
12436 with that article."
12437   (let* ((article (or article (gnus-summary-article-number)))
12438          (data (gnus-data-find-list article))
12439          (top-level (gnus-data-level (car data)))
12440          (top-subject
12441           (cond ((null gnus-thread-operation-ignore-subject)
12442                  (gnus-simplify-subject-re
12443                   (mail-header-subject (gnus-data-header (car data)))))
12444                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12445                  (gnus-simplify-subject-fuzzy
12446                   (mail-header-subject (gnus-data-header (car data)))))
12447                 (t nil)))
12448          (end-point (save-excursion
12449                       (if (gnus-summary-go-to-next-thread) 
12450                           (point) (point-max))))
12451          articles)
12452     (while (and data
12453                 (< (gnus-data-pos (car data)) end-point))
12454       (when (or (not top-subject)
12455                 (string= top-subject
12456                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12457                              (gnus-simplify-subject-fuzzy
12458                               (mail-header-subject
12459                                (gnus-data-header (car data))))
12460                            (gnus-simplify-subject-re
12461                             (mail-header-subject
12462                              (gnus-data-header (car data)))))))
12463         (push (gnus-data-number (car data)) articles))
12464       (unless (and (setq data (cdr data))
12465                    (> (gnus-data-level (car data)) top-level))
12466         (setq data nil)))
12467     ;; Return the list of articles.
12468     (nreverse articles)))
12469
12470 (defun gnus-summary-rethread-current ()
12471   "Rethread the thread the current article is part of."
12472   (interactive)
12473   (gnus-set-global-variables)
12474   (let* ((gnus-show-threads t)
12475          (article (gnus-summary-article-number))
12476          (id (mail-header-id (gnus-summary-article-header)))
12477          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12478     (unless id
12479       (error "No article on the current line"))
12480     (gnus-rebuild-thread id)
12481     (gnus-summary-goto-subject article)))
12482
12483 (defun gnus-summary-reparent-thread ()
12484   "Make current article child of the marked (or previous) article.
12485
12486 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12487 is non-nil or the Subject: of both articles are the same."
12488   (interactive)
12489   (or (not (gnus-group-read-only-p))
12490       (error "The current newsgroup does not support article editing."))
12491   (or (<= (length gnus-newsgroup-processable) 1)
12492       (error "No more than one article may be marked."))
12493   (save-window-excursion
12494     (let ((gnus-article-buffer " *reparent*")
12495           (current-article (gnus-summary-article-number))
12496           ; first grab the marked article, otherwise one line up.
12497           (parent-article (if (not (null gnus-newsgroup-processable))
12498                               (car gnus-newsgroup-processable)
12499                             (save-excursion
12500                               (if (eq (forward-line -1) 0)
12501                                   (gnus-summary-article-number)
12502                                 (error "Beginning of summary buffer."))))))
12503       (or (not (eq current-article parent-article))
12504           (error "An article may not be self-referential."))
12505       (let ((message-id (mail-header-id 
12506                          (gnus-summary-article-header parent-article))))
12507         (or (and message-id (not (equal message-id "")))
12508             (error "No message-id in desired parent."))
12509         (gnus-summary-select-article t t nil current-article)
12510         (set-buffer gnus-article-buffer)
12511         (setq buffer-read-only nil)
12512         (let ((buf (format "%s" (buffer-string))))
12513           (erase-buffer)
12514           (insert buf))
12515         (goto-char (point-min))
12516         (if (search-forward-regexp "^References: " nil t)
12517             (insert message-id " " )
12518           (insert "References: " message-id "\n"))
12519         (or (gnus-request-replace-article current-article
12520                                           (car gnus-article-current)
12521                                           gnus-article-buffer)
12522             (error "Couldn't replace article."))
12523         (set-buffer gnus-summary-buffer)
12524         (gnus-summary-unmark-all-processable)
12525         (gnus-summary-rethread-current)
12526         (gnus-message 3 "Article %d is now the child of article %d."
12527                       current-article parent-article)))))
12528
12529 (defun gnus-summary-toggle-threads (&optional arg)
12530   "Toggle showing conversation threads.
12531 If ARG is positive number, turn showing conversation threads on."
12532   (interactive "P")
12533   (gnus-set-global-variables)
12534   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12535     (setq gnus-show-threads
12536           (if (null arg) (not gnus-show-threads)
12537             (> (prefix-numeric-value arg) 0)))
12538     (gnus-summary-prepare)
12539     (gnus-summary-goto-subject current)
12540     (gnus-summary-position-point)))
12541
12542 (defun gnus-summary-show-all-threads ()
12543   "Show all threads."
12544   (interactive)
12545   (gnus-set-global-variables)
12546   (save-excursion
12547     (let ((buffer-read-only nil))
12548       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12549   (gnus-summary-position-point))
12550
12551 (defun gnus-summary-show-thread ()
12552   "Show thread subtrees.
12553 Returns nil if no thread was there to be shown."
12554   (interactive)
12555   (gnus-set-global-variables)
12556   (let ((buffer-read-only nil)
12557         (orig (point))
12558         ;; first goto end then to beg, to have point at beg after let
12559         (end (progn (end-of-line) (point)))
12560         (beg (progn (beginning-of-line) (point))))
12561     (prog1
12562         ;; Any hidden lines here?
12563         (search-forward "\r" end t)
12564       (subst-char-in-region beg end ?\^M ?\n t)
12565       (goto-char orig)
12566       (gnus-summary-position-point))))
12567
12568 (defun gnus-summary-hide-all-threads ()
12569   "Hide all thread subtrees."
12570   (interactive)
12571   (gnus-set-global-variables)
12572   (save-excursion
12573     (goto-char (point-min))
12574     (gnus-summary-hide-thread)
12575     (while (zerop (gnus-summary-next-thread 1 t))
12576       (gnus-summary-hide-thread)))
12577   (gnus-summary-position-point))
12578
12579 (defun gnus-summary-hide-thread ()
12580   "Hide thread subtrees.
12581 Returns nil if no threads were there to be hidden."
12582   (interactive)
12583   (gnus-set-global-variables)
12584   (let ((buffer-read-only nil)
12585         (start (point))
12586         (article (gnus-summary-article-number)))
12587     (goto-char start)
12588     ;; Go forward until either the buffer ends or the subthread
12589     ;; ends.
12590     (when (and (not (eobp))
12591                (or (zerop (gnus-summary-next-thread 1 t))
12592                    (goto-char (point-max))))
12593       (prog1
12594           (if (and (> (point) start)
12595                    (search-backward "\n" start t))
12596               (progn
12597                 (subst-char-in-region start (point) ?\n ?\^M)
12598                 (gnus-summary-goto-subject article))
12599             (goto-char start)
12600             nil)
12601         ;;(gnus-summary-position-point)
12602         ))))
12603
12604 (defun gnus-summary-go-to-next-thread (&optional previous)
12605   "Go to the same level (or less) next thread.
12606 If PREVIOUS is non-nil, go to previous thread instead.
12607 Return the article number moved to, or nil if moving was impossible."
12608   (let ((level (gnus-summary-thread-level))
12609         (way (if previous -1 1))
12610         (beg (point)))
12611     (forward-line way)
12612     (while (and (not (eobp))
12613                 (< level (gnus-summary-thread-level)))
12614       (forward-line way))
12615     (if (eobp)
12616         (progn
12617           (goto-char beg)
12618           nil)
12619       (setq beg (point))
12620       (prog1
12621           (gnus-summary-article-number)
12622         (goto-char beg)))))
12623
12624 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12625   "Go to the same level (or less) next thread.
12626 If PREVIOUS is non-nil, go to previous thread instead.
12627 Return the article number moved to, or nil if moving was impossible."
12628   (if (and (eq gnus-summary-make-false-root 'dummy)
12629            (gnus-summary-article-intangible-p))
12630       (let ((beg (point)))
12631         (while (and (zerop (forward-line 1))
12632                     (not (gnus-summary-article-intangible-p))
12633                     (not (zerop (save-excursion 
12634                                   (gnus-summary-thread-level))))))
12635         (if (eobp)
12636             (progn
12637               (goto-char beg)
12638               nil)
12639           (point)))
12640     (let* ((level (gnus-summary-thread-level))
12641            (article (gnus-summary-article-number))
12642            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12643            oart)
12644       (while data
12645         (if (<= (gnus-data-level (car data)) level)
12646             (setq oart (gnus-data-number (car data))
12647                   data nil)
12648           (setq data (cdr data))))
12649       (and oart
12650            (gnus-summary-goto-subject oart)))))
12651
12652 (defun gnus-summary-next-thread (n &optional silent)
12653   "Go to the same level next N'th thread.
12654 If N is negative, search backward instead.
12655 Returns the difference between N and the number of skips actually
12656 done.
12657
12658 If SILENT, don't output messages."
12659   (interactive "p")
12660   (gnus-set-global-variables)
12661   (let ((backward (< n 0))
12662         (n (abs n))
12663         old dum int)
12664     (while (and (> n 0)
12665                 (gnus-summary-go-to-next-thread backward))
12666       (decf n))
12667     (unless silent 
12668       (gnus-summary-position-point))
12669     (when (and (not silent) (/= 0 n))
12670       (gnus-message 7 "No more threads"))
12671     n))
12672
12673 (defun gnus-summary-prev-thread (n)
12674   "Go to the same level previous N'th thread.
12675 Returns the difference between N and the number of skips actually
12676 done."
12677   (interactive "p")
12678   (gnus-set-global-variables)
12679   (gnus-summary-next-thread (- n)))
12680
12681 (defun gnus-summary-go-down-thread ()
12682   "Go down one level in the current thread."
12683   (let ((children (gnus-summary-article-children)))
12684     (and children
12685          (gnus-summary-goto-subject (car children)))))
12686
12687 (defun gnus-summary-go-up-thread ()
12688   "Go up one level in the current thread."
12689   (let ((parent (gnus-summary-article-parent)))
12690     (and parent
12691          (gnus-summary-goto-subject parent))))
12692
12693 (defun gnus-summary-down-thread (n)
12694   "Go down thread N steps.
12695 If N is negative, go up instead.
12696 Returns the difference between N and how many steps down that were
12697 taken."
12698   (interactive "p")
12699   (gnus-set-global-variables)
12700   (let ((up (< n 0))
12701         (n (abs n)))
12702     (while (and (> n 0)
12703                 (if up (gnus-summary-go-up-thread)
12704                   (gnus-summary-go-down-thread)))
12705       (setq n (1- n)))
12706     (gnus-summary-position-point)
12707     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12708     n))
12709
12710 (defun gnus-summary-up-thread (n)
12711   "Go up thread N steps.
12712 If N is negative, go up instead.
12713 Returns the difference between N and how many steps down that were
12714 taken."
12715   (interactive "p")
12716   (gnus-set-global-variables)
12717   (gnus-summary-down-thread (- n)))
12718
12719 (defun gnus-summary-top-thread ()
12720   "Go to the top of the thread."
12721   (interactive)
12722   (gnus-set-global-variables)
12723   (while (gnus-summary-go-up-thread))
12724   (gnus-summary-article-number))
12725
12726 (defun gnus-summary-kill-thread (&optional unmark)
12727   "Mark articles under current thread as read.
12728 If the prefix argument is positive, remove any kinds of marks.
12729 If the prefix argument is negative, tick articles instead."
12730   (interactive "P")
12731   (gnus-set-global-variables)
12732   (if unmark
12733       (setq unmark (prefix-numeric-value unmark)))
12734   (let ((articles (gnus-summary-articles-in-thread)))
12735     (save-excursion
12736       ;; Expand the thread.
12737       (gnus-summary-show-thread)
12738       ;; Mark all the articles.
12739       (while articles
12740         (gnus-summary-goto-subject (car articles))
12741         (cond ((null unmark)
12742                (gnus-summary-mark-article-as-read gnus-killed-mark))
12743               ((> unmark 0)
12744                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12745               (t
12746                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12747         (setq articles (cdr articles))))
12748     ;; Hide killed subtrees.
12749     (and (null unmark)
12750          gnus-thread-hide-killed
12751          (gnus-summary-hide-thread))
12752     ;; If marked as read, go to next unread subject.
12753     (if (null unmark)
12754         ;; Go to next unread subject.
12755         (gnus-summary-next-subject 1 t)))
12756   (gnus-set-mode-line 'summary))
12757
12758 ;; Summary sorting commands
12759
12760 (defun gnus-summary-sort-by-number (&optional reverse)
12761   "Sort summary buffer by article number.
12762 Argument REVERSE means reverse order."
12763   (interactive "P")
12764   (gnus-summary-sort 'number reverse))
12765
12766 (defun gnus-summary-sort-by-author (&optional reverse)
12767   "Sort summary buffer by author name alphabetically.
12768 If case-fold-search is non-nil, case of letters is ignored.
12769 Argument REVERSE means reverse order."
12770   (interactive "P")
12771   (gnus-summary-sort 'author reverse))
12772
12773 (defun gnus-summary-sort-by-subject (&optional reverse)
12774   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12775 If case-fold-search is non-nil, case of letters is ignored.
12776 Argument REVERSE means reverse order."
12777   (interactive "P")
12778   (gnus-summary-sort 'subject reverse))
12779
12780 (defun gnus-summary-sort-by-date (&optional reverse)
12781   "Sort summary buffer by date.
12782 Argument REVERSE means reverse order."
12783   (interactive "P")
12784   (gnus-summary-sort 'date reverse))
12785
12786 (defun gnus-summary-sort-by-score (&optional reverse)
12787   "Sort summary buffer by score.
12788 Argument REVERSE means reverse order."
12789   (interactive "P")
12790   (gnus-summary-sort 'score reverse))
12791
12792 (defun gnus-summary-sort (predicate reverse)
12793   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12794   (gnus-set-global-variables)
12795   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12796          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12797          (gnus-thread-sort-functions
12798           (list
12799            (if (not reverse)
12800                thread
12801              `(lambda (t1 t2)
12802                 (,thread t2 t1)))))
12803          (gnus-article-sort-functions
12804           (list
12805            (if (not reverse)
12806                article
12807              `(lambda (t1 t2)
12808                 (,article t2 t1)))))
12809          (buffer-read-only)
12810          (gnus-summary-prepare-hook nil))
12811     ;; We do the sorting by regenerating the threads.
12812     (gnus-summary-prepare)
12813     ;; Hide subthreads if needed.
12814     (when (and gnus-show-threads gnus-thread-hide-subtree)
12815       (gnus-summary-hide-all-threads)))
12816   ;; If in async mode, we send some info to the backend.
12817   (when gnus-newsgroup-async
12818     (gnus-request-asynchronous
12819      gnus-newsgroup-name gnus-newsgroup-data)))
12820
12821 (defun gnus-sortable-date (date)
12822   "Make sortable string by string-lessp from DATE.
12823 Timezone package is used."
12824   (setq date (timezone-fix-time date nil nil))
12825   (timezone-make-sortable-date
12826    (aref date 0) (aref date 2) (aref date 2)
12827    (timezone-make-time-string
12828     (aref date 3) (aref date 4) (aref date 5))))
12829
12830 ;; Summary saving commands.
12831
12832 (defun gnus-summary-save-article (&optional n not-saved)
12833   "Save the current article using the default saver function.
12834 If N is a positive number, save the N next articles.
12835 If N is a negative number, save the N previous articles.
12836 If N is nil and any articles have been marked with the process mark,
12837 save those articles instead.
12838 The variable `gnus-default-article-saver' specifies the saver function."
12839   (interactive "P")
12840   (gnus-set-global-variables)
12841   (let ((articles (gnus-summary-work-articles n))
12842         file header article)
12843     (while articles
12844       (setq header (gnus-summary-article-header
12845                     (setq article (pop articles))))
12846       (if (not (vectorp header))
12847           ;; This is a pseudo-article.
12848           (if (assq 'name header)
12849               (gnus-copy-file (cdr (assq 'name header)))
12850             (gnus-message 1 "Article %d is unsaveable" article))
12851         ;; This is a real article.
12852         (save-window-excursion
12853           (gnus-summary-select-article t nil nil article))
12854         (unless gnus-save-all-headers
12855           ;; Remove headers accoring to `gnus-saved-headers'.
12856           (let ((gnus-visible-headers
12857                  (or gnus-saved-headers gnus-visible-headers)))
12858             (gnus-article-hide-headers nil t)))
12859         ;; Remove any X-Gnus lines.
12860         (save-excursion
12861           (set-buffer gnus-article-buffer)
12862           (save-restriction
12863             (let ((buffer-read-only nil))
12864               (nnheader-narrow-to-headers)
12865               (while (re-search-forward "^X-Gnus" nil t)
12866                 (gnus-delete-line)))))
12867         (save-window-excursion
12868           (if (not gnus-default-article-saver)
12869               (error "No default saver is defined.")
12870             (setq file (funcall
12871                         gnus-default-article-saver
12872                         (cond
12873                          ((not gnus-prompt-before-saving)
12874                           'default)
12875                          ((eq gnus-prompt-before-saving 'always)
12876                           nil)
12877                          (t file))))))
12878         (gnus-summary-remove-process-mark article)
12879         (unless not-saved
12880           (gnus-summary-set-saved-mark article))))
12881     (gnus-summary-position-point)
12882     n))
12883
12884 (defun gnus-summary-pipe-output (&optional arg)
12885   "Pipe the current article to a subprocess.
12886 If N is a positive number, pipe the N next articles.
12887 If N is a negative number, pipe the N previous articles.
12888 If N is nil and any articles have been marked with the process mark,
12889 pipe those articles instead."
12890   (interactive "P")
12891   (gnus-set-global-variables)
12892   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12893     (gnus-summary-save-article arg t))
12894   (gnus-configure-windows 'pipe))
12895
12896 (defun gnus-summary-save-article-mail (&optional arg)
12897   "Append the current article to an mail file.
12898 If N is a positive number, save the N next articles.
12899 If N is a negative number, save the N previous articles.
12900 If N is nil and any articles have been marked with the process mark,
12901 save those articles instead."
12902   (interactive "P")
12903   (gnus-set-global-variables)
12904   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12905     (gnus-summary-save-article arg)))
12906
12907 (defun gnus-summary-save-article-rmail (&optional arg)
12908   "Append the current article to an rmail file.
12909 If N is a positive number, save the N next articles.
12910 If N is a negative number, save the N previous articles.
12911 If N is nil and any articles have been marked with the process mark,
12912 save those articles instead."
12913   (interactive "P")
12914   (gnus-set-global-variables)
12915   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12916     (gnus-summary-save-article arg)))
12917
12918 (defun gnus-summary-save-article-file (&optional arg)
12919   "Append the current article to a file.
12920 If N is a positive number, save the N next articles.
12921 If N is a negative number, save the N previous articles.
12922 If N is nil and any articles have been marked with the process mark,
12923 save those articles instead."
12924   (interactive "P")
12925   (gnus-set-global-variables)
12926   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12927     (gnus-summary-save-article arg)))
12928
12929 (defun gnus-summary-save-article-body-file (&optional arg)
12930   "Append the current article body to a file.
12931 If N is a positive number, save the N next articles.
12932 If N is a negative number, save the N previous articles.
12933 If N is nil and any articles have been marked with the process mark,
12934 save those articles instead."
12935   (interactive "P")
12936   (gnus-set-global-variables)
12937   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12938     (gnus-summary-save-article arg)))
12939
12940 (defun gnus-get-split-value (methods)
12941   "Return a value based on the split METHODS."
12942   (let (split-name method result match)
12943     (when methods
12944       (save-excursion
12945         (set-buffer gnus-original-article-buffer)
12946         (save-restriction
12947           (nnheader-narrow-to-headers)
12948           (while methods
12949             (goto-char (point-min))
12950             (setq method (pop methods))
12951             (setq match (car method))
12952             (when (cond
12953                    ((stringp match)
12954                     ;; Regular expression.
12955                     (condition-case ()
12956                         (re-search-forward match nil t)
12957                       (error nil)))
12958                    ((gnus-functionp match)
12959                     ;; Function.
12960                     (save-restriction
12961                       (widen)
12962                       (setq result (funcall match gnus-newsgroup-name))))
12963                    ((consp match)
12964                     ;; Form.
12965                     (save-restriction
12966                       (widen)
12967                       (setq result (eval match)))))
12968               (setq split-name (append (cdr method) split-name))
12969               (cond ((stringp result)
12970                      (push result split-name))
12971                     ((consp result)
12972                      (setq split-name (append result split-name)))))))))
12973     split-name))
12974
12975 (defun gnus-read-move-group-name (prompt default articles prefix)
12976   "Read a group name."
12977   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12978          group-map
12979          (dum (mapatoms
12980                (lambda (g) 
12981                  (and (boundp g)
12982                       (symbol-name g)
12983                       (memq 'respool
12984                             (assoc (symbol-name
12985                                     (car (gnus-find-method-for-group
12986                                           (symbol-name g))))
12987                                    gnus-valid-select-methods))
12988                       (push (list (symbol-name g)) group-map)))
12989                gnus-active-hashtb))
12990          (prom
12991           (format "%s %s to:"
12992                   prompt
12993                   (if (> (length articles) 1)
12994                       (format "these %d articles" (length articles))
12995                     "this article")))
12996          (to-newsgroup
12997           (cond
12998            ((null split-name)
12999             (gnus-completing-read default prom
13000                                   group-map nil nil prefix
13001                                   'gnus-group-history))
13002            ((= 1 (length split-name))
13003             (gnus-completing-read (car split-name) prom group-map
13004                                   nil nil nil
13005                                   'gnus-group-history))
13006            (t
13007             (gnus-completing-read nil prom 
13008                                   (mapcar (lambda (el) (list el))
13009                                           (nreverse split-name))
13010                                   nil nil nil
13011                                   'gnus-group-history)))))
13012     (when to-newsgroup
13013       (if (or (string= to-newsgroup "")
13014               (string= to-newsgroup prefix))
13015           (setq to-newsgroup (or default "")))
13016       (or (gnus-active to-newsgroup)
13017           (gnus-activate-group to-newsgroup)
13018           (error "No such group: %s" to-newsgroup)))
13019     to-newsgroup))
13020
13021 (defun gnus-read-save-file-name (prompt default-name)
13022   (let* ((split-name (gnus-get-split-value gnus-split-methods))
13023          (file
13024           ;; Let the split methods have their say.
13025           (cond
13026            ;; No split name was found.
13027            ((null split-name)
13028             (read-file-name
13029              (concat prompt " (default "
13030                      (file-name-nondirectory default-name) ") ")
13031              (file-name-directory default-name)
13032              default-name))
13033            ;; A single split name was found
13034            ((= 1 (length split-name))
13035             (read-file-name
13036              (concat prompt " (default " (car split-name) ") ")
13037              gnus-article-save-directory
13038              (concat gnus-article-save-directory (car split-name))))
13039            ;; A list of splits was found.
13040            (t
13041             (setq split-name (nreverse split-name))
13042             (let (result)
13043               (let ((file-name-history (nconc split-name file-name-history)))
13044                 (setq result
13045                       (read-file-name
13046                        (concat prompt " (`M-p' for defaults) ")
13047                        gnus-article-save-directory
13048                        (car split-name))))
13049               (car (push result file-name-history)))))))
13050     ;; If we have read a directory, we append the default file name.
13051     (when (file-directory-p file)
13052       (setq file (concat (file-name-as-directory file)
13053                          (file-name-nondirectory default-name))))
13054     ;; Possibly translate some charaters.
13055     (nnheader-translate-file-chars file)))
13056
13057 (defun gnus-article-archive-name (group)
13058   "Return the first instance of an \"Archive-name\" in the current buffer."
13059   (let ((case-fold-search t))
13060     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13061       (match-string 1))))
13062
13063 (defun gnus-summary-save-in-rmail (&optional filename)
13064   "Append this article to Rmail file.
13065 Optional argument FILENAME specifies file name.
13066 Directory to save to is default to `gnus-article-save-directory'."
13067   (interactive)
13068   (gnus-set-global-variables)
13069   (let ((default-name
13070           (funcall gnus-rmail-save-name gnus-newsgroup-name
13071                    gnus-current-headers gnus-newsgroup-last-rmail)))
13072     (setq filename
13073           (cond ((eq filename 'default)
13074                  default-name)
13075                 (filename filename)
13076                 (t (gnus-read-save-file-name
13077                     "Save in rmail file:" default-name))))
13078     (gnus-make-directory (file-name-directory filename))
13079     (gnus-eval-in-buffer-window
13080      gnus-original-article-buffer
13081      (save-excursion
13082        (save-restriction
13083          (widen)
13084          (gnus-output-to-rmail filename))))
13085     ;; Remember the directory name to save articles
13086     (setq gnus-newsgroup-last-rmail filename)))
13087
13088 (defun gnus-summary-save-in-mail (&optional filename)
13089   "Append this article to Unix mail file.
13090 Optional argument FILENAME specifies file name.
13091 Directory to save to is default to `gnus-article-save-directory'."
13092   (interactive)
13093   (gnus-set-global-variables)
13094   (let ((default-name
13095           (funcall gnus-mail-save-name gnus-newsgroup-name
13096                    gnus-current-headers gnus-newsgroup-last-mail)))
13097     (setq filename
13098           (cond ((eq filename 'default)
13099                  default-name)
13100                 (filename filename)
13101                 (t (gnus-read-save-file-name
13102                     "Save in Unix mail file:" default-name))))
13103     (setq filename
13104           (expand-file-name filename
13105                             (and default-name
13106                                  (file-name-directory default-name))))
13107     (gnus-make-directory (file-name-directory filename))
13108     (gnus-eval-in-buffer-window
13109      gnus-original-article-buffer
13110      (save-excursion
13111        (save-restriction
13112          (widen)
13113          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13114              (gnus-output-to-rmail filename)
13115            (let ((mail-use-rfc822 t))
13116              (rmail-output filename 1 t t))))))
13117     ;; Remember the directory name to save articles.
13118     (setq gnus-newsgroup-last-mail filename)))
13119
13120 (defun gnus-summary-save-in-file (&optional filename)
13121   "Append this article to file.
13122 Optional argument FILENAME specifies file name.
13123 Directory to save to is default to `gnus-article-save-directory'."
13124   (interactive)
13125   (gnus-set-global-variables)
13126   (let ((default-name
13127           (funcall gnus-file-save-name gnus-newsgroup-name
13128                    gnus-current-headers gnus-newsgroup-last-file)))
13129     (setq filename
13130           (cond ((eq filename 'default)
13131                  default-name)
13132                 (filename filename)
13133                 (t (gnus-read-save-file-name
13134                     "Save in file:" default-name))))
13135     (gnus-make-directory (file-name-directory filename))
13136     (gnus-eval-in-buffer-window
13137      gnus-original-article-buffer
13138      (save-excursion
13139        (save-restriction
13140          (widen)
13141          (gnus-output-to-file filename))))
13142     ;; Remember the directory name to save articles.
13143     (setq gnus-newsgroup-last-file filename)))
13144
13145 (defun gnus-summary-save-body-in-file (&optional filename)
13146   "Append this article body to a file.
13147 Optional argument FILENAME specifies file name.
13148 The directory to save in defaults to `gnus-article-save-directory'."
13149   (interactive)
13150   (gnus-set-global-variables)
13151   (let ((default-name
13152           (funcall gnus-file-save-name gnus-newsgroup-name
13153                    gnus-current-headers gnus-newsgroup-last-file)))
13154     (setq filename
13155           (cond ((eq filename 'default)
13156                  default-name)
13157                 (filename filename)
13158                 (t (gnus-read-save-file-name
13159                     "Save body in file:" default-name))))
13160     (gnus-make-directory (file-name-directory filename))
13161     (gnus-eval-in-buffer-window
13162      gnus-article-buffer
13163      (save-excursion
13164        (save-restriction
13165          (widen)
13166          (goto-char (point-min))
13167          (and (search-forward "\n\n" nil t)
13168               (narrow-to-region (point) (point-max)))
13169          (gnus-output-to-file filename))))
13170     ;; Remember the directory name to save articles.
13171     (setq gnus-newsgroup-last-file filename)))
13172
13173 (defun gnus-summary-save-in-pipe (&optional command)
13174   "Pipe this article to subprocess."
13175   (interactive)
13176   (gnus-set-global-variables)
13177   (setq command
13178         (cond ((eq command 'default)
13179                gnus-last-shell-command)
13180               (command command)
13181               (t (read-string "Shell command on article: "
13182                               gnus-last-shell-command))))
13183   (if (string-equal command "")
13184       (setq command gnus-last-shell-command))
13185   (gnus-eval-in-buffer-window
13186    gnus-article-buffer
13187    (save-restriction
13188      (widen)
13189      (shell-command-on-region (point-min) (point-max) command nil)))
13190   (setq gnus-last-shell-command command))
13191
13192 ;; Summary extract commands
13193
13194 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13195   (let ((buffer-read-only nil)
13196         (article (gnus-summary-article-number))
13197         after-article b e)
13198     (or (gnus-summary-goto-subject article)
13199         (error (format "No such article: %d" article)))
13200     (gnus-summary-position-point)
13201     ;; If all commands are to be bunched up on one line, we collect
13202     ;; them here.
13203     (if gnus-view-pseudos-separately
13204         ()
13205       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13206             files action)
13207         (while ps
13208           (setq action (cdr (assq 'action (car ps))))
13209           (setq files (list (cdr (assq 'name (car ps)))))
13210           (while (and ps (cdr ps)
13211                       (string= (or action "1")
13212                                (or (cdr (assq 'action (cadr ps))) "2")))
13213             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13214             (setcdr ps (cddr ps)))
13215           (if (not files)
13216               ()
13217             (if (not (string-match "%s" action))
13218                 (setq files (cons " " files)))
13219             (setq files (cons " " files))
13220             (and (assq 'execute (car ps))
13221                  (setcdr (assq 'execute (car ps))
13222                          (funcall (if (string-match "%s" action)
13223                                       'format 'concat)
13224                                   action
13225                                   (mapconcat (lambda (f) f) files " ")))))
13226           (setq ps (cdr ps)))))
13227     (if (and gnus-view-pseudos (not not-view))
13228         (while pslist
13229           (and (assq 'execute (car pslist))
13230                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13231                                      (eq gnus-view-pseudos 'not-confirm)))
13232           (setq pslist (cdr pslist)))
13233       (save-excursion
13234         (while pslist
13235           (setq after-article (or (cdr (assq 'article (car pslist)))
13236                                   (gnus-summary-article-number)))
13237           (gnus-summary-goto-subject after-article)
13238           (forward-line 1)
13239           (setq b (point))
13240           (insert "    " (file-name-nondirectory
13241                                 (cdr (assq 'name (car pslist))))
13242                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13243           (setq e (point))
13244           (forward-line -1)             ; back to `b'
13245           (gnus-add-text-properties
13246            b e (list 'gnus-number gnus-reffed-article-number
13247                      gnus-mouse-face-prop gnus-mouse-face))
13248           (gnus-data-enter
13249            after-article gnus-reffed-article-number
13250            gnus-unread-mark b (car pslist) 0 (- e b))
13251           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13252           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13253           (setq pslist (cdr pslist)))))))
13254
13255 (defun gnus-pseudos< (p1 p2)
13256   (let ((c1 (cdr (assq 'action p1)))
13257         (c2 (cdr (assq 'action p2))))
13258     (and c1 c2 (string< c1 c2))))
13259
13260 (defun gnus-request-pseudo-article (props)
13261   (cond ((assq 'execute props)
13262          (gnus-execute-command (cdr (assq 'execute props)))))
13263   (let ((gnus-current-article (gnus-summary-article-number)))
13264     (run-hooks 'gnus-mark-article-hook)))
13265
13266 (defun gnus-execute-command (command &optional automatic)
13267   (save-excursion
13268     (gnus-article-setup-buffer)
13269     (set-buffer gnus-article-buffer)
13270     (setq buffer-read-only nil)
13271     (let ((command (if automatic command (read-string "Command: " command)))
13272           ;; Just binding this here doesn't help, because there might
13273           ;; be output from the process after exiting the scope of 
13274           ;; this `let'.
13275           ;; (buffer-read-only nil)
13276           )
13277       (erase-buffer)
13278       (insert "$ " command "\n\n")
13279       (if gnus-view-pseudo-asynchronously
13280           (start-process "gnus-execute" nil "sh" "-c" command)
13281         (call-process "sh" nil t nil "-c" command)))))
13282
13283 (defun gnus-copy-file (file &optional to)
13284   "Copy FILE to TO."
13285   (interactive
13286    (list (read-file-name "Copy file: " default-directory)
13287          (read-file-name "Copy file to: " default-directory)))
13288   (gnus-set-global-variables)
13289   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13290   (and (file-directory-p to)
13291        (setq to (concat (file-name-as-directory to)
13292                         (file-name-nondirectory file))))
13293   (copy-file file to))
13294
13295 ;; Summary kill commands.
13296
13297 (defun gnus-summary-edit-global-kill (article)
13298   "Edit the \"global\" kill file."
13299   (interactive (list (gnus-summary-article-number)))
13300   (gnus-set-global-variables)
13301   (gnus-group-edit-global-kill article))
13302
13303 (defun gnus-summary-edit-local-kill ()
13304   "Edit a local kill file applied to the current newsgroup."
13305   (interactive)
13306   (gnus-set-global-variables)
13307   (setq gnus-current-headers (gnus-summary-article-header))
13308   (gnus-set-global-variables)
13309   (gnus-group-edit-local-kill
13310    (gnus-summary-article-number) gnus-newsgroup-name))
13311
13312 \f
13313 ;;;
13314 ;;; Gnus article mode
13315 ;;;
13316
13317 (put 'gnus-article-mode 'mode-class 'special)
13318
13319 (if gnus-article-mode-map
13320     nil
13321   (setq gnus-article-mode-map (make-keymap))
13322   (suppress-keymap gnus-article-mode-map)
13323
13324   (gnus-define-keys gnus-article-mode-map
13325     " " gnus-article-goto-next-page
13326     "\177" gnus-article-goto-prev-page
13327     [delete] gnus-article-goto-prev-page
13328     "\C-c^" gnus-article-refer-article
13329     "h" gnus-article-show-summary
13330     "s" gnus-article-show-summary
13331     "\C-c\C-m" gnus-article-mail
13332     "?" gnus-article-describe-briefly
13333     gnus-mouse-2 gnus-article-push-button
13334     "\r" gnus-article-press-button
13335     "\t" gnus-article-next-button
13336     "\M-\t" gnus-article-prev-button
13337     "\C-c\C-b" gnus-bug)
13338
13339   (substitute-key-definition
13340    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13341
13342 (defun gnus-article-mode ()
13343   "Major mode for displaying an article.
13344
13345 All normal editing commands are switched off.
13346
13347 The following commands are available:
13348
13349 \\<gnus-article-mode-map>
13350 \\[gnus-article-next-page]\t Scroll the article one page forwards
13351 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13352 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13353 \\[gnus-article-show-summary]\t Display the summary buffer
13354 \\[gnus-article-mail]\t Send a reply to the address near point
13355 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13356 \\[gnus-info-find-node]\t Go to the Gnus info node"
13357   (interactive)
13358   (when (and menu-bar-mode
13359              (gnus-visual-p 'article-menu 'menu))
13360     (gnus-article-make-menu-bar))
13361   (kill-all-local-variables)
13362   (gnus-simplify-mode-line)
13363   (setq mode-name "Article")
13364   (setq major-mode 'gnus-article-mode)
13365   (make-local-variable 'minor-mode-alist)
13366   (or (assq 'gnus-show-mime minor-mode-alist)
13367       (setq minor-mode-alist
13368             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13369   (use-local-map gnus-article-mode-map)
13370   (make-local-variable 'page-delimiter)
13371   (setq page-delimiter gnus-page-delimiter)
13372   (buffer-disable-undo (current-buffer))
13373   (setq buffer-read-only t)             ;Disable modification
13374   (run-hooks 'gnus-article-mode-hook))
13375
13376 (defun gnus-article-setup-buffer ()
13377   "Initialize the article buffer."
13378   (let* ((name (if gnus-single-article-buffer "*Article*"
13379                  (concat "*Article " gnus-newsgroup-name "*")))
13380          (original
13381           (progn (string-match "\\*Article" name)
13382                  (concat " *Original Article"
13383                          (substring name (match-end 0))))))
13384     (setq gnus-article-buffer name)
13385     (setq gnus-original-article-buffer original)
13386     ;; This might be a variable local to the summary buffer.
13387     (unless gnus-single-article-buffer
13388       (save-excursion
13389         (set-buffer gnus-summary-buffer)
13390         (setq gnus-article-buffer name)
13391         (setq gnus-original-article-buffer original)
13392         (gnus-set-global-variables))
13393       (make-local-variable 'gnus-summary-buffer))
13394     ;; Init original article buffer.
13395     (save-excursion
13396       (set-buffer (get-buffer-create gnus-original-article-buffer))
13397       (buffer-disable-undo (current-buffer))
13398       (setq major-mode 'gnus-original-article-mode)
13399       (make-local-variable 'gnus-original-article))
13400     (if (get-buffer name)
13401         (save-excursion
13402           (set-buffer name)
13403           (buffer-disable-undo (current-buffer))
13404           (setq buffer-read-only t)
13405           (gnus-add-current-to-buffer-list)
13406           (or (eq major-mode 'gnus-article-mode)
13407               (gnus-article-mode))
13408           (current-buffer))
13409       (save-excursion
13410         (set-buffer (get-buffer-create name))
13411         (gnus-add-current-to-buffer-list)
13412         (gnus-article-mode)
13413         (current-buffer)))))
13414
13415 ;; Set article window start at LINE, where LINE is the number of lines
13416 ;; from the head of the article.
13417 (defun gnus-article-set-window-start (&optional line)
13418   (set-window-start
13419    (get-buffer-window gnus-article-buffer t)
13420    (save-excursion
13421      (set-buffer gnus-article-buffer)
13422      (goto-char (point-min))
13423      (if (not line)
13424          (point-min)
13425        (gnus-message 6 "Moved to bookmark")
13426        (search-forward "\n\n" nil t)
13427        (forward-line line)
13428        (point)))))
13429
13430 (defun gnus-kill-all-overlays ()
13431   "Delete all overlays in the current buffer."
13432   (when (fboundp 'overlay-lists)
13433     (let* ((overlayss (overlay-lists))
13434            (buffer-read-only nil)
13435            (overlays (nconc (car overlayss) (cdr overlayss))))
13436       (while overlays
13437         (delete-overlay (pop overlays))))))
13438
13439 (defun gnus-request-article-this-buffer (article group)
13440   "Get an article and insert it into this buffer."
13441   (let (do-update-line)
13442     (prog1
13443         (save-excursion
13444           (erase-buffer)
13445           (gnus-kill-all-overlays)
13446           (setq group (or group gnus-newsgroup-name))
13447
13448           ;; Open server if it has closed.
13449           (gnus-check-server (gnus-find-method-for-group group))
13450
13451           ;; Using `gnus-request-article' directly will insert the article into
13452           ;; `nntp-server-buffer' - so we'll save some time by not having to
13453           ;; copy it from the server buffer into the article buffer.
13454
13455           ;; We only request an article by message-id when we do not have the
13456           ;; headers for it, so we'll have to get those.
13457           (when (stringp article)
13458             (let ((gnus-override-method gnus-refer-article-method))
13459               (gnus-read-header article)))
13460
13461           ;; If the article number is negative, that means that this article
13462           ;; doesn't belong in this newsgroup (possibly), so we find its
13463           ;; message-id and request it by id instead of number.
13464           (when (and (numberp article)
13465                      gnus-summary-buffer
13466                      (get-buffer gnus-summary-buffer)
13467                      (buffer-name (get-buffer gnus-summary-buffer)))
13468             (save-excursion
13469               (set-buffer gnus-summary-buffer)
13470               (let ((header (gnus-summary-article-header article)))
13471                 (if (< article 0)
13472                     (cond 
13473                      ((memq article gnus-newsgroup-sparse)
13474                       ;; This is a sparse gap article.
13475                       (setq do-update-line article)
13476                       (setq article (mail-header-id header))
13477                       (let ((gnus-override-method gnus-refer-article-method))
13478                         (gnus-read-header article))
13479                       (setq gnus-newsgroup-sparse
13480                             (delq article gnus-newsgroup-sparse)))
13481                      ((vectorp header)
13482                       ;; It's a real article.
13483                       (setq article (mail-header-id header)))
13484                      (t
13485                       ;; It is an extracted pseudo-article.
13486                       (setq article 'pseudo)
13487                       (gnus-request-pseudo-article header))))
13488                 
13489                 (let ((method (gnus-find-method-for-group 
13490                                gnus-newsgroup-name)))
13491                   (if (not (eq (car method) 'nneething))
13492                       ()
13493                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13494                                        (mail-header-subject header))))
13495                       (if (file-directory-p dir)
13496                           (progn
13497                             (setq article 'nneething)
13498                             (gnus-group-enter-directory dir)))))))))
13499
13500           (cond
13501            ;; Refuse to select canceled articles.
13502            ((and (numberp article)
13503                  gnus-summary-buffer
13504                  (get-buffer gnus-summary-buffer)
13505                  (buffer-name (get-buffer gnus-summary-buffer))
13506                  (eq (cdr (save-excursion
13507                             (set-buffer gnus-summary-buffer)
13508                             (assq article gnus-newsgroup-reads)))
13509                      gnus-canceled-mark))
13510             nil)
13511            ;; We first check `gnus-original-article-buffer'.
13512            ((and (get-buffer gnus-original-article-buffer)
13513                  (numberp article)
13514                  (save-excursion
13515                    (set-buffer gnus-original-article-buffer)
13516                    (and (equal (car gnus-original-article) group)
13517                         (eq (cdr gnus-original-article) article))))
13518             (insert-buffer-substring gnus-original-article-buffer)
13519             'article)
13520            ;; Check the backlog.
13521            ((and gnus-keep-backlog
13522                  (gnus-backlog-request-article group article (current-buffer)))
13523             'article)
13524            ;; Check the cache.
13525            ((and gnus-use-cache
13526                  (numberp article)
13527                  (gnus-cache-request-article article group))
13528             'article)
13529            ;; Get the article and put into the article buffer.
13530            ((or (stringp article) (numberp article))
13531             (let ((gnus-override-method
13532                    (and (stringp article) gnus-refer-article-method))
13533                   (buffer-read-only nil))
13534               (erase-buffer)
13535               (gnus-kill-all-overlays)
13536               (if (gnus-request-article article group (current-buffer))
13537                   (progn
13538                     (and gnus-keep-backlog
13539                          (numberp article)
13540                          (gnus-backlog-enter-article
13541                           group article (current-buffer)))
13542                     'article))))
13543            ;; It was a pseudo.
13544            (t article)))
13545
13546       ;; Take the article from the original article buffer
13547       ;; and place it in the buffer it's supposed to be in.
13548       (when (and (get-buffer gnus-article-buffer)
13549                  (numberp article)
13550                  (equal (buffer-name (current-buffer))
13551                         (buffer-name (get-buffer gnus-article-buffer))))
13552         (save-excursion
13553           (if (get-buffer gnus-original-article-buffer)
13554               (set-buffer (get-buffer gnus-original-article-buffer))
13555             (set-buffer (get-buffer-create gnus-original-article-buffer))
13556             (buffer-disable-undo (current-buffer))
13557             (setq major-mode 'gnus-original-article-mode)
13558             (setq buffer-read-only t)
13559             (gnus-add-current-to-buffer-list))
13560           (let (buffer-read-only)
13561             (erase-buffer)
13562             (insert-buffer-substring gnus-article-buffer))
13563           (setq gnus-original-article (cons group article))))
13564     
13565       ;; Update sparse articles.
13566       (when (and do-update-line
13567                  (or (numberp article)
13568                      (stringp article)))
13569         (let ((buf (current-buffer)))
13570           (set-buffer gnus-summary-buffer)
13571           (gnus-summary-update-article do-update-line)
13572           (gnus-summary-goto-subject do-update-line nil t)
13573           (set-window-point (get-buffer-window (current-buffer) t)
13574                             (point))
13575           (set-buffer buf))))))
13576
13577 (defun gnus-read-header (id &optional header)
13578   "Read the headers of article ID and enter them into the Gnus system."
13579   (let ((group gnus-newsgroup-name)
13580         (gnus-override-method 
13581          (and (gnus-news-group-p gnus-newsgroup-name)
13582               gnus-refer-article-method))       
13583         where)
13584     ;; First we check to see whether the header in question is already
13585     ;; fetched.
13586     (if (stringp id)
13587         ;; This is a Message-ID.
13588         (setq header (or header (gnus-id-to-header id)))
13589       ;; This is an article number.
13590       (setq header (or header (gnus-summary-article-header id))))
13591     (if (and header
13592              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13593         ;; We have found the header.
13594         header
13595       ;; We have to really fetch the header to this article.
13596       (when (setq where
13597                   (if (gnus-check-backend-function 'request-head group)
13598                       (gnus-request-head id group)
13599                     (gnus-request-article id group)))
13600         (save-excursion
13601           (set-buffer nntp-server-buffer)
13602           (and (search-forward "\n\n" nil t)
13603                (delete-region (1- (point)) (point-max)))
13604           (goto-char (point-max))
13605           (insert ".\n")
13606           (goto-char (point-min))
13607           (insert "211 ")
13608           (princ (cond
13609                   ((numberp id) id)
13610                   ((cdr where) (cdr where))
13611                   (header (mail-header-number header))
13612                   (t gnus-reffed-article-number))
13613                  (current-buffer))
13614           (insert " Article retrieved.\n"))
13615         ;(when (and header
13616         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13617         ;  (setcar (gnus-id-to-thread id) nil))
13618         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13619             ()                          ; Malformed head.
13620           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13621             (if (and (stringp id)
13622                      (not (string= (gnus-group-real-name group)
13623                                    (car where))))
13624                 ;; If we fetched by Message-ID and the article came
13625                 ;; from a different group, we fudge some bogus article
13626                 ;; numbers for this article.
13627                 (mail-header-set-number header gnus-reffed-article-number))
13628             (decf gnus-reffed-article-number)
13629             (push header gnus-newsgroup-headers)
13630             (setq gnus-current-headers header)
13631             (push (mail-header-number header) gnus-newsgroup-limit))
13632           header)))))
13633
13634 (defun gnus-article-prepare (article &optional all-headers header)
13635   "Prepare ARTICLE in article mode buffer.
13636 ARTICLE should either be an article number or a Message-ID.
13637 If ARTICLE is an id, HEADER should be the article headers.
13638 If ALL-HEADERS is non-nil, no headers are hidden."
13639   (save-excursion
13640     ;; Make sure we start in a summary buffer.
13641     (unless (eq major-mode 'gnus-summary-mode)
13642       (set-buffer gnus-summary-buffer))
13643     (setq gnus-summary-buffer (current-buffer))
13644     ;; Make sure the connection to the server is alive.
13645     (unless (gnus-server-opened
13646              (gnus-find-method-for-group gnus-newsgroup-name))
13647       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13648       (gnus-request-group gnus-newsgroup-name t))
13649     (let* ((article (if header (mail-header-number header) article))
13650            (summary-buffer (current-buffer))
13651            (internal-hook gnus-article-internal-prepare-hook)
13652            (group gnus-newsgroup-name)
13653            result)
13654       (save-excursion
13655         (gnus-article-setup-buffer)
13656         (set-buffer gnus-article-buffer)
13657         ;; Deactivate active regions.
13658         (when (and (boundp 'transient-mark-mode)
13659                    transient-mark-mode)
13660           (setq mark-active nil))
13661         (if (not (setq result (let ((buffer-read-only nil))
13662                                 (gnus-request-article-this-buffer
13663                                  article group))))
13664             ;; There is no such article.
13665             (save-excursion
13666               (when (and (numberp article)
13667                          (not (memq article gnus-newsgroup-sparse)))
13668                 (setq gnus-article-current
13669                       (cons gnus-newsgroup-name article))
13670                 (set-buffer gnus-summary-buffer)
13671                 (setq gnus-current-article article)
13672                 (gnus-summary-mark-article article gnus-canceled-mark))
13673               (unless (memq article gnus-newsgroup-sparse)
13674                 (gnus-error
13675                  1 "No such article (may have expired or been canceled)")))
13676           (if (or (eq result 'pseudo) (eq result 'nneething))
13677               (progn
13678                 (save-excursion
13679                   (set-buffer summary-buffer)
13680                   (setq gnus-last-article gnus-current-article
13681                         gnus-newsgroup-history (cons gnus-current-article
13682                                                      gnus-newsgroup-history)
13683                         gnus-current-article 0
13684                         gnus-current-headers nil
13685                         gnus-article-current nil)
13686                   (if (eq result 'nneething)
13687                       (gnus-configure-windows 'summary)
13688                     (gnus-configure-windows 'article))
13689                   (gnus-set-global-variables))
13690                 (gnus-set-mode-line 'article))
13691             ;; The result from the `request' was an actual article -
13692             ;; or at least some text that is now displayed in the
13693             ;; article buffer.
13694             (if (and (numberp article)
13695                      (not (eq article gnus-current-article)))
13696                 ;; Seems like a new article has been selected.
13697                 ;; `gnus-current-article' must be an article number.
13698                 (save-excursion
13699                   (set-buffer summary-buffer)
13700                   (setq gnus-last-article gnus-current-article
13701                         gnus-newsgroup-history (cons gnus-current-article
13702                                                      gnus-newsgroup-history)
13703                         gnus-current-article article
13704                         gnus-current-headers
13705                         (gnus-summary-article-header gnus-current-article)
13706                         gnus-article-current
13707                         (cons gnus-newsgroup-name gnus-current-article))
13708                   (unless (vectorp gnus-current-headers)
13709                     (setq gnus-current-headers nil))
13710                   (gnus-summary-show-thread)
13711                   (run-hooks 'gnus-mark-article-hook)
13712                   (gnus-set-mode-line 'summary)
13713                   (and (gnus-visual-p 'article-highlight 'highlight)
13714                        (run-hooks 'gnus-visual-mark-article-hook))
13715                   ;; Set the global newsgroup variables here.
13716                   ;; Suggested by Jim Sisolak
13717                   ;; <sisolak@trans4.neep.wisc.edu>.
13718                   (gnus-set-global-variables)
13719                   (setq gnus-have-all-headers
13720                         (or all-headers gnus-show-all-headers))
13721                   (and gnus-use-cache
13722                        (vectorp (gnus-summary-article-header article))
13723                        (gnus-cache-possibly-enter-article
13724                         group article
13725                         (gnus-summary-article-header article)
13726                         (memq article gnus-newsgroup-marked)
13727                         (memq article gnus-newsgroup-dormant)
13728                         (memq article gnus-newsgroup-unreads)))))
13729             (when (or (numberp article)
13730                       (stringp article))
13731               ;; Hooks for getting information from the article.
13732               ;; This hook must be called before being narrowed.
13733               (let (buffer-read-only)
13734                 (run-hooks 'internal-hook)
13735                 (run-hooks 'gnus-article-prepare-hook)
13736                 ;; Decode MIME message.
13737                 (if gnus-show-mime
13738                     (if (or (not gnus-strict-mime)
13739                             (gnus-fetch-field "Mime-Version"))
13740                         (funcall gnus-show-mime-method)
13741                       (funcall gnus-decode-encoded-word-method)))
13742                 ;; Perform the article display hooks.
13743                 (run-hooks 'gnus-article-display-hook))
13744               ;; Do page break.
13745               (goto-char (point-min))
13746               (and gnus-break-pages (gnus-narrow-to-page)))
13747             (gnus-set-mode-line 'article)
13748             (gnus-configure-windows 'article)
13749             (goto-char (point-min))
13750             t))))))
13751
13752 (defun gnus-article-show-all-headers ()
13753   "Show all article headers in article mode buffer."
13754   (save-excursion
13755     (gnus-article-setup-buffer)
13756     (set-buffer gnus-article-buffer)
13757     (let ((buffer-read-only nil))
13758       (gnus-unhide-text (point-min) (point-max)))))
13759
13760 (defun gnus-article-hide-headers-if-wanted ()
13761   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13762 Provided for backwards compatibility."
13763   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13764       gnus-inhibit-hiding
13765       (gnus-article-hide-headers)))
13766
13767 (defsubst gnus-article-header-rank ()
13768   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13769   (let ((list gnus-sorted-header-list)
13770         (i 0))
13771     (while list
13772       (when (looking-at (car list))
13773         (setq list nil))
13774       (setq list (cdr list))
13775       (incf i))
13776     i))
13777
13778 (defun gnus-article-hide-headers (&optional arg delete)
13779   "Toggle whether to hide unwanted headers and possibly sort them as well.
13780 If given a negative prefix, always show; if given a positive prefix,
13781 always hide."
13782   (interactive "P")
13783   (unless (gnus-article-check-hidden-text 'headers arg)
13784     ;; This function might be inhibited.
13785     (unless gnus-inhibit-hiding
13786       (save-excursion
13787         (set-buffer gnus-article-buffer)
13788         (save-restriction
13789           (let ((buffer-read-only nil)
13790                 (props (nconc (list 'gnus-type 'headers)
13791                               gnus-hidden-properties))
13792                 (max (1+ (length gnus-sorted-header-list)))
13793                 (ignored (when (not (stringp gnus-visible-headers))
13794                            (cond ((stringp gnus-ignored-headers)
13795                                   gnus-ignored-headers)
13796                                  ((listp gnus-ignored-headers)
13797                                   (mapconcat 'identity gnus-ignored-headers
13798                                              "\\|")))))
13799                 (visible
13800                  (cond ((stringp gnus-visible-headers)
13801                         gnus-visible-headers)
13802                        ((and gnus-visible-headers
13803                              (listp gnus-visible-headers))
13804                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13805                 want-list beg)
13806             ;; First we narrow to just the headers.
13807             (widen)
13808             (goto-char (point-min))
13809             ;; Hide any "From " lines at the beginning of (mail) articles.
13810             (while (looking-at "From ")
13811               (forward-line 1))
13812             (unless (bobp)
13813               (gnus-hide-text (point-min) (point) props))
13814             ;; Then treat the rest of the header lines.
13815             (narrow-to-region
13816              (point)
13817              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13818             ;; Then we use the two regular expressions
13819             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13820             ;; select which header lines is to remain visible in the
13821             ;; article buffer.
13822             (goto-char (point-min))
13823             (while (re-search-forward "^[^ \t]*:" nil t)
13824               (beginning-of-line)
13825               ;; We add the headers we want to keep to a list and delete
13826               ;; them from the buffer.
13827               (gnus-put-text-property 
13828                (point) (1+ (point)) 'message-rank
13829                (if (or (and visible (looking-at visible))
13830                        (and ignored
13831                             (not (looking-at ignored))))
13832                    (gnus-article-header-rank) 
13833                  (+ 2 max)))
13834               (forward-line 1))
13835             (message-sort-headers-1)
13836             (when (setq beg (text-property-any 
13837                              (point-min) (point-max) 'message-rank (+ 2 max)))
13838               ;; We make the unwanted headers invisible.
13839               (if delete
13840                   (delete-region beg (point-max))
13841                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13842                 (gnus-hide-text-type beg (point-max) 'headers))
13843               ;; Work around XEmacs lossage.
13844               (gnus-put-text-property (point-min) beg 'invisible nil))))))))
13845
13846 (defun gnus-article-hide-boring-headers (&optional arg)
13847   "Toggle hiding of headers that aren't very interesting.
13848 If given a negative prefix, always show; if given a positive prefix,
13849 always hide."
13850   (interactive "P")
13851   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13852     (save-excursion
13853       (set-buffer gnus-article-buffer)
13854       (save-restriction
13855         (let ((buffer-read-only nil)
13856               (list gnus-boring-article-headers)
13857               (inhibit-point-motion-hooks t)
13858               elem)
13859           (nnheader-narrow-to-headers)
13860           (while list
13861             (setq elem (pop list))
13862             (goto-char (point-min))
13863             (cond
13864              ;; Hide empty headers.
13865              ((eq elem 'empty)
13866               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13867                 (forward-line -1)
13868                 (gnus-hide-text-type
13869                  (progn (beginning-of-line) (point))
13870                  (progn 
13871                    (end-of-line)
13872                    (if (re-search-forward "^[^ \t]" nil t)
13873                        (match-beginning 0)
13874                      (point-max)))
13875                  'boring-headers)))
13876              ;; Hide boring Newsgroups header.
13877              ((eq elem 'newsgroups)
13878               (when (equal (message-fetch-field "newsgroups")
13879                            (gnus-group-real-name gnus-newsgroup-name))
13880                 (gnus-article-hide-header "newsgroups")))
13881              ((eq elem 'followup-to)
13882               (when (equal (message-fetch-field "followup-to")
13883                            (message-fetch-field "newsgroups"))
13884                 (gnus-article-hide-header "followup-to")))
13885              ((eq elem 'reply-to)
13886               (let ((from (message-fetch-field "from"))
13887                     (reply-to (message-fetch-field "reply-to")))
13888                 (when (and
13889                        from reply-to
13890                        (equal 
13891                         (nth 1 (funcall gnus-extract-address-components from))
13892                         (nth 1 (funcall gnus-extract-address-components
13893                                         reply-to))))
13894                   (gnus-article-hide-header "reply-to"))))
13895              ((eq elem 'date)
13896               (let ((date (message-fetch-field "date")))
13897                 (when (and date
13898                            (< (gnus-days-between date (current-time-string))
13899                               4))
13900                   (gnus-article-hide-header "date")))))))))))
13901
13902 (defun gnus-article-hide-header (header)
13903   (save-excursion
13904     (goto-char (point-min))
13905     (when (re-search-forward (concat "^" header ":") nil t)
13906       (gnus-hide-text-type
13907        (progn (beginning-of-line) (point))
13908        (progn 
13909          (end-of-line)
13910          (if (re-search-forward "^[^ \t]" nil t)
13911              (match-beginning 0)
13912            (point-max)))
13913        'boring-headers))))
13914
13915 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13916 (defun gnus-article-treat-overstrike ()
13917   "Translate overstrikes into bold text."
13918   (interactive)
13919   (save-excursion
13920     (set-buffer gnus-article-buffer)
13921     (let ((buffer-read-only nil))
13922       (while (search-forward "\b" nil t)
13923         (let ((next (following-char))
13924               (previous (char-after (- (point) 2))))
13925           (cond ((eq next previous)
13926                  (gnus-put-text-property (- (point) 2) (point) 'invisible t)
13927                  (gnus-put-text-property (point) (1+ (point)) 'face 'bold))
13928                 ((eq next ?_)
13929                  (gnus-put-text-property (1- (point)) (1+ (point)) 'invisible t)
13930                  (gnus-put-text-property
13931                   (- (point) 2) (1- (point)) 'face 'underline))
13932                 ((eq previous ?_)
13933                  (gnus-put-text-property (- (point) 2) (point) 'invisible t)
13934                  (gnus-put-text-property
13935                   (point) (1+ (point))  'face 'underline))))))))
13936
13937 (defun gnus-article-word-wrap ()
13938   "Format too long lines."
13939   (interactive)
13940   (save-excursion
13941     (set-buffer gnus-article-buffer)
13942     (let ((buffer-read-only nil))
13943       (widen)
13944       (goto-char (point-min))
13945       (search-forward "\n\n" nil t)
13946       (end-of-line 1)
13947       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13948             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13949             (adaptive-fill-mode t))
13950         (while (not (eobp))
13951           (and (>= (current-column) (min fill-column (window-width)))
13952                (/= (preceding-char) ?:)
13953                (fill-paragraph nil))
13954           (end-of-line 2))))))
13955
13956 (defun gnus-article-remove-cr ()
13957   "Remove carriage returns from an article."
13958   (interactive)
13959   (save-excursion
13960     (set-buffer gnus-article-buffer)
13961     (let ((buffer-read-only nil))
13962       (goto-char (point-min))
13963       (while (search-forward "\r" nil t)
13964         (replace-match "" t t)))))
13965
13966 (defun gnus-article-remove-trailing-blank-lines ()
13967   "Remove all trailing blank lines from the article."
13968   (interactive)
13969   (save-excursion
13970     (set-buffer gnus-article-buffer)
13971     (let ((buffer-read-only nil))
13972       (goto-char (point-max))
13973       (delete-region
13974        (point)
13975        (progn
13976          (while (looking-at "^[ \t]*$")
13977            (forward-line -1))
13978          (forward-line 1)
13979          (point))))))
13980
13981 (defun gnus-article-display-x-face (&optional force)
13982   "Look for an X-Face header and display it if present."
13983   (interactive (list 'force))
13984   (save-excursion
13985     (set-buffer gnus-article-buffer)
13986     ;; Delete the old process, if any.
13987     (when (process-status "gnus-x-face")
13988       (delete-process "gnus-x-face"))
13989     (let ((inhibit-point-motion-hooks t)
13990           (case-fold-search nil)
13991           from)
13992       (save-restriction
13993         (nnheader-narrow-to-headers)
13994         (setq from (message-fetch-field "from"))
13995         (goto-char (point-min))
13996         (when (and gnus-article-x-face-command
13997                    (or force
13998                        ;; Check whether this face is censored.
13999                        (not gnus-article-x-face-too-ugly)
14000                        (and gnus-article-x-face-too-ugly from
14001                             (not (string-match gnus-article-x-face-too-ugly
14002                                                from))))
14003                    ;; Has to be present.
14004                    (re-search-forward "^X-Face: " nil t))
14005           ;; We now have the area of the buffer where the X-Face is stored.
14006           (let ((beg (point))
14007                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
14008             ;; We display the face.
14009             (if (symbolp gnus-article-x-face-command)
14010                 ;; The command is a lisp function, so we call it.
14011                 (if (gnus-functionp gnus-article-x-face-command)
14012                     (funcall gnus-article-x-face-command beg end)
14013                   (error "%s is not a function" gnus-article-x-face-command))
14014               ;; The command is a string, so we interpret the command
14015               ;; as a, well, command, and fork it off.
14016               (let ((process-connection-type nil))
14017                 (process-kill-without-query
14018                  (start-process
14019                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
14020                 (process-send-region "gnus-x-face" beg end)
14021                 (process-send-eof "gnus-x-face")))))))))
14022
14023 (defalias 'gnus-header-decode-quoted-printable 'gnus-decode-rfc1522)
14024 (defun gnus-decode-rfc1522 ()
14025   "Hack to remove QP encoding from headers."
14026   (let ((case-fold-search t)
14027         (inhibit-point-motion-hooks t)
14028         (buffer-read-only nil)
14029         string)
14030     (save-restriction
14031       (narrow-to-region
14032        (goto-char (point-min))
14033        (or (search-forward "\n\n" nil t) (point-max)))
14034
14035       (while (re-search-forward 
14036               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
14037         (setq string (match-string 1))
14038         (narrow-to-region (match-beginning 0) (match-end 0))
14039         (delete-region (point-min) (point-max))
14040         (insert string)
14041         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
14042         (subst-char-in-region (point-min) (point-max) ?_ ? )
14043         (widen)
14044         (goto-char (point-min))))))
14045
14046 (defun gnus-article-de-quoted-unreadable (&optional force)
14047   "Do a naive translation of a quoted-printable-encoded article.
14048 This is in no way, shape or form meant as a replacement for real MIME
14049 processing, but is simply a stop-gap measure until MIME support is
14050 written.
14051 If FORCE, decode the article whether it is marked as quoted-printable
14052 or not."
14053   (interactive (list 'force))
14054   (save-excursion
14055     (set-buffer gnus-article-buffer)
14056     (let ((case-fold-search t)
14057           (buffer-read-only nil)
14058           (type (gnus-fetch-field "content-transfer-encoding")))
14059       (gnus-decode-rfc1522)
14060       (when (or force
14061                 (and type (string-match "quoted-printable" (downcase type))))
14062         (goto-char (point-min))
14063         (search-forward "\n\n" nil 'move)
14064         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14065
14066 (defun gnus-mime-decode-quoted-printable (from to)
14067   "Decode Quoted-Printable in the region between FROM and TO."
14068   (interactive "r")
14069   (goto-char from)
14070   (while (search-forward "=" to t)
14071     (cond ((eq (following-char) ?\n)
14072            (delete-char -1)
14073            (delete-char 1))
14074           ((looking-at "[0-9A-F][0-9A-F]")
14075            (subst-char-in-region
14076             (1- (point)) (point) ?=
14077             (hexl-hex-string-to-integer
14078              (buffer-substring (point) (+ 2 (point)))))
14079            (delete-char 2))
14080           ((looking-at "=")
14081            (delete-char 1))
14082           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14083
14084 (defun gnus-article-hide-pgp (&optional arg)
14085   "Toggle hiding of any PGP headers and signatures in the current article.
14086 If given a negative prefix, always show; if given a positive prefix,
14087 always hide."
14088   (interactive "P")
14089   (unless (gnus-article-check-hidden-text 'pgp arg)
14090     (save-excursion
14091       (set-buffer gnus-article-buffer)
14092       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14093             buffer-read-only beg end)
14094         (widen)
14095         (goto-char (point-min))
14096         ;; Hide the "header".
14097         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14098              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14099         (setq beg (point))
14100         ;; Hide the actual signature.
14101         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14102              (setq end (1+ (match-beginning 0)))
14103              (gnus-hide-text
14104               end
14105               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14106                   (match-end 0)
14107                 ;; Perhaps we shouldn't hide to the end of the buffer
14108                 ;; if there is no end to the signature?
14109                 (point-max))
14110               props))
14111         ;; Hide "- " PGP quotation markers.
14112         (when (and beg end)
14113           (narrow-to-region beg end)
14114           (goto-char (point-min))
14115           (while (re-search-forward "^- " nil t)
14116             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14117           (widen))))))
14118
14119 (defun gnus-article-hide-signature (&optional arg)
14120   "Hide the signature in the current article.
14121 If given a negative prefix, always show; if given a positive prefix,
14122 always hide."
14123   (interactive "P")
14124   (unless (gnus-article-check-hidden-text 'signature arg)
14125     (save-excursion
14126       (set-buffer gnus-article-buffer)
14127       (save-restriction
14128         (let ((buffer-read-only nil))
14129           (when (gnus-narrow-to-signature)
14130             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14131
14132 (defun gnus-article-strip-leading-blank-lines ()
14133   "Remove all blank lines from the beginning of the article."
14134   (interactive)
14135   (save-excursion
14136     (set-buffer gnus-article-buffer)
14137     (let (buffer-read-only)
14138       (goto-char (point-min))
14139       (when (search-forward "\n\n" nil t)
14140         (while (looking-at "[ \t]$")
14141           (gnus-delete-line))))))
14142
14143 (defun gnus-narrow-to-signature ()
14144   "Narrow to the signature."
14145   (widen)
14146   (goto-char (point-max))
14147   (when (re-search-backward gnus-signature-separator nil t)
14148     (forward-line 1)
14149     (when (or (null gnus-signature-limit)
14150               (and (numberp gnus-signature-limit)
14151                    (< (- (point-max) (point)) gnus-signature-limit))
14152               (and (gnus-functionp gnus-signature-limit)
14153                    (funcall gnus-signature-limit))
14154               (and (stringp gnus-signature-limit)
14155                    (not (re-search-forward gnus-signature-limit nil t))))
14156       (narrow-to-region (point) (point-max))
14157       t)))
14158
14159 (defun gnus-article-check-hidden-text (type arg)
14160   "Return nil if hiding is necessary."
14161   (save-excursion
14162     (set-buffer gnus-article-buffer)
14163     (let ((hide (gnus-article-hidden-text-p type)))
14164       (cond ((or (and (null arg) (eq hide 'hidden))
14165                  (and arg (< 0 (prefix-numeric-value arg))))
14166              (gnus-article-show-hidden-text type))
14167             ((eq hide 'shown)
14168              (gnus-article-show-hidden-text type t))
14169             (t nil)))))
14170
14171 (defun gnus-article-hidden-text-p (type)
14172   "Say whether the current buffer contains hidden text of type TYPE."
14173   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14174     (when pos
14175       (if (get-text-property pos 'invisible)
14176           'hidden
14177         'shown))))
14178
14179 (defun gnus-article-hide (&optional arg force)
14180   "Hide all the gruft in the current article.
14181 This means that PGP stuff, signatures, cited text and (some)
14182 headers will be hidden.
14183 If given a prefix, show the hidden text instead."
14184   (interactive (list current-prefix-arg 'force))
14185   (gnus-article-hide-headers arg)
14186   (gnus-article-hide-pgp arg)
14187   (gnus-article-hide-citation-maybe arg force)
14188   (gnus-article-hide-signature arg))
14189
14190 (defun gnus-article-show-hidden-text (type &optional hide)
14191   "Show all hidden text of type TYPE.
14192 If HIDE, hide the text instead."
14193   (save-excursion
14194     (set-buffer gnus-article-buffer)
14195     (let ((buffer-read-only nil)
14196           (inhibit-point-motion-hooks t)
14197           (beg (point-min)))
14198       (while (gnus-goto-char (text-property-any
14199                               beg (point-max) 'gnus-type type))
14200         (setq beg (point))
14201         (forward-char)
14202         (if hide
14203             (gnus-hide-text beg (point) gnus-hidden-properties)
14204           (gnus-unhide-text beg (point)))
14205         (setq beg (point)))
14206       t)))
14207
14208 (defvar gnus-article-time-units
14209   `((year . ,(* 365.25 24 60 60))
14210     (week . ,(* 7 24 60 60))
14211     (day . ,(* 24 60 60))
14212     (hour . ,(* 60 60))
14213     (minute . 60)
14214     (second . 1))
14215   "Mapping from time units to seconds.")
14216
14217 (defun gnus-article-date-ut (&optional type highlight)
14218   "Convert DATE date to universal time in the current article.
14219 If TYPE is `local', convert to local time; if it is `lapsed', output
14220 how much time has lapsed since DATE."
14221   (interactive (list 'ut t))
14222   (let* ((header (or gnus-current-headers
14223                      (gnus-summary-article-header) ""))
14224          (date (and (vectorp header) (mail-header-date header)))
14225          (date-regexp "^Date: \\|^X-Sent: ")
14226          (now (current-time))
14227          (inhibit-point-motion-hooks t)
14228          bface eface)
14229     (when (and date (not (string= date "")))
14230       (save-excursion
14231         (set-buffer gnus-article-buffer)
14232         (save-restriction
14233           (nnheader-narrow-to-headers)
14234           (let ((buffer-read-only nil))
14235             ;; Delete any old Date headers.
14236             (if (re-search-forward date-regexp nil t)
14237                 (progn
14238                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14239                         eface (get-text-property (1- (gnus-point-at-eol))
14240                                                  'face))
14241                   (message-remove-header date-regexp t)
14242                   (beginning-of-line))
14243               (goto-char (point-max)))
14244             (insert (gnus-make-date-line date type))
14245             ;; Do highlighting.
14246             (forward-line -1)
14247             (when (and (gnus-visual-p 'article-highlight 'highlight)
14248                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14249               (gnus-put-text-property (match-beginning 1) (match-end 1)
14250                                  'face bface)
14251               (gnus-put-text-property (match-beginning 2) (match-end 2)
14252                                  'face eface))))))))
14253
14254 (defun gnus-make-date-line (date type)
14255   "Return a DATE line of TYPE."
14256   (cond
14257    ;; Convert to the local timezone.  We have to slap a
14258    ;; `condition-case' round the calls to the timezone
14259    ;; functions since they aren't particularly resistant to
14260    ;; buggy dates.
14261    ((eq type 'local)
14262     (concat "Date: " (condition-case ()
14263                          (timezone-make-date-arpa-standard date)
14264                        (error date))
14265             "\n"))
14266    ;; Convert to Universal Time.
14267    ((eq type 'ut)
14268     (concat "Date: "
14269             (condition-case ()
14270                 (timezone-make-date-arpa-standard date nil "UT")
14271               (error date))
14272             "\n"))
14273    ;; Get the original date from the article.
14274    ((eq type 'original)
14275     (concat "Date: " date "\n"))
14276    ;; Do an X-Sent lapsed format.
14277    ((eq type 'lapsed)
14278     ;; If the date is seriously mangled, the timezone
14279     ;; functions are liable to bug out, so we condition-case
14280     ;; the entire thing.
14281     (let* ((now (current-time))
14282            (real-time
14283             (condition-case ()
14284                 (gnus-time-minus
14285                  (gnus-encode-date
14286                   (timezone-make-date-arpa-standard
14287                    (current-time-string now)
14288                    (current-time-zone now) "UT"))
14289                  (gnus-encode-date
14290                   (timezone-make-date-arpa-standard
14291                    date nil "UT")))
14292               (error '(0 0))))
14293            (real-sec (+ (* (float (car real-time)) 65536)
14294                         (cadr real-time)))
14295            (sec (abs real-sec))
14296            num prev)
14297       (cond
14298        ((equal real-time '(0 0))
14299         "X-Sent: Unknown\n")
14300        ((zerop sec)
14301         "X-Sent: Now\n")
14302        (t
14303         (concat
14304          "X-Sent: "
14305          ;; This is a bit convoluted, but basically we go
14306          ;; through the time units for years, weeks, etc,
14307          ;; and divide things to see whether that results
14308          ;; in positive answers.
14309          (mapconcat
14310           (lambda (unit)
14311             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14312                 ;; The (remaining) seconds are too few to
14313                 ;; be divided into this time unit.
14314                 ""
14315               ;; It's big enough, so we output it.
14316               (setq sec (- sec (* num (cdr unit))))
14317               (prog1
14318                   (concat (if prev ", " "") (int-to-string
14319                                              (floor num))
14320                           " " (symbol-name (car unit))
14321                           (if (> num 1) "s" ""))
14322                 (setq prev t))))
14323           gnus-article-time-units "")
14324          ;; If dates are odd, then it might appear like the
14325          ;; article was sent in the future.
14326          (if (> real-sec 0)
14327              " ago\n"
14328            " in the future\n"))))))
14329    (t
14330     (error "Unknown conversion type: %s" type))))
14331
14332 (defun gnus-article-date-local (&optional highlight)
14333   "Convert the current article date to the local timezone."
14334   (interactive (list t))
14335   (gnus-article-date-ut 'local highlight))
14336
14337 (defun gnus-article-date-original (&optional highlight)
14338   "Convert the current article date to what it was originally.
14339 This is only useful if you have used some other date conversion
14340 function and want to see what the date was before converting."
14341   (interactive (list t))
14342   (gnus-article-date-ut 'original highlight))
14343
14344 (defun gnus-article-date-lapsed (&optional highlight)
14345   "Convert the current article date to time lapsed since it was sent."
14346   (interactive (list t))
14347   (gnus-article-date-ut 'lapsed highlight))
14348
14349 (defun gnus-article-maybe-highlight ()
14350   "Do some article highlighting if `gnus-visual' is non-nil."
14351   (if (gnus-visual-p 'article-highlight 'highlight)
14352       (gnus-article-highlight-some)))
14353
14354 ;; Article savers.
14355
14356 (defun gnus-output-to-rmail (file-name)
14357   "Append the current article to an Rmail file named FILE-NAME."
14358   (require 'rmail)
14359   ;; Most of these codes are borrowed from rmailout.el.
14360   (setq file-name (expand-file-name file-name))
14361   (setq rmail-default-rmail-file file-name)
14362   (let ((artbuf (current-buffer))
14363         (tmpbuf (get-buffer-create " *Gnus-output*")))
14364     (save-excursion
14365       (or (get-file-buffer file-name)
14366           (file-exists-p file-name)
14367           (if (gnus-yes-or-no-p
14368                (concat "\"" file-name "\" does not exist, create it? "))
14369               (let ((file-buffer (create-file-buffer file-name)))
14370                 (save-excursion
14371                   (set-buffer file-buffer)
14372                   (rmail-insert-rmail-file-header)
14373                   (let ((require-final-newline nil))
14374                     (write-region (point-min) (point-max) file-name t 1)))
14375                 (kill-buffer file-buffer))
14376             (error "Output file does not exist")))
14377       (set-buffer tmpbuf)
14378       (buffer-disable-undo (current-buffer))
14379       (erase-buffer)
14380       (insert-buffer-substring artbuf)
14381       (gnus-convert-article-to-rmail)
14382       ;; Decide whether to append to a file or to an Emacs buffer.
14383       (let ((outbuf (get-file-buffer file-name)))
14384         (if (not outbuf)
14385             (append-to-file (point-min) (point-max) file-name)
14386           ;; File has been visited, in buffer OUTBUF.
14387           (set-buffer outbuf)
14388           (let ((buffer-read-only nil)
14389                 (msg (and (boundp 'rmail-current-message)
14390                           (symbol-value 'rmail-current-message))))
14391             ;; If MSG is non-nil, buffer is in RMAIL mode.
14392             (if msg
14393                 (progn (widen)
14394                        (narrow-to-region (point-max) (point-max))))
14395             (insert-buffer-substring tmpbuf)
14396             (if msg
14397                 (progn
14398                   (goto-char (point-min))
14399                   (widen)
14400                   (search-backward "\^_")
14401                   (narrow-to-region (point) (point-max))
14402                   (goto-char (1+ (point-min)))
14403                   (rmail-count-new-messages t)
14404                   (rmail-show-message msg)))))))
14405     (kill-buffer tmpbuf)))
14406
14407 (defun gnus-output-to-file (file-name)
14408   "Append the current article to a file named FILE-NAME."
14409   (setq file-name (expand-file-name file-name))
14410   (let ((artbuf (current-buffer))
14411         (tmpbuf (get-buffer-create " *Gnus-output*")))
14412     (save-excursion
14413       (set-buffer tmpbuf)
14414       (buffer-disable-undo (current-buffer))
14415       (erase-buffer)
14416       (insert-buffer-substring artbuf)
14417       ;; Append newline at end of the buffer as separator, and then
14418       ;; save it to file.
14419       (goto-char (point-max))
14420       (insert "\n")
14421       (append-to-file (point-min) (point-max) file-name))
14422     (kill-buffer tmpbuf)))
14423
14424 (defun gnus-convert-article-to-rmail ()
14425   "Convert article in current buffer to Rmail message format."
14426   (let ((buffer-read-only nil))
14427     ;; Convert article directly into Babyl format.
14428     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14429     (goto-char (point-min))
14430     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14431     (while (search-forward "\n\^_" nil t) ;single char
14432       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14433     (goto-char (point-max))
14434     (insert "\^_")))
14435
14436 (defun gnus-narrow-to-page (&optional arg)
14437   "Narrow the article buffer to a page.
14438 If given a numerical ARG, move forward ARG pages."
14439   (interactive "P")
14440   (setq arg (if arg (prefix-numeric-value arg) 0))
14441   (save-excursion
14442     (set-buffer gnus-article-buffer)
14443     (goto-char (point-min))
14444     (widen)
14445     (when (gnus-visual-p 'page-marker)
14446       (let ((buffer-read-only nil))
14447         (gnus-remove-text-with-property 'gnus-prev)
14448         (gnus-remove-text-with-property 'gnus-next)))
14449     (when
14450         (cond ((< arg 0)
14451                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14452               ((> arg 0)
14453                (re-search-forward page-delimiter nil 'move arg)))
14454       (goto-char (match-end 0)))
14455     (narrow-to-region
14456      (point)
14457      (if (re-search-forward page-delimiter nil 'move)
14458          (match-beginning 0)
14459        (point)))
14460     (when (and (gnus-visual-p 'page-marker)
14461                (not (= (point-min) 1)))
14462       (save-excursion
14463         (goto-char (point-min))
14464         (gnus-insert-prev-page-button)))
14465     (when (and (gnus-visual-p 'page-marker)
14466                (not (= (1- (point-max)) (buffer-size))))
14467       (save-excursion
14468         (goto-char (point-max))
14469         (gnus-insert-next-page-button)))))
14470
14471 ;; Article mode commands
14472
14473 (defun gnus-article-goto-next-page ()
14474   "Show the next page of the article."
14475   (interactive)
14476   (when (gnus-article-next-page)
14477     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14478
14479 (defun gnus-article-goto-prev-page ()
14480   "Show the next page of the article."
14481   (interactive)
14482   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14483     (gnus-article-prev-page nil)))
14484
14485 (defun gnus-article-next-page (&optional lines)
14486   "Show the next page of the current article.
14487 If end of article, return non-nil.  Otherwise return nil.
14488 Argument LINES specifies lines to be scrolled up."
14489   (interactive "p")
14490   (move-to-window-line -1)
14491   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14492   (if (save-excursion
14493         (end-of-line)
14494         (and (pos-visible-in-window-p)  ;Not continuation line.
14495              (eobp)))
14496       ;; Nothing in this page.
14497       (if (or (not gnus-break-pages)
14498               (save-excursion
14499                 (save-restriction
14500                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14501           t                             ;Nothing more.
14502         (gnus-narrow-to-page 1)         ;Go to next page.
14503         nil)
14504     ;; More in this page.
14505     (condition-case ()
14506         (scroll-up lines)
14507       (end-of-buffer
14508        ;; Long lines may cause an end-of-buffer error.
14509        (goto-char (point-max))))
14510     (move-to-window-line 0)
14511     nil))
14512
14513 (defun gnus-article-prev-page (&optional lines)
14514   "Show previous page of current article.
14515 Argument LINES specifies lines to be scrolled down."
14516   (interactive "p")
14517   (move-to-window-line 0)
14518   (if (and gnus-break-pages
14519            (bobp)
14520            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14521       (progn
14522         (gnus-narrow-to-page -1)        ;Go to previous page.
14523         (goto-char (point-max))
14524         (recenter -1))
14525     (prog1
14526         (condition-case ()
14527             (scroll-down lines)
14528           (error nil))
14529       (move-to-window-line 0))))
14530
14531 (defun gnus-article-refer-article ()
14532   "Read article specified by message-id around point."
14533   (interactive)
14534   (let ((point (point)))
14535     (search-forward ">" nil t)          ;Move point to end of "<....>".
14536     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14537         (let ((message-id (match-string 1)))
14538           (goto-char point)
14539           (set-buffer gnus-summary-buffer)
14540           (gnus-summary-refer-article message-id))
14541       (goto-char (point))
14542       (error "No references around point"))))
14543
14544 (defun gnus-article-show-summary ()
14545   "Reconfigure windows to show summary buffer."
14546   (interactive)
14547   (gnus-configure-windows 'article)
14548   (gnus-summary-goto-subject gnus-current-article))
14549
14550 (defun gnus-article-describe-briefly ()
14551   "Describe article mode commands briefly."
14552   (interactive)
14553   (gnus-message 6
14554                 (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")))
14555
14556 (defun gnus-article-summary-command ()
14557   "Execute the last keystroke in the summary buffer."
14558   (interactive)
14559   (let ((obuf (current-buffer))
14560         (owin (current-window-configuration))
14561         func)
14562     (switch-to-buffer gnus-summary-buffer 'norecord)
14563     (setq func (lookup-key (current-local-map) (this-command-keys)))
14564     (call-interactively func)
14565     (set-buffer obuf)
14566     (set-window-configuration owin)
14567     (set-window-point (get-buffer-window (current-buffer)) (point))))
14568
14569 (defun gnus-article-summary-command-nosave ()
14570   "Execute the last keystroke in the summary buffer."
14571   (interactive)
14572   (let (func)
14573     (pop-to-buffer gnus-summary-buffer 'norecord)
14574     (setq func (lookup-key (current-local-map) (this-command-keys)))
14575     (call-interactively func)))
14576
14577 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14578   "Read a summary buffer key sequence and execute it from the article buffer."
14579   (interactive "P")
14580   (let ((nosaves
14581          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14582            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14583            "=" "^" "\M-^"))
14584         keys)
14585     (save-excursion
14586       (set-buffer gnus-summary-buffer)
14587       (push (or key last-command-event) unread-command-events)
14588       (setq keys (read-key-sequence nil)))
14589     (message "")
14590
14591     (if (member keys nosaves)
14592         (let (func)
14593           (pop-to-buffer gnus-summary-buffer 'norecord)
14594           (if (setq func (lookup-key (current-local-map) keys))
14595               (call-interactively func)
14596             (ding)))
14597       (let ((obuf (current-buffer))
14598             (owin (current-window-configuration))
14599             (opoint (point))
14600             func in-buffer)
14601         (if not-restore-window
14602             (pop-to-buffer gnus-summary-buffer 'norecord)
14603           (switch-to-buffer gnus-summary-buffer 'norecord))
14604         (setq in-buffer (current-buffer))
14605         (if (setq func (lookup-key (current-local-map) keys))
14606             (call-interactively func)
14607           (ding))
14608         (when (eq in-buffer (current-buffer))
14609           (set-buffer obuf)
14610           (unless not-restore-window
14611             (set-window-configuration owin))
14612           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14613
14614 \f
14615 ;;;
14616 ;;; Kill file handling.
14617 ;;;
14618
14619 ;;;###autoload
14620 (defalias 'gnus-batch-kill 'gnus-batch-score)
14621 ;;;###autoload
14622 (defun gnus-batch-score ()
14623   "Run batched scoring.
14624 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14625 Newsgroups is a list of strings in Bnews format.  If you want to score
14626 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14627 score the alt hierarchy, you'd say \"!alt.all\"."
14628   (interactive)
14629   (let* ((yes-and-no
14630           (gnus-newsrc-parse-options
14631            (apply (function concat)
14632                   (mapcar (lambda (g) (concat g " "))
14633                           command-line-args-left))))
14634          (gnus-expert-user t)
14635          (nnmail-spool-file nil)
14636          (gnus-use-dribble-file nil)
14637          (yes (car yes-and-no))
14638          (no (cdr yes-and-no))
14639          group newsrc entry
14640          ;; Disable verbose message.
14641          gnus-novice-user gnus-large-newsgroup)
14642     ;; Eat all arguments.
14643     (setq command-line-args-left nil)
14644     ;; Start Gnus.
14645     (gnus)
14646     ;; Apply kills to specified newsgroups in command line arguments.
14647     (setq newsrc (cdr gnus-newsrc-alist))
14648     (while newsrc
14649       (setq group (caar newsrc))
14650       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14651       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14652                (and (car entry)
14653                     (or (eq (car entry) t)
14654                         (not (zerop (car entry)))))
14655                (if yes (string-match yes group) t)
14656                (or (null no) (not (string-match no group))))
14657           (progn
14658             (gnus-summary-read-group group nil t nil t)
14659             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14660                  (gnus-summary-exit))))
14661       (setq newsrc (cdr newsrc)))
14662     ;; Exit Emacs.
14663     (switch-to-buffer gnus-group-buffer)
14664     (gnus-group-save-newsrc)))
14665
14666 (defun gnus-apply-kill-file ()
14667   "Apply a kill file to the current newsgroup.
14668 Returns the number of articles marked as read."
14669   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14670           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14671       (gnus-apply-kill-file-internal)
14672     0))
14673
14674 (defun gnus-kill-save-kill-buffer ()
14675   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14676     (when (get-file-buffer file)
14677       (save-excursion
14678         (set-buffer (get-file-buffer file))
14679         (and (buffer-modified-p) (save-buffer))
14680         (kill-buffer (current-buffer))))))
14681
14682 (defvar gnus-kill-file-name "KILL"
14683   "Suffix of the kill files.")
14684
14685 (defun gnus-newsgroup-kill-file (newsgroup)
14686   "Return the name of a kill file name for NEWSGROUP.
14687 If NEWSGROUP is nil, return the global kill file name instead."
14688   (cond 
14689    ;; The global KILL file is placed at top of the directory.
14690    ((or (null newsgroup)
14691         (string-equal newsgroup ""))
14692     (expand-file-name gnus-kill-file-name
14693                       gnus-kill-files-directory))
14694    ;; Append ".KILL" to newsgroup name.
14695    ((gnus-use-long-file-name 'not-kill)
14696     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14697                               "." gnus-kill-file-name)
14698                       gnus-kill-files-directory))
14699    ;; Place "KILL" under the hierarchical directory.
14700    (t
14701     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14702                               "/" gnus-kill-file-name)
14703                       gnus-kill-files-directory))))
14704
14705 \f
14706 ;;;
14707 ;;; Dribble file
14708 ;;;
14709
14710 (defvar gnus-dribble-ignore nil)
14711 (defvar gnus-dribble-eval-file nil)
14712
14713 (defun gnus-dribble-file-name ()
14714   "Return the dribble file for the current .newsrc."
14715   (concat
14716    (if gnus-dribble-directory
14717        (concat (file-name-as-directory gnus-dribble-directory)
14718                (file-name-nondirectory gnus-current-startup-file))
14719      gnus-current-startup-file)
14720    "-dribble"))
14721
14722 (defun gnus-dribble-enter (string)
14723   "Enter STRING into the dribble buffer."
14724   (if (and (not gnus-dribble-ignore)
14725            gnus-dribble-buffer
14726            (buffer-name gnus-dribble-buffer))
14727       (let ((obuf (current-buffer)))
14728         (set-buffer gnus-dribble-buffer)
14729         (insert string "\n")
14730         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14731         (set-buffer obuf))))
14732
14733 (defun gnus-dribble-read-file ()
14734   "Read the dribble file from disk."
14735   (let ((dribble-file (gnus-dribble-file-name)))
14736     (save-excursion
14737       (set-buffer (setq gnus-dribble-buffer
14738                         (get-buffer-create
14739                          (file-name-nondirectory dribble-file))))
14740       (gnus-add-current-to-buffer-list)
14741       (erase-buffer)
14742       (setq buffer-file-name dribble-file)
14743       (auto-save-mode t)
14744       (buffer-disable-undo (current-buffer))
14745       (bury-buffer (current-buffer))
14746       (set-buffer-modified-p nil)
14747       (let ((auto (make-auto-save-file-name))
14748             (gnus-dribble-ignore t)
14749             modes)
14750         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14751           ;; Load whichever file is newest -- the auto save file
14752           ;; or the "real" file.
14753           (if (file-newer-than-file-p auto dribble-file)
14754               (insert-file-contents auto)
14755             (insert-file-contents dribble-file))
14756           (unless (zerop (buffer-size))
14757             (set-buffer-modified-p t))
14758           ;; Set the file modes to reflect the .newsrc file modes.
14759           (save-buffer)
14760           (when (and (file-exists-p gnus-current-startup-file)
14761                      (setq modes (file-modes gnus-current-startup-file)))
14762             (set-file-modes dribble-file modes))
14763           ;; Possibly eval the file later.
14764           (when (gnus-y-or-n-p
14765                  "Auto-save file exists.  Do you want to read it? ")
14766             (setq gnus-dribble-eval-file t)))))))
14767
14768 (defun gnus-dribble-eval-file ()
14769   (when gnus-dribble-eval-file
14770     (setq gnus-dribble-eval-file nil)
14771     (save-excursion
14772       (let ((gnus-dribble-ignore t))
14773         (set-buffer gnus-dribble-buffer)
14774         (eval-buffer (current-buffer))))))
14775
14776 (defun gnus-dribble-delete-file ()
14777   (when (file-exists-p (gnus-dribble-file-name))
14778     (delete-file (gnus-dribble-file-name)))
14779   (when gnus-dribble-buffer
14780     (save-excursion
14781       (set-buffer gnus-dribble-buffer)
14782       (let ((auto (make-auto-save-file-name)))
14783         (if (file-exists-p auto)
14784             (delete-file auto))
14785         (erase-buffer)
14786         (set-buffer-modified-p nil)))))
14787
14788 (defun gnus-dribble-save ()
14789   (when (and gnus-dribble-buffer
14790              (buffer-name gnus-dribble-buffer))
14791     (save-excursion
14792       (set-buffer gnus-dribble-buffer)
14793       (save-buffer))))
14794
14795 (defun gnus-dribble-clear ()
14796   (when (gnus-buffer-exists-p gnus-dribble-buffer)
14797     (save-excursion
14798       (set-buffer gnus-dribble-buffer)
14799       (erase-buffer)
14800       (set-buffer-modified-p nil)
14801       (setq buffer-saved-size (buffer-size)))))
14802
14803 \f
14804 ;;;
14805 ;;; Server Communication
14806 ;;;
14807
14808 (defun gnus-start-news-server (&optional confirm)
14809   "Open a method for getting news.
14810 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14811   (let (how)
14812     (if gnus-current-select-method
14813         ;; Stream is already opened.
14814         nil
14815       ;; Open NNTP server.
14816       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14817       (if confirm
14818           (progn
14819             ;; Read server name with completion.
14820             (setq gnus-nntp-server
14821                   (completing-read "NNTP server: "
14822                                    (mapcar (lambda (server) (list server))
14823                                            (cons (list gnus-nntp-server)
14824                                                  gnus-secondary-servers))
14825                                    nil nil gnus-nntp-server))))
14826
14827       (if (and gnus-nntp-server
14828                (stringp gnus-nntp-server)
14829                (not (string= gnus-nntp-server "")))
14830           (setq gnus-select-method
14831                 (cond ((or (string= gnus-nntp-server "")
14832                            (string= gnus-nntp-server "::"))
14833                        (list 'nnspool (system-name)))
14834                       ((string-match "^:" gnus-nntp-server)
14835                        (list 'nnmh gnus-nntp-server
14836                              (list 'nnmh-directory
14837                                    (file-name-as-directory
14838                                     (expand-file-name
14839                                      (concat "~/" (substring
14840                                                    gnus-nntp-server 1)))))
14841                              (list 'nnmh-get-new-mail nil)))
14842                       (t
14843                        (list 'nntp gnus-nntp-server)))))
14844
14845       (setq how (car gnus-select-method))
14846       (cond ((eq how 'nnspool)
14847              (require 'nnspool)
14848              (gnus-message 5 "Looking up local news spool..."))
14849             ((eq how 'nnmh)
14850              (require 'nnmh)
14851              (gnus-message 5 "Looking up mh spool..."))
14852             (t
14853              (require 'nntp)))
14854       (setq gnus-current-select-method gnus-select-method)
14855       (run-hooks 'gnus-open-server-hook)
14856       (or
14857        ;; gnus-open-server-hook might have opened it
14858        (gnus-server-opened gnus-select-method)
14859        (gnus-open-server gnus-select-method)
14860        (gnus-y-or-n-p
14861         (format
14862          "%s (%s) open error: '%s'.     Continue? "
14863          (car gnus-select-method) (cadr gnus-select-method)
14864          (gnus-status-message gnus-select-method)))
14865        (gnus-error 1 "Couldn't open server on %s"
14866                    (nth 1 gnus-select-method))))))
14867
14868 (defun gnus-check-group (group)
14869   "Try to make sure that the server where GROUP exists is alive."
14870   (let ((method (gnus-find-method-for-group group)))
14871     (or (gnus-server-opened method)
14872         (gnus-open-server method))))
14873
14874 (defun gnus-check-server (&optional method silent)
14875   "Check whether the connection to METHOD is down.
14876 If METHOD is nil, use `gnus-select-method'.
14877 If it is down, start it up (again)."
14878   (let ((method (or method gnus-select-method)))
14879     ;; Transform virtual server names into select methods.
14880     (when (stringp method)
14881       (setq method (gnus-server-to-method method)))
14882     (if (gnus-server-opened method)
14883         ;; The stream is already opened.
14884         t
14885       ;; Open the server.
14886       (unless silent
14887         (gnus-message 5 "Opening %s server%s..." (car method)
14888                       (if (equal (nth 1 method) "") ""
14889                         (format " on %s" (nth 1 method)))))
14890       (run-hooks 'gnus-open-server-hook)
14891       (prog1
14892           (gnus-open-server method)
14893         (unless silent
14894           (message ""))))))
14895
14896 (defun gnus-get-function (method function)
14897   "Return a function symbol based on METHOD and FUNCTION."
14898   ;; Translate server names into methods.
14899   (unless method
14900     (error "Attempted use of a nil select method"))
14901   (when (stringp method)
14902     (setq method (gnus-server-to-method method)))
14903   (let ((func (intern (format "%s-%s" (car method) function))))
14904     ;; If the functions isn't bound, we require the backend in
14905     ;; question.
14906     (unless (fboundp func)
14907       (require (car method))
14908       (unless (fboundp func)
14909         ;; This backend doesn't implement this function.
14910         (error "No such function: %s" func)))
14911     func))
14912
14913 \f
14914 ;;;
14915 ;;; Interface functions to the backends.
14916 ;;;
14917
14918 (defun gnus-open-server (method)
14919   "Open a connection to METHOD."
14920   (when (stringp method)
14921     (setq method (gnus-server-to-method method)))
14922   (let ((elem (assoc method gnus-opened-servers)))
14923     ;; If this method was previously denied, we just return nil.
14924     (if (eq (nth 1 elem) 'denied)
14925         (progn
14926           (gnus-message 1 "Denied server")
14927           nil)
14928       ;; Open the server.
14929       (let ((result
14930              (funcall (gnus-get-function method 'open-server)
14931                       (nth 1 method) (nthcdr 2 method))))
14932         ;; If this hasn't been opened before, we add it to the list.
14933         (unless elem
14934           (setq elem (list method nil)
14935                 gnus-opened-servers (cons elem gnus-opened-servers)))
14936         ;; Set the status of this server.
14937         (setcar (cdr elem) (if result 'ok 'denied))
14938         ;; Return the result from the "open" call.
14939         result))))
14940
14941 (defun gnus-close-server (method)
14942   "Close the connection to METHOD."
14943   (when (stringp method)
14944     (setq method (gnus-server-to-method method)))
14945   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14946
14947 (defun gnus-request-list (method)
14948   "Request the active file from METHOD."
14949   (when (stringp method)
14950     (setq method (gnus-server-to-method method)))
14951   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14952
14953 (defun gnus-request-list-newsgroups (method)
14954   "Request the newsgroups file from METHOD."
14955   (when (stringp method)
14956     (setq method (gnus-server-to-method method)))
14957   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14958
14959 (defun gnus-request-newgroups (date method)
14960   "Request all new groups since DATE from METHOD."
14961   (when (stringp method)
14962     (setq method (gnus-server-to-method method)))
14963   (funcall (gnus-get-function method 'request-newgroups)
14964            date (nth 1 method)))
14965
14966 (defun gnus-server-opened (method)
14967   "Check whether a connection to METHOD has been opened."
14968   (when (stringp method)
14969     (setq method (gnus-server-to-method method)))
14970   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14971
14972 (defun gnus-status-message (method)
14973   "Return the status message from METHOD.
14974 If METHOD is a string, it is interpreted as a group name.   The method
14975 this group uses will be queried."
14976   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14977                   method)))
14978     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14979
14980 (defun gnus-request-group (group &optional dont-check method)
14981   "Request GROUP.  If DONT-CHECK, no information is required."
14982   (let ((method (or method (gnus-find-method-for-group group))))
14983     (when (stringp method)
14984       (setq method (gnus-server-to-method method)))
14985     (funcall (gnus-get-function method 'request-group)
14986              (gnus-group-real-name group) (nth 1 method) dont-check)))
14987
14988 (defun gnus-request-asynchronous (group &optional articles)
14989   "Request that GROUP behave asynchronously.
14990 ARTICLES is the `data' of the group."
14991   (let ((method (gnus-find-method-for-group group)))
14992     (funcall (gnus-get-function method 'request-asynchronous)
14993              (gnus-group-real-name group) (nth 1 method) articles)))
14994
14995 (defun gnus-list-active-group (group)
14996   "Request active information on GROUP."
14997   (let ((method (gnus-find-method-for-group group))
14998         (func 'list-active-group))
14999     (when (gnus-check-backend-function func group)
15000       (funcall (gnus-get-function method func)
15001                (gnus-group-real-name group) (nth 1 method)))))
15002
15003 (defun gnus-request-group-description (group)
15004   "Request a description of GROUP."
15005   (let ((method (gnus-find-method-for-group group))
15006         (func 'request-group-description))
15007     (when (gnus-check-backend-function func group)
15008       (funcall (gnus-get-function method func)
15009                (gnus-group-real-name group) (nth 1 method)))))
15010
15011 (defun gnus-close-group (group)
15012   "Request the GROUP be closed."
15013   (let ((method (gnus-find-method-for-group group)))
15014     (funcall (gnus-get-function method 'close-group)
15015              (gnus-group-real-name group) (nth 1 method))))
15016
15017 (defun gnus-retrieve-headers (articles group &optional fetch-old)
15018   "Request headers for ARTICLES in GROUP.
15019 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
15020   (let ((method (gnus-find-method-for-group group)))
15021     (if (and gnus-use-cache (numberp (car articles)))
15022         (gnus-cache-retrieve-headers articles group fetch-old)
15023       (funcall (gnus-get-function method 'retrieve-headers)
15024                articles (gnus-group-real-name group) (nth 1 method)
15025                fetch-old))))
15026
15027 (defun gnus-retrieve-groups (groups method)
15028   "Request active information on GROUPS from METHOD."
15029   (when (stringp method)
15030     (setq method (gnus-server-to-method method)))
15031   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
15032
15033 (defun gnus-request-type (group &optional article)
15034   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15035   (let ((method (gnus-find-method-for-group group)))
15036     (if (not (gnus-check-backend-function 'request-type (car method)))
15037         'unknown
15038       (funcall (gnus-get-function method 'request-type)
15039                (gnus-group-real-name group) article))))
15040
15041 (defun gnus-request-update-mark (group article mark)
15042   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15043   (let ((method (gnus-find-method-for-group group)))
15044     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
15045         mark
15046       (funcall (gnus-get-function method 'request-update-mark)
15047                (gnus-group-real-name group) article mark))))
15048
15049 (defun gnus-request-article (article group &optional buffer)
15050   "Request the ARTICLE in GROUP.
15051 ARTICLE can either be an article number or an article Message-ID.
15052 If BUFFER, insert the article in that group."
15053   (let ((method (gnus-find-method-for-group group)))
15054     (funcall (gnus-get-function method 'request-article)
15055              article (gnus-group-real-name group) (nth 1 method) buffer)))
15056
15057 (defun gnus-request-head (article group)
15058   "Request the head of ARTICLE in GROUP."
15059   (let ((method (gnus-find-method-for-group group)))
15060     (funcall (gnus-get-function method 'request-head)
15061              article (gnus-group-real-name group) (nth 1 method))))
15062
15063 (defun gnus-request-body (article group)
15064   "Request the body of ARTICLE in GROUP."
15065   (let ((method (gnus-find-method-for-group group)))
15066     (funcall (gnus-get-function method 'request-body)
15067              article (gnus-group-real-name group) (nth 1 method))))
15068
15069 (defun gnus-request-post (method)
15070   "Post the current buffer using METHOD."
15071   (when (stringp method)
15072     (setq method (gnus-server-to-method method)))
15073   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
15074
15075 (defun gnus-request-scan (group method)
15076   "Request a SCAN being performed in GROUP from METHOD.
15077 If GROUP is nil, all groups on METHOD are scanned."
15078   (let ((method (if group (gnus-find-method-for-group group) method)))
15079     (funcall (gnus-get-function method 'request-scan)
15080              (and group (gnus-group-real-name group)) (nth 1 method))))
15081
15082 (defsubst gnus-request-update-info (info method)
15083   "Request that METHOD update INFO."
15084   (when (stringp method)
15085     (setq method (gnus-server-to-method method)))
15086   (when (gnus-check-backend-function 'request-update-info (car method))
15087     (funcall (gnus-get-function method 'request-update-info)
15088              (gnus-group-real-name (gnus-info-group info))
15089              info (nth 1 method))))
15090
15091 (defun gnus-request-expire-articles (articles group &optional force)
15092   (let ((method (gnus-find-method-for-group group)))
15093     (funcall (gnus-get-function method 'request-expire-articles)
15094              articles (gnus-group-real-name group) (nth 1 method)
15095              force)))
15096
15097 (defun gnus-request-move-article
15098   (article group server accept-function &optional last)
15099   (let ((method (gnus-find-method-for-group group)))
15100     (funcall (gnus-get-function method 'request-move-article)
15101              article (gnus-group-real-name group)
15102              (nth 1 method) accept-function last)))
15103
15104 (defun gnus-request-accept-article (group method &optional last)
15105   ;; Make sure there's a newline at the end of the article.
15106   (when (stringp method)
15107     (setq method (gnus-server-to-method method)))
15108   (when (and (not method)
15109              (stringp group))
15110     (setq method (gnus-find-method-for-group group)))
15111   (goto-char (point-max))
15112   (unless (bolp)
15113     (insert "\n"))
15114   (let ((func (car (or method (gnus-find-method-for-group group)))))
15115     (funcall (intern (format "%s-request-accept-article" func))
15116              (if (stringp group) (gnus-group-real-name group) group)
15117              (cadr method)
15118              last)))
15119
15120 (defun gnus-request-replace-article (article group buffer)
15121   (let ((func (car (gnus-find-method-for-group group))))
15122     (funcall (intern (format "%s-request-replace-article" func))
15123              article (gnus-group-real-name group) buffer)))
15124
15125 (defun gnus-request-associate-buffer (group)
15126   (let ((method (gnus-find-method-for-group group)))
15127     (funcall (gnus-get-function method 'request-associate-buffer)
15128              (gnus-group-real-name group))))
15129
15130 (defun gnus-request-restore-buffer (article group)
15131   "Request a new buffer restored to the state of ARTICLE."
15132   (let ((method (gnus-find-method-for-group group)))
15133     (funcall (gnus-get-function method 'request-restore-buffer)
15134              article (gnus-group-real-name group) (nth 1 method))))
15135
15136 (defun gnus-request-create-group (group &optional method)
15137   (when (stringp method)
15138     (setq method (gnus-server-to-method method)))
15139   (let ((method (or method (gnus-find-method-for-group group))))
15140     (funcall (gnus-get-function method 'request-create-group)
15141              (gnus-group-real-name group) (nth 1 method))))
15142
15143 (defun gnus-request-delete-group (group &optional force)
15144   (let ((method (gnus-find-method-for-group group)))
15145     (funcall (gnus-get-function method 'request-delete-group)
15146              (gnus-group-real-name group) force (nth 1 method))))
15147
15148 (defun gnus-request-rename-group (group new-name)
15149   (let ((method (gnus-find-method-for-group group)))
15150     (funcall (gnus-get-function method 'request-rename-group)
15151              (gnus-group-real-name group)
15152              (gnus-group-real-name new-name) (nth 1 method))))
15153
15154 (defun gnus-member-of-valid (symbol group)
15155   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15156   (memq symbol (assoc
15157                 (symbol-name (car (gnus-find-method-for-group group)))
15158                 gnus-valid-select-methods)))
15159
15160 (defun gnus-method-option-p (method option)
15161   "Return non-nil if select METHOD has OPTION as a parameter."
15162   (when (stringp method)
15163     (setq method (gnus-server-to-method method)))
15164   (memq option (assoc (format "%s" (car method))
15165                       gnus-valid-select-methods)))
15166
15167 (defun gnus-server-extend-method (group method)
15168   ;; This function "extends" a virtual server.  If the server is
15169   ;; "hello", and the select method is ("hello" (my-var "something"))
15170   ;; in the group "alt.alt", this will result in a new virtual server
15171   ;; called "hello+alt.alt".
15172   (let ((entry
15173          (gnus-copy-sequence
15174           (if (equal (car method) "native") gnus-select-method
15175             (cdr (assoc (car method) gnus-server-alist))))))
15176     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15177     (nconc entry (cdr method))))
15178
15179 (defun gnus-find-method-for-group (group &optional info)
15180   "Find the select method that GROUP uses."
15181   (or gnus-override-method
15182       (and (not group)
15183            gnus-select-method)
15184       (let ((info (or info (gnus-get-info group)))
15185             method)
15186         (if (or (not info)
15187                 (not (setq method (gnus-info-method info)))
15188                 (equal method "native"))
15189             gnus-select-method
15190           (setq method
15191                 (cond ((stringp method)
15192                        (gnus-server-to-method method))
15193                       ((stringp (car method))
15194                        (gnus-server-extend-method group method))
15195                       (t
15196                        method)))
15197           (cond ((equal (cadr method) "")
15198                  method)
15199                 ((null (cadr method))
15200                  (list (car method) ""))
15201                 (t
15202                  (gnus-server-add-address method)))))))
15203
15204 (defun gnus-check-backend-function (func group)
15205   "Check whether GROUP supports function FUNC."
15206   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15207                   group)))
15208     (fboundp (intern (format "%s-%s" method func)))))
15209
15210 (defun gnus-methods-using (feature)
15211   "Find all methods that have FEATURE."
15212   (let ((valids gnus-valid-select-methods)
15213         outs)
15214     (while valids
15215       (if (memq feature (car valids))
15216           (setq outs (cons (car valids) outs)))
15217       (setq valids (cdr valids)))
15218     outs))
15219
15220 \f
15221 ;;;
15222 ;;; Active & Newsrc File Handling
15223 ;;;
15224
15225 (defun gnus-setup-news (&optional rawfile level dont-connect)
15226   "Setup news information.
15227 If RAWFILE is non-nil, the .newsrc file will also be read.
15228 If LEVEL is non-nil, the news will be set up at level LEVEL."
15229   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15230
15231     (when init 
15232       ;; Clear some variables to re-initialize news information.
15233       (setq gnus-newsrc-alist nil
15234             gnus-active-hashtb nil)
15235       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15236       (gnus-read-newsrc-file rawfile))
15237
15238     (when (and (not (assoc "archive" gnus-server-alist))
15239                gnus-message-archive-method)
15240       (push (cons "archive" gnus-message-archive-method)
15241             gnus-server-alist))
15242
15243     ;; If we don't read the complete active file, we fill in the
15244     ;; hashtb here.
15245     (if (or (null gnus-read-active-file)
15246             (eq gnus-read-active-file 'some))
15247         (gnus-update-active-hashtb-from-killed))
15248
15249     ;; Read the active file and create `gnus-active-hashtb'.
15250     ;; If `gnus-read-active-file' is nil, then we just create an empty
15251     ;; hash table.  The partial filling out of the hash table will be
15252     ;; done in `gnus-get-unread-articles'.
15253     (and gnus-read-active-file
15254          (not level)
15255          (gnus-read-active-file))
15256
15257     (or gnus-active-hashtb
15258         (setq gnus-active-hashtb (make-vector 4095 0)))
15259
15260     ;; Initialize the cache.
15261     (when gnus-use-cache
15262       (gnus-cache-open))
15263
15264     ;; Possibly eval the dribble file.
15265     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15266
15267     ;; Slave Gnusii should then clear the dribble buffer.
15268     (when (and init gnus-slave)
15269       (gnus-dribble-clear))
15270
15271     (gnus-update-format-specifications)
15272
15273     ;; See whether we need to read the description file.
15274     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15275              (not gnus-description-hashtb)
15276              (not dont-connect)
15277              gnus-read-active-file)
15278         (gnus-read-all-descriptions-files))
15279
15280     ;; Find new newsgroups and treat them.
15281     (if (and init gnus-check-new-newsgroups (not level)
15282              (gnus-check-server gnus-select-method))
15283         (gnus-find-new-newsgroups))
15284
15285     ;; We might read in new NoCeM messages here.
15286     (when (and gnus-use-nocem 
15287                (not level)
15288                (not dont-connect))
15289       (gnus-nocem-scan-groups))
15290
15291     ;; Find the number of unread articles in each non-dead group.
15292     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15293       (gnus-get-unread-articles level))
15294
15295     (if (and init gnus-check-bogus-newsgroups
15296              gnus-read-active-file (not level)
15297              (gnus-server-opened gnus-select-method))
15298         (gnus-check-bogus-newsgroups))))
15299
15300 (defun gnus-find-new-newsgroups (&optional arg)
15301   "Search for new newsgroups and add them.
15302 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15303 The `-n' option line from .newsrc is respected.
15304 If ARG (the prefix), use the `ask-server' method to query
15305 the server for new groups."
15306   (interactive "P")
15307   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15308                        (null gnus-read-active-file)
15309                        (eq gnus-read-active-file 'some))
15310                    'ask-server gnus-check-new-newsgroups)))
15311     (unless (gnus-check-first-time-used)
15312       (if (or (consp check)
15313               (eq check 'ask-server))
15314           ;; Ask the server for new groups.
15315           (gnus-ask-server-for-new-groups)
15316         ;; Go through the active hashtb and look for new groups.
15317         (let ((groups 0)
15318               group new-newsgroups)
15319           (gnus-message 5 "Looking for new newsgroups...")
15320           (unless gnus-have-read-active-file
15321             (gnus-read-active-file))
15322           (setq gnus-newsrc-last-checked-date (current-time-string))
15323           (unless gnus-killed-hashtb
15324             (gnus-make-hashtable-from-killed))
15325           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15326           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15327           (mapatoms
15328            (lambda (sym)
15329              (if (or (null (setq group (symbol-name sym)))
15330                      (not (boundp sym))
15331                      (null (symbol-value sym))
15332                      (gnus-gethash group gnus-killed-hashtb)
15333                      (gnus-gethash group gnus-newsrc-hashtb))
15334                  ()
15335                (let ((do-sub (gnus-matches-options-n group)))
15336                  (cond
15337                   ((eq do-sub 'subscribe)
15338                    (setq groups (1+ groups))
15339                    (gnus-sethash group group gnus-killed-hashtb)
15340                    (funcall gnus-subscribe-options-newsgroup-method group))
15341                   ((eq do-sub 'ignore)
15342                    nil)
15343                   (t
15344                    (setq groups (1+ groups))
15345                    (gnus-sethash group group gnus-killed-hashtb)
15346                    (if gnus-subscribe-hierarchical-interactive
15347                        (setq new-newsgroups (cons group new-newsgroups))
15348                      (funcall gnus-subscribe-newsgroup-method group)))))))
15349            gnus-active-hashtb)
15350           (when new-newsgroups
15351             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15352           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15353           (if (> groups 0)
15354               (gnus-message 6 "%d new newsgroup%s arrived."
15355                             groups (if (> groups 1) "s have" " has"))
15356             (gnus-message 6 "No new newsgroups.")))))))
15357
15358 (defun gnus-matches-options-n (group)
15359   ;; Returns `subscribe' if the group is to be unconditionally
15360   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15361   ;; no match for the group.
15362
15363   ;; First we check the two user variables.
15364   (cond
15365    ((and gnus-options-subscribe
15366          (string-match gnus-options-subscribe group))
15367     'subscribe)
15368    ((and gnus-auto-subscribed-groups
15369          (string-match gnus-auto-subscribed-groups group))
15370     'subscribe)
15371    ((and gnus-options-not-subscribe
15372          (string-match gnus-options-not-subscribe group))
15373     'ignore)
15374    ;; Then we go through the list that was retrieved from the .newsrc
15375    ;; file.  This list has elements on the form
15376    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15377    ;; is in the reverse order of the options line) is returned.
15378    (t
15379     (let ((regs gnus-newsrc-options-n))
15380       (while (and regs
15381                   (not (string-match (caar regs) group)))
15382         (setq regs (cdr regs)))
15383       (and regs (cdar regs))))))
15384
15385 (defun gnus-ask-server-for-new-groups ()
15386   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15387          (methods (cons gnus-select-method
15388                         (nconc
15389                          (when gnus-message-archive-method
15390                            (list "archive"))
15391                          (append
15392                           (and (consp gnus-check-new-newsgroups)
15393                                gnus-check-new-newsgroups)
15394                           gnus-secondary-select-methods))))
15395          (groups 0)
15396          (new-date (current-time-string))
15397          group new-newsgroups got-new method hashtb
15398          gnus-override-subscribe-method)
15399     ;; Go through both primary and secondary select methods and
15400     ;; request new newsgroups.
15401     (while (setq method (gnus-server-get-method nil (pop methods)))
15402       (setq new-newsgroups nil)
15403       (setq gnus-override-subscribe-method method)
15404       (when (and (gnus-check-server method)
15405                  (gnus-request-newgroups date method))
15406         (save-excursion
15407           (setq got-new t)
15408           (setq hashtb (gnus-make-hashtable 100))
15409           (set-buffer nntp-server-buffer)
15410           ;; Enter all the new groups into a hashtable.
15411           (gnus-active-to-gnus-format method hashtb 'ignore))
15412         ;; Now all new groups from `method' are in `hashtb'.
15413         (mapatoms
15414          (lambda (group-sym)
15415            (if (or (null (setq group (symbol-name group-sym)))
15416                    (not (boundp group-sym))
15417                    (null (symbol-value group-sym))
15418                    (gnus-gethash group gnus-newsrc-hashtb)
15419                    (member group gnus-zombie-list)
15420                    (member group gnus-killed-list))
15421                ;; The group is already known.
15422                ()
15423              ;; Make this group active.
15424              (when (symbol-value group-sym)
15425                (gnus-set-active group (symbol-value group-sym)))
15426              ;; Check whether we want it or not.
15427              (let ((do-sub (gnus-matches-options-n group)))
15428                (cond
15429                 ((eq do-sub 'subscribe)
15430                  (incf groups)
15431                  (gnus-sethash group group gnus-killed-hashtb)
15432                  (funcall gnus-subscribe-options-newsgroup-method group))
15433                 ((eq do-sub 'ignore)
15434                  nil)
15435                 (t
15436                  (incf groups)
15437                  (gnus-sethash group group gnus-killed-hashtb)
15438                  (if gnus-subscribe-hierarchical-interactive
15439                      (push group new-newsgroups)
15440                    (funcall gnus-subscribe-newsgroup-method group)))))))
15441          hashtb))
15442       (when new-newsgroups
15443         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15444     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15445     (when (> groups 0)
15446       (gnus-message 6 "%d new newsgroup%s arrived."
15447                     groups (if (> groups 1) "s have" " has")))
15448     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15449     got-new))
15450
15451 (defun gnus-check-first-time-used ()
15452   (if (or (> (length gnus-newsrc-alist) 1)
15453           (file-exists-p gnus-startup-file)
15454           (file-exists-p (concat gnus-startup-file ".el"))
15455           (file-exists-p (concat gnus-startup-file ".eld")))
15456       nil
15457     (gnus-message 6 "First time user; subscribing you to default groups")
15458     (unless (gnus-read-active-file-p)
15459       (gnus-read-active-file))
15460     (setq gnus-newsrc-last-checked-date (current-time-string))
15461     (let ((groups gnus-default-subscribed-newsgroups)
15462           group)
15463       (if (eq groups t)
15464           nil
15465         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15466         (mapatoms
15467          (lambda (sym)
15468            (if (null (setq group (symbol-name sym)))
15469                ()
15470              (let ((do-sub (gnus-matches-options-n group)))
15471                (cond
15472                 ((eq do-sub 'subscribe)
15473                  (gnus-sethash group group gnus-killed-hashtb)
15474                  (funcall gnus-subscribe-options-newsgroup-method group))
15475                 ((eq do-sub 'ignore)
15476                  nil)
15477                 (t
15478                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15479          gnus-active-hashtb)
15480         (while groups
15481           (if (gnus-active (car groups))
15482               (gnus-group-change-level
15483                (car groups) gnus-level-default-subscribed gnus-level-killed))
15484           (setq groups (cdr groups)))
15485         (gnus-group-make-help-group)
15486         (and gnus-novice-user
15487              (gnus-message 7 "`A k' to list killed groups"))))))
15488
15489 (defun gnus-subscribe-group (group previous &optional method)
15490   (gnus-group-change-level
15491    (if method
15492        (list t group gnus-level-default-subscribed nil nil method)
15493      group)
15494    gnus-level-default-subscribed gnus-level-killed previous t))
15495
15496 ;; `gnus-group-change-level' is the fundamental function for changing
15497 ;; subscription levels of newsgroups.  This might mean just changing
15498 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15499 ;; again, which subscribes/unsubscribes a group, which is equally
15500 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15501 ;; from 8-9 to 1-7 means that you remove the group from the list of
15502 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15503 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15504 ;; which is trivial.
15505 ;; ENTRY can either be a string (newsgroup name) or a list (if
15506 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15507 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15508 ;; entries.
15509 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15510 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15511 ;; after.
15512 (defun gnus-group-change-level (entry level &optional oldlevel
15513                                       previous fromkilled)
15514   (let (group info active num)
15515     ;; Glean what info we can from the arguments
15516     (if (consp entry)
15517         (if fromkilled (setq group (nth 1 entry))
15518           (setq group (car (nth 2 entry))))
15519       (setq group entry))
15520     (if (and (stringp entry)
15521              oldlevel
15522              (< oldlevel gnus-level-zombie))
15523         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15524     (if (and (not oldlevel)
15525              (consp entry))
15526         (setq oldlevel (gnus-info-level (nth 2 entry)))
15527       (setq oldlevel (or oldlevel 9)))
15528     (if (stringp previous)
15529         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15530
15531     (if (and (>= oldlevel gnus-level-zombie)
15532              (gnus-gethash group gnus-newsrc-hashtb))
15533         ;; We are trying to subscribe a group that is already
15534         ;; subscribed.
15535         ()                              ; Do nothing.
15536
15537       (or (gnus-ephemeral-group-p group)
15538           (gnus-dribble-enter
15539            (format "(gnus-group-change-level %S %S %S %S %S)"
15540                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15541
15542       ;; Then we remove the newgroup from any old structures, if needed.
15543       ;; If the group was killed, we remove it from the killed or zombie
15544       ;; list.  If not, and it is in fact going to be killed, we remove
15545       ;; it from the newsrc hash table and assoc.
15546       (cond
15547        ((>= oldlevel gnus-level-zombie)
15548         (if (= oldlevel gnus-level-zombie)
15549             (setq gnus-zombie-list (delete group gnus-zombie-list))
15550           (setq gnus-killed-list (delete group gnus-killed-list))))
15551        (t
15552         (if (and (>= level gnus-level-zombie)
15553                  entry)
15554             (progn
15555               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15556               (if (nth 3 entry)
15557                   (setcdr (gnus-gethash (car (nth 3 entry))
15558                                         gnus-newsrc-hashtb)
15559                           (cdr entry)))
15560               (setcdr (cdr entry) (cdddr entry))))))
15561
15562       ;; Finally we enter (if needed) the list where it is supposed to
15563       ;; go, and change the subscription level.  If it is to be killed,
15564       ;; we enter it into the killed or zombie list.
15565       (cond 
15566        ((>= level gnus-level-zombie)
15567         ;; Remove from the hash table.
15568         (gnus-sethash group nil gnus-newsrc-hashtb)
15569         ;; We do not enter foreign groups into the list of dead
15570         ;; groups.
15571         (unless (gnus-group-foreign-p group)
15572           (if (= level gnus-level-zombie)
15573               (setq gnus-zombie-list (cons group gnus-zombie-list))
15574             (setq gnus-killed-list (cons group gnus-killed-list)))))
15575        (t
15576         ;; If the list is to be entered into the newsrc assoc, and
15577         ;; it was killed, we have to create an entry in the newsrc
15578         ;; hashtb format and fix the pointers in the newsrc assoc.
15579         (if (< oldlevel gnus-level-zombie)
15580             ;; It was alive, and it is going to stay alive, so we
15581             ;; just change the level and don't change any pointers or
15582             ;; hash table entries.
15583             (setcar (cdaddr entry) level)
15584           (if (listp entry)
15585               (setq info (cdr entry)
15586                     num (car entry))
15587             (setq active (gnus-active group))
15588             (setq num
15589                   (if active (- (1+ (cdr active)) (car active)) t))
15590             ;; Check whether the group is foreign.  If so, the
15591             ;; foreign select method has to be entered into the
15592             ;; info.
15593             (let ((method (or gnus-override-subscribe-method
15594                               (gnus-group-method group))))
15595               (if (eq method gnus-select-method)
15596                   (setq info (list group level nil))
15597                 (setq info (list group level nil nil method)))))
15598           (unless previous
15599             (setq previous
15600                   (let ((p gnus-newsrc-alist))
15601                     (while (cddr p)
15602                       (setq p (cdr p)))
15603                     p)))
15604           (setq entry (cons info (cddr previous)))
15605           (if (cdr previous)
15606               (progn
15607                 (setcdr (cdr previous) entry)
15608                 (gnus-sethash group (cons num (cdr previous))
15609                               gnus-newsrc-hashtb))
15610             (setcdr previous entry)
15611             (gnus-sethash group (cons num previous)
15612                           gnus-newsrc-hashtb))
15613           (when (cdr entry)
15614             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15615       (when gnus-group-change-level-function
15616         (funcall gnus-group-change-level-function group level oldlevel)))))
15617
15618 (defun gnus-kill-newsgroup (newsgroup)
15619   "Obsolete function.  Kills a newsgroup."
15620   (gnus-group-change-level
15621    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15622
15623 (defun gnus-check-bogus-newsgroups (&optional confirm)
15624   "Remove bogus newsgroups.
15625 If CONFIRM is non-nil, the user has to confirm the deletion of every
15626 newsgroup."
15627   (let ((newsrc (cdr gnus-newsrc-alist))
15628         bogus group entry info)
15629     (gnus-message 5 "Checking bogus newsgroups...")
15630     (unless (gnus-read-active-file-p)
15631       (gnus-read-active-file))
15632     (when (gnus-read-active-file-p)
15633       ;; Find all bogus newsgroup that are subscribed.
15634       (while newsrc
15635         (setq info (pop newsrc)
15636               group (gnus-info-group info))
15637         (unless (or (gnus-active group) ; Active
15638                     (gnus-info-method info) ; Foreign
15639                     (and confirm
15640                          (not (gnus-y-or-n-p
15641                                (format "Remove bogus newsgroup: %s " group)))))
15642           ;; Found a bogus newsgroup.
15643           (push group bogus)))
15644       ;; Remove all bogus subscribed groups by first killing them, and
15645       ;; then removing them from the list of killed groups.
15646       (while bogus
15647         (when (setq entry (gnus-gethash (setq group (pop bogus))
15648                                         gnus-newsrc-hashtb))
15649           (gnus-group-change-level entry gnus-level-killed)
15650           (setq gnus-killed-list (delete group gnus-killed-list))))
15651       ;; Then we remove all bogus groups from the list of killed and
15652       ;; zombie groups.  They are are removed without confirmation.
15653       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15654             killed)
15655         (while dead-lists
15656           (setq killed (symbol-value (car dead-lists)))
15657           (while killed
15658             (unless (gnus-active (setq group (pop killed)))
15659               ;; The group is bogus.
15660               ;; !!!Slow as hell.
15661               (set (car dead-lists)
15662                    (delete group (symbol-value (car dead-lists))))))
15663           (setq dead-lists (cdr dead-lists))))
15664       (gnus-message 5 "Checking bogus newsgroups...done"))))
15665
15666 (defun gnus-check-duplicate-killed-groups ()
15667   "Remove duplicates from the list of killed groups."
15668   (interactive)
15669   (let ((killed gnus-killed-list))
15670     (while killed
15671       (gnus-message 9 "%d" (length killed))
15672       (setcdr killed (delete (car killed) (cdr killed)))
15673       (setq killed (cdr killed)))))
15674
15675 ;; We want to inline a function from gnus-cache, so we cheat here:
15676 (eval-when-compile
15677   (provide 'gnus)
15678   (require 'gnus-cache))
15679
15680 (defun gnus-get-unread-articles-in-group (info active &optional update)
15681   (when active
15682     ;; Allow the backend to update the info in the group.
15683     (when (and update 
15684                (gnus-request-update-info
15685                 info (gnus-find-method-for-group (gnus-info-group info))))
15686       (gnus-activate-group (gnus-info-group info) nil t))
15687     (let* ((range (gnus-info-read info))
15688            (num 0))
15689       ;; If a cache is present, we may have to alter the active info.
15690       (when (and gnus-use-cache info)
15691         (inline (gnus-cache-possibly-alter-active 
15692                  (gnus-info-group info) active)))
15693       ;; Modify the list of read articles according to what articles
15694       ;; are available; then tally the unread articles and add the
15695       ;; number to the group hash table entry.
15696       (cond
15697        ((zerop (cdr active))
15698         (setq num 0))
15699        ((not range)
15700         (setq num (- (1+ (cdr active)) (car active))))
15701        ((not (listp (cdr range)))
15702         ;; Fix a single (num . num) range according to the
15703         ;; active hash table.
15704         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15705         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15706         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15707         ;; Compute number of unread articles.
15708         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15709        (t
15710         ;; The read list is a list of ranges.  Fix them according to
15711         ;; the active hash table.
15712         ;; First peel off any elements that are below the lower
15713         ;; active limit.
15714         (while (and (cdr range)
15715                     (>= (car active)
15716                         (or (and (atom (cadr range)) (cadr range))
15717                             (caadr range))))
15718           (if (numberp (car range))
15719               (setcar range
15720                       (cons (car range)
15721                             (or (and (numberp (cadr range))
15722                                      (cadr range))
15723                                 (cdadr range))))
15724             (setcdr (car range)
15725                     (or (and (numberp (nth 1 range)) (nth 1 range))
15726                         (cdadr range))))
15727           (setcdr range (cddr range)))
15728         ;; Adjust the first element to be the same as the lower limit.
15729         (if (and (not (atom (car range)))
15730                  (< (cdar range) (car active)))
15731             (setcdr (car range) (1- (car active))))
15732         ;; Then we want to peel off any elements that are higher
15733         ;; than the upper active limit.
15734         (let ((srange range))
15735           ;; Go past all legal elements.
15736           (while (and (cdr srange)
15737                       (<= (or (and (atom (cadr srange))
15738                                    (cadr srange))
15739                               (caadr srange)) (cdr active)))
15740             (setq srange (cdr srange)))
15741           (if (cdr srange)
15742               ;; Nuke all remaining illegal elements.
15743               (setcdr srange nil))
15744
15745           ;; Adjust the final element.
15746           (if (and (not (atom (car srange)))
15747                    (> (cdar srange) (cdr active)))
15748               (setcdr (car srange) (cdr active))))
15749         ;; Compute the number of unread articles.
15750         (while range
15751           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15752                                       (cdar range)))
15753                               (or (and (atom (car range)) (car range))
15754                                   (caar range)))))
15755           (setq range (cdr range)))
15756         (setq num (max 0 (- (cdr active) num)))))
15757       ;; Set the number of unread articles.
15758       (when info
15759         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15760       num)))
15761
15762 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15763 ;; and compute how many unread articles there are in each group.
15764 (defun gnus-get-unread-articles (&optional level)
15765   (let* ((newsrc (cdr gnus-newsrc-alist))
15766          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15767          (foreign-level
15768           (min
15769            (cond ((and gnus-activate-foreign-newsgroups
15770                        (not (numberp gnus-activate-foreign-newsgroups)))
15771                   (1+ gnus-level-subscribed))
15772                  ((numberp gnus-activate-foreign-newsgroups)
15773                   gnus-activate-foreign-newsgroups)
15774                  (t 0))
15775            level))
15776          info group active method)
15777     (gnus-message 5 "Checking new news...")
15778
15779     (while newsrc
15780       (setq active (gnus-active (setq group (gnus-info-group
15781                                              (setq info (pop newsrc))))))
15782
15783       ;; Check newsgroups.  If the user doesn't want to check them, or
15784       ;; they can't be checked (for instance, if the news server can't
15785       ;; be reached) we just set the number of unread articles in this
15786       ;; newsgroup to t.  This means that Gnus thinks that there are
15787       ;; unread articles, but it has no idea how many.
15788       (if (and (setq method (gnus-info-method info))
15789                (not (gnus-server-equal
15790                      gnus-select-method
15791                      (setq method (gnus-server-get-method nil method))))
15792                (not (gnus-secondary-method-p method)))
15793           ;; These groups are foreign.  Check the level.
15794           (when (<= (gnus-info-level info) foreign-level)
15795             (setq active (gnus-activate-group group 'scan))
15796             (unless (inline (gnus-virtual-group-p group))
15797               (inline (gnus-close-group group)))
15798             (when (fboundp (intern (concat (symbol-name (car method))
15799                                            "-request-update-info")))
15800               (inline (gnus-request-update-info info method))))
15801         ;; These groups are native or secondary.
15802         (when (and (<= (gnus-info-level info) level)
15803                    (not gnus-read-active-file))
15804           (setq active (gnus-activate-group group 'scan))
15805           (inline (gnus-close-group group))))
15806
15807       ;; Get the number of unread articles in the group.
15808       (if active
15809           (inline (gnus-get-unread-articles-in-group info active))
15810         ;; The group couldn't be reached, so we nix out the number of
15811         ;; unread articles and stuff.
15812         (gnus-set-active group nil)
15813         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15814
15815     (gnus-message 5 "Checking new news...done")))
15816
15817 ;; Create a hash table out of the newsrc alist.  The `car's of the
15818 ;; alist elements are used as keys.
15819 (defun gnus-make-hashtable-from-newsrc-alist ()
15820   (let ((alist gnus-newsrc-alist)
15821         (ohashtb gnus-newsrc-hashtb)
15822         prev)
15823     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15824     (setq alist
15825           (setq prev (setq gnus-newsrc-alist
15826                            (if (equal (caar gnus-newsrc-alist)
15827                                       "dummy.group")
15828                                gnus-newsrc-alist
15829                              (cons (list "dummy.group" 0 nil) alist)))))
15830     (while alist
15831       (gnus-sethash
15832        (caar alist)
15833        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15834              prev)
15835        gnus-newsrc-hashtb)
15836       (setq prev alist
15837             alist (cdr alist)))))
15838
15839 (defun gnus-make-hashtable-from-killed ()
15840   "Create a hash table from the killed and zombie lists."
15841   (let ((lists '(gnus-killed-list gnus-zombie-list))
15842         list)
15843     (setq gnus-killed-hashtb
15844           (gnus-make-hashtable
15845            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15846     (while (setq list (pop lists))
15847       (setq list (symbol-value list))
15848       (while list
15849         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15850
15851 (defun gnus-activate-group (group &optional scan dont-check)
15852   ;; Check whether a group has been activated or not.
15853   ;; If SCAN, request a scan of that group as well.
15854   (let ((method (gnus-find-method-for-group group))
15855         active)
15856     (and (gnus-check-server method)
15857          ;; We escape all bugs and quit here to make it possible to
15858          ;; continue if a group is so out-there that it reports bugs
15859          ;; and stuff.
15860          (progn
15861            (and scan
15862                 (gnus-check-backend-function 'request-scan (car method))
15863                 (gnus-request-scan group method))
15864            t)
15865          (condition-case ()
15866              (gnus-request-group group dont-check)
15867         ;   (error nil)
15868            (quit nil))
15869          (save-excursion
15870            (set-buffer nntp-server-buffer)
15871            (goto-char (point-min))
15872            ;; Parse the result we got from `gnus-request-group'.
15873            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15874                 (progn
15875                   (goto-char (match-beginning 1))
15876                   (gnus-set-active
15877                    group (setq active (cons (read (current-buffer))
15878                                             (read (current-buffer)))))
15879                   ;; Return the new active info.
15880                   active))))))
15881
15882 (defun gnus-update-read-articles (group unread)
15883   "Update the list of read and ticked articles in GROUP using the
15884 UNREAD and TICKED lists.
15885 Note: UNSELECTED has to be sorted over `<'.
15886 Returns whether the updating was successful."
15887   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15888          (entry (gnus-gethash group gnus-newsrc-hashtb))
15889          (info (nth 2 entry))
15890          (prev 1)
15891          (unread (sort (copy-sequence unread) '<))
15892          read)
15893     (if (or (not info) (not active))
15894         ;; There is no info on this group if it was, in fact,
15895         ;; killed.  Gnus stores no information on killed groups, so
15896         ;; there's nothing to be done.
15897         ;; One could store the information somewhere temporarily,
15898         ;; perhaps...  Hmmm...
15899         ()
15900       ;; Remove any negative articles numbers.
15901       (while (and unread (< (car unread) 0))
15902         (setq unread (cdr unread)))
15903       ;; Remove any expired article numbers
15904       (while (and unread (< (car unread) (car active)))
15905         (setq unread (cdr unread)))
15906       ;; Compute the ranges of read articles by looking at the list of
15907       ;; unread articles.
15908       (while unread
15909         (if (/= (car unread) prev)
15910             (setq read (cons (if (= prev (1- (car unread))) prev
15911                                (cons prev (1- (car unread)))) read)))
15912         (setq prev (1+ (car unread)))
15913         (setq unread (cdr unread)))
15914       (when (<= prev (cdr active))
15915         (setq read (cons (cons prev (cdr active)) read)))
15916       ;; Enter this list into the group info.
15917       (gnus-info-set-read
15918        info (if (> (length read) 1) (nreverse read) read))
15919       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15920       (gnus-get-unread-articles-in-group info (gnus-active group))
15921       t)))
15922
15923 (defun gnus-make-articles-unread (group articles)
15924   "Mark ARTICLES in GROUP as unread."
15925   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15926                           (gnus-gethash (gnus-group-real-name group)
15927                                         gnus-newsrc-hashtb))))
15928          (ranges (gnus-info-read info))
15929          news article)
15930     (while articles
15931       (when (gnus-member-of-range
15932              (setq article (pop articles)) ranges)
15933         (setq news (cons article news))))
15934     (when news
15935       (gnus-info-set-read
15936        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15937       (gnus-group-update-group group t))))
15938
15939 ;; Enter all dead groups into the hashtb.
15940 (defun gnus-update-active-hashtb-from-killed ()
15941   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15942         (lists (list gnus-killed-list gnus-zombie-list))
15943         killed)
15944     (while lists
15945       (setq killed (car lists))
15946       (while killed
15947         (gnus-sethash (car killed) nil hashtb)
15948         (setq killed (cdr killed)))
15949       (setq lists (cdr lists)))))
15950
15951 (defun gnus-get-killed-groups ()
15952   "Go through the active hashtb and all all unknown groups as killed."
15953   ;; First make sure active file has been read.
15954   (unless (gnus-read-active-file-p)
15955     (let ((gnus-read-active-file t))
15956       (gnus-read-active-file)))
15957   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
15958   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
15959   (mapatoms
15960    (lambda (sym)
15961      (let ((groups 0)
15962            (group (symbol-name sym)))
15963        (if (or (null group)
15964                (gnus-gethash group gnus-killed-hashtb)
15965                (gnus-gethash group gnus-newsrc-hashtb))
15966            ()
15967          (let ((do-sub (gnus-matches-options-n group)))
15968            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
15969                ()
15970              (setq groups (1+ groups))
15971              (setq gnus-killed-list
15972                    (cons group gnus-killed-list))
15973              (gnus-sethash group group gnus-killed-hashtb))))))
15974    gnus-active-hashtb))
15975
15976 ;; Get the active file(s) from the backend(s).
15977 (defun gnus-read-active-file ()
15978   (gnus-group-set-mode-line)
15979   (let ((methods 
15980          (append
15981           (if (gnus-check-server gnus-select-method)
15982               ;; The native server is available.
15983               (cons gnus-select-method gnus-secondary-select-methods)
15984             ;; The native server is down, so we just do the
15985             ;; secondary ones.
15986             gnus-secondary-select-methods)
15987           ;; Also read from the archive server.
15988           (when gnus-message-archive-method
15989             (list "archive"))))
15990         list-type)
15991     (setq gnus-have-read-active-file nil)
15992     (save-excursion
15993       (set-buffer nntp-server-buffer)
15994       (while methods
15995         (let* ((method (if (stringp (car methods))
15996                            (gnus-server-get-method nil (car methods))
15997                          (car methods)))
15998                (where (nth 1 method))
15999                (mesg (format "Reading active file%s via %s..."
16000                              (if (and where (not (zerop (length where))))
16001                                  (concat " from " where) "")
16002                              (car method))))
16003           (gnus-message 5 mesg)
16004           (when (gnus-check-server method)
16005             ;; Request that the backend scan its incoming messages.
16006             (and (gnus-check-backend-function 'request-scan (car method))
16007                  (gnus-request-scan nil method))
16008             (cond
16009              ((and (eq gnus-read-active-file 'some)
16010                    (gnus-check-backend-function 'retrieve-groups (car method)))
16011               (let ((newsrc (cdr gnus-newsrc-alist))
16012                     (gmethod (gnus-server-get-method nil method))
16013                     groups info)
16014                 (while (setq info (pop newsrc))
16015                   (when (gnus-server-equal
16016                          (gnus-find-method-for-group 
16017                           (gnus-info-group info) info)
16018                          gmethod)
16019                     (push (gnus-group-real-name (gnus-info-group info)) 
16020                           groups)))
16021                 (when groups
16022                   (gnus-check-server method)
16023                   (setq list-type (gnus-retrieve-groups groups method))
16024                   (cond
16025                    ((not list-type)
16026                     (gnus-error
16027                      1.2 "Cannot read partial active file from %s server."
16028                      (car method)))
16029                    ((eq list-type 'active)
16030                     (gnus-active-to-gnus-format method gnus-active-hashtb))
16031                    (t
16032                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
16033              (t
16034               (if (not (gnus-request-list method))
16035                   (unless (equal method gnus-message-archive-method)
16036                     (gnus-error 1 "Cannot read active file from %s server."
16037                                 (car method)))
16038                 (gnus-active-to-gnus-format method gnus-active-hashtb)
16039                 ;; We mark this active file as read.
16040                 (push method gnus-have-read-active-file)
16041                 (gnus-message 5 "%sdone" mesg))))))
16042         (setq methods (cdr methods))))))
16043
16044 ;; Read an active file and place the results in `gnus-active-hashtb'.
16045 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
16046   (unless method
16047     (setq method gnus-select-method))
16048   (let ((cur (current-buffer))
16049         (hashtb (or hashtb
16050                     (if (and gnus-active-hashtb
16051                              (not (equal method gnus-select-method)))
16052                         gnus-active-hashtb
16053                       (setq gnus-active-hashtb
16054                             (if (equal method gnus-select-method)
16055                                 (gnus-make-hashtable
16056                                  (count-lines (point-min) (point-max)))
16057                               (gnus-make-hashtable 4096)))))))
16058     ;; Delete unnecessary lines.
16059     (goto-char (point-min))
16060     (while (search-forward "\nto." nil t)
16061       (delete-region (1+ (match-beginning 0))
16062                      (progn (forward-line 1) (point))))
16063     (or (string= gnus-ignored-newsgroups "")
16064         (progn
16065           (goto-char (point-min))
16066           (delete-matching-lines gnus-ignored-newsgroups)))
16067     ;; Make the group names readable as a lisp expression even if they
16068     ;; contain special characters.
16069     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
16070     (goto-char (point-max))
16071     (while (re-search-backward "[][';?()#]" nil t)
16072       (insert ?\\))
16073     ;; If these are groups from a foreign select method, we insert the
16074     ;; group prefix in front of the group names.
16075     (and method (not (gnus-server-equal
16076                       (gnus-server-get-method nil method)
16077                       (gnus-server-get-method nil gnus-select-method)))
16078          (let ((prefix (gnus-group-prefixed-name "" method)))
16079            (goto-char (point-min))
16080            (while (and (not (eobp))
16081                        (progn (insert prefix)
16082                               (zerop (forward-line 1)))))))
16083     ;; Store the active file in a hash table.
16084     (goto-char (point-min))
16085     (if (string-match "%[oO]" gnus-group-line-format)
16086         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16087         ;; If we want information on moderated groups, we use this
16088         ;; loop...
16089         (let* ((mod-hashtb (make-vector 7 0))
16090                (m (intern "m" mod-hashtb))
16091                group max min)
16092           (while (not (eobp))
16093             (condition-case nil
16094                 (progn
16095                   (narrow-to-region (point) (gnus-point-at-eol))
16096                   (setq group (let ((obarray hashtb)) (read cur)))
16097                   (if (and (numberp (setq max (read cur)))
16098                            (numberp (setq min (read cur)))
16099                            (progn
16100                              (skip-chars-forward " \t")
16101                              (not
16102                               (or (= (following-char) ?=)
16103                                   (= (following-char) ?x)
16104                                   (= (following-char) ?j)))))
16105                       (set group (cons min max))
16106                     (set group nil))
16107                   ;; Enter moderated groups into a list.
16108                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16109                       (setq gnus-moderated-list
16110                             (cons (symbol-name group) gnus-moderated-list))))
16111               (error
16112                (and group
16113                     (symbolp group)
16114                     (set group nil))))
16115             (widen)
16116             (forward-line 1)))
16117       ;; And if we do not care about moderation, we use this loop,
16118       ;; which is faster.
16119       (let (group max min)
16120         (while (not (eobp))
16121           (condition-case ()
16122               (progn
16123                 (narrow-to-region (point) (gnus-point-at-eol))
16124                 ;; group gets set to a symbol interned in the hash table
16125                 ;; (what a hack!!) - jwz
16126                 (setq group (let ((obarray hashtb)) (read cur)))
16127                 (if (and (numberp (setq max (read cur)))
16128                          (numberp (setq min (read cur)))
16129                          (progn
16130                            (skip-chars-forward " \t")
16131                            (not
16132                             (or (= (following-char) ?=)
16133                                 (= (following-char) ?x)
16134                                 (= (following-char) ?j)))))
16135                     (set group (cons min max))
16136                   (set group nil)))
16137             (error
16138              (progn
16139                (and group
16140                     (symbolp group)
16141                     (set group nil))
16142                (or ignore-errors
16143                    (gnus-message 3 "Warning - illegal active: %s"
16144                                  (buffer-substring
16145                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16146           (widen)
16147           (forward-line 1))))))
16148
16149 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16150   ;; Parse a "groups" active file.
16151   (let ((cur (current-buffer))
16152         (hashtb (or hashtb
16153                     (if (and method gnus-active-hashtb)
16154                         gnus-active-hashtb
16155                       (setq gnus-active-hashtb
16156                             (gnus-make-hashtable
16157                              (count-lines (point-min) (point-max)))))))
16158         (prefix (and method
16159                      (not (gnus-server-equal
16160                            (gnus-server-get-method nil method)
16161                            (gnus-server-get-method nil gnus-select-method)))
16162                      (gnus-group-prefixed-name "" method))))
16163
16164     (goto-char (point-min))
16165     ;; We split this into to separate loops, one with the prefix
16166     ;; and one without to speed the reading up somewhat.
16167     (if prefix
16168         (let (min max opoint group)
16169           (while (not (eobp))
16170             (condition-case ()
16171                 (progn
16172                   (read cur) (read cur)
16173                   (setq min (read cur)
16174                         max (read cur)
16175                         opoint (point))
16176                   (skip-chars-forward " \t")
16177                   (insert prefix)
16178                   (goto-char opoint)
16179                   (set (let ((obarray hashtb)) (read cur))
16180                        (cons min max)))
16181               (error (and group (symbolp group) (set group nil))))
16182             (forward-line 1)))
16183       (let (min max group)
16184         (while (not (eobp))
16185           (condition-case ()
16186               (if (= (following-char) ?2)
16187                   (progn
16188                     (read cur) (read cur)
16189                     (setq min (read cur)
16190                           max (read cur))
16191                     (set (setq group (let ((obarray hashtb)) (read cur)))
16192                          (cons min max))))
16193             (error (and group (symbolp group) (set group nil))))
16194           (forward-line 1))))))
16195
16196 (defun gnus-read-newsrc-file (&optional force)
16197   "Read startup file.
16198 If FORCE is non-nil, the .newsrc file is read."
16199   ;; Reset variables that might be defined in the .newsrc.eld file.
16200   (let ((variables gnus-variable-list))
16201     (while variables
16202       (set (car variables) nil)
16203       (setq variables (cdr variables))))
16204   (let* ((newsrc-file gnus-current-startup-file)
16205          (quick-file (concat newsrc-file ".el")))
16206     (save-excursion
16207       ;; We always load the .newsrc.eld file.  If always contains
16208       ;; much information that can not be gotten from the .newsrc
16209       ;; file (ticked articles, killed groups, foreign methods, etc.)
16210       (gnus-read-newsrc-el-file quick-file)
16211
16212       (if (and (file-exists-p gnus-current-startup-file)
16213                (or force
16214                    (and (file-newer-than-file-p newsrc-file quick-file)
16215                         (file-newer-than-file-p newsrc-file
16216                                                 (concat quick-file "d")))
16217                    (not gnus-newsrc-alist)))
16218           ;; We read the .newsrc file.  Note that if there if a
16219           ;; .newsrc.eld file exists, it has already been read, and
16220           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16221           ;; the .newsrc file, Gnus will only use the information it
16222           ;; can find there for changing the data already read -
16223           ;; ie. reading the .newsrc file will not trash the data
16224           ;; already read (except for read articles).
16225           (save-excursion
16226             (gnus-message 5 "Reading %s..." newsrc-file)
16227             (set-buffer (find-file-noselect newsrc-file))
16228             (buffer-disable-undo (current-buffer))
16229             (gnus-newsrc-to-gnus-format)
16230             (kill-buffer (current-buffer))
16231             (gnus-message 5 "Reading %s...done" newsrc-file)))
16232
16233       ;; Read any slave files.
16234       (unless gnus-slave
16235         (gnus-master-read-slave-newsrc))
16236       
16237       ;; Convert old to new.
16238       (gnus-convert-old-newsrc))))
16239
16240 (defun gnus-continuum-version (version)
16241   "Return VERSION as a floating point number."
16242   (when (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16243     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16244            (number (match-string 2 version))
16245            major minor least)
16246       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16247       (setq major (string-to-number (match-string 1 number)))
16248       (setq minor (string-to-number (match-string 2 number)))
16249       (setq least (if (match-beginning 3)
16250                       (string-to-number (match-string 3 number))
16251                     0))
16252       (string-to-number
16253        (if (zerop major)
16254            (format "%s00%02d%02d"
16255                    (cond 
16256                     ((string= alpha "(ding)") "4.99")
16257                     ((string= alpha "September") "5.01")
16258                     ((string= alpha "Red") "5.03"))
16259                    minor least)
16260          (format "%d.%02d%20d" major minor least))))))
16261
16262 (defun gnus-convert-old-newsrc ()
16263   "Convert old newsrc into the new format, if needed."
16264   (let ((fcv (and gnus-newsrc-file-version
16265                   (gnus-continuum-version gnus-newsrc-file-version))))
16266     (cond
16267      ;; No .newsrc.eld file was loaded.
16268      ((null fcv) nil)
16269      ;; Gnus 5 .newsrc.eld was loaded.
16270      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16271       (gnus-convert-old-ticks)))))
16272
16273 (defun gnus-convert-old-ticks ()
16274   (let ((newsrc (cdr gnus-newsrc-alist))
16275         marks info dormant ticked)
16276     (while (setq info (pop newsrc))
16277       (when (setq marks (gnus-info-marks info))
16278         (setq dormant (cdr (assq 'dormant marks))
16279               ticked (cdr (assq 'tick marks)))
16280         (when (or dormant ticked)
16281           (gnus-info-set-read
16282            info
16283            (gnus-add-to-range
16284             (gnus-info-read info)
16285             (nconc (gnus-uncompress-range dormant)
16286                    (gnus-uncompress-range ticked)))))))))
16287
16288 (defun gnus-read-newsrc-el-file (file)
16289   (let ((ding-file (concat file "d")))
16290     ;; We always, always read the .eld file.
16291     (gnus-message 5 "Reading %s..." ding-file)
16292     (let (gnus-newsrc-assoc)
16293       (condition-case nil
16294           (load ding-file t t t)
16295         (error
16296          (gnus-error 1 "Error in %s" ding-file)))
16297       (when gnus-newsrc-assoc
16298         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16299     (gnus-make-hashtable-from-newsrc-alist)
16300     (when (file-newer-than-file-p file ding-file)
16301       ;; Old format quick file
16302       (gnus-message 5 "Reading %s..." file)
16303       ;; The .el file is newer than the .eld file, so we read that one
16304       ;; as well.
16305       (gnus-read-old-newsrc-el-file file))))
16306
16307 ;; Parse the old-style quick startup file
16308 (defun gnus-read-old-newsrc-el-file (file)
16309   (let (newsrc killed marked group m info)
16310     (prog1
16311         (let ((gnus-killed-assoc nil)
16312               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16313           (prog1
16314               (condition-case nil
16315                   (load file t t t)
16316                 (error nil))
16317             (setq newsrc gnus-newsrc-assoc
16318                   killed gnus-killed-assoc
16319                   marked gnus-marked-assoc)))
16320       (setq gnus-newsrc-alist nil)
16321       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16322         (if info
16323             (progn
16324               (gnus-info-set-read info (cddr group))
16325               (gnus-info-set-level
16326                info (if (nth 1 group) gnus-level-default-subscribed
16327                       gnus-level-default-unsubscribed))
16328               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16329           (push (setq info
16330                       (list (car group)
16331                             (if (nth 1 group) gnus-level-default-subscribed
16332                               gnus-level-default-unsubscribed)
16333                             (cddr group)))
16334                 gnus-newsrc-alist))
16335         ;; Copy marks into info.
16336         (when (setq m (assoc (car group) marked))
16337           (unless (nthcdr 3 info)
16338             (nconc info (list nil)))
16339           (gnus-info-set-marks
16340            info (list (cons 'tick (gnus-compress-sequence 
16341                                    (sort (cdr m) '<) t))))))
16342       (setq newsrc killed)
16343       (while newsrc
16344         (setcar newsrc (caar newsrc))
16345         (setq newsrc (cdr newsrc)))
16346       (setq gnus-killed-list killed))
16347     ;; The .el file version of this variable does not begin with
16348     ;; "options", while the .eld version does, so we just add it if it
16349     ;; isn't there.
16350     (and
16351      gnus-newsrc-options
16352      (progn
16353        (and (not (string-match "^ *options" gnus-newsrc-options))
16354             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16355        (and (not (string-match "\n$" gnus-newsrc-options))
16356             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16357        ;; Finally, if we read some options lines, we parse them.
16358        (or (string= gnus-newsrc-options "")
16359            (gnus-newsrc-parse-options gnus-newsrc-options))))
16360
16361     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16362     (gnus-make-hashtable-from-newsrc-alist)))
16363
16364 (defun gnus-make-newsrc-file (file)
16365   "Make server dependent file name by catenating FILE and server host name."
16366   (let* ((file (expand-file-name file nil))
16367          (real-file (concat file "-" (nth 1 gnus-select-method))))
16368     (if (or (file-exists-p real-file)
16369             (file-exists-p (concat real-file ".el"))
16370             (file-exists-p (concat real-file ".eld")))
16371         real-file file)))
16372
16373 (defun gnus-newsrc-to-gnus-format ()
16374   (setq gnus-newsrc-options "")
16375   (setq gnus-newsrc-options-n nil)
16376
16377   (or gnus-active-hashtb
16378       (setq gnus-active-hashtb (make-vector 4095 0)))
16379   (let ((buf (current-buffer))
16380         (already-read (> (length gnus-newsrc-alist) 1))
16381         group subscribed options-symbol newsrc Options-symbol
16382         symbol reads num1)
16383     (goto-char (point-min))
16384     ;; We intern the symbol `options' in the active hashtb so that we
16385     ;; can `eq' against it later.
16386     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16387     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16388
16389     (while (not (eobp))
16390       ;; We first read the first word on the line by narrowing and
16391       ;; then reading into `gnus-active-hashtb'.  Most groups will
16392       ;; already exist in that hashtb, so this will save some string
16393       ;; space.
16394       (narrow-to-region
16395        (point)
16396        (progn (skip-chars-forward "^ \t!:\n") (point)))
16397       (goto-char (point-min))
16398       (setq symbol
16399             (and (/= (point-min) (point-max))
16400                  (let ((obarray gnus-active-hashtb)) (read buf))))
16401       (widen)
16402       ;; Now, the symbol we have read is either `options' or a group
16403       ;; name.  If it is an options line, we just add it to a string.
16404       (cond
16405        ((or (eq symbol options-symbol)
16406             (eq symbol Options-symbol))
16407         (setq gnus-newsrc-options
16408               ;; This concating is quite inefficient, but since our
16409               ;; thorough studies show that approx 99.37% of all
16410               ;; .newsrc files only contain a single options line, we
16411               ;; don't give a damn, frankly, my dear.
16412               (concat gnus-newsrc-options
16413                       (buffer-substring
16414                        (gnus-point-at-bol)
16415                        ;; Options may continue on the next line.
16416                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16417                                 (progn (beginning-of-line) (point)))
16418                            (point)))))
16419         (forward-line -1))
16420        (symbol
16421         ;; Group names can be just numbers.  
16422         (when (numberp symbol) 
16423           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16424         (or (boundp symbol) (set symbol nil))
16425         ;; It was a group name.
16426         (setq subscribed (= (following-char) ?:)
16427               group (symbol-name symbol)
16428               reads nil)
16429         (if (eolp)
16430             ;; If the line ends here, this is clearly a buggy line, so
16431             ;; we put point a the beginning of line and let the cond
16432             ;; below do the error handling.
16433             (beginning-of-line)
16434           ;; We skip to the beginning of the ranges.
16435           (skip-chars-forward "!: \t"))
16436         ;; We are now at the beginning of the list of read articles.
16437         ;; We read them range by range.
16438         (while
16439             (cond
16440              ((looking-at "[0-9]+")
16441               ;; We narrow and read a number instead of buffer-substring/
16442               ;; string-to-int because it's faster.  narrow/widen is
16443               ;; faster than save-restriction/narrow, and save-restriction
16444               ;; produces a garbage object.
16445               (setq num1 (progn
16446                            (narrow-to-region (match-beginning 0) (match-end 0))
16447                            (read buf)))
16448               (widen)
16449               ;; If the next character is a dash, then this is a range.
16450               (if (= (following-char) ?-)
16451                   (progn
16452                     ;; We read the upper bound of the range.
16453                     (forward-char 1)
16454                     (if (not (looking-at "[0-9]+"))
16455                         ;; This is a buggy line, by we pretend that
16456                         ;; it's kinda OK.  Perhaps the user should be
16457                         ;; dinged?
16458                         (setq reads (cons num1 reads))
16459                       (setq reads
16460                             (cons
16461                              (cons num1
16462                                    (progn
16463                                      (narrow-to-region (match-beginning 0)
16464                                                        (match-end 0))
16465                                      (read buf)))
16466                              reads))
16467                       (widen)))
16468                 ;; It was just a simple number, so we add it to the
16469                 ;; list of ranges.
16470                 (setq reads (cons num1 reads)))
16471               ;; If the next char in ?\n, then we have reached the end
16472               ;; of the line and return nil.
16473               (/= (following-char) ?\n))
16474              ((= (following-char) ?\n)
16475               ;; End of line, so we end.
16476               nil)
16477              (t
16478               ;; Not numbers and not eol, so this might be a buggy
16479               ;; line...
16480               (or (eobp)
16481                   ;; If it was eob instead of ?\n, we allow it.
16482                   (progn
16483                     ;; The line was buggy.
16484                     (setq group nil)
16485                     (gnus-error 3.1 "Mangled line: %s"
16486                                 (buffer-substring (gnus-point-at-bol)
16487                                                   (gnus-point-at-eol)))))
16488               nil))
16489           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16490           ;; we allow them, because it's a common mistake to put a
16491           ;; space after the comma.
16492           (skip-chars-forward ", "))
16493
16494         ;; We have already read .newsrc.eld, so we gently update the
16495         ;; data in the hash table with the information we have just
16496         ;; read.
16497         (when group
16498           (let ((info (gnus-get-info group))
16499                 level)
16500             (if info
16501                 ;; There is an entry for this file in the alist.
16502                 (progn
16503                   (gnus-info-set-read info (nreverse reads))
16504                   ;; We update the level very gently.  In fact, we
16505                   ;; only change it if there's been a status change
16506                   ;; from subscribed to unsubscribed, or vice versa.
16507                   (setq level (gnus-info-level info))
16508                   (cond ((and (<= level gnus-level-subscribed)
16509                               (not subscribed))
16510                          (setq level (if reads
16511                                          gnus-level-default-unsubscribed
16512                                        (1+ gnus-level-default-unsubscribed))))
16513                         ((and (> level gnus-level-subscribed) subscribed)
16514                          (setq level gnus-level-default-subscribed)))
16515                   (gnus-info-set-level info level))
16516               ;; This is a new group.
16517               (setq info (list group
16518                                (if subscribed
16519                                    gnus-level-default-subscribed
16520                                  (if reads
16521                                      (1+ gnus-level-subscribed)
16522                                    gnus-level-default-unsubscribed))
16523                                (nreverse reads))))
16524             (setq newsrc (cons info newsrc))))))
16525       (forward-line 1))
16526
16527     (setq newsrc (nreverse newsrc))
16528
16529     (if (not already-read)
16530         ()
16531       ;; We now have two newsrc lists - `newsrc', which is what we
16532       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16533       ;; what we've read from .newsrc.eld.  We have to merge these
16534       ;; lists.  We do this by "attaching" any (foreign) groups in the
16535       ;; gnus-newsrc-alist to the (native) group that precedes them.
16536       (let ((rc (cdr gnus-newsrc-alist))
16537             (prev gnus-newsrc-alist)
16538             entry mentry)
16539         (while rc
16540           (or (null (nth 4 (car rc)))   ; It's a native group.
16541               (assoc (caar rc) newsrc) ; It's already in the alist.
16542               (if (setq entry (assoc (caar prev) newsrc))
16543                   (setcdr (setq mentry (memq entry newsrc))
16544                           (cons (car rc) (cdr mentry)))
16545                 (setq newsrc (cons (car rc) newsrc))))
16546           (setq prev rc
16547                 rc (cdr rc)))))
16548
16549     (setq gnus-newsrc-alist newsrc)
16550     ;; We make the newsrc hashtb.
16551     (gnus-make-hashtable-from-newsrc-alist)
16552
16553     ;; Finally, if we read some options lines, we parse them.
16554     (or (string= gnus-newsrc-options "")
16555         (gnus-newsrc-parse-options gnus-newsrc-options))))
16556
16557 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16558 ;; The return value will be a list on the form
16559 ;; ((regexp1 . ignore)
16560 ;;  (regexp2 . subscribe)...)
16561 ;; When handling new newsgroups, groups that match a `ignore' regexp
16562 ;; will be ignored, and groups that match a `subscribe' regexp will be
16563 ;; subscribed.  A line like
16564 ;; options -n !all rec.all
16565 ;; will lead to a list that looks like
16566 ;; (("^rec\\..+" . subscribe)
16567 ;;  ("^.+" . ignore))
16568 ;; So all "rec.*" groups will be subscribed, while all the other
16569 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16570 ;; different from "options -n rec.all !all".
16571 (defun gnus-newsrc-parse-options (options)
16572   (let (out eol)
16573     (save-excursion
16574       (gnus-set-work-buffer)
16575       (insert (regexp-quote options))
16576       ;; First we treat all continuation lines.
16577       (goto-char (point-min))
16578       (while (re-search-forward "\n[ \t]+" nil t)
16579         (replace-match " " t t))
16580       ;; Then we transform all "all"s into ".+"s.
16581       (goto-char (point-min))
16582       (while (re-search-forward "\\ball\\b" nil t)
16583         (replace-match ".+" t t))
16584       (goto-char (point-min))
16585       ;; We remove all other options than the "-n" ones.
16586       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16587         (replace-match " ")
16588         (forward-char -1))
16589       (goto-char (point-min))
16590
16591       ;; We are only interested in "options -n" lines - we
16592       ;; ignore the other option lines.
16593       (while (re-search-forward "[ \t]-n" nil t)
16594         (setq eol
16595               (or (save-excursion
16596                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16597                          (- (point) 2)))
16598                   (gnus-point-at-eol)))
16599         ;; Search for all "words"...
16600         (while (re-search-forward "[^ \t,\n]+" eol t)
16601           (if (= (char-after (match-beginning 0)) ?!)
16602               ;; If the word begins with a bang (!), this is a "not"
16603               ;; spec.  We put this spec (minus the bang) and the
16604               ;; symbol `ignore' into the list.
16605               (setq out (cons (cons (concat
16606                                      "^" (buffer-substring
16607                                           (1+ (match-beginning 0))
16608                                           (match-end 0)))
16609                                     'ignore) out))
16610             ;; There was no bang, so this is a "yes" spec.
16611             (setq out (cons (cons (concat "^" (match-string 0))
16612                                   'subscribe) out)))))
16613
16614       (setq gnus-newsrc-options-n out))))
16615
16616 (defun gnus-save-newsrc-file (&optional force)
16617   "Save .newsrc file."
16618   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16619   ;; from the variable gnus-newsrc-alist.
16620   (when (and (or gnus-newsrc-alist gnus-killed-list)
16621              gnus-current-startup-file)
16622     (save-excursion
16623       (if (and (or gnus-use-dribble-file gnus-slave)
16624                (not force)
16625                (or (not gnus-dribble-buffer)
16626                    (not (buffer-name gnus-dribble-buffer))
16627                    (zerop (save-excursion
16628                             (set-buffer gnus-dribble-buffer)
16629                             (buffer-size)))))
16630           (gnus-message 4 "(No changes need to be saved)")
16631         (run-hooks 'gnus-save-newsrc-hook)
16632         (if gnus-slave
16633             (gnus-slave-save-newsrc)
16634           ;; Save .newsrc.
16635           (when gnus-save-newsrc-file
16636             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16637             (gnus-gnus-to-newsrc-format)
16638             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16639           ;; Save .newsrc.eld.
16640           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16641           (make-local-variable 'version-control)
16642           (setq version-control 'never)
16643           (setq buffer-file-name
16644                 (concat gnus-current-startup-file ".eld"))
16645           (gnus-add-current-to-buffer-list)
16646           (buffer-disable-undo (current-buffer))
16647           (erase-buffer)
16648           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16649           (gnus-gnus-to-quick-newsrc-format)
16650           (run-hooks 'gnus-save-quick-newsrc-hook)
16651           (save-buffer)
16652           (kill-buffer (current-buffer))
16653           (gnus-message
16654            5 "Saving %s.eld...done" gnus-current-startup-file))
16655         (gnus-dribble-delete-file)
16656         (gnus-group-set-mode-line)))))
16657
16658 (defun gnus-gnus-to-quick-newsrc-format ()
16659   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16660   (insert ";; Gnus startup file.\n")
16661   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16662   (insert ";; to read .newsrc.\n")
16663   (insert "(setq gnus-newsrc-file-version "
16664           (prin1-to-string gnus-version) ")\n")
16665   (let ((variables
16666          (if gnus-save-killed-list gnus-variable-list
16667            ;; Remove the `gnus-killed-list' from the list of variables
16668            ;; to be saved, if required.
16669            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16670         ;; Peel off the "dummy" group.
16671         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16672         variable)
16673     ;; Insert the variables into the file.
16674     (while variables
16675       (when (and (boundp (setq variable (pop variables)))
16676                  (symbol-value variable))
16677         (insert "(setq " (symbol-name variable) " '")
16678         (prin1 (symbol-value variable) (current-buffer))
16679         (insert ")\n")))))
16680
16681 (defun gnus-gnus-to-newsrc-format ()
16682   ;; Generate and save the .newsrc file.
16683   (save-excursion
16684     (set-buffer (create-file-buffer gnus-current-startup-file))
16685     (let ((newsrc (cdr gnus-newsrc-alist))
16686           (standard-output (current-buffer))
16687           info ranges range method)
16688       (setq buffer-file-name gnus-current-startup-file)
16689       (buffer-disable-undo (current-buffer))
16690       (erase-buffer)
16691       ;; Write options.
16692       (if gnus-newsrc-options (insert gnus-newsrc-options))
16693       ;; Write subscribed and unsubscribed.
16694       (while (setq info (pop newsrc))
16695         ;; Don't write foreign groups to .newsrc.
16696         (when (or (null (setq method (gnus-info-method info)))
16697                   (equal method "native")
16698                   (gnus-server-equal method gnus-select-method))
16699           (insert (gnus-info-group info)
16700                   (if (> (gnus-info-level info) gnus-level-subscribed)
16701                       "!" ":"))
16702           (when (setq ranges (gnus-info-read info))
16703             (insert " ")
16704             (if (not (listp (cdr ranges)))
16705                 (if (= (car ranges) (cdr ranges))
16706                     (princ (car ranges))
16707                   (princ (car ranges))
16708                   (insert "-")
16709                   (princ (cdr ranges)))
16710               (while (setq range (pop ranges))
16711                 (if (or (atom range) (= (car range) (cdr range)))
16712                     (princ (or (and (atom range) range) (car range)))
16713                   (princ (car range))
16714                   (insert "-")
16715                   (princ (cdr range)))
16716                 (if ranges (insert ",")))))
16717           (insert "\n")))
16718       (make-local-variable 'version-control)
16719       (setq version-control 'never)
16720       ;; It has been reported that sometime the modtime on the .newsrc
16721       ;; file seems to be off.  We really do want to overwrite it, so
16722       ;; we clear the modtime here before saving.  It's a bit odd,
16723       ;; though...
16724       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16725       ;; delete the silly thing entirely first.  but this fails to provide
16726       ;; such niceties as .newsrc~ creation.
16727       (if gnus-modtime-botch
16728           (delete-file gnus-startup-file)
16729         (clear-visited-file-modtime))
16730       (run-hooks 'gnus-save-standard-newsrc-hook)
16731       (save-buffer)
16732       (kill-buffer (current-buffer)))))
16733
16734 \f
16735 ;;;
16736 ;;; Slave functions.
16737 ;;;
16738
16739 (defun gnus-slave-save-newsrc ()
16740   (save-excursion
16741     (set-buffer gnus-dribble-buffer)
16742     (let ((slave-name
16743            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16744       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16745
16746 (defun gnus-master-read-slave-newsrc ()
16747   (let ((slave-files
16748          (directory-files
16749           (file-name-directory gnus-current-startup-file)
16750           t (concat
16751              "^" (regexp-quote
16752                   (concat
16753                    (file-name-nondirectory gnus-current-startup-file)
16754                    "-slave-")))
16755           t))
16756         file)
16757     (if (not slave-files)
16758         ()                              ; There are no slave files to read.
16759       (gnus-message 7 "Reading slave newsrcs...")
16760       (save-excursion
16761         (set-buffer (get-buffer-create " *gnus slave*"))
16762         (buffer-disable-undo (current-buffer))
16763         (setq slave-files
16764               (sort (mapcar (lambda (file)
16765                               (list (nth 5 (file-attributes file)) file))
16766                             slave-files)
16767                     (lambda (f1 f2)
16768                       (or (< (caar f1) (caar f2))
16769                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16770         (while slave-files
16771           (erase-buffer)
16772           (setq file (nth 1 (car slave-files)))
16773           (insert-file-contents file)
16774           (if (condition-case ()
16775                   (progn
16776                     (eval-buffer (current-buffer))
16777                     t)
16778                 (error
16779                  (gnus-error 3.2 "Possible error in %s" file)
16780                  nil))
16781               (or gnus-slave ; Slaves shouldn't delete these files.
16782                   (condition-case ()
16783                       (delete-file file)
16784                     (error nil))))
16785           (setq slave-files (cdr slave-files))))
16786       (gnus-message 7 "Reading slave newsrcs...done"))))
16787
16788 \f
16789 ;;;
16790 ;;; Group description.
16791 ;;;
16792
16793 (defun gnus-read-all-descriptions-files ()
16794   (let ((methods (cons gnus-select-method 
16795                        (nconc
16796                         (when gnus-message-archive-method
16797                           (list "archive"))
16798                         gnus-secondary-select-methods))))
16799     (while methods
16800       (gnus-read-descriptions-file (car methods))
16801       (setq methods (cdr methods)))
16802     t))
16803
16804 (defun gnus-read-descriptions-file (&optional method)
16805   (let ((method (or method gnus-select-method)))
16806     (when (stringp method)
16807       (setq method (gnus-server-to-method method)))
16808     ;; We create the hashtable whether we manage to read the desc file
16809     ;; to avoid trying to re-read after a failed read.
16810     (or gnus-description-hashtb
16811         (setq gnus-description-hashtb
16812               (gnus-make-hashtable (length gnus-active-hashtb))))
16813     ;; Mark this method's desc file as read.
16814     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16815                   gnus-description-hashtb)
16816
16817     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16818     (cond
16819      ((not (gnus-check-server method))
16820       (gnus-message 1 "Couldn't open server")
16821       nil)
16822      ((not (gnus-request-list-newsgroups method))
16823       (gnus-message 1 "Couldn't read newsgroups descriptions")
16824       nil)
16825      (t
16826       (let (group)
16827         (save-excursion
16828           (save-restriction
16829             (set-buffer nntp-server-buffer)
16830             (goto-char (point-min))
16831             (if (or (search-forward "\n.\n" nil t)
16832                     (goto-char (point-max)))
16833                 (progn
16834                   (beginning-of-line)
16835                   (narrow-to-region (point-min) (point))))
16836             (goto-char (point-min))
16837             (while (not (eobp))
16838               ;; If we get an error, we set group to 0, which is not a
16839               ;; symbol...
16840               (setq group
16841                     (condition-case ()
16842                         (let ((obarray gnus-description-hashtb))
16843                           ;; Group is set to a symbol interned in this
16844                           ;; hash table.
16845                           (read nntp-server-buffer))
16846                       (error 0)))
16847               (skip-chars-forward " \t")
16848               ;; ...  which leads to this line being effectively ignored.
16849               (and (symbolp group)
16850                    (set group (buffer-substring
16851                                (point) (progn (end-of-line) (point)))))
16852               (forward-line 1))))
16853         (gnus-message 5 "Reading descriptions file...done")
16854         t)))))
16855
16856 (defun gnus-group-get-description (group)
16857   "Get the description of a group by sending XGTITLE to the server."
16858   (when (gnus-request-group-description group)
16859     (save-excursion
16860       (set-buffer nntp-server-buffer)
16861       (goto-char (point-min))
16862       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16863         (match-string 1)))))
16864
16865 \f
16866 ;;;
16867 ;;; Buffering of read articles.
16868 ;;;
16869
16870 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16871 (defvar gnus-backlog-articles nil)
16872 (defvar gnus-backlog-hashtb nil)
16873
16874 (defun gnus-backlog-buffer ()
16875   "Return the backlog buffer."
16876   (or (get-buffer gnus-backlog-buffer)
16877       (save-excursion
16878         (set-buffer (get-buffer-create gnus-backlog-buffer))
16879         (buffer-disable-undo (current-buffer))
16880         (setq buffer-read-only t)
16881         (gnus-add-current-to-buffer-list)
16882         (get-buffer gnus-backlog-buffer))))
16883
16884 (defun gnus-backlog-setup ()
16885   "Initialize backlog variables."
16886   (unless gnus-backlog-hashtb
16887     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16888
16889 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16890
16891 (defun gnus-backlog-shutdown ()
16892   "Clear all backlog variables and buffers."
16893   (when (get-buffer gnus-backlog-buffer)
16894     (kill-buffer gnus-backlog-buffer))
16895   (setq gnus-backlog-hashtb nil
16896         gnus-backlog-articles nil))
16897
16898 (defun gnus-backlog-enter-article (group number buffer)
16899   (gnus-backlog-setup)
16900   (let ((ident (intern (concat group ":" (int-to-string number))
16901                        gnus-backlog-hashtb))
16902         b)
16903     (if (memq ident gnus-backlog-articles)
16904         () ; It's already kept.
16905       ;; Remove the oldest article, if necessary.
16906       (and (numberp gnus-keep-backlog)
16907            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16908            (gnus-backlog-remove-oldest-article))
16909       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16910       ;; Insert the new article.
16911       (save-excursion
16912         (set-buffer (gnus-backlog-buffer))
16913         (let (buffer-read-only)
16914           (goto-char (point-max))
16915           (or (bolp) (insert "\n"))
16916           (setq b (point))
16917           (insert-buffer-substring buffer)
16918           ;; Tag the beginning of the article with the ident.
16919           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
16920
16921 (defun gnus-backlog-remove-oldest-article ()
16922   (save-excursion
16923     (set-buffer (gnus-backlog-buffer))
16924     (goto-char (point-min))
16925     (if (zerop (buffer-size))
16926         () ; The buffer is empty.
16927       (let ((ident (get-text-property (point) 'gnus-backlog))
16928             buffer-read-only)
16929         ;; Remove the ident from the list of articles.
16930         (when ident
16931           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16932         ;; Delete the article itself.
16933         (delete-region
16934          (point) (next-single-property-change
16935                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16936
16937 (defun gnus-backlog-remove-article (group number)
16938   "Remove article NUMBER in GROUP from the backlog."
16939   (when (numberp number)
16940     (gnus-backlog-setup)
16941     (let ((ident (intern (concat group ":" (int-to-string number))
16942                          gnus-backlog-hashtb))
16943           beg end)
16944       (when (memq ident gnus-backlog-articles)
16945         ;; It was in the backlog.
16946         (save-excursion
16947           (set-buffer (gnus-backlog-buffer))
16948           (let (buffer-read-only)
16949             (when (setq beg (text-property-any
16950                              (point-min) (point-max) 'gnus-backlog
16951                              ident))
16952               ;; Find the end (i. e., the beginning of the next article).
16953               (setq end
16954                     (next-single-property-change
16955                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
16956               (delete-region beg end)
16957               ;; Return success.
16958               t)))))))
16959
16960 (defun gnus-backlog-request-article (group number buffer)
16961   (when (numberp number)
16962     (gnus-backlog-setup)
16963     (let ((ident (intern (concat group ":" (int-to-string number))
16964                          gnus-backlog-hashtb))
16965           beg end)
16966       (when (memq ident gnus-backlog-articles)
16967         ;; It was in the backlog.
16968         (save-excursion
16969           (set-buffer (gnus-backlog-buffer))
16970           (if (not (setq beg (text-property-any
16971                               (point-min) (point-max) 'gnus-backlog
16972                               ident)))
16973               ;; It wasn't in the backlog after all.
16974               (ignore
16975                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16976             ;; Find the end (i. e., the beginning of the next article).
16977             (setq end
16978                   (next-single-property-change
16979                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16980         (let ((buffer-read-only nil))
16981           (erase-buffer)
16982           (insert-buffer-substring gnus-backlog-buffer beg end)
16983           t)))))
16984
16985 ;; Allow redefinition of Gnus functions.
16986
16987 (gnus-ems-redefine)
16988
16989 (provide 'gnus)
16990
16991 ;;; gnus.el ends here