*** 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.93"
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   (make-local-hook 'post-command-hook)
4267   (add-hook 'post-command-hook 'gnus-clear-inboxes-moved)
4268   (run-hooks 'gnus-group-mode-hook))
4269
4270 (defun gnus-clear-inboxes-moved ()
4271   (setq nnmail-moved-inboxes nil))
4272
4273 (defun gnus-mouse-pick-group (e)
4274   "Enter the group under the mouse pointer."
4275   (interactive "e")
4276   (mouse-set-point e)
4277   (gnus-group-read-group nil))
4278
4279 ;; Look at LEVEL and find out what the level is really supposed to be.
4280 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4281 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4282 (defun gnus-group-default-level (&optional level number-or-nil)
4283   (cond
4284    (gnus-group-use-permanent-levels
4285     (or (setq gnus-group-use-permanent-levels
4286               (or level (if (numberp gnus-group-use-permanent-levels)
4287                             gnus-group-use-permanent-levels
4288                           (or gnus-group-default-list-level
4289                               gnus-level-subscribed))))
4290         gnus-group-default-list-level gnus-level-subscribed))
4291    (number-or-nil
4292     level)
4293    (t
4294     (or level gnus-group-default-list-level gnus-level-subscribed))))
4295
4296 ;;;###autoload
4297 (defun gnus-slave-no-server (&optional arg)
4298   "Read network news as a slave, without connecting to local server"
4299   (interactive "P")
4300   (gnus-no-server arg t))
4301
4302 ;;;###autoload
4303 (defun gnus-no-server (&optional arg slave)
4304   "Read network news.
4305 If ARG is a positive number, Gnus will use that as the
4306 startup level.  If ARG is nil, Gnus will be started at level 2.
4307 If ARG is non-nil and not a positive number, Gnus will
4308 prompt the user for the name of an NNTP server to use.
4309 As opposed to `gnus', this command will not connect to the local server."
4310   (interactive "P")
4311   (let ((val (or arg (1- gnus-level-default-subscribed))))
4312     (gnus val t slave)
4313     (make-local-variable 'gnus-group-use-permanent-levels)
4314     (setq gnus-group-use-permanent-levels val)))
4315
4316 ;;;###autoload
4317 (defun gnus-slave (&optional arg)
4318   "Read news as a slave."
4319   (interactive "P")
4320   (gnus arg nil 'slave))
4321
4322 ;;;###autoload
4323 (defun gnus-other-frame (&optional arg)
4324   "Pop up a frame to read news."
4325   (interactive "P")
4326   (if (get-buffer gnus-group-buffer)
4327       (let ((pop-up-frames t))
4328         (gnus arg))
4329     (select-frame (make-frame))
4330     (gnus arg)))
4331
4332 ;;;###autoload
4333 (defun gnus (&optional arg dont-connect slave)
4334   "Read network news.
4335 If ARG is non-nil and a positive number, Gnus will use that as the
4336 startup level.  If ARG is non-nil and not a positive number, Gnus will
4337 prompt the user for the name of an NNTP server to use."
4338   (interactive "P")
4339
4340   (if (get-buffer gnus-group-buffer)
4341       (progn
4342         (switch-to-buffer gnus-group-buffer)
4343         (gnus-group-get-new-news))
4344
4345     (gnus-clear-system)
4346     (nnheader-init-server-buffer)
4347     (gnus-read-init-file)
4348     (setq gnus-slave slave)
4349
4350     (gnus-group-setup-buffer)
4351     (let ((buffer-read-only nil))
4352       (erase-buffer)
4353       (if (not gnus-inhibit-startup-message)
4354           (progn
4355             (gnus-group-startup-message)
4356             (sit-for 0))))
4357
4358     (let ((level (and (numberp arg) (> arg 0) arg))
4359           did-connect)
4360       (unwind-protect
4361           (progn
4362             (or dont-connect
4363                 (setq did-connect
4364                       (gnus-start-news-server (and arg (not level))))))
4365         (if (and (not dont-connect)
4366                  (not did-connect))
4367             (gnus-group-quit)
4368           (run-hooks 'gnus-startup-hook)
4369           ;; NNTP server is successfully open.
4370
4371           ;; Find the current startup file name.
4372           (setq gnus-current-startup-file
4373                 (gnus-make-newsrc-file gnus-startup-file))
4374
4375           ;; Read the dribble file.
4376           (when (or gnus-slave gnus-use-dribble-file)
4377             (gnus-dribble-read-file))
4378
4379           ;; Allow using GroupLens predictions.
4380           (when gnus-use-grouplens
4381             (bbb-login)
4382             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4383
4384           (gnus-summary-make-display-table)
4385           ;; Do the actual startup.
4386           (gnus-setup-news nil level dont-connect)
4387           ;; Generate the group buffer.
4388           (gnus-group-list-groups level)
4389           (gnus-group-first-unread-group)
4390           (gnus-configure-windows 'group)
4391           (gnus-group-set-mode-line))))))
4392
4393 (defun gnus-unload ()
4394   "Unload all Gnus features."
4395   (interactive)
4396   (or (boundp 'load-history)
4397       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4398   (let ((history load-history)
4399         feature)
4400     (while history
4401       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4402            (setq feature (cdr (assq 'provide (car history))))
4403            (unload-feature feature 'force))
4404       (setq history (cdr history)))))
4405
4406 (defun gnus-compile ()
4407   "Byte-compile the user-defined format specs."
4408   (interactive)
4409   (let ((entries gnus-format-specs)
4410         entry gnus-tmp-func)
4411     (save-excursion
4412       (gnus-message 7 "Compiling format specs...")
4413
4414       (while entries
4415         (setq entry (pop entries))
4416         (if (eq (car entry) 'version)
4417             (setq gnus-format-specs (delq entry gnus-format-specs))
4418           (when (and (listp (caddr entry))
4419                      (not (eq 'byte-code (caaddr entry))))
4420             (fset 'gnus-tmp-func
4421                   `(lambda () ,(caddr entry)))
4422             (byte-compile 'gnus-tmp-func)
4423             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4424
4425       (push (cons 'version emacs-version) gnus-format-specs)
4426
4427       (gnus-message 7 "Compiling user specs...done"))))
4428
4429 (defun gnus-indent-rigidly (start end arg)
4430   "Indent rigidly using only spaces and no tabs."
4431   (save-excursion
4432     (save-restriction
4433       (narrow-to-region start end)
4434       (indent-rigidly start end arg)
4435       (goto-char (point-min))
4436       (while (search-forward "\t" nil t)
4437         (replace-match "        " t t)))))
4438
4439 (defun gnus-group-startup-message (&optional x y)
4440   "Insert startup message in current buffer."
4441   ;; Insert the message.
4442   (erase-buffer)
4443   (insert
4444    (format "              %s
4445           _    ___ _             _
4446           _ ___ __ ___  __    _ ___
4447           __   _     ___    __  ___
4448               _           ___     _
4449              _  _ __             _
4450              ___   __            _
4451                    __           _
4452                     _      _   _
4453                    _      _    _
4454                       _  _    _
4455                   __  ___
4456                  _   _ _     _
4457                 _   _
4458               _    _
4459              _    _
4460             _
4461           __
4462
4463 "
4464            ""))
4465   ;; And then hack it.
4466   (gnus-indent-rigidly (point-min) (point-max)
4467                        (/ (max (- (window-width) (or x 46)) 0) 2))
4468   (goto-char (point-min))
4469   (forward-line 1)
4470   (let* ((pheight (count-lines (point-min) (point-max)))
4471          (wheight (window-height))
4472          (rest (- wheight pheight)))
4473     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4474   ;; Fontify some.
4475   (goto-char (point-min))
4476   (and (search-forward "Praxis" nil t)
4477        (gnus-put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4478   (goto-char (point-min))
4479   (let* ((mode-string (gnus-group-set-mode-line)))
4480     (setq mode-line-buffer-identification
4481           (list (concat gnus-version (substring (car mode-string) 4))))
4482     (set-buffer-modified-p t)))
4483
4484 (defun gnus-group-setup-buffer ()
4485   (or (get-buffer gnus-group-buffer)
4486       (progn
4487         (switch-to-buffer gnus-group-buffer)
4488         (gnus-add-current-to-buffer-list)
4489         (gnus-group-mode)
4490         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4491
4492 (defun gnus-group-list-groups (&optional level unread lowest)
4493   "List newsgroups with level LEVEL or lower that have unread articles.
4494 Default is all subscribed groups.
4495 If argument UNREAD is non-nil, groups with no unread articles are also
4496 listed."
4497   (interactive (list (if current-prefix-arg
4498                          (prefix-numeric-value current-prefix-arg)
4499                        (or
4500                         (gnus-group-default-level nil t)
4501                         gnus-group-default-list-level
4502                         gnus-level-subscribed))))
4503   (or level
4504       (setq level (car gnus-group-list-mode)
4505             unread (cdr gnus-group-list-mode)))
4506   (setq level (gnus-group-default-level level))
4507   (gnus-group-setup-buffer)             ;May call from out of group buffer
4508   (gnus-update-format-specifications)
4509   (let ((case-fold-search nil)
4510         (props (text-properties-at (gnus-point-at-bol)))
4511         (group (gnus-group-group-name)))
4512     (set-buffer gnus-group-buffer)
4513     (funcall gnus-group-prepare-function level unread lowest)
4514     (if (zerop (buffer-size))
4515         (gnus-message 5 gnus-no-groups-message)
4516       (goto-char (point-max))
4517       (when (or (not gnus-group-goto-next-group-function)
4518                 (not (funcall gnus-group-goto-next-group-function 
4519                               group props)))
4520         (if (not group)
4521             ;; Go to the first group with unread articles.
4522             (gnus-group-search-forward t)
4523           ;; Find the right group to put point on.  If the current group
4524           ;; has disappeared in the new listing, try to find the next
4525           ;; one.        If no next one can be found, just leave point at the
4526           ;; first newsgroup in the buffer.
4527           (if (not (gnus-goto-char
4528                     (text-property-any
4529                      (point-min) (point-max)
4530                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4531               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4532                 (while (and newsrc
4533                             (not (gnus-goto-char
4534                                   (text-property-any
4535                                    (point-min) (point-max) 'gnus-group
4536                                    (gnus-intern-safe
4537                                     (caar newsrc) gnus-active-hashtb)))))
4538                   (setq newsrc (cdr newsrc)))
4539                 (or newsrc (progn (goto-char (point-max))
4540                                   (forward-line -1)))))))
4541       ;; Adjust cursor point.
4542       (gnus-group-position-point))))
4543
4544 (defun gnus-group-list-level (level &optional all)
4545   "List groups on LEVEL.
4546 If ALL (the prefix), also list groups that have no unread articles."
4547   (interactive "nList groups on level: \nP")
4548   (gnus-group-list-groups level all level))
4549
4550 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4551   "List all newsgroups with unread articles of level LEVEL or lower.
4552 If ALL is non-nil, list groups that have no unread articles.
4553 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4554 If REGEXP, only list groups matching REGEXP."
4555   (set-buffer gnus-group-buffer)
4556   (let ((buffer-read-only nil)
4557         (newsrc (cdr gnus-newsrc-alist))
4558         (lowest (or lowest 1))
4559         info clevel unread group params)
4560     (erase-buffer)
4561     (if (< lowest gnus-level-zombie)
4562         ;; List living groups.
4563         (while newsrc
4564           (setq info (car newsrc)
4565                 group (gnus-info-group info)
4566                 params (gnus-info-params info)
4567                 newsrc (cdr newsrc)
4568                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4569           (and unread                   ; This group might be bogus
4570                (or (not regexp)
4571                    (string-match regexp group))
4572                (<= (setq clevel (gnus-info-level info)) level)
4573                (>= clevel lowest)
4574                (or all                  ; We list all groups?
4575                    (if (eq unread t)    ; Unactivated?
4576                        gnus-group-list-inactive-groups ; We list unactivated 
4577                      (> unread 0))      ; We list groups with unread articles
4578                    (and gnus-list-groups-with-ticked-articles
4579                         (cdr (assq 'tick (gnus-info-marks info))))
4580                                         ; And groups with tickeds
4581                    ;; Check for permanent visibility.
4582                    (and gnus-permanently-visible-groups
4583                         (string-match gnus-permanently-visible-groups
4584                                       group))
4585                    (memq 'visible params)
4586                    (cdr (assq 'visible params)))
4587                (gnus-group-insert-group-line
4588                 group (gnus-info-level info)
4589                 (gnus-info-marks info) unread (gnus-info-method info)))))
4590
4591     ;; List dead groups.
4592     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4593          (gnus-group-prepare-flat-list-dead
4594           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4595           gnus-level-zombie ?Z
4596           regexp))
4597     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4598          (gnus-group-prepare-flat-list-dead
4599           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4600           gnus-level-killed ?K regexp))
4601
4602     (gnus-group-set-mode-line)
4603     (setq gnus-group-list-mode (cons level all))
4604     (run-hooks 'gnus-group-prepare-hook)))
4605
4606 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4607   ;; List zombies and killed lists somewhat faster, which was
4608   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4609   ;; this by ignoring the group format specification altogether.
4610   (let (group)
4611     (if regexp
4612         ;; This loop is used when listing groups that match some
4613         ;; regexp.
4614         (while groups
4615           (setq group (pop groups))
4616           (when (string-match regexp group)
4617             (gnus-add-text-properties
4618              (point) (prog1 (1+ (point))
4619                        (insert " " mark "     *: " group "\n"))
4620              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4621                    'gnus-unread t
4622                    'gnus-level level))))
4623       ;; This loop is used when listing all groups.
4624       (while groups
4625         (gnus-add-text-properties
4626          (point) (prog1 (1+ (point))
4627                    (insert " " mark "     *: "
4628                            (setq group (pop groups)) "\n"))
4629          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4630                'gnus-unread t
4631                'gnus-level level))))))
4632
4633 (defmacro gnus-group-real-name (group)
4634   "Find the real name of a foreign newsgroup."
4635   `(let ((gname ,group))
4636      (if (string-match ":[^:]+$" gname)
4637          (substring gname (1+ (match-beginning 0)))
4638        gname)))
4639
4640 (defsubst gnus-server-add-address (method)
4641   (let ((method-name (symbol-name (car method))))
4642     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4643              (not (assq (intern (concat method-name "-address")) method)))
4644         (append method (list (list (intern (concat method-name "-address"))
4645                                    (nth 1 method))))
4646       method)))
4647
4648 (defsubst gnus-server-get-method (group method)
4649   ;; Input either a server name, and extended server name, or a
4650   ;; select method, and return a select method.
4651   (cond ((stringp method)
4652          (gnus-server-to-method method))
4653         ((equal method gnus-select-method)
4654          gnus-select-method)
4655         ((and (stringp (car method)) group)
4656          (gnus-server-extend-method group method))
4657         ((and method (not group)
4658               (equal (cadr method) ""))
4659          method)
4660         (t
4661          (gnus-server-add-address method))))
4662
4663 (defun gnus-server-to-method (server)
4664   "Map virtual server names to select methods."
4665   (or 
4666    ;; Is this a method, perhaps?
4667    (and server (listp server) server)
4668    ;; Perhaps this is the native server?
4669    (and (equal server "native") gnus-select-method)
4670    ;; It should be in the server alist.
4671    (cdr (assoc server gnus-server-alist))
4672    ;; If not, we look through all the opened server
4673    ;; to see whether we can find it there.
4674    (let ((opened gnus-opened-servers))
4675      (while (and opened
4676                  (not (equal server (format "%s:%s" (caaar opened)
4677                                             (cadaar opened)))))
4678        (pop opened))
4679      (caar opened))))
4680
4681 (defmacro gnus-method-equal (ss1 ss2)
4682   "Say whether two servers are equal."
4683   `(let ((s1 ,ss1)
4684          (s2 ,ss2))
4685      (or (equal s1 s2)
4686          (and (= (length s1) (length s2))
4687               (progn
4688                 (while (and s1 (member (car s1) s2))
4689                   (setq s1 (cdr s1)))
4690                 (null s1))))))
4691
4692 (defun gnus-server-equal (m1 m2)
4693   "Say whether two methods are equal."
4694   (let ((m1 (cond ((null m1) gnus-select-method)
4695                   ((stringp m1) (gnus-server-to-method m1))
4696                   (t m1)))
4697         (m2 (cond ((null m2) gnus-select-method)
4698                   ((stringp m2) (gnus-server-to-method m2))
4699                   (t m2))))
4700     (gnus-method-equal m1 m2)))
4701
4702 (defun gnus-servers-using-backend (backend)
4703   "Return a list of known servers using BACKEND."
4704   (let ((opened gnus-opened-servers)
4705         out)
4706     (while opened
4707       (when (eq backend (caaar opened))
4708         (push (caar opened) out))
4709       (pop opened))
4710     out))
4711
4712 (defun gnus-group-prefixed-name (group method)
4713   "Return the whole name from GROUP and METHOD."
4714   (and (stringp method) (setq method (gnus-server-to-method method)))
4715   (concat (format "%s" (car method))
4716           (if (and
4717                (or (assoc (format "%s" (car method)) 
4718                           (gnus-methods-using 'address))
4719                    (gnus-server-equal method gnus-message-archive-method))
4720                (nth 1 method)
4721                (not (string= (nth 1 method) "")))
4722               (concat "+" (nth 1 method)))
4723           ":" group))
4724
4725 (defun gnus-group-real-prefix (group)
4726   "Return the prefix of the current group name."
4727   (if (string-match "^[^:]+:" group)
4728       (substring group 0 (match-end 0))
4729     ""))
4730
4731 (defun gnus-group-method (group)
4732   "Return the server or method used for selecting GROUP."
4733   (let ((prefix (gnus-group-real-prefix group)))
4734     (if (equal prefix "")
4735         gnus-select-method
4736       (let ((servers gnus-opened-servers)
4737             (server "")
4738             backend possible found)
4739         (if (string-match "^[^\\+]+\\+" prefix)
4740             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4741                   server (substring prefix (match-end 0) (1- (length prefix))))
4742           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4743         (while servers
4744           (when (eq (caaar servers) backend)
4745             (setq possible (caar servers))
4746             (when (equal (cadaar servers) server)
4747               (setq found (caar servers))))
4748           (pop servers))
4749         (or (car (rassoc found gnus-server-alist))
4750             found
4751             (car (rassoc possible gnus-server-alist))
4752             possible
4753             (list backend server))))))
4754
4755 (defsubst gnus-secondary-method-p (method)
4756   "Return whether METHOD is a secondary select method."
4757   (let ((methods gnus-secondary-select-methods)
4758         (gmethod (gnus-server-get-method nil method)))
4759     (while (and methods
4760                 (not (equal (gnus-server-get-method nil (car methods))
4761                             gmethod)))
4762       (setq methods (cdr methods)))
4763     methods))
4764
4765 (defun gnus-group-foreign-p (group)
4766   "Say whether a group is foreign or not."
4767   (and (not (gnus-group-native-p group))
4768        (not (gnus-group-secondary-p group))))
4769
4770 (defun gnus-group-native-p (group)
4771   "Say whether the group is native or not."
4772   (not (string-match ":" group)))
4773
4774 (defun gnus-group-secondary-p (group)
4775   "Say whether the group is secondary or not."
4776   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4777
4778 (defun gnus-group-get-parameter (group &optional symbol)
4779   "Returns the group parameters for GROUP.
4780 If SYMBOL, return the value of that symbol in the group parameters."
4781   (let ((params (gnus-info-params (gnus-get-info group))))
4782     (if symbol
4783         (gnus-group-parameter-value params symbol)
4784       params)))
4785
4786 (defun gnus-group-parameter-value (params symbol)
4787   "Return the value of SYMBOL in group PARAMS."
4788   (or (car (memq symbol params))        ; It's either a simple symbol
4789       (cdr (assq symbol params))))      ; or a cons.
4790
4791 (defun gnus-group-add-parameter (group param)
4792   "Add parameter PARAM to GROUP."
4793   (let ((info (gnus-get-info group)))
4794     (if (not info)
4795         () ; This is a dead group.  We just ignore it.
4796       ;; Cons the new param to the old one and update.
4797       (gnus-group-set-info (cons param (gnus-info-params info))
4798                            group 'params))))
4799
4800 (defun gnus-group-set-parameter (group name value)
4801   "Set parameter NAME to VALUE in GROUP."
4802   (let ((info (gnus-get-info group)))
4803     (if (not info)
4804         () ; This is a dead group.  We just ignore it.
4805       (let ((old-params (gnus-info-params info))
4806             (new-params (list (cons name value))))
4807         (while old-params
4808           (if (or (not (listp (car old-params)))
4809                   (not (eq (caar old-params) name)))
4810               (setq new-params (append new-params (list (car old-params)))))
4811           (setq old-params (cdr old-params)))
4812         (gnus-group-set-info new-params group 'params)))))
4813
4814 (defun gnus-group-add-score (group &optional score)
4815   "Add SCORE to the GROUP score.
4816 If SCORE is nil, add 1 to the score of GROUP."
4817   (let ((info (gnus-get-info group)))
4818     (when info
4819       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
4820
4821 (defun gnus-summary-bubble-group ()
4822   "Increase the score of the current group.
4823 This is a handy function to add to `gnus-summary-exit-hook' to
4824 increase the score of each group you read."
4825   (gnus-group-add-score gnus-newsgroup-name))
4826
4827 (defun gnus-group-set-info (info &optional method-only-group part)
4828   (let* ((entry (gnus-gethash
4829                  (or method-only-group (gnus-info-group info))
4830                  gnus-newsrc-hashtb))
4831          (part-info info)
4832          (info (if method-only-group (nth 2 entry) info))
4833          method)
4834     (when method-only-group
4835       (unless entry
4836         (error "Trying to change non-existent group %s" method-only-group))
4837       ;; We have received parts of the actual group info - either the
4838       ;; select method or the group parameters.  We first check
4839       ;; whether we have to extend the info, and if so, do that.
4840       (let ((len (length info))
4841             (total (if (eq part 'method) 5 6)))
4842         (when (< len total)
4843           (setcdr (nthcdr (1- len) info)
4844                   (make-list (- total len) nil)))
4845         ;; Then we enter the new info.
4846         (setcar (nthcdr (1- total) info) part-info)))
4847     (unless entry
4848       ;; This is a new group, so we just create it.
4849       (save-excursion
4850         (set-buffer gnus-group-buffer)
4851         (setq method (gnus-info-method info))
4852         (when (gnus-server-equal method "native")
4853           (setq method nil))
4854         (save-excursion
4855           (set-buffer gnus-group-buffer)
4856           (if method
4857               ;; It's a foreign group...
4858               (gnus-group-make-group
4859                (gnus-group-real-name (gnus-info-group info))
4860                (if (stringp method) method
4861                  (prin1-to-string (car method)))
4862                (and (consp method)
4863                     (nth 1 (gnus-info-method info))))
4864             ;; It's a native group.
4865             (gnus-group-make-group (gnus-info-group info))))
4866         (gnus-message 6 "Note: New group created")
4867         (setq entry
4868               (gnus-gethash (gnus-group-prefixed-name
4869                              (gnus-group-real-name (gnus-info-group info))
4870                              (or (gnus-info-method info) gnus-select-method))
4871                             gnus-newsrc-hashtb))))
4872     ;; Whether it was a new group or not, we now have the entry, so we
4873     ;; can do the update.
4874     (if entry
4875         (progn
4876           (setcar (nthcdr 2 entry) info)
4877           (when (and (not (eq (car entry) t))
4878                      (gnus-active (gnus-info-group info)))
4879             (setcar entry (length (gnus-list-of-unread-articles (car info))))))
4880       (error "No such group: %s" (gnus-info-group info)))))
4881
4882 (defun gnus-group-set-method-info (group select-method)
4883   (gnus-group-set-info select-method group 'method))
4884
4885 (defun gnus-group-set-params-info (group params)
4886   (gnus-group-set-info params group 'params))
4887
4888 (defun gnus-group-update-group-line ()
4889   "Update the current line in the group buffer."
4890   (let* ((buffer-read-only nil)
4891          (group (gnus-group-group-name))
4892          (entry (and group (gnus-gethash group gnus-newsrc-hashtb)))
4893          gnus-group-indentation)
4894     (and entry
4895          (not (gnus-ephemeral-group-p group))
4896          (gnus-dribble-enter
4897           (concat "(gnus-group-set-info '"
4898                   (prin1-to-string (nth 2 entry)) ")")))
4899     (setq gnus-group-indentation (gnus-group-group-indentation))
4900     (gnus-delete-line)
4901     (gnus-group-insert-group-line-info group)
4902     (forward-line -1)
4903     (gnus-group-position-point)))
4904
4905 (defun gnus-group-insert-group-line-info (group)
4906   "Insert GROUP on the current line."
4907   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4908         active info)
4909     (if entry
4910         (progn
4911           ;; (Un)subscribed group.
4912           (setq info (nth 2 entry))
4913           (gnus-group-insert-group-line
4914            group (gnus-info-level info) (gnus-info-marks info)
4915            (or (car entry) t) (gnus-info-method info)))
4916       ;; This group is dead.
4917       (gnus-group-insert-group-line
4918        group
4919        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4920        nil
4921        (if (setq active (gnus-active group))
4922            (- (1+ (cdr active)) (car active)) 0)
4923        nil))))
4924
4925 (defun gnus-group-insert-group-line (gnus-tmp-group gnus-tmp-level 
4926                                                     gnus-tmp-marked number
4927                                                     gnus-tmp-method)
4928   "Insert a group line in the group buffer."
4929   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4930          (gnus-tmp-number-total
4931           (if gnus-tmp-active
4932               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4933             0))
4934          (gnus-tmp-number-of-unread
4935           (if (numberp number) (int-to-string (max 0 number))
4936             "*"))
4937          (gnus-tmp-number-of-read
4938           (if (numberp number)
4939               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4940             "*"))
4941          (gnus-tmp-subscribed
4942           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4943                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4944                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4945                 (t ?K)))
4946          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4947          (gnus-tmp-newsgroup-description
4948           (if gnus-description-hashtb
4949               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4950             ""))
4951          (gnus-tmp-moderated
4952           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4953          (gnus-tmp-moderated-string
4954           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4955          (gnus-tmp-method
4956           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4957          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4958          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4959          (gnus-tmp-news-method-string
4960           (if gnus-tmp-method
4961               (format "(%s:%s)" (car gnus-tmp-method)
4962                       (cadr gnus-tmp-method)) ""))
4963          (gnus-tmp-marked-mark
4964           (if (and (numberp number)
4965                    (zerop number)
4966                    (cdr (assq 'tick gnus-tmp-marked)))
4967               ?* ? ))
4968          (gnus-tmp-process-marked
4969           (if (member gnus-tmp-group gnus-group-marked)
4970               gnus-process-mark ? ))
4971          (gnus-tmp-grouplens
4972           (or (and gnus-use-grouplens
4973                    (bbb-grouplens-group-p gnus-tmp-group))
4974               ""))
4975          (buffer-read-only nil)
4976          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4977     (beginning-of-line)
4978     (gnus-add-text-properties
4979      (point)
4980      (prog1 (1+ (point))
4981        ;; Insert the text.
4982        (eval gnus-group-line-format-spec))
4983      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4984        gnus-unread ,(if (numberp number)
4985                         (string-to-int gnus-tmp-number-of-unread)
4986                       t)
4987        gnus-marked ,gnus-tmp-marked-mark
4988        gnus-indentation ,gnus-group-indentation
4989        gnus-level ,gnus-tmp-level))
4990     (when (inline (gnus-visual-p 'group-highlight 'highlight))
4991       (forward-line -1)
4992       (run-hooks 'gnus-group-update-hook)
4993       (forward-line))
4994     ;; Allow XEmacs to remove front-sticky text properties.
4995     (gnus-group-remove-excess-properties)))
4996
4997 (defun gnus-group-update-group (group &optional visible-only)
4998   "Update all lines where GROUP appear.
4999 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
5000 already."
5001   (save-excursion
5002     (set-buffer gnus-group-buffer)
5003     ;; The buffer may be narrowed.
5004     (save-restriction
5005       (widen)
5006       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
5007             (loc (point-min))
5008             found buffer-read-only)
5009         ;; Enter the current status into the dribble buffer.
5010         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
5011           (if (and entry (not (gnus-ephemeral-group-p group)))
5012               (gnus-dribble-enter
5013                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
5014                        ")"))))
5015         ;; Find all group instances.  If topics are in use, each group
5016         ;; may be listed in more than once.
5017         (while (setq loc (text-property-any
5018                           loc (point-max) 'gnus-group ident))
5019           (setq found t)
5020           (goto-char loc)
5021           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5022             (gnus-delete-line)
5023             (gnus-group-insert-group-line-info group))
5024           (setq loc (1+ loc)))
5025         (unless (or found visible-only)
5026           ;; No such line in the buffer, find out where it's supposed to
5027           ;; go, and insert it there (or at the end of the buffer).
5028           (if gnus-goto-missing-group-function
5029               (funcall gnus-goto-missing-group-function group)
5030             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
5031               (while (and entry (car entry)
5032                           (not
5033                            (gnus-goto-char
5034                             (text-property-any
5035                              (point-min) (point-max)
5036                              'gnus-group (gnus-intern-safe
5037                                           (caar entry) gnus-active-hashtb)))))
5038                 (setq entry (cdr entry)))
5039               (or entry (goto-char (point-max)))))
5040           ;; Finally insert the line.
5041           (let ((gnus-group-indentation (gnus-group-group-indentation)))
5042             (gnus-group-insert-group-line-info group)))
5043         (gnus-group-set-mode-line)))))
5044
5045 (defun gnus-group-set-mode-line ()
5046   "Update the mode line in the group buffer."
5047   (when (memq 'group gnus-updated-mode-lines)
5048     ;; Yes, we want to keep this mode line updated.
5049     (save-excursion
5050       (set-buffer gnus-group-buffer)
5051       (let* ((gformat (or gnus-group-mode-line-format-spec
5052                           (setq gnus-group-mode-line-format-spec
5053                                 (gnus-parse-format
5054                                  gnus-group-mode-line-format
5055                                  gnus-group-mode-line-format-alist))))
5056              (gnus-tmp-news-server (cadr gnus-select-method))
5057              (gnus-tmp-news-method (car gnus-select-method))
5058              (gnus-tmp-colon (if (equal gnus-tmp-news-server "") "" ":"))
5059              (max-len 60)
5060              gnus-tmp-header            ;Dummy binding for user-defined formats
5061              ;; Get the resulting string.
5062              (mode-string (eval gformat)))
5063         ;; Say whether the dribble buffer has been modified.
5064         (setq mode-line-modified
5065               (if (and gnus-dribble-buffer
5066                        (buffer-name gnus-dribble-buffer)
5067                        (buffer-modified-p gnus-dribble-buffer)
5068                        (save-excursion
5069                          (set-buffer gnus-dribble-buffer)
5070                          (not (zerop (buffer-size)))))
5071                   "---*- " "----- "))
5072         ;; If the line is too long, we chop it off.
5073         (when (> (length mode-string) max-len)
5074           (setq mode-string (substring mode-string 0 (- max-len 4))))
5075         (prog1
5076             (setq mode-line-buffer-identification 
5077                   (list mode-string))
5078           (set-buffer-modified-p t))))))
5079
5080 (defun gnus-group-group-name ()
5081   "Get the name of the newsgroup on the current line."
5082   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
5083     (and group (symbol-name group))))
5084
5085 (defun gnus-group-group-level ()
5086   "Get the level of the newsgroup on the current line."
5087   (get-text-property (gnus-point-at-bol) 'gnus-level))
5088
5089 (defun gnus-group-group-indentation ()
5090   "Get the indentation of the newsgroup on the current line."
5091   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation)
5092       (and gnus-group-indentation-function
5093            (funcall gnus-group-indentation-function))
5094       ""))
5095
5096 (defun gnus-group-group-unread ()
5097   "Get the number of unread articles of the newsgroup on the current line."
5098   (get-text-property (gnus-point-at-bol) 'gnus-unread))
5099
5100 (defun gnus-group-search-forward (&optional backward all level first-too)
5101   "Find the next newsgroup with unread articles.
5102 If BACKWARD is non-nil, find the previous newsgroup instead.
5103 If ALL is non-nil, just find any newsgroup.
5104 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
5105 group exists.
5106 If FIRST-TOO, the current line is also eligible as a target."
5107   (let ((way (if backward -1 1))
5108         (low gnus-level-killed)
5109         (beg (point))
5110         pos found lev)
5111     (if (and backward (progn (beginning-of-line)) (bobp))
5112         nil
5113       (or first-too (forward-line way))
5114       (while (and
5115               (not (eobp))
5116               (not (setq
5117                     found
5118                     (and (or all
5119                              (and
5120                               (let ((unread
5121                                      (get-text-property (point) 'gnus-unread)))
5122                                 (and (numberp unread) (> unread 0)))
5123                               (setq lev (get-text-property (point)
5124                                                            'gnus-level))
5125                               (<= lev gnus-level-subscribed)))
5126                          (or (not level)
5127                              (and (setq lev (get-text-property (point)
5128                                                                'gnus-level))
5129                                   (or (= lev level)
5130                                       (and (< lev low)
5131                                            (< level lev)
5132                                            (progn
5133                                              (setq low lev)
5134                                              (setq pos (point))
5135                                              nil))))))))
5136               (zerop (forward-line way)))))
5137     (if found
5138         (progn (gnus-group-position-point) t)
5139       (goto-char (or pos beg))
5140       (and pos t))))
5141
5142 ;;; Gnus group mode commands
5143
5144 ;; Group marking.
5145
5146 (defun gnus-group-mark-group (n &optional unmark no-advance)
5147   "Mark the current group."
5148   (interactive "p")
5149   (let ((buffer-read-only nil)
5150         group)
5151     (while (and (> n 0)
5152                 (not (eobp)))
5153       (when (setq group (gnus-group-group-name))
5154         ;; Update the mark.
5155         (beginning-of-line)
5156         (forward-char
5157          (or (cdr (assq 'process gnus-group-mark-positions)) 2))
5158         (delete-char 1)
5159         (if unmark
5160             (progn
5161               (insert " ")
5162               (setq gnus-group-marked (delete group gnus-group-marked)))
5163           (insert "#")
5164           (setq gnus-group-marked
5165                 (cons group (delete group gnus-group-marked)))))
5166       (or no-advance (gnus-group-next-group 1))
5167       (decf n))
5168     (gnus-summary-position-point)
5169     n))
5170
5171 (defun gnus-group-unmark-group (n)
5172   "Remove the mark from the current group."
5173   (interactive "p")
5174   (gnus-group-mark-group n 'unmark)
5175   (gnus-group-position-point))
5176
5177 (defun gnus-group-unmark-all-groups ()
5178   "Unmark all groups."
5179   (interactive)
5180   (let ((groups gnus-group-marked))
5181     (save-excursion
5182       (while groups
5183         (gnus-group-remove-mark (pop groups)))))
5184   (gnus-group-position-point))
5185
5186 (defun gnus-group-mark-region (unmark beg end)
5187   "Mark all groups between point and mark.
5188 If UNMARK, remove the mark instead."
5189   (interactive "P\nr")
5190   (let ((num (count-lines beg end)))
5191     (save-excursion
5192       (goto-char beg)
5193       (- num (gnus-group-mark-group num unmark)))))
5194
5195 (defun gnus-group-mark-buffer (&optional unmark)
5196   "Mark all groups in the buffer.
5197 If UNMARK, remove the mark instead."
5198   (interactive "P")
5199   (gnus-group-mark-region unmark (point-min) (point-max)))
5200
5201 (defun gnus-group-mark-regexp (regexp)
5202   "Mark all groups that match some regexp."
5203   (interactive "sMark (regexp): ")
5204   (let ((alist (cdr gnus-newsrc-alist))
5205         group)
5206     (while alist
5207       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5208         (gnus-group-set-mark group))))
5209   (gnus-group-position-point))
5210
5211 (defun gnus-group-remove-mark (group)
5212   "Remove the process mark from GROUP and move point there.
5213 Return nil if the group isn't displayed."
5214   (if (gnus-group-goto-group group)
5215       (save-excursion
5216         (gnus-group-mark-group 1 'unmark t)
5217         t)
5218     (setq gnus-group-marked
5219           (delete group gnus-group-marked))
5220     nil))
5221
5222 (defun gnus-group-set-mark (group)
5223   "Set the process mark on GROUP."
5224   (if (gnus-group-goto-group group) 
5225       (save-excursion
5226         (gnus-group-mark-group 1 nil t))
5227     (setq gnus-group-marked (cons group (delete group gnus-group-marked)))))
5228
5229 (defun gnus-group-universal-argument (arg &optional groups func)
5230   "Perform any command on all groups accoring to the process/prefix convention."
5231   (interactive "P")
5232   (let ((groups (or groups (gnus-group-process-prefix arg)))
5233         group func)
5234     (if (eq (setq func (or func
5235                            (key-binding
5236                             (read-key-sequence
5237                              (substitute-command-keys
5238                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5239             'undefined)
5240         (gnus-error 1 "Undefined key")
5241       (while groups
5242         (gnus-group-remove-mark (setq group (pop groups)))
5243         (command-execute func))))
5244   (gnus-group-position-point))
5245
5246 (defun gnus-group-process-prefix (n)
5247   "Return a list of groups to work on.
5248 Take into consideration N (the prefix) and the list of marked groups."
5249   (cond
5250    (n
5251     (setq n (prefix-numeric-value n))
5252     ;; There is a prefix, so we return a list of the N next
5253     ;; groups.
5254     (let ((way (if (< n 0) -1 1))
5255           (n (abs n))
5256           group groups)
5257       (save-excursion
5258         (while (and (> n 0)
5259                     (setq group (gnus-group-group-name)))
5260           (setq groups (cons group groups))
5261           (setq n (1- n))
5262           (gnus-group-next-group way)))
5263       (nreverse groups)))
5264    ((and (boundp 'transient-mark-mode)
5265          transient-mark-mode
5266          (boundp 'mark-active)
5267          mark-active)
5268     ;; Work on the region between point and mark.
5269     (let ((max (max (point) (mark)))
5270           groups)
5271       (save-excursion
5272         (goto-char (min (point) (mark)))
5273         (while
5274             (and
5275              (push (gnus-group-group-name) groups)
5276              (zerop (gnus-group-next-group 1))
5277              (< (point) max)))
5278         (nreverse groups))))
5279    (gnus-group-marked
5280     ;; No prefix, but a list of marked articles.
5281     (reverse gnus-group-marked))
5282    (t
5283     ;; Neither marked articles or a prefix, so we return the
5284     ;; current group.
5285     (let ((group (gnus-group-group-name)))
5286       (and group (list group))))))
5287
5288 ;; Selecting groups.
5289
5290 (defun gnus-group-read-group (&optional all no-article group)
5291   "Read news in this newsgroup.
5292 If the prefix argument ALL is non-nil, already read articles become
5293 readable.  IF ALL is a number, fetch this number of articles.  If the
5294 optional argument NO-ARTICLE is non-nil, no article will be
5295 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5296 group."
5297   (interactive "P")
5298   (let ((group (or group (gnus-group-group-name)))
5299         number active marked entry)
5300     (or group (error "No group on current line"))
5301     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5302                                             group gnus-newsrc-hashtb)))))
5303     ;; This group might be a dead group.  In that case we have to get
5304     ;; the number of unread articles from `gnus-active-hashtb'.
5305     (setq number
5306           (cond ((numberp all) all)
5307                 (entry (car entry))
5308                 ((setq active (gnus-active group))
5309                  (- (1+ (cdr active)) (car active)))))
5310     (gnus-summary-read-group
5311      group (or all (and (numberp number)
5312                         (zerop (+ number (length (cdr (assq 'tick marked)))
5313                                   (length (cdr (assq 'dormant marked)))))))
5314      no-article)))
5315
5316 (defun gnus-group-select-group (&optional all)
5317   "Select this newsgroup.
5318 No article is selected automatically.
5319 If ALL is non-nil, already read articles become readable.
5320 If ALL is a number, fetch this number of articles."
5321   (interactive "P")
5322   (gnus-group-read-group all t))
5323
5324 (defun gnus-group-quick-select-group (&optional all)
5325   "Select the current group \"quickly\".
5326 This means that no highlighting or scoring will be performed."
5327   (interactive "P")
5328   (let (gnus-visual
5329         gnus-score-find-score-files-function
5330         gnus-apply-kill-hook
5331         gnus-summary-expunge-below)
5332     (gnus-group-read-group all t)))
5333
5334 (defun gnus-group-visible-select-group (&optional all)
5335   "Select the current group without hiding any articles."
5336   (interactive "P")
5337   (let ((gnus-inhibit-limiting t))
5338     (gnus-group-read-group all t)))
5339
5340 ;;;###autoload
5341 (defun gnus-fetch-group (group)
5342   "Start Gnus if necessary and enter GROUP.
5343 Returns whether the fetching was successful or not."
5344   (interactive "sGroup name: ")
5345   (or (get-buffer gnus-group-buffer)
5346       (gnus))
5347   (gnus-group-read-group nil nil group))
5348
5349 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5350 ;; if selection was successful.
5351 (defun gnus-group-read-ephemeral-group
5352   (group method &optional activate quit-config)
5353   (let ((group (if (gnus-group-foreign-p group) group
5354                  (gnus-group-prefixed-name group method))))
5355     (gnus-sethash
5356      group
5357      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5358                      ((quit-config . ,(if quit-config quit-config
5359                                         (cons (current-buffer) 'summary))))))
5360      gnus-newsrc-hashtb)
5361     (set-buffer gnus-group-buffer)
5362     (or (gnus-check-server method)
5363         (error "Unable to contact server: %s" (gnus-status-message method)))
5364     (if activate (or (gnus-request-group group)
5365                      (error "Couldn't request group")))
5366     (condition-case ()
5367         (gnus-group-read-group t t group)
5368       (error nil)
5369       (quit nil))))
5370
5371 (defun gnus-group-jump-to-group (group)
5372   "Jump to newsgroup GROUP."
5373   (interactive
5374    (list (completing-read
5375           "Group: " gnus-active-hashtb nil
5376           (gnus-read-active-file-p)
5377           nil
5378           'gnus-group-history)))
5379
5380   (when (equal group "")
5381     (error "Empty group name"))
5382
5383   (when (string-match "[\000-\032]" group)
5384     (error "Control characters in group: %s" group))
5385
5386   (let ((b (text-property-any
5387             (point-min) (point-max)
5388             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5389     (unless (gnus-ephemeral-group-p group)
5390       (if b
5391           ;; Either go to the line in the group buffer...
5392           (goto-char b)
5393         ;; ... or insert the line.
5394         (or
5395          (gnus-active group)
5396          (gnus-activate-group group)
5397          (error "%s error: %s" group (gnus-status-message group)))
5398
5399         (gnus-group-update-group group)
5400         (goto-char (text-property-any
5401                     (point-min) (point-max)
5402                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5403     ;; Adjust cursor point.
5404     (gnus-group-position-point)))
5405
5406 (defun gnus-group-goto-group (group)
5407   "Goto to newsgroup GROUP."
5408   (when group
5409     (let ((b (text-property-any (point-min) (point-max)
5410                                 'gnus-group (gnus-intern-safe
5411                                              group gnus-active-hashtb))))
5412       (and b (goto-char b)))))
5413
5414 (defun gnus-group-next-group (n)
5415   "Go to next N'th newsgroup.
5416 If N is negative, search backward instead.
5417 Returns the difference between N and the number of skips actually
5418 done."
5419   (interactive "p")
5420   (gnus-group-next-unread-group n t))
5421
5422 (defun gnus-group-next-unread-group (n &optional all level)
5423   "Go to next N'th unread newsgroup.
5424 If N is negative, search backward instead.
5425 If ALL is non-nil, choose any newsgroup, unread or not.
5426 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5427 such group can be found, the next group with a level higher than
5428 LEVEL.
5429 Returns the difference between N and the number of skips actually
5430 made."
5431   (interactive "p")
5432   (let ((backward (< n 0))
5433         (n (abs n)))
5434     (while (and (> n 0)
5435                 (gnus-group-search-forward
5436                  backward (or (not gnus-group-goto-unread) all) level))
5437       (setq n (1- n)))
5438     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5439                                (if level " on this level or higher" "")))
5440     n))
5441
5442 (defun gnus-group-prev-group (n)
5443   "Go to previous N'th newsgroup.
5444 Returns the difference between N and the number of skips actually
5445 done."
5446   (interactive "p")
5447   (gnus-group-next-unread-group (- n) t))
5448
5449 (defun gnus-group-prev-unread-group (n)
5450   "Go to previous N'th unread newsgroup.
5451 Returns the difference between N and the number of skips actually
5452 done."
5453   (interactive "p")
5454   (gnus-group-next-unread-group (- n)))
5455
5456 (defun gnus-group-next-unread-group-same-level (n)
5457   "Go to next N'th unread newsgroup on the same level.
5458 If N is negative, search backward instead.
5459 Returns the difference between N and the number of skips actually
5460 done."
5461   (interactive "p")
5462   (gnus-group-next-unread-group n t (gnus-group-group-level))
5463   (gnus-group-position-point))
5464
5465 (defun gnus-group-prev-unread-group-same-level (n)
5466   "Go to next N'th unread newsgroup on the same level.
5467 Returns the difference between N and the number of skips actually
5468 done."
5469   (interactive "p")
5470   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5471   (gnus-group-position-point))
5472
5473 (defun gnus-group-best-unread-group (&optional exclude-group)
5474   "Go to the group with the highest level.
5475 If EXCLUDE-GROUP, do not go to that group."
5476   (interactive)
5477   (goto-char (point-min))
5478   (let ((best 100000)
5479         unread best-point)
5480     (while (not (eobp))
5481       (setq unread (get-text-property (point) 'gnus-unread))
5482       (if (and (numberp unread) (> unread 0))
5483           (progn
5484             (if (and (get-text-property (point) 'gnus-level)
5485                      (< (get-text-property (point) 'gnus-level) best)
5486                      (or (not exclude-group)
5487                          (not (equal exclude-group (gnus-group-group-name)))))
5488                 (progn
5489                   (setq best (get-text-property (point) 'gnus-level))
5490                   (setq best-point (point))))))
5491       (forward-line 1))
5492     (if best-point (goto-char best-point))
5493     (gnus-summary-position-point)
5494     (and best-point (gnus-group-group-name))))
5495
5496 (defun gnus-group-first-unread-group ()
5497   "Go to the first group with unread articles."
5498   (interactive)
5499   (prog1
5500       (let ((opoint (point))
5501             unread)
5502         (goto-char (point-min))
5503         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5504                 (and (numberp unread)   ; Not a topic.
5505                      (not (zerop unread))) ; Has unread articles.
5506                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5507             (point)                     ; Success.
5508           (goto-char opoint)
5509           nil))                         ; Not success.
5510     (gnus-group-position-point)))
5511
5512 (defun gnus-group-enter-server-mode ()
5513   "Jump to the server buffer."
5514   (interactive)
5515   (gnus-enter-server-buffer))
5516
5517 (defun gnus-group-make-group (name &optional method address)
5518   "Add a new newsgroup.
5519 The user will be prompted for a NAME, for a select METHOD, and an
5520 ADDRESS."
5521   (interactive
5522    (cons
5523     (read-string "Group name: ")
5524     (let ((method
5525            (completing-read
5526             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5527             nil t nil 'gnus-method-history)))
5528       (cond ((assoc method gnus-valid-select-methods)
5529              (list method
5530                    (if (memq 'prompt-address
5531                              (assoc method gnus-valid-select-methods))
5532                        (read-string "Address: ")
5533                      "")))
5534             ((assoc method gnus-server-alist)
5535              (list method))
5536             (t
5537              (list method ""))))))
5538
5539   (let* ((meth (and method (if address (list (intern method) address)
5540                              method)))
5541          (nname (if method (gnus-group-prefixed-name name meth) name))
5542          backend info)
5543     (when (gnus-gethash nname gnus-newsrc-hashtb)
5544       (error "Group %s already exists" nname))
5545     ;; Subscribe to the new group.
5546     (gnus-group-change-level
5547      (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5548      gnus-level-default-subscribed gnus-level-killed
5549      (and (gnus-group-group-name)
5550           (gnus-gethash (gnus-group-group-name)
5551                         gnus-newsrc-hashtb))
5552      t)
5553     ;; Make it active.
5554     (gnus-set-active nname (cons 1 0))
5555     (or (gnus-ephemeral-group-p name)
5556         (gnus-dribble-enter
5557          (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5558     ;; Insert the line.
5559     (gnus-group-insert-group-line-info nname)
5560     (forward-line -1)
5561     (gnus-group-position-point)
5562
5563     ;; Load the backend and try to make the backend create
5564     ;; the group as well.
5565     (when (assoc (symbol-name (setq backend (car (gnus-server-get-method
5566                                                   nil meth))))
5567                  gnus-valid-select-methods)
5568       (require backend))
5569     (gnus-check-server meth)
5570     (and (gnus-check-backend-function 'request-create-group nname)
5571          (gnus-request-create-group nname))
5572     t))
5573
5574 (defun gnus-group-delete-group (group &optional force)
5575   "Delete the current group.
5576 If FORCE (the prefix) is non-nil, all the articles in the group will
5577 be deleted.  This is \"deleted\" as in \"removed forever from the face
5578 of the Earth\".  There is no undo."
5579   (interactive
5580    (list (gnus-group-group-name)
5581          current-prefix-arg))
5582   (or group (error "No group to rename"))
5583   (or (gnus-check-backend-function 'request-delete-group group)
5584       (error "This backend does not support group deletion"))
5585   (prog1
5586       (if (not (gnus-yes-or-no-p
5587                 (format
5588                  "Do you really want to delete %s%s? "
5589                  group (if force " and all its contents" ""))))
5590           () ; Whew!
5591         (gnus-message 6 "Deleting group %s..." group)
5592         (if (not (gnus-request-delete-group group force))
5593             (gnus-error 3 "Couldn't delete group %s" group)
5594           (gnus-message 6 "Deleting group %s...done" group)
5595           (gnus-group-goto-group group)
5596           (gnus-group-kill-group 1 t)
5597           (gnus-sethash group nil gnus-active-hashtb)
5598           t))
5599     (gnus-group-position-point)))
5600
5601 (defun gnus-group-rename-group (group new-name)
5602   (interactive
5603    (list
5604     (gnus-group-group-name)
5605     (progn
5606       (or (gnus-check-backend-function
5607            'request-rename-group (gnus-group-group-name))
5608           (error "This backend does not support renaming groups"))
5609       (read-string "New group name: "))))
5610
5611   (or (gnus-check-backend-function 'request-rename-group group)
5612       (error "This backend does not support renaming groups"))
5613
5614   (or group (error "No group to rename"))
5615   (and (string-match "^[ \t]*$" new-name)
5616        (error "Not a valid group name"))
5617
5618   ;; We find the proper prefixed name.
5619   (setq new-name
5620         (gnus-group-prefixed-name
5621          (gnus-group-real-name new-name)
5622          (gnus-info-method (gnus-get-info group))))
5623
5624   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5625   (prog1
5626       (if (not (gnus-request-rename-group group new-name))
5627           (gnus-error 3 "Couldn't rename group %s to %s" group new-name)
5628         ;; We rename the group internally by killing it...
5629         (gnus-group-goto-group group)
5630         (gnus-group-kill-group)
5631         ;; ... changing its name ...
5632         (setcar (cdar gnus-list-of-killed-groups) new-name)
5633         ;; ... and then yanking it.  Magic!
5634         (gnus-group-yank-group)
5635         (gnus-set-active new-name (gnus-active group))
5636         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5637         new-name)
5638     (gnus-group-position-point)))
5639
5640 (defun gnus-group-edit-group (group &optional part)
5641   "Edit the group on the current line."
5642   (interactive (list (gnus-group-group-name)))
5643   (let* ((part (or part 'info))
5644          (done-func `(lambda ()
5645                        "Exit editing mode and update the information."
5646                        (interactive)
5647                        (gnus-group-edit-group-done ',part ,group)))
5648          (winconf (current-window-configuration))
5649          info)
5650     (or group (error "No group on current line"))
5651     (or (setq info (gnus-get-info group))
5652         (error "Killed group; can't be edited"))
5653     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5654     (gnus-configure-windows 'edit-group)
5655     (gnus-add-current-to-buffer-list)
5656     (emacs-lisp-mode)
5657     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5658     (use-local-map (copy-keymap emacs-lisp-mode-map))
5659     (local-set-key "\C-c\C-c" done-func)
5660     (make-local-variable 'gnus-prev-winconf)
5661     (setq gnus-prev-winconf winconf)
5662     (erase-buffer)
5663     (insert
5664      (cond
5665       ((eq part 'method)
5666        ";; Type `C-c C-c' after editing the select method.\n\n")
5667       ((eq part 'params)
5668        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5669       ((eq part 'info)
5670        ";; Type `C-c C-c' after editing the group info.\n\n")))
5671     (insert
5672      (pp-to-string
5673       (cond ((eq part 'method)
5674              (or (gnus-info-method info) "native"))
5675             ((eq part 'params)
5676              (gnus-info-params info))
5677             (t info)))
5678      "\n")))
5679
5680 (defun gnus-group-edit-group-method (group)
5681   "Edit the select method of GROUP."
5682   (interactive (list (gnus-group-group-name)))
5683   (gnus-group-edit-group group 'method))
5684
5685 (defun gnus-group-edit-group-parameters (group)
5686   "Edit the group parameters of GROUP."
5687   (interactive (list (gnus-group-group-name)))
5688   (gnus-group-edit-group group 'params))
5689
5690 (defun gnus-group-edit-group-done (part group)
5691   "Get info from buffer, update variables and jump to the group buffer."
5692   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5693   (goto-char (point-min))
5694   (let* ((form (read (current-buffer)))
5695          (winconf gnus-prev-winconf)
5696          (method (cond ((eq part 'info) (nth 4 form))
5697                        ((eq part 'method) form)
5698                        (t nil)))
5699          (info (cond ((eq part 'info) form)
5700                      ((eq part 'method) (gnus-get-info group))
5701                      (t nil)))
5702          (new-group (if info
5703                       (if (or (not method)
5704                               (gnus-server-equal
5705                                gnus-select-method method))
5706                           (gnus-group-real-name (car info))
5707                         (gnus-group-prefixed-name
5708                          (gnus-group-real-name (car info)) method))
5709                       nil)))
5710     (when (and new-group
5711                (not (equal new-group group)))
5712       (when (gnus-group-goto-group group)
5713         (gnus-group-kill-group 1))
5714       (gnus-activate-group new-group))
5715     ;; Set the info.
5716     (if (and info new-group)
5717         (progn
5718           (setq info (gnus-copy-sequence info))
5719           (setcar info new-group)
5720           (unless (gnus-server-equal method "native")
5721             (unless (nthcdr 3 info)
5722               (nconc info (list nil nil)))
5723             (unless (nthcdr 4 info)
5724               (nconc info (list nil)))
5725             (gnus-info-set-method info method))
5726           (gnus-group-set-info info))
5727       (gnus-group-set-info form (or new-group group) part))
5728     (kill-buffer (current-buffer))
5729     (and winconf (set-window-configuration winconf))
5730     (set-buffer gnus-group-buffer)
5731     (gnus-group-update-group (or new-group group))
5732     (gnus-group-position-point)))
5733
5734 (defun gnus-group-make-help-group ()
5735   "Create the Gnus documentation group."
5736   (interactive)
5737   (let ((path load-path)
5738         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5739         file dir)
5740     (and (gnus-gethash name gnus-newsrc-hashtb)
5741          (error "Documentation group already exists"))
5742     (while path
5743       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5744             file nil)
5745       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5746                 (file-exists-p
5747                  (setq file (concat (file-name-directory
5748                                      (directory-file-name dir))
5749                                     "etc/gnus-tut.txt"))))
5750         (setq path nil)))
5751     (if (not file)
5752         (gnus-message 1 "Couldn't find doc group")
5753       (gnus-group-make-group
5754        (gnus-group-real-name name)
5755        (list 'nndoc "gnus-help"
5756              (list 'nndoc-address file)
5757              (list 'nndoc-article-type 'mbox)))))
5758   (gnus-group-position-point))
5759
5760 (defun gnus-group-make-doc-group (file type)
5761   "Create a group that uses a single file as the source."
5762   (interactive
5763    (list (read-file-name "File name: ")
5764          (and current-prefix-arg 'ask)))
5765   (when (eq type 'ask)
5766     (let ((err "")
5767           char found)
5768       (while (not found)
5769         (message
5770          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5771          err)
5772         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5773                           ((= char ?b) 'babyl)
5774                           ((= char ?d) 'digest)
5775                           ((= char ?f) 'forward)
5776                           ((= char ?a) 'mmfd)
5777                           (t (setq err (format "%c unknown. " char))
5778                              nil))))
5779       (setq type found)))
5780   (let* ((file (expand-file-name file))
5781          (name (gnus-generate-new-group-name
5782                 (gnus-group-prefixed-name
5783                  (file-name-nondirectory file) '(nndoc "")))))
5784     (gnus-group-make-group
5785      (gnus-group-real-name name)
5786      (list 'nndoc (file-name-nondirectory file)
5787            (list 'nndoc-address file)
5788            (list 'nndoc-article-type (or type 'guess))))))
5789
5790 (defun gnus-group-make-archive-group (&optional all)
5791   "Create the (ding) Gnus archive group of the most recent articles.
5792 Given a prefix, create a full group."
5793   (interactive "P")
5794   (let ((group (gnus-group-prefixed-name
5795                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5796     (and (gnus-gethash group gnus-newsrc-hashtb)
5797          (error "Archive group already exists"))
5798     (gnus-group-make-group
5799      (gnus-group-real-name group)
5800      (list 'nndir (if all "hpc" "edu")
5801            (list 'nndir-directory
5802                  (if all gnus-group-archive-directory
5803                    gnus-group-recent-archive-directory))))))
5804
5805 (defun gnus-group-make-directory-group (dir)
5806   "Create an nndir group.
5807 The user will be prompted for a directory.  The contents of this
5808 directory will be used as a newsgroup.  The directory should contain
5809 mail messages or news articles in files that have numeric names."
5810   (interactive
5811    (list (read-file-name "Create group from directory: ")))
5812   (or (file-exists-p dir) (error "No such directory"))
5813   (or (file-directory-p dir) (error "Not a directory"))
5814   (let ((ext "")
5815         (i 0)
5816         group)
5817     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5818       (setq group
5819             (gnus-group-prefixed-name
5820              (concat (file-name-as-directory (directory-file-name dir))
5821                      ext)
5822              '(nndir "")))
5823       (setq ext (format "<%d>" (setq i (1+ i)))))
5824     (gnus-group-make-group
5825      (gnus-group-real-name group)
5826      (list 'nndir group (list 'nndir-directory dir)))))
5827
5828 (defun gnus-group-make-kiboze-group (group address scores)
5829   "Create an nnkiboze group.
5830 The user will be prompted for a name, a regexp to match groups, and
5831 score file entries for articles to include in the group."
5832   (interactive
5833    (list
5834     (read-string "nnkiboze group name: ")
5835     (read-string "Source groups (regexp): ")
5836     (let ((headers (mapcar (lambda (group) (list group))
5837                            '("subject" "from" "number" "date" "message-id"
5838                              "references" "chars" "lines" "xref"
5839                              "followup" "all" "body" "head")))
5840           scores header regexp regexps)
5841       (while (not (equal "" (setq header (completing-read
5842                                           "Match on header: " headers nil t))))
5843         (setq regexps nil)
5844         (while (not (equal "" (setq regexp (read-string
5845                                             (format "Match on %s (string): "
5846                                                     header)))))
5847           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5848         (setq scores (cons (cons header regexps) scores)))
5849       scores)))
5850   (gnus-group-make-group group "nnkiboze" address)
5851   (nnheader-temp-write (gnus-score-file-name (concat "nnkiboze:" group))
5852     (let (emacs-lisp-mode-hook)
5853       (pp scores (current-buffer)))))
5854
5855 (defun gnus-group-add-to-virtual (n vgroup)
5856   "Add the current group to a virtual group."
5857   (interactive
5858    (list current-prefix-arg
5859          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5860                           "nnvirtual:")))
5861   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5862       (error "%s is not an nnvirtual group" vgroup))
5863   (let* ((groups (gnus-group-process-prefix n))
5864          (method (gnus-info-method (gnus-get-info vgroup))))
5865     (setcar (cdr method)
5866             (concat
5867              (nth 1 method) "\\|"
5868              (mapconcat
5869               (lambda (s)
5870                 (gnus-group-remove-mark s)
5871                 (concat "\\(^" (regexp-quote s) "$\\)"))
5872               groups "\\|"))))
5873   (gnus-group-position-point))
5874
5875 (defun gnus-group-make-empty-virtual (group)
5876   "Create a new, fresh, empty virtual group."
5877   (interactive "sCreate new, empty virtual group: ")
5878   (let* ((method (list 'nnvirtual "^$"))
5879          (pgroup (gnus-group-prefixed-name group method)))
5880     ;; Check whether it exists already.
5881     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5882          (error "Group %s already exists." pgroup))
5883     ;; Subscribe the new group after the group on the current line.
5884     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5885     (gnus-group-update-group pgroup)
5886     (forward-line -1)
5887     (gnus-group-position-point)))
5888
5889 (defun gnus-group-enter-directory (dir)
5890   "Enter an ephemeral nneething group."
5891   (interactive "DDirectory to read: ")
5892   (let* ((method (list 'nneething dir))
5893          (leaf (gnus-group-prefixed-name
5894                 (file-name-nondirectory (directory-file-name dir))
5895                 method))
5896          (name (gnus-generate-new-group-name leaf)))
5897     (let ((nneething-read-only t))
5898       (or (gnus-group-read-ephemeral-group
5899            name method t
5900            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5901                                       'summary 'group)))
5902           (error "Couldn't enter %s" dir)))))
5903
5904 ;; Group sorting commands
5905 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5906
5907 (defun gnus-group-sort-groups (func &optional reverse)
5908   "Sort the group buffer according to FUNC.
5909 If REVERSE, reverse the sorting order."
5910   (interactive (list gnus-group-sort-function
5911                      current-prefix-arg))
5912   (let ((func (cond 
5913                ((not (listp func)) func)
5914                ((null func) func)
5915                ((= 1 (length func)) (car func))
5916                (t `(lambda (t1 t2)
5917                      ,(gnus-make-sort-function 
5918                        (reverse func)))))))
5919     ;; We peel off the dummy group from the alist.
5920     (when func
5921       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5922         (pop gnus-newsrc-alist))
5923       ;; Do the sorting.
5924       (setq gnus-newsrc-alist
5925             (sort gnus-newsrc-alist func))
5926       (when reverse
5927         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5928       ;; Regenerate the hash table.
5929       (gnus-make-hashtable-from-newsrc-alist)
5930       (gnus-group-list-groups))))
5931
5932 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5933   "Sort the group buffer alphabetically by group name.
5934 If REVERSE, sort in reverse order."
5935   (interactive "P")
5936   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5937
5938 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5939   "Sort the group buffer by number of unread articles.
5940 If REVERSE, sort in reverse order."
5941   (interactive "P")
5942   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5943
5944 (defun gnus-group-sort-groups-by-level (&optional reverse)
5945   "Sort the group buffer by group level.
5946 If REVERSE, sort in reverse order."
5947   (interactive "P")
5948   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5949
5950 (defun gnus-group-sort-groups-by-score (&optional reverse)
5951   "Sort the group buffer by group score.
5952 If REVERSE, sort in reverse order."
5953   (interactive "P")
5954   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5955
5956 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5957   "Sort the group buffer by group rank.
5958 If REVERSE, sort in reverse order."
5959   (interactive "P")
5960   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5961
5962 (defun gnus-group-sort-groups-by-method (&optional reverse)
5963   "Sort the group buffer alphabetically by backend name.
5964 If REVERSE, sort in reverse order."
5965   (interactive "P")
5966   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5967
5968 (defun gnus-group-sort-by-alphabet (info1 info2)
5969   "Sort alphabetically."
5970   (string< (gnus-info-group info1) (gnus-info-group info2)))
5971
5972 (defun gnus-group-sort-by-unread (info1 info2)
5973   "Sort by number of unread articles."
5974   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5975         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5976     (< (or (and (numberp n1) n1) 0)
5977        (or (and (numberp n2) n2) 0))))
5978
5979 (defun gnus-group-sort-by-level (info1 info2)
5980   "Sort by level."
5981   (< (gnus-info-level info1) (gnus-info-level info2)))
5982
5983 (defun gnus-group-sort-by-method (info1 info2)
5984   "Sort alphabetically by backend name."
5985   (string< (symbol-name (car (gnus-find-method-for-group
5986                               (gnus-info-group info1) info1)))
5987            (symbol-name (car (gnus-find-method-for-group
5988                               (gnus-info-group info2) info2)))))
5989
5990 (defun gnus-group-sort-by-score (info1 info2)
5991   "Sort by group score."
5992   (< (gnus-info-score info1) (gnus-info-score info2)))
5993
5994 (defun gnus-group-sort-by-rank (info1 info2)
5995   "Sort by level and score."
5996   (let ((level1 (gnus-info-level info1))
5997         (level2 (gnus-info-level info2)))
5998     (or (< level1 level2)
5999         (and (= level1 level2)
6000              (> (gnus-info-score info1) (gnus-info-score info2))))))
6001
6002 ;; Group catching up.
6003
6004 (defun gnus-group-clear-data (n)
6005   "Clear all marks and read ranges from the current group."
6006   (interactive "P")
6007   (let ((groups (gnus-group-process-prefix n))
6008         group info)
6009     (while (setq group (pop groups))
6010       (setq info (gnus-get-info group))
6011       (gnus-info-set-read info nil)
6012       (when (gnus-info-marks info)
6013         (gnus-info-set-marks info nil))
6014       (gnus-get-unread-articles-in-group info (gnus-active group) t)
6015       (when (gnus-group-goto-group group)
6016         (gnus-group-remove-mark group)
6017         (gnus-group-update-group-line)))))
6018
6019 (defun gnus-group-catchup-current (&optional n all)
6020   "Mark all articles not marked as unread in current newsgroup as read.
6021 If prefix argument N is numeric, the ARG next newsgroups will be
6022 caught up.  If ALL is non-nil, marked articles will also be marked as
6023 read.  Cross references (Xref: header) of articles are ignored.
6024 The difference between N and actual number of newsgroups that were
6025 caught up is returned."
6026   (interactive "P")
6027   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
6028                gnus-expert-user
6029                (gnus-y-or-n-p
6030                 (if all
6031                     "Do you really want to mark all articles as read? "
6032                   "Mark all unread articles as read? "))))
6033       n
6034     (let ((groups (gnus-group-process-prefix n))
6035           (ret 0))
6036       (while groups
6037         ;; Virtual groups have to be given special treatment.
6038         (let ((method (gnus-find-method-for-group (car groups))))
6039           (if (eq 'nnvirtual (car method))
6040               (nnvirtual-catchup-group
6041                (gnus-group-real-name (car groups)) (nth 1 method) all)))
6042         (gnus-group-remove-mark (car groups))
6043         (if (>= (gnus-group-group-level) gnus-level-zombie)
6044             (gnus-message 2 "Dead groups can't be caught up")
6045           (if (prog1
6046                   (gnus-group-goto-group (car groups))
6047                 (gnus-group-catchup (car groups) all))
6048               (gnus-group-update-group-line)
6049             (setq ret (1+ ret))))
6050         (setq groups (cdr groups)))
6051       (gnus-group-next-unread-group 1)
6052       ret)))
6053
6054 (defun gnus-group-catchup-current-all (&optional n)
6055   "Mark all articles in current newsgroup as read.
6056 Cross references (Xref: header) of articles are ignored."
6057   (interactive "P")
6058   (gnus-group-catchup-current n 'all))
6059
6060 (defun gnus-group-catchup (group &optional all)
6061   "Mark all articles in GROUP as read.
6062 If ALL is non-nil, all articles are marked as read.
6063 The return value is the number of articles that were marked as read,
6064 or nil if no action could be taken."
6065   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
6066          (num (car entry)))
6067     ;; Do the updating only if the newsgroup isn't killed.
6068     (if (not (numberp (car entry)))
6069         (gnus-message 1 "Can't catch up; non-active group")
6070       ;; Do auto-expirable marks if that's required.
6071       (when (gnus-group-auto-expirable-p group)
6072         (gnus-add-marked-articles
6073          group 'expire (gnus-list-of-unread-articles group))
6074         (when all
6075           (let ((marks (nth 3 (nth 2 entry))))
6076             (gnus-add-marked-articles
6077              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
6078             (gnus-add-marked-articles
6079              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
6080       (when entry
6081         (gnus-update-read-articles group nil)
6082         ;; Also nix out the lists of marks and dormants.
6083         (when all
6084           (gnus-add-marked-articles group 'tick nil nil 'force)
6085           (gnus-add-marked-articles group 'dormant nil nil 'force))
6086         (run-hooks 'gnus-group-catchup-group-hook)
6087         num))))
6088
6089 (defun gnus-group-expire-articles (&optional n)
6090   "Expire all expirable articles in the current newsgroup."
6091   (interactive "P")
6092   (let ((groups (gnus-group-process-prefix n))
6093         group)
6094     (unless groups
6095       (error "No groups to expire"))
6096     (while (setq group (pop groups))
6097       (gnus-group-remove-mark group)
6098       (when (gnus-check-backend-function 'request-expire-articles group)
6099         (gnus-message 6 "Expiring articles in %s..." group)
6100         (let* ((info (gnus-get-info group))
6101                (expirable (if (gnus-group-total-expirable-p group)
6102                               (cons nil (gnus-list-of-read-articles group))
6103                             (assq 'expire (gnus-info-marks info))))
6104                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
6105           (when expirable
6106             (setcdr
6107              expirable
6108              (gnus-compress-sequence
6109               (if expiry-wait
6110                   ;; We set the expiry variables to the groupp
6111                   ;; parameter. 
6112                   (let ((nnmail-expiry-wait-function nil)
6113                         (nnmail-expiry-wait expiry-wait))
6114                     (gnus-request-expire-articles
6115                      (gnus-uncompress-sequence (cdr expirable)) group))
6116                 ;; Just expire using the normal expiry values.
6117                 (gnus-request-expire-articles
6118                  (gnus-uncompress-sequence (cdr expirable)) group)))))
6119           (gnus-message 6 "Expiring articles in %s...done" group)))
6120       (gnus-group-position-point))))
6121
6122 (defun gnus-group-expire-all-groups ()
6123   "Expire all expirable articles in all newsgroups."
6124   (interactive)
6125   (save-excursion
6126     (gnus-message 5 "Expiring...")
6127     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
6128                                      (cdr gnus-newsrc-alist))))
6129       (gnus-group-expire-articles nil)))
6130   (gnus-group-position-point)
6131   (gnus-message 5 "Expiring...done"))
6132
6133 (defun gnus-group-set-current-level (n level)
6134   "Set the level of the next N groups to LEVEL."
6135   (interactive
6136    (list
6137     current-prefix-arg
6138     (string-to-int
6139      (let ((s (read-string
6140                (format "Level (default %s): "
6141                        (or (gnus-group-group-level) 
6142                            gnus-level-default-subscribed)))))
6143        (if (string-match "^\\s-*$" s)
6144            (int-to-string (or (gnus-group-group-level) 
6145                               gnus-level-default-subscribed))
6146          s)))))
6147   (or (and (>= level 1) (<= level gnus-level-killed))
6148       (error "Illegal level: %d" level))
6149   (let ((groups (gnus-group-process-prefix n))
6150         group)
6151     (while (setq group (pop groups))
6152       (gnus-group-remove-mark group)
6153       (gnus-message 6 "Changed level of %s from %d to %d"
6154                     group (or (gnus-group-group-level) gnus-level-killed)
6155                     level)
6156       (gnus-group-change-level
6157        group level (or (gnus-group-group-level) gnus-level-killed))
6158       (gnus-group-update-group-line)))
6159   (gnus-group-position-point))
6160
6161 (defun gnus-group-unsubscribe-current-group (&optional n)
6162   "Toggle subscription of the current group.
6163 If given numerical prefix, toggle the N next groups."
6164   (interactive "P")
6165   (let ((groups (gnus-group-process-prefix n))
6166         group)
6167     (while groups
6168       (setq group (car groups)
6169             groups (cdr groups))
6170       (gnus-group-remove-mark group)
6171       (gnus-group-unsubscribe-group
6172        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
6173                  gnus-level-default-unsubscribed
6174                gnus-level-default-subscribed) t)
6175       (gnus-group-update-group-line))
6176     (gnus-group-next-group 1)))
6177
6178 (defun gnus-group-unsubscribe-group (group &optional level silent)
6179   "Toggle subscription to GROUP.
6180 Killed newsgroups are subscribed.  If SILENT, don't try to update the
6181 group line."
6182   (interactive
6183    (list (completing-read
6184           "Group: " gnus-active-hashtb nil
6185           (gnus-read-active-file-p)
6186           nil 
6187           'gnus-group-history)))
6188   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
6189     (cond
6190      ((string-match "^[ \t]$" group)
6191       (error "Empty group name"))
6192      (newsrc
6193       ;; Toggle subscription flag.
6194       (gnus-group-change-level
6195        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6196                                       gnus-level-subscribed)
6197                                   (1+ gnus-level-subscribed)
6198                                 gnus-level-default-subscribed)))
6199       (unless silent
6200         (gnus-group-update-group group)))
6201      ((and (stringp group)
6202            (or (not (gnus-read-active-file-p))
6203                (gnus-active group)))
6204       ;; Add new newsgroup.
6205       (gnus-group-change-level
6206        group
6207        (if level level gnus-level-default-subscribed)
6208        (or (and (member group gnus-zombie-list)
6209                 gnus-level-zombie)
6210            gnus-level-killed)
6211        (and (gnus-group-group-name)
6212             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6213       (unless silent
6214         (gnus-group-update-group group)))
6215      (t (error "No such newsgroup: %s" group)))
6216     (gnus-group-position-point)))
6217
6218 (defun gnus-group-transpose-groups (n)
6219   "Move the current newsgroup up N places.
6220 If given a negative prefix, move down instead.  The difference between
6221 N and the number of steps taken is returned."
6222   (interactive "p")
6223   (or (gnus-group-group-name)
6224       (error "No group on current line"))
6225   (gnus-group-kill-group 1)
6226   (prog1
6227       (forward-line (- n))
6228     (gnus-group-yank-group)
6229     (gnus-group-position-point)))
6230
6231 (defun gnus-group-kill-all-zombies ()
6232   "Kill all zombie newsgroups."
6233   (interactive)
6234   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6235   (setq gnus-zombie-list nil)
6236   (gnus-group-list-groups))
6237
6238 (defun gnus-group-kill-region (begin end)
6239   "Kill newsgroups in current region (excluding current point).
6240 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6241   (interactive "r")
6242   (let ((lines
6243          ;; Count lines.
6244          (save-excursion
6245            (count-lines
6246             (progn
6247               (goto-char begin)
6248               (beginning-of-line)
6249               (point))
6250             (progn
6251               (goto-char end)
6252               (beginning-of-line)
6253               (point))))))
6254     (goto-char begin)
6255     (beginning-of-line)                 ;Important when LINES < 1
6256     (gnus-group-kill-group lines)))
6257
6258 (defun gnus-group-kill-group (&optional n discard)
6259   "Kill the next N groups.
6260 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6261 However, only groups that were alive can be yanked; already killed
6262 groups or zombie groups can't be yanked.
6263 The return value is the name of the group that was killed, or a list
6264 of groups killed."
6265   (interactive "P")
6266   (let ((buffer-read-only nil)
6267         (groups (gnus-group-process-prefix n))
6268         group entry level out)
6269     (if (< (length groups) 10)
6270         ;; This is faster when there are few groups.
6271         (while groups
6272           (push (setq group (pop groups)) out)
6273           (gnus-group-remove-mark group)
6274           (setq level (gnus-group-group-level))
6275           (gnus-delete-line)
6276           (when (and (not discard)
6277                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6278             (push (cons (car entry) (nth 2 entry))
6279                   gnus-list-of-killed-groups))
6280           (gnus-group-change-level
6281            (if entry entry group) gnus-level-killed (if entry nil level)))
6282       ;; If there are lots and lots of groups to be killed, we use
6283       ;; this thing instead.
6284       (let (entry)
6285         (setq groups (nreverse groups))
6286         (while groups
6287           (gnus-group-remove-mark (setq group (pop groups)))
6288           (gnus-delete-line)
6289           (cond
6290            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6291             (push (cons (car entry) (nth 2 entry))
6292                   gnus-list-of-killed-groups)
6293             (setcdr (cdr entry) (cdddr entry)))
6294            ((member group gnus-zombie-list)
6295             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6296         (gnus-make-hashtable-from-newsrc-alist)))
6297
6298     (gnus-group-position-point)
6299     (if (< (length out) 2) (car out) (nreverse out))))
6300
6301 (defun gnus-group-yank-group (&optional arg)
6302   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6303 inserting it before the current newsgroup.  The numeric ARG specifies
6304 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6305 is returned, or (if several groups are yanked) a list of yanked groups
6306 is returned."
6307   (interactive "p")
6308   (setq arg (or arg 1))
6309   (let (info group prev out)
6310     (while (>= (decf arg) 0)
6311       (if (not (setq info (pop gnus-list-of-killed-groups)))
6312           (error "No more newsgroups to yank"))
6313       (push (setq group (nth 1 info)) out)
6314       ;; Find which newsgroup to insert this one before - search
6315       ;; backward until something suitable is found.  If there are no
6316       ;; other newsgroups in this buffer, just make this newsgroup the
6317       ;; first newsgroup.
6318       (setq prev (gnus-group-group-name))
6319       (gnus-group-change-level
6320        info (gnus-info-level (cdr info)) gnus-level-killed
6321        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6322        t)
6323       (gnus-group-insert-group-line-info group))
6324     (forward-line -1)
6325     (gnus-group-position-point)
6326     (if (< (length out) 2) (car out) (nreverse out))))
6327
6328 (defun gnus-group-kill-level (level)
6329   "Kill all groups that is on a certain LEVEL."
6330   (interactive "nKill all groups on level: ")
6331   (cond
6332    ((= level gnus-level-zombie)
6333     (setq gnus-killed-list
6334           (nconc gnus-zombie-list gnus-killed-list))
6335     (setq gnus-zombie-list nil))
6336    ((and (< level gnus-level-zombie)
6337          (> level 0)
6338          (or gnus-expert-user
6339              (gnus-yes-or-no-p
6340               (format
6341                "Do you really want to kill all groups on level %d? "
6342                level))))
6343     (let* ((prev gnus-newsrc-alist)
6344            (alist (cdr prev)))
6345       (while alist
6346         (if (= (gnus-info-level level) level)
6347             (setcdr prev (cdr alist))
6348           (setq prev alist))
6349         (setq alist (cdr alist)))
6350       (gnus-make-hashtable-from-newsrc-alist)
6351       (gnus-group-list-groups)))
6352    (t
6353     (error "Can't kill; illegal level: %d" level))))
6354
6355 (defun gnus-group-list-all-groups (&optional arg)
6356   "List all newsgroups with level ARG or lower.
6357 Default is gnus-level-unsubscribed, which lists all subscribed and most
6358 unsubscribed groups."
6359   (interactive "P")
6360   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6361
6362 ;; Redefine this to list ALL killed groups if prefix arg used.
6363 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6364 (defun gnus-group-list-killed (&optional arg)
6365   "List all killed newsgroups in the group buffer.
6366 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6367 entail asking the server for the groups."
6368   (interactive "P")
6369   ;; Find all possible killed newsgroups if arg.
6370   (when arg
6371     (gnus-get-killed-groups))
6372   (if (not gnus-killed-list)
6373       (gnus-message 6 "No killed groups")
6374     (let (gnus-group-list-mode)
6375       (funcall gnus-group-prepare-function
6376                gnus-level-killed t gnus-level-killed))
6377     (goto-char (point-min)))
6378   (gnus-group-position-point))
6379
6380 (defun gnus-group-list-zombies ()
6381   "List all zombie newsgroups in the group buffer."
6382   (interactive)
6383   (if (not gnus-zombie-list)
6384       (gnus-message 6 "No zombie groups")
6385     (let (gnus-group-list-mode)
6386       (funcall gnus-group-prepare-function
6387                gnus-level-zombie t gnus-level-zombie))
6388     (goto-char (point-min)))
6389   (gnus-group-position-point))
6390
6391 (defun gnus-group-list-active ()
6392   "List all groups that are available from the server(s)."
6393   (interactive)
6394   ;; First we make sure that we have really read the active file.
6395   (unless (gnus-read-active-file-p)
6396     (let ((gnus-read-active-file t))
6397       (gnus-read-active-file)))
6398   ;; Find all groups and sort them.
6399   (let ((groups
6400          (sort
6401           (let (list)
6402             (mapatoms
6403              (lambda (sym)
6404                (and (boundp sym)
6405                     (symbol-value sym)
6406                     (setq list (cons (symbol-name sym) list))))
6407              gnus-active-hashtb)
6408             list)
6409           'string<))
6410         (buffer-read-only nil))
6411     (erase-buffer)
6412     (while groups
6413       (gnus-group-insert-group-line-info (pop groups)))
6414     (goto-char (point-min))))
6415
6416 (defun gnus-activate-all-groups (level)
6417   "Activate absolutely all groups."
6418   (interactive (list 7))
6419   (let ((gnus-activate-level level)
6420         (gnus-activate-foreign-newsgroups level))
6421     (gnus-group-get-new-news)))
6422
6423 (defun gnus-group-get-new-news (&optional arg)
6424   "Get newly arrived articles.
6425 If ARG is a number, it specifies which levels you are interested in
6426 re-scanning.  If ARG is non-nil and not a number, this will force
6427 \"hard\" re-reading of the active files from all servers."
6428   (interactive "P")
6429   (run-hooks 'gnus-get-new-news-hook)
6430   ;; We might read in new NoCeM messages here.
6431   (when (and gnus-use-nocem 
6432              (null arg))
6433     (gnus-nocem-scan-groups))
6434   ;; If ARG is not a number, then we read the active file.
6435   (when (and arg (not (numberp arg)))
6436     (let ((gnus-read-active-file t))
6437       (gnus-read-active-file))
6438     (setq arg nil))
6439
6440   (setq arg (gnus-group-default-level arg t))
6441   (if (and gnus-read-active-file (not arg))
6442       (progn
6443         (gnus-read-active-file)
6444         (gnus-get-unread-articles arg))
6445     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6446       (gnus-get-unread-articles arg)))
6447   (run-hooks 'gnus-after-getting-new-news-hook)
6448   (gnus-group-list-groups))
6449
6450 (defun gnus-group-get-new-news-this-group (&optional n)
6451   "Check for newly arrived news in the current group (and the N-1 next groups).
6452 The difference between N and the number of newsgroup checked is returned.
6453 If N is negative, this group and the N-1 previous groups will be checked."
6454   (interactive "P")
6455   (let* ((groups (gnus-group-process-prefix n))
6456          (ret (if (numberp n) (- n (length groups)) 0))
6457          (beg (unless n (point)))
6458          group)
6459     (while (setq group (pop groups))
6460       (gnus-group-remove-mark group)
6461       (if (gnus-activate-group group 'scan)
6462           (progn
6463             (gnus-get-unread-articles-in-group
6464              (gnus-get-info group) (gnus-active group) t)
6465             (unless (gnus-virtual-group-p group)
6466               (gnus-close-group group))
6467             (gnus-group-update-group group))
6468         (gnus-error 3 "%s error: %s" group (gnus-status-message group))))
6469     (when beg (goto-char beg))
6470     (when gnus-goto-next-group-when-activating
6471       (gnus-group-next-unread-group 1 t))
6472     (gnus-summary-position-point)
6473     ret))
6474
6475 (defun gnus-group-fetch-faq (group &optional faq-dir)
6476   "Fetch the FAQ for the current group."
6477   (interactive
6478    (list
6479     (gnus-group-real-name (gnus-group-group-name))
6480     (cond (current-prefix-arg
6481            (completing-read
6482             "Faq dir: " (and (listp gnus-group-faq-directory)
6483                              gnus-group-faq-directory))))))
6484   (or faq-dir
6485       (setq faq-dir (if (listp gnus-group-faq-directory)
6486                         (car gnus-group-faq-directory)
6487                       gnus-group-faq-directory)))
6488   (or group (error "No group name given"))
6489   (let ((file (concat (file-name-as-directory faq-dir)
6490                       (gnus-group-real-name group))))
6491     (if (not (file-exists-p file))
6492         (error "No such file: %s" file)
6493       (find-file file))))
6494
6495 (defun gnus-group-describe-group (force &optional group)
6496   "Display a description of the current newsgroup."
6497   (interactive (list current-prefix-arg (gnus-group-group-name)))
6498   (and force (setq gnus-description-hashtb nil))
6499   (let ((method (gnus-find-method-for-group group))
6500         desc)
6501     (or group (error "No group name given"))
6502     (and (or (and gnus-description-hashtb
6503                   ;; We check whether this group's method has been
6504                   ;; queried for a description file.
6505                   (gnus-gethash
6506                    (gnus-group-prefixed-name "" method)
6507                    gnus-description-hashtb))
6508              (setq desc (gnus-group-get-description group))
6509              (gnus-read-descriptions-file method))
6510          (gnus-message 1
6511           (or desc (gnus-gethash group gnus-description-hashtb)
6512               "No description available")))))
6513
6514 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6515 (defun gnus-group-describe-all-groups (&optional force)
6516   "Pop up a buffer with descriptions of all newsgroups."
6517   (interactive "P")
6518   (and force (setq gnus-description-hashtb nil))
6519   (if (not (or gnus-description-hashtb
6520                (gnus-read-all-descriptions-files)))
6521       (error "Couldn't request descriptions file"))
6522   (let ((buffer-read-only nil)
6523         b)
6524     (erase-buffer)
6525     (mapatoms
6526      (lambda (group)
6527        (setq b (point))
6528        (insert (format "      *: %-20s %s\n" (symbol-name group)
6529                        (symbol-value group)))
6530        (gnus-add-text-properties
6531         b (1+ b) (list 'gnus-group group
6532                        'gnus-unread t 'gnus-marked nil
6533                        'gnus-level (1+ gnus-level-subscribed))))
6534      gnus-description-hashtb)
6535     (goto-char (point-min))
6536     (gnus-group-position-point)))
6537
6538 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6539 (defun gnus-group-apropos (regexp &optional search-description)
6540   "List all newsgroups that have names that match a regexp."
6541   (interactive "sGnus apropos (regexp): ")
6542   (let ((prev "")
6543         (obuf (current-buffer))
6544         groups des)
6545     ;; Go through all newsgroups that are known to Gnus.
6546     (mapatoms
6547      (lambda (group)
6548        (and (symbol-name group)
6549             (string-match regexp (symbol-name group))
6550             (setq groups (cons (symbol-name group) groups))))
6551      gnus-active-hashtb)
6552     ;; Also go through all descriptions that are known to Gnus.
6553     (when search-description
6554       (mapatoms
6555        (lambda (group)
6556          (and (string-match regexp (symbol-value group))
6557               (gnus-active (symbol-name group))
6558               (setq groups (cons (symbol-name group) groups))))
6559        gnus-description-hashtb))
6560     (if (not groups)
6561         (gnus-message 3 "No groups matched \"%s\"." regexp)
6562       ;; Print out all the groups.
6563       (save-excursion
6564         (pop-to-buffer "*Gnus Help*")
6565         (buffer-disable-undo (current-buffer))
6566         (erase-buffer)
6567         (setq groups (sort groups 'string<))
6568         (while groups
6569           ;; Groups may be entered twice into the list of groups.
6570           (if (not (string= (car groups) prev))
6571               (progn
6572                 (insert (setq prev (car groups)) "\n")
6573                 (if (and gnus-description-hashtb
6574                          (setq des (gnus-gethash (car groups)
6575                                                  gnus-description-hashtb)))
6576                     (insert "  " des "\n"))))
6577           (setq groups (cdr groups)))
6578         (goto-char (point-min))))
6579     (pop-to-buffer obuf)))
6580
6581 (defun gnus-group-description-apropos (regexp)
6582   "List all newsgroups that have names or descriptions that match a regexp."
6583   (interactive "sGnus description apropos (regexp): ")
6584   (if (not (or gnus-description-hashtb
6585                (gnus-read-all-descriptions-files)))
6586       (error "Couldn't request descriptions file"))
6587   (gnus-group-apropos regexp t))
6588
6589 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6590 (defun gnus-group-list-matching (level regexp &optional all lowest)
6591   "List all groups with unread articles that match REGEXP.
6592 If the prefix LEVEL is non-nil, it should be a number that says which
6593 level to cut off listing groups.
6594 If ALL, also list groups with no unread articles.
6595 If LOWEST, don't list groups with level lower than LOWEST.
6596
6597 This command may read the active file."
6598   (interactive "P\nsList newsgroups matching: ")
6599   ;; First make sure active file has been read.
6600   (when (and level
6601              (>= (prefix-numeric-value level) gnus-level-killed))
6602     (gnus-get-killed-groups))
6603   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6604                            all (or lowest 1) regexp)
6605   (goto-char (point-min))
6606   (gnus-group-position-point))
6607
6608 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6609   "List all groups that match REGEXP.
6610 If the prefix LEVEL is non-nil, it should be a number that says which
6611 level to cut off listing groups.
6612 If LOWEST, don't list groups with level lower than LOWEST."
6613   (interactive "P\nsList newsgroups matching: ")
6614   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6615
6616 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6617 (defun gnus-group-save-newsrc (&optional force)
6618   "Save the Gnus startup files.
6619 If FORCE, force saving whether it is necessary or not."
6620   (interactive "P")
6621   (gnus-save-newsrc-file force))
6622
6623 (defun gnus-group-restart (&optional arg)
6624   "Force Gnus to read the .newsrc file."
6625   (interactive "P")
6626   (when (gnus-yes-or-no-p
6627          (format "Are you sure you want to read %s? "
6628                  gnus-current-startup-file))
6629     (gnus-save-newsrc-file)
6630     (gnus-setup-news 'force)
6631     (gnus-group-list-groups arg)))
6632
6633 (defun gnus-group-read-init-file ()
6634   "Read the Gnus elisp init file."
6635   (interactive)
6636   (gnus-read-init-file))
6637
6638 (defun gnus-group-check-bogus-groups (&optional silent)
6639   "Check bogus newsgroups.
6640 If given a prefix, don't ask for confirmation before removing a bogus
6641 group."
6642   (interactive "P")
6643   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6644   (gnus-group-list-groups))
6645
6646 (defun gnus-group-edit-global-kill (&optional article group)
6647   "Edit the global kill file.
6648 If GROUP, edit that local kill file instead."
6649   (interactive "P")
6650   (setq gnus-current-kill-article article)
6651   (gnus-kill-file-edit-file group)
6652   (gnus-message
6653    6
6654    (substitute-command-keys
6655     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6656             (if group "local" "global")))))
6657
6658 (defun gnus-group-edit-local-kill (article group)
6659   "Edit a local kill file."
6660   (interactive (list nil (gnus-group-group-name)))
6661   (gnus-group-edit-global-kill article group))
6662
6663 (defun gnus-group-force-update ()
6664   "Update `.newsrc' file."
6665   (interactive)
6666   (gnus-save-newsrc-file))
6667
6668 (defun gnus-group-suspend ()
6669   "Suspend the current Gnus session.
6670 In fact, cleanup buffers except for group mode buffer.
6671 The hook gnus-suspend-gnus-hook is called before actually suspending."
6672   (interactive)
6673   (run-hooks 'gnus-suspend-gnus-hook)
6674   ;; Kill Gnus buffers except for group mode buffer.
6675   (let ((group-buf (get-buffer gnus-group-buffer)))
6676     ;; Do this on a separate list in case the user does a ^G before we finish
6677     (let ((gnus-buffer-list
6678            (delq group-buf (delq gnus-dribble-buffer
6679                                  (append gnus-buffer-list nil)))))
6680       (while gnus-buffer-list
6681         (gnus-kill-buffer (car gnus-buffer-list))
6682         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6683     (if group-buf
6684         (progn
6685           (setq gnus-buffer-list (list group-buf))
6686           (bury-buffer group-buf)
6687           (delete-windows-on group-buf t)))))
6688
6689 (defun gnus-group-clear-dribble ()
6690   "Clear all information from the dribble buffer."
6691   (interactive)
6692   (gnus-dribble-clear)
6693   (gnus-message 7 "Cleared dribble buffer"))
6694
6695 (defun gnus-group-exit ()
6696   "Quit reading news after updating .newsrc.eld and .newsrc.
6697 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6698   (interactive)
6699   (when 
6700       (or noninteractive                ;For gnus-batch-kill
6701           (not gnus-interactive-exit)   ;Without confirmation
6702           gnus-expert-user
6703           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6704     (run-hooks 'gnus-exit-gnus-hook)
6705     ;; Offer to save data from non-quitted summary buffers.
6706     (gnus-offer-save-summaries)
6707     ;; Save the newsrc file(s).
6708     (gnus-save-newsrc-file)
6709     ;; Kill-em-all.
6710     (gnus-close-backends)
6711     ;; Reset everything.
6712     (gnus-clear-system)
6713     ;; Allow the user to do things after cleaning up.
6714     (run-hooks 'gnus-after-exiting-gnus-hook)))
6715
6716 (defun gnus-close-backends ()
6717   ;; Send a close request to all backends that support such a request.
6718   (let ((methods gnus-valid-select-methods)
6719         func)
6720     (while methods
6721       (if (fboundp (setq func (intern (concat (caar methods)
6722                                               "-request-close"))))
6723           (funcall func))
6724       (setq methods (cdr methods)))))
6725
6726 (defun gnus-group-quit ()
6727   "Quit reading news without updating .newsrc.eld or .newsrc.
6728 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6729   (interactive)
6730   (when (or noninteractive              ;For gnus-batch-kill
6731             (zerop (buffer-size))
6732             (not (gnus-server-opened gnus-select-method))
6733             gnus-expert-user
6734             (not gnus-current-startup-file)
6735             (gnus-yes-or-no-p
6736              (format "Quit reading news without saving %s? "
6737                      (file-name-nondirectory gnus-current-startup-file))))
6738     (run-hooks 'gnus-exit-gnus-hook)
6739     (if gnus-use-full-window
6740         (delete-other-windows)
6741       (gnus-remove-some-windows))
6742     (gnus-dribble-save)
6743     (gnus-close-backends)
6744     (gnus-clear-system)
6745     ;; Allow the user to do things after cleaning up.
6746     (run-hooks 'gnus-after-exiting-gnus-hook)))
6747
6748 (defun gnus-offer-save-summaries ()
6749   "Offer to save all active summary buffers."
6750   (save-excursion
6751     (let ((buflist (buffer-list))
6752           buffers bufname)
6753       ;; Go through all buffers and find all summaries.
6754       (while buflist
6755         (and (setq bufname (buffer-name (car buflist)))
6756              (string-match "Summary" bufname)
6757              (save-excursion
6758                (set-buffer bufname)
6759                ;; We check that this is, indeed, a summary buffer.
6760                (and (eq major-mode 'gnus-summary-mode)
6761                     ;; Also make sure this isn't bogus.
6762                     gnus-newsgroup-prepared))
6763              (push bufname buffers))
6764         (setq buflist (cdr buflist)))
6765       ;; Go through all these summary buffers and offer to save them.
6766       (when buffers
6767         (map-y-or-n-p
6768          "Update summary buffer %s? "
6769          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6770          buffers)))))
6771
6772 (defun gnus-group-describe-briefly ()
6773   "Give a one line description of the group mode commands."
6774   (interactive)
6775   (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")))
6776
6777 (defun gnus-group-browse-foreign-server (method)
6778   "Browse a foreign news server.
6779 If called interactively, this function will ask for a select method
6780  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6781 If not, METHOD should be a list where the first element is the method
6782 and the second element is the address."
6783   (interactive
6784    (list (let ((how (completing-read
6785                      "Which backend: "
6786                      (append gnus-valid-select-methods gnus-server-alist)
6787                      nil t (cons "nntp" 0) 'gnus-method-history)))
6788            ;; We either got a backend name or a virtual server name.
6789            ;; If the first, we also need an address.
6790            (if (assoc how gnus-valid-select-methods)
6791                (list (intern how)
6792                      ;; Suggested by mapjph@bath.ac.uk.
6793                      (completing-read
6794                       "Address: "
6795                       (mapcar (lambda (server) (list server))
6796                               gnus-secondary-servers)))
6797              ;; We got a server name, so we find the method.
6798              (gnus-server-to-method how)))))
6799   (gnus-browse-foreign-server method))
6800
6801 \f
6802 ;;;
6803 ;;; Gnus summary mode
6804 ;;;
6805
6806 (defvar gnus-summary-mode-map nil)
6807
6808 (put 'gnus-summary-mode 'mode-class 'special)
6809
6810 (unless gnus-summary-mode-map
6811   (setq gnus-summary-mode-map (make-keymap))
6812   (suppress-keymap gnus-summary-mode-map)
6813
6814   ;; Non-orthogonal keys
6815
6816   (gnus-define-keys gnus-summary-mode-map
6817     " " gnus-summary-next-page
6818     "\177" gnus-summary-prev-page
6819     [delete] gnus-summary-prev-page
6820     "\r" gnus-summary-scroll-up
6821     "n" gnus-summary-next-unread-article
6822     "p" gnus-summary-prev-unread-article
6823     "N" gnus-summary-next-article
6824     "P" gnus-summary-prev-article
6825     "\M-\C-n" gnus-summary-next-same-subject
6826     "\M-\C-p" gnus-summary-prev-same-subject
6827     "\M-n" gnus-summary-next-unread-subject
6828     "\M-p" gnus-summary-prev-unread-subject
6829     "." gnus-summary-first-unread-article
6830     "," gnus-summary-best-unread-article
6831     "\M-s" gnus-summary-search-article-forward
6832     "\M-r" gnus-summary-search-article-backward
6833     "<" gnus-summary-beginning-of-article
6834     ">" gnus-summary-end-of-article
6835     "j" gnus-summary-goto-article
6836     "^" gnus-summary-refer-parent-article
6837     "\M-^" gnus-summary-refer-article
6838     "u" gnus-summary-tick-article-forward
6839     "!" gnus-summary-tick-article-forward
6840     "U" gnus-summary-tick-article-backward
6841     "d" gnus-summary-mark-as-read-forward
6842     "D" gnus-summary-mark-as-read-backward
6843     "E" gnus-summary-mark-as-expirable
6844     "\M-u" gnus-summary-clear-mark-forward
6845     "\M-U" gnus-summary-clear-mark-backward
6846     "k" gnus-summary-kill-same-subject-and-select
6847     "\C-k" gnus-summary-kill-same-subject
6848     "\M-\C-k" gnus-summary-kill-thread
6849     "\M-\C-l" gnus-summary-lower-thread
6850     "e" gnus-summary-edit-article
6851     "#" gnus-summary-mark-as-processable
6852     "\M-#" gnus-summary-unmark-as-processable
6853     "\M-\C-t" gnus-summary-toggle-threads
6854     "\M-\C-s" gnus-summary-show-thread
6855     "\M-\C-h" gnus-summary-hide-thread
6856     "\M-\C-f" gnus-summary-next-thread
6857     "\M-\C-b" gnus-summary-prev-thread
6858     "\M-\C-u" gnus-summary-up-thread
6859     "\M-\C-d" gnus-summary-down-thread
6860     "&" gnus-summary-execute-command
6861     "c" gnus-summary-catchup-and-exit
6862     "\C-w" gnus-summary-mark-region-as-read
6863     "\C-t" gnus-summary-toggle-truncation
6864     "?" gnus-summary-mark-as-dormant
6865     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6866     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6867     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6868     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6869     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6870     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6871     "=" gnus-summary-expand-window
6872     "\C-x\C-s" gnus-summary-reselect-current-group
6873     "\M-g" gnus-summary-rescan-group
6874     "w" gnus-summary-stop-page-breaking
6875     "\C-c\C-r" gnus-summary-caesar-message
6876     "\M-t" gnus-summary-toggle-mime
6877     "f" gnus-summary-followup
6878     "F" gnus-summary-followup-with-original
6879     "C" gnus-summary-cancel-article
6880     "r" gnus-summary-reply
6881     "R" gnus-summary-reply-with-original
6882     "\C-c\C-f" gnus-summary-mail-forward
6883     "o" gnus-summary-save-article
6884     "\C-o" gnus-summary-save-article-mail
6885     "|" gnus-summary-pipe-output
6886     "\M-k" gnus-summary-edit-local-kill
6887     "\M-K" gnus-summary-edit-global-kill
6888     "V" gnus-version
6889     "\C-c\C-d" gnus-summary-describe-group
6890     "q" gnus-summary-exit
6891     "Q" gnus-summary-exit-no-update
6892     "\C-c\C-i" gnus-info-find-node
6893     gnus-mouse-2 gnus-mouse-pick-article
6894     "m" gnus-summary-mail-other-window
6895     "a" gnus-summary-post-news
6896     "x" gnus-summary-limit-to-unread
6897     "s" gnus-summary-isearch-article
6898     "t" gnus-article-hide-headers
6899     "g" gnus-summary-show-article
6900     "l" gnus-summary-goto-last-article
6901     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6902     "\C-d" gnus-summary-enter-digest-group
6903     "\C-c\C-b" gnus-bug
6904     "*" gnus-cache-enter-article
6905     "\M-*" gnus-cache-remove-article
6906     "\M-&" gnus-summary-universal-argument
6907     "\C-l" gnus-recenter
6908     "I" gnus-summary-increase-score
6909     "L" gnus-summary-lower-score
6910
6911     "V" gnus-summary-score-map
6912     "X" gnus-uu-extract-map
6913     "S" gnus-summary-send-map)
6914
6915   ;; Sort of orthogonal keymap
6916   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6917     "t" gnus-summary-tick-article-forward
6918     "!" gnus-summary-tick-article-forward
6919     "d" gnus-summary-mark-as-read-forward
6920     "r" gnus-summary-mark-as-read-forward
6921     "c" gnus-summary-clear-mark-forward
6922     " " gnus-summary-clear-mark-forward
6923     "e" gnus-summary-mark-as-expirable
6924     "x" gnus-summary-mark-as-expirable
6925     "?" gnus-summary-mark-as-dormant
6926     "b" gnus-summary-set-bookmark
6927     "B" gnus-summary-remove-bookmark
6928     "#" gnus-summary-mark-as-processable
6929     "\M-#" gnus-summary-unmark-as-processable
6930     "S" gnus-summary-limit-include-expunged
6931     "C" gnus-summary-catchup
6932     "H" gnus-summary-catchup-to-here
6933     "\C-c" gnus-summary-catchup-all
6934     "k" gnus-summary-kill-same-subject-and-select
6935     "K" gnus-summary-kill-same-subject
6936     "P" gnus-uu-mark-map)
6937
6938   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6939     "c" gnus-summary-clear-above
6940     "u" gnus-summary-tick-above
6941     "m" gnus-summary-mark-above
6942     "k" gnus-summary-kill-below)
6943
6944   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6945     "/" gnus-summary-limit-to-subject
6946     "n" gnus-summary-limit-to-articles
6947     "w" gnus-summary-pop-limit
6948     "s" gnus-summary-limit-to-subject
6949     "a" gnus-summary-limit-to-author
6950     "u" gnus-summary-limit-to-unread
6951     "m" gnus-summary-limit-to-marks
6952     "v" gnus-summary-limit-to-score
6953     "D" gnus-summary-limit-include-dormant
6954     "d" gnus-summary-limit-exclude-dormant
6955     ;;  "t" gnus-summary-limit-exclude-thread
6956     "E" gnus-summary-limit-include-expunged
6957     "c" gnus-summary-limit-exclude-childless-dormant
6958     "C" gnus-summary-limit-mark-excluded-as-read)
6959
6960   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6961     "n" gnus-summary-next-unread-article
6962     "p" gnus-summary-prev-unread-article
6963     "N" gnus-summary-next-article
6964     "P" gnus-summary-prev-article
6965     "\C-n" gnus-summary-next-same-subject
6966     "\C-p" gnus-summary-prev-same-subject
6967     "\M-n" gnus-summary-next-unread-subject
6968     "\M-p" gnus-summary-prev-unread-subject
6969     "f" gnus-summary-first-unread-article
6970     "b" gnus-summary-best-unread-article
6971     "j" gnus-summary-goto-article
6972     "g" gnus-summary-goto-subject
6973     "l" gnus-summary-goto-last-article
6974     "p" gnus-summary-pop-article)
6975
6976   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6977     "k" gnus-summary-kill-thread
6978     "l" gnus-summary-lower-thread
6979     "i" gnus-summary-raise-thread
6980     "T" gnus-summary-toggle-threads
6981     "t" gnus-summary-rethread-current
6982     "^" gnus-summary-reparent-thread
6983     "s" gnus-summary-show-thread
6984     "S" gnus-summary-show-all-threads
6985     "h" gnus-summary-hide-thread
6986     "H" gnus-summary-hide-all-threads
6987     "n" gnus-summary-next-thread
6988     "p" gnus-summary-prev-thread
6989     "u" gnus-summary-up-thread
6990     "o" gnus-summary-top-thread
6991     "d" gnus-summary-down-thread
6992     "#" gnus-uu-mark-thread
6993     "\M-#" gnus-uu-unmark-thread)
6994
6995   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6996     "c" gnus-summary-catchup-and-exit
6997     "C" gnus-summary-catchup-all-and-exit
6998     "E" gnus-summary-exit-no-update
6999     "Q" gnus-summary-exit
7000     "Z" gnus-summary-exit
7001     "n" gnus-summary-catchup-and-goto-next-group
7002     "R" gnus-summary-reselect-current-group
7003     "G" gnus-summary-rescan-group
7004     "N" gnus-summary-next-group
7005     "P" gnus-summary-prev-group)
7006
7007   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
7008     " " gnus-summary-next-page
7009     "n" gnus-summary-next-page
7010     "\177" gnus-summary-prev-page
7011     [delete] gnus-summary-prev-page
7012     "p" gnus-summary-prev-page
7013     "\r" gnus-summary-scroll-up
7014     "<" gnus-summary-beginning-of-article
7015     ">" gnus-summary-end-of-article
7016     "b" gnus-summary-beginning-of-article
7017     "e" gnus-summary-end-of-article
7018     "^" gnus-summary-refer-parent-article
7019     "r" gnus-summary-refer-parent-article
7020     "R" gnus-summary-refer-references
7021     "g" gnus-summary-show-article
7022     "s" gnus-summary-isearch-article)
7023
7024   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
7025     "b" gnus-article-add-buttons
7026     "B" gnus-article-add-buttons-to-head
7027     "o" gnus-article-treat-overstrike
7028     ;;  "w" gnus-article-word-wrap
7029     "w" gnus-article-fill-cited-article
7030     "c" gnus-article-remove-cr
7031     "L" gnus-article-remove-trailing-blank-lines
7032     "q" gnus-article-de-quoted-unreadable
7033     "f" gnus-article-display-x-face
7034     "l" gnus-summary-stop-page-breaking
7035     "r" gnus-summary-caesar-message
7036     "t" gnus-article-hide-headers
7037     "v" gnus-summary-verbose-headers
7038     "m" gnus-summary-toggle-mime)
7039
7040   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
7041     "a" gnus-article-hide
7042     "h" gnus-article-hide-headers
7043     "b" gnus-article-hide-boring-headers
7044     "s" gnus-article-hide-signature
7045     "c" gnus-article-hide-citation
7046     "p" gnus-article-hide-pgp
7047     "\C-c" gnus-article-hide-citation-maybe)
7048
7049   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
7050     "a" gnus-article-highlight
7051     "h" gnus-article-highlight-headers
7052     "c" gnus-article-highlight-citation
7053     "s" gnus-article-highlight-signature)
7054
7055   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
7056     "z" gnus-article-date-ut
7057     "u" gnus-article-date-ut
7058     "l" gnus-article-date-local
7059     "e" gnus-article-date-lapsed
7060     "o" gnus-article-date-original)
7061
7062   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
7063     "v" gnus-version
7064     "f" gnus-summary-fetch-faq
7065     "d" gnus-summary-describe-group
7066     "h" gnus-summary-describe-briefly
7067     "i" gnus-info-find-node)
7068
7069   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
7070     "e" gnus-summary-expire-articles
7071     "\M-\C-e" gnus-summary-expire-articles-now
7072     "\177" gnus-summary-delete-article
7073     [delete] gnus-summary-delete-article
7074     "m" gnus-summary-move-article
7075     "r" gnus-summary-respool-article
7076     "w" gnus-summary-edit-article
7077     "c" gnus-summary-copy-article
7078     "B" gnus-summary-crosspost-article
7079     "q" gnus-summary-respool-query
7080     "i" gnus-summary-import-article)
7081
7082   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
7083     "o" gnus-summary-save-article
7084     "m" gnus-summary-save-article-mail
7085     "r" gnus-summary-save-article-rmail
7086     "f" gnus-summary-save-article-file
7087     "b" gnus-summary-save-article-body-file
7088     "h" gnus-summary-save-article-folder
7089     "v" gnus-summary-save-article-vm
7090     "p" gnus-summary-pipe-output
7091     "s" gnus-soup-add-article)
7092   )
7093
7094 \f
7095
7096 (defun gnus-summary-mode (&optional group)
7097   "Major mode for reading articles.
7098
7099 All normal editing commands are switched off.
7100 \\<gnus-summary-mode-map>
7101 Each line in this buffer represents one article.  To read an
7102 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
7103 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
7104 respectively.
7105
7106 You can also post articles and send mail from this buffer.  To
7107 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
7108 of an article, type `\\[gnus-summary-reply]'.
7109
7110 There are approx. one gazillion commands you can execute in this
7111 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
7112
7113 The following commands are available:
7114
7115 \\{gnus-summary-mode-map}"
7116   (interactive)
7117   (when (and menu-bar-mode
7118              (gnus-visual-p 'summary-menu 'menu))
7119     (gnus-summary-make-menu-bar))
7120   (kill-all-local-variables)
7121   (let ((locals gnus-summary-local-variables))
7122     (while locals
7123       (if (consp (car locals))
7124           (progn
7125             (make-local-variable (caar locals))
7126             (set (caar locals) (eval (cdar locals))))
7127         (make-local-variable (car locals))
7128         (set (car locals) nil))
7129       (setq locals (cdr locals))))
7130   (gnus-make-thread-indent-array)
7131   (gnus-simplify-mode-line)
7132   (setq major-mode 'gnus-summary-mode)
7133   (setq mode-name "Summary")
7134   (make-local-variable 'minor-mode-alist)
7135   (use-local-map gnus-summary-mode-map)
7136   (buffer-disable-undo (current-buffer))
7137   (setq buffer-read-only t)             ;Disable modification
7138   (setq truncate-lines t)
7139   (setq selective-display t)
7140   (setq selective-display-ellipses t)   ;Display `...'
7141   (setq buffer-display-table gnus-summary-display-table)
7142   (setq gnus-newsgroup-name group)
7143   (make-local-variable 'gnus-summary-line-format)
7144   (make-local-variable 'gnus-summary-line-format-spec)
7145   (make-local-variable 'gnus-summary-mark-positions)
7146   (run-hooks 'gnus-summary-mode-hook))
7147
7148 (defun gnus-summary-make-display-table ()
7149   ;; Change the display table.  Odd characters have a tendency to mess
7150   ;; up nicely formatted displays - we make all possible glyphs
7151   ;; display only a single character.
7152
7153   ;; We start from the standard display table, if any.
7154   (setq gnus-summary-display-table
7155         (or (copy-sequence standard-display-table)
7156             (make-display-table)))
7157   ;; Nix out all the control chars...
7158   (let ((i 32))
7159     (while (>= (setq i (1- i)) 0)
7160       (aset gnus-summary-display-table i [??])))
7161   ;; ... but not newline and cr, of course. (cr is necessary for the
7162   ;; selective display).
7163   (aset gnus-summary-display-table ?\n nil)
7164   (aset gnus-summary-display-table ?\r nil)
7165   ;; We nix out any glyphs over 126 that are not set already.
7166   (let ((i 256))
7167     (while (>= (setq i (1- i)) 127)
7168       ;; Only modify if the entry is nil.
7169       (or (aref gnus-summary-display-table i)
7170           (aset gnus-summary-display-table i [??])))))
7171
7172 (defun gnus-summary-clear-local-variables ()
7173   (let ((locals gnus-summary-local-variables))
7174     (while locals
7175       (if (consp (car locals))
7176           (and (vectorp (caar locals))
7177                (set (caar locals) nil))
7178         (and (vectorp (car locals))
7179              (set (car locals) nil)))
7180       (setq locals (cdr locals)))))
7181
7182 ;; Summary data functions.
7183
7184 (defmacro gnus-data-number (data)
7185   `(car ,data))
7186
7187 (defmacro gnus-data-set-number (data number)
7188   `(setcar ,data ,number))
7189
7190 (defmacro gnus-data-mark (data)
7191   `(nth 1 ,data))
7192
7193 (defmacro gnus-data-set-mark (data mark)
7194   `(setcar (nthcdr 1 ,data) ,mark))
7195
7196 (defmacro gnus-data-pos (data)
7197   `(nth 2 ,data))
7198
7199 (defmacro gnus-data-set-pos (data pos)
7200   `(setcar (nthcdr 2 ,data) ,pos))
7201
7202 (defmacro gnus-data-header (data)
7203   `(nth 3 ,data))
7204
7205 (defmacro gnus-data-level (data)
7206   `(nth 4 ,data))
7207
7208 (defmacro gnus-data-unread-p (data)
7209   `(= (nth 1 ,data) gnus-unread-mark))
7210
7211 (defmacro gnus-data-pseudo-p (data)
7212   `(consp (nth 3 ,data)))
7213
7214 (defmacro gnus-data-find (number)
7215   `(assq ,number gnus-newsgroup-data))
7216
7217 (defmacro gnus-data-find-list (number &optional data)
7218   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7219      (memq (assq ,number bdata)
7220            bdata)))
7221
7222 (defmacro gnus-data-make (number mark pos header level)
7223   `(list ,number ,mark ,pos ,header ,level))
7224
7225 (defun gnus-data-enter (after-article number mark pos header level offset)
7226   (let ((data (gnus-data-find-list after-article)))
7227     (or data (error "No such article: %d" after-article))
7228     (setcdr data (cons (gnus-data-make number mark pos header level)
7229                        (cdr data)))
7230     (setq gnus-newsgroup-data-reverse nil)
7231     (gnus-data-update-list (cddr data) offset)))
7232
7233 (defun gnus-data-enter-list (after-article list &optional offset)
7234   (when list
7235     (let ((data (and after-article (gnus-data-find-list after-article)))
7236           (ilist list))
7237       (or data (not after-article) (error "No such article: %d" after-article))
7238       ;; Find the last element in the list to be spliced into the main
7239       ;; list.
7240       (while (cdr list)
7241         (setq list (cdr list)))
7242       (if (not data)
7243           (progn
7244             (setcdr list gnus-newsgroup-data)
7245             (setq gnus-newsgroup-data ilist)
7246             (and offset (gnus-data-update-list (cdr list) offset)))
7247         (setcdr list (cdr data))
7248         (setcdr data ilist)
7249         (and offset (gnus-data-update-list (cdr data) offset)))
7250       (setq gnus-newsgroup-data-reverse nil))))
7251
7252 (defun gnus-data-remove (article &optional offset)
7253   (let ((data gnus-newsgroup-data))
7254     (if (= (gnus-data-number (car data)) article)
7255         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7256               gnus-newsgroup-data-reverse nil)
7257       (while (cdr data)
7258         (and (= (gnus-data-number (cadr data)) article)
7259              (progn
7260                (setcdr data (cddr data))
7261                (and offset (gnus-data-update-list (cdr data) offset))
7262                (setq data nil
7263                      gnus-newsgroup-data-reverse nil)))
7264         (setq data (cdr data))))))
7265
7266 (defmacro gnus-data-list (backward)
7267   `(if ,backward
7268        (or gnus-newsgroup-data-reverse
7269            (setq gnus-newsgroup-data-reverse
7270                  (reverse gnus-newsgroup-data)))
7271      gnus-newsgroup-data))
7272
7273 (defun gnus-data-update-list (data offset)
7274   "Add OFFSET to the POS of all data entries in DATA."
7275   (while data
7276     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7277     (setq data (cdr data))))
7278
7279 (defun gnus-data-compute-positions ()
7280   "Compute the positions of all articles."
7281   (let ((data gnus-newsgroup-data)
7282         pos)
7283     (while data
7284       (when (setq pos (text-property-any
7285                        (point-min) (point-max)
7286                        'gnus-number (gnus-data-number (car data))))
7287         (gnus-data-set-pos (car data) (+ pos 3)))
7288       (setq data (cdr data)))))
7289
7290 (defun gnus-summary-article-pseudo-p (article)
7291   "Say whether this article is a pseudo article or not."
7292   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7293
7294 (defun gnus-article-parent-p (number)
7295   "Say whether this article is a parent or not."
7296   (let ((data (gnus-data-find-list number)))
7297     (and (cdr data)                     ; There has to be an article after...
7298          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7299             (gnus-data-level (nth 1 data))))))
7300
7301 (defun gnus-article-children (number)
7302   "Return a list of all children to NUMBER."
7303   (let* ((data (gnus-data-find-list number))
7304          (level (gnus-data-level (car data)))
7305          children)
7306     (setq data (cdr data))
7307     (while (and data            
7308                 (= (gnus-data-level (car data)) (1+ level)))
7309       (push (gnus-data-number (car data)) children)
7310       (setq data (cdr data)))
7311     children))
7312
7313 (defmacro gnus-summary-skip-intangible ()
7314   "If the current article is intangible, then jump to a different article."
7315   '(let ((to (get-text-property (point) 'gnus-intangible)))
7316     (and to (gnus-summary-goto-subject to))))
7317
7318 (defmacro gnus-summary-article-intangible-p ()
7319   "Say whether this article is intangible or not."
7320   '(get-text-property (point) 'gnus-intangible))
7321
7322 ;; Some summary mode macros.
7323
7324 (defmacro gnus-summary-article-number ()
7325   "The article number of the article on the current line.
7326 If there isn's an article number here, then we return the current
7327 article number."
7328   '(progn
7329      (gnus-summary-skip-intangible)
7330      (or (get-text-property (point) 'gnus-number)
7331          (gnus-summary-last-subject))))
7332
7333 (defmacro gnus-summary-article-header (&optional number)
7334   `(gnus-data-header (gnus-data-find
7335                       ,(or number '(gnus-summary-article-number)))))
7336
7337 (defmacro gnus-summary-thread-level (&optional number)
7338   `(if (and (eq gnus-summary-make-false-root 'dummy)
7339             (get-text-property (point) 'gnus-intangible))
7340        0
7341      (gnus-data-level (gnus-data-find
7342                        ,(or number '(gnus-summary-article-number))))))
7343
7344 (defmacro gnus-summary-article-mark (&optional number)
7345   `(gnus-data-mark (gnus-data-find
7346                     ,(or number '(gnus-summary-article-number)))))
7347
7348 (defmacro gnus-summary-article-pos (&optional number)
7349   `(gnus-data-pos (gnus-data-find
7350                    ,(or number '(gnus-summary-article-number)))))
7351
7352 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7353 (defmacro gnus-summary-article-subject (&optional number)
7354   "Return current subject string or nil if nothing."
7355   `(let ((headers
7356           ,(if number
7357                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7358              '(gnus-data-header (assq (gnus-summary-article-number)
7359                                       gnus-newsgroup-data)))))
7360      (and headers
7361           (vectorp headers)
7362           (mail-header-subject headers))))
7363
7364 (defmacro gnus-summary-article-score (&optional number)
7365   "Return current article score."
7366   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7367                   gnus-newsgroup-scored))
7368        gnus-summary-default-score 0))
7369
7370 (defun gnus-summary-article-children (&optional number)
7371   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7372          (level (gnus-data-level (car data)))
7373          l children)
7374     (while (and (setq data (cdr data))
7375                 (> (setq l (gnus-data-level (car data))) level))
7376       (and (= (1+ level) l)
7377            (setq children (cons (gnus-data-number (car data))
7378                                 children))))
7379     (nreverse children)))
7380
7381 (defun gnus-summary-article-parent (&optional number)
7382   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7383                                     (gnus-data-list t)))
7384          (level (gnus-data-level (car data))))
7385     (if (zerop level)
7386         () ; This is a root.
7387       ;; We search until we find an article with a level less than
7388       ;; this one.  That function has to be the parent.
7389       (while (and (setq data (cdr data))
7390                   (not (< (gnus-data-level (car data)) level))))
7391       (and data (gnus-data-number (car data))))))
7392
7393 (defun gnus-unread-mark-p (mark)
7394   "Say whether MARK is the unread mark."
7395   (= mark gnus-unread-mark))
7396
7397 (defun gnus-read-mark-p (mark)
7398   "Say whether MARK is one of the marks that mark as read.
7399 This is all marks except unread, ticked, dormant, and expirable."
7400   (not (or (= mark gnus-unread-mark)
7401            (= mark gnus-ticked-mark)
7402            (= mark gnus-dormant-mark)
7403            (= mark gnus-expirable-mark))))
7404
7405 ;; Saving hidden threads.
7406
7407 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
7408 (put 'gnus-save-hidden-threads 'lisp-indent-hook 0)
7409
7410 (defmacro gnus-save-hidden-threads (&rest forms)
7411   "Save hidden threads, eval FORMS, and restore the hidden threads."
7412   (let ((config (make-symbol "config")))
7413     `(let ((,config (gnus-hidden-threads-configuration)))
7414        (unwind-protect
7415            (progn
7416              ,@forms)
7417          (gnus-restore-hidden-threads-configuration ,config)))))
7418
7419 (defun gnus-hidden-threads-configuration ()
7420   "Return the current hidden threads configuration."
7421   (save-excursion
7422     (let (config)
7423       (goto-char (point-min))
7424       (while (search-forward "\r" nil t)
7425         (push (1- (point)) config))
7426       config)))
7427
7428 (defun gnus-restore-hidden-threads-configuration (config)
7429   "Restore hidden threads configuration from CONFIG."
7430   (let (point buffer-read-only)
7431     (while (setq point (pop config))
7432       (when (and (< point (point-max))
7433                  (goto-char point)
7434                  (= (following-char) ?\n))
7435         (subst-char-in-region point (1+ point) ?\n ?\r)))))
7436
7437 ;; Various summary mode internalish functions.
7438
7439 (defun gnus-mouse-pick-article (e)
7440   (interactive "e")
7441   (mouse-set-point e)
7442   (gnus-summary-next-page nil t))
7443
7444 (defun gnus-summary-setup-buffer (group)
7445   "Initialize summary buffer."
7446   (let ((buffer (concat "*Summary " group "*")))
7447     (if (get-buffer buffer)
7448         (progn
7449           (set-buffer buffer)
7450           (setq gnus-summary-buffer (current-buffer))
7451           (not gnus-newsgroup-prepared))
7452       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7453       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7454       (gnus-add-current-to-buffer-list)
7455       (gnus-summary-mode group)
7456       (when gnus-carpal
7457         (gnus-carpal-setup-buffer 'summary))
7458       (unless gnus-single-article-buffer
7459         (make-local-variable 'gnus-article-buffer)
7460         (make-local-variable 'gnus-article-current)
7461         (make-local-variable 'gnus-original-article-buffer))
7462       (setq gnus-newsgroup-name group)
7463       t)))
7464
7465 (defun gnus-set-global-variables ()
7466   ;; Set the global equivalents of the summary buffer-local variables
7467   ;; to the latest values they had.  These reflect the summary buffer
7468   ;; that was in action when the last article was fetched.
7469   (when (eq major-mode 'gnus-summary-mode)
7470     (setq gnus-summary-buffer (current-buffer))
7471     (let ((name gnus-newsgroup-name)
7472           (marked gnus-newsgroup-marked)
7473           (unread gnus-newsgroup-unreads)
7474           (headers gnus-current-headers)
7475           (data gnus-newsgroup-data)
7476           (summary gnus-summary-buffer)
7477           (article-buffer gnus-article-buffer)
7478           (original gnus-original-article-buffer)
7479           (gac gnus-article-current)
7480           (score-file gnus-current-score-file))
7481       (save-excursion
7482         (set-buffer gnus-group-buffer)
7483         (setq gnus-newsgroup-name name)
7484         (setq gnus-newsgroup-marked marked)
7485         (setq gnus-newsgroup-unreads unread)
7486         (setq gnus-current-headers headers)
7487         (setq gnus-newsgroup-data data)
7488         (setq gnus-article-current gac)
7489         (setq gnus-summary-buffer summary)
7490         (setq gnus-article-buffer article-buffer)
7491         (setq gnus-original-article-buffer original)
7492         (setq gnus-current-score-file score-file)))))
7493
7494 (defun gnus-summary-last-article-p (&optional article)
7495   "Return whether ARTICLE is the last article in the buffer."
7496   (if (not (setq article (or article (gnus-summary-article-number))))
7497       t ; All non-existant numbers are the last article. :-)
7498     (not (cdr (gnus-data-find-list article)))))
7499
7500 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7501   "Insert a dummy root in the summary buffer."
7502   (beginning-of-line)
7503   (gnus-add-text-properties
7504    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7505    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7506
7507 (defun gnus-make-thread-indent-array ()
7508   (let ((n 200))
7509     (unless (and gnus-thread-indent-array
7510                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
7511       (setq gnus-thread-indent-array (make-vector 201 "")
7512             gnus-thread-indent-array-level gnus-thread-indent-level)
7513       (while (>= n 0)
7514         (aset gnus-thread-indent-array n
7515               (make-string (* n gnus-thread-indent-level) ? ))
7516         (setq n (1- n))))))
7517
7518 (defun gnus-summary-insert-line
7519   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7520                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7521                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7522   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7523          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7524          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7525          (gnus-tmp-score-char
7526           (if (or (null gnus-summary-default-score)
7527                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7528                       gnus-summary-zcore-fuzz)) ? 
7529             (if (< gnus-tmp-score gnus-summary-default-score)
7530                 gnus-score-below-mark gnus-score-over-mark)))
7531          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7532                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7533                                   gnus-cached-mark)
7534                                  (gnus-tmp-replied gnus-replied-mark)
7535                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7536                                   gnus-saved-mark)
7537                                  (t gnus-unread-mark)))
7538          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7539          (gnus-tmp-name
7540           (cond
7541            ((string-match "(.+)" gnus-tmp-from)
7542             (substring gnus-tmp-from
7543                        (1+ (match-beginning 0)) (1- (match-end 0))))
7544            ((string-match "<[^>]+> *$" gnus-tmp-from)
7545             (let ((beg (match-beginning 0)))
7546               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7547                        (substring gnus-tmp-from (1+ (match-beginning 0))
7548                                   (1- (match-end 0))))
7549                   (substring gnus-tmp-from 0 beg))))
7550            (t gnus-tmp-from)))
7551          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7552          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7553          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7554          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7555          (buffer-read-only nil))
7556     (when (string= gnus-tmp-name "")
7557       (setq gnus-tmp-name gnus-tmp-from))
7558     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7559     (gnus-put-text-property
7560      (point)
7561      (progn (eval gnus-summary-line-format-spec) (point))
7562      'gnus-number gnus-tmp-number)
7563     (when (gnus-visual-p 'summary-highlight 'highlight)
7564       (forward-line -1)
7565       (run-hooks 'gnus-summary-update-hook)
7566       (forward-line 1))))
7567
7568 (defun gnus-summary-update-line (&optional dont-update)
7569   ;; Update summary line after change.
7570   (when (and gnus-summary-default-score
7571              (not gnus-summary-inhibit-highlight))
7572     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7573            (article (gnus-summary-article-number))
7574            (score (gnus-summary-article-score article)))
7575       (unless dont-update
7576         (if (and gnus-summary-mark-below
7577                  (< (gnus-summary-article-score)
7578                     gnus-summary-mark-below))
7579             ;; This article has a low score, so we mark it as read.
7580             (when (memq article gnus-newsgroup-unreads)
7581               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7582           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7583             ;; This article was previously marked as read on account
7584             ;; of a low score, but now it has risen, so we mark it as
7585             ;; unread.
7586             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7587         (gnus-summary-update-mark
7588          (if (or (null gnus-summary-default-score)
7589                  (<= (abs (- score gnus-summary-default-score))
7590                      gnus-summary-zcore-fuzz)) ? 
7591            (if (< score gnus-summary-default-score)
7592                gnus-score-below-mark gnus-score-over-mark)) 'score))
7593       ;; Do visual highlighting.
7594       (when (gnus-visual-p 'summary-highlight 'highlight)
7595         (run-hooks 'gnus-summary-update-hook)))))
7596
7597 (defvar gnus-tmp-new-adopts nil)
7598
7599 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7600   ;; Sum up all elements (and sub-elements) in a list.
7601   (let* ((number
7602           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7603           (cond
7604            ((and (consp thread) (cdr thread))
7605             (apply
7606              '+ 1 (mapcar
7607                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7608            ((null thread)
7609             1)
7610            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7611             1)
7612            (t 1))))
7613     (when (and level (zerop level) gnus-tmp-new-adopts)
7614       (incf number
7615             (apply '+ (mapcar
7616                        'gnus-summary-number-of-articles-in-thread
7617                        gnus-tmp-new-adopts))))
7618     (if char
7619         (if (> number 1) gnus-not-empty-thread-mark
7620           gnus-empty-thread-mark)
7621       number)))
7622
7623 (defun gnus-summary-set-local-parameters (group)
7624  "Go through the local params of GROUP and set all variable specs in that list."
7625   (let ((params (gnus-info-params (gnus-get-info group)))
7626         elem)
7627     (while params
7628       (setq elem (car params)
7629             params (cdr params))
7630       (and (consp elem)                 ; Has to be a cons.
7631            (consp (cdr elem))           ; The cdr has to be a list.
7632            (symbolp (car elem))         ; Has to be a symbol in there.
7633            (not (memq (car elem) 
7634                       '(quit-config to-address to-list to-group)))
7635            (progn                       ; So we set it.
7636              (make-local-variable (car elem))
7637              (set (car elem) (eval (nth 1 elem))))))))
7638
7639 (defun gnus-summary-read-group (group &optional show-all no-article
7640                                       kill-buffer no-display)
7641   "Start reading news in newsgroup GROUP.
7642 If SHOW-ALL is non-nil, already read articles are also listed.
7643 If NO-ARTICLE is non-nil, no article is selected initially.
7644 If NO-DISPLAY, don't generate a summary buffer."
7645   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7646   (let* ((new-group (gnus-summary-setup-buffer group))
7647          (quit-config (gnus-group-quit-config group))
7648          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7649     (cond
7650      ;; This summary buffer exists already, so we just select it.
7651      ((not new-group)
7652       (gnus-set-global-variables)
7653       (when kill-buffer
7654         (gnus-kill-or-deaden-summary kill-buffer))
7655       (gnus-configure-windows 'summary 'force)
7656       (gnus-set-mode-line 'summary)
7657       (gnus-summary-position-point)
7658       (message "")
7659       t)
7660      ;; We couldn't select this group.
7661      ((null did-select)
7662       (when (and (eq major-mode 'gnus-summary-mode)
7663                  (not (equal (current-buffer) kill-buffer)))
7664         (kill-buffer (current-buffer))
7665         (if (not quit-config)
7666             (progn
7667               (set-buffer gnus-group-buffer)
7668               (gnus-group-jump-to-group group)
7669               (gnus-group-next-unread-group 1))
7670           (if (not (buffer-name (car quit-config)))
7671               (gnus-configure-windows 'group 'force)
7672             (set-buffer (car quit-config))
7673             (and (eq major-mode 'gnus-summary-mode)
7674                  (gnus-set-global-variables))
7675             (gnus-configure-windows (cdr quit-config)))))
7676       (gnus-message 3 "Can't select group")
7677       nil)
7678      ;; The user did a `C-g' while prompting for number of articles,
7679      ;; so we exit this group.
7680      ((eq did-select 'quit)
7681       (and (eq major-mode 'gnus-summary-mode)
7682            (not (equal (current-buffer) kill-buffer))
7683            (kill-buffer (current-buffer)))
7684       (when kill-buffer
7685         (gnus-kill-or-deaden-summary kill-buffer))
7686       (if (not quit-config)
7687           (progn
7688             (set-buffer gnus-group-buffer)
7689             (gnus-group-jump-to-group group)
7690             (gnus-group-next-unread-group 1)
7691             (gnus-configure-windows 'group 'force))
7692         (if (not (buffer-name (car quit-config)))
7693             (gnus-configure-windows 'group 'force)
7694           (set-buffer (car quit-config))
7695           (and (eq major-mode 'gnus-summary-mode)
7696                (gnus-set-global-variables))
7697           (gnus-configure-windows (cdr quit-config))))
7698       ;; Finally signal the quit.
7699       (signal 'quit nil))
7700      ;; The group was successfully selected.
7701      (t
7702       (gnus-set-global-variables)
7703       ;; Save the active value in effect when the group was entered.
7704       (setq gnus-newsgroup-active
7705             (gnus-copy-sequence
7706              (gnus-active gnus-newsgroup-name)))
7707       ;; You can change the summary buffer in some way with this hook.
7708       (run-hooks 'gnus-select-group-hook)
7709       ;; Set any local variables in the group parameters.
7710       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7711       (gnus-update-format-specifications)
7712       ;; Do score processing.
7713       (when gnus-use-scoring
7714         (gnus-possibly-score-headers))
7715       ;; Check whether to fill in the gaps in the threads.
7716       (when gnus-build-sparse-threads
7717         (gnus-build-sparse-threads))
7718       ;; Find the initial limit.
7719       (if gnus-show-threads
7720           (if show-all
7721               (let ((gnus-newsgroup-dormant nil))
7722                 (gnus-summary-initial-limit show-all))
7723             (gnus-summary-initial-limit show-all))
7724         (setq gnus-newsgroup-limit 
7725               (mapcar 
7726                (lambda (header) (mail-header-number header))
7727                gnus-newsgroup-headers)))
7728       ;; Generate the summary buffer.
7729       (unless no-display
7730         (gnus-summary-prepare))
7731       (when gnus-use-trees
7732         (gnus-tree-open group)
7733         (setq gnus-summary-highlight-line-function
7734               'gnus-tree-highlight-article))
7735       ;; If the summary buffer is empty, but there are some low-scored
7736       ;; articles or some excluded dormants, we include these in the
7737       ;; buffer.
7738       (when (and (zerop (buffer-size))
7739                  (not no-display))
7740         (cond (gnus-newsgroup-dormant
7741                (gnus-summary-limit-include-dormant))
7742               ((and gnus-newsgroup-scored show-all)
7743                (gnus-summary-limit-include-expunged))))
7744       ;; Function `gnus-apply-kill-file' must be called in this hook.
7745       (run-hooks 'gnus-apply-kill-hook)
7746       (if (and (zerop (buffer-size))
7747                (not no-display))
7748           (progn
7749             ;; This newsgroup is empty.
7750             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7751             (gnus-message 6 "No unread news")
7752             (when kill-buffer
7753               (gnus-kill-or-deaden-summary kill-buffer))
7754             ;; Return nil from this function.
7755             nil)
7756         ;; Hide conversation thread subtrees.  We cannot do this in
7757         ;; gnus-summary-prepare-hook since kill processing may not
7758         ;; work with hidden articles.
7759         (and gnus-show-threads
7760              gnus-thread-hide-subtree
7761              (gnus-summary-hide-all-threads))
7762         ;; Show first unread article if requested.
7763         (if (and (not no-article)
7764                  (not no-display)
7765                  gnus-newsgroup-unreads
7766                  gnus-auto-select-first)
7767             (unless (if (eq gnus-auto-select-first 'best)
7768                         (gnus-summary-best-unread-article)
7769                       (gnus-summary-first-unread-article))
7770               (gnus-configure-windows 'summary))
7771           ;; Don't select any articles, just move point to the first
7772           ;; article in the group.
7773           (goto-char (point-min))
7774           (gnus-summary-position-point)
7775           (gnus-set-mode-line 'summary)
7776           (gnus-configure-windows 'summary 'force))
7777         ;; If we are in async mode, we send some info to the backend.
7778         (when gnus-newsgroup-async
7779           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7780         (when kill-buffer
7781           (gnus-kill-or-deaden-summary kill-buffer))
7782         (when (get-buffer-window gnus-group-buffer t)
7783           ;; Gotta use windows, because recenter does wierd stuff if
7784           ;; the current buffer ain't the displayed window.
7785           (let ((owin (selected-window)))
7786             (select-window (get-buffer-window gnus-group-buffer t))
7787             (when (gnus-group-goto-group group)
7788               (recenter))
7789             (select-window owin))))
7790       ;; Mark this buffer as "prepared".
7791       (setq gnus-newsgroup-prepared t)
7792       t))))
7793
7794 (defun gnus-summary-prepare ()
7795   "Generate the summary buffer."
7796   (let ((buffer-read-only nil))
7797     (erase-buffer)
7798     (setq gnus-newsgroup-data nil
7799           gnus-newsgroup-data-reverse nil)
7800     (run-hooks 'gnus-summary-generate-hook)
7801     ;; Generate the buffer, either with threads or without.
7802     (when gnus-newsgroup-headers
7803       (gnus-summary-prepare-threads
7804        (if gnus-show-threads
7805            (gnus-sort-gathered-threads
7806             (funcall gnus-summary-thread-gathering-function
7807                      (gnus-sort-threads
7808                       (gnus-cut-threads (gnus-make-threads)))))
7809          ;; Unthreaded display.
7810          (gnus-sort-articles gnus-newsgroup-headers))))
7811     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7812     ;; Call hooks for modifying summary buffer.
7813     (goto-char (point-min))
7814     (run-hooks 'gnus-summary-prepare-hook)))
7815
7816 (defun gnus-gather-threads-by-subject (threads)
7817   "Gather threads by looking at Subject headers."
7818   (if (not gnus-summary-make-false-root)
7819       threads
7820     (let ((hashtb (gnus-make-hashtable 1023))
7821           (prev threads)
7822           (result threads)
7823           subject hthread whole-subject)
7824       (while threads
7825         (setq whole-subject (mail-header-subject (caar threads)))
7826         (setq subject
7827               (cond
7828                ;; Truncate the subject.
7829                ((numberp gnus-summary-gather-subject-limit)
7830                 (setq subject (gnus-simplify-subject-re whole-subject))
7831                 (if (> (length subject) gnus-summary-gather-subject-limit)
7832                     (substring subject 0 gnus-summary-gather-subject-limit)
7833                   subject))
7834                ;; Fuzzily simplify it.
7835                ((eq 'fuzzy gnus-summary-gather-subject-limit)
7836                 (gnus-simplify-subject-fuzzy whole-subject))
7837                ;; Just remove the leading "Re:".
7838                (t
7839                 (gnus-simplify-subject-re whole-subject))))
7840
7841         (if (and gnus-summary-gather-exclude-subject
7842                  (string-match gnus-summary-gather-exclude-subject
7843                                subject))
7844             ()          ; We don't want to do anything with this article.
7845           ;; We simplify the subject before looking it up in the
7846           ;; hash table.
7847
7848           (if (setq hthread (gnus-gethash subject hashtb))
7849               (progn
7850                 ;; We enter a dummy root into the thread, if we
7851                 ;; haven't done that already.
7852                 (unless (stringp (caar hthread))
7853                   (setcar hthread (list whole-subject (car hthread))))
7854                 ;; We add this new gathered thread to this gathered
7855                 ;; thread.
7856                 (setcdr (car hthread)
7857                         (nconc (cdar hthread) (list (car threads))))
7858                 ;; Remove it from the list of threads.
7859                 (setcdr prev (cdr threads))
7860                 (setq threads prev))
7861             ;; Enter this thread into the hash table.
7862             (gnus-sethash subject threads hashtb)))
7863         (setq prev threads)
7864         (setq threads (cdr threads)))
7865       result)))
7866
7867 (defun gnus-gather-threads-by-references (threads)
7868   "Gather threads by looking at References headers."
7869   (let ((idhashtb (gnus-make-hashtable 1023))
7870         (thhashtb (gnus-make-hashtable 1023))
7871         (prev threads)
7872         (result threads)
7873         ids references id gthread gid entered)
7874     (while threads
7875       (when (setq references (mail-header-references (caar threads)))
7876         (setq id (mail-header-id (caar threads)))
7877         (setq ids (gnus-split-references references))
7878         (setq entered nil)
7879         (while ids
7880           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7881               (progn
7882                 (gnus-sethash (car ids) id idhashtb)
7883                 (gnus-sethash id threads thhashtb))
7884             (setq gthread (gnus-gethash gid thhashtb))
7885             (unless entered
7886               ;; We enter a dummy root into the thread, if we
7887               ;; haven't done that already.
7888               (unless (stringp (caar gthread))
7889                 (setcar gthread (list (mail-header-subject (caar gthread))
7890                                       (car gthread))))
7891               ;; We add this new gathered thread to this gathered
7892               ;; thread.
7893               (setcdr (car gthread)
7894                       (nconc (cdar gthread) (list (car threads)))))
7895             ;; Add it into the thread hash table.
7896             (gnus-sethash id gthread thhashtb)
7897             (setq entered t)
7898             ;; Remove it from the list of threads.
7899             (setcdr prev (cdr threads))
7900             (setq threads prev))
7901           (setq ids (cdr ids))))
7902       (setq prev threads)
7903       (setq threads (cdr threads)))
7904     result))
7905
7906 (defun gnus-sort-gathered-threads (threads)
7907   "Sort subtreads inside each gathered thread by article number."
7908   (let ((result threads))
7909     (while threads
7910       (when (stringp (caar threads))
7911         (setcdr (car threads)
7912                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7913       (setq threads (cdr threads)))
7914     result))
7915
7916 (defun gnus-make-threads ()
7917   "Go through the dependency hashtb and find the roots.  Return all threads."
7918   (let (threads)
7919     (mapatoms
7920      (lambda (refs)
7921        (unless (car (symbol-value refs))
7922          ;; These threads do not refer back to any other articles,
7923          ;; so they're roots.
7924          (setq threads (append (cdr (symbol-value refs)) threads))))
7925      gnus-newsgroup-dependencies)
7926     threads))
7927
7928 (defun gnus-build-sparse-threads ()
7929   (let ((headers gnus-newsgroup-headers)
7930         (deps gnus-newsgroup-dependencies)
7931         header references generation relations 
7932         cthread subject child end pthread relation)
7933     ;; First we create an alist of generations/relations, where 
7934     ;; generations is how much we trust the ralation, and the relation
7935     ;; is parent/child.
7936     (gnus-message 7 "Making sparse threads...")
7937     (save-excursion
7938       (nnheader-set-temp-buffer " *gnus sparse threads*")
7939       (while (setq header (pop headers))
7940         (when (and (setq references (mail-header-references header))
7941                    (not (string= references "")))
7942           (insert references)
7943           (setq child (mail-header-id header)
7944                 subject (mail-header-subject header))
7945           (setq generation 0)
7946           (while (search-backward ">" nil t)
7947             (setq end (1+ (point)))
7948             (when (search-backward "<" nil t)
7949               (push (list (incf generation) 
7950                           child (setq child (buffer-substring (point) end))
7951                           subject)
7952                     relations)))
7953           (push (list (1+ generation) child nil subject) relations)
7954           (erase-buffer)))
7955       (kill-buffer (current-buffer)))
7956     ;; Sort over trustworthiness.
7957     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7958     (while (setq relation (pop relations))
7959       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7960                 (unless (car (symbol-value cthread))
7961                   ;; Make this article the parent of these threads.
7962                   (setcar (symbol-value cthread)
7963                           (vector gnus-reffed-article-number 
7964                                   (cadddr relation) 
7965                                   "" ""
7966                                   (cadr relation) 
7967                                   (or (caddr relation) "") 0 0 "")))
7968               (set cthread (list (vector gnus-reffed-article-number
7969                                          (cadddr relation) 
7970                                          "" "" (cadr relation) 
7971                                          (or (caddr relation) "") 0 0 ""))))
7972         (push gnus-reffed-article-number gnus-newsgroup-limit)
7973         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7974         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7975               gnus-newsgroup-reads)
7976         (decf gnus-reffed-article-number)
7977         ;; Make this new thread the child of its parent.
7978         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7979             (setcdr (symbol-value pthread)
7980                     (nconc (cdr (symbol-value pthread))
7981                            (list (symbol-value cthread))))
7982           (set pthread (list nil (symbol-value cthread))))))
7983     (gnus-message 7 "Making sparse threads...done")))
7984
7985 (defun gnus-build-old-threads ()
7986   ;; Look at all the articles that refer back to old articles, and
7987   ;; fetch the headers for the articles that aren't there.  This will
7988   ;; build complete threads - if the roots haven't been expired by the
7989   ;; server, that is.
7990   (let (id heads)
7991     (mapatoms
7992      (lambda (refs)
7993        (when (not (car (symbol-value refs)))
7994          (setq heads (cdr (symbol-value refs)))
7995          (while heads
7996            (if (memq (mail-header-number (caar heads))
7997                      gnus-newsgroup-dormant)
7998                (setq heads (cdr heads))
7999              (setq id (symbol-name refs))
8000              (while (and (setq id (gnus-build-get-header id))
8001                          (not (car (gnus-gethash
8002                                     id gnus-newsgroup-dependencies)))))
8003              (setq heads nil)))))
8004      gnus-newsgroup-dependencies)))
8005
8006 (defun gnus-build-get-header (id)
8007   ;; Look through the buffer of NOV lines and find the header to
8008   ;; ID.  Enter this line into the dependencies hash table, and return
8009   ;; the id of the parent article (if any).
8010   (let ((deps gnus-newsgroup-dependencies)
8011         found header)
8012     (prog1
8013         (save-excursion
8014           (set-buffer nntp-server-buffer)
8015           (goto-char (point-min))
8016           (while (and (not found) (search-forward id nil t))
8017             (beginning-of-line)
8018             (setq found (looking-at
8019                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
8020                                  (regexp-quote id))))
8021             (or found (beginning-of-line 2)))
8022           (when found
8023             (beginning-of-line)
8024             (and
8025              (setq header (gnus-nov-parse-line
8026                            (read (current-buffer)) deps))
8027              (gnus-parent-id (mail-header-references header)))))
8028       (when header
8029         (let ((number (mail-header-number header)))
8030           (push number gnus-newsgroup-limit)
8031           (push header gnus-newsgroup-headers)
8032           (if (memq number gnus-newsgroup-unselected)
8033               (progn
8034                 (push number gnus-newsgroup-unreads)
8035                 (setq gnus-newsgroup-unselected
8036                       (delq number gnus-newsgroup-unselected)))
8037             (push number gnus-newsgroup-ancient)))))))
8038
8039 (defun gnus-summary-update-article (article &optional header)
8040   "Update ARTICLE in the summary buffer."
8041   (set-buffer gnus-summary-buffer)
8042   (let* ((header (or header (gnus-summary-article-header article)))
8043          (id (mail-header-id header))
8044          (data (gnus-data-find article))
8045          (thread (gnus-id-to-thread id))
8046          (parent
8047           (gnus-id-to-thread (or (gnus-parent-id 
8048                                   (mail-header-references header))
8049                                  "tull")))
8050          (buffer-read-only nil)
8051          (old (car thread))
8052          (number (mail-header-number header))
8053          pos)
8054     (when thread
8055       (setcar thread nil)
8056       (when parent
8057         (delq thread parent))
8058       (if (gnus-summary-insert-subject id header)
8059           ;; Set the (possibly) new article number in the data structure.
8060           (gnus-data-set-number data (gnus-id-to-article id))
8061         (setcar thread old)
8062         nil))))
8063
8064 (defun gnus-rebuild-thread (id)
8065   "Rebuild the thread containing ID."
8066   (let ((buffer-read-only nil)
8067         current thread data)
8068     (if (not gnus-show-threads)
8069         (setq thread (list (car (gnus-id-to-thread id))))
8070       ;; Get the thread this article is part of.
8071       (setq thread (gnus-remove-thread id)))
8072     (setq current (save-excursion
8073                     (and (zerop (forward-line -1))
8074                          (gnus-summary-article-number))))
8075     ;; If this is a gathered thread, we have to go some re-gathering.
8076     (when (stringp (car thread))
8077       (let ((subject (car thread))
8078             roots thr)
8079         (setq thread (cdr thread))
8080         (while thread
8081           (unless (memq (setq thr (gnus-id-to-thread
8082                                       (gnus-root-id
8083                                        (mail-header-id (caar thread)))))
8084                         roots)
8085             (push thr roots))
8086           (setq thread (cdr thread)))
8087         ;; We now have all (unique) roots.
8088         (if (= (length roots) 1)
8089             ;; All the loose roots are now one solid root.
8090             (setq thread (car roots))
8091           (setq thread (cons subject (gnus-sort-threads roots))))))
8092     (let (threads)
8093       ;; We then insert this thread into the summary buffer.
8094       (let (gnus-newsgroup-data gnus-newsgroup-threads)
8095         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
8096         (setq data (nreverse gnus-newsgroup-data))
8097         (setq threads gnus-newsgroup-threads))
8098       ;; We splice the new data into the data structure.
8099       (gnus-data-enter-list current data)
8100       (gnus-data-compute-positions)
8101       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
8102
8103 (defun gnus-id-to-thread (id)
8104   "Return the (sub-)thread where ID appears."
8105   (gnus-gethash id gnus-newsgroup-dependencies))
8106
8107 (defun gnus-id-to-article (id)
8108   "Return the article number of ID."
8109   (let ((thread (gnus-id-to-thread id)))
8110     (when (and thread
8111                (car thread))
8112       (mail-header-number (car thread)))))
8113
8114 (defun gnus-id-to-header (id)
8115   "Return the article headers of ID."
8116   (car (gnus-id-to-thread id)))
8117
8118 (defun gnus-article-displayed-root-p (article)
8119   "Say whether ARTICLE is a root(ish) article."
8120   (let ((level (gnus-summary-thread-level article))
8121         (refs (mail-header-references  (gnus-summary-article-header article)))
8122         particle)
8123     (cond 
8124      ((null level) nil)
8125      ((zerop level) t)
8126      ((null refs) t)
8127      ((null (gnus-parent-id refs)) t)
8128      ((and (= 1 level)
8129            (null (setq particle (gnus-id-to-article
8130                                  (gnus-parent-id refs))))
8131            (null (gnus-summary-thread-level particle)))))))
8132
8133 (defun gnus-root-id (id)
8134   "Return the id of the root of the thread where ID appears."
8135   (let (last-id prev)
8136     (while (and id (setq prev (car (gnus-gethash 
8137                                     id gnus-newsgroup-dependencies))))
8138       (setq last-id id
8139             id (gnus-parent-id (mail-header-references prev))))
8140     last-id))
8141
8142 (defun gnus-remove-thread (id &optional dont-remove)
8143   "Remove the thread that has ID in it."
8144   (let ((dep gnus-newsgroup-dependencies)
8145         headers thread last-id)
8146     ;; First go up in this thread until we find the root.
8147     (setq last-id (gnus-root-id id))
8148     (setq headers (list (car (gnus-id-to-thread last-id))
8149                         (caadr (gnus-id-to-thread last-id))))
8150     ;; We have now found the real root of this thread.  It might have
8151     ;; been gathered into some loose thread, so we have to search
8152     ;; through the threads to find the thread we wanted.
8153     (let ((threads gnus-newsgroup-threads)
8154           sub)
8155       (while threads
8156         (setq sub (car threads))
8157         (if (stringp (car sub))
8158             ;; This is a gathered threads, so we look at the roots
8159             ;; below it to find whether this article in in this
8160             ;; gathered root.
8161             (progn
8162               (setq sub (cdr sub))
8163               (while sub
8164                 (when (member (caar sub) headers)
8165                   (setq thread (car threads)
8166                         threads nil
8167                         sub nil))
8168                 (setq sub (cdr sub))))
8169           ;; It's an ordinary thread, so we check it.
8170           (when (eq (car sub) (car headers))
8171             (setq thread sub
8172                   threads nil)))
8173         (setq threads (cdr threads)))
8174       ;; If this article is in no thread, then it's a root.
8175       (if thread
8176           (unless dont-remove
8177             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
8178         (setq thread (gnus-gethash last-id dep)))
8179       (when thread
8180         (prog1
8181             thread ; We return this thread.
8182           (unless dont-remove
8183             (if (stringp (car thread))
8184                 (progn
8185                   ;; If we use dummy roots, then we have to remove the
8186                   ;; dummy root as well.
8187                   (when (eq gnus-summary-make-false-root 'dummy)
8188                     ;; Uhm.
8189                     )
8190                   (setq thread (cdr thread))
8191                   (while thread
8192                     (gnus-remove-thread-1 (car thread))
8193                     (setq thread (cdr thread))))
8194               (gnus-remove-thread-1 thread))))))))
8195
8196 (defun gnus-remove-thread-1 (thread)
8197   "Remove the thread THREAD recursively."
8198   (let ((number (mail-header-number (car thread)))
8199         pos)
8200     (when (setq pos (text-property-any
8201                      (point-min) (point-max) 'gnus-number number))
8202       (goto-char pos)
8203       (gnus-delete-line)
8204       (gnus-data-remove number))
8205     (setq thread (cdr thread))
8206     (while thread
8207       (gnus-remove-thread-1 (pop thread)))))
8208
8209 (defun gnus-sort-threads (threads)
8210   "Sort THREADS."
8211   (if (not gnus-thread-sort-functions)
8212       threads
8213     (let ((func (if (= 1 (length gnus-thread-sort-functions))
8214                     (car gnus-thread-sort-functions)
8215                   `(lambda (t1 t2)
8216                      ,(gnus-make-sort-function 
8217                        (reverse gnus-thread-sort-functions))))))
8218       (gnus-message 7 "Sorting threads...")
8219       (prog1
8220           (sort threads func)
8221         (gnus-message 7 "Sorting threads...done")))))
8222
8223 (defun gnus-sort-articles (articles)
8224   "Sort ARTICLES."
8225   (when gnus-article-sort-functions
8226     (let ((func (if (= 1 (length gnus-article-sort-functions))
8227                     (car gnus-article-sort-functions)
8228                   `(lambda (t1 t2)
8229                      ,(gnus-make-sort-function 
8230                        (reverse gnus-article-sort-functions))))))
8231       (gnus-message 7 "Sorting articles...")
8232       (prog1
8233           (setq gnus-newsgroup-headers (sort articles func))
8234         (gnus-message 7 "Sorting articles...done")))))
8235
8236 (defun gnus-make-sort-function (funs)
8237   "Return a composite sort condition based on the functions in FUNC."
8238   (if (cdr funs)
8239       `(or (,(car funs) t1 t2)
8240            (and (not (,(car funs) t2 t1))
8241                 ,(gnus-make-sort-function (cdr funs))))
8242     `(,(car funs) t1 t2)))
8243                  
8244 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8245 (defmacro gnus-thread-header (thread)
8246   ;; Return header of first article in THREAD.
8247   ;; Note that THREAD must never, ever be anything else than a variable -
8248   ;; using some other form will lead to serious barfage.
8249   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8250   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8251   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8252         (vector thread) 2))
8253
8254 (defsubst gnus-article-sort-by-number (h1 h2)
8255   "Sort articles by article number."
8256   (< (mail-header-number h1)
8257      (mail-header-number h2)))
8258
8259 (defun gnus-thread-sort-by-number (h1 h2)
8260   "Sort threads by root article number."
8261   (gnus-article-sort-by-number
8262    (gnus-thread-header h1) (gnus-thread-header h2)))
8263
8264 (defsubst gnus-article-sort-by-author (h1 h2)
8265   "Sort articles by root author."
8266   (string-lessp
8267    (let ((extract (funcall
8268                    gnus-extract-address-components
8269                    (mail-header-from h1))))
8270      (or (car extract) (cdr extract)))
8271    (let ((extract (funcall
8272                    gnus-extract-address-components
8273                    (mail-header-from h2))))
8274      (or (car extract) (cdr extract)))))
8275
8276 (defun gnus-thread-sort-by-author (h1 h2)
8277   "Sort threads by root author."
8278   (gnus-article-sort-by-author
8279    (gnus-thread-header h1)  (gnus-thread-header h2)))
8280
8281 (defsubst gnus-article-sort-by-subject (h1 h2)
8282   "Sort articles by root subject."
8283   (string-lessp
8284    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8285    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8286
8287 (defun gnus-thread-sort-by-subject (h1 h2)
8288   "Sort threads by root subject."
8289   (gnus-article-sort-by-subject
8290    (gnus-thread-header h1) (gnus-thread-header h2)))
8291
8292 (defsubst gnus-article-sort-by-date (h1 h2)
8293   "Sort articles by root article date."
8294   (string-lessp
8295    (gnus-sortable-date (mail-header-date h1))
8296    (gnus-sortable-date (mail-header-date h2))))
8297
8298 (defun gnus-thread-sort-by-date (h1 h2)
8299   "Sort threads by root article date."
8300   (gnus-article-sort-by-date
8301    (gnus-thread-header h1) (gnus-thread-header h2)))
8302
8303 (defsubst gnus-article-sort-by-score (h1 h2)
8304   "Sort articles by root article score.
8305 Unscored articles will be counted as having a score of zero."
8306   (> (or (cdr (assq (mail-header-number h1)
8307                     gnus-newsgroup-scored))
8308          gnus-summary-default-score 0)
8309      (or (cdr (assq (mail-header-number h2)
8310                     gnus-newsgroup-scored))
8311          gnus-summary-default-score 0)))
8312
8313 (defun gnus-thread-sort-by-score (h1 h2)
8314   "Sort threads by root article score."
8315   (gnus-article-sort-by-score
8316    (gnus-thread-header h1) (gnus-thread-header h2)))
8317
8318 (defun gnus-thread-sort-by-total-score (h1 h2)
8319   "Sort threads by the sum of all scores in the thread.
8320 Unscored articles will be counted as having a score of zero."
8321   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8322
8323 (defun gnus-thread-total-score (thread)
8324   ;;  This function find the total score of THREAD.
8325   (if (consp thread)
8326       (if (stringp (car thread))
8327           (apply gnus-thread-score-function 0
8328                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8329         (gnus-thread-total-score-1 thread))
8330     (gnus-thread-total-score-1 (list thread))))
8331
8332 (defun gnus-thread-total-score-1 (root)
8333   ;; This function find the total score of the thread below ROOT.
8334   (setq root (car root))
8335   (apply gnus-thread-score-function
8336          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8337              gnus-summary-default-score 0)
8338          (mapcar 'gnus-thread-total-score
8339                  (cdr (gnus-gethash (mail-header-id root)
8340                                     gnus-newsgroup-dependencies)))))
8341
8342 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8343 (defvar gnus-tmp-prev-subject nil)
8344 (defvar gnus-tmp-false-parent nil)
8345 (defvar gnus-tmp-root-expunged nil)
8346 (defvar gnus-tmp-dummy-line nil)
8347
8348 (defun gnus-summary-prepare-threads (threads)
8349   "Prepare summary buffer from THREADS and indentation LEVEL.
8350 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8351 or a straight list of headers."
8352   (gnus-message 7 "Generating summary...")
8353
8354   (setq gnus-newsgroup-threads threads)
8355   (beginning-of-line)
8356
8357   (let ((gnus-tmp-level 0)
8358         (default-score (or gnus-summary-default-score 0))
8359         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8360         thread number subject stack state gnus-tmp-gathered beg-match
8361         new-roots gnus-tmp-new-adopts thread-end
8362         gnus-tmp-header gnus-tmp-unread
8363         gnus-tmp-replied gnus-tmp-subject-or-nil
8364         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8365         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8366         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8367
8368     (setq gnus-tmp-prev-subject nil)
8369
8370     (if (vectorp (car threads))
8371         ;; If this is a straight (sic) list of headers, then a
8372         ;; threaded summary display isn't required, so we just create
8373         ;; an unthreaded one.
8374         (gnus-summary-prepare-unthreaded threads)
8375
8376       ;; Do the threaded display.
8377
8378       (while (or threads stack gnus-tmp-new-adopts new-roots)
8379
8380         (if (and (= gnus-tmp-level 0)
8381                  (not (setq gnus-tmp-dummy-line nil))
8382                  (or (not stack)
8383                      (= (caar stack) 0))
8384                  (not gnus-tmp-false-parent)
8385                  (or gnus-tmp-new-adopts new-roots))
8386             (if gnus-tmp-new-adopts
8387                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8388                       thread (list (car gnus-tmp-new-adopts))
8389                       gnus-tmp-header (caar thread)
8390                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8391               (if new-roots
8392                   (setq thread (list (car new-roots))
8393                         gnus-tmp-header (caar thread)
8394                         new-roots (cdr new-roots))))
8395
8396           (if threads
8397               ;; If there are some threads, we do them before the
8398               ;; threads on the stack.
8399               (setq thread threads
8400                     gnus-tmp-header (caar thread))
8401             ;; There were no current threads, so we pop something off
8402             ;; the stack.
8403             (setq state (car stack)
8404                   gnus-tmp-level (car state)
8405                   thread (cdr state)
8406                   stack (cdr stack)
8407                   gnus-tmp-header (caar thread))))
8408
8409         (setq gnus-tmp-false-parent nil)
8410         (setq gnus-tmp-root-expunged nil)
8411         (setq thread-end nil)
8412
8413         (if (stringp gnus-tmp-header)
8414             ;; The header is a dummy root.
8415             (cond
8416              ((eq gnus-summary-make-false-root 'adopt)
8417               ;; We let the first article adopt the rest.
8418               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8419                                                (cddar thread)))
8420               (setq gnus-tmp-gathered
8421                     (nconc (mapcar
8422                             (lambda (h) (mail-header-number (car h)))
8423                             (cddar thread))
8424                            gnus-tmp-gathered))
8425               (setq thread (cons (list (caar thread)
8426                                        (cadar thread))
8427                                  (cdr thread)))
8428               (setq gnus-tmp-level -1
8429                     gnus-tmp-false-parent t))
8430              ((eq gnus-summary-make-false-root 'empty)
8431               ;; We print adopted articles with empty subject fields.
8432               (setq gnus-tmp-gathered
8433                     (nconc (mapcar
8434                             (lambda (h) (mail-header-number (car h)))
8435                             (cddar thread))
8436                            gnus-tmp-gathered))
8437               (setq gnus-tmp-level -1))
8438              ((eq gnus-summary-make-false-root 'dummy)
8439               ;; We remember that we probably want to output a dummy
8440               ;; root.
8441               (setq gnus-tmp-dummy-line gnus-tmp-header)
8442               (setq gnus-tmp-prev-subject gnus-tmp-header))
8443              (t
8444               ;; We do not make a root for the gathered
8445               ;; sub-threads at all.
8446               (setq gnus-tmp-level -1)))
8447
8448           (setq number (mail-header-number gnus-tmp-header)
8449                 subject (mail-header-subject gnus-tmp-header))
8450
8451           (cond
8452            ;; If the thread has changed subject, we might want to make
8453            ;; this subthread into a root.
8454            ((and (null gnus-thread-ignore-subject)
8455                  (not (zerop gnus-tmp-level))
8456                  gnus-tmp-prev-subject
8457                  (not (inline
8458                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8459             (setq new-roots (nconc new-roots (list (car thread)))
8460                   thread-end t
8461                   gnus-tmp-header nil))
8462            ;; If the article lies outside the current limit,
8463            ;; then we do not display it.
8464            ((and (not (memq number gnus-newsgroup-limit))
8465                  (not gnus-tmp-dummy-line))
8466             (setq gnus-tmp-gathered
8467                   (nconc (mapcar
8468                           (lambda (h) (mail-header-number (car h)))
8469                           (cdar thread))
8470                          gnus-tmp-gathered))
8471             (setq gnus-tmp-new-adopts (if (cdar thread)
8472                                           (append gnus-tmp-new-adopts
8473                                                   (cdar thread))
8474                                         gnus-tmp-new-adopts)
8475                   thread-end t
8476                   gnus-tmp-header nil)
8477             (when (zerop gnus-tmp-level)
8478               (setq gnus-tmp-root-expunged t)))
8479            ;; Perhaps this article is to be marked as read?
8480            ((and gnus-summary-mark-below
8481                  (< (or (cdr (assq number gnus-newsgroup-scored))
8482                         default-score)
8483                     gnus-summary-mark-below)
8484                  ;; Don't touch sparse articles.
8485                  (not (memq number gnus-newsgroup-sparse))
8486                  (not (memq number gnus-newsgroup-ancient)))
8487             (setq gnus-newsgroup-unreads
8488                   (delq number gnus-newsgroup-unreads))
8489             (if gnus-newsgroup-auto-expire
8490                 (push number gnus-newsgroup-expirable)
8491               (push (cons number gnus-low-score-mark)
8492                     gnus-newsgroup-reads))))
8493
8494           (when gnus-tmp-header
8495             ;; We may have an old dummy line to output before this
8496             ;; article.
8497             (when gnus-tmp-dummy-line
8498               (gnus-summary-insert-dummy-line
8499                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8500               (setq gnus-tmp-dummy-line nil))
8501
8502             ;; Compute the mark.
8503             (setq
8504              gnus-tmp-unread
8505              (cond
8506               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8507               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8508               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8509               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8510               (t (or (cdr (assq number gnus-newsgroup-reads))
8511                      gnus-ancient-mark))))
8512
8513             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8514                                   gnus-tmp-header gnus-tmp-level)
8515                   gnus-newsgroup-data)
8516
8517             ;; Actually insert the line.
8518             (setq
8519              gnus-tmp-subject-or-nil
8520              (cond
8521               ((and gnus-thread-ignore-subject
8522                     gnus-tmp-prev-subject
8523                     (not (inline (gnus-subject-equal
8524                                   gnus-tmp-prev-subject subject))))
8525                subject)
8526               ((zerop gnus-tmp-level)
8527                (if (and (eq gnus-summary-make-false-root 'empty)
8528                         (memq number gnus-tmp-gathered)
8529                         gnus-tmp-prev-subject
8530                         (inline (gnus-subject-equal
8531                                  gnus-tmp-prev-subject subject)))
8532                    gnus-summary-same-subject
8533                  subject))
8534               (t gnus-summary-same-subject)))
8535             (if (and (eq gnus-summary-make-false-root 'adopt)
8536                      (= gnus-tmp-level 1)
8537                      (memq number gnus-tmp-gathered))
8538                 (setq gnus-tmp-opening-bracket ?\<
8539                       gnus-tmp-closing-bracket ?\>)
8540               (setq gnus-tmp-opening-bracket ?\[
8541                     gnus-tmp-closing-bracket ?\]))
8542             (setq
8543              gnus-tmp-indentation
8544              (aref gnus-thread-indent-array gnus-tmp-level)
8545              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8546              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8547                                 gnus-summary-default-score 0)
8548              gnus-tmp-score-char
8549              (if (or (null gnus-summary-default-score)
8550                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8551                          gnus-summary-zcore-fuzz)) ? 
8552                (if (< gnus-tmp-score gnus-summary-default-score)
8553                    gnus-score-below-mark gnus-score-over-mark))
8554              gnus-tmp-replied
8555              (cond ((memq number gnus-newsgroup-processable)
8556                     gnus-process-mark)
8557                    ((memq number gnus-newsgroup-cached)
8558                     gnus-cached-mark)
8559                    ((memq number gnus-newsgroup-replied)
8560                     gnus-replied-mark)
8561                    (t gnus-unread-mark))
8562              gnus-tmp-from (mail-header-from gnus-tmp-header)
8563              gnus-tmp-name
8564              (cond
8565               ((string-match "(.+)" gnus-tmp-from)
8566                (substring gnus-tmp-from
8567                           (1+ (match-beginning 0)) (1- (match-end 0))))
8568               ((string-match "<[^>]+> *$" gnus-tmp-from)
8569                (setq beg-match (match-beginning 0))
8570                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8571                         (substring gnus-tmp-from (1+ (match-beginning 0))
8572                                    (1- (match-end 0))))
8573                    (substring gnus-tmp-from 0 beg-match)))
8574               (t gnus-tmp-from)))
8575             (when (string= gnus-tmp-name "")
8576               (setq gnus-tmp-name gnus-tmp-from))
8577             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8578             (gnus-put-text-property
8579              (point)
8580              (progn (eval gnus-summary-line-format-spec) (point))
8581              'gnus-number number)
8582             (when gnus-visual-p
8583               (forward-line -1)
8584               (run-hooks 'gnus-summary-update-hook)
8585               (forward-line 1))
8586
8587             (setq gnus-tmp-prev-subject subject)))
8588
8589         (when (nth 1 thread)
8590           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8591         (incf gnus-tmp-level)
8592         (setq threads (if thread-end nil (cdar thread)))
8593         (unless threads
8594           (setq gnus-tmp-level 0)))))
8595   (gnus-message 7 "Generating summary...done"))
8596
8597 (defun gnus-summary-prepare-unthreaded (headers)
8598   "Generate an unthreaded summary buffer based on HEADERS."
8599   (let (header number mark)
8600
8601     (while headers
8602       ;; We may have to root out some bad articles...
8603       (when (memq (setq number (mail-header-number
8604                                 (setq header (pop headers))))
8605                   gnus-newsgroup-limit)
8606         ;; Mark article as read when it has a low score.
8607         (when (and gnus-summary-mark-below
8608                    (< (or (cdr (assq number gnus-newsgroup-scored))
8609                           gnus-summary-default-score 0)
8610                       gnus-summary-mark-below)
8611                    (not (memq number gnus-newsgroup-ancient)))
8612           (setq gnus-newsgroup-unreads
8613                 (delq number gnus-newsgroup-unreads))
8614           (if gnus-newsgroup-auto-expire
8615               (push number gnus-newsgroup-expirable)
8616             (push (cons number gnus-low-score-mark)
8617                   gnus-newsgroup-reads)))
8618
8619         (setq mark
8620               (cond
8621                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8622                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8623                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8624                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8625                (t (or (cdr (assq number gnus-newsgroup-reads))
8626                       gnus-ancient-mark))))
8627         (setq gnus-newsgroup-data
8628               (cons (gnus-data-make number mark (1+ (point)) header 0)
8629                     gnus-newsgroup-data))
8630         (gnus-summary-insert-line
8631          header 0 nil mark (memq number gnus-newsgroup-replied)
8632          (memq number gnus-newsgroup-expirable)
8633          (mail-header-subject header) nil
8634          (cdr (assq number gnus-newsgroup-scored))
8635          (memq number gnus-newsgroup-processable))))))
8636
8637 (defun gnus-select-newsgroup (group &optional read-all)
8638   "Select newsgroup GROUP.
8639 If READ-ALL is non-nil, all articles in the group are selected."
8640   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8641          (info (nth 2 entry))
8642          articles fetched-articles cached)
8643
8644     (or (gnus-check-server
8645          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8646         (error "Couldn't open server"))
8647
8648     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8649         (gnus-activate-group group)     ; Or we can activate it...
8650         (progn                          ; Or we bug out.
8651           (when (equal major-mode 'gnus-summary-mode)
8652             (kill-buffer (current-buffer)))
8653           (error "Couldn't request group %s: %s"
8654                  group (gnus-status-message group))))
8655
8656     (unless (gnus-request-group group t)
8657       (when (equal major-mode 'gnus-summary-mode)
8658         (kill-buffer (current-buffer)))
8659       (error "Couldn't request group %s: %s"
8660              group (gnus-status-message group)))      
8661
8662     (setq gnus-newsgroup-name group)
8663     (setq gnus-newsgroup-unselected nil)
8664     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8665
8666     (and gnus-asynchronous
8667          (gnus-check-backend-function
8668           'request-asynchronous gnus-newsgroup-name)
8669          (setq gnus-newsgroup-async
8670                (gnus-request-asynchronous gnus-newsgroup-name)))
8671
8672     ;; Adjust and set lists of article marks.
8673     (when info
8674       (gnus-adjust-marked-articles info))
8675
8676     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8677     (when (gnus-virtual-group-p group)
8678       (setq cached gnus-newsgroup-cached))
8679
8680     (setq gnus-newsgroup-unreads
8681           (gnus-set-difference
8682            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8683            gnus-newsgroup-dormant))
8684
8685     (setq gnus-newsgroup-processable nil)
8686
8687     (setq articles (gnus-articles-to-read group read-all))
8688
8689     (cond
8690      ((null articles)
8691       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8692       'quit)
8693      ((eq articles 0) nil)
8694      (t
8695       ;; Init the dependencies hash table.
8696       (setq gnus-newsgroup-dependencies
8697             (gnus-make-hashtable (length articles)))
8698       ;; Retrieve the headers and read them in.
8699       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8700       (setq gnus-newsgroup-headers
8701             (if (eq 'nov
8702                     (setq gnus-headers-retrieved-by
8703                           (gnus-retrieve-headers
8704                            articles gnus-newsgroup-name
8705                            ;; We might want to fetch old headers, but
8706                            ;; not if there is only 1 article.
8707                            (and gnus-fetch-old-headers
8708                                 (or (and
8709                                      (not (eq gnus-fetch-old-headers 'some))
8710                                      (not (numberp gnus-fetch-old-headers)))
8711                                     (> (length articles) 1))))))
8712                 (gnus-get-newsgroup-headers-xover articles)
8713               (gnus-get-newsgroup-headers)))
8714       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
8715
8716       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8717       (when cached
8718         (setq gnus-newsgroup-cached cached))
8719
8720       ;; Set the initial limit.
8721       (setq gnus-newsgroup-limit (copy-sequence articles))
8722       ;; Remove canceled articles from the list of unread articles.
8723       (setq gnus-newsgroup-unreads
8724             (gnus-set-sorted-intersection
8725              gnus-newsgroup-unreads
8726              (setq fetched-articles
8727                    (mapcar (lambda (headers) (mail-header-number headers))
8728                            gnus-newsgroup-headers))))
8729       ;; Removed marked articles that do not exist.
8730       (gnus-update-missing-marks
8731        (gnus-sorted-complement fetched-articles articles))
8732       ;; We might want to build some more threads first.
8733       (and gnus-fetch-old-headers
8734            (eq gnus-headers-retrieved-by 'nov)
8735            (gnus-build-old-threads))
8736       ;; Check whether auto-expire is to be done in this group.
8737       (setq gnus-newsgroup-auto-expire
8738             (gnus-group-auto-expirable-p group))
8739       ;; Set up the article buffer now, if necessary.
8740       (unless gnus-single-article-buffer
8741         (gnus-article-setup-buffer))
8742       ;; First and last article in this newsgroup.
8743       (and gnus-newsgroup-headers
8744            (setq gnus-newsgroup-begin
8745                  (mail-header-number (car gnus-newsgroup-headers)))
8746            (setq gnus-newsgroup-end
8747                  (mail-header-number
8748                   (gnus-last-element gnus-newsgroup-headers))))
8749       (setq gnus-reffed-article-number -1)
8750       ;; GROUP is successfully selected.
8751       (or gnus-newsgroup-headers t)))))
8752
8753 (defun gnus-articles-to-read (group read-all)
8754   ;; Find out what articles the user wants to read.
8755   (let* ((articles
8756           ;; Select all articles if `read-all' is non-nil, or if there
8757           ;; are no unread articles.
8758           (if (or read-all
8759                   (and (zerop (length gnus-newsgroup-marked))
8760                        (zerop (length gnus-newsgroup-unreads))))
8761               (gnus-uncompress-range (gnus-active group))
8762             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8763                           (copy-sequence gnus-newsgroup-unreads))
8764                   '<)))
8765          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8766          (scored (length scored-list))
8767          (number (length articles))
8768          (marked (+ (length gnus-newsgroup-marked)
8769                     (length gnus-newsgroup-dormant)))
8770          (select
8771           (cond
8772            ((numberp read-all)
8773             read-all)
8774            (t
8775             (condition-case ()
8776                 (cond
8777                  ((and (or (<= scored marked) (= scored number))
8778                        (numberp gnus-large-newsgroup)
8779                        (> number gnus-large-newsgroup))
8780                   (let ((input
8781                          (read-string
8782                           (format
8783                            "How many articles from %s (default %d): "
8784                            gnus-newsgroup-name number))))
8785                     (if (string-match "^[ \t]*$" input) number input)))
8786                  ((and (> scored marked) (< scored number))
8787                   (let ((input
8788                          (read-string
8789                           (format "%s %s (%d scored, %d total): "
8790                                   "How many articles from"
8791                                   group scored number))))
8792                     (if (string-match "^[ \t]*$" input)
8793                         number input)))
8794                  (t number))
8795               (quit nil))))))
8796     (setq select (if (stringp select) (string-to-number select) select))
8797     (if (or (null select) (zerop select))
8798         select
8799       (if (and (not (zerop scored)) (<= (abs select) scored))
8800           (progn
8801             (setq articles (sort scored-list '<))
8802             (setq number (length articles)))
8803         (setq articles (copy-sequence articles)))
8804
8805       (if (< (abs select) number)
8806           (if (< select 0)
8807               ;; Select the N oldest articles.
8808               (setcdr (nthcdr (1- (abs select)) articles) nil)
8809             ;; Select the N most recent articles.
8810             (setq articles (nthcdr (- number select) articles))))
8811       (setq gnus-newsgroup-unselected
8812             (gnus-sorted-intersection
8813              gnus-newsgroup-unreads
8814              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8815       articles)))
8816
8817 (defun gnus-killed-articles (killed articles)
8818   (let (out)
8819     (while articles
8820       (if (inline (gnus-member-of-range (car articles) killed))
8821           (setq out (cons (car articles) out)))
8822       (setq articles (cdr articles)))
8823     out))
8824
8825 (defun gnus-uncompress-marks (marks)
8826   "Uncompress the mark ranges in MARKS."
8827   (let ((uncompressed '(score bookmark))
8828         out)
8829     (while marks
8830       (if (memq (caar marks) uncompressed)
8831           (push (car marks) out)
8832         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8833       (setq marks (cdr marks)))
8834     out))
8835
8836 (defun gnus-adjust-marked-articles (info)
8837   "Set all article lists and remove all marks that are no longer legal."
8838   (let* ((marked-lists (gnus-info-marks info))
8839          (active (gnus-active (gnus-info-group info)))
8840          (min (car active))
8841          (max (cdr active))
8842          (types gnus-article-mark-lists)
8843          (uncompressed '(score bookmark))
8844          marks var articles article mark)
8845
8846     (while marked-lists
8847       (setq marks (pop marked-lists))
8848       (set (setq var (intern (format "gnus-newsgroup-%s"
8849                                      (car (rassq (setq mark (car marks))
8850                                                  types)))))
8851            (if (memq (car marks) uncompressed) (cdr marks)
8852              (gnus-uncompress-range (cdr marks))))
8853
8854       (setq articles (symbol-value var))
8855
8856       ;; All articles have to be subsets of the active articles.
8857       (cond
8858        ;; Adjust "simple" lists.
8859        ((memq mark '(tick dormant expirable reply killed save))
8860         (while articles
8861           (when (or (< (setq article (pop articles)) min) (> article max))
8862             (set var (delq article (symbol-value var))))))
8863        ;; Adjust assocs.
8864        ((memq mark '(score bookmark))
8865         (while articles
8866           (when (or (< (car (setq article (pop articles))) min)
8867                     (> (car article) max))
8868             (set var (delq article (symbol-value var))))))))))
8869
8870 (defun gnus-update-missing-marks (missing)
8871   "Go through the list of MISSING articles and remove them mark lists."
8872   (when missing
8873     (let ((types gnus-article-mark-lists)
8874           var m)
8875       ;; Go through all types.
8876       (while types
8877         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8878         (when (symbol-value var)
8879           ;; This list has articles.  So we delete all missing articles
8880           ;; from it.
8881           (setq m missing)
8882           (while m
8883             (set var (delq (pop m) (symbol-value var)))))))))
8884
8885 (defun gnus-update-marks ()
8886   "Enter the various lists of marked articles into the newsgroup info list."
8887   (let ((types gnus-article-mark-lists)
8888         (info (gnus-get-info gnus-newsgroup-name))
8889         (uncompressed '(score bookmark killed))
8890         type list newmarked symbol)
8891     (when info
8892       ;; Add all marks lists that are non-nil to the list of marks lists.
8893       (while types
8894         (setq type (pop types))
8895         (when (setq list (symbol-value
8896                           (setq symbol
8897                                 (intern (format "gnus-newsgroup-%s"
8898                                                 (car type))))))
8899           (push (cons (cdr type)
8900                       (if (memq (cdr type) uncompressed) list
8901                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8902                 newmarked)))
8903
8904       ;; Enter these new marks into the info of the group.
8905       (if (nthcdr 3 info)
8906           (setcar (nthcdr 3 info) newmarked)
8907         ;; Add the marks lists to the end of the info.
8908         (when newmarked
8909           (setcdr (nthcdr 2 info) (list newmarked))))
8910
8911       ;; Cut off the end of the info if there's nothing else there.
8912       (let ((i 5))
8913         (while (and (> i 2)
8914                     (not (nth i info)))
8915           (when (nthcdr (decf i) info)
8916             (setcdr (nthcdr i info) nil)))))))
8917
8918 (defun gnus-add-marked-articles (group type articles &optional info force)
8919   ;; Add ARTICLES of TYPE to the info of GROUP.
8920   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8921   ;; add, but replace marked articles of TYPE with ARTICLES.
8922   (let ((info (or info (gnus-get-info group)))
8923         (uncompressed '(score bookmark killed))
8924         marked m)
8925     (or (not info)
8926         (and (not (setq marked (nthcdr 3 info)))
8927              (or (null articles)
8928                  (setcdr (nthcdr 2 info)
8929                          (list (list (cons type (gnus-compress-sequence
8930                                                  articles t)))))))
8931         (and (not (setq m (assq type (car marked))))
8932              (or (null articles)
8933                  (setcar marked
8934                          (cons (cons type (gnus-compress-sequence articles t) )
8935                                (car marked)))))
8936         (if force
8937             (if (null articles)
8938                 (setcar (nthcdr 3 info)
8939                         (delq (assq type (car marked)) (car marked)))
8940               (setcdr m (gnus-compress-sequence articles t)))
8941           (setcdr m (gnus-compress-sequence
8942                      (sort (nconc (gnus-uncompress-range (cdr m))
8943                                   (copy-sequence articles)) '<) t))))))
8944
8945 (defun gnus-set-mode-line (where)
8946   "This function sets the mode line of the article or summary buffers.
8947 If WHERE is `summary', the summary mode line format will be used."
8948   ;; Is this mode line one we keep updated?
8949   (when (memq where gnus-updated-mode-lines)
8950     (let (mode-string)
8951       (save-excursion
8952         ;; We evaluate this in the summary buffer since these
8953         ;; variables are buffer-local to that buffer.
8954         (set-buffer gnus-summary-buffer)
8955         ;; We bind all these variables that are used in the `eval' form
8956         ;; below.
8957         (let* ((mformat (symbol-value
8958                          (intern
8959                           (format "gnus-%s-mode-line-format-spec" where))))
8960                (gnus-tmp-group-name gnus-newsgroup-name)
8961                (gnus-tmp-article-number (or gnus-current-article 0))
8962                (gnus-tmp-unread gnus-newsgroup-unreads)
8963                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8964                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8965                (gnus-tmp-unread-and-unselected
8966                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8967                             (zerop gnus-tmp-unselected)) "")
8968                       ((zerop gnus-tmp-unselected)
8969                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8970                       (t (format "{%d(+%d) more}"
8971                                  gnus-tmp-unread-and-unticked
8972                                  gnus-tmp-unselected))))
8973                (gnus-tmp-subject
8974                 (if (and gnus-current-headers
8975                          (vectorp gnus-current-headers))
8976                     (gnus-mode-string-quote
8977                      (mail-header-subject gnus-current-headers)) ""))
8978                max-len
8979                gnus-tmp-header);; passed as argument to any user-format-funcs
8980           (setq mode-string (eval mformat))
8981           (setq max-len (max 4 (if gnus-mode-non-string-length
8982                                    (- (window-width)
8983                                       gnus-mode-non-string-length)
8984                                  (length mode-string))))
8985           ;; We might have to chop a bit of the string off...
8986           (when (> (length mode-string) max-len)
8987             (setq mode-string
8988                   (concat (gnus-truncate-string mode-string (- max-len 3))
8989                           "...")))
8990           ;; Pad the mode string a bit.
8991           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8992       ;; Update the mode line.
8993       (setq mode-line-buffer-identification (list mode-string))
8994       (set-buffer-modified-p t))))
8995
8996 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8997   "Go through the HEADERS list and add all Xrefs to a hash table.
8998 The resulting hash table is returned, or nil if no Xrefs were found."
8999   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
9000          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
9001          (xref-hashtb (make-vector 63 0))
9002          start group entry number xrefs header)
9003     (while headers
9004       (setq header (pop headers))
9005       (when (and (setq xrefs (mail-header-xref header))
9006                  (not (memq (setq number (mail-header-number header))
9007                             unreads)))
9008         (setq start 0)
9009         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
9010           (setq start (match-end 0))
9011           (setq group (if prefix
9012                           (concat prefix (substring xrefs (match-beginning 1)
9013                                                     (match-end 1)))
9014                         (substring xrefs (match-beginning 1) (match-end 1))))
9015           (setq number
9016                 (string-to-int (substring xrefs (match-beginning 2)
9017                                           (match-end 2))))
9018           (if (setq entry (gnus-gethash group xref-hashtb))
9019               (setcdr entry (cons number (cdr entry)))
9020             (gnus-sethash group (cons number nil) xref-hashtb)))))
9021     (and start xref-hashtb)))
9022
9023 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
9024   "Look through all the headers and mark the Xrefs as read."
9025   (let ((virtual (gnus-virtual-group-p from-newsgroup))
9026         name entry info xref-hashtb idlist method nth4)
9027     (save-excursion
9028       (set-buffer gnus-group-buffer)
9029       (when (setq xref-hashtb
9030                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
9031         (mapatoms
9032          (lambda (group)
9033            (unless (string= from-newsgroup (setq name (symbol-name group)))
9034              (setq idlist (symbol-value group))
9035              ;; Dead groups are not updated.
9036              (and (prog1
9037                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
9038                             info (nth 2 entry))
9039                     (if (stringp (setq nth4 (gnus-info-method info)))
9040                         (setq nth4 (gnus-server-to-method nth4))))
9041                   ;; Only do the xrefs if the group has the same
9042                   ;; select method as the group we have just read.
9043                   (or (gnus-methods-equal-p
9044                        nth4 (gnus-find-method-for-group from-newsgroup))
9045                       virtual
9046                       (equal nth4 (setq method (gnus-find-method-for-group
9047                                                 from-newsgroup)))
9048                       (and (equal (car nth4) (car method))
9049                            (equal (nth 1 nth4) (nth 1 method))))
9050                   gnus-use-cross-reference
9051                   (or (not (eq gnus-use-cross-reference t))
9052                       virtual
9053                       ;; Only do cross-references on subscribed
9054                       ;; groups, if that is what is wanted.
9055                       (<= (gnus-info-level info) gnus-level-subscribed))
9056                   (gnus-group-make-articles-read name idlist))))
9057          xref-hashtb)))))
9058
9059 (defun gnus-group-make-articles-read (group articles)
9060   (let* ((num 0)
9061          (entry (gnus-gethash group gnus-newsrc-hashtb))
9062          (info (nth 2 entry))
9063          (active (gnus-active group))
9064          range)
9065     ;; First peel off all illegal article numbers.
9066     (if active
9067         (let ((ids articles)
9068               id first)
9069           (while ids
9070             (setq id (car ids))
9071             (if (and first (> id (cdr active)))
9072                 (progn
9073                   ;; We'll end up in this situation in one particular
9074                   ;; obscure situation.  If you re-scan a group and get
9075                   ;; a new article that is cross-posted to a different
9076                   ;; group that has not been re-scanned, you might get
9077                   ;; crossposted article that has a higher number than
9078                   ;; Gnus believes possible.  So we re-activate this
9079                   ;; group as well.  This might mean doing the
9080                   ;; crossposting thingy will *increase* the number
9081                   ;; of articles in some groups.  Tsk, tsk.
9082                   (setq active (or (gnus-activate-group group) active))))
9083             (if (or (> id (cdr active))
9084                     (< id (car active)))
9085                 (setq articles (delq id articles)))
9086             (setq ids (cdr ids)))))
9087     ;; If the read list is nil, we init it.
9088     (and active
9089          (null (gnus-info-read info))
9090          (> (car active) 1)
9091          (gnus-info-set-read info (cons 1 (1- (car active)))))
9092     ;; Then we add the read articles to the range.
9093     (gnus-info-set-read
9094      info
9095      (setq range
9096            (gnus-add-to-range
9097             (gnus-info-read info) (setq articles (sort articles '<)))))
9098     ;; Then we have to re-compute how many unread
9099     ;; articles there are in this group.
9100     (if active
9101         (progn
9102           (cond
9103            ((not range)
9104             (setq num (- (1+ (cdr active)) (car active))))
9105            ((not (listp (cdr range)))
9106             (setq num (- (cdr active) (- (1+ (cdr range))
9107                                          (car range)))))
9108            (t
9109             (while range
9110               (if (numberp (car range))
9111                   (setq num (1+ num))
9112                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
9113               (setq range (cdr range)))
9114             (setq num (- (cdr active) num))))
9115           ;; Update the number of unread articles.
9116           (setcar entry num)
9117           ;; Update the group buffer.
9118           (gnus-group-update-group group t)))))
9119
9120 (defun gnus-methods-equal-p (m1 m2)
9121   (let ((m1 (or m1 gnus-select-method))
9122         (m2 (or m2 gnus-select-method)))
9123     (or (equal m1 m2)
9124         (and (eq (car m1) (car m2))
9125              (or (not (memq 'address (assoc (symbol-name (car m1))
9126                                             gnus-valid-select-methods)))
9127                  (equal (nth 1 m1) (nth 1 m2)))))))
9128
9129 (defsubst gnus-header-value ()
9130   (buffer-substring (match-end 0) (gnus-point-at-eol)))
9131
9132 (defvar gnus-newsgroup-none-id 0)
9133
9134 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
9135   (let ((cur nntp-server-buffer)
9136         (dependencies
9137          (or dependencies
9138              (save-excursion (set-buffer gnus-summary-buffer)
9139                              gnus-newsgroup-dependencies)))
9140         headers id id-dep ref-dep end ref)
9141     (save-excursion
9142       (set-buffer nntp-server-buffer)
9143       (run-hooks 'gnus-parse-headers-hook)
9144       (let ((case-fold-search t)
9145             in-reply-to header p lines)
9146         (goto-char (point-min))
9147         ;; Search to the beginning of the next header.  Error messages
9148         ;; do not begin with 2 or 3.
9149         (while (re-search-forward "^[23][0-9]+ " nil t)
9150           (setq id nil
9151                 ref nil)
9152           ;; This implementation of this function, with nine
9153           ;; search-forwards instead of the one re-search-forward and
9154           ;; a case (which basically was the old function) is actually
9155           ;; about twice as fast, even though it looks messier.  You
9156           ;; can't have everything, I guess.  Speed and elegance
9157           ;; doesn't always go hand in hand.
9158           (setq
9159            header
9160            (vector
9161             ;; Number.
9162             (prog1
9163                 (read cur)
9164               (end-of-line)
9165               (setq p (point))
9166               (narrow-to-region (point)
9167                                 (or (and (search-forward "\n.\n" nil t)
9168                                          (- (point) 2))
9169                                     (point))))
9170             ;; Subject.
9171             (progn
9172               (goto-char p)
9173               (if (search-forward "\nsubject: " nil t)
9174                   (gnus-header-value) "(none)"))
9175             ;; From.
9176             (progn
9177               (goto-char p)
9178               (if (search-forward "\nfrom: " nil t)
9179                   (gnus-header-value) "(nobody)"))
9180             ;; Date.
9181             (progn
9182               (goto-char p)
9183               (if (search-forward "\ndate: " nil t)
9184                   (gnus-header-value) ""))
9185             ;; Message-ID.
9186             (progn
9187               (goto-char p)
9188               (if (search-forward "\nmessage-id: " nil t)
9189                   (setq id (gnus-header-value))
9190                 ;; If there was no message-id, we just fake one to make
9191                 ;; subsequent routines simpler.
9192                 (setq id (concat "none+"
9193                                  (int-to-string
9194                                   (setq gnus-newsgroup-none-id
9195                                         (1+ gnus-newsgroup-none-id)))))))
9196             ;; References.
9197             (progn
9198               (goto-char p)
9199               (if (search-forward "\nreferences: " nil t)
9200                   (prog1
9201                       (gnus-header-value)
9202                     (setq end (match-end 0))
9203                     (save-excursion
9204                       (setq ref
9205                             (buffer-substring
9206                              (progn
9207                                (end-of-line)
9208                                (search-backward ">" end t)
9209                                (1+ (point)))
9210                              (progn
9211                                (search-backward "<" end t)
9212                                (point))))))
9213                 ;; Get the references from the in-reply-to header if there
9214                 ;; were no references and the in-reply-to header looks
9215                 ;; promising.
9216                 (if (and (search-forward "\nin-reply-to: " nil t)
9217                          (setq in-reply-to (gnus-header-value))
9218                          (string-match "<[^>]+>" in-reply-to))
9219                     (setq ref (substring in-reply-to (match-beginning 0)
9220                                          (match-end 0)))
9221                   (setq ref ""))))
9222             ;; Chars.
9223             0
9224             ;; Lines.
9225             (progn
9226               (goto-char p)
9227               (if (search-forward "\nlines: " nil t)
9228                   (if (numberp (setq lines (read cur)))
9229                       lines 0)
9230                 0))
9231             ;; Xref.
9232             (progn
9233               (goto-char p)
9234               (and (search-forward "\nxref: " nil t)
9235                    (gnus-header-value)))))
9236           ;; We do the threading while we read the headers.  The
9237           ;; message-id and the last reference are both entered into
9238           ;; the same hash table.  Some tippy-toeing around has to be
9239           ;; done in case an article has arrived before the article
9240           ;; which it refers to.
9241           (if (boundp (setq id-dep (intern id dependencies)))
9242               (if (and (car (symbol-value id-dep))
9243                        (not force-new))
9244                   ;; An article with this Message-ID has already
9245                   ;; been seen, so we ignore this one, except we add
9246                   ;; any additional Xrefs (in case the two articles
9247                   ;; came from different servers).
9248                   (progn
9249                     (mail-header-set-xref
9250                      (car (symbol-value id-dep))
9251                      (concat (or (mail-header-xref
9252                                   (car (symbol-value id-dep))) "")
9253                              (or (mail-header-xref header) "")))
9254                     (setq header nil))
9255                 (setcar (symbol-value id-dep) header))
9256             (set id-dep (list header)))
9257           (when header
9258             (if (boundp (setq ref-dep (intern ref dependencies)))
9259                 (setcdr (symbol-value ref-dep)
9260                         (nconc (cdr (symbol-value ref-dep))
9261                                (list (symbol-value id-dep))))
9262               (set ref-dep (list nil (symbol-value id-dep))))
9263             (setq headers (cons header headers)))
9264           (goto-char (point-max))
9265           (widen))
9266         (nreverse headers)))))
9267
9268 ;; The following macros and functions were written by Felix Lee
9269 ;; <flee@cse.psu.edu>.
9270
9271 (defmacro gnus-nov-read-integer ()
9272   '(prog1
9273        (if (= (following-char) ?\t)
9274            0
9275          (let ((num (condition-case nil (read buffer) (error nil))))
9276            (if (numberp num) num 0)))
9277      (or (eobp) (forward-char 1))))
9278
9279 (defmacro gnus-nov-skip-field ()
9280   '(search-forward "\t" eol 'move))
9281
9282 (defmacro gnus-nov-field ()
9283   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9284
9285 ;; Goes through the xover lines and returns a list of vectors
9286 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
9287                                                   force-new dependencies)
9288   "Parse the news overview data in the server buffer, and return a
9289 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9290   ;; Get the Xref when the users reads the articles since most/some
9291   ;; NNTP servers do not include Xrefs when using XOVER.
9292   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9293   (let ((cur nntp-server-buffer)
9294         (dependencies (or dependencies gnus-newsgroup-dependencies))
9295         number headers header)
9296     (save-excursion
9297       (set-buffer nntp-server-buffer)
9298       ;; Allow the user to mangle the headers before parsing them.
9299       (run-hooks 'gnus-parse-headers-hook)
9300       (goto-char (point-min))
9301       (while (and sequence (not (eobp)))
9302         (setq number (read cur))
9303         (while (and sequence (< (car sequence) number))
9304           (setq sequence (cdr sequence)))
9305         (and sequence
9306              (eq number (car sequence))
9307              (progn
9308                (setq sequence (cdr sequence))
9309                (if (setq header
9310                          (inline (gnus-nov-parse-line
9311                                   number dependencies force-new)))
9312                    (setq headers (cons header headers)))))
9313         (forward-line 1))
9314       (setq headers (nreverse headers)))
9315     headers))
9316
9317 ;; This function has to be called with point after the article number
9318 ;; on the beginning of the line.
9319 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9320   (let ((none 0)
9321         (eol (gnus-point-at-eol))
9322         (buffer (current-buffer))
9323         header ref id id-dep ref-dep)
9324
9325     ;; overview: [num subject from date id refs chars lines misc]
9326     (narrow-to-region (point) eol)
9327     (or (eobp) (forward-char))
9328
9329     (condition-case nil
9330         (setq header
9331               (vector
9332                number                   ; number
9333                (gnus-nov-field)         ; subject
9334                (gnus-nov-field)         ; from
9335                (gnus-nov-field)         ; date
9336                (setq id (or (gnus-nov-field)
9337                             (concat "none+"
9338                                     (int-to-string
9339                                      (setq none (1+ none)))))) ; id
9340                (progn
9341                  (save-excursion
9342                    (let ((beg (point)))
9343                      (search-forward "\t" eol)
9344                      (if (search-backward ">" beg t)
9345                          (setq ref
9346                                (buffer-substring
9347                                 (1+ (point))
9348                                 (search-backward "<" beg t)))
9349                        (setq ref nil))))
9350                  (gnus-nov-field))      ; refs
9351                (gnus-nov-read-integer)  ; chars
9352                (gnus-nov-read-integer)  ; lines
9353                (if (= (following-char) ?\n)
9354                    nil
9355                  (gnus-nov-field))      ; misc
9356                ))
9357       (error (progn
9358                (gnus-error 4 "Strange nov line")
9359                (setq header nil)
9360                (goto-char eol))))
9361
9362     (widen)
9363
9364     ;; We build the thread tree.
9365     (when header
9366       (if (boundp (setq id-dep (intern id dependencies)))
9367           (if (and (car (symbol-value id-dep))
9368                    (not force-new))
9369               ;; An article with this Message-ID has already been seen,
9370               ;; so we ignore this one, except we add any additional
9371               ;; Xrefs (in case the two articles came from different
9372               ;; servers.
9373               (progn
9374                 (mail-header-set-xref
9375                  (car (symbol-value id-dep))
9376                  (concat (or (mail-header-xref
9377                               (car (symbol-value id-dep))) "")
9378                          (or (mail-header-xref header) "")))
9379                 (setq header nil))
9380             (setcar (symbol-value id-dep) header))
9381         (set id-dep (list header))))
9382     (if header
9383         (progn
9384           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9385               (setcdr (symbol-value ref-dep)
9386                       (nconc (cdr (symbol-value ref-dep))
9387                              (list (symbol-value id-dep))))
9388             (set ref-dep (list nil (symbol-value id-dep))))))
9389     header))
9390
9391 (defun gnus-article-get-xrefs ()
9392   "Fill in the Xref value in `gnus-current-headers', if necessary.
9393 This is meant to be called in `gnus-article-internal-prepare-hook'."
9394   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9395                                  gnus-current-headers)))
9396     (or (not gnus-use-cross-reference)
9397         (not headers)
9398         (and (mail-header-xref headers)
9399              (not (string= (mail-header-xref headers) "")))
9400         (let ((case-fold-search t)
9401               xref)
9402           (save-restriction
9403             (nnheader-narrow-to-headers)
9404             (goto-char (point-min))
9405             (if (or (and (eq (downcase (following-char)) ?x)
9406                          (looking-at "Xref:"))
9407                     (search-forward "\nXref:" nil t))
9408                 (progn
9409                   (goto-char (1+ (match-end 0)))
9410                   (setq xref (buffer-substring (point)
9411                                                (progn (end-of-line) (point))))
9412                   (mail-header-set-xref headers xref))))))))
9413
9414 (defun gnus-summary-insert-subject (id &optional old-header)
9415   "Find article ID and insert the summary line for that article."
9416   (let ((header (gnus-read-header id))
9417         (number (and (numberp id) id))
9418         pos)
9419     (when header
9420       ;; Rebuild the thread that this article is part of and go to the
9421       ;; article we have fetched.
9422       (when old-header
9423         (when (setq pos (text-property-any
9424                          (point-min) (point-max) 'gnus-number 
9425                          (mail-header-number old-header)))
9426           (goto-char pos)
9427           (gnus-delete-line)
9428           (gnus-data-remove (mail-header-number old-header))))
9429       (when old-header
9430         (mail-header-set-number header (mail-header-number old-header)))
9431       (setq gnus-newsgroup-sparse
9432             (delq (mail-header-number header) gnus-newsgroup-sparse))
9433       (gnus-rebuild-thread (mail-header-id header))
9434       (gnus-summary-goto-subject (setq number (mail-header-number header))
9435                                  nil t))
9436     (when (and (numberp number)
9437                (> number 0))
9438       ;; We have to update the boundaries even if we can't fetch the
9439       ;; article if ID is a number -- so that the next `P' or `N'
9440       ;; command will fetch the previous (or next) article even
9441       ;; if the one we tried to fetch this time has been canceled.
9442       (and (> number gnus-newsgroup-end)
9443            (setq gnus-newsgroup-end number))
9444       (and (< number gnus-newsgroup-begin)
9445            (setq gnus-newsgroup-begin number))
9446       (setq gnus-newsgroup-unselected
9447             (delq number gnus-newsgroup-unselected)))
9448     ;; Report back a success?
9449     (and header (mail-header-number header))))
9450
9451 (defun gnus-summary-work-articles (n)
9452   "Return a list of articles to be worked upon.  The prefix argument,
9453 the list of process marked articles, and the current article will be
9454 taken into consideration."
9455   (cond
9456    ((and n (numberp n))
9457     ;; A numerical prefix has been given.
9458     (let ((backward (< n 0))
9459           (n (abs n))
9460           articles article)
9461       (save-excursion
9462         (while
9463             (and (> n 0)
9464                  (push (setq article (gnus-summary-article-number))
9465                        articles)
9466                  (if backward
9467                      (gnus-summary-find-prev nil article)
9468                    (gnus-summary-find-next nil article)))
9469           (decf n)))
9470       (nreverse articles)))
9471    ((and (boundp 'transient-mark-mode)
9472          transient-mark-mode
9473          mark-active)
9474     ;; Work on the region between point and mark.
9475     (let ((max (max (point) (mark)))
9476           articles article)
9477       (save-excursion
9478         (goto-char (min (point) (mark)))
9479         (while
9480             (and
9481              (push (setq article (gnus-summary-article-number)) articles)
9482              (gnus-summary-find-next nil article)
9483              (< (point) max)))
9484         (nreverse articles))))
9485    (gnus-newsgroup-processable
9486     ;; There are process-marked articles present.
9487     (reverse gnus-newsgroup-processable))
9488    (t
9489     ;; Just return the current article.
9490     (list (gnus-summary-article-number)))))
9491
9492 (defun gnus-summary-search-group (&optional backward use-level)
9493   "Search for next unread newsgroup.
9494 If optional argument BACKWARD is non-nil, search backward instead."
9495   (save-excursion
9496     (set-buffer gnus-group-buffer)
9497     (if (gnus-group-search-forward
9498          backward nil (if use-level (gnus-group-group-level) nil))
9499         (gnus-group-group-name))))
9500
9501 (defun gnus-summary-best-group (&optional exclude-group)
9502   "Find the name of the best unread group.
9503 If EXCLUDE-GROUP, do not go to this group."
9504   (save-excursion
9505     (set-buffer gnus-group-buffer)
9506     (save-excursion
9507       (gnus-group-best-unread-group exclude-group))))
9508
9509 (defun gnus-summary-find-next (&optional unread article backward)
9510   (if backward (gnus-summary-find-prev)
9511     (let* ((dummy (gnus-summary-article-intangible-p))
9512            (article (or article (gnus-summary-article-number)))
9513            (arts (gnus-data-find-list article))
9514            result)
9515       (when (and (not dummy)
9516                  (or (not gnus-summary-check-current)
9517                      (not unread)
9518                      (not (gnus-data-unread-p (car arts)))))
9519         (setq arts (cdr arts)))
9520       (when (setq result
9521                   (if unread
9522                       (progn
9523                         (while arts
9524                           (when (gnus-data-unread-p (car arts))
9525                             (setq result (car arts)
9526                                   arts nil))
9527                           (setq arts (cdr arts)))
9528                         result)
9529                     (car arts)))
9530         (goto-char (gnus-data-pos result))
9531         (gnus-data-number result)))))
9532
9533 (defun gnus-summary-find-prev (&optional unread article)
9534   (let* ((article (or article (gnus-summary-article-number)))
9535          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9536          result)
9537     (when (or (not gnus-summary-check-current)
9538               (not unread)
9539               (not (gnus-data-unread-p (car arts))))
9540       (setq arts (cdr arts)))
9541     (if (setq result
9542               (if unread
9543                   (progn
9544                     (while arts
9545                       (and (gnus-data-unread-p (car arts))
9546                            (setq result (car arts)
9547                                  arts nil))
9548                       (setq arts (cdr arts)))
9549                     result)
9550                 (car arts)))
9551         (progn
9552           (goto-char (gnus-data-pos result))
9553           (gnus-data-number result)))))
9554
9555 (defun gnus-summary-find-subject (subject &optional unread backward article)
9556   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9557          (article (or article (gnus-summary-article-number)))
9558          (articles (gnus-data-list backward))
9559          (arts (gnus-data-find-list article articles))
9560          result)
9561     (when (or (not gnus-summary-check-current)
9562               (not unread)
9563               (not (gnus-data-unread-p (car arts))))
9564       (setq arts (cdr arts)))
9565     (while arts
9566       (and (or (not unread)
9567                (gnus-data-unread-p (car arts)))
9568            (vectorp (gnus-data-header (car arts)))
9569            (gnus-subject-equal
9570             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9571            (setq result (car arts)
9572                  arts nil))
9573       (setq arts (cdr arts)))
9574     (and result
9575          (goto-char (gnus-data-pos result))
9576          (gnus-data-number result))))
9577
9578 (defun gnus-summary-search-forward (&optional unread subject backward)
9579   "Search forward for an article.
9580 If UNREAD, look for unread articles.  If SUBJECT, look for
9581 articles with that subject.  If BACKWARD, search backward instead."
9582   (cond (subject (gnus-summary-find-subject subject unread backward))
9583         (backward (gnus-summary-find-prev unread))
9584         (t (gnus-summary-find-next unread))))
9585
9586 (defun gnus-recenter (&optional n)
9587   "Center point in window and redisplay frame.
9588 Also do horizontal recentering."
9589   (interactive "P")
9590   (when (and gnus-auto-center-summary
9591              (not (eq gnus-auto-center-summary 'vertical)))
9592     (gnus-horizontal-recenter))
9593   (recenter n))
9594
9595 (defun gnus-summary-recenter ()
9596   "Center point in the summary window.
9597 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9598 displayed, no centering will be performed."
9599   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9600   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9601   (let* ((top (cond ((< (window-height) 4) 0)
9602                     ((< (window-height) 7) 1)
9603                     (t 2)))
9604          (height (1- (window-height)))
9605          (bottom (save-excursion (goto-char (point-max))
9606                                  (forward-line (- height))
9607                                  (point)))
9608          (window (get-buffer-window (current-buffer))))
9609     ;; The user has to want it.
9610     (when gnus-auto-center-summary
9611       (when (get-buffer-window gnus-article-buffer)
9612        ;; Only do recentering when the article buffer is displayed,
9613        ;; Set the window start to either `bottom', which is the biggest
9614        ;; possible valid number, or the second line from the top,
9615        ;; whichever is the least.
9616        (set-window-start
9617         window (min bottom (save-excursion 
9618                              (forward-line (- top)) (point)))))
9619       ;; Do horizontal recentering while we're at it.
9620       (when (and (get-buffer-window (current-buffer) t)
9621                  (not (eq gnus-auto-center-summary 'vertical)))
9622         (let ((selected (selected-window)))
9623           (select-window (get-buffer-window (current-buffer) t))
9624           (gnus-summary-position-point)
9625           (gnus-horizontal-recenter)
9626           (select-window selected))))))
9627
9628 (defun gnus-horizontal-recenter ()
9629   "Recenter the current buffer horizontally."
9630   (if (< (current-column) (/ (window-width) 2))
9631       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9632     (let* ((orig (point))
9633            (end (window-end (get-buffer-window (current-buffer) t)))
9634            (max 0))
9635       ;; Find the longest line currently displayed in the window.
9636       (goto-char (window-start))
9637       (while (and (not (eobp)) 
9638                   (< (point) end))
9639         (end-of-line)
9640         (setq max (max max (current-column)))
9641         (forward-line 1))
9642       (goto-char orig)
9643       ;; Scroll horizontally to center (sort of) the point.
9644       (if (> max (window-width))
9645           (set-window-hscroll 
9646            (get-buffer-window (current-buffer) t)
9647            (min (- (current-column) (/ (window-width) 3))
9648                 (+ 2 (- max (window-width)))))
9649         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9650       max)))
9651
9652 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9653 (defun gnus-short-group-name (group &optional levels)
9654   "Collapse GROUP name LEVELS."
9655   (let* ((name "") 
9656          (foreign "")
9657          (depth 0) 
9658          (skip 1)
9659          (levels (or levels
9660                      (progn
9661                        (while (string-match "\\." group skip)
9662                          (setq skip (match-end 0)
9663                                depth (+ depth 1)))
9664                        depth))))
9665     (if (string-match ":" group)
9666         (setq foreign (substring group 0 (match-end 0))
9667               group (substring group (match-end 0))))
9668     (while group
9669       (if (and (string-match "\\." group)
9670                (> levels (- gnus-group-uncollapsed-levels 1)))
9671           (setq name (concat name (substring group 0 1))
9672                 group (substring group (match-end 0))
9673                 levels (- levels 1)
9674                 name (concat name "."))
9675         (setq name (concat foreign name group)
9676               group nil)))
9677     name))
9678
9679 (defun gnus-summary-jump-to-group (newsgroup)
9680   "Move point to NEWSGROUP in group mode buffer."
9681   ;; Keep update point of group mode buffer if visible.
9682   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9683       (save-window-excursion
9684         ;; Take care of tree window mode.
9685         (if (get-buffer-window gnus-group-buffer)
9686             (pop-to-buffer gnus-group-buffer))
9687         (gnus-group-jump-to-group newsgroup))
9688     (save-excursion
9689       ;; Take care of tree window mode.
9690       (if (get-buffer-window gnus-group-buffer)
9691           (pop-to-buffer gnus-group-buffer)
9692         (set-buffer gnus-group-buffer))
9693       (gnus-group-jump-to-group newsgroup))))
9694
9695 ;; This function returns a list of article numbers based on the
9696 ;; difference between the ranges of read articles in this group and
9697 ;; the range of active articles.
9698 (defun gnus-list-of-unread-articles (group)
9699   (let* ((read (gnus-info-read (gnus-get-info group)))
9700          (active (gnus-active group))
9701          (last (cdr active))
9702          first nlast unread)
9703     ;; If none are read, then all are unread.
9704     (if (not read)
9705         (setq first (car active))
9706       ;; If the range of read articles is a single range, then the
9707       ;; first unread article is the article after the last read
9708       ;; article.  Sounds logical, doesn't it?
9709       (if (not (listp (cdr read)))
9710           (setq first (1+ (cdr read)))
9711         ;; `read' is a list of ranges.
9712         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9713                                 (caar read))) 1)
9714             (setq first 1))
9715         (while read
9716           (if first
9717               (while (< first nlast)
9718                 (setq unread (cons first unread))
9719                 (setq first (1+ first))))
9720           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9721           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9722           (setq read (cdr read)))))
9723     ;; And add the last unread articles.
9724     (while (<= first last)
9725       (setq unread (cons first unread))
9726       (setq first (1+ first)))
9727     ;; Return the list of unread articles.
9728     (nreverse unread)))
9729
9730 (defun gnus-list-of-read-articles (group)
9731   "Return a list of unread, unticked and non-dormant articles."
9732   (let* ((info (gnus-get-info group))
9733          (marked (gnus-info-marks info))
9734          (active (gnus-active group)))
9735     (and info active
9736          (gnus-set-difference
9737           (gnus-sorted-complement
9738            (gnus-uncompress-range active)
9739            (gnus-list-of-unread-articles group))
9740           (append
9741            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9742            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9743
9744 ;; Various summary commands
9745
9746 (defun gnus-summary-universal-argument (arg)
9747   "Perform any operation on all articles that are process/prefixed."
9748   (interactive "P")
9749   (gnus-set-global-variables)
9750   (let ((articles (gnus-summary-work-articles arg))
9751         func article)
9752     (if (eq
9753          (setq
9754           func
9755           (key-binding
9756            (read-key-sequence
9757             (substitute-command-keys
9758              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9759              ))))
9760          'undefined)
9761         (gnus-error 1 "Undefined key")
9762       (save-excursion
9763         (while articles
9764           (gnus-summary-goto-subject (setq article (pop articles)))
9765           (command-execute func)
9766           (gnus-summary-remove-process-mark article)))))
9767   (gnus-summary-position-point))
9768
9769 (defun gnus-summary-toggle-truncation (&optional arg)
9770   "Toggle truncation of summary lines.
9771 With arg, turn line truncation on iff arg is positive."
9772   (interactive "P")
9773   (setq truncate-lines
9774         (if (null arg) (not truncate-lines)
9775           (> (prefix-numeric-value arg) 0)))
9776   (redraw-display))
9777
9778 (defun gnus-summary-reselect-current-group (&optional all rescan)
9779   "Exit and then reselect the current newsgroup.
9780 The prefix argument ALL means to select all articles."
9781   (interactive "P")
9782   (gnus-set-global-variables)
9783   (let ((current-subject (gnus-summary-article-number))
9784         (group gnus-newsgroup-name))
9785     (setq gnus-newsgroup-begin nil)
9786     (gnus-summary-exit)
9787     ;; We have to adjust the point of group mode buffer because the
9788     ;; current point was moved to the next unread newsgroup by
9789     ;; exiting.
9790     (gnus-summary-jump-to-group group)
9791     (when rescan
9792       (save-excursion
9793         (gnus-group-get-new-news-this-group 1)))
9794     (gnus-group-read-group all t)
9795     (gnus-summary-goto-subject current-subject)))
9796
9797 (defun gnus-summary-rescan-group (&optional all)
9798   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9799   (interactive "P")
9800   (gnus-summary-reselect-current-group all t))
9801
9802 (defun gnus-summary-update-info ()
9803   (let* ((group gnus-newsgroup-name))
9804     (when gnus-newsgroup-kill-headers
9805       (setq gnus-newsgroup-killed
9806             (gnus-compress-sequence
9807              (nconc
9808               (gnus-set-sorted-intersection
9809                (gnus-uncompress-range gnus-newsgroup-killed)
9810                (setq gnus-newsgroup-unselected
9811                      (sort gnus-newsgroup-unselected '<)))
9812               (setq gnus-newsgroup-unreads
9813                     (sort gnus-newsgroup-unreads '<))) t)))
9814     (unless (listp (cdr gnus-newsgroup-killed))
9815       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9816     (let ((headers gnus-newsgroup-headers))
9817       (run-hooks 'gnus-exit-group-hook)
9818       (unless gnus-save-score
9819         (setq gnus-newsgroup-scored nil))
9820       ;; Set the new ranges of read articles.
9821       (gnus-update-read-articles
9822        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9823       ;; Set the current article marks.
9824       (gnus-update-marks)
9825       ;; Do the cross-ref thing.
9826       (when gnus-use-cross-reference
9827         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9828       ;; Do adaptive scoring, and possibly save score files.
9829       (when gnus-newsgroup-adaptive
9830         (gnus-score-adaptive))
9831       (when gnus-use-scoring
9832         (gnus-score-save))
9833       ;; Do not switch windows but change the buffer to work.
9834       (set-buffer gnus-group-buffer)
9835       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9836           (gnus-group-update-group group)))))
9837
9838 (defun gnus-summary-exit (&optional temporary)
9839   "Exit reading current newsgroup, and then return to group selection mode.
9840 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9841   (interactive)
9842   (gnus-set-global-variables)
9843   (gnus-kill-save-kill-buffer)
9844   (let* ((group gnus-newsgroup-name)
9845          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9846          (mode major-mode)
9847          (buf (current-buffer)))
9848     (run-hooks 'gnus-summary-prepare-exit-hook)
9849     ;; If we have several article buffers, we kill them at exit.
9850     (unless gnus-single-article-buffer
9851       (gnus-kill-buffer gnus-original-article-buffer)
9852       (setq gnus-article-current nil))
9853     (when gnus-use-cache
9854       (gnus-cache-possibly-remove-articles)
9855       (gnus-cache-save-buffers))
9856     (when gnus-use-trees
9857       (gnus-tree-close group))
9858     ;; Make all changes in this group permanent.
9859     (unless quit-config
9860       (gnus-summary-update-info))
9861     (gnus-close-group group)
9862     ;; Make sure where I was, and go to next newsgroup.
9863     (set-buffer gnus-group-buffer)
9864     (unless quit-config
9865       (gnus-group-jump-to-group group))
9866     (run-hooks 'gnus-summary-exit-hook)
9867     (unless quit-config
9868       (gnus-group-next-unread-group 1))
9869     (if temporary
9870         nil                             ;Nothing to do.
9871       ;; If we have several article buffers, we kill them at exit.
9872       (unless gnus-single-article-buffer
9873         (gnus-kill-buffer gnus-article-buffer)
9874         (gnus-kill-buffer gnus-original-article-buffer)
9875         (setq gnus-article-current nil))
9876       (set-buffer buf)
9877       (if (not gnus-kill-summary-on-exit)
9878           (gnus-deaden-summary)
9879         ;; We set all buffer-local variables to nil.  It is unclear why
9880         ;; this is needed, but if we don't, buffer-local variables are
9881         ;; not garbage-collected, it seems.  This would the lead to en
9882         ;; ever-growing Emacs.
9883         (gnus-summary-clear-local-variables)
9884         (when (get-buffer gnus-article-buffer)
9885           (bury-buffer gnus-article-buffer))
9886         ;; We clear the global counterparts of the buffer-local
9887         ;; variables as well, just to be on the safe side.
9888         (gnus-configure-windows 'group 'force)
9889         (gnus-summary-clear-local-variables)
9890         ;; Return to group mode buffer.
9891         (if (eq mode 'gnus-summary-mode)
9892             (gnus-kill-buffer buf)))
9893       (setq gnus-current-select-method gnus-select-method)
9894       (pop-to-buffer gnus-group-buffer)
9895       ;; Clear the current group name.
9896       (if (not quit-config)
9897           (progn
9898             (gnus-group-jump-to-group group)
9899             (gnus-group-next-unread-group 1)
9900             (gnus-configure-windows 'group 'force))
9901         (if (not (buffer-name (car quit-config)))
9902             (gnus-configure-windows 'group 'force)
9903           (set-buffer (car quit-config))
9904           (and (eq major-mode 'gnus-summary-mode)
9905                (gnus-set-global-variables))
9906           (gnus-configure-windows (cdr quit-config))))
9907       (unless quit-config
9908         (setq gnus-newsgroup-name nil)))))
9909
9910 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9911 (defun gnus-summary-exit-no-update (&optional no-questions)
9912   "Quit reading current newsgroup without updating read article info."
9913   (interactive)
9914   (gnus-set-global-variables)
9915   (let* ((group gnus-newsgroup-name)
9916          (quit-config (gnus-group-quit-config group)))
9917     (when (or no-questions
9918               gnus-expert-user
9919               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9920       ;; If we have several article buffers, we kill them at exit.
9921       (unless gnus-single-article-buffer
9922         (gnus-kill-buffer gnus-article-buffer)
9923         (gnus-kill-buffer gnus-original-article-buffer)
9924         (setq gnus-article-current nil))
9925       (if (not gnus-kill-summary-on-exit)
9926           (gnus-deaden-summary)
9927         (gnus-close-group group)
9928         (gnus-summary-clear-local-variables)
9929         (set-buffer gnus-group-buffer)
9930         (gnus-summary-clear-local-variables)
9931         (when (get-buffer gnus-summary-buffer)
9932           (kill-buffer gnus-summary-buffer)))
9933       (unless gnus-single-article-buffer
9934         (setq gnus-article-current nil))
9935       (when gnus-use-trees
9936         (gnus-tree-close group))
9937       (when (get-buffer gnus-article-buffer)
9938         (bury-buffer gnus-article-buffer))
9939       ;; Return to the group buffer.
9940       (gnus-configure-windows 'group 'force)
9941       ;; Clear the current group name.
9942       (setq gnus-newsgroup-name nil)
9943       (when (equal (gnus-group-group-name) group)
9944         (gnus-group-next-unread-group 1))
9945       (when quit-config
9946         (if (not (buffer-name (car quit-config)))
9947             (gnus-configure-windows 'group 'force)
9948           (set-buffer (car quit-config))
9949           (when (eq major-mode 'gnus-summary-mode)
9950             (gnus-set-global-variables))
9951           (gnus-configure-windows (cdr quit-config)))))))
9952
9953 ;;; Dead summaries.
9954
9955 (defvar gnus-dead-summary-mode-map nil)
9956
9957 (if gnus-dead-summary-mode-map
9958     nil
9959   (setq gnus-dead-summary-mode-map (make-keymap))
9960   (suppress-keymap gnus-dead-summary-mode-map)
9961   (substitute-key-definition
9962    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9963   (let ((keys '("\C-d" "\r" "\177")))
9964     (while keys
9965       (define-key gnus-dead-summary-mode-map
9966         (pop keys) 'gnus-summary-wake-up-the-dead))))
9967
9968 (defvar gnus-dead-summary-mode nil
9969   "Minor mode for Gnus summary buffers.")
9970
9971 (defun gnus-dead-summary-mode (&optional arg)
9972   "Minor mode for Gnus summary buffers."
9973   (interactive "P")
9974   (when (eq major-mode 'gnus-summary-mode)
9975     (make-local-variable 'gnus-dead-summary-mode)
9976     (setq gnus-dead-summary-mode
9977           (if (null arg) (not gnus-dead-summary-mode)
9978             (> (prefix-numeric-value arg) 0)))
9979     (when gnus-dead-summary-mode
9980       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9981         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9982       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9983         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9984               minor-mode-map-alist)))))
9985
9986 (defun gnus-deaden-summary ()
9987   "Make the current summary buffer into a dead summary buffer."
9988   ;; Kill any previous dead summary buffer.
9989   (when (and gnus-dead-summary
9990              (buffer-name gnus-dead-summary))
9991     (save-excursion
9992       (set-buffer gnus-dead-summary)
9993       (when gnus-dead-summary-mode
9994         (kill-buffer (current-buffer)))))
9995   ;; Make this the current dead summary.
9996   (setq gnus-dead-summary (current-buffer))
9997   (gnus-dead-summary-mode 1)
9998   (let ((name (buffer-name)))
9999     (when (string-match "Summary" name)
10000       (rename-buffer
10001        (concat (substring name 0 (match-beginning 0)) "Dead "
10002                (substring name (match-beginning 0))) t))))
10003
10004 (defun gnus-kill-or-deaden-summary (buffer)
10005   "Kill or deaden the summary BUFFER."
10006   (when (and (buffer-name buffer)
10007              (not gnus-single-article-buffer))
10008     (save-excursion
10009       (set-buffer buffer)
10010       (gnus-kill-buffer gnus-article-buffer)
10011       (gnus-kill-buffer gnus-original-article-buffer)))
10012   (cond (gnus-kill-summary-on-exit
10013          (when (and gnus-use-trees
10014                     (and (get-buffer buffer)
10015                          (buffer-name (get-buffer buffer))))
10016            (save-excursion
10017              (set-buffer (get-buffer buffer))
10018              (gnus-tree-close gnus-newsgroup-name)))
10019          (gnus-kill-buffer buffer))
10020         ((and (get-buffer buffer)
10021               (buffer-name (get-buffer buffer)))
10022          (save-excursion
10023            (set-buffer buffer)
10024            (gnus-deaden-summary)))))
10025
10026 (defun gnus-summary-wake-up-the-dead (&rest args)
10027   "Wake up the dead summary buffer."
10028   (interactive)
10029   (gnus-dead-summary-mode -1)
10030   (let ((name (buffer-name)))
10031     (when (string-match "Dead " name)
10032       (rename-buffer
10033        (concat (substring name 0 (match-beginning 0))
10034                (substring name (match-end 0))) t)))
10035   (gnus-message 3 "This dead summary is now alive again"))
10036
10037 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
10038 (defun gnus-summary-fetch-faq (&optional faq-dir)
10039   "Fetch the FAQ for the current group.
10040 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
10041 in."
10042   (interactive
10043    (list
10044     (if current-prefix-arg
10045         (completing-read
10046          "Faq dir: " (and (listp gnus-group-faq-directory)
10047                           gnus-group-faq-directory)))))
10048   (let (gnus-faq-buffer)
10049     (and (setq gnus-faq-buffer
10050                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
10051          (gnus-configure-windows 'summary-faq))))
10052
10053 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
10054 (defun gnus-summary-describe-group (&optional force)
10055   "Describe the current newsgroup."
10056   (interactive "P")
10057   (gnus-group-describe-group force gnus-newsgroup-name))
10058
10059 (defun gnus-summary-describe-briefly ()
10060   "Describe summary mode commands briefly."
10061   (interactive)
10062   (gnus-message 6
10063                 (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")))
10064
10065 ;; Walking around group mode buffer from summary mode.
10066
10067 (defun gnus-summary-next-group (&optional no-article target-group backward)
10068   "Exit current newsgroup and then select next unread newsgroup.
10069 If prefix argument NO-ARTICLE is non-nil, no article is selected
10070 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
10071 previous group instead."
10072   (interactive "P")
10073   (gnus-set-global-variables)
10074   (let ((current-group gnus-newsgroup-name)
10075         (current-buffer (current-buffer))
10076         entered)
10077     ;; First we semi-exit this group to update Xrefs and all variables.
10078     ;; We can't do a real exit, because the window conf must remain
10079     ;; the same in case the user is prompted for info, and we don't
10080     ;; want the window conf to change before that...
10081     (gnus-summary-exit t)
10082     (while (not entered)
10083       ;; Then we find what group we are supposed to enter.
10084       (set-buffer gnus-group-buffer)
10085       (gnus-group-jump-to-group current-group)
10086       (setq target-group
10087             (or target-group
10088                 (if (eq gnus-keep-same-level 'best)
10089                     (gnus-summary-best-group gnus-newsgroup-name)
10090                   (gnus-summary-search-group backward gnus-keep-same-level))))
10091       (if (not target-group)
10092           ;; There are no further groups, so we return to the group
10093           ;; buffer.
10094           (progn
10095             (gnus-message 5 "Returning to the group buffer")
10096             (setq entered t)
10097             (set-buffer current-buffer)
10098             (gnus-summary-exit))
10099         ;; We try to enter the target group.
10100         (gnus-group-jump-to-group target-group)
10101         (let ((unreads (gnus-group-group-unread)))
10102           (if (and (or (eq t unreads)
10103                        (and unreads (not (zerop unreads))))
10104                    (gnus-summary-read-group
10105                     target-group nil no-article current-buffer))
10106               (setq entered t)
10107             (setq current-group target-group
10108                   target-group nil)))))))
10109
10110 (defun gnus-summary-prev-group (&optional no-article)
10111   "Exit current newsgroup and then select previous unread newsgroup.
10112 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
10113   (interactive "P")
10114   (gnus-summary-next-group no-article nil t))
10115
10116 ;; Walking around summary lines.
10117
10118 (defun gnus-summary-first-subject (&optional unread)
10119   "Go to the first unread subject.
10120 If UNREAD is non-nil, go to the first unread article.
10121 Returns the article selected or nil if there are no unread articles."
10122   (interactive "P")
10123   (prog1
10124       (cond
10125        ;; Empty summary.
10126        ((null gnus-newsgroup-data)
10127         (gnus-message 3 "No articles in the group")
10128         nil)
10129        ;; Pick the first article.
10130        ((not unread)
10131         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
10132         (gnus-data-number (car gnus-newsgroup-data)))
10133        ;; No unread articles.
10134        ((null gnus-newsgroup-unreads)
10135         (gnus-message 3 "No more unread articles")
10136         nil)
10137        ;; Find the first unread article.
10138        (t
10139         (let ((data gnus-newsgroup-data))
10140           (while (and data
10141                       (not (gnus-data-unread-p (car data))))
10142             (setq data (cdr data)))
10143           (if data
10144               (progn
10145                 (goto-char (gnus-data-pos (car data)))
10146                 (gnus-data-number (car data)))))))
10147     (gnus-summary-position-point)))
10148
10149 (defun gnus-summary-next-subject (n &optional unread dont-display)
10150   "Go to next N'th summary line.
10151 If N is negative, go to the previous N'th subject line.
10152 If UNREAD is non-nil, only unread articles are selected.
10153 The difference between N and the actual number of steps taken is
10154 returned."
10155   (interactive "p")
10156   (let ((backward (< n 0))
10157         (n (abs n)))
10158     (while (and (> n 0)
10159                 (if backward
10160                     (gnus-summary-find-prev unread)
10161                   (gnus-summary-find-next unread)))
10162       (setq n (1- n)))
10163     (if (/= 0 n) (gnus-message 7 "No more%s articles"
10164                                (if unread " unread" "")))
10165     (unless dont-display
10166       (gnus-summary-recenter)
10167       (gnus-summary-position-point))
10168     n))
10169
10170 (defun gnus-summary-next-unread-subject (n)
10171   "Go to next N'th unread summary line."
10172   (interactive "p")
10173   (gnus-summary-next-subject n t))
10174
10175 (defun gnus-summary-prev-subject (n &optional unread)
10176   "Go to previous N'th summary line.
10177 If optional argument UNREAD is non-nil, only unread article is selected."
10178   (interactive "p")
10179   (gnus-summary-next-subject (- n) unread))
10180
10181 (defun gnus-summary-prev-unread-subject (n)
10182   "Go to previous N'th unread summary line."
10183   (interactive "p")
10184   (gnus-summary-next-subject (- n) t))
10185
10186 (defun gnus-summary-goto-subject (article &optional force silent)
10187   "Go the subject line of ARTICLE.
10188 If FORCE, also allow jumping to articles not currently shown."
10189   (let ((b (point))
10190         (data (gnus-data-find article)))
10191     ;; We read in the article if we have to.
10192     (and (not data)
10193          force
10194          (gnus-summary-insert-subject article)
10195          (setq data (gnus-data-find article)))
10196     (goto-char b)
10197     (if (not data)
10198         (progn
10199           (unless silent
10200             (gnus-message 3 "Can't find article %d" article))
10201           nil)
10202       (goto-char (gnus-data-pos data))
10203       article)))
10204
10205 ;; Walking around summary lines with displaying articles.
10206
10207 (defun gnus-summary-expand-window (&optional arg)
10208   "Make the summary buffer take up the entire Emacs frame.
10209 Given a prefix, will force an `article' buffer configuration."
10210   (interactive "P")
10211   (gnus-set-global-variables)
10212   (if arg
10213       (gnus-configure-windows 'article 'force)
10214     (gnus-configure-windows 'summary 'force)))
10215
10216 (defun gnus-summary-display-article (article &optional all-header)
10217   "Display ARTICLE in article buffer."
10218   (gnus-set-global-variables)
10219   (if (null article)
10220       nil
10221     (prog1
10222         (if gnus-summary-display-article-function
10223             (funcall gnus-summary-display-article-function article all-header)
10224           (gnus-article-prepare article all-header))
10225       (run-hooks 'gnus-select-article-hook)
10226       (unless (zerop gnus-current-article)
10227         (gnus-summary-goto-subject gnus-current-article))
10228       (gnus-summary-recenter)
10229       (when gnus-use-trees
10230         (gnus-possibly-generate-tree article)
10231         (gnus-highlight-selected-tree article))
10232       ;; Successfully display article.
10233       (gnus-article-set-window-start
10234        (cdr (assq article gnus-newsgroup-bookmarks))))))
10235
10236 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
10237   "Select the current article.
10238 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
10239 non-nil, the article will be re-fetched even if it already present in
10240 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
10241 be displayed."
10242   ;; Make sure we are in the summary buffer to work around bbdb bug.
10243   (unless (eq major-mode 'gnus-summary-mode)
10244     (set-buffer gnus-summary-buffer))
10245   (let ((article (or article (gnus-summary-article-number)))
10246         (all-headers (not (not all-headers))) ;Must be T or NIL.
10247         gnus-summary-display-article-function
10248         did)
10249     (and (not pseudo)
10250          (gnus-summary-article-pseudo-p article)
10251          (error "This is a pseudo-article."))
10252     (prog1
10253         (save-excursion
10254           (set-buffer gnus-summary-buffer)
10255           (if (or (and gnus-single-article-buffer
10256                        (or (null gnus-current-article)
10257                            (null gnus-article-current)
10258                            (null (get-buffer gnus-article-buffer))
10259                            (not (eq article (cdr gnus-article-current)))
10260                            (not (equal (car gnus-article-current)
10261                                        gnus-newsgroup-name))))
10262                   (and (not gnus-single-article-buffer)
10263                        (or (null gnus-current-article)
10264                            (not (eq gnus-current-article article))))
10265                   force)
10266               ;; The requested article is different from the current article.
10267               (prog1
10268                   (gnus-summary-display-article article all-headers)
10269                 (setq did article))
10270             (if (or all-headers gnus-show-all-headers)
10271                 (gnus-article-show-all-headers))
10272             'old))
10273       (if did
10274           (gnus-article-set-window-start
10275            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10276
10277 (defun gnus-summary-set-current-mark (&optional current-mark)
10278   "Obsolete function."
10279   nil)
10280
10281 (defun gnus-summary-next-article (&optional unread subject backward push)
10282   "Select the next article.
10283 If UNREAD, only unread articles are selected.
10284 If SUBJECT, only articles with SUBJECT are selected.
10285 If BACKWARD, the previous article is selected instead of the next."
10286   (interactive "P")
10287   (gnus-set-global-variables)
10288   (cond
10289    ;; Is there such an article?
10290    ((and (gnus-summary-search-forward unread subject backward)
10291          (or (gnus-summary-display-article (gnus-summary-article-number))
10292              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10293     (gnus-summary-position-point))
10294    ;; If not, we try the first unread, if that is wanted.
10295    ((and subject
10296          gnus-auto-select-same
10297          (or (gnus-summary-first-unread-article)
10298              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10299     (gnus-summary-position-point)
10300     (gnus-message 6 "Wrapped"))
10301    ;; Try to get next/previous article not displayed in this group.
10302    ((and gnus-auto-extend-newsgroup
10303          (not unread) (not subject))
10304     (gnus-summary-goto-article
10305      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10306      nil t))
10307    ;; Go to next/previous group.
10308    (t
10309     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10310         (gnus-summary-jump-to-group gnus-newsgroup-name))
10311     (let ((cmd last-command-char)
10312           (group
10313            (if (eq gnus-keep-same-level 'best)
10314                (gnus-summary-best-group gnus-newsgroup-name)
10315              (gnus-summary-search-group backward gnus-keep-same-level))))
10316       ;; For some reason, the group window gets selected.  We change
10317       ;; it back.
10318       (select-window (get-buffer-window (current-buffer)))
10319       ;; Select next unread newsgroup automagically.
10320       (cond
10321        ((not gnus-auto-select-next)
10322         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10323        ((or (eq gnus-auto-select-next 'quietly)
10324             (and (eq gnus-auto-select-next 'slightly-quietly)
10325                  push)
10326             (and (eq gnus-auto-select-next 'almost-quietly)
10327                  (gnus-summary-last-article-p)))
10328         ;; Select quietly.
10329         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10330             (gnus-summary-exit)
10331           (gnus-message 7 "No more%s articles (%s)..."
10332                         (if unread " unread" "")
10333                         (if group (concat "selecting " group)
10334                           "exiting"))
10335           (gnus-summary-next-group nil group backward)))
10336        (t
10337         (gnus-summary-walk-group-buffer
10338          gnus-newsgroup-name cmd unread backward)))))))
10339
10340 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10341   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10342                       (?\C-p (gnus-group-prev-unread-group 1))))
10343         keve key group ended)
10344     (save-excursion
10345       (set-buffer gnus-group-buffer)
10346       (gnus-summary-jump-to-group from-group)
10347       (setq group
10348             (if (eq gnus-keep-same-level 'best)
10349                 (gnus-summary-best-group gnus-newsgroup-name)
10350               (gnus-summary-search-group backward gnus-keep-same-level))))
10351     (while (not ended)
10352       (gnus-message
10353        5 "No more%s articles%s" (if unread " unread" "")
10354        (if (and group
10355                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10356            (format " (Type %s for %s [%s])"
10357                    (single-key-description cmd) group
10358                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10359          (format " (Type %s to exit %s)"
10360                  (single-key-description cmd)
10361                  gnus-newsgroup-name)))
10362       ;; Confirm auto selection.
10363       (setq key (car (setq keve (gnus-read-event-char))))
10364       (setq ended t)
10365       (cond
10366        ((assq key keystrokes)
10367         (let ((obuf (current-buffer)))
10368           (switch-to-buffer gnus-group-buffer)
10369           (and group
10370                (gnus-group-jump-to-group group))
10371           (eval (cadr (assq key keystrokes)))
10372           (setq group (gnus-group-group-name))
10373           (switch-to-buffer obuf))
10374         (setq ended nil))
10375        ((equal key cmd)
10376         (if (or (not group)
10377                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10378             (gnus-summary-exit)
10379           (gnus-summary-next-group nil group backward)))
10380        (t
10381         (push (cdr keve) unread-command-events))))))
10382
10383 (defun gnus-read-event-char ()
10384   "Get the next event."
10385   (let ((event (read-event)))
10386     (cons (and (numberp event) event) event)))
10387
10388 (defun gnus-summary-next-unread-article ()
10389   "Select unread article after current one."
10390   (interactive)
10391   (gnus-summary-next-article t (and gnus-auto-select-same
10392                                     (gnus-summary-article-subject))))
10393
10394 (defun gnus-summary-prev-article (&optional unread subject)
10395   "Select the article after the current one.
10396 If UNREAD is non-nil, only unread articles are selected."
10397   (interactive "P")
10398   (gnus-summary-next-article unread subject t))
10399
10400 (defun gnus-summary-prev-unread-article ()
10401   "Select unred article before current one."
10402   (interactive)
10403   (gnus-summary-prev-article t (and gnus-auto-select-same
10404                                     (gnus-summary-article-subject))))
10405
10406 (defun gnus-summary-next-page (&optional lines circular)
10407   "Show next page of the selected article.
10408 If at the end of the current article, select the next article.
10409 LINES says how many lines should be scrolled up.
10410
10411 If CIRCULAR is non-nil, go to the start of the article instead of
10412 selecting the next article when reaching the end of the current
10413 article."
10414   (interactive "P")
10415   (setq gnus-summary-buffer (current-buffer))
10416   (gnus-set-global-variables)
10417   (let ((article (gnus-summary-article-number))
10418         (endp nil))
10419     (gnus-configure-windows 'article)
10420     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
10421         (if (and (eq gnus-summary-goto-unread 'never)
10422                  (not (gnus-summary-last-article-p article)))
10423             (gnus-summary-next-article)
10424           (gnus-summary-next-unread-article))
10425       (if (or (null gnus-current-article)
10426               (null gnus-article-current)
10427               (/= article (cdr gnus-article-current))
10428               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10429           ;; Selected subject is different from current article's.
10430           (gnus-summary-display-article article)
10431         (gnus-eval-in-buffer-window
10432          gnus-article-buffer
10433          (setq endp (gnus-article-next-page lines)))
10434         (if endp
10435             (cond (circular
10436                    (gnus-summary-beginning-of-article))
10437                   (lines
10438                    (gnus-message 3 "End of message"))
10439                   ((null lines)
10440                    (if (and (eq gnus-summary-goto-unread 'never)
10441                             (not (gnus-summary-last-article-p article)))
10442                        (gnus-summary-next-article)
10443                      (gnus-summary-next-unread-article)))))))
10444     (gnus-summary-recenter)
10445     (gnus-summary-position-point)))
10446
10447 (defun gnus-summary-prev-page (&optional lines)
10448   "Show previous page of selected article.
10449 Argument LINES specifies lines to be scrolled down."
10450   (interactive "P")
10451   (gnus-set-global-variables)
10452   (let ((article (gnus-summary-article-number)))
10453     (gnus-configure-windows 'article)
10454     (if (or (null gnus-current-article)
10455             (null gnus-article-current)
10456             (/= article (cdr gnus-article-current))
10457             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10458         ;; Selected subject is different from current article's.
10459         (gnus-summary-display-article article)
10460       (gnus-summary-recenter)
10461       (gnus-eval-in-buffer-window gnus-article-buffer
10462                                   (gnus-article-prev-page lines))))
10463   (gnus-summary-position-point))
10464
10465 (defun gnus-summary-scroll-up (lines)
10466   "Scroll up (or down) one line current article.
10467 Argument LINES specifies lines to be scrolled up (or down if negative)."
10468   (interactive "p")
10469   (gnus-set-global-variables)
10470   (gnus-configure-windows 'article)
10471   (gnus-summary-show-thread)
10472   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10473     (gnus-eval-in-buffer-window
10474      gnus-article-buffer
10475      (cond ((> lines 0)
10476             (if (gnus-article-next-page lines)
10477                 (gnus-message 3 "End of message")))
10478            ((< lines 0)
10479             (gnus-article-prev-page (- lines))))))
10480   (gnus-summary-recenter)
10481   (gnus-summary-position-point))
10482
10483 (defun gnus-summary-next-same-subject ()
10484   "Select next article which has the same subject as current one."
10485   (interactive)
10486   (gnus-set-global-variables)
10487   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10488
10489 (defun gnus-summary-prev-same-subject ()
10490   "Select previous article which has the same subject as current one."
10491   (interactive)
10492   (gnus-set-global-variables)
10493   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10494
10495 (defun gnus-summary-next-unread-same-subject ()
10496   "Select next unread article which has the same subject as current one."
10497   (interactive)
10498   (gnus-set-global-variables)
10499   (gnus-summary-next-article t (gnus-summary-article-subject)))
10500
10501 (defun gnus-summary-prev-unread-same-subject ()
10502   "Select previous unread article which has the same subject as current one."
10503   (interactive)
10504   (gnus-set-global-variables)
10505   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10506
10507 (defun gnus-summary-first-unread-article ()
10508   "Select the first unread article.
10509 Return nil if there are no unread articles."
10510   (interactive)
10511   (gnus-set-global-variables)
10512   (prog1
10513       (if (gnus-summary-first-subject t)
10514           (progn
10515             (gnus-summary-show-thread)
10516             (gnus-summary-first-subject t)
10517             (gnus-summary-display-article (gnus-summary-article-number))))
10518     (gnus-summary-position-point)))
10519
10520 (defun gnus-summary-best-unread-article ()
10521   "Select the unread article with the highest score."
10522   (interactive)
10523   (gnus-set-global-variables)
10524   (let ((best -1000000)
10525         (data gnus-newsgroup-data)
10526         article score)
10527     (while data
10528       (and (gnus-data-unread-p (car data))
10529            (> (setq score
10530                     (gnus-summary-article-score (gnus-data-number (car data))))
10531               best)
10532            (setq best score
10533                  article (gnus-data-number (car data))))
10534       (setq data (cdr data)))
10535     (prog1
10536         (if article
10537             (gnus-summary-goto-article article)
10538           (error "No unread articles"))
10539       (gnus-summary-position-point))))
10540
10541 (defun gnus-summary-last-subject ()
10542   "Go to the last displayed subject line in the group."
10543   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10544     (when article
10545       (gnus-summary-goto-subject article))))
10546
10547 (defun gnus-summary-goto-article (article &optional all-headers force)
10548   "Fetch ARTICLE and display it if it exists.
10549 If ALL-HEADERS is non-nil, no header lines are hidden."
10550   (interactive
10551    (list
10552     (string-to-int
10553      (completing-read
10554       "Article number: "
10555       (mapcar (lambda (number) (list (int-to-string number)))
10556               gnus-newsgroup-limit)))
10557     current-prefix-arg
10558     t))
10559   (prog1
10560       (if (gnus-summary-goto-subject article force)
10561           (gnus-summary-display-article article all-headers)
10562         (gnus-message 4 "Couldn't go to article %s" article) nil)
10563     (gnus-summary-position-point)))
10564
10565 (defun gnus-summary-goto-last-article ()
10566   "Go to the previously read article."
10567   (interactive)
10568   (prog1
10569       (and gnus-last-article
10570            (gnus-summary-goto-article gnus-last-article))
10571     (gnus-summary-position-point)))
10572
10573 (defun gnus-summary-pop-article (number)
10574   "Pop one article off the history and go to the previous.
10575 NUMBER articles will be popped off."
10576   (interactive "p")
10577   (let (to)
10578     (setq gnus-newsgroup-history
10579           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10580     (if to
10581         (gnus-summary-goto-article (car to))
10582       (error "Article history empty")))
10583   (gnus-summary-position-point))
10584
10585 ;; Summary commands and functions for limiting the summary buffer.
10586
10587 (defun gnus-summary-limit-to-articles (n)
10588   "Limit the summary buffer to the next N articles.
10589 If not given a prefix, use the process marked articles instead."
10590   (interactive "P")
10591   (gnus-set-global-variables)
10592   (prog1
10593       (let ((articles (gnus-summary-work-articles n)))
10594         (setq gnus-newsgroup-processable nil)
10595         (gnus-summary-limit articles))
10596     (gnus-summary-position-point)))
10597
10598 (defun gnus-summary-pop-limit (&optional total)
10599   "Restore the previous limit.
10600 If given a prefix, remove all limits."
10601   (interactive "P")
10602   (gnus-set-global-variables)
10603   (when total 
10604     (setq gnus-newsgroup-limits
10605           (list (mapcar (lambda (h) (mail-header-number h))
10606                         gnus-newsgroup-headers))))
10607   (unless gnus-newsgroup-limits
10608     (error "No limit to pop"))
10609   (prog1
10610       (gnus-summary-limit nil 'pop)
10611     (gnus-summary-position-point)))
10612
10613 (defun gnus-summary-limit-to-subject (subject &optional header)
10614   "Limit the summary buffer to articles that have subjects that match a regexp."
10615   (interactive "sRegexp: ")
10616   (unless header
10617     (setq header "subject"))
10618   (when (not (equal "" subject))
10619     (prog1
10620         (let ((articles (gnus-summary-find-matching
10621                          (or header "subject") subject 'all)))
10622           (or articles (error "Found no matches for \"%s\"" subject))
10623           (gnus-summary-limit articles))
10624       (gnus-summary-position-point))))
10625
10626 (defun gnus-summary-limit-to-author (from)
10627   "Limit the summary buffer to articles that have authors that match a regexp."
10628   (interactive "sRegexp: ")
10629   (gnus-summary-limit-to-subject from "from"))
10630
10631 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10632 (make-obsolete
10633  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10634
10635 (defun gnus-summary-limit-to-unread (&optional all)
10636   "Limit the summary buffer to articles that are not marked as read.
10637 If ALL is non-nil, limit strictly to unread articles."
10638   (interactive "P")
10639   (if all
10640       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10641     (gnus-summary-limit-to-marks
10642      ;; Concat all the marks that say that an article is read and have
10643      ;; those removed.
10644      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10645            gnus-killed-mark gnus-kill-file-mark
10646            gnus-low-score-mark gnus-expirable-mark
10647            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10648      'reverse)))
10649
10650 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10651 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10652
10653 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10654   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10655 If REVERSE, limit the summary buffer to articles that are not marked
10656 with MARKS.  MARKS can either be a string of marks or a list of marks.
10657 Returns how many articles were removed."
10658   (interactive "sMarks: ")
10659   (gnus-set-global-variables)
10660   (prog1
10661       (let ((data gnus-newsgroup-data)
10662             (marks (if (listp marks) marks
10663                      (append marks nil))) ; Transform to list.
10664             articles)
10665         (while data
10666           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10667                  (memq (gnus-data-mark (car data)) marks))
10668                (setq articles (cons (gnus-data-number (car data)) articles)))
10669           (setq data (cdr data)))
10670         (gnus-summary-limit articles))
10671     (gnus-summary-position-point)))
10672
10673 (defun gnus-summary-limit-to-score (&optional score)
10674   "Limit to articles with score at or above SCORE."
10675   (interactive "P")
10676   (gnus-set-global-variables)
10677   (setq score (if score
10678                   (prefix-numeric-value score)
10679                 (or gnus-summary-default-score 0)))
10680   (let ((data gnus-newsgroup-data)
10681         articles)
10682     (while data
10683       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10684                 score)
10685         (push (gnus-data-number (car data)) articles))
10686       (setq data (cdr data)))
10687     (prog1
10688         (gnus-summary-limit articles)
10689       (gnus-summary-position-point))))
10690
10691 (defun gnus-summary-limit-include-dormant ()
10692   "Display all the hidden articles that are marked as dormant."
10693   (interactive)
10694   (gnus-set-global-variables)
10695   (or gnus-newsgroup-dormant
10696       (error "There are no dormant articles in this group"))
10697   (prog1
10698       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10699     (gnus-summary-position-point)))
10700
10701 (defun gnus-summary-limit-exclude-dormant ()
10702   "Hide all dormant articles."
10703   (interactive)
10704   (gnus-set-global-variables)
10705   (prog1
10706       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10707     (gnus-summary-position-point)))
10708
10709 (defun gnus-summary-limit-exclude-childless-dormant ()
10710   "Hide all dormant articles that have no children."
10711   (interactive)
10712   (gnus-set-global-variables)
10713   (let ((data (gnus-data-list t))
10714         articles d children)
10715     ;; Find all articles that are either not dormant or have
10716     ;; children.
10717     (while (setq d (pop data))
10718       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10719                 (and (setq children 
10720                            (gnus-article-children (gnus-data-number d)))
10721                      (let (found)
10722                        (while children
10723                          (when (memq (car children) articles)
10724                            (setq children nil
10725                                  found t))
10726                          (pop children))
10727                        found)))
10728         (push (gnus-data-number d) articles)))
10729     ;; Do the limiting.
10730     (prog1
10731         (gnus-summary-limit articles)
10732       (gnus-summary-position-point))))
10733
10734 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10735   "Mark all unread excluded articles as read.
10736 If ALL, mark even excluded ticked and dormants as read."
10737   (interactive "P")
10738   (let ((articles (gnus-sorted-complement
10739                    (sort
10740                     (mapcar (lambda (h) (mail-header-number h))
10741                             gnus-newsgroup-headers)
10742                     '<)
10743                    (sort gnus-newsgroup-limit '<)))
10744         article)
10745     (setq gnus-newsgroup-unreads nil)
10746     (if all
10747         (setq gnus-newsgroup-dormant nil
10748               gnus-newsgroup-marked nil
10749               gnus-newsgroup-reads
10750               (nconc
10751                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10752                gnus-newsgroup-reads))
10753       (while (setq article (pop articles))
10754         (unless (or (memq article gnus-newsgroup-dormant)
10755                     (memq article gnus-newsgroup-marked))
10756           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10757
10758 (defun gnus-summary-limit (articles &optional pop)
10759   (if pop
10760       ;; We pop the previous limit off the stack and use that.
10761       (setq articles (car gnus-newsgroup-limits)
10762             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10763     ;; We use the new limit, so we push the old limit on the stack.
10764     (setq gnus-newsgroup-limits
10765           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10766   ;; Set the limit.
10767   (setq gnus-newsgroup-limit articles)
10768   (let ((total (length gnus-newsgroup-data))
10769         (data (gnus-data-find-list (gnus-summary-article-number)))
10770         found)
10771     ;; This will do all the work of generating the new summary buffer
10772     ;; according to the new limit.
10773     (gnus-summary-prepare)
10774     ;; Hide any threads, possibly.
10775     (and gnus-show-threads
10776          gnus-thread-hide-subtree
10777          (gnus-summary-hide-all-threads))
10778     ;; Try to return to the article you were at, or one in the
10779     ;; neighborhood.
10780     (if data
10781         ;; We try to find some article after the current one.
10782         (while data
10783           (and (gnus-summary-goto-subject
10784                 (gnus-data-number (car data)) nil t)
10785                (setq data nil
10786                      found t))
10787           (setq data (cdr data))))
10788     (or found
10789         ;; If there is no data, that means that we were after the last
10790         ;; article.  The same goes when we can't find any articles
10791         ;; after the current one.
10792         (progn
10793           (goto-char (point-max))
10794           (gnus-summary-find-prev)))
10795     ;; We return how many articles were removed from the summary
10796     ;; buffer as a result of the new limit.
10797     (- total (length gnus-newsgroup-data))))
10798
10799 (defsubst gnus-cut-thread (thread)
10800   "Go forwards in the thread until we find an article that we want to display."
10801   (when (eq gnus-fetch-old-headers 'some)
10802     ;; Deal with old-fetched headers.
10803     (while (and thread
10804                 (memq (mail-header-number (car thread)) 
10805                       gnus-newsgroup-ancient)
10806                 (<= (length (cdr thread)) 1))
10807       (setq thread (cadr thread))))
10808   ;; Deal with sparse threads.
10809   (when (or (eq gnus-build-sparse-threads 'some)
10810             (eq gnus-build-sparse-threads 'more))
10811     (while (and thread
10812                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10813                 (= (length (cdr thread)) 1))
10814       (setq thread (cadr thread))))
10815   thread)
10816
10817 (defun gnus-cut-threads (threads)
10818   "Cut off all uninteresting articles from the beginning of threads."
10819   (when (or (eq gnus-fetch-old-headers 'some)
10820             (eq gnus-build-sparse-threads 'some)
10821             (eq gnus-build-sparse-threads 'more))
10822     (let ((th threads))
10823       (while th
10824         (setcar th (gnus-cut-thread (car th)))
10825         (setq th (cdr th)))))
10826   ;; Remove nixed out threads.
10827   (delq nil threads))
10828
10829 (defun gnus-summary-initial-limit (&optional show-if-empty)
10830   "Figure out what the initial limit is supposed to be on group entry.
10831 This entails weeding out unwanted dormants, low-scored articles,
10832 fetch-old-headers verbiage, and so on."
10833   ;; Most groups have nothing to remove.
10834   (if (or gnus-inhibit-limiting
10835           (and (null gnus-newsgroup-dormant)
10836                (not (eq gnus-fetch-old-headers 'some))
10837                (null gnus-summary-expunge-below)
10838                (not (eq gnus-build-sparse-threads 'some))
10839                (not (eq gnus-build-sparse-threads 'more))
10840                (null gnus-thread-expunge-below)
10841                (not gnus-use-nocem)))
10842       () ; Do nothing.
10843     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10844     (setq gnus-newsgroup-limit nil)
10845     (mapatoms
10846      (lambda (node)
10847        (unless (car (symbol-value node))
10848          ;; These threads have no parents -- they are roots.
10849          (let ((nodes (cdr (symbol-value node)))
10850                thread)
10851            (while nodes
10852              (if (and gnus-thread-expunge-below
10853                       (< (gnus-thread-total-score (car nodes))
10854                          gnus-thread-expunge-below))
10855                  (gnus-expunge-thread (pop nodes))
10856                (setq thread (pop nodes))
10857                (gnus-summary-limit-children thread))))))
10858      gnus-newsgroup-dependencies)
10859     ;; If this limitation resulted in an empty group, we might
10860     ;; pop the previous limit and use it instead.
10861     (when (and (not gnus-newsgroup-limit)
10862                show-if-empty)
10863       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10864     gnus-newsgroup-limit))
10865
10866 (defun gnus-summary-limit-children (thread)
10867   "Return 1 if this subthread is visible and 0 if it is not."
10868   ;; First we get the number of visible children to this thread.  This
10869   ;; is done by recursing down the thread using this function, so this
10870   ;; will really go down to a leaf article first, before slowly
10871   ;; working its way up towards the root.
10872   (when thread
10873     (let ((children
10874            (if (cdr thread)
10875                (apply '+ (mapcar 'gnus-summary-limit-children
10876                                  (cdr thread)))
10877              0))
10878           (number (mail-header-number (car thread)))
10879           score)
10880       (if (or
10881            ;; If this article is dormant and has absolutely no visible
10882            ;; children, then this article isn't visible.
10883            (and (memq number gnus-newsgroup-dormant)
10884                 (= children 0))
10885            ;; If this is a "fetch-old-headered" and there is only one
10886            ;; visible child (or less), then we don't want this article.
10887            (and (eq gnus-fetch-old-headers 'some)
10888                 (memq number gnus-newsgroup-ancient)
10889                 (zerop children))
10890            ;; If this is a sparsely inserted article with no children,
10891            ;; we don't want it.
10892            (and (eq gnus-build-sparse-threads 'some)
10893                 (memq number gnus-newsgroup-sparse)
10894                 (zerop children))
10895            ;; If we use expunging, and this article is really
10896            ;; low-scored, then we don't want this article.
10897            (when (and gnus-summary-expunge-below
10898                       (< (setq score
10899                                (or (cdr (assq number gnus-newsgroup-scored))
10900                                    gnus-summary-default-score))
10901                          gnus-summary-expunge-below))
10902              ;; We increase the expunge-tally here, but that has
10903              ;; nothing to do with the limits, really.
10904              (incf gnus-newsgroup-expunged-tally)
10905              ;; We also mark as read here, if that's wanted.
10906              (when (and gnus-summary-mark-below
10907                         (< score gnus-summary-mark-below))
10908                (setq gnus-newsgroup-unreads
10909                      (delq number gnus-newsgroup-unreads))
10910                (if gnus-newsgroup-auto-expire
10911                    (push number gnus-newsgroup-expirable)
10912                  (push (cons number gnus-low-score-mark)
10913                        gnus-newsgroup-reads)))
10914              t)
10915            (and gnus-use-nocem
10916                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10917           ;; Nope, invisible article.
10918           0
10919         ;; Ok, this article is to be visible, so we add it to the limit
10920         ;; and return 1.
10921         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10922         1))))
10923
10924 (defun gnus-expunge-thread (thread)
10925   "Mark all articles in THREAD as read."
10926   (let* ((number (mail-header-number (car thread))))
10927     (incf gnus-newsgroup-expunged-tally)
10928     ;; We also mark as read here, if that's wanted.
10929     (setq gnus-newsgroup-unreads
10930           (delq number gnus-newsgroup-unreads))
10931     (if gnus-newsgroup-auto-expire
10932         (push number gnus-newsgroup-expirable)
10933       (push (cons number gnus-low-score-mark)
10934             gnus-newsgroup-reads)))
10935   ;; Go recursively through all subthreads.
10936   (mapcar 'gnus-expunge-thread (cdr thread)))
10937
10938 ;; Summary article oriented commands
10939
10940 (defun gnus-summary-refer-parent-article (n)
10941   "Refer parent article N times.
10942 The difference between N and the number of articles fetched is returned."
10943   (interactive "p")
10944   (gnus-set-global-variables)
10945   (while
10946       (and
10947        (> n 0)
10948        (let* ((header (gnus-summary-article-header))
10949               (ref
10950                ;; If we try to find the parent of the currently
10951                ;; displayed article, then we take a look at the actual
10952                ;; References header, since this is slightly more
10953                ;; reliable than the References field we got from the
10954                ;; server.
10955                (if (and (eq (mail-header-number header)
10956                             (cdr gnus-article-current))
10957                         (equal gnus-newsgroup-name
10958                                (car gnus-article-current)))
10959                    (save-excursion
10960                      (set-buffer gnus-original-article-buffer)
10961                      (nnheader-narrow-to-headers)
10962                      (prog1
10963                          (message-fetch-field "references")
10964                        (widen)))
10965                  ;; It's not the current article, so we take a bet on
10966                  ;; the value we got from the server.
10967                  (mail-header-references header))))
10968          (if (setq ref (or ref (mail-header-references header)))
10969              (or (gnus-summary-refer-article (gnus-parent-id ref))
10970                  (gnus-message 1 "Couldn't find parent"))
10971            (gnus-message 1 "No references in article %d"
10972                          (gnus-summary-article-number))
10973            nil)))
10974     (setq n (1- n)))
10975   (gnus-summary-position-point)
10976   n)
10977
10978 (defun gnus-summary-refer-references ()
10979   "Fetch all articles mentioned in the References header.
10980 Return how many articles were fetched."
10981   (interactive)
10982   (gnus-set-global-variables)
10983   (let ((ref (mail-header-references (gnus-summary-article-header)))
10984         (current (gnus-summary-article-number))
10985         (n 0))
10986     ;; For each Message-ID in the References header...
10987     (while (string-match "<[^>]*>" ref)
10988       (incf n)
10989       ;; ... fetch that article.
10990       (gnus-summary-refer-article
10991        (prog1 (match-string 0 ref)
10992          (setq ref (substring ref (match-end 0))))))
10993     (gnus-summary-goto-subject current)
10994     (gnus-summary-position-point)
10995     n))
10996
10997 (defun gnus-summary-refer-article (message-id)
10998   "Fetch an article specified by MESSAGE-ID."
10999   (interactive "sMessage-ID: ")
11000   (when (and (stringp message-id)
11001              (not (zerop (length message-id))))
11002     ;; Construct the correct Message-ID if necessary.
11003     ;; Suggested by tale@pawl.rpi.edu.
11004     (unless (string-match "^<" message-id)
11005       (setq message-id (concat "<" message-id)))
11006     (unless (string-match ">$" message-id)
11007       (setq message-id (concat message-id ">")))
11008     (let ((header (car (gnus-gethash message-id
11009                                      gnus-newsgroup-dependencies))))
11010       (if header
11011           ;; The article is present in the buffer, to we just go to it.
11012           (gnus-summary-goto-article (mail-header-number header) nil t)
11013         ;; We fetch the article
11014         (let ((gnus-override-method 
11015                (and (gnus-news-group-p gnus-newsgroup-name)
11016                     gnus-refer-article-method))
11017               number)
11018           ;; Start the special refer-article method, if necessary.
11019           (when (and gnus-refer-article-method
11020                      (gnus-news-group-p gnus-newsgroup-name))
11021             (gnus-check-server gnus-refer-article-method))
11022           ;; Fetch the header, and display the article.
11023           (if (setq number (gnus-summary-insert-subject message-id))
11024               (gnus-summary-select-article nil nil nil number)
11025             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
11026
11027 (defun gnus-summary-enter-digest-group (&optional force)
11028   "Enter a digest group based on the current article."
11029   (interactive "P")
11030   (gnus-set-global-variables)
11031   (gnus-summary-select-article)
11032   (let ((name (format "%s-%d"
11033                       (gnus-group-prefixed-name
11034                        gnus-newsgroup-name (list 'nndoc ""))
11035                       gnus-current-article))
11036         (ogroup gnus-newsgroup-name)
11037         (case-fold-search t)
11038         (buf (current-buffer))
11039         dig)
11040     (save-excursion
11041       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
11042       (insert-buffer-substring gnus-original-article-buffer)
11043       (narrow-to-region
11044        (goto-char (point-min))
11045        (or (search-forward "\n\n" nil t) (point)))
11046       (goto-char (point-min))
11047       (delete-matching-lines "^\\(Path\\):\\|^From ")
11048       (widen))
11049     (unwind-protect
11050         (if (gnus-group-read-ephemeral-group
11051              name `(nndoc ,name (nndoc-address
11052                                  ,(get-buffer dig))
11053                           (nndoc-article-type ,(if force 'digest 'guess))) t)
11054             ;; Make all postings to this group go to the parent group.
11055             (nconc (gnus-info-params (gnus-get-info name))
11056                    (list (cons 'to-group ogroup)))
11057           ;; Couldn't select this doc group.
11058           (switch-to-buffer buf)
11059           (gnus-set-global-variables)
11060           (gnus-configure-windows 'summary)
11061           (gnus-message 3 "Article couldn't be entered?"))
11062       (kill-buffer dig))))
11063
11064 (defun gnus-summary-isearch-article (&optional regexp-p)
11065   "Do incremental search forward on the current article.
11066 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
11067   (interactive "P")
11068   (gnus-set-global-variables)
11069   (gnus-summary-select-article)
11070   (gnus-configure-windows 'article)
11071   (gnus-eval-in-buffer-window
11072    gnus-article-buffer
11073    (goto-char (point-min))
11074    (isearch-forward regexp-p)))
11075
11076 (defun gnus-summary-search-article-forward (regexp &optional backward)
11077   "Search for an article containing REGEXP forward.
11078 If BACKWARD, search backward instead."
11079   (interactive
11080    (list (read-string
11081           (format "Search article %s (regexp%s): "
11082                   (if current-prefix-arg "backward" "forward")
11083                   (if gnus-last-search-regexp
11084                       (concat ", default " gnus-last-search-regexp)
11085                     "")))
11086          current-prefix-arg))
11087   (gnus-set-global-variables)
11088   (if (string-equal regexp "")
11089       (setq regexp (or gnus-last-search-regexp ""))
11090     (setq gnus-last-search-regexp regexp))
11091   (if (gnus-summary-search-article regexp backward)
11092       (gnus-article-set-window-start
11093        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
11094     (error "Search failed: \"%s\"" regexp)))
11095
11096 (defun gnus-summary-search-article-backward (regexp)
11097   "Search for an article containing REGEXP backward."
11098   (interactive
11099    (list (read-string
11100           (format "Search article backward (regexp%s): "
11101                   (if gnus-last-search-regexp
11102                       (concat ", default " gnus-last-search-regexp)
11103                     "")))))
11104   (gnus-summary-search-article-forward regexp 'backward))
11105
11106 (defun gnus-summary-search-article (regexp &optional backward)
11107   "Search for an article containing REGEXP.
11108 Optional argument BACKWARD means do search for backward.
11109 `gnus-select-article-hook' is not called during the search."
11110   (let ((gnus-select-article-hook nil)  ;Disable hook.
11111         (gnus-article-display-hook nil)
11112         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
11113         (re-search
11114          (if backward
11115              're-search-backward 're-search-forward))
11116         (sum (current-buffer))
11117         (found nil))
11118     (gnus-save-hidden-threads
11119       (gnus-summary-select-article)
11120       (set-buffer gnus-article-buffer)
11121       (while (not found)
11122         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
11123         (if (if backward
11124                 (re-search-backward regexp nil t)
11125               (re-search-forward regexp nil t))
11126             ;; We found the regexp.
11127             (progn
11128               (setq found 'found)
11129               (beginning-of-line)
11130               (set-window-start
11131                (get-buffer-window (current-buffer))
11132                (point)))
11133           ;; We didn't find it, so we go to the next article.
11134           (set-buffer sum)
11135           (if (not (if backward (gnus-summary-find-prev)
11136                      (gnus-summary-find-next)))
11137               ;; No more articles.
11138               (setq found t)
11139             ;; Select the next article and adjust point.
11140             (gnus-summary-select-article)
11141             (set-buffer gnus-article-buffer)
11142             (widen)
11143             (goto-char (if backward (point-max) (point-min))))))
11144       (set-buffer sum)
11145       (gnus-message 7 ""))
11146     ;; Return whether we found the regexp.
11147     (when (eq found 'found)
11148       (gnus-summary-show-thread)
11149       (gnus-summary-goto-subject gnus-current-article)
11150       (gnus-summary-position-point)
11151       t)))
11152
11153 (defun gnus-summary-find-matching (header regexp &optional backward unread
11154                                           not-case-fold)
11155   "Return a list of all articles that match REGEXP on HEADER.
11156 The search stars on the current article and goes forwards unless
11157 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
11158 If UNREAD is non-nil, only unread articles will
11159 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
11160 in the comparisons."
11161   (let ((data (if (eq backward 'all) gnus-newsgroup-data
11162                 (gnus-data-find-list
11163                  (gnus-summary-article-number) (gnus-data-list backward))))
11164         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
11165         (case-fold-search (not not-case-fold))
11166         articles d)
11167     (or (fboundp (intern (concat "mail-header-" header)))
11168         (error "%s is not a valid header" header))
11169     (while data
11170       (setq d (car data))
11171       (and (or (not unread)             ; We want all articles...
11172                (gnus-data-unread-p d))  ; Or just unreads.
11173            (vectorp (gnus-data-header d)) ; It's not a pseudo.
11174            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
11175            (setq articles (cons (gnus-data-number d) articles))) ; Success!
11176       (setq data (cdr data)))
11177     (nreverse articles)))
11178
11179 (defun gnus-summary-execute-command (header regexp command &optional backward)
11180   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
11181 If HEADER is an empty string (or nil), the match is done on the entire
11182 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
11183   (interactive
11184    (list (let ((completion-ignore-case t))
11185            (completing-read
11186             "Header name: "
11187             (mapcar (lambda (string) (list string))
11188                     '("Number" "Subject" "From" "Lines" "Date"
11189                       "Message-ID" "Xref" "References" "Body"))
11190             nil 'require-match))
11191          (read-string "Regexp: ")
11192          (read-key-sequence "Command: ")
11193          current-prefix-arg))
11194   (when (equal header "Body")
11195     (setq header ""))
11196   (gnus-set-global-variables)
11197   ;; Hidden thread subtrees must be searched as well.
11198   (gnus-summary-show-all-threads)
11199   ;; We don't want to change current point nor window configuration.
11200   (save-excursion
11201     (save-window-excursion
11202       (gnus-message 6 "Executing %s..." (key-description command))
11203       ;; We'd like to execute COMMAND interactively so as to give arguments.
11204       (gnus-execute header regexp
11205                     `(lambda () (call-interactively ',(key-binding command)))
11206                     backward)
11207       (gnus-message 6 "Executing %s...done" (key-description command)))))
11208
11209 (defun gnus-summary-beginning-of-article ()
11210   "Scroll the article back to the beginning."
11211   (interactive)
11212   (gnus-set-global-variables)
11213   (gnus-summary-select-article)
11214   (gnus-configure-windows 'article)
11215   (gnus-eval-in-buffer-window
11216    gnus-article-buffer
11217    (widen)
11218    (goto-char (point-min))
11219    (and gnus-break-pages (gnus-narrow-to-page))))
11220
11221 (defun gnus-summary-end-of-article ()
11222   "Scroll to the end of the article."
11223   (interactive)
11224   (gnus-set-global-variables)
11225   (gnus-summary-select-article)
11226   (gnus-configure-windows 'article)
11227   (gnus-eval-in-buffer-window
11228    gnus-article-buffer
11229    (widen)
11230    (goto-char (point-max))
11231    (recenter -3)
11232    (and gnus-break-pages (gnus-narrow-to-page))))
11233
11234 (defun gnus-summary-show-article (&optional arg)
11235   "Force re-fetching of the current article.
11236 If ARG (the prefix) is non-nil, show the raw article without any
11237 article massaging functions being run."
11238   (interactive "P")
11239   (gnus-set-global-variables)
11240   (if (not arg)
11241       ;; Select the article the normal way.
11242       (gnus-summary-select-article nil 'force)
11243     ;; Bind the article treatment functions to nil.
11244     (let ((gnus-have-all-headers t)
11245           gnus-article-display-hook
11246           gnus-article-prepare-hook
11247           gnus-break-pages
11248           gnus-visual)
11249       (gnus-summary-select-article nil 'force)))
11250   (gnus-summary-goto-subject gnus-current-article)
11251 ;  (gnus-configure-windows 'article)
11252   (gnus-summary-position-point))
11253
11254 (defun gnus-summary-verbose-headers (&optional arg)
11255   "Toggle permanent full header display.
11256 If ARG is a positive number, turn header display on.
11257 If ARG is a negative number, turn header display off."
11258   (interactive "P")
11259   (gnus-set-global-variables)
11260   (gnus-summary-toggle-header arg)
11261   (setq gnus-show-all-headers
11262         (cond ((or (not (numberp arg))
11263                    (zerop arg))
11264                (not gnus-show-all-headers))
11265               ((natnump arg)
11266                t))))
11267
11268 (defun gnus-summary-toggle-header (&optional arg)
11269   "Show the headers if they are hidden, or hide them if they are shown.
11270 If ARG is a positive number, show the entire header.
11271 If ARG is a negative number, hide the unwanted header lines."
11272   (interactive "P")
11273   (gnus-set-global-variables)
11274   (save-excursion
11275     (set-buffer gnus-article-buffer)
11276     (let* ((buffer-read-only nil)
11277            (inhibit-point-motion-hooks t)
11278            (hidden (text-property-any
11279                     (goto-char (point-min)) (search-forward "\n\n")
11280                     'invisible t))
11281            e)
11282       (goto-char (point-min))
11283       (when (search-forward "\n\n" nil t)
11284         (delete-region (point-min) (1- (point))))
11285       (goto-char (point-min))
11286       (save-excursion
11287         (set-buffer gnus-original-article-buffer)
11288         (goto-char (point-min))
11289         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11290       (insert-buffer-substring gnus-original-article-buffer 1 e)
11291       (let ((gnus-inhibit-hiding t))
11292         (run-hooks 'gnus-article-display-hook))
11293       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11294           (gnus-article-hide-headers)))))
11295
11296 (defun gnus-summary-show-all-headers ()
11297   "Make all header lines visible."
11298   (interactive)
11299   (gnus-set-global-variables)
11300   (gnus-article-show-all-headers))
11301
11302 (defun gnus-summary-toggle-mime (&optional arg)
11303   "Toggle MIME processing.
11304 If ARG is a positive number, turn MIME processing on."
11305   (interactive "P")
11306   (gnus-set-global-variables)
11307   (setq gnus-show-mime
11308         (if (null arg) (not gnus-show-mime)
11309           (> (prefix-numeric-value arg) 0)))
11310   (gnus-summary-select-article t 'force))
11311
11312 (defun gnus-summary-caesar-message (&optional arg)
11313   "Caesar rotate the current article by 13.
11314 The numerical prefix specifies how manu places to rotate each letter
11315 forward."
11316   (interactive "P")
11317   (gnus-set-global-variables)
11318   (gnus-summary-select-article)
11319   (let ((mail-header-separator ""))
11320     (gnus-eval-in-buffer-window
11321      gnus-article-buffer
11322      (save-restriction
11323        (widen)
11324        (let ((start (window-start)))
11325          (news-caesar-buffer-body arg)
11326          (set-window-start (get-buffer-window (current-buffer)) start))))))
11327
11328 (defun gnus-summary-stop-page-breaking ()
11329   "Stop page breaking in the current article."
11330   (interactive)
11331   (gnus-set-global-variables)
11332   (gnus-summary-select-article)
11333   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11334
11335 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11336   "Move the current article to a different newsgroup.
11337 If N is a positive number, move the N next articles.
11338 If N is a negative number, move the N previous articles.
11339 If N is nil and any articles have been marked with the process mark,
11340 move those articles instead.
11341 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11342 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11343 re-spool using this method.
11344
11345 For this function to work, both the current newsgroup and the
11346 newsgroup that you want to move to have to support the `request-move'
11347 and `request-accept' functions."
11348   (interactive "P")
11349   (unless action (setq action 'move))
11350   (gnus-set-global-variables)
11351   ;; Check whether the source group supports the required functions.
11352   (cond ((and (eq action 'move)
11353               (not (gnus-check-backend-function
11354                     'request-move-article gnus-newsgroup-name)))
11355          (error "The current group does not support article moving"))
11356         ((and (eq action 'crosspost)
11357               (not (gnus-check-backend-function
11358                     'request-replace-article gnus-newsgroup-name)))
11359          (error "The current group does not support article editing")))
11360   (let ((articles (gnus-summary-work-articles n))
11361         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11362         (names '((move "Move" "Moving")
11363                  (copy "Copy" "Copying")
11364                  (crosspost "Crosspost" "Crossposting")))
11365         (copy-buf (save-excursion
11366                     (nnheader-set-temp-buffer " *copy article*")))
11367         art-group to-method new-xref article to-groups)
11368     (unless (assq action names)
11369       (error "Unknown action %s" action))
11370     ;; Read the newsgroup name.
11371     (when (and (not to-newsgroup)
11372                (not select-method))
11373       (setq to-newsgroup
11374             (gnus-read-move-group-name
11375              (cadr (assq action names))
11376              (symbol-value (intern (format "gnus-current-%s-group" action)))
11377              articles prefix))
11378       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11379     (setq to-method (or select-method 
11380                         (gnus-find-method-for-group to-newsgroup)))
11381     ;; Check the method we are to move this article to...
11382     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11383         (error "%s does not support article copying" (car to-method)))
11384     (or (gnus-check-server to-method)
11385         (error "Can't open server %s" (car to-method)))
11386     (gnus-message 6 "%s to %s: %s..."
11387                   (caddr (assq action names))
11388                   (or (car select-method) to-newsgroup) articles)
11389     (while articles
11390       (setq article (pop articles))
11391       (setq
11392        art-group
11393        (cond
11394         ;; Move the article.
11395         ((eq action 'move)
11396          (gnus-request-move-article
11397           article                       ; Article to move
11398           gnus-newsgroup-name           ; From newsgrouo
11399           (nth 1 (gnus-find-method-for-group
11400                   gnus-newsgroup-name)) ; Server
11401           (list 'gnus-request-accept-article
11402                 to-newsgroup (list 'quote select-method)
11403                 (not articles))         ; Accept form
11404           (not articles)))              ; Only save nov last time
11405         ;; Copy the article.
11406         ((eq action 'copy)
11407          (save-excursion
11408            (set-buffer copy-buf)
11409            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11410            (gnus-request-accept-article
11411             to-newsgroup select-method (not articles))))
11412         ;; Crosspost the article.
11413         ((eq action 'crosspost)
11414          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11415            (setq new-xref (concat gnus-newsgroup-name ":" article))
11416            (if (and xref (not (string= xref "")))
11417                (progn
11418                  (when (string-match "^Xref: " xref)
11419                    (setq xref (substring xref (match-end 0))))
11420                  (setq new-xref (concat xref " " new-xref)))
11421              (setq new-xref (concat (system-name) " " new-xref)))
11422            (save-excursion
11423              (set-buffer copy-buf)
11424              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11425              (nnheader-replace-header "xref" new-xref)
11426              (gnus-request-accept-article
11427               to-newsgroup select-method (not articles)))))))
11428       (if (not art-group)
11429           (gnus-message 1 "Couldn't %s article %s"
11430                         (cadr (assq action names)) article)
11431         (let* ((entry
11432                 (or
11433                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11434                  (gnus-gethash
11435                   (gnus-group-prefixed-name
11436                    (car art-group)
11437                    (or select-method 
11438                        (gnus-find-method-for-group to-newsgroup)))
11439                   gnus-newsrc-hashtb)))
11440                (info (nth 2 entry))
11441                (to-group (gnus-info-group info)))
11442           ;; Update the group that has been moved to.
11443           (when (and info
11444                      (memq action '(move copy)))
11445             (unless (member to-group to-groups)
11446               (push to-group to-groups))
11447
11448             (unless (memq article gnus-newsgroup-unreads)
11449               (gnus-info-set-read
11450                info (gnus-add-to-range (gnus-info-read info)
11451                                        (list (cdr art-group)))))
11452
11453             ;; Copy any marks over to the new group.
11454             (let ((marks gnus-article-mark-lists)
11455                   (to-article (cdr art-group)))
11456
11457               ;; See whether the article is to be put in the cache.
11458               (when gnus-use-cache
11459                 (gnus-cache-possibly-enter-article
11460                  to-group to-article
11461                  (let ((header (copy-sequence
11462                                 (gnus-summary-article-header article))))
11463                    (mail-header-set-number header to-article)
11464                    header)
11465                  (memq article gnus-newsgroup-marked)
11466                  (memq article gnus-newsgroup-dormant)
11467                  (memq article gnus-newsgroup-unreads)))
11468
11469               (while marks
11470                 (when (memq article (symbol-value
11471                                      (intern (format "gnus-newsgroup-%s"
11472                                                      (caar marks)))))
11473                   ;; If the other group is the same as this group,
11474                   ;; then we have to add the mark to the list.
11475                   (when (equal to-group gnus-newsgroup-name)
11476                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
11477                          (cons to-article
11478                                (symbol-value
11479                                 (intern (format "gnus-newsgroup-%s"
11480                                                 (caar marks)))))))
11481                   ;; Copy mark to other group.
11482                   (gnus-add-marked-articles
11483                    to-group (cdar marks) (list to-article) info))
11484                 (setq marks (cdr marks)))))
11485
11486           ;; Update the Xref header in this article to point to
11487           ;; the new crossposted article we have just created.
11488           (when (eq action 'crosspost)
11489             (save-excursion
11490               (set-buffer copy-buf)
11491               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11492               (nnheader-replace-header
11493                "xref" (concat new-xref " " (gnus-group-prefixed-name
11494                                             (car art-group) to-method)
11495                               ":" (cdr art-group)))
11496               (gnus-request-replace-article
11497                article gnus-newsgroup-name (current-buffer)))))
11498
11499         (gnus-summary-goto-subject article)
11500         (when (eq action 'move)
11501           (gnus-summary-mark-article article gnus-canceled-mark)))
11502       (gnus-summary-remove-process-mark article))
11503     ;; Re-activate all groups that have been moved to.
11504     (while to-groups
11505       (gnus-activate-group (pop to-groups)))
11506     
11507     (gnus-kill-buffer copy-buf)
11508     (gnus-summary-position-point)
11509     (gnus-set-mode-line 'summary)))
11510
11511 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11512   "Move the current article to a different newsgroup.
11513 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11514 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
11515 re-spool using this method."
11516   (interactive "P")
11517   (gnus-summary-move-article n nil select-method 'copy))
11518
11519 (defun gnus-summary-crosspost-article (&optional n)
11520   "Crosspost the current article to some other group."
11521   (interactive "P")
11522   (gnus-summary-move-article n nil nil 'crosspost))
11523
11524 (defvar gnus-summary-respool-default-method nil
11525   "Default method for respooling an article.  
11526 If nil, use to the current newsgroup method.")
11527
11528 (defun gnus-summary-respool-article (&optional n method)
11529   "Respool the current article.
11530 The article will be squeezed through the mail spooling process again,
11531 which means that it will be put in some mail newsgroup or other
11532 depending on `nnmail-split-methods'.
11533 If N is a positive number, respool the N next articles.
11534 If N is a negative number, respool the N previous articles.
11535 If N is nil and any articles have been marked with the process mark,
11536 respool those articles instead.
11537
11538 Respooling can be done both from mail groups and \"real\" newsgroups.
11539 In the former case, the articles in question will be moved from the
11540 current group into whatever groups they are destined to.  In the
11541 latter case, they will be copied into the relevant groups."
11542   (interactive 
11543    (list current-prefix-arg
11544          (let* ((methods (gnus-methods-using 'respool))
11545                 (methname
11546                  (symbol-name (or gnus-summary-respool-default-method
11547                                   (car (gnus-find-method-for-group
11548                                         gnus-newsgroup-name)))))
11549                 (method
11550                  (gnus-completing-read 
11551                   methname "What backend do you want to use when? "
11552                   methods nil t nil 'gnus-method-history))
11553                 ms)
11554            (cond
11555             ((zerop (length (setq ms (gnus-servers-using-backend method))))
11556              (list (intern method) ""))
11557             ((= 1 (length ms))
11558              (car ms))
11559             (t
11560              (cdr (completing-read 
11561                    "Server name: "
11562                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
11563   (gnus-set-global-variables)
11564   (unless method
11565     (error "No method given for respooling"))
11566   (if (assoc (symbol-name
11567               (car (gnus-find-method-for-group gnus-newsgroup-name)))
11568              (gnus-methods-using 'respool))
11569       (gnus-summary-move-article n nil method)
11570     (gnus-summary-copy-article n nil method)))
11571
11572 (defun gnus-summary-import-article (file)
11573   "Import a random file into a mail newsgroup."
11574   (interactive "fImport file: ")
11575   (gnus-set-global-variables)
11576   (let ((group gnus-newsgroup-name)
11577         (now (current-time))
11578         atts lines)
11579     (or (gnus-check-backend-function 'request-accept-article group)
11580         (error "%s does not support article importing" group))
11581     (or (file-readable-p file)
11582         (not (file-regular-p file))
11583         (error "Can't read %s" file))
11584     (save-excursion
11585       (set-buffer (get-buffer-create " *import file*"))
11586       (buffer-disable-undo (current-buffer))
11587       (erase-buffer)
11588       (insert-file-contents file)
11589       (goto-char (point-min))
11590       (unless (nnheader-article-p)
11591         ;; This doesn't look like an article, so we fudge some headers.
11592         (setq atts (file-attributes file)
11593               lines (count-lines (point-min) (point-max)))
11594         (insert "From: " (read-string "From: ") "\n"
11595                 "Subject: " (read-string "Subject: ") "\n"
11596                 "Date: " (timezone-make-date-arpa-standard
11597                           (current-time-string (nth 5 atts))
11598                           (current-time-zone now)
11599                           (current-time-zone now)) "\n"
11600                 "Message-ID: " (gnus-inews-message-id) "\n"
11601                 "Lines: " (int-to-string lines) "\n"
11602                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11603       (gnus-request-accept-article group nil t)
11604       (kill-buffer (current-buffer)))))
11605
11606 (defun gnus-summary-expire-articles (&optional now)
11607   "Expire all articles that are marked as expirable in the current group."
11608   (interactive)
11609   (gnus-set-global-variables)
11610   (when (gnus-check-backend-function
11611          'request-expire-articles gnus-newsgroup-name)
11612     ;; This backend supports expiry.
11613     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11614            (expirable (if total
11615                           (gnus-list-of-read-articles gnus-newsgroup-name)
11616                         (setq gnus-newsgroup-expirable
11617                               (sort gnus-newsgroup-expirable '<))))
11618            (expiry-wait (if now 'immediate
11619                           (gnus-group-get-parameter
11620                            gnus-newsgroup-name 'expiry-wait)))
11621            es)
11622       (when expirable
11623         ;; There are expirable articles in this group, so we run them
11624         ;; through the expiry process.
11625         (gnus-message 6 "Expiring articles...")
11626         ;; The list of articles that weren't expired is returned.
11627         (if expiry-wait
11628             (let ((nnmail-expiry-wait-function nil)
11629                   (nnmail-expiry-wait expiry-wait))
11630               (setq es (gnus-request-expire-articles
11631                         expirable gnus-newsgroup-name)))
11632           (setq es (gnus-request-expire-articles
11633                     expirable gnus-newsgroup-name)))
11634         (or total (setq gnus-newsgroup-expirable es))
11635         ;; We go through the old list of expirable, and mark all
11636         ;; really expired articles as nonexistent.
11637         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11638           (let ((gnus-use-cache nil))
11639             (while expirable
11640               (unless (memq (car expirable) es)
11641                 (when (gnus-data-find (car expirable))
11642                   (gnus-summary-mark-article
11643                    (car expirable) gnus-canceled-mark)))
11644               (setq expirable (cdr expirable)))))
11645         (gnus-message 6 "Expiring articles...done")))))
11646
11647 (defun gnus-summary-expire-articles-now ()
11648   "Expunge all expirable articles in the current group.
11649 This means that *all* articles that are marked as expirable will be
11650 deleted forever, right now."
11651   (interactive)
11652   (gnus-set-global-variables)
11653   (or gnus-expert-user
11654       (gnus-y-or-n-p
11655        "Are you really, really, really sure you want to delete all these messages? ")
11656       (error "Phew!"))
11657   (gnus-summary-expire-articles t))
11658
11659 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11660 (defun gnus-summary-delete-article (&optional n)
11661   "Delete the N next (mail) articles.
11662 This command actually deletes articles.  This is not a marking
11663 command.  The article will disappear forever from your life, never to
11664 return.
11665 If N is negative, delete backwards.
11666 If N is nil and articles have been marked with the process mark,
11667 delete these instead."
11668   (interactive "P")
11669   (gnus-set-global-variables)
11670   (or (gnus-check-backend-function 'request-expire-articles
11671                                    gnus-newsgroup-name)
11672       (error "The current newsgroup does not support article deletion."))
11673   ;; Compute the list of articles to delete.
11674   (let ((articles (gnus-summary-work-articles n))
11675         not-deleted)
11676     (if (and gnus-novice-user
11677              (not (gnus-y-or-n-p
11678                    (format "Do you really want to delete %s forever? "
11679                            (if (> (length articles) 1) 
11680                                (format "these %s articles" (length articles))
11681                              "this article")))))
11682         ()
11683       ;; Delete the articles.
11684       (setq not-deleted (gnus-request-expire-articles
11685                          articles gnus-newsgroup-name 'force))
11686       (while articles
11687         (gnus-summary-remove-process-mark (car articles))
11688         ;; The backend might not have been able to delete the article
11689         ;; after all.
11690         (or (memq (car articles) not-deleted)
11691             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11692         (setq articles (cdr articles))))
11693     (gnus-summary-position-point)
11694     (gnus-set-mode-line 'summary)
11695     not-deleted))
11696
11697 (defun gnus-summary-edit-article (&optional force)
11698   "Enter into a buffer and edit the current article.
11699 This will have permanent effect only in mail groups.
11700 If FORCE is non-nil, allow editing of articles even in read-only
11701 groups."
11702   (interactive "P")
11703   (save-excursion
11704     (set-buffer gnus-summary-buffer)
11705     (gnus-set-global-variables)
11706     (when (and (not force)
11707                (gnus-group-read-only-p))
11708       (error "The current newsgroup does not support article editing."))
11709     (gnus-summary-select-article t nil t)
11710     (gnus-configure-windows 'article)
11711     (select-window (get-buffer-window gnus-article-buffer))
11712     (gnus-message 6 "C-c C-c to end edits")
11713     (setq buffer-read-only nil)
11714     (text-mode)
11715     (use-local-map (copy-keymap (current-local-map)))
11716     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11717     (buffer-enable-undo)
11718     (widen)
11719     (goto-char (point-min))
11720     (search-forward "\n\n" nil t)))
11721
11722 (defun gnus-summary-edit-article-done ()
11723   "Make edits to the current article permanent."
11724   (interactive)
11725   (if (gnus-group-read-only-p)
11726       (progn
11727         (gnus-summary-edit-article-postpone)
11728         (gnus-error
11729          1 "The current newsgroup does not support article editing."))
11730     (let ((buf (format "%s" (buffer-string))))
11731       (erase-buffer)
11732       (insert buf)
11733       (if (not (gnus-request-replace-article
11734                 (cdr gnus-article-current) (car gnus-article-current)
11735                 (current-buffer)))
11736           (error "Couldn't replace article.")
11737         (gnus-article-mode)
11738         (use-local-map gnus-article-mode-map)
11739         (setq buffer-read-only t)
11740         (buffer-disable-undo (current-buffer))
11741         (gnus-configure-windows 'summary)
11742         (gnus-summary-update-article (cdr gnus-article-current))
11743         (when gnus-use-cache
11744           (gnus-cache-update-article 
11745            (cdr gnus-article-current) (car gnus-article-current)))
11746         (when gnus-keep-backlog
11747           (gnus-backlog-remove-article 
11748            (car gnus-article-current) (cdr gnus-article-current))))
11749       (save-excursion
11750         (when (get-buffer gnus-original-article-buffer)
11751           (set-buffer gnus-original-article-buffer)
11752           (setq gnus-original-article nil)))
11753       (setq gnus-article-current nil
11754             gnus-current-article nil)
11755       (run-hooks 'gnus-article-display-hook)
11756       (and (gnus-visual-p 'summary-highlight 'highlight)
11757            (run-hooks 'gnus-visual-mark-article-hook)))))
11758
11759 (defun gnus-summary-edit-article-postpone ()
11760   "Postpone changes to the current article."
11761   (interactive)
11762   (gnus-article-mode)
11763   (use-local-map gnus-article-mode-map)
11764   (setq buffer-read-only t)
11765   (buffer-disable-undo (current-buffer))
11766   (gnus-configure-windows 'summary)
11767   (and (gnus-visual-p 'summary-highlight 'highlight)
11768        (run-hooks 'gnus-visual-mark-article-hook)))
11769
11770 (defun gnus-summary-respool-query ()
11771   "Query where the respool algorithm would put this article."
11772   (interactive)
11773   (gnus-set-global-variables)
11774   (gnus-summary-select-article)
11775   (save-excursion
11776     (set-buffer gnus-article-buffer)
11777     (save-restriction
11778       (goto-char (point-min))
11779       (search-forward "\n\n")
11780       (narrow-to-region (point-min) (point))
11781       (pp-eval-expression
11782        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11783
11784 ;; Summary marking commands.
11785
11786 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11787   "Mark articles which has the same subject as read, and then select the next.
11788 If UNMARK is positive, remove any kind of mark.
11789 If UNMARK is negative, tick articles."
11790   (interactive "P")
11791   (gnus-set-global-variables)
11792   (if unmark
11793       (setq unmark (prefix-numeric-value unmark)))
11794   (let ((count
11795          (gnus-summary-mark-same-subject
11796           (gnus-summary-article-subject) unmark)))
11797     ;; Select next unread article.  If auto-select-same mode, should
11798     ;; select the first unread article.
11799     (gnus-summary-next-article t (and gnus-auto-select-same
11800                                       (gnus-summary-article-subject)))
11801     (gnus-message 7 "%d article%s marked as %s"
11802                   count (if (= count 1) " is" "s are")
11803                   (if unmark "unread" "read"))))
11804
11805 (defun gnus-summary-kill-same-subject (&optional unmark)
11806   "Mark articles which has the same subject as read.
11807 If UNMARK is positive, remove any kind of mark.
11808 If UNMARK is negative, tick articles."
11809   (interactive "P")
11810   (gnus-set-global-variables)
11811   (if unmark
11812       (setq unmark (prefix-numeric-value unmark)))
11813   (let ((count
11814          (gnus-summary-mark-same-subject
11815           (gnus-summary-article-subject) unmark)))
11816     ;; If marked as read, go to next unread subject.
11817     (if (null unmark)
11818         ;; Go to next unread subject.
11819         (gnus-summary-next-subject 1 t))
11820     (gnus-message 7 "%d articles are marked as %s"
11821                   count (if unmark "unread" "read"))))
11822
11823 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11824   "Mark articles with same SUBJECT as read, and return marked number.
11825 If optional argument UNMARK is positive, remove any kinds of marks.
11826 If optional argument UNMARK is negative, mark articles as unread instead."
11827   (let ((count 1))
11828     (save-excursion
11829       (cond
11830        ((null unmark)                   ; Mark as read.
11831         (while (and
11832                 (progn
11833                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11834                   (gnus-summary-show-thread) t)
11835                 (gnus-summary-find-subject subject))
11836           (setq count (1+ count))))
11837        ((> unmark 0)                    ; Tick.
11838         (while (and
11839                 (progn
11840                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11841                   (gnus-summary-show-thread) t)
11842                 (gnus-summary-find-subject subject))
11843           (setq count (1+ count))))
11844        (t                               ; Mark as unread.
11845         (while (and
11846                 (progn
11847                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11848                   (gnus-summary-show-thread) t)
11849                 (gnus-summary-find-subject subject))
11850           (setq count (1+ count)))))
11851       (gnus-set-mode-line 'summary)
11852       ;; Return the number of marked articles.
11853       count)))
11854
11855 (defun gnus-summary-mark-as-processable (n &optional unmark)
11856   "Set the process mark on the next N articles.
11857 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11858 the process mark instead.  The difference between N and the actual
11859 number of articles marked is returned."
11860   (interactive "p")
11861   (gnus-set-global-variables)
11862   (let ((backward (< n 0))
11863         (n (abs n)))
11864     (while (and
11865             (> n 0)
11866             (if unmark
11867                 (gnus-summary-remove-process-mark
11868                  (gnus-summary-article-number))
11869               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11870             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11871       (setq n (1- n)))
11872     (if (/= 0 n) (gnus-message 7 "No more articles"))
11873     (gnus-summary-recenter)
11874     (gnus-summary-position-point)
11875     n))
11876
11877 (defun gnus-summary-unmark-as-processable (n)
11878   "Remove the process mark from the next N articles.
11879 If N is negative, mark backward instead.  The difference between N and
11880 the actual number of articles marked is returned."
11881   (interactive "p")
11882   (gnus-set-global-variables)
11883   (gnus-summary-mark-as-processable n t))
11884
11885 (defun gnus-summary-unmark-all-processable ()
11886   "Remove the process mark from all articles."
11887   (interactive)
11888   (gnus-set-global-variables)
11889   (save-excursion
11890     (while gnus-newsgroup-processable
11891       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11892   (gnus-summary-position-point))
11893
11894 (defun gnus-summary-mark-as-expirable (n)
11895   "Mark N articles forward as expirable.
11896 If N is negative, mark backward instead.  The difference between N and
11897 the actual number of articles marked is returned."
11898   (interactive "p")
11899   (gnus-set-global-variables)
11900   (gnus-summary-mark-forward n gnus-expirable-mark))
11901
11902 (defun gnus-summary-mark-article-as-replied (article)
11903   "Mark ARTICLE replied and update the summary line."
11904   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11905   (let ((buffer-read-only nil))
11906     (when (gnus-summary-goto-subject article)
11907       (gnus-summary-update-secondary-mark article))))
11908
11909 (defun gnus-summary-set-bookmark (article)
11910   "Set a bookmark in current article."
11911   (interactive (list (gnus-summary-article-number)))
11912   (gnus-set-global-variables)
11913   (if (or (not (get-buffer gnus-article-buffer))
11914           (not gnus-current-article)
11915           (not gnus-article-current)
11916           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11917       (error "No current article selected"))
11918   ;; Remove old bookmark, if one exists.
11919   (let ((old (assq article gnus-newsgroup-bookmarks)))
11920     (if old (setq gnus-newsgroup-bookmarks
11921                   (delq old gnus-newsgroup-bookmarks))))
11922   ;; Set the new bookmark, which is on the form
11923   ;; (article-number . line-number-in-body).
11924   (setq gnus-newsgroup-bookmarks
11925         (cons
11926          (cons article
11927                (save-excursion
11928                  (set-buffer gnus-article-buffer)
11929                  (count-lines
11930                   (min (point)
11931                        (save-excursion
11932                          (goto-char (point-min))
11933                          (search-forward "\n\n" nil t)
11934                          (point)))
11935                   (point))))
11936          gnus-newsgroup-bookmarks))
11937   (gnus-message 6 "A bookmark has been added to the current article."))
11938
11939 (defun gnus-summary-remove-bookmark (article)
11940   "Remove the bookmark from the current article."
11941   (interactive (list (gnus-summary-article-number)))
11942   (gnus-set-global-variables)
11943   ;; Remove old bookmark, if one exists.
11944   (let ((old (assq article gnus-newsgroup-bookmarks)))
11945     (if old
11946         (progn
11947           (setq gnus-newsgroup-bookmarks
11948                 (delq old gnus-newsgroup-bookmarks))
11949           (gnus-message 6 "Removed bookmark."))
11950       (gnus-message 6 "No bookmark in current article."))))
11951
11952 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11953 (defun gnus-summary-mark-as-dormant (n)
11954   "Mark N articles forward as dormant.
11955 If N is negative, mark backward instead.  The difference between N and
11956 the actual number of articles marked is returned."
11957   (interactive "p")
11958   (gnus-set-global-variables)
11959   (gnus-summary-mark-forward n gnus-dormant-mark))
11960
11961 (defun gnus-summary-set-process-mark (article)
11962   "Set the process mark on ARTICLE and update the summary line."
11963   (setq gnus-newsgroup-processable
11964         (cons article
11965               (delq article gnus-newsgroup-processable)))
11966   (when (gnus-summary-goto-subject article)
11967     (gnus-summary-show-thread)
11968     (gnus-summary-update-secondary-mark article)))
11969
11970 (defun gnus-summary-remove-process-mark (article)
11971   "Remove the process mark from ARTICLE and update the summary line."
11972   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11973   (when (gnus-summary-goto-subject article)
11974     (gnus-summary-show-thread)
11975     (gnus-summary-update-secondary-mark article)))
11976
11977 (defun gnus-summary-set-saved-mark (article)
11978   "Set the process mark on ARTICLE and update the summary line."
11979   (push article gnus-newsgroup-saved)
11980   (when (gnus-summary-goto-subject article)
11981     (gnus-summary-update-secondary-mark article)))
11982
11983 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11984   "Mark N articles as read forwards.
11985 If N is negative, mark backwards instead.
11986 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11987 marked as unread.
11988 The difference between N and the actual number of articles marked is
11989 returned."
11990   (interactive "p")
11991   (gnus-set-global-variables)
11992   (let ((backward (< n 0))
11993         (gnus-summary-goto-unread
11994          (and gnus-summary-goto-unread
11995               (not (eq gnus-summary-goto-unread 'never))
11996               (not (memq mark (list gnus-unread-mark
11997                                     gnus-ticked-mark gnus-dormant-mark)))))
11998         (n (abs n))
11999         (mark (or mark gnus-del-mark)))
12000     (while (and (> n 0)
12001                 (gnus-summary-mark-article nil mark no-expire)
12002                 (zerop (gnus-summary-next-subject
12003                         (if backward -1 1)
12004                         (and gnus-summary-goto-unread
12005                              (not (eq gnus-summary-goto-unread 'never)))
12006                         t)))
12007       (setq n (1- n)))
12008     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
12009     (gnus-summary-recenter)
12010     (gnus-summary-position-point)
12011     (gnus-set-mode-line 'summary)
12012     n))
12013
12014 (defun gnus-summary-mark-article-as-read (mark)
12015   "Mark the current article quickly as read with MARK."
12016   (let ((article (gnus-summary-article-number)))
12017     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12018     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12019     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12020     (setq gnus-newsgroup-reads
12021           (cons (cons article mark) gnus-newsgroup-reads))
12022     ;; Possibly remove from cache, if that is used.
12023     (and gnus-use-cache (gnus-cache-enter-remove-article article))
12024     ;; Allow the backend to change the mark.
12025     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
12026     ;; Check for auto-expiry.
12027     (when (and gnus-newsgroup-auto-expire
12028                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12029                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12030                    (= mark gnus-ancient-mark)
12031                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
12032       (setq mark gnus-expirable-mark)
12033       (push article gnus-newsgroup-expirable))
12034     ;; Set the mark in the buffer.
12035     (gnus-summary-update-mark mark 'unread)
12036     t))
12037
12038 (defun gnus-summary-mark-article-as-unread (mark)
12039   "Mark the current article quickly as unread with MARK."
12040   (let ((article (gnus-summary-article-number)))
12041     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12042     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12043     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12044     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
12045     (cond ((= mark gnus-ticked-mark)
12046            (push article gnus-newsgroup-marked))
12047           ((= mark gnus-dormant-mark)
12048            (push article gnus-newsgroup-dormant))
12049           (t
12050            (push article gnus-newsgroup-unreads)))
12051     (setq gnus-newsgroup-reads
12052           (delq (assq article gnus-newsgroup-reads)
12053                 gnus-newsgroup-reads))
12054
12055     ;; See whether the article is to be put in the cache.
12056     (and gnus-use-cache
12057          (vectorp (gnus-summary-article-header article))
12058          (save-excursion
12059            (gnus-cache-possibly-enter-article
12060             gnus-newsgroup-name article
12061             (gnus-summary-article-header article)
12062             (= mark gnus-ticked-mark)
12063             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12064
12065     ;; Fix the mark.
12066     (gnus-summary-update-mark mark 'unread)
12067     t))
12068
12069 (defun gnus-summary-mark-article (&optional article mark no-expire)
12070   "Mark ARTICLE with MARK.  MARK can be any character.
12071 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
12072 `??' (dormant) and `?E' (expirable).
12073 If MARK is nil, then the default character `?D' is used.
12074 If ARTICLE is nil, then the article on the current line will be
12075 marked."
12076   ;; The mark might be a string.
12077   (and (stringp mark)
12078        (setq mark (aref mark 0)))
12079   ;; If no mark is given, then we check auto-expiring.
12080   (and (not no-expire)
12081        gnus-newsgroup-auto-expire
12082        (or (not mark)
12083            (and (numberp mark)
12084                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
12085                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
12086                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
12087        (setq mark gnus-expirable-mark))
12088   (let* ((mark (or mark gnus-del-mark))
12089          (article (or article (gnus-summary-article-number))))
12090     (or article (error "No article on current line"))
12091     (if (or (= mark gnus-unread-mark)
12092             (= mark gnus-ticked-mark)
12093             (= mark gnus-dormant-mark))
12094         (gnus-mark-article-as-unread article mark)
12095       (gnus-mark-article-as-read article mark))
12096
12097     ;; See whether the article is to be put in the cache.
12098     (and gnus-use-cache
12099          (not (= mark gnus-canceled-mark))
12100          (vectorp (gnus-summary-article-header article))
12101          (save-excursion
12102            (gnus-cache-possibly-enter-article
12103             gnus-newsgroup-name article
12104             (gnus-summary-article-header article)
12105             (= mark gnus-ticked-mark)
12106             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
12107
12108     (if (gnus-summary-goto-subject article nil t)
12109         (let ((buffer-read-only nil))
12110           (gnus-summary-show-thread)
12111           ;; Fix the mark.
12112           (gnus-summary-update-mark mark 'unread)
12113           t))))
12114
12115 (defun gnus-summary-update-secondary-mark (article)
12116   "Update the secondary (read, process, cache) mark."
12117   (gnus-summary-update-mark
12118    (cond ((memq article gnus-newsgroup-processable)
12119           gnus-process-mark)
12120          ((memq article gnus-newsgroup-cached)
12121           gnus-cached-mark)
12122          ((memq article gnus-newsgroup-replied)
12123           gnus-replied-mark)
12124          ((memq article gnus-newsgroup-saved)
12125           gnus-saved-mark)
12126          (t gnus-unread-mark))
12127    'replied)
12128   (when (gnus-visual-p 'summary-highlight 'highlight)
12129     (run-hooks 'gnus-summary-update-hook))
12130   t)
12131
12132 (defun gnus-summary-update-mark (mark type)
12133   (beginning-of-line)
12134   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
12135         (buffer-read-only nil))
12136     (when (and forward
12137                (<= (+ forward (point)) (point-max)))
12138       ;; Go to the right position on the line.
12139       (goto-char (+ forward (point)))
12140       ;; Replace the old mark with the new mark.
12141       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
12142       ;; Optionally update the marks by some user rule.
12143       (when (eq type 'unread)
12144         (gnus-data-set-mark
12145          (gnus-data-find (gnus-summary-article-number)) mark)
12146         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
12147
12148 (defun gnus-mark-article-as-read (article &optional mark)
12149   "Enter ARTICLE in the pertinent lists and remove it from others."
12150   ;; Make the article expirable.
12151   (let ((mark (or mark gnus-del-mark)))
12152     (if (= mark gnus-expirable-mark)
12153         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
12154       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
12155     ;; Remove from unread and marked lists.
12156     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12157     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12158     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12159     (push (cons article mark) gnus-newsgroup-reads)
12160     ;; Possibly remove from cache, if that is used.
12161     (when gnus-use-cache
12162       (gnus-cache-enter-remove-article article))))
12163
12164 (defun gnus-mark-article-as-unread (article &optional mark)
12165   "Enter ARTICLE in the pertinent lists and remove it from others."
12166   (let ((mark (or mark gnus-ticked-mark)))
12167     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
12168     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
12169     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
12170     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
12171     (cond ((= mark gnus-ticked-mark)
12172            (push article gnus-newsgroup-marked))
12173           ((= mark gnus-dormant-mark)
12174            (push article gnus-newsgroup-dormant))
12175           (t
12176            (push article gnus-newsgroup-unreads)))
12177     (setq gnus-newsgroup-reads
12178           (delq (assq article gnus-newsgroup-reads)
12179                 gnus-newsgroup-reads))))
12180
12181 (defalias 'gnus-summary-mark-as-unread-forward
12182   'gnus-summary-tick-article-forward)
12183 (make-obsolete 'gnus-summary-mark-as-unread-forward
12184                'gnus-summary-tick-article-forward)
12185 (defun gnus-summary-tick-article-forward (n)
12186   "Tick N articles forwards.
12187 If N is negative, tick backwards instead.
12188 The difference between N and the number of articles ticked is returned."
12189   (interactive "p")
12190   (gnus-summary-mark-forward n gnus-ticked-mark))
12191
12192 (defalias 'gnus-summary-mark-as-unread-backward
12193   'gnus-summary-tick-article-backward)
12194 (make-obsolete 'gnus-summary-mark-as-unread-backward
12195                'gnus-summary-tick-article-backward)
12196 (defun gnus-summary-tick-article-backward (n)
12197   "Tick N articles backwards.
12198 The difference between N and the number of articles ticked is returned."
12199   (interactive "p")
12200   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
12201
12202 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12203 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
12204 (defun gnus-summary-tick-article (&optional article clear-mark)
12205   "Mark current article as unread.
12206 Optional 1st argument ARTICLE specifies article number to be marked as unread.
12207 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
12208   (interactive)
12209   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
12210                                        gnus-ticked-mark)))
12211
12212 (defun gnus-summary-mark-as-read-forward (n)
12213   "Mark N articles as read forwards.
12214 If N is negative, mark backwards instead.
12215 The difference between N and the actual number of articles marked is
12216 returned."
12217   (interactive "p")
12218   (gnus-summary-mark-forward n gnus-del-mark t))
12219
12220 (defun gnus-summary-mark-as-read-backward (n)
12221   "Mark the N articles as read backwards.
12222 The difference between N and the actual number of articles marked is
12223 returned."
12224   (interactive "p")
12225   (gnus-summary-mark-forward (- n) gnus-del-mark t))
12226
12227 (defun gnus-summary-mark-as-read (&optional article mark)
12228   "Mark current article as read.
12229 ARTICLE specifies the article to be marked as read.
12230 MARK specifies a string to be inserted at the beginning of the line."
12231   (gnus-summary-mark-article article mark))
12232
12233 (defun gnus-summary-clear-mark-forward (n)
12234   "Clear marks from N articles forward.
12235 If N is negative, clear backward instead.
12236 The difference between N and the number of marks cleared is returned."
12237   (interactive "p")
12238   (gnus-summary-mark-forward n gnus-unread-mark))
12239
12240 (defun gnus-summary-clear-mark-backward (n)
12241   "Clear marks from N articles backward.
12242 The difference between N and the number of marks cleared is returned."
12243   (interactive "p")
12244   (gnus-summary-mark-forward (- n) gnus-unread-mark))
12245
12246 (defun gnus-summary-mark-unread-as-read ()
12247   "Intended to be used by `gnus-summary-mark-article-hook'."
12248   (when (memq gnus-current-article gnus-newsgroup-unreads)
12249     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
12250
12251 (defun gnus-summary-mark-read-and-unread-as-read ()
12252   "Intended to be used by `gnus-summary-mark-article-hook'."
12253   (let ((mark (gnus-summary-article-mark)))
12254     (when (or (gnus-unread-mark-p mark)
12255               (gnus-read-mark-p mark))
12256       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
12257
12258 (defun gnus-summary-mark-region-as-read (point mark all)
12259   "Mark all unread articles between point and mark as read.
12260 If given a prefix, mark all articles between point and mark as read,
12261 even ticked and dormant ones."
12262   (interactive "r\nP")
12263   (save-excursion
12264     (let (article)
12265       (goto-char point)
12266       (beginning-of-line)
12267       (while (and
12268               (< (point) mark)
12269               (progn
12270                 (when (or all
12271                           (memq (setq article (gnus-summary-article-number))
12272                                 gnus-newsgroup-unreads))
12273                   (gnus-summary-mark-article article gnus-del-mark))
12274                 t)
12275               (gnus-summary-find-next))))))
12276
12277 (defun gnus-summary-mark-below (score mark)
12278   "Mark articles with score less than SCORE with MARK."
12279   (interactive "P\ncMark: ")
12280   (gnus-set-global-variables)
12281   (setq score (if score
12282                   (prefix-numeric-value score)
12283                 (or gnus-summary-default-score 0)))
12284   (save-excursion
12285     (set-buffer gnus-summary-buffer)
12286     (goto-char (point-min))
12287     (while 
12288         (progn
12289           (and (< (gnus-summary-article-score) score)
12290                (gnus-summary-mark-article nil mark))
12291           (gnus-summary-find-next)))))
12292
12293 (defun gnus-summary-kill-below (&optional score)
12294   "Mark articles with score below SCORE as read."
12295   (interactive "P")
12296   (gnus-set-global-variables)
12297   (gnus-summary-mark-below score gnus-killed-mark))
12298
12299 (defun gnus-summary-clear-above (&optional score)
12300   "Clear all marks from articles with score above SCORE."
12301   (interactive "P")
12302   (gnus-set-global-variables)
12303   (gnus-summary-mark-above score gnus-unread-mark))
12304
12305 (defun gnus-summary-tick-above (&optional score)
12306   "Tick all articles with score above SCORE."
12307   (interactive "P")
12308   (gnus-set-global-variables)
12309   (gnus-summary-mark-above score gnus-ticked-mark))
12310
12311 (defun gnus-summary-mark-above (score mark)
12312   "Mark articles with score over SCORE with MARK."
12313   (interactive "P\ncMark: ")
12314   (gnus-set-global-variables)
12315   (setq score (if score
12316                   (prefix-numeric-value score)
12317                 (or gnus-summary-default-score 0)))
12318   (save-excursion
12319     (set-buffer gnus-summary-buffer)
12320     (goto-char (point-min))
12321     (while (and (progn
12322                   (if (> (gnus-summary-article-score) score)
12323                       (gnus-summary-mark-article nil mark))
12324                   t)
12325                 (gnus-summary-find-next)))))
12326
12327 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12328 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12329 (defun gnus-summary-limit-include-expunged ()
12330   "Display all the hidden articles that were expunged for low scores."
12331   (interactive)
12332   (gnus-set-global-variables)
12333   (let ((buffer-read-only nil))
12334     (let ((scored gnus-newsgroup-scored)
12335           headers h)
12336       (while scored
12337         (or (gnus-summary-goto-subject (caar scored))
12338             (and (setq h (gnus-summary-article-header (caar scored)))
12339                  (< (cdar scored) gnus-summary-expunge-below)
12340                  (setq headers (cons h headers))))
12341         (setq scored (cdr scored)))
12342       (or headers (error "No expunged articles hidden."))
12343       (goto-char (point-min))
12344       (gnus-summary-prepare-unthreaded (nreverse headers)))
12345     (goto-char (point-min))
12346     (gnus-summary-position-point)))
12347
12348 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12349   "Mark all articles not marked as unread in this newsgroup as read.
12350 If prefix argument ALL is non-nil, all articles are marked as read.
12351 If QUIETLY is non-nil, no questions will be asked.
12352 If TO-HERE is non-nil, it should be a point in the buffer.  All
12353 articles before this point will be marked as read.
12354 The number of articles marked as read is returned."
12355   (interactive "P")
12356   (gnus-set-global-variables)
12357   (prog1
12358       (if (or quietly
12359               (not gnus-interactive-catchup) ;Without confirmation?
12360               gnus-expert-user
12361               (gnus-y-or-n-p
12362                (if all
12363                    "Mark absolutely all articles as read? "
12364                  "Mark all unread articles as read? ")))
12365           (if (and not-mark
12366                    (not gnus-newsgroup-adaptive)
12367                    (not gnus-newsgroup-auto-expire))
12368               (progn
12369                 (when all
12370                   (setq gnus-newsgroup-marked nil
12371                         gnus-newsgroup-dormant nil))
12372                 (setq gnus-newsgroup-unreads nil))
12373             ;; We actually mark all articles as canceled, which we
12374             ;; have to do when using auto-expiry or adaptive scoring.
12375             (gnus-summary-show-all-threads)
12376             (if (gnus-summary-first-subject (not all))
12377                 (while (and
12378                         (if to-here (< (point) to-here) t)
12379                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12380                         (gnus-summary-find-next (not all)))))
12381             (unless to-here
12382               (setq gnus-newsgroup-unreads nil))
12383             (gnus-set-mode-line 'summary)))
12384     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12385       (if (and (not to-here) (eq 'nnvirtual (car method)))
12386           (nnvirtual-catchup-group
12387            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12388     (gnus-summary-position-point)))
12389
12390 (defun gnus-summary-catchup-to-here (&optional all)
12391   "Mark all unticked articles before the current one as read.
12392 If ALL is non-nil, also mark ticked and dormant articles as read."
12393   (interactive "P")
12394   (gnus-set-global-variables)
12395   (save-excursion
12396     (gnus-save-hidden-threads
12397       (let ((beg (point)))
12398         ;; We check that there are unread articles.
12399         (when (or all (gnus-summary-find-prev))
12400           (gnus-summary-catchup all t beg)))))
12401   (gnus-summary-position-point))
12402
12403 (defun gnus-summary-catchup-all (&optional quietly)
12404   "Mark all articles in this newsgroup as read."
12405   (interactive "P")
12406   (gnus-set-global-variables)
12407   (gnus-summary-catchup t quietly))
12408
12409 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12410   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12411 If prefix argument ALL is non-nil, all articles are marked as read."
12412   (interactive "P")
12413   (gnus-set-global-variables)
12414   (gnus-summary-catchup all quietly nil 'fast)
12415   ;; Select next newsgroup or exit.
12416   (if (eq gnus-auto-select-next 'quietly)
12417       (gnus-summary-next-group nil)
12418     (gnus-summary-exit)))
12419
12420 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12421   "Mark all articles in this newsgroup as read, and then exit."
12422   (interactive "P")
12423   (gnus-set-global-variables)
12424   (gnus-summary-catchup-and-exit t quietly))
12425
12426 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12427 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12428   "Mark all articles in this group as read and select the next group.
12429 If given a prefix, mark all articles, unread as well as ticked, as
12430 read."
12431   (interactive "P")
12432   (gnus-set-global-variables)
12433   (save-excursion
12434     (gnus-summary-catchup all))
12435   (gnus-summary-next-article t nil nil t))
12436
12437 ;; Thread-based commands.
12438
12439 (defun gnus-summary-articles-in-thread (&optional article)
12440   "Return a list of all articles in the current thread.
12441 If ARTICLE is non-nil, return all articles in the thread that starts
12442 with that article."
12443   (let* ((article (or article (gnus-summary-article-number)))
12444          (data (gnus-data-find-list article))
12445          (top-level (gnus-data-level (car data)))
12446          (top-subject
12447           (cond ((null gnus-thread-operation-ignore-subject)
12448                  (gnus-simplify-subject-re
12449                   (mail-header-subject (gnus-data-header (car data)))))
12450                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12451                  (gnus-simplify-subject-fuzzy
12452                   (mail-header-subject (gnus-data-header (car data)))))
12453                 (t nil)))
12454          (end-point (save-excursion
12455                       (if (gnus-summary-go-to-next-thread) 
12456                           (point) (point-max))))
12457          articles)
12458     (while (and data
12459                 (< (gnus-data-pos (car data)) end-point))
12460       (when (or (not top-subject)
12461                 (string= top-subject
12462                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12463                              (gnus-simplify-subject-fuzzy
12464                               (mail-header-subject
12465                                (gnus-data-header (car data))))
12466                            (gnus-simplify-subject-re
12467                             (mail-header-subject
12468                              (gnus-data-header (car data)))))))
12469         (push (gnus-data-number (car data)) articles))
12470       (unless (and (setq data (cdr data))
12471                    (> (gnus-data-level (car data)) top-level))
12472         (setq data nil)))
12473     ;; Return the list of articles.
12474     (nreverse articles)))
12475
12476 (defun gnus-summary-rethread-current ()
12477   "Rethread the thread the current article is part of."
12478   (interactive)
12479   (gnus-set-global-variables)
12480   (let* ((gnus-show-threads t)
12481          (article (gnus-summary-article-number))
12482          (id (mail-header-id (gnus-summary-article-header)))
12483          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12484     (unless id
12485       (error "No article on the current line"))
12486     (gnus-rebuild-thread id)
12487     (gnus-summary-goto-subject article)))
12488
12489 (defun gnus-summary-reparent-thread ()
12490   "Make current article child of the marked (or previous) article.
12491
12492 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12493 is non-nil or the Subject: of both articles are the same."
12494   (interactive)
12495   (or (not (gnus-group-read-only-p))
12496       (error "The current newsgroup does not support article editing."))
12497   (or (<= (length gnus-newsgroup-processable) 1)
12498       (error "No more than one article may be marked."))
12499   (save-window-excursion
12500     (let ((gnus-article-buffer " *reparent*")
12501           (current-article (gnus-summary-article-number))
12502           ; first grab the marked article, otherwise one line up.
12503           (parent-article (if (not (null gnus-newsgroup-processable))
12504                               (car gnus-newsgroup-processable)
12505                             (save-excursion
12506                               (if (eq (forward-line -1) 0)
12507                                   (gnus-summary-article-number)
12508                                 (error "Beginning of summary buffer."))))))
12509       (or (not (eq current-article parent-article))
12510           (error "An article may not be self-referential."))
12511       (let ((message-id (mail-header-id 
12512                          (gnus-summary-article-header parent-article))))
12513         (or (and message-id (not (equal message-id "")))
12514             (error "No message-id in desired parent."))
12515         (gnus-summary-select-article t t nil current-article)
12516         (set-buffer gnus-article-buffer)
12517         (setq buffer-read-only nil)
12518         (let ((buf (format "%s" (buffer-string))))
12519           (erase-buffer)
12520           (insert buf))
12521         (goto-char (point-min))
12522         (if (search-forward-regexp "^References: " nil t)
12523             (insert message-id " " )
12524           (insert "References: " message-id "\n"))
12525         (or (gnus-request-replace-article current-article
12526                                           (car gnus-article-current)
12527                                           gnus-article-buffer)
12528             (error "Couldn't replace article."))
12529         (set-buffer gnus-summary-buffer)
12530         (gnus-summary-unmark-all-processable)
12531         (gnus-summary-rethread-current)
12532         (gnus-message 3 "Article %d is now the child of article %d."
12533                       current-article parent-article)))))
12534
12535 (defun gnus-summary-toggle-threads (&optional arg)
12536   "Toggle showing conversation threads.
12537 If ARG is positive number, turn showing conversation threads on."
12538   (interactive "P")
12539   (gnus-set-global-variables)
12540   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12541     (setq gnus-show-threads
12542           (if (null arg) (not gnus-show-threads)
12543             (> (prefix-numeric-value arg) 0)))
12544     (gnus-summary-prepare)
12545     (gnus-summary-goto-subject current)
12546     (gnus-summary-position-point)))
12547
12548 (defun gnus-summary-show-all-threads ()
12549   "Show all threads."
12550   (interactive)
12551   (gnus-set-global-variables)
12552   (save-excursion
12553     (let ((buffer-read-only nil))
12554       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12555   (gnus-summary-position-point))
12556
12557 (defun gnus-summary-show-thread ()
12558   "Show thread subtrees.
12559 Returns nil if no thread was there to be shown."
12560   (interactive)
12561   (gnus-set-global-variables)
12562   (let ((buffer-read-only nil)
12563         (orig (point))
12564         ;; first goto end then to beg, to have point at beg after let
12565         (end (progn (end-of-line) (point)))
12566         (beg (progn (beginning-of-line) (point))))
12567     (prog1
12568         ;; Any hidden lines here?
12569         (search-forward "\r" end t)
12570       (subst-char-in-region beg end ?\^M ?\n t)
12571       (goto-char orig)
12572       (gnus-summary-position-point))))
12573
12574 (defun gnus-summary-hide-all-threads ()
12575   "Hide all thread subtrees."
12576   (interactive)
12577   (gnus-set-global-variables)
12578   (save-excursion
12579     (goto-char (point-min))
12580     (gnus-summary-hide-thread)
12581     (while (zerop (gnus-summary-next-thread 1 t))
12582       (gnus-summary-hide-thread)))
12583   (gnus-summary-position-point))
12584
12585 (defun gnus-summary-hide-thread ()
12586   "Hide thread subtrees.
12587 Returns nil if no threads were there to be hidden."
12588   (interactive)
12589   (gnus-set-global-variables)
12590   (let ((buffer-read-only nil)
12591         (start (point))
12592         (article (gnus-summary-article-number)))
12593     (goto-char start)
12594     ;; Go forward until either the buffer ends or the subthread
12595     ;; ends.
12596     (when (and (not (eobp))
12597                (or (zerop (gnus-summary-next-thread 1 t))
12598                    (goto-char (point-max))))
12599       (prog1
12600           (if (and (> (point) start)
12601                    (search-backward "\n" start t))
12602               (progn
12603                 (subst-char-in-region start (point) ?\n ?\^M)
12604                 (gnus-summary-goto-subject article))
12605             (goto-char start)
12606             nil)
12607         ;;(gnus-summary-position-point)
12608         ))))
12609
12610 (defun gnus-summary-go-to-next-thread (&optional previous)
12611   "Go to the same level (or less) next thread.
12612 If PREVIOUS is non-nil, go to previous thread instead.
12613 Return the article number moved to, or nil if moving was impossible."
12614   (let ((level (gnus-summary-thread-level))
12615         (way (if previous -1 1))
12616         (beg (point)))
12617     (forward-line way)
12618     (while (and (not (eobp))
12619                 (< level (gnus-summary-thread-level)))
12620       (forward-line way))
12621     (if (eobp)
12622         (progn
12623           (goto-char beg)
12624           nil)
12625       (setq beg (point))
12626       (prog1
12627           (gnus-summary-article-number)
12628         (goto-char beg)))))
12629
12630 (defun gnus-summary-go-to-next-thread-old (&optional previous)
12631   "Go to the same level (or less) next thread.
12632 If PREVIOUS is non-nil, go to previous thread instead.
12633 Return the article number moved to, or nil if moving was impossible."
12634   (if (and (eq gnus-summary-make-false-root 'dummy)
12635            (gnus-summary-article-intangible-p))
12636       (let ((beg (point)))
12637         (while (and (zerop (forward-line 1))
12638                     (not (gnus-summary-article-intangible-p))
12639                     (not (zerop (save-excursion 
12640                                   (gnus-summary-thread-level))))))
12641         (if (eobp)
12642             (progn
12643               (goto-char beg)
12644               nil)
12645           (point)))
12646     (let* ((level (gnus-summary-thread-level))
12647            (article (gnus-summary-article-number))
12648            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12649            oart)
12650       (while data
12651         (if (<= (gnus-data-level (car data)) level)
12652             (setq oart (gnus-data-number (car data))
12653                   data nil)
12654           (setq data (cdr data))))
12655       (and oart
12656            (gnus-summary-goto-subject oart)))))
12657
12658 (defun gnus-summary-next-thread (n &optional silent)
12659   "Go to the same level next N'th thread.
12660 If N is negative, search backward instead.
12661 Returns the difference between N and the number of skips actually
12662 done.
12663
12664 If SILENT, don't output messages."
12665   (interactive "p")
12666   (gnus-set-global-variables)
12667   (let ((backward (< n 0))
12668         (n (abs n))
12669         old dum int)
12670     (while (and (> n 0)
12671                 (gnus-summary-go-to-next-thread backward))
12672       (decf n))
12673     (unless silent 
12674       (gnus-summary-position-point))
12675     (when (and (not silent) (/= 0 n))
12676       (gnus-message 7 "No more threads"))
12677     n))
12678
12679 (defun gnus-summary-prev-thread (n)
12680   "Go to the same level previous N'th thread.
12681 Returns the difference between N and the number of skips actually
12682 done."
12683   (interactive "p")
12684   (gnus-set-global-variables)
12685   (gnus-summary-next-thread (- n)))
12686
12687 (defun gnus-summary-go-down-thread ()
12688   "Go down one level in the current thread."
12689   (let ((children (gnus-summary-article-children)))
12690     (and children
12691          (gnus-summary-goto-subject (car children)))))
12692
12693 (defun gnus-summary-go-up-thread ()
12694   "Go up one level in the current thread."
12695   (let ((parent (gnus-summary-article-parent)))
12696     (and parent
12697          (gnus-summary-goto-subject parent))))
12698
12699 (defun gnus-summary-down-thread (n)
12700   "Go down thread N steps.
12701 If N is negative, go up instead.
12702 Returns the difference between N and how many steps down that were
12703 taken."
12704   (interactive "p")
12705   (gnus-set-global-variables)
12706   (let ((up (< n 0))
12707         (n (abs n)))
12708     (while (and (> n 0)
12709                 (if up (gnus-summary-go-up-thread)
12710                   (gnus-summary-go-down-thread)))
12711       (setq n (1- n)))
12712     (gnus-summary-position-point)
12713     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12714     n))
12715
12716 (defun gnus-summary-up-thread (n)
12717   "Go up thread N steps.
12718 If N is negative, go up instead.
12719 Returns the difference between N and how many steps down that were
12720 taken."
12721   (interactive "p")
12722   (gnus-set-global-variables)
12723   (gnus-summary-down-thread (- n)))
12724
12725 (defun gnus-summary-top-thread ()
12726   "Go to the top of the thread."
12727   (interactive)
12728   (gnus-set-global-variables)
12729   (while (gnus-summary-go-up-thread))
12730   (gnus-summary-article-number))
12731
12732 (defun gnus-summary-kill-thread (&optional unmark)
12733   "Mark articles under current thread as read.
12734 If the prefix argument is positive, remove any kinds of marks.
12735 If the prefix argument is negative, tick articles instead."
12736   (interactive "P")
12737   (gnus-set-global-variables)
12738   (if unmark
12739       (setq unmark (prefix-numeric-value unmark)))
12740   (let ((articles (gnus-summary-articles-in-thread)))
12741     (save-excursion
12742       ;; Expand the thread.
12743       (gnus-summary-show-thread)
12744       ;; Mark all the articles.
12745       (while articles
12746         (gnus-summary-goto-subject (car articles))
12747         (cond ((null unmark)
12748                (gnus-summary-mark-article-as-read gnus-killed-mark))
12749               ((> unmark 0)
12750                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12751               (t
12752                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12753         (setq articles (cdr articles))))
12754     ;; Hide killed subtrees.
12755     (and (null unmark)
12756          gnus-thread-hide-killed
12757          (gnus-summary-hide-thread))
12758     ;; If marked as read, go to next unread subject.
12759     (if (null unmark)
12760         ;; Go to next unread subject.
12761         (gnus-summary-next-subject 1 t)))
12762   (gnus-set-mode-line 'summary))
12763
12764 ;; Summary sorting commands
12765
12766 (defun gnus-summary-sort-by-number (&optional reverse)
12767   "Sort summary buffer by article number.
12768 Argument REVERSE means reverse order."
12769   (interactive "P")
12770   (gnus-summary-sort 'number reverse))
12771
12772 (defun gnus-summary-sort-by-author (&optional reverse)
12773   "Sort summary buffer by author name alphabetically.
12774 If case-fold-search is non-nil, case of letters is ignored.
12775 Argument REVERSE means reverse order."
12776   (interactive "P")
12777   (gnus-summary-sort 'author reverse))
12778
12779 (defun gnus-summary-sort-by-subject (&optional reverse)
12780   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12781 If case-fold-search is non-nil, case of letters is ignored.
12782 Argument REVERSE means reverse order."
12783   (interactive "P")
12784   (gnus-summary-sort 'subject reverse))
12785
12786 (defun gnus-summary-sort-by-date (&optional reverse)
12787   "Sort summary buffer by date.
12788 Argument REVERSE means reverse order."
12789   (interactive "P")
12790   (gnus-summary-sort 'date reverse))
12791
12792 (defun gnus-summary-sort-by-score (&optional reverse)
12793   "Sort summary buffer by score.
12794 Argument REVERSE means reverse order."
12795   (interactive "P")
12796   (gnus-summary-sort 'score reverse))
12797
12798 (defun gnus-summary-sort (predicate reverse)
12799   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12800   (gnus-set-global-variables)
12801   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12802          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12803          (gnus-thread-sort-functions
12804           (list
12805            (if (not reverse)
12806                thread
12807              `(lambda (t1 t2)
12808                 (,thread t2 t1)))))
12809          (gnus-article-sort-functions
12810           (list
12811            (if (not reverse)
12812                article
12813              `(lambda (t1 t2)
12814                 (,article t2 t1)))))
12815          (buffer-read-only)
12816          (gnus-summary-prepare-hook nil))
12817     ;; We do the sorting by regenerating the threads.
12818     (gnus-summary-prepare)
12819     ;; Hide subthreads if needed.
12820     (when (and gnus-show-threads gnus-thread-hide-subtree)
12821       (gnus-summary-hide-all-threads)))
12822   ;; If in async mode, we send some info to the backend.
12823   (when gnus-newsgroup-async
12824     (gnus-request-asynchronous
12825      gnus-newsgroup-name gnus-newsgroup-data)))
12826
12827 (defun gnus-sortable-date (date)
12828   "Make sortable string by string-lessp from DATE.
12829 Timezone package is used."
12830   (setq date (timezone-fix-time date nil nil))
12831   (timezone-make-sortable-date
12832    (aref date 0) (aref date 2) (aref date 2)
12833    (timezone-make-time-string
12834     (aref date 3) (aref date 4) (aref date 5))))
12835
12836 ;; Summary saving commands.
12837
12838 (defun gnus-summary-save-article (&optional n not-saved)
12839   "Save the current article using the default saver function.
12840 If N is a positive number, save the N next articles.
12841 If N is a negative number, save the N previous articles.
12842 If N is nil and any articles have been marked with the process mark,
12843 save those articles instead.
12844 The variable `gnus-default-article-saver' specifies the saver function."
12845   (interactive "P")
12846   (gnus-set-global-variables)
12847   (let ((articles (gnus-summary-work-articles n))
12848         file header article)
12849     (while articles
12850       (setq header (gnus-summary-article-header
12851                     (setq article (pop articles))))
12852       (if (not (vectorp header))
12853           ;; This is a pseudo-article.
12854           (if (assq 'name header)
12855               (gnus-copy-file (cdr (assq 'name header)))
12856             (gnus-message 1 "Article %d is unsaveable" article))
12857         ;; This is a real article.
12858         (save-window-excursion
12859           (gnus-summary-select-article t nil nil article))
12860         (unless gnus-save-all-headers
12861           ;; Remove headers accoring to `gnus-saved-headers'.
12862           (let ((gnus-visible-headers
12863                  (or gnus-saved-headers gnus-visible-headers)))
12864             (gnus-article-hide-headers nil t)))
12865         ;; Remove any X-Gnus lines.
12866         (save-excursion
12867           (set-buffer gnus-article-buffer)
12868           (save-restriction
12869             (let ((buffer-read-only nil))
12870               (nnheader-narrow-to-headers)
12871               (while (re-search-forward "^X-Gnus" nil t)
12872                 (gnus-delete-line)))))
12873         (save-window-excursion
12874           (if (not gnus-default-article-saver)
12875               (error "No default saver is defined.")
12876             (setq file (funcall
12877                         gnus-default-article-saver
12878                         (cond
12879                          ((not gnus-prompt-before-saving)
12880                           'default)
12881                          ((eq gnus-prompt-before-saving 'always)
12882                           nil)
12883                          (t file))))))
12884         (gnus-summary-remove-process-mark article)
12885         (unless not-saved
12886           (gnus-summary-set-saved-mark article))))
12887     (gnus-summary-position-point)
12888     n))
12889
12890 (defun gnus-summary-pipe-output (&optional arg)
12891   "Pipe the current article to a subprocess.
12892 If N is a positive number, pipe the N next articles.
12893 If N is a negative number, pipe the N previous articles.
12894 If N is nil and any articles have been marked with the process mark,
12895 pipe those articles instead."
12896   (interactive "P")
12897   (gnus-set-global-variables)
12898   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12899     (gnus-summary-save-article arg t))
12900   (gnus-configure-windows 'pipe))
12901
12902 (defun gnus-summary-save-article-mail (&optional arg)
12903   "Append the current article to an mail file.
12904 If N is a positive number, save the N next articles.
12905 If N is a negative number, save the N previous articles.
12906 If N is nil and any articles have been marked with the process mark,
12907 save those articles instead."
12908   (interactive "P")
12909   (gnus-set-global-variables)
12910   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12911     (gnus-summary-save-article arg)))
12912
12913 (defun gnus-summary-save-article-rmail (&optional arg)
12914   "Append the current article to an rmail file.
12915 If N is a positive number, save the N next articles.
12916 If N is a negative number, save the N previous articles.
12917 If N is nil and any articles have been marked with the process mark,
12918 save those articles instead."
12919   (interactive "P")
12920   (gnus-set-global-variables)
12921   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12922     (gnus-summary-save-article arg)))
12923
12924 (defun gnus-summary-save-article-file (&optional arg)
12925   "Append the current article to a file.
12926 If N is a positive number, save the N next articles.
12927 If N is a negative number, save the N previous articles.
12928 If N is nil and any articles have been marked with the process mark,
12929 save those articles instead."
12930   (interactive "P")
12931   (gnus-set-global-variables)
12932   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12933     (gnus-summary-save-article arg)))
12934
12935 (defun gnus-summary-save-article-body-file (&optional arg)
12936   "Append the current article body to a file.
12937 If N is a positive number, save the N next articles.
12938 If N is a negative number, save the N previous articles.
12939 If N is nil and any articles have been marked with the process mark,
12940 save those articles instead."
12941   (interactive "P")
12942   (gnus-set-global-variables)
12943   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12944     (gnus-summary-save-article arg)))
12945
12946 (defun gnus-get-split-value (methods)
12947   "Return a value based on the split METHODS."
12948   (let (split-name method result match)
12949     (when methods
12950       (save-excursion
12951         (set-buffer gnus-original-article-buffer)
12952         (save-restriction
12953           (nnheader-narrow-to-headers)
12954           (while methods
12955             (goto-char (point-min))
12956             (setq method (pop methods))
12957             (setq match (car method))
12958             (when (cond
12959                    ((stringp match)
12960                     ;; Regular expression.
12961                     (condition-case ()
12962                         (re-search-forward match nil t)
12963                       (error nil)))
12964                    ((gnus-functionp match)
12965                     ;; Function.
12966                     (save-restriction
12967                       (widen)
12968                       (setq result (funcall match gnus-newsgroup-name))))
12969                    ((consp match)
12970                     ;; Form.
12971                     (save-restriction
12972                       (widen)
12973                       (setq result (eval match)))))
12974               (setq split-name (append (cdr method) split-name))
12975               (cond ((stringp result)
12976                      (push result split-name))
12977                     ((consp result)
12978                      (setq split-name (append result split-name)))))))))
12979     split-name))
12980
12981 (defun gnus-read-move-group-name (prompt default articles prefix)
12982   "Read a group name."
12983   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12984          (minibuffer-confirm-incomplete nil) ; XEmacs
12985          group-map
12986          (dum (mapatoms
12987                (lambda (g) 
12988                  (and (boundp g)
12989                       (symbol-name g)
12990                       (memq 'respool
12991                             (assoc (symbol-name
12992                                     (car (gnus-find-method-for-group
12993                                           (symbol-name g))))
12994                                    gnus-valid-select-methods))
12995                       (push (list (symbol-name g)) group-map)))
12996                gnus-active-hashtb))
12997          (prom
12998           (format "%s %s to:"
12999                   prompt
13000                   (if (> (length articles) 1)
13001                       (format "these %d articles" (length articles))
13002                     "this article")))
13003          (to-newsgroup
13004           (cond
13005            ((null split-name)
13006             (gnus-completing-read default prom
13007                                   group-map nil nil prefix
13008                                   'gnus-group-history))
13009            ((= 1 (length split-name))
13010             (gnus-completing-read (car split-name) prom group-map
13011                                   nil nil nil
13012                                   'gnus-group-history))
13013            (t
13014             (gnus-completing-read nil prom 
13015                                   (mapcar (lambda (el) (list el))
13016                                           (nreverse split-name))
13017                                   nil nil nil
13018                                   'gnus-group-history)))))
13019     (when to-newsgroup
13020       (if (or (string= to-newsgroup "")
13021               (string= to-newsgroup prefix))
13022           (setq to-newsgroup (or default "")))
13023       (or (gnus-active to-newsgroup)
13024           (gnus-activate-group to-newsgroup)
13025           (error "No such group: %s" to-newsgroup)))
13026     to-newsgroup))
13027
13028 (defun gnus-read-save-file-name (prompt default-name)
13029   (let* ((split-name (gnus-get-split-value gnus-split-methods))
13030          (file
13031           ;; Let the split methods have their say.
13032           (cond
13033            ;; No split name was found.
13034            ((null split-name)
13035             (read-file-name
13036              (concat prompt " (default "
13037                      (file-name-nondirectory default-name) ") ")
13038              (file-name-directory default-name)
13039              default-name))
13040            ;; A single split name was found
13041            ((= 1 (length split-name))
13042             (read-file-name
13043              (concat prompt " (default " (car split-name) ") ")
13044              gnus-article-save-directory
13045              (concat gnus-article-save-directory (car split-name))))
13046            ;; A list of splits was found.
13047            (t
13048             (setq split-name (nreverse split-name))
13049             (let (result)
13050               (let ((file-name-history (nconc split-name file-name-history)))
13051                 (setq result
13052                       (read-file-name
13053                        (concat prompt " (`M-p' for defaults) ")
13054                        gnus-article-save-directory
13055                        (car split-name))))
13056               (car (push result file-name-history)))))))
13057     ;; If we have read a directory, we append the default file name.
13058     (when (file-directory-p file)
13059       (setq file (concat (file-name-as-directory file)
13060                          (file-name-nondirectory default-name))))
13061     ;; Possibly translate some charaters.
13062     (nnheader-translate-file-chars file)))
13063
13064 (defun gnus-article-archive-name (group)
13065   "Return the first instance of an \"Archive-name\" in the current buffer."
13066   (let ((case-fold-search t))
13067     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
13068       (match-string 1))))
13069
13070 (defun gnus-summary-save-in-rmail (&optional filename)
13071   "Append this article to Rmail file.
13072 Optional argument FILENAME specifies file name.
13073 Directory to save to is default to `gnus-article-save-directory'."
13074   (interactive)
13075   (gnus-set-global-variables)
13076   (let ((default-name
13077           (funcall gnus-rmail-save-name gnus-newsgroup-name
13078                    gnus-current-headers gnus-newsgroup-last-rmail)))
13079     (setq filename
13080           (cond ((eq filename 'default)
13081                  default-name)
13082                 (filename filename)
13083                 (t (gnus-read-save-file-name
13084                     "Save in rmail file:" default-name))))
13085     (gnus-make-directory (file-name-directory filename))
13086     (gnus-eval-in-buffer-window
13087      gnus-original-article-buffer
13088      (save-excursion
13089        (save-restriction
13090          (widen)
13091          (gnus-output-to-rmail filename))))
13092     ;; Remember the directory name to save articles
13093     (setq gnus-newsgroup-last-rmail filename)))
13094
13095 (defun gnus-summary-save-in-mail (&optional filename)
13096   "Append this article to Unix mail file.
13097 Optional argument FILENAME specifies file name.
13098 Directory to save to is default to `gnus-article-save-directory'."
13099   (interactive)
13100   (gnus-set-global-variables)
13101   (let ((default-name
13102           (funcall gnus-mail-save-name gnus-newsgroup-name
13103                    gnus-current-headers gnus-newsgroup-last-mail)))
13104     (setq filename
13105           (cond ((eq filename 'default)
13106                  default-name)
13107                 (filename filename)
13108                 (t (gnus-read-save-file-name
13109                     "Save in Unix mail file:" default-name))))
13110     (setq filename
13111           (expand-file-name filename
13112                             (and default-name
13113                                  (file-name-directory default-name))))
13114     (gnus-make-directory (file-name-directory filename))
13115     (gnus-eval-in-buffer-window
13116      gnus-original-article-buffer
13117      (save-excursion
13118        (save-restriction
13119          (widen)
13120          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
13121              (gnus-output-to-rmail filename)
13122            (let ((mail-use-rfc822 t))
13123              (rmail-output filename 1 t t))))))
13124     ;; Remember the directory name to save articles.
13125     (setq gnus-newsgroup-last-mail filename)))
13126
13127 (defun gnus-summary-save-in-file (&optional filename)
13128   "Append this article to file.
13129 Optional argument FILENAME specifies file name.
13130 Directory to save to is default to `gnus-article-save-directory'."
13131   (interactive)
13132   (gnus-set-global-variables)
13133   (let ((default-name
13134           (funcall gnus-file-save-name gnus-newsgroup-name
13135                    gnus-current-headers gnus-newsgroup-last-file)))
13136     (setq filename
13137           (cond ((eq filename 'default)
13138                  default-name)
13139                 (filename filename)
13140                 (t (gnus-read-save-file-name
13141                     "Save in file:" default-name))))
13142     (gnus-make-directory (file-name-directory filename))
13143     (gnus-eval-in-buffer-window
13144      gnus-original-article-buffer
13145      (save-excursion
13146        (save-restriction
13147          (widen)
13148          (gnus-output-to-file filename))))
13149     ;; Remember the directory name to save articles.
13150     (setq gnus-newsgroup-last-file filename)))
13151
13152 (defun gnus-summary-save-body-in-file (&optional filename)
13153   "Append this article body to a file.
13154 Optional argument FILENAME specifies file name.
13155 The directory to save in defaults to `gnus-article-save-directory'."
13156   (interactive)
13157   (gnus-set-global-variables)
13158   (let ((default-name
13159           (funcall gnus-file-save-name gnus-newsgroup-name
13160                    gnus-current-headers gnus-newsgroup-last-file)))
13161     (setq filename
13162           (cond ((eq filename 'default)
13163                  default-name)
13164                 (filename filename)
13165                 (t (gnus-read-save-file-name
13166                     "Save body in file:" default-name))))
13167     (gnus-make-directory (file-name-directory filename))
13168     (gnus-eval-in-buffer-window
13169      gnus-article-buffer
13170      (save-excursion
13171        (save-restriction
13172          (widen)
13173          (goto-char (point-min))
13174          (and (search-forward "\n\n" nil t)
13175               (narrow-to-region (point) (point-max)))
13176          (gnus-output-to-file filename))))
13177     ;; Remember the directory name to save articles.
13178     (setq gnus-newsgroup-last-file filename)))
13179
13180 (defun gnus-summary-save-in-pipe (&optional command)
13181   "Pipe this article to subprocess."
13182   (interactive)
13183   (gnus-set-global-variables)
13184   (setq command
13185         (cond ((eq command 'default)
13186                gnus-last-shell-command)
13187               (command command)
13188               (t (read-string "Shell command on article: "
13189                               gnus-last-shell-command))))
13190   (if (string-equal command "")
13191       (setq command gnus-last-shell-command))
13192   (gnus-eval-in-buffer-window
13193    gnus-article-buffer
13194    (save-restriction
13195      (widen)
13196      (shell-command-on-region (point-min) (point-max) command nil)))
13197   (setq gnus-last-shell-command command))
13198
13199 ;; Summary extract commands
13200
13201 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
13202   (let ((buffer-read-only nil)
13203         (article (gnus-summary-article-number))
13204         after-article b e)
13205     (or (gnus-summary-goto-subject article)
13206         (error (format "No such article: %d" article)))
13207     (gnus-summary-position-point)
13208     ;; If all commands are to be bunched up on one line, we collect
13209     ;; them here.
13210     (if gnus-view-pseudos-separately
13211         ()
13212       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
13213             files action)
13214         (while ps
13215           (setq action (cdr (assq 'action (car ps))))
13216           (setq files (list (cdr (assq 'name (car ps)))))
13217           (while (and ps (cdr ps)
13218                       (string= (or action "1")
13219                                (or (cdr (assq 'action (cadr ps))) "2")))
13220             (setq files (cons (cdr (assq 'name (cadr ps))) files))
13221             (setcdr ps (cddr ps)))
13222           (if (not files)
13223               ()
13224             (if (not (string-match "%s" action))
13225                 (setq files (cons " " files)))
13226             (setq files (cons " " files))
13227             (and (assq 'execute (car ps))
13228                  (setcdr (assq 'execute (car ps))
13229                          (funcall (if (string-match "%s" action)
13230                                       'format 'concat)
13231                                   action
13232                                   (mapconcat (lambda (f) f) files " ")))))
13233           (setq ps (cdr ps)))))
13234     (if (and gnus-view-pseudos (not not-view))
13235         (while pslist
13236           (and (assq 'execute (car pslist))
13237                (gnus-execute-command (cdr (assq 'execute (car pslist)))
13238                                      (eq gnus-view-pseudos 'not-confirm)))
13239           (setq pslist (cdr pslist)))
13240       (save-excursion
13241         (while pslist
13242           (setq after-article (or (cdr (assq 'article (car pslist)))
13243                                   (gnus-summary-article-number)))
13244           (gnus-summary-goto-subject after-article)
13245           (forward-line 1)
13246           (setq b (point))
13247           (insert "    " (file-name-nondirectory
13248                                 (cdr (assq 'name (car pslist))))
13249                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
13250           (setq e (point))
13251           (forward-line -1)             ; back to `b'
13252           (gnus-add-text-properties
13253            b (1- e) (list 'gnus-number gnus-reffed-article-number
13254                           gnus-mouse-face-prop gnus-mouse-face))
13255           (gnus-data-enter
13256            after-article gnus-reffed-article-number
13257            gnus-unread-mark b (car pslist) 0 (- e b))
13258           (push gnus-reffed-article-number gnus-newsgroup-unreads)
13259           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
13260           (setq pslist (cdr pslist)))))))
13261
13262 (defun gnus-pseudos< (p1 p2)
13263   (let ((c1 (cdr (assq 'action p1)))
13264         (c2 (cdr (assq 'action p2))))
13265     (and c1 c2 (string< c1 c2))))
13266
13267 (defun gnus-request-pseudo-article (props)
13268   (cond ((assq 'execute props)
13269          (gnus-execute-command (cdr (assq 'execute props)))))
13270   (let ((gnus-current-article (gnus-summary-article-number)))
13271     (run-hooks 'gnus-mark-article-hook)))
13272
13273 (defun gnus-execute-command (command &optional automatic)
13274   (save-excursion
13275     (gnus-article-setup-buffer)
13276     (set-buffer gnus-article-buffer)
13277     (setq buffer-read-only nil)
13278     (let ((command (if automatic command (read-string "Command: " command)))
13279           ;; Just binding this here doesn't help, because there might
13280           ;; be output from the process after exiting the scope of 
13281           ;; this `let'.
13282           ;; (buffer-read-only nil)
13283           )
13284       (erase-buffer)
13285       (insert "$ " command "\n\n")
13286       (if gnus-view-pseudo-asynchronously
13287           (start-process "gnus-execute" nil "sh" "-c" command)
13288         (call-process "sh" nil t nil "-c" command)))))
13289
13290 (defun gnus-copy-file (file &optional to)
13291   "Copy FILE to TO."
13292   (interactive
13293    (list (read-file-name "Copy file: " default-directory)
13294          (read-file-name "Copy file to: " default-directory)))
13295   (gnus-set-global-variables)
13296   (or to (setq to (read-file-name "Copy file to: " default-directory)))
13297   (and (file-directory-p to)
13298        (setq to (concat (file-name-as-directory to)
13299                         (file-name-nondirectory file))))
13300   (copy-file file to))
13301
13302 ;; Summary kill commands.
13303
13304 (defun gnus-summary-edit-global-kill (article)
13305   "Edit the \"global\" kill file."
13306   (interactive (list (gnus-summary-article-number)))
13307   (gnus-set-global-variables)
13308   (gnus-group-edit-global-kill article))
13309
13310 (defun gnus-summary-edit-local-kill ()
13311   "Edit a local kill file applied to the current newsgroup."
13312   (interactive)
13313   (gnus-set-global-variables)
13314   (setq gnus-current-headers (gnus-summary-article-header))
13315   (gnus-set-global-variables)
13316   (gnus-group-edit-local-kill
13317    (gnus-summary-article-number) gnus-newsgroup-name))
13318
13319 \f
13320 ;;;
13321 ;;; Gnus article mode
13322 ;;;
13323
13324 (put 'gnus-article-mode 'mode-class 'special)
13325
13326 (if gnus-article-mode-map
13327     nil
13328   (setq gnus-article-mode-map (make-keymap))
13329   (suppress-keymap gnus-article-mode-map)
13330
13331   (gnus-define-keys gnus-article-mode-map
13332     " " gnus-article-goto-next-page
13333     "\177" gnus-article-goto-prev-page
13334     [delete] gnus-article-goto-prev-page
13335     "\C-c^" gnus-article-refer-article
13336     "h" gnus-article-show-summary
13337     "s" gnus-article-show-summary
13338     "\C-c\C-m" gnus-article-mail
13339     "?" gnus-article-describe-briefly
13340     gnus-mouse-2 gnus-article-push-button
13341     "\r" gnus-article-press-button
13342     "\t" gnus-article-next-button
13343     "\M-\t" gnus-article-prev-button
13344     "\C-c\C-b" gnus-bug)
13345
13346   (substitute-key-definition
13347    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13348
13349 (defun gnus-article-mode ()
13350   "Major mode for displaying an article.
13351
13352 All normal editing commands are switched off.
13353
13354 The following commands are available:
13355
13356 \\<gnus-article-mode-map>
13357 \\[gnus-article-next-page]\t Scroll the article one page forwards
13358 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13359 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13360 \\[gnus-article-show-summary]\t Display the summary buffer
13361 \\[gnus-article-mail]\t Send a reply to the address near point
13362 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13363 \\[gnus-info-find-node]\t Go to the Gnus info node"
13364   (interactive)
13365   (when (and menu-bar-mode
13366              (gnus-visual-p 'article-menu 'menu))
13367     (gnus-article-make-menu-bar))
13368   (kill-all-local-variables)
13369   (gnus-simplify-mode-line)
13370   (setq mode-name "Article")
13371   (setq major-mode 'gnus-article-mode)
13372   (make-local-variable 'minor-mode-alist)
13373   (or (assq 'gnus-show-mime minor-mode-alist)
13374       (setq minor-mode-alist
13375             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13376   (use-local-map gnus-article-mode-map)
13377   (make-local-variable 'page-delimiter)
13378   (setq page-delimiter gnus-page-delimiter)
13379   (buffer-disable-undo (current-buffer))
13380   (setq buffer-read-only t)             ;Disable modification
13381   (run-hooks 'gnus-article-mode-hook))
13382
13383 (defun gnus-article-setup-buffer ()
13384   "Initialize the article buffer."
13385   (let* ((name (if gnus-single-article-buffer "*Article*"
13386                  (concat "*Article " gnus-newsgroup-name "*")))
13387          (original
13388           (progn (string-match "\\*Article" name)
13389                  (concat " *Original Article"
13390                          (substring name (match-end 0))))))
13391     (setq gnus-article-buffer name)
13392     (setq gnus-original-article-buffer original)
13393     ;; This might be a variable local to the summary buffer.
13394     (unless gnus-single-article-buffer
13395       (save-excursion
13396         (set-buffer gnus-summary-buffer)
13397         (setq gnus-article-buffer name)
13398         (setq gnus-original-article-buffer original)
13399         (gnus-set-global-variables))
13400       (make-local-variable 'gnus-summary-buffer))
13401     ;; Init original article buffer.
13402     (save-excursion
13403       (set-buffer (get-buffer-create gnus-original-article-buffer))
13404       (buffer-disable-undo (current-buffer))
13405       (setq major-mode 'gnus-original-article-mode)
13406       (make-local-variable 'gnus-original-article))
13407     (if (get-buffer name)
13408         (save-excursion
13409           (set-buffer name)
13410           (buffer-disable-undo (current-buffer))
13411           (setq buffer-read-only t)
13412           (gnus-add-current-to-buffer-list)
13413           (or (eq major-mode 'gnus-article-mode)
13414               (gnus-article-mode))
13415           (current-buffer))
13416       (save-excursion
13417         (set-buffer (get-buffer-create name))
13418         (gnus-add-current-to-buffer-list)
13419         (gnus-article-mode)
13420         (current-buffer)))))
13421
13422 ;; Set article window start at LINE, where LINE is the number of lines
13423 ;; from the head of the article.
13424 (defun gnus-article-set-window-start (&optional line)
13425   (set-window-start
13426    (get-buffer-window gnus-article-buffer t)
13427    (save-excursion
13428      (set-buffer gnus-article-buffer)
13429      (goto-char (point-min))
13430      (if (not line)
13431          (point-min)
13432        (gnus-message 6 "Moved to bookmark")
13433        (search-forward "\n\n" nil t)
13434        (forward-line line)
13435        (point)))))
13436
13437 (defun gnus-kill-all-overlays ()
13438   "Delete all overlays in the current buffer."
13439   (when (fboundp 'overlay-lists)
13440     (let* ((overlayss (overlay-lists))
13441            (buffer-read-only nil)
13442            (overlays (nconc (car overlayss) (cdr overlayss))))
13443       (while overlays
13444         (delete-overlay (pop overlays))))))
13445
13446 (defun gnus-request-article-this-buffer (article group)
13447   "Get an article and insert it into this buffer."
13448   (let (do-update-line)
13449     (prog1
13450         (save-excursion
13451           (erase-buffer)
13452           (gnus-kill-all-overlays)
13453           (setq group (or group gnus-newsgroup-name))
13454
13455           ;; Open server if it has closed.
13456           (gnus-check-server (gnus-find-method-for-group group))
13457
13458           ;; Using `gnus-request-article' directly will insert the article into
13459           ;; `nntp-server-buffer' - so we'll save some time by not having to
13460           ;; copy it from the server buffer into the article buffer.
13461
13462           ;; We only request an article by message-id when we do not have the
13463           ;; headers for it, so we'll have to get those.
13464           (when (stringp article)
13465             (let ((gnus-override-method gnus-refer-article-method))
13466               (gnus-read-header article)))
13467
13468           ;; If the article number is negative, that means that this article
13469           ;; doesn't belong in this newsgroup (possibly), so we find its
13470           ;; message-id and request it by id instead of number.
13471           (when (and (numberp article)
13472                      gnus-summary-buffer
13473                      (get-buffer gnus-summary-buffer)
13474                      (buffer-name (get-buffer gnus-summary-buffer)))
13475             (save-excursion
13476               (set-buffer gnus-summary-buffer)
13477               (let ((header (gnus-summary-article-header article)))
13478                 (if (< article 0)
13479                     (cond 
13480                      ((memq article gnus-newsgroup-sparse)
13481                       ;; This is a sparse gap article.
13482                       (setq do-update-line article)
13483                       (setq article (mail-header-id header))
13484                       (let ((gnus-override-method gnus-refer-article-method))
13485                         (gnus-read-header article))
13486                       (setq gnus-newsgroup-sparse
13487                             (delq article gnus-newsgroup-sparse)))
13488                      ((vectorp header)
13489                       ;; It's a real article.
13490                       (setq article (mail-header-id header)))
13491                      (t
13492                       ;; It is an extracted pseudo-article.
13493                       (setq article 'pseudo)
13494                       (gnus-request-pseudo-article header))))
13495                 
13496                 (let ((method (gnus-find-method-for-group 
13497                                gnus-newsgroup-name)))
13498                   (if (not (eq (car method) 'nneething))
13499                       ()
13500                     (let ((dir (concat (file-name-as-directory (nth 1 method))
13501                                        (mail-header-subject header))))
13502                       (if (file-directory-p dir)
13503                           (progn
13504                             (setq article 'nneething)
13505                             (gnus-group-enter-directory dir)))))))))
13506
13507           (cond
13508            ;; Refuse to select canceled articles.
13509            ((and (numberp article)
13510                  gnus-summary-buffer
13511                  (get-buffer gnus-summary-buffer)
13512                  (buffer-name (get-buffer gnus-summary-buffer))
13513                  (eq (cdr (save-excursion
13514                             (set-buffer gnus-summary-buffer)
13515                             (assq article gnus-newsgroup-reads)))
13516                      gnus-canceled-mark))
13517             nil)
13518            ;; We first check `gnus-original-article-buffer'.
13519            ((and (get-buffer gnus-original-article-buffer)
13520                  (numberp article)
13521                  (save-excursion
13522                    (set-buffer gnus-original-article-buffer)
13523                    (and (equal (car gnus-original-article) group)
13524                         (eq (cdr gnus-original-article) article))))
13525             (insert-buffer-substring gnus-original-article-buffer)
13526             'article)
13527            ;; Check the backlog.
13528            ((and gnus-keep-backlog
13529                  (gnus-backlog-request-article group article (current-buffer)))
13530             'article)
13531            ;; Check the cache.
13532            ((and gnus-use-cache
13533                  (numberp article)
13534                  (gnus-cache-request-article article group))
13535             'article)
13536            ;; Get the article and put into the article buffer.
13537            ((or (stringp article) (numberp article))
13538             (let ((gnus-override-method
13539                    (and (stringp article) gnus-refer-article-method))
13540                   (buffer-read-only nil))
13541               (erase-buffer)
13542               (gnus-kill-all-overlays)
13543               (if (gnus-request-article article group (current-buffer))
13544                   (progn
13545                     (and gnus-keep-backlog
13546                          (numberp article)
13547                          (gnus-backlog-enter-article
13548                           group article (current-buffer)))
13549                     'article))))
13550            ;; It was a pseudo.
13551            (t article)))
13552
13553       ;; Take the article from the original article buffer
13554       ;; and place it in the buffer it's supposed to be in.
13555       (when (and (get-buffer gnus-article-buffer)
13556                  (numberp article)
13557                  (equal (buffer-name (current-buffer))
13558                         (buffer-name (get-buffer gnus-article-buffer))))
13559         (save-excursion
13560           (if (get-buffer gnus-original-article-buffer)
13561               (set-buffer (get-buffer gnus-original-article-buffer))
13562             (set-buffer (get-buffer-create gnus-original-article-buffer))
13563             (buffer-disable-undo (current-buffer))
13564             (setq major-mode 'gnus-original-article-mode)
13565             (setq buffer-read-only t)
13566             (gnus-add-current-to-buffer-list))
13567           (let (buffer-read-only)
13568             (erase-buffer)
13569             (insert-buffer-substring gnus-article-buffer))
13570           (setq gnus-original-article (cons group article))))
13571     
13572       ;; Update sparse articles.
13573       (when (and do-update-line
13574                  (or (numberp article)
13575                      (stringp article)))
13576         (let ((buf (current-buffer)))
13577           (set-buffer gnus-summary-buffer)
13578           (gnus-summary-update-article do-update-line)
13579           (gnus-summary-goto-subject do-update-line nil t)
13580           (set-window-point (get-buffer-window (current-buffer) t)
13581                             (point))
13582           (set-buffer buf))))))
13583
13584 (defun gnus-read-header (id &optional header)
13585   "Read the headers of article ID and enter them into the Gnus system."
13586   (let ((group gnus-newsgroup-name)
13587         (gnus-override-method 
13588          (and (gnus-news-group-p gnus-newsgroup-name)
13589               gnus-refer-article-method))       
13590         where)
13591     ;; First we check to see whether the header in question is already
13592     ;; fetched.
13593     (if (stringp id)
13594         ;; This is a Message-ID.
13595         (setq header (or header (gnus-id-to-header id)))
13596       ;; This is an article number.
13597       (setq header (or header (gnus-summary-article-header id))))
13598     (if (and header
13599              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
13600         ;; We have found the header.
13601         header
13602       ;; We have to really fetch the header to this article.
13603       (when (setq where
13604                   (if (gnus-check-backend-function 'request-head group)
13605                       (gnus-request-head id group)
13606                     (gnus-request-article id group)))
13607         (save-excursion
13608           (set-buffer nntp-server-buffer)
13609           (and (search-forward "\n\n" nil t)
13610                (delete-region (1- (point)) (point-max)))
13611           (goto-char (point-max))
13612           (insert ".\n")
13613           (goto-char (point-min))
13614           (insert "211 ")
13615           (princ (cond
13616                   ((numberp id) id)
13617                   ((cdr where) (cdr where))
13618                   (header (mail-header-number header))
13619                   (t gnus-reffed-article-number))
13620                  (current-buffer))
13621           (insert " Article retrieved.\n"))
13622         ;(when (and header
13623         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
13624         ;  (setcar (gnus-id-to-thread id) nil))
13625         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13626             ()                          ; Malformed head.
13627           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
13628             (if (and (stringp id)
13629                      (not (string= (gnus-group-real-name group)
13630                                    (car where))))
13631                 ;; If we fetched by Message-ID and the article came
13632                 ;; from a different group, we fudge some bogus article
13633                 ;; numbers for this article.
13634                 (mail-header-set-number header gnus-reffed-article-number))
13635             (decf gnus-reffed-article-number)
13636             (push header gnus-newsgroup-headers)
13637             (setq gnus-current-headers header)
13638             (push (mail-header-number header) gnus-newsgroup-limit))
13639           header)))))
13640
13641 (defun gnus-article-prepare (article &optional all-headers header)
13642   "Prepare ARTICLE in article mode buffer.
13643 ARTICLE should either be an article number or a Message-ID.
13644 If ARTICLE is an id, HEADER should be the article headers.
13645 If ALL-HEADERS is non-nil, no headers are hidden."
13646   (save-excursion
13647     ;; Make sure we start in a summary buffer.
13648     (unless (eq major-mode 'gnus-summary-mode)
13649       (set-buffer gnus-summary-buffer))
13650     (setq gnus-summary-buffer (current-buffer))
13651     ;; Make sure the connection to the server is alive.
13652     (unless (gnus-server-opened
13653              (gnus-find-method-for-group gnus-newsgroup-name))
13654       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13655       (gnus-request-group gnus-newsgroup-name t))
13656     (let* ((article (if header (mail-header-number header) article))
13657            (summary-buffer (current-buffer))
13658            (internal-hook gnus-article-internal-prepare-hook)
13659            (group gnus-newsgroup-name)
13660            result)
13661       (save-excursion
13662         (gnus-article-setup-buffer)
13663         (set-buffer gnus-article-buffer)
13664         ;; Deactivate active regions.
13665         (when (and (boundp 'transient-mark-mode)
13666                    transient-mark-mode)
13667           (setq mark-active nil))
13668         (if (not (setq result (let ((buffer-read-only nil))
13669                                 (gnus-request-article-this-buffer
13670                                  article group))))
13671             ;; There is no such article.
13672             (save-excursion
13673               (when (and (numberp article)
13674                          (not (memq article gnus-newsgroup-sparse)))
13675                 (setq gnus-article-current
13676                       (cons gnus-newsgroup-name article))
13677                 (set-buffer gnus-summary-buffer)
13678                 (setq gnus-current-article article)
13679                 (gnus-summary-mark-article article gnus-canceled-mark))
13680               (unless (memq article gnus-newsgroup-sparse)
13681                 (gnus-error
13682                  1 "No such article (may have expired or been canceled)")))
13683           (if (or (eq result 'pseudo) (eq result 'nneething))
13684               (progn
13685                 (save-excursion
13686                   (set-buffer summary-buffer)
13687                   (setq gnus-last-article gnus-current-article
13688                         gnus-newsgroup-history (cons gnus-current-article
13689                                                      gnus-newsgroup-history)
13690                         gnus-current-article 0
13691                         gnus-current-headers nil
13692                         gnus-article-current nil)
13693                   (if (eq result 'nneething)
13694                       (gnus-configure-windows 'summary)
13695                     (gnus-configure-windows 'article))
13696                   (gnus-set-global-variables))
13697                 (gnus-set-mode-line 'article))
13698             ;; The result from the `request' was an actual article -
13699             ;; or at least some text that is now displayed in the
13700             ;; article buffer.
13701             (if (and (numberp article)
13702                      (not (eq article gnus-current-article)))
13703                 ;; Seems like a new article has been selected.
13704                 ;; `gnus-current-article' must be an article number.
13705                 (save-excursion
13706                   (set-buffer summary-buffer)
13707                   (setq gnus-last-article gnus-current-article
13708                         gnus-newsgroup-history (cons gnus-current-article
13709                                                      gnus-newsgroup-history)
13710                         gnus-current-article article
13711                         gnus-current-headers
13712                         (gnus-summary-article-header gnus-current-article)
13713                         gnus-article-current
13714                         (cons gnus-newsgroup-name gnus-current-article))
13715                   (unless (vectorp gnus-current-headers)
13716                     (setq gnus-current-headers nil))
13717                   (gnus-summary-show-thread)
13718                   (run-hooks 'gnus-mark-article-hook)
13719                   (gnus-set-mode-line 'summary)
13720                   (and (gnus-visual-p 'article-highlight 'highlight)
13721                        (run-hooks 'gnus-visual-mark-article-hook))
13722                   ;; Set the global newsgroup variables here.
13723                   ;; Suggested by Jim Sisolak
13724                   ;; <sisolak@trans4.neep.wisc.edu>.
13725                   (gnus-set-global-variables)
13726                   (setq gnus-have-all-headers
13727                         (or all-headers gnus-show-all-headers))
13728                   (and gnus-use-cache
13729                        (vectorp (gnus-summary-article-header article))
13730                        (gnus-cache-possibly-enter-article
13731                         group article
13732                         (gnus-summary-article-header article)
13733                         (memq article gnus-newsgroup-marked)
13734                         (memq article gnus-newsgroup-dormant)
13735                         (memq article gnus-newsgroup-unreads)))))
13736             (when (or (numberp article)
13737                       (stringp article))
13738               ;; Hooks for getting information from the article.
13739               ;; This hook must be called before being narrowed.
13740               (let (buffer-read-only)
13741                 (run-hooks 'internal-hook)
13742                 (run-hooks 'gnus-article-prepare-hook)
13743                 ;; Decode MIME message.
13744                 (if gnus-show-mime
13745                     (if (or (not gnus-strict-mime)
13746                             (gnus-fetch-field "Mime-Version"))
13747                         (funcall gnus-show-mime-method)
13748                       (funcall gnus-decode-encoded-word-method)))
13749                 ;; Perform the article display hooks.
13750                 (run-hooks 'gnus-article-display-hook))
13751               ;; Do page break.
13752               (goto-char (point-min))
13753               (and gnus-break-pages (gnus-narrow-to-page)))
13754             (gnus-set-mode-line 'article)
13755             (gnus-configure-windows 'article)
13756             (goto-char (point-min))
13757             t))))))
13758
13759 (defun gnus-article-show-all-headers ()
13760   "Show all article headers in article mode buffer."
13761   (save-excursion
13762     (gnus-article-setup-buffer)
13763     (set-buffer gnus-article-buffer)
13764     (let ((buffer-read-only nil))
13765       (gnus-unhide-text (point-min) (point-max)))))
13766
13767 (defun gnus-article-hide-headers-if-wanted ()
13768   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13769 Provided for backwards compatibility."
13770   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13771       gnus-inhibit-hiding
13772       (gnus-article-hide-headers)))
13773
13774 (defsubst gnus-article-header-rank ()
13775   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13776   (let ((list gnus-sorted-header-list)
13777         (i 0))
13778     (while list
13779       (when (looking-at (car list))
13780         (setq list nil))
13781       (setq list (cdr list))
13782       (incf i))
13783     i))
13784
13785 (defun gnus-article-hide-headers (&optional arg delete)
13786   "Toggle whether to hide unwanted headers and possibly sort them as well.
13787 If given a negative prefix, always show; if given a positive prefix,
13788 always hide."
13789   (interactive "P")
13790   (unless (gnus-article-check-hidden-text 'headers arg)
13791     ;; This function might be inhibited.
13792     (unless gnus-inhibit-hiding
13793       (save-excursion
13794         (set-buffer gnus-article-buffer)
13795         (save-restriction
13796           (let ((buffer-read-only nil)
13797                 (props (nconc (list 'gnus-type 'headers)
13798                               gnus-hidden-properties))
13799                 (max (1+ (length gnus-sorted-header-list)))
13800                 (ignored (when (not (stringp gnus-visible-headers))
13801                            (cond ((stringp gnus-ignored-headers)
13802                                   gnus-ignored-headers)
13803                                  ((listp gnus-ignored-headers)
13804                                   (mapconcat 'identity gnus-ignored-headers
13805                                              "\\|")))))
13806                 (visible
13807                  (cond ((stringp gnus-visible-headers)
13808                         gnus-visible-headers)
13809                        ((and gnus-visible-headers
13810                              (listp gnus-visible-headers))
13811                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13812                 want-list beg)
13813             ;; First we narrow to just the headers.
13814             (widen)
13815             (goto-char (point-min))
13816             ;; Hide any "From " lines at the beginning of (mail) articles.
13817             (while (looking-at "From ")
13818               (forward-line 1))
13819             (unless (bobp)
13820               (gnus-hide-text (point-min) (point) props))
13821             ;; Then treat the rest of the header lines.
13822             (narrow-to-region
13823              (point)
13824              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13825             ;; Then we use the two regular expressions
13826             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13827             ;; select which header lines is to remain visible in the
13828             ;; article buffer.
13829             (goto-char (point-min))
13830             (while (re-search-forward "^[^ \t]*:" nil t)
13831               (beginning-of-line)
13832               ;; We add the headers we want to keep to a list and delete
13833               ;; them from the buffer.
13834               (gnus-put-text-property 
13835                (point) (1+ (point)) 'message-rank
13836                (if (or (and visible (looking-at visible))
13837                        (and ignored
13838                             (not (looking-at ignored))))
13839                    (gnus-article-header-rank) 
13840                  (+ 2 max)))
13841               (forward-line 1))
13842             (message-sort-headers-1)
13843             (when (setq beg (text-property-any 
13844                              (point-min) (point-max) 'message-rank (+ 2 max)))
13845               ;; We make the unwanted headers invisible.
13846               (if delete
13847                   (delete-region beg (point-max))
13848                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13849                 (gnus-hide-text-type beg (point-max) 'headers))
13850               ;; Work around XEmacs lossage.
13851               (gnus-put-text-property (point-min) beg 'invisible nil))))))))
13852
13853 (defun gnus-article-hide-boring-headers (&optional arg)
13854   "Toggle hiding of headers that aren't very interesting.
13855 If given a negative prefix, always show; if given a positive prefix,
13856 always hide."
13857   (interactive "P")
13858   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13859     (save-excursion
13860       (set-buffer gnus-article-buffer)
13861       (save-restriction
13862         (let ((buffer-read-only nil)
13863               (list gnus-boring-article-headers)
13864               (inhibit-point-motion-hooks t)
13865               elem)
13866           (nnheader-narrow-to-headers)
13867           (while list
13868             (setq elem (pop list))
13869             (goto-char (point-min))
13870             (cond
13871              ;; Hide empty headers.
13872              ((eq elem 'empty)
13873               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13874                 (forward-line -1)
13875                 (gnus-hide-text-type
13876                  (progn (beginning-of-line) (point))
13877                  (progn 
13878                    (end-of-line)
13879                    (if (re-search-forward "^[^ \t]" nil t)
13880                        (match-beginning 0)
13881                      (point-max)))
13882                  'boring-headers)))
13883              ;; Hide boring Newsgroups header.
13884              ((eq elem 'newsgroups)
13885               (when (equal (message-fetch-field "newsgroups")
13886                            (gnus-group-real-name gnus-newsgroup-name))
13887                 (gnus-article-hide-header "newsgroups")))
13888              ((eq elem 'followup-to)
13889               (when (equal (message-fetch-field "followup-to")
13890                            (message-fetch-field "newsgroups"))
13891                 (gnus-article-hide-header "followup-to")))
13892              ((eq elem 'reply-to)
13893               (let ((from (message-fetch-field "from"))
13894                     (reply-to (message-fetch-field "reply-to")))
13895                 (when (and
13896                        from reply-to
13897                        (equal 
13898                         (nth 1 (funcall gnus-extract-address-components from))
13899                         (nth 1 (funcall gnus-extract-address-components
13900                                         reply-to))))
13901                   (gnus-article-hide-header "reply-to"))))
13902              ((eq elem 'date)
13903               (let ((date (message-fetch-field "date")))
13904                 (when (and date
13905                            (< (gnus-days-between date (current-time-string))
13906                               4))
13907                   (gnus-article-hide-header "date")))))))))))
13908
13909 (defun gnus-article-hide-header (header)
13910   (save-excursion
13911     (goto-char (point-min))
13912     (when (re-search-forward (concat "^" header ":") nil t)
13913       (gnus-hide-text-type
13914        (progn (beginning-of-line) (point))
13915        (progn 
13916          (end-of-line)
13917          (if (re-search-forward "^[^ \t]" nil t)
13918              (match-beginning 0)
13919            (point-max)))
13920        'boring-headers))))
13921
13922 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13923 (defun gnus-article-treat-overstrike ()
13924   "Translate overstrikes into bold text."
13925   (interactive)
13926   (save-excursion
13927     (set-buffer gnus-article-buffer)
13928     (let ((buffer-read-only nil))
13929       (while (search-forward "\b" nil t)
13930         (let ((next (following-char))
13931               (previous (char-after (- (point) 2))))
13932           (cond ((eq next previous)
13933                  (gnus-put-text-property (- (point) 2) (point) 'invisible t)
13934                  (gnus-put-text-property (point) (1+ (point)) 'face 'bold))
13935                 ((eq next ?_)
13936                  (gnus-put-text-property (1- (point)) (1+ (point)) 'invisible t)
13937                  (gnus-put-text-property
13938                   (- (point) 2) (1- (point)) 'face 'underline))
13939                 ((eq previous ?_)
13940                  (gnus-put-text-property (- (point) 2) (point) 'invisible t)
13941                  (gnus-put-text-property
13942                   (point) (1+ (point))  'face 'underline))))))))
13943
13944 (defun gnus-article-word-wrap ()
13945   "Format too long lines."
13946   (interactive)
13947   (save-excursion
13948     (set-buffer gnus-article-buffer)
13949     (let ((buffer-read-only nil))
13950       (widen)
13951       (goto-char (point-min))
13952       (search-forward "\n\n" nil t)
13953       (end-of-line 1)
13954       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13955             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13956             (adaptive-fill-mode t))
13957         (while (not (eobp))
13958           (and (>= (current-column) (min fill-column (window-width)))
13959                (/= (preceding-char) ?:)
13960                (fill-paragraph nil))
13961           (end-of-line 2))))))
13962
13963 (defun gnus-article-remove-cr ()
13964   "Remove carriage returns from an article."
13965   (interactive)
13966   (save-excursion
13967     (set-buffer gnus-article-buffer)
13968     (let ((buffer-read-only nil))
13969       (goto-char (point-min))
13970       (while (search-forward "\r" nil t)
13971         (replace-match "" t t)))))
13972
13973 (defun gnus-article-remove-trailing-blank-lines ()
13974   "Remove all trailing blank lines from the article."
13975   (interactive)
13976   (save-excursion
13977     (set-buffer gnus-article-buffer)
13978     (let ((buffer-read-only nil))
13979       (goto-char (point-max))
13980       (delete-region
13981        (point)
13982        (progn
13983          (while (looking-at "^[ \t]*$")
13984            (forward-line -1))
13985          (forward-line 1)
13986          (point))))))
13987
13988 (defun gnus-article-display-x-face (&optional force)
13989   "Look for an X-Face header and display it if present."
13990   (interactive (list 'force))
13991   (save-excursion
13992     (set-buffer gnus-article-buffer)
13993     ;; Delete the old process, if any.
13994     (when (process-status "gnus-x-face")
13995       (delete-process "gnus-x-face"))
13996     (let ((inhibit-point-motion-hooks t)
13997           (case-fold-search nil)
13998           from)
13999       (save-restriction
14000         (nnheader-narrow-to-headers)
14001         (setq from (message-fetch-field "from"))
14002         (goto-char (point-min))
14003         (when (and gnus-article-x-face-command
14004                    (or force
14005                        ;; Check whether this face is censored.
14006                        (not gnus-article-x-face-too-ugly)
14007                        (and gnus-article-x-face-too-ugly from
14008                             (not (string-match gnus-article-x-face-too-ugly
14009                                                from))))
14010                    ;; Has to be present.
14011                    (re-search-forward "^X-Face: " nil t))
14012           ;; We now have the area of the buffer where the X-Face is stored.
14013           (let ((beg (point))
14014                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
14015             ;; We display the face.
14016             (if (symbolp gnus-article-x-face-command)
14017                 ;; The command is a lisp function, so we call it.
14018                 (if (gnus-functionp gnus-article-x-face-command)
14019                     (funcall gnus-article-x-face-command beg end)
14020                   (error "%s is not a function" gnus-article-x-face-command))
14021               ;; The command is a string, so we interpret the command
14022               ;; as a, well, command, and fork it off.
14023               (let ((process-connection-type nil))
14024                 (process-kill-without-query
14025                  (start-process
14026                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
14027                 (process-send-region "gnus-x-face" beg end)
14028                 (process-send-eof "gnus-x-face")))))))))
14029
14030 (defalias 'gnus-header-decode-quoted-printable 'gnus-decode-rfc1522)
14031 (defun gnus-decode-rfc1522 ()
14032   "Hack to remove QP encoding from headers."
14033   (let ((case-fold-search t)
14034         (inhibit-point-motion-hooks t)
14035         (buffer-read-only nil)
14036         string)
14037     (save-restriction
14038       (narrow-to-region
14039        (goto-char (point-min))
14040        (or (search-forward "\n\n" nil t) (point-max)))
14041
14042       (while (re-search-forward 
14043               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
14044         (setq string (match-string 1))
14045         (narrow-to-region (match-beginning 0) (match-end 0))
14046         (delete-region (point-min) (point-max))
14047         (insert string)
14048         (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
14049         (subst-char-in-region (point-min) (point-max) ?_ ? )
14050         (widen)
14051         (goto-char (point-min))))))
14052
14053 (defun gnus-article-de-quoted-unreadable (&optional force)
14054   "Do a naive translation of a quoted-printable-encoded article.
14055 This is in no way, shape or form meant as a replacement for real MIME
14056 processing, but is simply a stop-gap measure until MIME support is
14057 written.
14058 If FORCE, decode the article whether it is marked as quoted-printable
14059 or not."
14060   (interactive (list 'force))
14061   (save-excursion
14062     (set-buffer gnus-article-buffer)
14063     (let ((case-fold-search t)
14064           (buffer-read-only nil)
14065           (type (gnus-fetch-field "content-transfer-encoding")))
14066       (gnus-decode-rfc1522)
14067       (when (or force
14068                 (and type (string-match "quoted-printable" (downcase type))))
14069         (goto-char (point-min))
14070         (search-forward "\n\n" nil 'move)
14071         (gnus-mime-decode-quoted-printable (point) (point-max))))))
14072
14073 (defun gnus-mime-decode-quoted-printable (from to)
14074   "Decode Quoted-Printable in the region between FROM and TO."
14075   (interactive "r")
14076   (goto-char from)
14077   (while (search-forward "=" to t)
14078     (cond ((eq (following-char) ?\n)
14079            (delete-char -1)
14080            (delete-char 1))
14081           ((looking-at "[0-9A-F][0-9A-F]")
14082            (subst-char-in-region
14083             (1- (point)) (point) ?=
14084             (hexl-hex-string-to-integer
14085              (buffer-substring (point) (+ 2 (point)))))
14086            (delete-char 2))
14087           ((looking-at "=")
14088            (delete-char 1))
14089           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
14090
14091 (defun gnus-article-hide-pgp (&optional arg)
14092   "Toggle hiding of any PGP headers and signatures in the current article.
14093 If given a negative prefix, always show; if given a positive prefix,
14094 always hide."
14095   (interactive "P")
14096   (unless (gnus-article-check-hidden-text 'pgp arg)
14097     (save-excursion
14098       (set-buffer gnus-article-buffer)
14099       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
14100             buffer-read-only beg end)
14101         (widen)
14102         (goto-char (point-min))
14103         ;; Hide the "header".
14104         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
14105              (gnus-hide-text (match-beginning 0) (match-end 0) props))
14106         (setq beg (point))
14107         ;; Hide the actual signature.
14108         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
14109              (setq end (1+ (match-beginning 0)))
14110              (gnus-hide-text
14111               end
14112               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
14113                   (match-end 0)
14114                 ;; Perhaps we shouldn't hide to the end of the buffer
14115                 ;; if there is no end to the signature?
14116                 (point-max))
14117               props))
14118         ;; Hide "- " PGP quotation markers.
14119         (when (and beg end)
14120           (narrow-to-region beg end)
14121           (goto-char (point-min))
14122           (while (re-search-forward "^- " nil t)
14123             (gnus-hide-text (match-beginning 0) (match-end 0) props))
14124           (widen))))))
14125
14126 (defun gnus-article-hide-signature (&optional arg)
14127   "Hide the signature in the current article.
14128 If given a negative prefix, always show; if given a positive prefix,
14129 always hide."
14130   (interactive "P")
14131   (unless (gnus-article-check-hidden-text 'signature arg)
14132     (save-excursion
14133       (set-buffer gnus-article-buffer)
14134       (save-restriction
14135         (let ((buffer-read-only nil))
14136           (when (gnus-narrow-to-signature)
14137             (gnus-hide-text-type (point-min) (point-max) 'signature)))))))
14138
14139 (defun gnus-article-strip-leading-blank-lines ()
14140   "Remove all blank lines from the beginning of the article."
14141   (interactive)
14142   (save-excursion
14143     (set-buffer gnus-article-buffer)
14144     (let (buffer-read-only)
14145       (goto-char (point-min))
14146       (when (search-forward "\n\n" nil t)
14147         (while (looking-at "[ \t]$")
14148           (gnus-delete-line))))))
14149
14150 (defvar mime::preview/content-list)
14151 (defvar mime::preview-content-info/point-min)
14152 (defun gnus-narrow-to-signature ()
14153   "Narrow to the signature."
14154   (widen)
14155   (if (and (boundp 'mime::preview/content-list)
14156            mime::preview/content-list)
14157       (let ((pcinfo (car (last mime::preview/content-list))))
14158         (narrow-to-region
14159          (funcall (intern "mime::preview-content-info/point-min") pcinfo)
14160          (point-max))
14161         t)
14162     (goto-char (point-max))
14163     (when (re-search-backward gnus-signature-separator nil t)
14164       (forward-line 1)
14165       (when (or (null gnus-signature-limit)
14166                 (and (numberp gnus-signature-limit)
14167                      (< (- (point-max) (point)) gnus-signature-limit))
14168                 (and (gnus-functionp gnus-signature-limit)
14169                      (funcall gnus-signature-limit))
14170                 (and (stringp gnus-signature-limit)
14171                      (not (re-search-forward gnus-signature-limit nil t))))
14172         (narrow-to-region (point) (point-max))
14173         t))))
14174
14175 (defun gnus-article-check-hidden-text (type arg)
14176   "Return nil if hiding is necessary."
14177   (save-excursion
14178     (set-buffer gnus-article-buffer)
14179     (let ((hide (gnus-article-hidden-text-p type)))
14180       (cond ((or (and (null arg) (eq hide 'hidden))
14181                  (and arg (< 0 (prefix-numeric-value arg))))
14182              (gnus-article-show-hidden-text type))
14183             ((eq hide 'shown)
14184              (gnus-article-show-hidden-text type t))
14185             (t nil)))))
14186
14187 (defun gnus-article-hidden-text-p (type)
14188   "Say whether the current buffer contains hidden text of type TYPE."
14189   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type)))
14190     (when pos
14191       (if (get-text-property pos 'invisible)
14192           'hidden
14193         'shown))))
14194
14195 (defun gnus-article-hide (&optional arg force)
14196   "Hide all the gruft in the current article.
14197 This means that PGP stuff, signatures, cited text and (some)
14198 headers will be hidden.
14199 If given a prefix, show the hidden text instead."
14200   (interactive (list current-prefix-arg 'force))
14201   (gnus-article-hide-headers arg)
14202   (gnus-article-hide-pgp arg)
14203   (gnus-article-hide-citation-maybe arg force)
14204   (gnus-article-hide-signature arg))
14205
14206 (defun gnus-article-show-hidden-text (type &optional hide)
14207   "Show all hidden text of type TYPE.
14208 If HIDE, hide the text instead."
14209   (save-excursion
14210     (set-buffer gnus-article-buffer)
14211     (let ((buffer-read-only nil)
14212           (inhibit-point-motion-hooks t)
14213           (beg (point-min)))
14214       (while (gnus-goto-char (text-property-any
14215                               beg (point-max) 'gnus-type type))
14216         (setq beg (point))
14217         (forward-char)
14218         (if hide
14219             (gnus-hide-text beg (point) gnus-hidden-properties)
14220           (gnus-unhide-text beg (point)))
14221         (setq beg (point)))
14222       t)))
14223
14224 (defvar gnus-article-time-units
14225   `((year . ,(* 365.25 24 60 60))
14226     (week . ,(* 7 24 60 60))
14227     (day . ,(* 24 60 60))
14228     (hour . ,(* 60 60))
14229     (minute . 60)
14230     (second . 1))
14231   "Mapping from time units to seconds.")
14232
14233 (defun gnus-article-date-ut (&optional type highlight)
14234   "Convert DATE date to universal time in the current article.
14235 If TYPE is `local', convert to local time; if it is `lapsed', output
14236 how much time has lapsed since DATE."
14237   (interactive (list 'ut t))
14238   (let* ((header (or gnus-current-headers
14239                      (gnus-summary-article-header) ""))
14240          (date (and (vectorp header) (mail-header-date header)))
14241          (date-regexp "^Date: \\|^X-Sent: ")
14242          (now (current-time))
14243          (inhibit-point-motion-hooks t)
14244          bface eface)
14245     (when (and date (not (string= date "")))
14246       (save-excursion
14247         (set-buffer gnus-article-buffer)
14248         (save-restriction
14249           (nnheader-narrow-to-headers)
14250           (let ((buffer-read-only nil))
14251             ;; Delete any old Date headers.
14252             (if (re-search-forward date-regexp nil t)
14253                 (progn
14254                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
14255                         eface (get-text-property (1- (gnus-point-at-eol))
14256                                                  'face))
14257                   (message-remove-header date-regexp t)
14258                   (beginning-of-line))
14259               (goto-char (point-max)))
14260             (insert (gnus-make-date-line date type))
14261             ;; Do highlighting.
14262             (forward-line -1)
14263             (when (and (gnus-visual-p 'article-highlight 'highlight)
14264                        (looking-at "\\([^:]+\\): *\\(.*\\)$"))
14265               (gnus-put-text-property (match-beginning 1) (match-end 1)
14266                                  'face bface)
14267               (gnus-put-text-property (match-beginning 2) (match-end 2)
14268                                  'face eface))))))))
14269
14270 (defun gnus-make-date-line (date type)
14271   "Return a DATE line of TYPE."
14272   (cond
14273    ;; Convert to the local timezone.  We have to slap a
14274    ;; `condition-case' round the calls to the timezone
14275    ;; functions since they aren't particularly resistant to
14276    ;; buggy dates.
14277    ((eq type 'local)
14278     (concat "Date: " (condition-case ()
14279                          (timezone-make-date-arpa-standard date)
14280                        (error date))
14281             "\n"))
14282    ;; Convert to Universal Time.
14283    ((eq type 'ut)
14284     (concat "Date: "
14285             (condition-case ()
14286                 (timezone-make-date-arpa-standard date nil "UT")
14287               (error date))
14288             "\n"))
14289    ;; Get the original date from the article.
14290    ((eq type 'original)
14291     (concat "Date: " date "\n"))
14292    ;; Do an X-Sent lapsed format.
14293    ((eq type 'lapsed)
14294     ;; If the date is seriously mangled, the timezone
14295     ;; functions are liable to bug out, so we condition-case
14296     ;; the entire thing.
14297     (let* ((now (current-time))
14298            (real-time
14299             (condition-case ()
14300                 (gnus-time-minus
14301                  (gnus-encode-date
14302                   (timezone-make-date-arpa-standard
14303                    (current-time-string now)
14304                    (current-time-zone now) "UT"))
14305                  (gnus-encode-date
14306                   (timezone-make-date-arpa-standard
14307                    date nil "UT")))
14308               (error '(0 0))))
14309            (real-sec (+ (* (float (car real-time)) 65536)
14310                         (cadr real-time)))
14311            (sec (abs real-sec))
14312            num prev)
14313       (cond
14314        ((equal real-time '(0 0))
14315         "X-Sent: Unknown\n")
14316        ((zerop sec)
14317         "X-Sent: Now\n")
14318        (t
14319         (concat
14320          "X-Sent: "
14321          ;; This is a bit convoluted, but basically we go
14322          ;; through the time units for years, weeks, etc,
14323          ;; and divide things to see whether that results
14324          ;; in positive answers.
14325          (mapconcat
14326           (lambda (unit)
14327             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
14328                 ;; The (remaining) seconds are too few to
14329                 ;; be divided into this time unit.
14330                 ""
14331               ;; It's big enough, so we output it.
14332               (setq sec (- sec (* num (cdr unit))))
14333               (prog1
14334                   (concat (if prev ", " "") (int-to-string
14335                                              (floor num))
14336                           " " (symbol-name (car unit))
14337                           (if (> num 1) "s" ""))
14338                 (setq prev t))))
14339           gnus-article-time-units "")
14340          ;; If dates are odd, then it might appear like the
14341          ;; article was sent in the future.
14342          (if (> real-sec 0)
14343              " ago\n"
14344            " in the future\n"))))))
14345    (t
14346     (error "Unknown conversion type: %s" type))))
14347
14348 (defun gnus-article-date-local (&optional highlight)
14349   "Convert the current article date to the local timezone."
14350   (interactive (list t))
14351   (gnus-article-date-ut 'local highlight))
14352
14353 (defun gnus-article-date-original (&optional highlight)
14354   "Convert the current article date to what it was originally.
14355 This is only useful if you have used some other date conversion
14356 function and want to see what the date was before converting."
14357   (interactive (list t))
14358   (gnus-article-date-ut 'original highlight))
14359
14360 (defun gnus-article-date-lapsed (&optional highlight)
14361   "Convert the current article date to time lapsed since it was sent."
14362   (interactive (list t))
14363   (gnus-article-date-ut 'lapsed highlight))
14364
14365 (defun gnus-article-maybe-highlight ()
14366   "Do some article highlighting if `gnus-visual' is non-nil."
14367   (if (gnus-visual-p 'article-highlight 'highlight)
14368       (gnus-article-highlight-some)))
14369
14370 ;; Article savers.
14371
14372 (defun gnus-output-to-rmail (file-name)
14373   "Append the current article to an Rmail file named FILE-NAME."
14374   (require 'rmail)
14375   ;; Most of these codes are borrowed from rmailout.el.
14376   (setq file-name (expand-file-name file-name))
14377   (setq rmail-default-rmail-file file-name)
14378   (let ((artbuf (current-buffer))
14379         (tmpbuf (get-buffer-create " *Gnus-output*")))
14380     (save-excursion
14381       (or (get-file-buffer file-name)
14382           (file-exists-p file-name)
14383           (if (gnus-yes-or-no-p
14384                (concat "\"" file-name "\" does not exist, create it? "))
14385               (let ((file-buffer (create-file-buffer file-name)))
14386                 (save-excursion
14387                   (set-buffer file-buffer)
14388                   (rmail-insert-rmail-file-header)
14389                   (let ((require-final-newline nil))
14390                     (write-region (point-min) (point-max) file-name t 1)))
14391                 (kill-buffer file-buffer))
14392             (error "Output file does not exist")))
14393       (set-buffer tmpbuf)
14394       (buffer-disable-undo (current-buffer))
14395       (erase-buffer)
14396       (insert-buffer-substring artbuf)
14397       (gnus-convert-article-to-rmail)
14398       ;; Decide whether to append to a file or to an Emacs buffer.
14399       (let ((outbuf (get-file-buffer file-name)))
14400         (if (not outbuf)
14401             (append-to-file (point-min) (point-max) file-name)
14402           ;; File has been visited, in buffer OUTBUF.
14403           (set-buffer outbuf)
14404           (let ((buffer-read-only nil)
14405                 (msg (and (boundp 'rmail-current-message)
14406                           (symbol-value 'rmail-current-message))))
14407             ;; If MSG is non-nil, buffer is in RMAIL mode.
14408             (if msg
14409                 (progn (widen)
14410                        (narrow-to-region (point-max) (point-max))))
14411             (insert-buffer-substring tmpbuf)
14412             (if msg
14413                 (progn
14414                   (goto-char (point-min))
14415                   (widen)
14416                   (search-backward "\^_")
14417                   (narrow-to-region (point) (point-max))
14418                   (goto-char (1+ (point-min)))
14419                   (rmail-count-new-messages t)
14420                   (rmail-show-message msg)))))))
14421     (kill-buffer tmpbuf)))
14422
14423 (defun gnus-output-to-file (file-name)
14424   "Append the current article to a file named FILE-NAME."
14425   (setq file-name (expand-file-name file-name))
14426   (let ((artbuf (current-buffer))
14427         (tmpbuf (get-buffer-create " *Gnus-output*")))
14428     (save-excursion
14429       (set-buffer tmpbuf)
14430       (buffer-disable-undo (current-buffer))
14431       (erase-buffer)
14432       (insert-buffer-substring artbuf)
14433       ;; Append newline at end of the buffer as separator, and then
14434       ;; save it to file.
14435       (goto-char (point-max))
14436       (insert "\n")
14437       (append-to-file (point-min) (point-max) file-name))
14438     (kill-buffer tmpbuf)))
14439
14440 (defun gnus-convert-article-to-rmail ()
14441   "Convert article in current buffer to Rmail message format."
14442   (let ((buffer-read-only nil))
14443     ;; Convert article directly into Babyl format.
14444     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14445     (goto-char (point-min))
14446     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14447     (while (search-forward "\n\^_" nil t) ;single char
14448       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14449     (goto-char (point-max))
14450     (insert "\^_")))
14451
14452 (defun gnus-narrow-to-page (&optional arg)
14453   "Narrow the article buffer to a page.
14454 If given a numerical ARG, move forward ARG pages."
14455   (interactive "P")
14456   (setq arg (if arg (prefix-numeric-value arg) 0))
14457   (save-excursion
14458     (set-buffer gnus-article-buffer)
14459     (goto-char (point-min))
14460     (widen)
14461     (when (gnus-visual-p 'page-marker)
14462       (let ((buffer-read-only nil))
14463         (gnus-remove-text-with-property 'gnus-prev)
14464         (gnus-remove-text-with-property 'gnus-next)))
14465     (when
14466         (cond ((< arg 0)
14467                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14468               ((> arg 0)
14469                (re-search-forward page-delimiter nil 'move arg)))
14470       (goto-char (match-end 0)))
14471     (narrow-to-region
14472      (point)
14473      (if (re-search-forward page-delimiter nil 'move)
14474          (match-beginning 0)
14475        (point)))
14476     (when (and (gnus-visual-p 'page-marker)
14477                (not (= (point-min) 1)))
14478       (save-excursion
14479         (goto-char (point-min))
14480         (gnus-insert-prev-page-button)))
14481     (when (and (gnus-visual-p 'page-marker)
14482                (not (= (1- (point-max)) (buffer-size))))
14483       (save-excursion
14484         (goto-char (point-max))
14485         (gnus-insert-next-page-button)))))
14486
14487 ;; Article mode commands
14488
14489 (defun gnus-article-goto-next-page ()
14490   "Show the next page of the article."
14491   (interactive)
14492   (when (gnus-article-next-page)
14493     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
14494
14495 (defun gnus-article-goto-prev-page ()
14496   "Show the next page of the article."
14497   (interactive)
14498   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
14499     (gnus-article-prev-page nil)))
14500
14501 (defun gnus-article-next-page (&optional lines)
14502   "Show the next page of the current article.
14503 If end of article, return non-nil.  Otherwise return nil.
14504 Argument LINES specifies lines to be scrolled up."
14505   (interactive "p")
14506   (move-to-window-line -1)
14507   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14508   (if (save-excursion
14509         (end-of-line)
14510         (and (pos-visible-in-window-p)  ;Not continuation line.
14511              (eobp)))
14512       ;; Nothing in this page.
14513       (if (or (not gnus-break-pages)
14514               (save-excursion
14515                 (save-restriction
14516                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14517           t                             ;Nothing more.
14518         (gnus-narrow-to-page 1)         ;Go to next page.
14519         nil)
14520     ;; More in this page.
14521     (condition-case ()
14522         (scroll-up lines)
14523       (end-of-buffer
14524        ;; Long lines may cause an end-of-buffer error.
14525        (goto-char (point-max))))
14526     (move-to-window-line 0)
14527     nil))
14528
14529 (defun gnus-article-prev-page (&optional lines)
14530   "Show previous page of current article.
14531 Argument LINES specifies lines to be scrolled down."
14532   (interactive "p")
14533   (move-to-window-line 0)
14534   (if (and gnus-break-pages
14535            (bobp)
14536            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14537       (progn
14538         (gnus-narrow-to-page -1)        ;Go to previous page.
14539         (goto-char (point-max))
14540         (recenter -1))
14541     (prog1
14542         (condition-case ()
14543             (scroll-down lines)
14544           (error nil))
14545       (move-to-window-line 0))))
14546
14547 (defun gnus-article-refer-article ()
14548   "Read article specified by message-id around point."
14549   (interactive)
14550   (let ((point (point)))
14551     (search-forward ">" nil t)          ;Move point to end of "<....>".
14552     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14553         (let ((message-id (match-string 1)))
14554           (goto-char point)
14555           (set-buffer gnus-summary-buffer)
14556           (gnus-summary-refer-article message-id))
14557       (goto-char (point))
14558       (error "No references around point"))))
14559
14560 (defun gnus-article-show-summary ()
14561   "Reconfigure windows to show summary buffer."
14562   (interactive)
14563   (gnus-configure-windows 'article)
14564   (gnus-summary-goto-subject gnus-current-article))
14565
14566 (defun gnus-article-describe-briefly ()
14567   "Describe article mode commands briefly."
14568   (interactive)
14569   (gnus-message 6
14570                 (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")))
14571
14572 (defun gnus-article-summary-command ()
14573   "Execute the last keystroke in the summary buffer."
14574   (interactive)
14575   (let ((obuf (current-buffer))
14576         (owin (current-window-configuration))
14577         func)
14578     (switch-to-buffer gnus-summary-buffer 'norecord)
14579     (setq func (lookup-key (current-local-map) (this-command-keys)))
14580     (call-interactively func)
14581     (set-buffer obuf)
14582     (set-window-configuration owin)
14583     (set-window-point (get-buffer-window (current-buffer)) (point))))
14584
14585 (defun gnus-article-summary-command-nosave ()
14586   "Execute the last keystroke in the summary buffer."
14587   (interactive)
14588   (let (func)
14589     (pop-to-buffer gnus-summary-buffer 'norecord)
14590     (setq func (lookup-key (current-local-map) (this-command-keys)))
14591     (call-interactively func)))
14592
14593 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14594   "Read a summary buffer key sequence and execute it from the article buffer."
14595   (interactive "P")
14596   (let ((nosaves
14597          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14598            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14599            "=" "^" "\M-^"))
14600         keys)
14601     (save-excursion
14602       (set-buffer gnus-summary-buffer)
14603       (push (or key last-command-event) unread-command-events)
14604       (setq keys (read-key-sequence nil)))
14605     (message "")
14606
14607     (if (member keys nosaves)
14608         (let (func)
14609           (pop-to-buffer gnus-summary-buffer 'norecord)
14610           (if (setq func (lookup-key (current-local-map) keys))
14611               (call-interactively func)
14612             (ding)))
14613       (let ((obuf (current-buffer))
14614             (owin (current-window-configuration))
14615             (opoint (point))
14616             func in-buffer)
14617         (if not-restore-window
14618             (pop-to-buffer gnus-summary-buffer 'norecord)
14619           (switch-to-buffer gnus-summary-buffer 'norecord))
14620         (setq in-buffer (current-buffer))
14621         (if (setq func (lookup-key (current-local-map) keys))
14622             (call-interactively func)
14623           (ding))
14624         (when (eq in-buffer (current-buffer))
14625           (set-buffer obuf)
14626           (unless not-restore-window
14627             (set-window-configuration owin))
14628           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14629
14630 \f
14631 ;;;
14632 ;;; Kill file handling.
14633 ;;;
14634
14635 ;;;###autoload
14636 (defalias 'gnus-batch-kill 'gnus-batch-score)
14637 ;;;###autoload
14638 (defun gnus-batch-score ()
14639   "Run batched scoring.
14640 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14641 Newsgroups is a list of strings in Bnews format.  If you want to score
14642 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14643 score the alt hierarchy, you'd say \"!alt.all\"."
14644   (interactive)
14645   (let* ((yes-and-no
14646           (gnus-newsrc-parse-options
14647            (apply (function concat)
14648                   (mapcar (lambda (g) (concat g " "))
14649                           command-line-args-left))))
14650          (gnus-expert-user t)
14651          (nnmail-spool-file nil)
14652          (gnus-use-dribble-file nil)
14653          (yes (car yes-and-no))
14654          (no (cdr yes-and-no))
14655          group newsrc entry
14656          ;; Disable verbose message.
14657          gnus-novice-user gnus-large-newsgroup)
14658     ;; Eat all arguments.
14659     (setq command-line-args-left nil)
14660     ;; Start Gnus.
14661     (gnus)
14662     ;; Apply kills to specified newsgroups in command line arguments.
14663     (setq newsrc (cdr gnus-newsrc-alist))
14664     (while newsrc
14665       (setq group (caar newsrc))
14666       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14667       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14668                (and (car entry)
14669                     (or (eq (car entry) t)
14670                         (not (zerop (car entry)))))
14671                (if yes (string-match yes group) t)
14672                (or (null no) (not (string-match no group))))
14673           (progn
14674             (gnus-summary-read-group group nil t nil t)
14675             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14676                  (gnus-summary-exit))))
14677       (setq newsrc (cdr newsrc)))
14678     ;; Exit Emacs.
14679     (switch-to-buffer gnus-group-buffer)
14680     (gnus-group-save-newsrc)))
14681
14682 (defun gnus-apply-kill-file ()
14683   "Apply a kill file to the current newsgroup.
14684 Returns the number of articles marked as read."
14685   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14686           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14687       (gnus-apply-kill-file-internal)
14688     0))
14689
14690 (defun gnus-kill-save-kill-buffer ()
14691   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14692     (when (get-file-buffer file)
14693       (save-excursion
14694         (set-buffer (get-file-buffer file))
14695         (and (buffer-modified-p) (save-buffer))
14696         (kill-buffer (current-buffer))))))
14697
14698 (defvar gnus-kill-file-name "KILL"
14699   "Suffix of the kill files.")
14700
14701 (defun gnus-newsgroup-kill-file (newsgroup)
14702   "Return the name of a kill file name for NEWSGROUP.
14703 If NEWSGROUP is nil, return the global kill file name instead."
14704   (cond 
14705    ;; The global KILL file is placed at top of the directory.
14706    ((or (null newsgroup)
14707         (string-equal newsgroup ""))
14708     (expand-file-name gnus-kill-file-name
14709                       gnus-kill-files-directory))
14710    ;; Append ".KILL" to newsgroup name.
14711    ((gnus-use-long-file-name 'not-kill)
14712     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14713                               "." gnus-kill-file-name)
14714                       gnus-kill-files-directory))
14715    ;; Place "KILL" under the hierarchical directory.
14716    (t
14717     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14718                               "/" gnus-kill-file-name)
14719                       gnus-kill-files-directory))))
14720
14721 \f
14722 ;;;
14723 ;;; Dribble file
14724 ;;;
14725
14726 (defvar gnus-dribble-ignore nil)
14727 (defvar gnus-dribble-eval-file nil)
14728
14729 (defun gnus-dribble-file-name ()
14730   "Return the dribble file for the current .newsrc."
14731   (concat
14732    (if gnus-dribble-directory
14733        (concat (file-name-as-directory gnus-dribble-directory)
14734                (file-name-nondirectory gnus-current-startup-file))
14735      gnus-current-startup-file)
14736    "-dribble"))
14737
14738 (defun gnus-dribble-enter (string)
14739   "Enter STRING into the dribble buffer."
14740   (if (and (not gnus-dribble-ignore)
14741            gnus-dribble-buffer
14742            (buffer-name gnus-dribble-buffer))
14743       (let ((obuf (current-buffer)))
14744         (set-buffer gnus-dribble-buffer)
14745         (insert string "\n")
14746         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14747         (set-buffer obuf))))
14748
14749 (defun gnus-dribble-read-file ()
14750   "Read the dribble file from disk."
14751   (let ((dribble-file (gnus-dribble-file-name)))
14752     (save-excursion
14753       (set-buffer (setq gnus-dribble-buffer
14754                         (get-buffer-create
14755                          (file-name-nondirectory dribble-file))))
14756       (gnus-add-current-to-buffer-list)
14757       (erase-buffer)
14758       (setq buffer-file-name dribble-file)
14759       (auto-save-mode t)
14760       (buffer-disable-undo (current-buffer))
14761       (bury-buffer (current-buffer))
14762       (set-buffer-modified-p nil)
14763       (let ((auto (make-auto-save-file-name))
14764             (gnus-dribble-ignore t)
14765             modes)
14766         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14767           ;; Load whichever file is newest -- the auto save file
14768           ;; or the "real" file.
14769           (if (file-newer-than-file-p auto dribble-file)
14770               (insert-file-contents auto)
14771             (insert-file-contents dribble-file))
14772           (unless (zerop (buffer-size))
14773             (set-buffer-modified-p t))
14774           ;; Set the file modes to reflect the .newsrc file modes.
14775           (save-buffer)
14776           (when (and (file-exists-p gnus-current-startup-file)
14777                      (setq modes (file-modes gnus-current-startup-file)))
14778             (set-file-modes dribble-file modes))
14779           ;; Possibly eval the file later.
14780           (when (gnus-y-or-n-p
14781                  "Auto-save file exists.  Do you want to read it? ")
14782             (setq gnus-dribble-eval-file t)))))))
14783
14784 (defun gnus-dribble-eval-file ()
14785   (when gnus-dribble-eval-file
14786     (setq gnus-dribble-eval-file nil)
14787     (save-excursion
14788       (let ((gnus-dribble-ignore t))
14789         (set-buffer gnus-dribble-buffer)
14790         (eval-buffer (current-buffer))))))
14791
14792 (defun gnus-dribble-delete-file ()
14793   (when (file-exists-p (gnus-dribble-file-name))
14794     (delete-file (gnus-dribble-file-name)))
14795   (when gnus-dribble-buffer
14796     (save-excursion
14797       (set-buffer gnus-dribble-buffer)
14798       (let ((auto (make-auto-save-file-name)))
14799         (if (file-exists-p auto)
14800             (delete-file auto))
14801         (erase-buffer)
14802         (set-buffer-modified-p nil)))))
14803
14804 (defun gnus-dribble-save ()
14805   (when (and gnus-dribble-buffer
14806              (buffer-name gnus-dribble-buffer))
14807     (save-excursion
14808       (set-buffer gnus-dribble-buffer)
14809       (save-buffer))))
14810
14811 (defun gnus-dribble-clear ()
14812   (when (gnus-buffer-exists-p gnus-dribble-buffer)
14813     (save-excursion
14814       (set-buffer gnus-dribble-buffer)
14815       (erase-buffer)
14816       (set-buffer-modified-p nil)
14817       (setq buffer-saved-size (buffer-size)))))
14818
14819 \f
14820 ;;;
14821 ;;; Server Communication
14822 ;;;
14823
14824 (defun gnus-start-news-server (&optional confirm)
14825   "Open a method for getting news.
14826 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14827   (let (how)
14828     (if gnus-current-select-method
14829         ;; Stream is already opened.
14830         nil
14831       ;; Open NNTP server.
14832       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14833       (if confirm
14834           (progn
14835             ;; Read server name with completion.
14836             (setq gnus-nntp-server
14837                   (completing-read "NNTP server: "
14838                                    (mapcar (lambda (server) (list server))
14839                                            (cons (list gnus-nntp-server)
14840                                                  gnus-secondary-servers))
14841                                    nil nil gnus-nntp-server))))
14842
14843       (if (and gnus-nntp-server
14844                (stringp gnus-nntp-server)
14845                (not (string= gnus-nntp-server "")))
14846           (setq gnus-select-method
14847                 (cond ((or (string= gnus-nntp-server "")
14848                            (string= gnus-nntp-server "::"))
14849                        (list 'nnspool (system-name)))
14850                       ((string-match "^:" gnus-nntp-server)
14851                        (list 'nnmh gnus-nntp-server
14852                              (list 'nnmh-directory
14853                                    (file-name-as-directory
14854                                     (expand-file-name
14855                                      (concat "~/" (substring
14856                                                    gnus-nntp-server 1)))))
14857                              (list 'nnmh-get-new-mail nil)))
14858                       (t
14859                        (list 'nntp gnus-nntp-server)))))
14860
14861       (setq how (car gnus-select-method))
14862       (cond ((eq how 'nnspool)
14863              (require 'nnspool)
14864              (gnus-message 5 "Looking up local news spool..."))
14865             ((eq how 'nnmh)
14866              (require 'nnmh)
14867              (gnus-message 5 "Looking up mh spool..."))
14868             (t
14869              (require 'nntp)))
14870       (setq gnus-current-select-method gnus-select-method)
14871       (run-hooks 'gnus-open-server-hook)
14872       (or
14873        ;; gnus-open-server-hook might have opened it
14874        (gnus-server-opened gnus-select-method)
14875        (gnus-open-server gnus-select-method)
14876        (gnus-y-or-n-p
14877         (format
14878          "%s (%s) open error: '%s'.     Continue? "
14879          (car gnus-select-method) (cadr gnus-select-method)
14880          (gnus-status-message gnus-select-method)))
14881        (gnus-error 1 "Couldn't open server on %s"
14882                    (nth 1 gnus-select-method))))))
14883
14884 (defun gnus-check-group (group)
14885   "Try to make sure that the server where GROUP exists is alive."
14886   (let ((method (gnus-find-method-for-group group)))
14887     (or (gnus-server-opened method)
14888         (gnus-open-server method))))
14889
14890 (defun gnus-check-server (&optional method silent)
14891   "Check whether the connection to METHOD is down.
14892 If METHOD is nil, use `gnus-select-method'.
14893 If it is down, start it up (again)."
14894   (let ((method (or method gnus-select-method)))
14895     ;; Transform virtual server names into select methods.
14896     (when (stringp method)
14897       (setq method (gnus-server-to-method method)))
14898     (if (gnus-server-opened method)
14899         ;; The stream is already opened.
14900         t
14901       ;; Open the server.
14902       (unless silent
14903         (gnus-message 5 "Opening %s server%s..." (car method)
14904                       (if (equal (nth 1 method) "") ""
14905                         (format " on %s" (nth 1 method)))))
14906       (run-hooks 'gnus-open-server-hook)
14907       (prog1
14908           (gnus-open-server method)
14909         (unless silent
14910           (message ""))))))
14911
14912 (defun gnus-get-function (method function)
14913   "Return a function symbol based on METHOD and FUNCTION."
14914   ;; Translate server names into methods.
14915   (unless method
14916     (error "Attempted use of a nil select method"))
14917   (when (stringp method)
14918     (setq method (gnus-server-to-method method)))
14919   (let ((func (intern (format "%s-%s" (car method) function))))
14920     ;; If the functions isn't bound, we require the backend in
14921     ;; question.
14922     (unless (fboundp func)
14923       (require (car method))
14924       (unless (fboundp func)
14925         ;; This backend doesn't implement this function.
14926         (error "No such function: %s" func)))
14927     func))
14928
14929 \f
14930 ;;;
14931 ;;; Interface functions to the backends.
14932 ;;;
14933
14934 (defun gnus-open-server (method)
14935   "Open a connection to METHOD."
14936   (when (stringp method)
14937     (setq method (gnus-server-to-method method)))
14938   (let ((elem (assoc method gnus-opened-servers)))
14939     ;; If this method was previously denied, we just return nil.
14940     (if (eq (nth 1 elem) 'denied)
14941         (progn
14942           (gnus-message 1 "Denied server")
14943           nil)
14944       ;; Open the server.
14945       (let ((result
14946              (funcall (gnus-get-function method 'open-server)
14947                       (nth 1 method) (nthcdr 2 method))))
14948         ;; If this hasn't been opened before, we add it to the list.
14949         (unless elem
14950           (setq elem (list method nil)
14951                 gnus-opened-servers (cons elem gnus-opened-servers)))
14952         ;; Set the status of this server.
14953         (setcar (cdr elem) (if result 'ok 'denied))
14954         ;; Return the result from the "open" call.
14955         result))))
14956
14957 (defun gnus-close-server (method)
14958   "Close the connection to METHOD."
14959   (when (stringp method)
14960     (setq method (gnus-server-to-method method)))
14961   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14962
14963 (defun gnus-request-list (method)
14964   "Request the active file from METHOD."
14965   (when (stringp method)
14966     (setq method (gnus-server-to-method method)))
14967   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14968
14969 (defun gnus-request-list-newsgroups (method)
14970   "Request the newsgroups file from METHOD."
14971   (when (stringp method)
14972     (setq method (gnus-server-to-method method)))
14973   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14974
14975 (defun gnus-request-newgroups (date method)
14976   "Request all new groups since DATE from METHOD."
14977   (when (stringp method)
14978     (setq method (gnus-server-to-method method)))
14979   (funcall (gnus-get-function method 'request-newgroups)
14980            date (nth 1 method)))
14981
14982 (defun gnus-server-opened (method)
14983   "Check whether a connection to METHOD has been opened."
14984   (when (stringp method)
14985     (setq method (gnus-server-to-method method)))
14986   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14987
14988 (defun gnus-status-message (method)
14989   "Return the status message from METHOD.
14990 If METHOD is a string, it is interpreted as a group name.   The method
14991 this group uses will be queried."
14992   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14993                   method)))
14994     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14995
14996 (defun gnus-request-group (group &optional dont-check method)
14997   "Request GROUP.  If DONT-CHECK, no information is required."
14998   (let ((method (or method (gnus-find-method-for-group group))))
14999     (when (stringp method)
15000       (setq method (gnus-server-to-method method)))
15001     (funcall (gnus-get-function method 'request-group)
15002              (gnus-group-real-name group) (nth 1 method) dont-check)))
15003
15004 (defun gnus-request-asynchronous (group &optional articles)
15005   "Request that GROUP behave asynchronously.
15006 ARTICLES is the `data' of the group."
15007   (let ((method (gnus-find-method-for-group group)))
15008     (funcall (gnus-get-function method 'request-asynchronous)
15009              (gnus-group-real-name group) (nth 1 method) articles)))
15010
15011 (defun gnus-list-active-group (group)
15012   "Request active information on GROUP."
15013   (let ((method (gnus-find-method-for-group group))
15014         (func 'list-active-group))
15015     (when (gnus-check-backend-function func group)
15016       (funcall (gnus-get-function method func)
15017                (gnus-group-real-name group) (nth 1 method)))))
15018
15019 (defun gnus-request-group-description (group)
15020   "Request a description of GROUP."
15021   (let ((method (gnus-find-method-for-group group))
15022         (func 'request-group-description))
15023     (when (gnus-check-backend-function func group)
15024       (funcall (gnus-get-function method func)
15025                (gnus-group-real-name group) (nth 1 method)))))
15026
15027 (defun gnus-close-group (group)
15028   "Request the GROUP be closed."
15029   (let ((method (gnus-find-method-for-group group)))
15030     (funcall (gnus-get-function method 'close-group)
15031              (gnus-group-real-name group) (nth 1 method))))
15032
15033 (defun gnus-retrieve-headers (articles group &optional fetch-old)
15034   "Request headers for ARTICLES in GROUP.
15035 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
15036   (let ((method (gnus-find-method-for-group group)))
15037     (if (and gnus-use-cache (numberp (car articles)))
15038         (gnus-cache-retrieve-headers articles group fetch-old)
15039       (funcall (gnus-get-function method 'retrieve-headers)
15040                articles (gnus-group-real-name group) (nth 1 method)
15041                fetch-old))))
15042
15043 (defun gnus-retrieve-groups (groups method)
15044   "Request active information on GROUPS from METHOD."
15045   (when (stringp method)
15046     (setq method (gnus-server-to-method method)))
15047   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
15048
15049 (defun gnus-request-type (group &optional article)
15050   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15051   (let ((method (gnus-find-method-for-group group)))
15052     (if (not (gnus-check-backend-function 'request-type (car method)))
15053         'unknown
15054       (funcall (gnus-get-function method 'request-type)
15055                (gnus-group-real-name group) article))))
15056
15057 (defun gnus-request-update-mark (group article mark)
15058   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
15059   (let ((method (gnus-find-method-for-group group)))
15060     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
15061         mark
15062       (funcall (gnus-get-function method 'request-update-mark)
15063                (gnus-group-real-name group) article mark))))
15064
15065 (defun gnus-request-article (article group &optional buffer)
15066   "Request the ARTICLE in GROUP.
15067 ARTICLE can either be an article number or an article Message-ID.
15068 If BUFFER, insert the article in that group."
15069   (let ((method (gnus-find-method-for-group group)))
15070     (funcall (gnus-get-function method 'request-article)
15071              article (gnus-group-real-name group) (nth 1 method) buffer)))
15072
15073 (defun gnus-request-head (article group)
15074   "Request the head of ARTICLE in GROUP."
15075   (let ((method (gnus-find-method-for-group group)))
15076     (funcall (gnus-get-function method 'request-head)
15077              article (gnus-group-real-name group) (nth 1 method))))
15078
15079 (defun gnus-request-body (article group)
15080   "Request the body of ARTICLE in GROUP."
15081   (let ((method (gnus-find-method-for-group group)))
15082     (funcall (gnus-get-function method 'request-body)
15083              article (gnus-group-real-name group) (nth 1 method))))
15084
15085 (defun gnus-request-post (method)
15086   "Post the current buffer using METHOD."
15087   (when (stringp method)
15088     (setq method (gnus-server-to-method method)))
15089   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
15090
15091 (defun gnus-request-scan (group method)
15092   "Request a SCAN being performed in GROUP from METHOD.
15093 If GROUP is nil, all groups on METHOD are scanned."
15094   (let ((method (if group (gnus-find-method-for-group group) method)))
15095     (funcall (gnus-get-function method 'request-scan)
15096              (and group (gnus-group-real-name group)) (nth 1 method))))
15097
15098 (defsubst gnus-request-update-info (info method)
15099   "Request that METHOD update INFO."
15100   (when (stringp method)
15101     (setq method (gnus-server-to-method method)))
15102   (when (gnus-check-backend-function 'request-update-info (car method))
15103     (funcall (gnus-get-function method 'request-update-info)
15104              (gnus-group-real-name (gnus-info-group info))
15105              info (nth 1 method))))
15106
15107 (defun gnus-request-expire-articles (articles group &optional force)
15108   (let ((method (gnus-find-method-for-group group)))
15109     (funcall (gnus-get-function method 'request-expire-articles)
15110              articles (gnus-group-real-name group) (nth 1 method)
15111              force)))
15112
15113 (defun gnus-request-move-article
15114   (article group server accept-function &optional last)
15115   (let ((method (gnus-find-method-for-group group)))
15116     (funcall (gnus-get-function method 'request-move-article)
15117              article (gnus-group-real-name group)
15118              (nth 1 method) accept-function last)))
15119
15120 (defun gnus-request-accept-article (group method &optional last)
15121   ;; Make sure there's a newline at the end of the article.
15122   (when (stringp method)
15123     (setq method (gnus-server-to-method method)))
15124   (when (and (not method)
15125              (stringp group))
15126     (setq method (gnus-find-method-for-group group)))
15127   (goto-char (point-max))
15128   (unless (bolp)
15129     (insert "\n"))
15130   (let ((func (car (or method (gnus-find-method-for-group group)))))
15131     (funcall (intern (format "%s-request-accept-article" func))
15132              (if (stringp group) (gnus-group-real-name group) group)
15133              (cadr method)
15134              last)))
15135
15136 (defun gnus-request-replace-article (article group buffer)
15137   (let ((func (car (gnus-find-method-for-group group))))
15138     (funcall (intern (format "%s-request-replace-article" func))
15139              article (gnus-group-real-name group) buffer)))
15140
15141 (defun gnus-request-associate-buffer (group)
15142   (let ((method (gnus-find-method-for-group group)))
15143     (funcall (gnus-get-function method 'request-associate-buffer)
15144              (gnus-group-real-name group))))
15145
15146 (defun gnus-request-restore-buffer (article group)
15147   "Request a new buffer restored to the state of ARTICLE."
15148   (let ((method (gnus-find-method-for-group group)))
15149     (funcall (gnus-get-function method 'request-restore-buffer)
15150              article (gnus-group-real-name group) (nth 1 method))))
15151
15152 (defun gnus-request-create-group (group &optional method)
15153   (when (stringp method)
15154     (setq method (gnus-server-to-method method)))
15155   (let ((method (or method (gnus-find-method-for-group group))))
15156     (funcall (gnus-get-function method 'request-create-group)
15157              (gnus-group-real-name group) (nth 1 method))))
15158
15159 (defun gnus-request-delete-group (group &optional force)
15160   (let ((method (gnus-find-method-for-group group)))
15161     (funcall (gnus-get-function method 'request-delete-group)
15162              (gnus-group-real-name group) force (nth 1 method))))
15163
15164 (defun gnus-request-rename-group (group new-name)
15165   (let ((method (gnus-find-method-for-group group)))
15166     (funcall (gnus-get-function method 'request-rename-group)
15167              (gnus-group-real-name group)
15168              (gnus-group-real-name new-name) (nth 1 method))))
15169
15170 (defun gnus-member-of-valid (symbol group)
15171   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
15172   (memq symbol (assoc
15173                 (symbol-name (car (gnus-find-method-for-group group)))
15174                 gnus-valid-select-methods)))
15175
15176 (defun gnus-method-option-p (method option)
15177   "Return non-nil if select METHOD has OPTION as a parameter."
15178   (when (stringp method)
15179     (setq method (gnus-server-to-method method)))
15180   (memq option (assoc (format "%s" (car method))
15181                       gnus-valid-select-methods)))
15182
15183 (defun gnus-server-extend-method (group method)
15184   ;; This function "extends" a virtual server.  If the server is
15185   ;; "hello", and the select method is ("hello" (my-var "something"))
15186   ;; in the group "alt.alt", this will result in a new virtual server
15187   ;; called "hello+alt.alt".
15188   (let ((entry
15189          (gnus-copy-sequence
15190           (if (equal (car method) "native") gnus-select-method
15191             (cdr (assoc (car method) gnus-server-alist))))))
15192     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
15193     (nconc entry (cdr method))))
15194
15195 (defun gnus-find-method-for-group (group &optional info)
15196   "Find the select method that GROUP uses."
15197   (or gnus-override-method
15198       (and (not group)
15199            gnus-select-method)
15200       (let ((info (or info (gnus-get-info group)))
15201             method)
15202         (if (or (not info)
15203                 (not (setq method (gnus-info-method info)))
15204                 (equal method "native"))
15205             gnus-select-method
15206           (setq method
15207                 (cond ((stringp method)
15208                        (gnus-server-to-method method))
15209                       ((stringp (car method))
15210                        (gnus-server-extend-method group method))
15211                       (t
15212                        method)))
15213           (cond ((equal (cadr method) "")
15214                  method)
15215                 ((null (cadr method))
15216                  (list (car method) ""))
15217                 (t
15218                  (gnus-server-add-address method)))))))
15219
15220 (defun gnus-check-backend-function (func group)
15221   "Check whether GROUP supports function FUNC."
15222   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
15223                   group)))
15224     (fboundp (intern (format "%s-%s" method func)))))
15225
15226 (defun gnus-methods-using (feature)
15227   "Find all methods that have FEATURE."
15228   (let ((valids gnus-valid-select-methods)
15229         outs)
15230     (while valids
15231       (if (memq feature (car valids))
15232           (setq outs (cons (car valids) outs)))
15233       (setq valids (cdr valids)))
15234     outs))
15235
15236 \f
15237 ;;;
15238 ;;; Active & Newsrc File Handling
15239 ;;;
15240
15241 (defun gnus-setup-news (&optional rawfile level dont-connect)
15242   "Setup news information.
15243 If RAWFILE is non-nil, the .newsrc file will also be read.
15244 If LEVEL is non-nil, the news will be set up at level LEVEL."
15245   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
15246
15247     (when init 
15248       ;; Clear some variables to re-initialize news information.
15249       (setq gnus-newsrc-alist nil
15250             gnus-active-hashtb nil)
15251       ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
15252       (gnus-read-newsrc-file rawfile))
15253
15254     (when (and (not (assoc "archive" gnus-server-alist))
15255                gnus-message-archive-method)
15256       (push (cons "archive" gnus-message-archive-method)
15257             gnus-server-alist))
15258
15259     ;; If we don't read the complete active file, we fill in the
15260     ;; hashtb here.
15261     (if (or (null gnus-read-active-file)
15262             (eq gnus-read-active-file 'some))
15263         (gnus-update-active-hashtb-from-killed))
15264
15265     ;; Read the active file and create `gnus-active-hashtb'.
15266     ;; If `gnus-read-active-file' is nil, then we just create an empty
15267     ;; hash table.  The partial filling out of the hash table will be
15268     ;; done in `gnus-get-unread-articles'.
15269     (and gnus-read-active-file
15270          (not level)
15271          (gnus-read-active-file))
15272
15273     (or gnus-active-hashtb
15274         (setq gnus-active-hashtb (make-vector 4095 0)))
15275
15276     ;; Initialize the cache.
15277     (when gnus-use-cache
15278       (gnus-cache-open))
15279
15280     ;; Possibly eval the dribble file.
15281     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
15282
15283     ;; Slave Gnusii should then clear the dribble buffer.
15284     (when (and init gnus-slave)
15285       (gnus-dribble-clear))
15286
15287     (gnus-update-format-specifications)
15288
15289     ;; See whether we need to read the description file.
15290     (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
15291              (not gnus-description-hashtb)
15292              (not dont-connect)
15293              gnus-read-active-file)
15294         (gnus-read-all-descriptions-files))
15295
15296     ;; Find new newsgroups and treat them.
15297     (if (and init gnus-check-new-newsgroups (not level)
15298              (gnus-check-server gnus-select-method))
15299         (gnus-find-new-newsgroups))
15300
15301     ;; We might read in new NoCeM messages here.
15302     (when (and gnus-use-nocem 
15303                (not level)
15304                (not dont-connect))
15305       (gnus-nocem-scan-groups))
15306
15307     ;; Find the number of unread articles in each non-dead group.
15308     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
15309       (gnus-get-unread-articles level))
15310
15311     (if (and init gnus-check-bogus-newsgroups
15312              gnus-read-active-file (not level)
15313              (gnus-server-opened gnus-select-method))
15314         (gnus-check-bogus-newsgroups))))
15315
15316 (defun gnus-find-new-newsgroups (&optional arg)
15317   "Search for new newsgroups and add them.
15318 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
15319 The `-n' option line from .newsrc is respected.
15320 If ARG (the prefix), use the `ask-server' method to query
15321 the server for new groups."
15322   (interactive "P")
15323   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
15324                        (null gnus-read-active-file)
15325                        (eq gnus-read-active-file 'some))
15326                    'ask-server gnus-check-new-newsgroups)))
15327     (unless (gnus-check-first-time-used)
15328       (if (or (consp check)
15329               (eq check 'ask-server))
15330           ;; Ask the server for new groups.
15331           (gnus-ask-server-for-new-groups)
15332         ;; Go through the active hashtb and look for new groups.
15333         (let ((groups 0)
15334               group new-newsgroups)
15335           (gnus-message 5 "Looking for new newsgroups...")
15336           (unless gnus-have-read-active-file
15337             (gnus-read-active-file))
15338           (setq gnus-newsrc-last-checked-date (current-time-string))
15339           (unless gnus-killed-hashtb
15340             (gnus-make-hashtable-from-killed))
15341           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
15342           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
15343           (mapatoms
15344            (lambda (sym)
15345              (if (or (null (setq group (symbol-name sym)))
15346                      (not (boundp sym))
15347                      (null (symbol-value sym))
15348                      (gnus-gethash group gnus-killed-hashtb)
15349                      (gnus-gethash group gnus-newsrc-hashtb))
15350                  ()
15351                (let ((do-sub (gnus-matches-options-n group)))
15352                  (cond
15353                   ((eq do-sub 'subscribe)
15354                    (setq groups (1+ groups))
15355                    (gnus-sethash group group gnus-killed-hashtb)
15356                    (funcall gnus-subscribe-options-newsgroup-method group))
15357                   ((eq do-sub 'ignore)
15358                    nil)
15359                   (t
15360                    (setq groups (1+ groups))
15361                    (gnus-sethash group group gnus-killed-hashtb)
15362                    (if gnus-subscribe-hierarchical-interactive
15363                        (setq new-newsgroups (cons group new-newsgroups))
15364                      (funcall gnus-subscribe-newsgroup-method group)))))))
15365            gnus-active-hashtb)
15366           (when new-newsgroups
15367             (gnus-subscribe-hierarchical-interactive new-newsgroups))
15368           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15369           (if (> groups 0)
15370               (gnus-message 6 "%d new newsgroup%s arrived."
15371                             groups (if (> groups 1) "s have" " has"))
15372             (gnus-message 6 "No new newsgroups.")))))))
15373
15374 (defun gnus-matches-options-n (group)
15375   ;; Returns `subscribe' if the group is to be unconditionally
15376   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
15377   ;; no match for the group.
15378
15379   ;; First we check the two user variables.
15380   (cond
15381    ((and gnus-options-subscribe
15382          (string-match gnus-options-subscribe group))
15383     'subscribe)
15384    ((and gnus-auto-subscribed-groups
15385          (string-match gnus-auto-subscribed-groups group))
15386     'subscribe)
15387    ((and gnus-options-not-subscribe
15388          (string-match gnus-options-not-subscribe group))
15389     'ignore)
15390    ;; Then we go through the list that was retrieved from the .newsrc
15391    ;; file.  This list has elements on the form
15392    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
15393    ;; is in the reverse order of the options line) is returned.
15394    (t
15395     (let ((regs gnus-newsrc-options-n))
15396       (while (and regs
15397                   (not (string-match (caar regs) group)))
15398         (setq regs (cdr regs)))
15399       (and regs (cdar regs))))))
15400
15401 (defun gnus-ask-server-for-new-groups ()
15402   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
15403          (methods (cons gnus-select-method
15404                         (nconc
15405                          (when gnus-message-archive-method
15406                            (list "archive"))
15407                          (append
15408                           (and (consp gnus-check-new-newsgroups)
15409                                gnus-check-new-newsgroups)
15410                           gnus-secondary-select-methods))))
15411          (groups 0)
15412          (new-date (current-time-string))
15413          group new-newsgroups got-new method hashtb
15414          gnus-override-subscribe-method)
15415     ;; Go through both primary and secondary select methods and
15416     ;; request new newsgroups.
15417     (while (setq method (gnus-server-get-method nil (pop methods)))
15418       (setq new-newsgroups nil)
15419       (setq gnus-override-subscribe-method method)
15420       (when (and (gnus-check-server method)
15421                  (gnus-request-newgroups date method))
15422         (save-excursion
15423           (setq got-new t)
15424           (setq hashtb (gnus-make-hashtable 100))
15425           (set-buffer nntp-server-buffer)
15426           ;; Enter all the new groups into a hashtable.
15427           (gnus-active-to-gnus-format method hashtb 'ignore))
15428         ;; Now all new groups from `method' are in `hashtb'.
15429         (mapatoms
15430          (lambda (group-sym)
15431            (if (or (null (setq group (symbol-name group-sym)))
15432                    (not (boundp group-sym))
15433                    (null (symbol-value group-sym))
15434                    (gnus-gethash group gnus-newsrc-hashtb)
15435                    (member group gnus-zombie-list)
15436                    (member group gnus-killed-list))
15437                ;; The group is already known.
15438                ()
15439              ;; Make this group active.
15440              (when (symbol-value group-sym)
15441                (gnus-set-active group (symbol-value group-sym)))
15442              ;; Check whether we want it or not.
15443              (let ((do-sub (gnus-matches-options-n group)))
15444                (cond
15445                 ((eq do-sub 'subscribe)
15446                  (incf groups)
15447                  (gnus-sethash group group gnus-killed-hashtb)
15448                  (funcall gnus-subscribe-options-newsgroup-method group))
15449                 ((eq do-sub 'ignore)
15450                  nil)
15451                 (t
15452                  (incf groups)
15453                  (gnus-sethash group group gnus-killed-hashtb)
15454                  (if gnus-subscribe-hierarchical-interactive
15455                      (push group new-newsgroups)
15456                    (funcall gnus-subscribe-newsgroup-method group)))))))
15457          hashtb))
15458       (when new-newsgroups
15459         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15460     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15461     (when (> groups 0)
15462       (gnus-message 6 "%d new newsgroup%s arrived."
15463                     groups (if (> groups 1) "s have" " has")))
15464     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15465     got-new))
15466
15467 (defun gnus-check-first-time-used ()
15468   (if (or (> (length gnus-newsrc-alist) 1)
15469           (file-exists-p gnus-startup-file)
15470           (file-exists-p (concat gnus-startup-file ".el"))
15471           (file-exists-p (concat gnus-startup-file ".eld")))
15472       nil
15473     (gnus-message 6 "First time user; subscribing you to default groups")
15474     (unless (gnus-read-active-file-p)
15475       (gnus-read-active-file))
15476     (setq gnus-newsrc-last-checked-date (current-time-string))
15477     (let ((groups gnus-default-subscribed-newsgroups)
15478           group)
15479       (if (eq groups t)
15480           nil
15481         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15482         (mapatoms
15483          (lambda (sym)
15484            (if (null (setq group (symbol-name sym)))
15485                ()
15486              (let ((do-sub (gnus-matches-options-n group)))
15487                (cond
15488                 ((eq do-sub 'subscribe)
15489                  (gnus-sethash group group gnus-killed-hashtb)
15490                  (funcall gnus-subscribe-options-newsgroup-method group))
15491                 ((eq do-sub 'ignore)
15492                  nil)
15493                 (t
15494                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15495          gnus-active-hashtb)
15496         (while groups
15497           (if (gnus-active (car groups))
15498               (gnus-group-change-level
15499                (car groups) gnus-level-default-subscribed gnus-level-killed))
15500           (setq groups (cdr groups)))
15501         (gnus-group-make-help-group)
15502         (and gnus-novice-user
15503              (gnus-message 7 "`A k' to list killed groups"))))))
15504
15505 (defun gnus-subscribe-group (group previous &optional method)
15506   (gnus-group-change-level
15507    (if method
15508        (list t group gnus-level-default-subscribed nil nil method)
15509      group)
15510    gnus-level-default-subscribed gnus-level-killed previous t))
15511
15512 ;; `gnus-group-change-level' is the fundamental function for changing
15513 ;; subscription levels of newsgroups.  This might mean just changing
15514 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15515 ;; again, which subscribes/unsubscribes a group, which is equally
15516 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15517 ;; from 8-9 to 1-7 means that you remove the group from the list of
15518 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15519 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15520 ;; which is trivial.
15521 ;; ENTRY can either be a string (newsgroup name) or a list (if
15522 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15523 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15524 ;; entries.
15525 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15526 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15527 ;; after.
15528 (defun gnus-group-change-level (entry level &optional oldlevel
15529                                       previous fromkilled)
15530   (let (group info active num)
15531     ;; Glean what info we can from the arguments
15532     (if (consp entry)
15533         (if fromkilled (setq group (nth 1 entry))
15534           (setq group (car (nth 2 entry))))
15535       (setq group entry))
15536     (if (and (stringp entry)
15537              oldlevel
15538              (< oldlevel gnus-level-zombie))
15539         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15540     (if (and (not oldlevel)
15541              (consp entry))
15542         (setq oldlevel (gnus-info-level (nth 2 entry)))
15543       (setq oldlevel (or oldlevel 9)))
15544     (if (stringp previous)
15545         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15546
15547     (if (and (>= oldlevel gnus-level-zombie)
15548              (gnus-gethash group gnus-newsrc-hashtb))
15549         ;; We are trying to subscribe a group that is already
15550         ;; subscribed.
15551         ()                              ; Do nothing.
15552
15553       (or (gnus-ephemeral-group-p group)
15554           (gnus-dribble-enter
15555            (format "(gnus-group-change-level %S %S %S %S %S)"
15556                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15557
15558       ;; Then we remove the newgroup from any old structures, if needed.
15559       ;; If the group was killed, we remove it from the killed or zombie
15560       ;; list.  If not, and it is in fact going to be killed, we remove
15561       ;; it from the newsrc hash table and assoc.
15562       (cond
15563        ((>= oldlevel gnus-level-zombie)
15564         (if (= oldlevel gnus-level-zombie)
15565             (setq gnus-zombie-list (delete group gnus-zombie-list))
15566           (setq gnus-killed-list (delete group gnus-killed-list))))
15567        (t
15568         (if (and (>= level gnus-level-zombie)
15569                  entry)
15570             (progn
15571               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15572               (if (nth 3 entry)
15573                   (setcdr (gnus-gethash (car (nth 3 entry))
15574                                         gnus-newsrc-hashtb)
15575                           (cdr entry)))
15576               (setcdr (cdr entry) (cdddr entry))))))
15577
15578       ;; Finally we enter (if needed) the list where it is supposed to
15579       ;; go, and change the subscription level.  If it is to be killed,
15580       ;; we enter it into the killed or zombie list.
15581       (cond 
15582        ((>= level gnus-level-zombie)
15583         ;; Remove from the hash table.
15584         (gnus-sethash group nil gnus-newsrc-hashtb)
15585         ;; We do not enter foreign groups into the list of dead
15586         ;; groups.
15587         (unless (gnus-group-foreign-p group)
15588           (if (= level gnus-level-zombie)
15589               (setq gnus-zombie-list (cons group gnus-zombie-list))
15590             (setq gnus-killed-list (cons group gnus-killed-list)))))
15591        (t
15592         ;; If the list is to be entered into the newsrc assoc, and
15593         ;; it was killed, we have to create an entry in the newsrc
15594         ;; hashtb format and fix the pointers in the newsrc assoc.
15595         (if (< oldlevel gnus-level-zombie)
15596             ;; It was alive, and it is going to stay alive, so we
15597             ;; just change the level and don't change any pointers or
15598             ;; hash table entries.
15599             (setcar (cdaddr entry) level)
15600           (if (listp entry)
15601               (setq info (cdr entry)
15602                     num (car entry))
15603             (setq active (gnus-active group))
15604             (setq num
15605                   (if active (- (1+ (cdr active)) (car active)) t))
15606             ;; Check whether the group is foreign.  If so, the
15607             ;; foreign select method has to be entered into the
15608             ;; info.
15609             (let ((method (or gnus-override-subscribe-method
15610                               (gnus-group-method group))))
15611               (if (eq method gnus-select-method)
15612                   (setq info (list group level nil))
15613                 (setq info (list group level nil nil method)))))
15614           (unless previous
15615             (setq previous
15616                   (let ((p gnus-newsrc-alist))
15617                     (while (cddr p)
15618                       (setq p (cdr p)))
15619                     p)))
15620           (setq entry (cons info (cddr previous)))
15621           (if (cdr previous)
15622               (progn
15623                 (setcdr (cdr previous) entry)
15624                 (gnus-sethash group (cons num (cdr previous))
15625                               gnus-newsrc-hashtb))
15626             (setcdr previous entry)
15627             (gnus-sethash group (cons num previous)
15628                           gnus-newsrc-hashtb))
15629           (when (cdr entry)
15630             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15631       (when gnus-group-change-level-function
15632         (funcall gnus-group-change-level-function group level oldlevel)))))
15633
15634 (defun gnus-kill-newsgroup (newsgroup)
15635   "Obsolete function.  Kills a newsgroup."
15636   (gnus-group-change-level
15637    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15638
15639 (defun gnus-check-bogus-newsgroups (&optional confirm)
15640   "Remove bogus newsgroups.
15641 If CONFIRM is non-nil, the user has to confirm the deletion of every
15642 newsgroup."
15643   (let ((newsrc (cdr gnus-newsrc-alist))
15644         bogus group entry info)
15645     (gnus-message 5 "Checking bogus newsgroups...")
15646     (unless (gnus-read-active-file-p)
15647       (gnus-read-active-file))
15648     (when (gnus-read-active-file-p)
15649       ;; Find all bogus newsgroup that are subscribed.
15650       (while newsrc
15651         (setq info (pop newsrc)
15652               group (gnus-info-group info))
15653         (unless (or (gnus-active group) ; Active
15654                     (gnus-info-method info) ; Foreign
15655                     (and confirm
15656                          (not (gnus-y-or-n-p
15657                                (format "Remove bogus newsgroup: %s " group)))))
15658           ;; Found a bogus newsgroup.
15659           (push group bogus)))
15660       ;; Remove all bogus subscribed groups by first killing them, and
15661       ;; then removing them from the list of killed groups.
15662       (while bogus
15663         (when (setq entry (gnus-gethash (setq group (pop bogus))
15664                                         gnus-newsrc-hashtb))
15665           (gnus-group-change-level entry gnus-level-killed)
15666           (setq gnus-killed-list (delete group gnus-killed-list))))
15667       ;; Then we remove all bogus groups from the list of killed and
15668       ;; zombie groups.  They are are removed without confirmation.
15669       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15670             killed)
15671         (while dead-lists
15672           (setq killed (symbol-value (car dead-lists)))
15673           (while killed
15674             (unless (gnus-active (setq group (pop killed)))
15675               ;; The group is bogus.
15676               ;; !!!Slow as hell.
15677               (set (car dead-lists)
15678                    (delete group (symbol-value (car dead-lists))))))
15679           (setq dead-lists (cdr dead-lists))))
15680       (gnus-message 5 "Checking bogus newsgroups...done"))))
15681
15682 (defun gnus-check-duplicate-killed-groups ()
15683   "Remove duplicates from the list of killed groups."
15684   (interactive)
15685   (let ((killed gnus-killed-list))
15686     (while killed
15687       (gnus-message 9 "%d" (length killed))
15688       (setcdr killed (delete (car killed) (cdr killed)))
15689       (setq killed (cdr killed)))))
15690
15691 ;; We want to inline a function from gnus-cache, so we cheat here:
15692 (eval-when-compile
15693   (provide 'gnus)
15694   (require 'gnus-cache))
15695
15696 (defun gnus-get-unread-articles-in-group (info active &optional update)
15697   (when active
15698     ;; Allow the backend to update the info in the group.
15699     (when (and update 
15700                (gnus-request-update-info
15701                 info (gnus-find-method-for-group (gnus-info-group info))))
15702       (gnus-activate-group (gnus-info-group info) nil t))
15703     (let* ((range (gnus-info-read info))
15704            (num 0))
15705       ;; If a cache is present, we may have to alter the active info.
15706       (when (and gnus-use-cache info)
15707         (inline (gnus-cache-possibly-alter-active 
15708                  (gnus-info-group info) active)))
15709       ;; Modify the list of read articles according to what articles
15710       ;; are available; then tally the unread articles and add the
15711       ;; number to the group hash table entry.
15712       (cond
15713        ((zerop (cdr active))
15714         (setq num 0))
15715        ((not range)
15716         (setq num (- (1+ (cdr active)) (car active))))
15717        ((not (listp (cdr range)))
15718         ;; Fix a single (num . num) range according to the
15719         ;; active hash table.
15720         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15721         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15722         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15723         ;; Compute number of unread articles.
15724         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15725        (t
15726         ;; The read list is a list of ranges.  Fix them according to
15727         ;; the active hash table.
15728         ;; First peel off any elements that are below the lower
15729         ;; active limit.
15730         (while (and (cdr range)
15731                     (>= (car active)
15732                         (or (and (atom (cadr range)) (cadr range))
15733                             (caadr range))))
15734           (if (numberp (car range))
15735               (setcar range
15736                       (cons (car range)
15737                             (or (and (numberp (cadr range))
15738                                      (cadr range))
15739                                 (cdadr range))))
15740             (setcdr (car range)
15741                     (or (and (numberp (nth 1 range)) (nth 1 range))
15742                         (cdadr range))))
15743           (setcdr range (cddr range)))
15744         ;; Adjust the first element to be the same as the lower limit.
15745         (if (and (not (atom (car range)))
15746                  (< (cdar range) (car active)))
15747             (setcdr (car range) (1- (car active))))
15748         ;; Then we want to peel off any elements that are higher
15749         ;; than the upper active limit.
15750         (let ((srange range))
15751           ;; Go past all legal elements.
15752           (while (and (cdr srange)
15753                       (<= (or (and (atom (cadr srange))
15754                                    (cadr srange))
15755                               (caadr srange)) (cdr active)))
15756             (setq srange (cdr srange)))
15757           (if (cdr srange)
15758               ;; Nuke all remaining illegal elements.
15759               (setcdr srange nil))
15760
15761           ;; Adjust the final element.
15762           (if (and (not (atom (car srange)))
15763                    (> (cdar srange) (cdr active)))
15764               (setcdr (car srange) (cdr active))))
15765         ;; Compute the number of unread articles.
15766         (while range
15767           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15768                                       (cdar range)))
15769                               (or (and (atom (car range)) (car range))
15770                                   (caar range)))))
15771           (setq range (cdr range)))
15772         (setq num (max 0 (- (cdr active) num)))))
15773       ;; Set the number of unread articles.
15774       (when info
15775         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15776       num)))
15777
15778 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15779 ;; and compute how many unread articles there are in each group.
15780 (defun gnus-get-unread-articles (&optional level)
15781   (let* ((newsrc (cdr gnus-newsrc-alist))
15782          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15783          (foreign-level
15784           (min
15785            (cond ((and gnus-activate-foreign-newsgroups
15786                        (not (numberp gnus-activate-foreign-newsgroups)))
15787                   (1+ gnus-level-subscribed))
15788                  ((numberp gnus-activate-foreign-newsgroups)
15789                   gnus-activate-foreign-newsgroups)
15790                  (t 0))
15791            level))
15792          info group active method)
15793     (gnus-message 5 "Checking new news...")
15794
15795     (while newsrc
15796       (setq active (gnus-active (setq group (gnus-info-group
15797                                              (setq info (pop newsrc))))))
15798
15799       ;; Check newsgroups.  If the user doesn't want to check them, or
15800       ;; they can't be checked (for instance, if the news server can't
15801       ;; be reached) we just set the number of unread articles in this
15802       ;; newsgroup to t.  This means that Gnus thinks that there are
15803       ;; unread articles, but it has no idea how many.
15804       (if (and (setq method (gnus-info-method info))
15805                (not (gnus-server-equal
15806                      gnus-select-method
15807                      (setq method (gnus-server-get-method nil method))))
15808                (not (gnus-secondary-method-p method)))
15809           ;; These groups are foreign.  Check the level.
15810           (when (<= (gnus-info-level info) foreign-level)
15811             (setq active (gnus-activate-group group 'scan))
15812             (unless (inline (gnus-virtual-group-p group))
15813               (inline (gnus-close-group group)))
15814             (when (fboundp (intern (concat (symbol-name (car method))
15815                                            "-request-update-info")))
15816               (inline (gnus-request-update-info info method))))
15817         ;; These groups are native or secondary.
15818         (when (and (<= (gnus-info-level info) level)
15819                    (not gnus-read-active-file))
15820           (setq active (gnus-activate-group group 'scan))
15821           (inline (gnus-close-group group))))
15822
15823       ;; Get the number of unread articles in the group.
15824       (if active
15825           (inline (gnus-get-unread-articles-in-group info active))
15826         ;; The group couldn't be reached, so we nix out the number of
15827         ;; unread articles and stuff.
15828         (gnus-set-active group nil)
15829         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15830
15831     (gnus-message 5 "Checking new news...done")))
15832
15833 ;; Create a hash table out of the newsrc alist.  The `car's of the
15834 ;; alist elements are used as keys.
15835 (defun gnus-make-hashtable-from-newsrc-alist ()
15836   (let ((alist gnus-newsrc-alist)
15837         (ohashtb gnus-newsrc-hashtb)
15838         prev)
15839     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15840     (setq alist
15841           (setq prev (setq gnus-newsrc-alist
15842                            (if (equal (caar gnus-newsrc-alist)
15843                                       "dummy.group")
15844                                gnus-newsrc-alist
15845                              (cons (list "dummy.group" 0 nil) alist)))))
15846     (while alist
15847       (gnus-sethash
15848        (caar alist)
15849        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15850              prev)
15851        gnus-newsrc-hashtb)
15852       (setq prev alist
15853             alist (cdr alist)))))
15854
15855 (defun gnus-make-hashtable-from-killed ()
15856   "Create a hash table from the killed and zombie lists."
15857   (let ((lists '(gnus-killed-list gnus-zombie-list))
15858         list)
15859     (setq gnus-killed-hashtb
15860           (gnus-make-hashtable
15861            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15862     (while (setq list (pop lists))
15863       (setq list (symbol-value list))
15864       (while list
15865         (gnus-sethash (car list) (pop list) gnus-killed-hashtb)))))
15866
15867 (defun gnus-activate-group (group &optional scan dont-check)
15868   ;; Check whether a group has been activated or not.
15869   ;; If SCAN, request a scan of that group as well.
15870   (let ((method (gnus-find-method-for-group group))
15871         active)
15872     (and (gnus-check-server method)
15873          ;; We escape all bugs and quit here to make it possible to
15874          ;; continue if a group is so out-there that it reports bugs
15875          ;; and stuff.
15876          (progn
15877            (and scan
15878                 (gnus-check-backend-function 'request-scan (car method))
15879                 (gnus-request-scan group method))
15880            t)
15881          (condition-case ()
15882              (gnus-request-group group dont-check)
15883         ;   (error nil)
15884            (quit nil))
15885          (save-excursion
15886            (set-buffer nntp-server-buffer)
15887            (goto-char (point-min))
15888            ;; Parse the result we got from `gnus-request-group'.
15889            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15890                 (progn
15891                   (goto-char (match-beginning 1))
15892                   (gnus-set-active
15893                    group (setq active (cons (read (current-buffer))
15894                                             (read (current-buffer)))))
15895                   ;; Return the new active info.
15896                   active))))))
15897
15898 (defun gnus-update-read-articles (group unread)
15899   "Update the list of read and ticked articles in GROUP using the
15900 UNREAD and TICKED lists.
15901 Note: UNSELECTED has to be sorted over `<'.
15902 Returns whether the updating was successful."
15903   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15904          (entry (gnus-gethash group gnus-newsrc-hashtb))
15905          (info (nth 2 entry))
15906          (prev 1)
15907          (unread (sort (copy-sequence unread) '<))
15908          read)
15909     (if (or (not info) (not active))
15910         ;; There is no info on this group if it was, in fact,
15911         ;; killed.  Gnus stores no information on killed groups, so
15912         ;; there's nothing to be done.
15913         ;; One could store the information somewhere temporarily,
15914         ;; perhaps...  Hmmm...
15915         ()
15916       ;; Remove any negative articles numbers.
15917       (while (and unread (< (car unread) 0))
15918         (setq unread (cdr unread)))
15919       ;; Remove any expired article numbers
15920       (while (and unread (< (car unread) (car active)))
15921         (setq unread (cdr unread)))
15922       ;; Compute the ranges of read articles by looking at the list of
15923       ;; unread articles.
15924       (while unread
15925         (if (/= (car unread) prev)
15926             (setq read (cons (if (= prev (1- (car unread))) prev
15927                                (cons prev (1- (car unread)))) read)))
15928         (setq prev (1+ (car unread)))
15929         (setq unread (cdr unread)))
15930       (when (<= prev (cdr active))
15931         (setq read (cons (cons prev (cdr active)) read)))
15932       ;; Enter this list into the group info.
15933       (gnus-info-set-read
15934        info (if (> (length read) 1) (nreverse read) read))
15935       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15936       (gnus-get-unread-articles-in-group info (gnus-active group))
15937       t)))
15938
15939 (defun gnus-make-articles-unread (group articles)
15940   "Mark ARTICLES in GROUP as unread."
15941   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15942                           (gnus-gethash (gnus-group-real-name group)
15943                                         gnus-newsrc-hashtb))))
15944          (ranges (gnus-info-read info))
15945          news article)
15946     (while articles
15947       (when (gnus-member-of-range
15948              (setq article (pop articles)) ranges)
15949         (setq news (cons article news))))
15950     (when news
15951       (gnus-info-set-read
15952        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15953       (gnus-group-update-group group t))))
15954
15955 ;; Enter all dead groups into the hashtb.
15956 (defun gnus-update-active-hashtb-from-killed ()
15957   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15958         (lists (list gnus-killed-list gnus-zombie-list))
15959         killed)
15960     (while lists
15961       (setq killed (car lists))
15962       (while killed
15963         (gnus-sethash (car killed) nil hashtb)
15964         (setq killed (cdr killed)))
15965       (setq lists (cdr lists)))))
15966
15967 (defun gnus-get-killed-groups ()
15968   "Go through the active hashtb and all all unknown groups as killed."
15969   ;; First make sure active file has been read.
15970   (unless (gnus-read-active-file-p)
15971     (let ((gnus-read-active-file t))
15972       (gnus-read-active-file)))
15973   (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
15974   ;; Go through all newsgroups that are known to Gnus - enlarge kill list.
15975   (mapatoms
15976    (lambda (sym)
15977      (let ((groups 0)
15978            (group (symbol-name sym)))
15979        (if (or (null group)
15980                (gnus-gethash group gnus-killed-hashtb)
15981                (gnus-gethash group gnus-newsrc-hashtb))
15982            ()
15983          (let ((do-sub (gnus-matches-options-n group)))
15984            (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
15985                ()
15986              (setq groups (1+ groups))
15987              (setq gnus-killed-list
15988                    (cons group gnus-killed-list))
15989              (gnus-sethash group group gnus-killed-hashtb))))))
15990    gnus-active-hashtb))
15991
15992 ;; Get the active file(s) from the backend(s).
15993 (defun gnus-read-active-file ()
15994   (gnus-group-set-mode-line)
15995   (let ((methods 
15996          (append
15997           (if (gnus-check-server gnus-select-method)
15998               ;; The native server is available.
15999               (cons gnus-select-method gnus-secondary-select-methods)
16000             ;; The native server is down, so we just do the
16001             ;; secondary ones.
16002             gnus-secondary-select-methods)
16003           ;; Also read from the archive server.
16004           (when gnus-message-archive-method
16005             (list "archive"))))
16006         list-type)
16007     (setq gnus-have-read-active-file nil)
16008     (save-excursion
16009       (set-buffer nntp-server-buffer)
16010       (while methods
16011         (let* ((method (if (stringp (car methods))
16012                            (gnus-server-get-method nil (car methods))
16013                          (car methods)))
16014                (where (nth 1 method))
16015                (mesg (format "Reading active file%s via %s..."
16016                              (if (and where (not (zerop (length where))))
16017                                  (concat " from " where) "")
16018                              (car method))))
16019           (gnus-message 5 mesg)
16020           (when (gnus-check-server method)
16021             ;; Request that the backend scan its incoming messages.
16022             (and (gnus-check-backend-function 'request-scan (car method))
16023                  (gnus-request-scan nil method))
16024             (cond
16025              ((and (eq gnus-read-active-file 'some)
16026                    (gnus-check-backend-function 'retrieve-groups (car method)))
16027               (let ((newsrc (cdr gnus-newsrc-alist))
16028                     (gmethod (gnus-server-get-method nil method))
16029                     groups info)
16030                 (while (setq info (pop newsrc))
16031                   (when (gnus-server-equal
16032                          (gnus-find-method-for-group 
16033                           (gnus-info-group info) info)
16034                          gmethod)
16035                     (push (gnus-group-real-name (gnus-info-group info)) 
16036                           groups)))
16037                 (when groups
16038                   (gnus-check-server method)
16039                   (setq list-type (gnus-retrieve-groups groups method))
16040                   (cond
16041                    ((not list-type)
16042                     (gnus-error
16043                      1.2 "Cannot read partial active file from %s server."
16044                      (car method)))
16045                    ((eq list-type 'active)
16046                     (gnus-active-to-gnus-format method gnus-active-hashtb))
16047                    (t
16048                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
16049              (t
16050               (if (not (gnus-request-list method))
16051                   (unless (equal method gnus-message-archive-method)
16052                     (gnus-error 1 "Cannot read active file from %s server."
16053                                 (car method)))
16054                 (gnus-active-to-gnus-format method gnus-active-hashtb)
16055                 ;; We mark this active file as read.
16056                 (push method gnus-have-read-active-file)
16057                 (gnus-message 5 "%sdone" mesg))))))
16058         (setq methods (cdr methods))))))
16059
16060 ;; Read an active file and place the results in `gnus-active-hashtb'.
16061 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
16062   (unless method
16063     (setq method gnus-select-method))
16064   (let ((cur (current-buffer))
16065         (hashtb (or hashtb
16066                     (if (and gnus-active-hashtb
16067                              (not (equal method gnus-select-method)))
16068                         gnus-active-hashtb
16069                       (setq gnus-active-hashtb
16070                             (if (equal method gnus-select-method)
16071                                 (gnus-make-hashtable
16072                                  (count-lines (point-min) (point-max)))
16073                               (gnus-make-hashtable 4096)))))))
16074     ;; Delete unnecessary lines.
16075     (goto-char (point-min))
16076     (while (search-forward "\nto." nil t)
16077       (delete-region (1+ (match-beginning 0))
16078                      (progn (forward-line 1) (point))))
16079     (or (string= gnus-ignored-newsgroups "")
16080         (progn
16081           (goto-char (point-min))
16082           (delete-matching-lines gnus-ignored-newsgroups)))
16083     ;; Make the group names readable as a lisp expression even if they
16084     ;; contain special characters.
16085     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
16086     (goto-char (point-max))
16087     (while (re-search-backward "[][';?()#]" nil t)
16088       (insert ?\\))
16089     ;; If these are groups from a foreign select method, we insert the
16090     ;; group prefix in front of the group names.
16091     (and method (not (gnus-server-equal
16092                       (gnus-server-get-method nil method)
16093                       (gnus-server-get-method nil gnus-select-method)))
16094          (let ((prefix (gnus-group-prefixed-name "" method)))
16095            (goto-char (point-min))
16096            (while (and (not (eobp))
16097                        (progn (insert prefix)
16098                               (zerop (forward-line 1)))))))
16099     ;; Store the active file in a hash table.
16100     (goto-char (point-min))
16101     (if (string-match "%[oO]" gnus-group-line-format)
16102         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
16103         ;; If we want information on moderated groups, we use this
16104         ;; loop...
16105         (let* ((mod-hashtb (make-vector 7 0))
16106                (m (intern "m" mod-hashtb))
16107                group max min)
16108           (while (not (eobp))
16109             (condition-case nil
16110                 (progn
16111                   (narrow-to-region (point) (gnus-point-at-eol))
16112                   (setq group (let ((obarray hashtb)) (read cur)))
16113                   (if (and (numberp (setq max (read cur)))
16114                            (numberp (setq min (read cur)))
16115                            (progn
16116                              (skip-chars-forward " \t")
16117                              (not
16118                               (or (= (following-char) ?=)
16119                                   (= (following-char) ?x)
16120                                   (= (following-char) ?j)))))
16121                       (set group (cons min max))
16122                     (set group nil))
16123                   ;; Enter moderated groups into a list.
16124                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
16125                       (setq gnus-moderated-list
16126                             (cons (symbol-name group) gnus-moderated-list))))
16127               (error
16128                (and group
16129                     (symbolp group)
16130                     (set group nil))))
16131             (widen)
16132             (forward-line 1)))
16133       ;; And if we do not care about moderation, we use this loop,
16134       ;; which is faster.
16135       (let (group max min)
16136         (while (not (eobp))
16137           (condition-case ()
16138               (progn
16139                 (narrow-to-region (point) (gnus-point-at-eol))
16140                 ;; group gets set to a symbol interned in the hash table
16141                 ;; (what a hack!!) - jwz
16142                 (setq group (let ((obarray hashtb)) (read cur)))
16143                 (if (and (numberp (setq max (read cur)))
16144                          (numberp (setq min (read cur)))
16145                          (progn
16146                            (skip-chars-forward " \t")
16147                            (not
16148                             (or (= (following-char) ?=)
16149                                 (= (following-char) ?x)
16150                                 (= (following-char) ?j)))))
16151                     (set group (cons min max))
16152                   (set group nil)))
16153             (error
16154              (progn
16155                (and group
16156                     (symbolp group)
16157                     (set group nil))
16158                (or ignore-errors
16159                    (gnus-message 3 "Warning - illegal active: %s"
16160                                  (buffer-substring
16161                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
16162           (widen)
16163           (forward-line 1))))))
16164
16165 (defun gnus-groups-to-gnus-format (method &optional hashtb)
16166   ;; Parse a "groups" active file.
16167   (let ((cur (current-buffer))
16168         (hashtb (or hashtb
16169                     (if (and method gnus-active-hashtb)
16170                         gnus-active-hashtb
16171                       (setq gnus-active-hashtb
16172                             (gnus-make-hashtable
16173                              (count-lines (point-min) (point-max)))))))
16174         (prefix (and method
16175                      (not (gnus-server-equal
16176                            (gnus-server-get-method nil method)
16177                            (gnus-server-get-method nil gnus-select-method)))
16178                      (gnus-group-prefixed-name "" method))))
16179
16180     (goto-char (point-min))
16181     ;; We split this into to separate loops, one with the prefix
16182     ;; and one without to speed the reading up somewhat.
16183     (if prefix
16184         (let (min max opoint group)
16185           (while (not (eobp))
16186             (condition-case ()
16187                 (progn
16188                   (read cur) (read cur)
16189                   (setq min (read cur)
16190                         max (read cur)
16191                         opoint (point))
16192                   (skip-chars-forward " \t")
16193                   (insert prefix)
16194                   (goto-char opoint)
16195                   (set (let ((obarray hashtb)) (read cur))
16196                        (cons min max)))
16197               (error (and group (symbolp group) (set group nil))))
16198             (forward-line 1)))
16199       (let (min max group)
16200         (while (not (eobp))
16201           (condition-case ()
16202               (if (= (following-char) ?2)
16203                   (progn
16204                     (read cur) (read cur)
16205                     (setq min (read cur)
16206                           max (read cur))
16207                     (set (setq group (let ((obarray hashtb)) (read cur)))
16208                          (cons min max))))
16209             (error (and group (symbolp group) (set group nil))))
16210           (forward-line 1))))))
16211
16212 (defun gnus-read-newsrc-file (&optional force)
16213   "Read startup file.
16214 If FORCE is non-nil, the .newsrc file is read."
16215   ;; Reset variables that might be defined in the .newsrc.eld file.
16216   (let ((variables gnus-variable-list))
16217     (while variables
16218       (set (car variables) nil)
16219       (setq variables (cdr variables))))
16220   (let* ((newsrc-file gnus-current-startup-file)
16221          (quick-file (concat newsrc-file ".el")))
16222     (save-excursion
16223       ;; We always load the .newsrc.eld file.  If always contains
16224       ;; much information that can not be gotten from the .newsrc
16225       ;; file (ticked articles, killed groups, foreign methods, etc.)
16226       (gnus-read-newsrc-el-file quick-file)
16227
16228       (if (and (file-exists-p gnus-current-startup-file)
16229                (or force
16230                    (and (file-newer-than-file-p newsrc-file quick-file)
16231                         (file-newer-than-file-p newsrc-file
16232                                                 (concat quick-file "d")))
16233                    (not gnus-newsrc-alist)))
16234           ;; We read the .newsrc file.  Note that if there if a
16235           ;; .newsrc.eld file exists, it has already been read, and
16236           ;; the `gnus-newsrc-hashtb' has been created.  While reading
16237           ;; the .newsrc file, Gnus will only use the information it
16238           ;; can find there for changing the data already read -
16239           ;; ie. reading the .newsrc file will not trash the data
16240           ;; already read (except for read articles).
16241           (save-excursion
16242             (gnus-message 5 "Reading %s..." newsrc-file)
16243             (set-buffer (find-file-noselect newsrc-file))
16244             (buffer-disable-undo (current-buffer))
16245             (gnus-newsrc-to-gnus-format)
16246             (kill-buffer (current-buffer))
16247             (gnus-message 5 "Reading %s...done" newsrc-file)))
16248
16249       ;; Read any slave files.
16250       (unless gnus-slave
16251         (gnus-master-read-slave-newsrc))
16252       
16253       ;; Convert old to new.
16254       (gnus-convert-old-newsrc))))
16255
16256 (defun gnus-continuum-version (version)
16257   "Return VERSION as a floating point number."
16258   (when (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
16259     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
16260            (number (match-string 2 version))
16261            major minor least)
16262       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
16263       (setq major (string-to-number (match-string 1 number)))
16264       (setq minor (string-to-number (match-string 2 number)))
16265       (setq least (if (match-beginning 3)
16266                       (string-to-number (match-string 3 number))
16267                     0))
16268       (string-to-number
16269        (if (zerop major)
16270            (format "%s00%02d%02d"
16271                    (cond 
16272                     ((string= alpha "(ding)") "4.99")
16273                     ((string= alpha "September") "5.01")
16274                     ((string= alpha "Red") "5.03"))
16275                    minor least)
16276          (format "%d.%02d%20d" major minor least))))))
16277
16278 (defun gnus-convert-old-newsrc ()
16279   "Convert old newsrc into the new format, if needed."
16280   (let ((fcv (and gnus-newsrc-file-version
16281                   (gnus-continuum-version gnus-newsrc-file-version))))
16282     (cond
16283      ;; No .newsrc.eld file was loaded.
16284      ((null fcv) nil)
16285      ;; Gnus 5 .newsrc.eld was loaded.
16286      ((< fcv (gnus-continuum-version "September Gnus v0.1"))
16287       (gnus-convert-old-ticks)))))
16288
16289 (defun gnus-convert-old-ticks ()
16290   (let ((newsrc (cdr gnus-newsrc-alist))
16291         marks info dormant ticked)
16292     (while (setq info (pop newsrc))
16293       (when (setq marks (gnus-info-marks info))
16294         (setq dormant (cdr (assq 'dormant marks))
16295               ticked (cdr (assq 'tick marks)))
16296         (when (or dormant ticked)
16297           (gnus-info-set-read
16298            info
16299            (gnus-add-to-range
16300             (gnus-info-read info)
16301             (nconc (gnus-uncompress-range dormant)
16302                    (gnus-uncompress-range ticked)))))))))
16303
16304 (defun gnus-read-newsrc-el-file (file)
16305   (let ((ding-file (concat file "d")))
16306     ;; We always, always read the .eld file.
16307     (gnus-message 5 "Reading %s..." ding-file)
16308     (let (gnus-newsrc-assoc)
16309       (condition-case nil
16310           (load ding-file t t t)
16311         (error
16312          (gnus-error 1 "Error in %s" ding-file)))
16313       (when gnus-newsrc-assoc
16314         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
16315     (gnus-make-hashtable-from-newsrc-alist)
16316     (when (file-newer-than-file-p file ding-file)
16317       ;; Old format quick file
16318       (gnus-message 5 "Reading %s..." file)
16319       ;; The .el file is newer than the .eld file, so we read that one
16320       ;; as well.
16321       (gnus-read-old-newsrc-el-file file))))
16322
16323 ;; Parse the old-style quick startup file
16324 (defun gnus-read-old-newsrc-el-file (file)
16325   (let (newsrc killed marked group m info)
16326     (prog1
16327         (let ((gnus-killed-assoc nil)
16328               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
16329           (prog1
16330               (condition-case nil
16331                   (load file t t t)
16332                 (error nil))
16333             (setq newsrc gnus-newsrc-assoc
16334                   killed gnus-killed-assoc
16335                   marked gnus-marked-assoc)))
16336       (setq gnus-newsrc-alist nil)
16337       (while (setq info (gnus-get-info (setq group (pop newsrc))))
16338         (if info
16339             (progn
16340               (gnus-info-set-read info (cddr group))
16341               (gnus-info-set-level
16342                info (if (nth 1 group) gnus-level-default-subscribed
16343                       gnus-level-default-unsubscribed))
16344               (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
16345           (push (setq info
16346                       (list (car group)
16347                             (if (nth 1 group) gnus-level-default-subscribed
16348                               gnus-level-default-unsubscribed)
16349                             (cddr group)))
16350                 gnus-newsrc-alist))
16351         ;; Copy marks into info.
16352         (when (setq m (assoc (car group) marked))
16353           (unless (nthcdr 3 info)
16354             (nconc info (list nil)))
16355           (gnus-info-set-marks
16356            info (list (cons 'tick (gnus-compress-sequence 
16357                                    (sort (cdr m) '<) t))))))
16358       (setq newsrc killed)
16359       (while newsrc
16360         (setcar newsrc (caar newsrc))
16361         (setq newsrc (cdr newsrc)))
16362       (setq gnus-killed-list killed))
16363     ;; The .el file version of this variable does not begin with
16364     ;; "options", while the .eld version does, so we just add it if it
16365     ;; isn't there.
16366     (and
16367      gnus-newsrc-options
16368      (progn
16369        (and (not (string-match "^ *options" gnus-newsrc-options))
16370             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
16371        (and (not (string-match "\n$" gnus-newsrc-options))
16372             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
16373        ;; Finally, if we read some options lines, we parse them.
16374        (or (string= gnus-newsrc-options "")
16375            (gnus-newsrc-parse-options gnus-newsrc-options))))
16376
16377     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
16378     (gnus-make-hashtable-from-newsrc-alist)))
16379
16380 (defun gnus-make-newsrc-file (file)
16381   "Make server dependent file name by catenating FILE and server host name."
16382   (let* ((file (expand-file-name file nil))
16383          (real-file (concat file "-" (nth 1 gnus-select-method))))
16384     (if (or (file-exists-p real-file)
16385             (file-exists-p (concat real-file ".el"))
16386             (file-exists-p (concat real-file ".eld")))
16387         real-file file)))
16388
16389 (defun gnus-newsrc-to-gnus-format ()
16390   (setq gnus-newsrc-options "")
16391   (setq gnus-newsrc-options-n nil)
16392
16393   (or gnus-active-hashtb
16394       (setq gnus-active-hashtb (make-vector 4095 0)))
16395   (let ((buf (current-buffer))
16396         (already-read (> (length gnus-newsrc-alist) 1))
16397         group subscribed options-symbol newsrc Options-symbol
16398         symbol reads num1)
16399     (goto-char (point-min))
16400     ;; We intern the symbol `options' in the active hashtb so that we
16401     ;; can `eq' against it later.
16402     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
16403     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
16404
16405     (while (not (eobp))
16406       ;; We first read the first word on the line by narrowing and
16407       ;; then reading into `gnus-active-hashtb'.  Most groups will
16408       ;; already exist in that hashtb, so this will save some string
16409       ;; space.
16410       (narrow-to-region
16411        (point)
16412        (progn (skip-chars-forward "^ \t!:\n") (point)))
16413       (goto-char (point-min))
16414       (setq symbol
16415             (and (/= (point-min) (point-max))
16416                  (let ((obarray gnus-active-hashtb)) (read buf))))
16417       (widen)
16418       ;; Now, the symbol we have read is either `options' or a group
16419       ;; name.  If it is an options line, we just add it to a string.
16420       (cond
16421        ((or (eq symbol options-symbol)
16422             (eq symbol Options-symbol))
16423         (setq gnus-newsrc-options
16424               ;; This concating is quite inefficient, but since our
16425               ;; thorough studies show that approx 99.37% of all
16426               ;; .newsrc files only contain a single options line, we
16427               ;; don't give a damn, frankly, my dear.
16428               (concat gnus-newsrc-options
16429                       (buffer-substring
16430                        (gnus-point-at-bol)
16431                        ;; Options may continue on the next line.
16432                        (or (and (re-search-forward "^[^ \t]" nil 'move)
16433                                 (progn (beginning-of-line) (point)))
16434                            (point)))))
16435         (forward-line -1))
16436        (symbol
16437         ;; Group names can be just numbers.  
16438         (when (numberp symbol) 
16439           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
16440         (or (boundp symbol) (set symbol nil))
16441         ;; It was a group name.
16442         (setq subscribed (= (following-char) ?:)
16443               group (symbol-name symbol)
16444               reads nil)
16445         (if (eolp)
16446             ;; If the line ends here, this is clearly a buggy line, so
16447             ;; we put point a the beginning of line and let the cond
16448             ;; below do the error handling.
16449             (beginning-of-line)
16450           ;; We skip to the beginning of the ranges.
16451           (skip-chars-forward "!: \t"))
16452         ;; We are now at the beginning of the list of read articles.
16453         ;; We read them range by range.
16454         (while
16455             (cond
16456              ((looking-at "[0-9]+")
16457               ;; We narrow and read a number instead of buffer-substring/
16458               ;; string-to-int because it's faster.  narrow/widen is
16459               ;; faster than save-restriction/narrow, and save-restriction
16460               ;; produces a garbage object.
16461               (setq num1 (progn
16462                            (narrow-to-region (match-beginning 0) (match-end 0))
16463                            (read buf)))
16464               (widen)
16465               ;; If the next character is a dash, then this is a range.
16466               (if (= (following-char) ?-)
16467                   (progn
16468                     ;; We read the upper bound of the range.
16469                     (forward-char 1)
16470                     (if (not (looking-at "[0-9]+"))
16471                         ;; This is a buggy line, by we pretend that
16472                         ;; it's kinda OK.  Perhaps the user should be
16473                         ;; dinged?
16474                         (setq reads (cons num1 reads))
16475                       (setq reads
16476                             (cons
16477                              (cons num1
16478                                    (progn
16479                                      (narrow-to-region (match-beginning 0)
16480                                                        (match-end 0))
16481                                      (read buf)))
16482                              reads))
16483                       (widen)))
16484                 ;; It was just a simple number, so we add it to the
16485                 ;; list of ranges.
16486                 (setq reads (cons num1 reads)))
16487               ;; If the next char in ?\n, then we have reached the end
16488               ;; of the line and return nil.
16489               (/= (following-char) ?\n))
16490              ((= (following-char) ?\n)
16491               ;; End of line, so we end.
16492               nil)
16493              (t
16494               ;; Not numbers and not eol, so this might be a buggy
16495               ;; line...
16496               (or (eobp)
16497                   ;; If it was eob instead of ?\n, we allow it.
16498                   (progn
16499                     ;; The line was buggy.
16500                     (setq group nil)
16501                     (gnus-error 3.1 "Mangled line: %s"
16502                                 (buffer-substring (gnus-point-at-bol)
16503                                                   (gnus-point-at-eol)))))
16504               nil))
16505           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16506           ;; we allow them, because it's a common mistake to put a
16507           ;; space after the comma.
16508           (skip-chars-forward ", "))
16509
16510         ;; We have already read .newsrc.eld, so we gently update the
16511         ;; data in the hash table with the information we have just
16512         ;; read.
16513         (when group
16514           (let ((info (gnus-get-info group))
16515                 level)
16516             (if info
16517                 ;; There is an entry for this file in the alist.
16518                 (progn
16519                   (gnus-info-set-read info (nreverse reads))
16520                   ;; We update the level very gently.  In fact, we
16521                   ;; only change it if there's been a status change
16522                   ;; from subscribed to unsubscribed, or vice versa.
16523                   (setq level (gnus-info-level info))
16524                   (cond ((and (<= level gnus-level-subscribed)
16525                               (not subscribed))
16526                          (setq level (if reads
16527                                          gnus-level-default-unsubscribed
16528                                        (1+ gnus-level-default-unsubscribed))))
16529                         ((and (> level gnus-level-subscribed) subscribed)
16530                          (setq level gnus-level-default-subscribed)))
16531                   (gnus-info-set-level info level))
16532               ;; This is a new group.
16533               (setq info (list group
16534                                (if subscribed
16535                                    gnus-level-default-subscribed
16536                                  (if reads
16537                                      (1+ gnus-level-subscribed)
16538                                    gnus-level-default-unsubscribed))
16539                                (nreverse reads))))
16540             (setq newsrc (cons info newsrc))))))
16541       (forward-line 1))
16542
16543     (setq newsrc (nreverse newsrc))
16544
16545     (if (not already-read)
16546         ()
16547       ;; We now have two newsrc lists - `newsrc', which is what we
16548       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16549       ;; what we've read from .newsrc.eld.  We have to merge these
16550       ;; lists.  We do this by "attaching" any (foreign) groups in the
16551       ;; gnus-newsrc-alist to the (native) group that precedes them.
16552       (let ((rc (cdr gnus-newsrc-alist))
16553             (prev gnus-newsrc-alist)
16554             entry mentry)
16555         (while rc
16556           (or (null (nth 4 (car rc)))   ; It's a native group.
16557               (assoc (caar rc) newsrc) ; It's already in the alist.
16558               (if (setq entry (assoc (caar prev) newsrc))
16559                   (setcdr (setq mentry (memq entry newsrc))
16560                           (cons (car rc) (cdr mentry)))
16561                 (setq newsrc (cons (car rc) newsrc))))
16562           (setq prev rc
16563                 rc (cdr rc)))))
16564
16565     (setq gnus-newsrc-alist newsrc)
16566     ;; We make the newsrc hashtb.
16567     (gnus-make-hashtable-from-newsrc-alist)
16568
16569     ;; Finally, if we read some options lines, we parse them.
16570     (or (string= gnus-newsrc-options "")
16571         (gnus-newsrc-parse-options gnus-newsrc-options))))
16572
16573 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16574 ;; The return value will be a list on the form
16575 ;; ((regexp1 . ignore)
16576 ;;  (regexp2 . subscribe)...)
16577 ;; When handling new newsgroups, groups that match a `ignore' regexp
16578 ;; will be ignored, and groups that match a `subscribe' regexp will be
16579 ;; subscribed.  A line like
16580 ;; options -n !all rec.all
16581 ;; will lead to a list that looks like
16582 ;; (("^rec\\..+" . subscribe)
16583 ;;  ("^.+" . ignore))
16584 ;; So all "rec.*" groups will be subscribed, while all the other
16585 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16586 ;; different from "options -n rec.all !all".
16587 (defun gnus-newsrc-parse-options (options)
16588   (let (out eol)
16589     (save-excursion
16590       (gnus-set-work-buffer)
16591       (insert (regexp-quote options))
16592       ;; First we treat all continuation lines.
16593       (goto-char (point-min))
16594       (while (re-search-forward "\n[ \t]+" nil t)
16595         (replace-match " " t t))
16596       ;; Then we transform all "all"s into ".+"s.
16597       (goto-char (point-min))
16598       (while (re-search-forward "\\ball\\b" nil t)
16599         (replace-match ".+" t t))
16600       (goto-char (point-min))
16601       ;; We remove all other options than the "-n" ones.
16602       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16603         (replace-match " ")
16604         (forward-char -1))
16605       (goto-char (point-min))
16606
16607       ;; We are only interested in "options -n" lines - we
16608       ;; ignore the other option lines.
16609       (while (re-search-forward "[ \t]-n" nil t)
16610         (setq eol
16611               (or (save-excursion
16612                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16613                          (- (point) 2)))
16614                   (gnus-point-at-eol)))
16615         ;; Search for all "words"...
16616         (while (re-search-forward "[^ \t,\n]+" eol t)
16617           (if (= (char-after (match-beginning 0)) ?!)
16618               ;; If the word begins with a bang (!), this is a "not"
16619               ;; spec.  We put this spec (minus the bang) and the
16620               ;; symbol `ignore' into the list.
16621               (setq out (cons (cons (concat
16622                                      "^" (buffer-substring
16623                                           (1+ (match-beginning 0))
16624                                           (match-end 0)))
16625                                     'ignore) out))
16626             ;; There was no bang, so this is a "yes" spec.
16627             (setq out (cons (cons (concat "^" (match-string 0))
16628                                   'subscribe) out)))))
16629
16630       (setq gnus-newsrc-options-n out))))
16631
16632 (defun gnus-save-newsrc-file (&optional force)
16633   "Save .newsrc file."
16634   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16635   ;; from the variable gnus-newsrc-alist.
16636   (when (and (or gnus-newsrc-alist gnus-killed-list)
16637              gnus-current-startup-file)
16638     (save-excursion
16639       (if (and (or gnus-use-dribble-file gnus-slave)
16640                (not force)
16641                (or (not gnus-dribble-buffer)
16642                    (not (buffer-name gnus-dribble-buffer))
16643                    (zerop (save-excursion
16644                             (set-buffer gnus-dribble-buffer)
16645                             (buffer-size)))))
16646           (gnus-message 4 "(No changes need to be saved)")
16647         (run-hooks 'gnus-save-newsrc-hook)
16648         (if gnus-slave
16649             (gnus-slave-save-newsrc)
16650           ;; Save .newsrc.
16651           (when gnus-save-newsrc-file
16652             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16653             (gnus-gnus-to-newsrc-format)
16654             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16655           ;; Save .newsrc.eld.
16656           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16657           (make-local-variable 'version-control)
16658           (setq version-control 'never)
16659           (setq buffer-file-name
16660                 (concat gnus-current-startup-file ".eld"))
16661           (gnus-add-current-to-buffer-list)
16662           (buffer-disable-undo (current-buffer))
16663           (erase-buffer)
16664           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16665           (gnus-gnus-to-quick-newsrc-format)
16666           (run-hooks 'gnus-save-quick-newsrc-hook)
16667           (save-buffer)
16668           (kill-buffer (current-buffer))
16669           (gnus-message
16670            5 "Saving %s.eld...done" gnus-current-startup-file))
16671         (gnus-dribble-delete-file)
16672         (gnus-group-set-mode-line)))))
16673
16674 (defun gnus-gnus-to-quick-newsrc-format ()
16675   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16676   (insert ";; Gnus startup file.\n")
16677   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16678   (insert ";; to read .newsrc.\n")
16679   (insert "(setq gnus-newsrc-file-version "
16680           (prin1-to-string gnus-version) ")\n")
16681   (let ((variables
16682          (if gnus-save-killed-list gnus-variable-list
16683            ;; Remove the `gnus-killed-list' from the list of variables
16684            ;; to be saved, if required.
16685            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16686         ;; Peel off the "dummy" group.
16687         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16688         variable)
16689     ;; Insert the variables into the file.
16690     (while variables
16691       (when (and (boundp (setq variable (pop variables)))
16692                  (symbol-value variable))
16693         (insert "(setq " (symbol-name variable) " '")
16694         (prin1 (symbol-value variable) (current-buffer))
16695         (insert ")\n")))))
16696
16697 (defun gnus-gnus-to-newsrc-format ()
16698   ;; Generate and save the .newsrc file.
16699   (save-excursion
16700     (set-buffer (create-file-buffer gnus-current-startup-file))
16701     (let ((newsrc (cdr gnus-newsrc-alist))
16702           (standard-output (current-buffer))
16703           info ranges range method)
16704       (setq buffer-file-name gnus-current-startup-file)
16705       (buffer-disable-undo (current-buffer))
16706       (erase-buffer)
16707       ;; Write options.
16708       (if gnus-newsrc-options (insert gnus-newsrc-options))
16709       ;; Write subscribed and unsubscribed.
16710       (while (setq info (pop newsrc))
16711         ;; Don't write foreign groups to .newsrc.
16712         (when (or (null (setq method (gnus-info-method info)))
16713                   (equal method "native")
16714                   (gnus-server-equal method gnus-select-method))
16715           (insert (gnus-info-group info)
16716                   (if (> (gnus-info-level info) gnus-level-subscribed)
16717                       "!" ":"))
16718           (when (setq ranges (gnus-info-read info))
16719             (insert " ")
16720             (if (not (listp (cdr ranges)))
16721                 (if (= (car ranges) (cdr ranges))
16722                     (princ (car ranges))
16723                   (princ (car ranges))
16724                   (insert "-")
16725                   (princ (cdr ranges)))
16726               (while (setq range (pop ranges))
16727                 (if (or (atom range) (= (car range) (cdr range)))
16728                     (princ (or (and (atom range) range) (car range)))
16729                   (princ (car range))
16730                   (insert "-")
16731                   (princ (cdr range)))
16732                 (if ranges (insert ",")))))
16733           (insert "\n")))
16734       (make-local-variable 'version-control)
16735       (setq version-control 'never)
16736       ;; It has been reported that sometime the modtime on the .newsrc
16737       ;; file seems to be off.  We really do want to overwrite it, so
16738       ;; we clear the modtime here before saving.  It's a bit odd,
16739       ;; though...
16740       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16741       ;; delete the silly thing entirely first.  but this fails to provide
16742       ;; such niceties as .newsrc~ creation.
16743       (if gnus-modtime-botch
16744           (delete-file gnus-startup-file)
16745         (clear-visited-file-modtime))
16746       (run-hooks 'gnus-save-standard-newsrc-hook)
16747       (save-buffer)
16748       (kill-buffer (current-buffer)))))
16749
16750 \f
16751 ;;;
16752 ;;; Slave functions.
16753 ;;;
16754
16755 (defun gnus-slave-save-newsrc ()
16756   (save-excursion
16757     (set-buffer gnus-dribble-buffer)
16758     (let ((slave-name
16759            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16760       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16761
16762 (defun gnus-master-read-slave-newsrc ()
16763   (let ((slave-files
16764          (directory-files
16765           (file-name-directory gnus-current-startup-file)
16766           t (concat
16767              "^" (regexp-quote
16768                   (concat
16769                    (file-name-nondirectory gnus-current-startup-file)
16770                    "-slave-")))
16771           t))
16772         file)
16773     (if (not slave-files)
16774         ()                              ; There are no slave files to read.
16775       (gnus-message 7 "Reading slave newsrcs...")
16776       (save-excursion
16777         (set-buffer (get-buffer-create " *gnus slave*"))
16778         (buffer-disable-undo (current-buffer))
16779         (setq slave-files
16780               (sort (mapcar (lambda (file)
16781                               (list (nth 5 (file-attributes file)) file))
16782                             slave-files)
16783                     (lambda (f1 f2)
16784                       (or (< (caar f1) (caar f2))
16785                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16786         (while slave-files
16787           (erase-buffer)
16788           (setq file (nth 1 (car slave-files)))
16789           (insert-file-contents file)
16790           (if (condition-case ()
16791                   (progn
16792                     (eval-buffer (current-buffer))
16793                     t)
16794                 (error
16795                  (gnus-error 3.2 "Possible error in %s" file)
16796                  nil))
16797               (or gnus-slave ; Slaves shouldn't delete these files.
16798                   (condition-case ()
16799                       (delete-file file)
16800                     (error nil))))
16801           (setq slave-files (cdr slave-files))))
16802       (gnus-message 7 "Reading slave newsrcs...done"))))
16803
16804 \f
16805 ;;;
16806 ;;; Group description.
16807 ;;;
16808
16809 (defun gnus-read-all-descriptions-files ()
16810   (let ((methods (cons gnus-select-method 
16811                        (nconc
16812                         (when gnus-message-archive-method
16813                           (list "archive"))
16814                         gnus-secondary-select-methods))))
16815     (while methods
16816       (gnus-read-descriptions-file (car methods))
16817       (setq methods (cdr methods)))
16818     t))
16819
16820 (defun gnus-read-descriptions-file (&optional method)
16821   (let ((method (or method gnus-select-method)))
16822     (when (stringp method)
16823       (setq method (gnus-server-to-method method)))
16824     ;; We create the hashtable whether we manage to read the desc file
16825     ;; to avoid trying to re-read after a failed read.
16826     (or gnus-description-hashtb
16827         (setq gnus-description-hashtb
16828               (gnus-make-hashtable (length gnus-active-hashtb))))
16829     ;; Mark this method's desc file as read.
16830     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16831                   gnus-description-hashtb)
16832
16833     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16834     (cond
16835      ((not (gnus-check-server method))
16836       (gnus-message 1 "Couldn't open server")
16837       nil)
16838      ((not (gnus-request-list-newsgroups method))
16839       (gnus-message 1 "Couldn't read newsgroups descriptions")
16840       nil)
16841      (t
16842       (let (group)
16843         (save-excursion
16844           (save-restriction
16845             (set-buffer nntp-server-buffer)
16846             (goto-char (point-min))
16847             (if (or (search-forward "\n.\n" nil t)
16848                     (goto-char (point-max)))
16849                 (progn
16850                   (beginning-of-line)
16851                   (narrow-to-region (point-min) (point))))
16852             (goto-char (point-min))
16853             (while (not (eobp))
16854               ;; If we get an error, we set group to 0, which is not a
16855               ;; symbol...
16856               (setq group
16857                     (condition-case ()
16858                         (let ((obarray gnus-description-hashtb))
16859                           ;; Group is set to a symbol interned in this
16860                           ;; hash table.
16861                           (read nntp-server-buffer))
16862                       (error 0)))
16863               (skip-chars-forward " \t")
16864               ;; ...  which leads to this line being effectively ignored.
16865               (and (symbolp group)
16866                    (set group (buffer-substring
16867                                (point) (progn (end-of-line) (point)))))
16868               (forward-line 1))))
16869         (gnus-message 5 "Reading descriptions file...done")
16870         t)))))
16871
16872 (defun gnus-group-get-description (group)
16873   "Get the description of a group by sending XGTITLE to the server."
16874   (when (gnus-request-group-description group)
16875     (save-excursion
16876       (set-buffer nntp-server-buffer)
16877       (goto-char (point-min))
16878       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16879         (match-string 1)))))
16880
16881 \f
16882 ;;;
16883 ;;; Buffering of read articles.
16884 ;;;
16885
16886 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16887 (defvar gnus-backlog-articles nil)
16888 (defvar gnus-backlog-hashtb nil)
16889
16890 (defun gnus-backlog-buffer ()
16891   "Return the backlog buffer."
16892   (or (get-buffer gnus-backlog-buffer)
16893       (save-excursion
16894         (set-buffer (get-buffer-create gnus-backlog-buffer))
16895         (buffer-disable-undo (current-buffer))
16896         (setq buffer-read-only t)
16897         (gnus-add-current-to-buffer-list)
16898         (get-buffer gnus-backlog-buffer))))
16899
16900 (defun gnus-backlog-setup ()
16901   "Initialize backlog variables."
16902   (unless gnus-backlog-hashtb
16903     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16904
16905 (gnus-add-shutdown 'gnus-backlog-shutdown 'gnus)
16906
16907 (defun gnus-backlog-shutdown ()
16908   "Clear all backlog variables and buffers."
16909   (when (get-buffer gnus-backlog-buffer)
16910     (kill-buffer gnus-backlog-buffer))
16911   (setq gnus-backlog-hashtb nil
16912         gnus-backlog-articles nil))
16913
16914 (defun gnus-backlog-enter-article (group number buffer)
16915   (gnus-backlog-setup)
16916   (let ((ident (intern (concat group ":" (int-to-string number))
16917                        gnus-backlog-hashtb))
16918         b)
16919     (if (memq ident gnus-backlog-articles)
16920         () ; It's already kept.
16921       ;; Remove the oldest article, if necessary.
16922       (and (numberp gnus-keep-backlog)
16923            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16924            (gnus-backlog-remove-oldest-article))
16925       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16926       ;; Insert the new article.
16927       (save-excursion
16928         (set-buffer (gnus-backlog-buffer))
16929         (let (buffer-read-only)
16930           (goto-char (point-max))
16931           (or (bolp) (insert "\n"))
16932           (setq b (point))
16933           (insert-buffer-substring buffer)
16934           ;; Tag the beginning of the article with the ident.
16935           (gnus-put-text-property b (1+ b) 'gnus-backlog ident))))))
16936
16937 (defun gnus-backlog-remove-oldest-article ()
16938   (save-excursion
16939     (set-buffer (gnus-backlog-buffer))
16940     (goto-char (point-min))
16941     (if (zerop (buffer-size))
16942         () ; The buffer is empty.
16943       (let ((ident (get-text-property (point) 'gnus-backlog))
16944             buffer-read-only)
16945         ;; Remove the ident from the list of articles.
16946         (when ident
16947           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16948         ;; Delete the article itself.
16949         (delete-region
16950          (point) (next-single-property-change
16951                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16952
16953 (defun gnus-backlog-remove-article (group number)
16954   "Remove article NUMBER in GROUP from the backlog."
16955   (when (numberp number)
16956     (gnus-backlog-setup)
16957     (let ((ident (intern (concat group ":" (int-to-string number))
16958                          gnus-backlog-hashtb))
16959           beg end)
16960       (when (memq ident gnus-backlog-articles)
16961         ;; It was in the backlog.
16962         (save-excursion
16963           (set-buffer (gnus-backlog-buffer))
16964           (let (buffer-read-only)
16965             (when (setq beg (text-property-any
16966                              (point-min) (point-max) 'gnus-backlog
16967                              ident))
16968               ;; Find the end (i. e., the beginning of the next article).
16969               (setq end
16970                     (next-single-property-change
16971                      (1+ beg) 'gnus-backlog (current-buffer) (point-max)))
16972               (delete-region beg end)
16973               ;; Return success.
16974               t)))))))
16975
16976 (defun gnus-backlog-request-article (group number buffer)
16977   (when (numberp number)
16978     (gnus-backlog-setup)
16979     (let ((ident (intern (concat group ":" (int-to-string number))
16980                          gnus-backlog-hashtb))
16981           beg end)
16982       (when (memq ident gnus-backlog-articles)
16983         ;; It was in the backlog.
16984         (save-excursion
16985           (set-buffer (gnus-backlog-buffer))
16986           (if (not (setq beg (text-property-any
16987                               (point-min) (point-max) 'gnus-backlog
16988                               ident)))
16989               ;; It wasn't in the backlog after all.
16990               (ignore
16991                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16992             ;; Find the end (i. e., the beginning of the next article).
16993             (setq end
16994                   (next-single-property-change
16995                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16996         (let ((buffer-read-only nil))
16997           (erase-buffer)
16998           (insert-buffer-substring gnus-backlog-buffer beg end)
16999           t)))))
17000
17001 ;; Allow redefinition of Gnus functions.
17002
17003 (gnus-ems-redefine)
17004
17005 (provide 'gnus)
17006
17007 ;;; gnus.el ends here